0
votes

I am trying to store the coefficients from a simulated regression in a variable b1 and b2 in the code below, but I'm not quite sure how to go about this. I've tried using return scalar b1 = _b[x1] and return scalar b2 = _b[x2], from the rclass() function, but that didn't work. Then I tried using scalar b1 = e(x1) and scalar b2 = e(x2), from the eclass() function and also wasn't successful.

The goal is to use these stored coefficients to estimate some value (say rhat) and test the standard error of rhat.

Here's my code below:

program montecarlo2, eclass  
    clear  
    version 11  
    drop _all  
    set obs 20  
    gen x1 = rchi2(4) - 4  
    gen x2 = (runiform(1,2) + 3.5)^2  
    gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)  
    gen y = 1.3*x1 + 0.7*x2 + 0.5*u  
* OLS Model  
    regress y x1 x2             
    scalar b1 = e(x1)  
    scalar b2 = e(x2)  
end  

I want to do something like,

rhat = b1 + b2, and then test the standard error of rhat.

1
Quite a lot of fantasy syntax here such as e(x1). But scalar b1 = _b[x1] etc. would work either within the program or after it. It is not clear that you need to declare the program either eclass or rclass if all you want to access the coefficient estimates. Off-topic on CV regardless, so I'm voting to migrate to SO. See help in the CV Help Center on software-specific questions. .Nick Cox
Thanks for the clarification Nick! It works now. But a quick follow up question, I'm trying to store the scalars in a variable, such as, rhat. I'm trying rhat = display b1 + b2, but I get an error saying the command isn't recognized. Any suggestions on how to address this?peakyblinders
Use the lincom command to get the sum, which you can then use for the hypothesis test. No need to store it as a variable.dimitriy

1 Answers

0
votes

Let's hack a bit at your program:

Version 1

program montecarlo2
    clear  
    version 11  
    set obs 20  
    gen x1 = rchi2(4) - 4  
    gen x2 = (runiform(1,2) + 3.5)^2  
    gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)  
    gen y = 1.3*x1 + 0.7*x2 + 0.5*u  
* OLS Model  
    regress y x1 x2             
end  

I cut drop _all as unnecessary given the clear. I cut the eclass. One reason for doing that is the regress will leave e-class results in its wake any way. Also, you can if you wish add

scalar b1 = _b[x1] 
scalar b2 = _b[x2] 
scalar r = b1 + b2 

either within the program after the regress or immediately after the program runs.

Version 2

program montecarlo2, eclass 
    clear  
    version 11  
    set obs 20  
    gen x1 = rchi2(4) - 4  
    gen x2 = (runiform(1,2) + 3.5)^2  
    gen u = 0.3*rnormal(0,25) + 0.7*rnormal(0,5)  
    gen y = 1.3*x1 + 0.7*x2 + 0.5*u  
* OLS Model  
    regress y x1 x2         
    * stuff to add    
end  

Again, I cut drop _all as unnecessary given the clear. Now the declaration eclass is double-edged. It gives the programmer scope for their program to save e-class results, but you have to say what they will be. That's the stuff to add indicated by a comment above.

Warning: I've tested none of this. I am not addressing the wider context. @Dimitriy V. Masterov's suggestion of lincom is likely to be a really good idea for whatever your problem is.