Announcement

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

  • Creating 3D Graphs in Stata or external application

    Dear all,

    I am fitting a fixed-effects model using xtregar in Stata 13.0 on Win 10, and export the fitted data to a .csv file to create a 3-D graph in an external application (Matlab). The code below worked nicely until it suddenly stopped working. I don't recall having made any changes to the code, but only to the model specification. Yet, I receive a "conformability error" r(503);


    Questions:
    1. Can someone spot/correct the error ?
    2. Are their alternative and possibly easier approaches to creating high(er)-quality 3-D graphs (see example below)? The "surface" library seems rather limited

    Thank you for your help!


    Code:
     eststo: quietly xtregar $dv c.$iv1 c.l.fsu c.$iv1#c.l.fsu c.$iv1#c.$iv1  $ctrl_a , fe
    
    matrix b =e(b)
    margins, atmeans post
    matrix avg = e(at)
    matrix list avg
        
    
    scalar minx = 0.15 // set the minimum value for variable1 
    scalar maxx = 1 // set the maximum value for variable1
    scalar intervalx = 0.05 // set the periodic interval for variable1 marginal estimation
    
    scalar miny = 0 // set the minimum value for variable2 
    scalar maxy = 0.16 // set the maximum value for variable2
    scalar intervaly = 0.01 // set the periodic interval for variable2 marginal estimation
    
    scalar rows = (((abs(minx) + abs(maxx))/intervalx)+1)*(((abs(miny)+ abs(maxy))/intervaly)+1) //set the number of rows for the matrix to be iterated 
    matrix fitted = J(rows ,3,.) //generates the matrix to be iterated 
    
    capture drop i
    gen i = 1
    
        forvalues x = `=minx'(`=intervalx')`=maxx'  {
            forvalues y = `=miny'(`=intervaly')`=maxy' {
    
            forv z = 1(1)2{         // selects the marginal variables from the regression
            matrix b = b[1,`z'..`z'] 
            global name`z': colname(b)
            } 
        
        scalar numbv= colsof(b)-1     // selects the fixed variables from the regression
        matrix b = b[1,3..numbv] 
        global fixedvars: colname(b)
              
        eststo: quietly xtregar $dv c.$iv1 c.l.fsu c.$iv1#c.l.fsu c.$iv1#c.$iv1  $ctrl_a ,  fe
        eststo: quietly  margins, at($name1==(`x') $name2==(`y')) atmeans continuous vsquish post
            
        scalar ypredict = _b[_cons]
        matrix fitted[i,1] = ypredict
        matrix fitted[i,2] = `x'
        matrix fitted[i,3] = `y' 
                    
        replace i = i + 1
        estimates drop _all 
        }
    }
    
    drop i
    
    preserve 
    
    // transforms the matrix into a dataset
    matsave fitted, p("$main_analysis_dir") saving dropall replace
    
    use "fitted.dta"
    export delimited using "3DGrapg.csv", replace
    
    surface  c2 c3 c1, xtitle("Competitor exploration (t)") ytitle("Technological Proximity to Competitors (t)") ztitle("Firm exploration (t)") scheme(s1manual) // generates 3d graph
    
    restore

    3D Graph created in Matlab:
    Click image for larger version

Name:	Picture1.png
Views:	1
Size:	135.8 KB
ID:	1449623
Working...
X