Announcement

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

  • new Stata ado-file GRAPH3D: Stata module to draw colored, scalable, rotatable 3D plots

    Hello Statalist,

    please note that there is a new ado to produce three-dimensional scatter plots available via ssc (written by Robin Jessen and myself).

    Type
    Code:
    ssc install graph3d
    Here you find a short demo video showing a rotating sphere. The video was produced with graph3d and ffmpeg. You can find the code here.

    Here is a still example. You find more here.
    Last edited by Davud Rostam-Afschar; 05 Nov 2014, 14:26.

  • #2
    Does that work for margins plot?

    Comment


    • #3
      Yes. In fact, it works for every kind of data that can be put into the form of three variables x,y,z, or even four variables x,y,z,a (see graph3d help file).

      For your case, the following example might be helpful:
      Code:
      clear
      webuse nhanes2
      
      regress bpsystol agegrp##c.bmi
      margins agegrp, at(bmi=(10(10)70)) post
      mat b = e(b)
      mat mgns = b[1...,1...]'
      svmat double mgns, name(mgn)
      gen x = .
      gen y = .
      ren mgn1 z
      
      levelsof agegrp, local(agegrplevels)
          forv l = 10(10)70 {
                  foreach m of local agegrplevels {
                  replace x = `l' in `=`m'+`l'/10*6-6'
                  replace y = `m' in `=`m'+`l'/10*6-6'
              }
          }
          
      graph3d x y z, xangle(0) yangle(75) zangle(3) xcam(10) zcam(400) cuboid innergrid blv perspective colorscheme(bcgyr) xlab("Margin") ylab("Age Group") zlab("Body Mass Index") xlang(270) ylang(84) zlang(0) xlpos(3) ylpos(3) zlpos(11)

      Comment


      • #4
        Here is an economics application of how graph3d works in practice from the forthcoming article "Getting the poor to work: three welfare increasing reforms for a busy Germany" writen by Robin Jessen, Viktor Steiner, and myself:
        Click image for larger version

Name:	budget.jpg
Views:	1
Size:	683.4 KB
ID:	1366340

        Comment


        • #5
          Hi Davud,

          is it possible to margins plot quadratic interactions,too?

          If yes, how would the code look like?

          Thanks a lot!

          Giuseppe

          Comment


          • #6
            Update to post #4:
            In response to frequent demands, I posted the code and the data on the graph3d website. Scroll down to example 14 to find the download link. Note that we used a modified version of the program graph3d which is included in the zip file.

            Comment


            • #7
              Originally posted by Giuseppe Criaco View Post
              Hi Davud,

              is it possible to margins plot quadratic interactions,too?

              If yes, how would the code look like?

              Thanks a lot!

              Giuseppe
              Sure! Here is some code using the example of a zero-inflated Poisson regression model:

              Code:
              /* load data */
              set more off
              use "http://www.ats.ucla.edu/stat/stata/notes/lahigh", clear
              
              /* estimate model: CHANGE MODEL HERE */
              zip daysabs gender##c.langnce mathnce c.mathnce#c.mathnce c.langnce#c.mathnce, inflate(mathnce langnce) vuong
              
              /* specify values of x and y at which margins are calculated: CHANGE VALUES HERE */
              local xfrom 20
              local xto   100    
              local xstep 5
              
              local yfrom 20
              local yto   100
              local ystep 5
              
              /* post margins in matrix e(b) at specified values of the x and y variables: CHANGE VARIABLES FOR MARGINAL EFFECT HERE (This is just for illustration, please make sure that your marginal effects can be interpreted in a sensible way!) */
              margins, dydx(mathnce) at(langnce=(`xfrom'(`xstep')`xto') mathnce=(`yfrom'(`ystep')`yto')) post
              
              /* construct variable z */
              mat b = e(b)
              mat mgns = b[1...,1...]'
              svmat double mgns, name(mgn)
              ren mgn1 z
              replace z=z*10 /* scale z variable to make it more visible: CHANGE SCALING HERE */
              
              /* construct variables x and y */
              gen x = .
              gen y = .
              
              forv lx = `xfrom'(`xstep')`xto' {
                  forv ly = `yfrom'(`ystep')`yto' {
                          replace x = `lx' in `=int((`ly'-`yfrom')/`ystep'+1+((`yto'-`yfrom')/`ystep'+1)*((`lx'-`xfrom')/`xstep'))'
                          replace y = `ly' in `=int((`ly'-`yfrom')/`ystep'+1+((`yto'-`yfrom')/`ystep'+1)*((`lx'-`xfrom')/`xstep'))'
                  }
              }
              
              /* plot variables x, y, and z: CHANGE OPTIONS HERE */
              graph3d x y z, xangle(88) yangle(179) zangle(45) xcam(-30) zcam(200) mark cuboid innergrid blv perspective colorscheme(bcgyr) xlab("") ylab("math standardized test scores {&rarr}") zlab("{&larr} language std. test scores") xlang(90) ylang(30) zlang(-12) xlpos(9) ylpos(4) zlpos(12)
              graph export "Effect_on_days_absent_during_school_year.eps", replace
              The result looks like this:
              Click image for larger version

Name:	zip.jpg
Views:	1
Size:	64.9 KB
ID:	1367642

              Comment


              • #8
                Dear Davud,

                this is great! Thank you very much for your very help.

                All the best,

                Giuseppe

                Comment


                • #9
                  Dear,

                  I am using Quantile regression in stata (attached is the data and code file). I am able to get the 2D graph but i want to have 3D graph as given in "3D grpah example".

                  Can you please help me to make the codes accordingly?

                  Thanks

                  Attached Files

                  Comment

                  Working...
                  X