Announcement

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

  • Plotting the interaction of a categorical variable with a quadratic effect (graph3d)

    Hi,

    I am trying to plot the interaction of a categorical variable with two other variables that have a quadratic effect on the dependent variable.
    In my mind, that would give me two beautiful parabolas.
    I am trying to adapt the code in for graph3d, with no success (found here: http://davud.rostam-afschar.de/graph...inb_example.do)
    Ideas are welcome!

    See a mock example:

    Code:
    * http://davud.rostam-afschar.de/graph3d/graph3d_zinb_example.do
    set more off
    /* load data */
    clear
    sysuse auto
    
    /* estimate model: CHANGE MODEL HERE */
    reg price i.foreign##c.mpg##c.mpg i.foreign##c.length##c.length
    
    /* specify values of x and y at which margins are calculated: CHANGE VALUES HERE */
    local xfrom 15
    local xto   40    
    local xstep 5
    
    local yfrom 140
    local yto   230
    local ystep 10
    
    /* post margins in matrix e(b) at specified values of the x and y variables: CHANGE VARIABLE FOR MARGINAL EFFECT HERE */
    margins i.foreign, at(mpg=(`xfrom'(`xstep')`xto') length=(`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) xlang(90) ylang(30) zlang(-12) xlpos(9) ylpos(4) zlpos(12)
    *graph export "car_prices.eps", replace

  • #2
    I was able to progress, but not there yet. Ideas?

    Code:
    set more off
    /* load data */
    clear
    sysuse auto
    
    set matsize 11000
    center price mpg length, standardize replace
    
    /* estimate model: CHANGE MODEL HERE */
    reg price i.foreign##c.mpg##c.mpg i.foreign##c.length##c.length
    
    /* specify values of x and y at which margins are calculated: CHANGE VALUES HERE */
    local xfrom -1
    local xto   1    
    local xstep .1
    
    local yfrom -1
    local yto   1
    local ystep .1
    
    /* post margins in matrix e(b) at specified values of the x and y variables: CHANGE VARIABLE FOR MARGINAL EFFECT HERE */
    margins i.foreign, at(mpg=(`xfrom'(`xstep')`xto') length=(`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 = .
    gen m = .
    
    local pos=0
    forv lx = `xfrom'(`xstep')`xto' {
        forv ly = `yfrom'(`ystep')`yto' {
            forv lm =0(1)1 {
                local pos=`pos'+1
                replace x = `lx' in `pos'
                replace y = `ly' in `pos'
                replace m = `lm' in `pos'
            }
        }
    }
    
    /* plot variables x, y, and z: CHANGE OPTIONS HERE */
    graph3d x y z m, ///
        ///xangle(88) yangle(179) zangle(45) xcam(-30) zcam(200) ///
        aspectratio(1) ///
        mark cuboid innergrid blv perspective colorscheme(bcgyr) ///
        ////xlang(90) ylang(30) zlang(-12) xlpos(9) ylpos(4) zlpos(12) ///
        xang(15) yang(80) ///
        xlab("mpg") ylab("length") zlab("price") ///
        xlang(315) ylang(90) zlang(0) ///
        xlpos(11) ylpos(3) zlpos(11) ///
        markeroptions(msize(tiny))
    *graph export "car_prices.eps", replace

    Comment


    • #3
      Hi,
      I believe I found the problem. Looking at the examples from help(graph3d), I realized that the response variable is supposed to be *y*, not *z*. Therefore, the following code does the trick:

      Code:
      set more off
      /* load data */
      clear
      sysuse auto
      
      set matsize 11000
      center price mpg length, standardize replace
      
      /* estimate model: CHANGE MODEL HERE */
      reg c_price i.foreign##c.c_mpg##c.c_mpg i.foreign##c.c_length##c.c_length
      
      summ price foreign mpg length
      
      /* specify values of x and y at which margins are calculated: CHANGE VALUES HERE */
      local xfrom -1
      local xto   1    
      local xstep .1
      
      local zfrom -1
      local zto   1
      local zstep .1
      
      /* post margins in matrix e(b) at specified values of the x and y variables: CHANGE VARIABLE FOR MARGINAL EFFECT HERE */
      margins i.foreign, at(c_mpg=(`xfrom'(`xstep')`xto') c_length=(`zfrom'(`zstep')`zto')) post
      
      /* construct variable z */
      mat b = e(b)
      mat mgns = b[1...,1...]'
      svmat double mgns, name(mgn)
      ren mgn1 y
      replace y=y*1 /* scale z variable to make it more visible: CHANGE SCALING HERE */
      summ y
      
      /* construct variables x and z -- observe that the response variable is y (not z) */
      gen x = .
      gen z = .
      gen m = .
      
      local pos=1
      forv lx = `xfrom'(`xstep')`xto' {
          forv lz = `zfrom'(`zstep')`zto' {
              forv lm =0(1)1 {
                  local lx = round(`lx',.0001)
                  local lz = round(`lz',.0001)
                  qui replace x = `lx' in `pos'
                  qui replace z = `lz' in `pos'
                  qui replace m = `lm' in `pos'
                  local pos=`pos'+1
              }
          }
      }
      
      /* plot variables x, y, and z: CHANGE OPTIONS HERE */
      graph3d x y z m, ///
          ///xangle(88) yangle(179) zangle(45) xcam(-30) zcam(200) ///
          aspectratio(1) ///
          mark cuboid innergrid blv perspective colorscheme(cr) ///
          /// xlang(90) ylang(30) zlang(-12) xlpos(9) ylpos(4) zlpos(12) ///
          /// xang(15) yang(80) ///
          xlab("mpg") ylab("length") zlab("price") ///
          /// xlang(315) ylang(90) zlang(0) ///
          /// xlpos(11) ylpos(3) zlpos(11) ///
          markeroptions(msize(tiny))
      graph export "car_prices.png", replace
      The output plot looks like this:
      Click image for larger version

Name:	car_prices.png
Views:	2
Size:	186.9 KB
ID:	1514274

      Comment

      Working...
      X