Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • scoreplot: plot score variables across different groups with circle or ellipse

    Dear Stata users,

    I want to encircle different groups of scoreplot after executing pca command. Is there a way to achieve this in Stata like in R or Python? see Clusters: https://r-statistics.co/Top50-Ggplot....html#Clusters and https://statisticsglobe.com/draw-ell...t-groups-pca-r
    Code:
    webuse iris
    pca seplen sepwid petlen petwid
    predict f1 f2, score
    twoway scatter f2 f1 if iris==1 || scatter f2 f1 if iris==2 || scatter f2 f1 if iris==3, legend(order(1 "setosa" 2 "versicolor" 3 "virginica"))
    Click image for larger version

Name:	iris.png
Views:	1
Size:	151.5 KB
ID:	1774614

  • #2
    The appropriate search term here is 'convex hull.' See https://www.statalist.org/forums/for...-scatter-plots.

    Comment


    • #3
      This might not be exactly what you want, but here's a first pass using cvxhull (originally from Stata Technical Bulletin I believe).

      See
      https://www.statalist.org/forums/for...-scatter-plots

      Code:
      clear all
      
      webuse iris
      pca seplen sepwid petlen petwid
      predict f1 f2, score
      
      cvxhull f1 f2 , group(iris) noreport hull(1)
      
      
       twoway rarea _cvxh1l _cvxh1r f2 if iris == 1, lwidth(none) color(orange%20) sort || scatter  f1 f2 if iris== 1 , mcolor(orange)         || ///
              rarea _cvxh1l _cvxh1r f2 if iris == 2, lwidth(none) color(red%20)    sort || scatter  f1 f2 if iris== 2,  mcolor(red)            || ///
              rarea _cvxh1l _cvxh1r f2 if iris == 3, lwidth(none) color(blue%20)   sort || scatter  f1 f2 if iris== 3,  mcolor(blue) leg(off)
      Edit: crossed w/ #2

      Comment


      • #4
        See also


        Code:
        SJ-4-3  gr32_1  . Graphing confidence ellipses: An update of ellip for Stata 8
                (help ellip, ellip_dlg if installed)  . . . . . . . . A. Alexandersson
                Q3/04   SJ 4(3):242--256
                ellip command for graphing confidence ellipses updated
                to use Stata 8 graphics and provide new features

        Comment


        • #5
          Thank you very much Andrew Musau Justin Niakamal Nick Cox. I know -cvxhull- and outdated commands -conhull-, -condraw-, -chplot-, see http://www.stata.com/stb/stb23 and http://www.stata.com/stb/stb36
          My further question is how to draw simple convex hull with different colors for different groups? In this case, I need not to draw an rarea plot.
          cvxhull draw with same line color, and code such as
          Code:
          twoway connected _cvxh1l _cvxh1r f2 if iris == 1, sort ...
          fails.
          conhull and condraw can do this work, but they are outdated. Thank you.
          Code:
          webuse iris
          pca seplen sepwid petlen petwid
          predict f1 f2, score
          
          cvxhull f2 f1, group(iris) noreport hull(1)
          
          conhull f1 f2, group(iris)
          condraw f1 f2 conhull, group(iris)
          Click image for larger version

Name:	hull.png
Views:	1
Size:	349.7 KB
ID:	1774640

          Comment


          • #6
            I think you could just go back to the help file for cvxhull to get the commands for what you want.

            Otherwise set the area fill colour to none.

            Code:
            sysuse auto, clear
            ssc install cvxhull
            set scheme s1color 
            cvxhull mpg weight , group(foreign) noreport hull(2)
            sort weight mpg 
            local opts legend(off) aspect(1) yla(, ang(h)) ytitle("`: var label mpg'")
            
            twoway rarea _cvxh1l _cvxh1r weight if foreign, lcolor(orange%20) fcolor(none) sort /// 
            || rarea _cvxh1l _cvxh1r weight if !foreign, lcolor(blue%20) fcolor(none) sort      ///
            || scatter mpg weight if foreign, ms(Oh) mc(orange)                   ///
            || scatter mpg weight if !foreign, ms(+) mc(blue) `opts' name(G1, replace)

            Comment


            • #7
              Thank you very much Nick Cox. I combine condraw (which can generate hv1 hv2 hv3…) with twoway line and get what I want.

              Comment


              • #8
                Picking this all up again and writing something a bit more flexible is among my good intentions, like much else.

                Comment


                • #9
                  Thank you Nick Cox. And I'm happy that this thread draws your attention and arouses your interest after 29 years later. (Can I say that? You wrote -chplot- in 1996, regardless of the recent post on Convex hulls on scatter plots.)
                  Code:
                  sysuse auto, clear
                  set scheme s1color 
                  cvxhull mpg weight , group(foreign) noreport hull(2)
                  sort weight mpg 
                  local opts legend(off) aspect(1) yla(, ang(h)) ytitle("`: var label mpg'")
                  
                  twoway rarea _cvxh1l _cvxh1r weight if foreign, lcolor(orange%20) fcolor(none) sort /// 
                  || rarea _cvxh1l _cvxh1r weight if !foreign, lcolor(blue%20) fcolor(none) sort      ///
                  || scatter mpg weight if foreign, ms(Oh) mc(orange)                   ///
                  || scatter mpg weight if !foreign, ms(+) mc(blue) `opts' name(G1, replace)
                  
                  conhull weight mpg, group(foreign)
                  condraw weight mpg conhull, group(foreign)
                  twoway line hv0 hv1 weight, lcolor(blue%20 orange%20) || scatter mpg weight if foreign, mcolor(orange%20) || scatter mpg weight if !foreign, mcolor(blue%20) `opts' name(G2, replace)
                  
                  graph combine G1 G2, name(G3, replace)
                  Click image for larger version

Name:	G3.png
Views:	1
Size:	219.4 KB
ID:	1774656

                  Comment


                  • #10
                    chplot (Stata Technical Bulletin) was just a wrapper for other commands that did most of the serious work. Similarly my 2019 post here was just building on Allan Reese's command.

                    What I imagine is a unified command that leads directly to outline plots or area plots of convex hulls (user's choice) and allows indicator variables to be generated at will. If someone else does that first, that would be delightful.

                    Comment

                    Working...
                    X