Search code examples
matrixregressionstata

Creating a matrix using stored regression estimates in Stata


I am performing an event study in Stata with the following specification:

reghdfe numPixelsLost tee_1##db_1 tee_2##db_1  tee_3##db_1  tee_4##db_1 tee_5##db_1 tee_6##db_1  tee_7##db_1  tee_8##db_1 tee_9##db_1 tee_10##db_1  if biome==6, absorb(year case_id) vce(cluster case_id) 

When I inspect the full results, I can see that Stata estimated coefficients and standard errors for all of my variables of interest (the interaction terms).

Then I tried to use a loop to take this information and store it as matrices:

forvalues j = 1/10 {
matrix co1`j' = [_b[1.tee_`j'#1.db_1], _b[1.tee_`j'#1.db_1] - 1.96*_se[1.tee_`j'#1.db_1], _b[1.tee_`j'#1.db_1] + 1.96*_se[1.tee_`j'#1.db_1]]
}

Then I then try to stitch all of these matrices together into one matrix (that I can use to make graphs) like this:

matrix all1 =  [co11 \ co12 \ co13 \ co14 \ col5 \ col6 \ col7 \ col8 \ col9 \ col10]

But Stata will not produce the matrix because it claims some of the vectors are "not found". That is odd since all of the coefficients are estimated. I'd like help understanding why the loop does not seem to recognize estimated coefficients and how to produce the matrix.


Solution

  • It looks like your problem was solved, but I will add a solution that would take advantage of the stored results from reghdfe and some linear algebra. Also note that you are using 1.96 to construct your confidence intervals which, depending on the number of observations and regressors, may not be a good approximation for the underlying t-distribution.

    sysuse auto, clear
    reghdfe price weight length, absorb(rep78)
    
    * SET T stat
    global sig= 0.95 
    scalar _df = e(df_r) // df scalar
    scalar _t = abs(invt(_df,(1 - ${sig})/2)) // t stat scalar
    
    * SET BETA AND SE MATRICES
    local names : colfullnames e(b)
    matrix B = (e(b))'
    mata st_matrix("SD_matrix",sqrt(diagonal(st_matrix("e(V)"))))
    
    * IDENTITY MATRIX FOR CONFORMABILITY
    matrix I = I(`= rowsof(B)')
    
    * MAKE Confidence interval MATRICES
    matrix CI_low = B - _t*I*SD_matrix
    matrix CI_high = B + _t*I*SD_matrix
    
    * Final Results
    matrix all1 = [B' \ CI_low' \ CI_high']
    matrix rownames all1
    
    
                     weight      length       _cons
           Beta   5.4783091  -109.50651   10154.617
     CI_.95_Low    3.162337  -187.98821   1617.9601
    CI_.95_High   7.7942812   -31.02482   18691.274
    

    This will allow you to recreate the matrix if you alter the number of regressors.