0

I have written the program below and keep getting the error message that my variables are not defined.

Can somebody plese see where the error is and how I should adapt the code? Really nothing seems to work.

program define myreg, rclass
drop all

set obs 200
gen x= 2*uniform()
gen  z  = rnormal(0,1)
gen e = (invnorm(uniform()))^2
e=e-r(mean)
replace e=e-r(mean)
more
gen y = 1 + 1*x +1*z + 1*e
reg y  x z
e=e-r(mean)
replace e=e-r(mean)
more
gen y = 1 + 1*x +1*z + 1*e
reg y x z
more
return scalar b0 =_[_cons]
return scalar b1=_[x]
return scalar b2 =_[z]
more
end

simulate b_0 = r(b0) b_1 = r(b1) b_2 = r(b2), rep(1000): myreg
Falko
  • 17,076
  • 13
  • 60
  • 105
user3387899
  • 601
  • 5
  • 18
  • 1
    `e=e-r(mean)` is illegal here. It is also redundant as the `replace` would do that. But unless you did a `summarize` before running this, there is no `r(mean)` to use so `e` ends up all missing. A stylistic point is that if your Stata allows `rnormal()` it allows `runiform()` and you need not use the older function names for random number generation. – Nick Cox Feb 23 '15 at 18:26
  • 1
    `drop all` drops a variable called `all` if it exists. You may well mean `drop _all`. – Nick Cox Feb 23 '15 at 18:27
  • 1
    The first `regress` in the program is pointless as you never use its results. If you think that `r(mean)` is left behind by `regress` that's quite wrong. – Nick Cox Feb 23 '15 at 18:29
  • 1
    The second `gen` will fail as `y` already exists. – Nick Cox Feb 23 '15 at 18:30
  • This seems related to (e.g.) http://stackoverflow.com/questions/28674482/monte-carlo-study-using-stata http://stackoverflow.com/questions/28674405/impose-constraints-on-variables-in-stata If the same person is behind all, please delete questions of yours if they are no longer current and they are unanswered. It's in no one's interests that the forum is littered with failed threads. – Nick Cox Feb 23 '15 at 18:38

1 Answers1

1
*A possible solution with eclass

capture program drop myreg
program define myreg, eclass 
 * create an empty data by dropping all variables
 drop _all

set obs 200
gen x= 2*uniform()
gen z  = rnormal(0,1)
gen e = (invnorm(uniform()))^2
qui sum e  /*to get r(mean) you need to run sum first*/
replace e=e-r(mean)
gen y = 1 + 1*x +1*z + 1*e
reg y  x z
end
*gather the coefficients (_b) and standard errors (_se) from the *regression each time
simulate _b _se, reps(1000) seed (123): myreg
* show the final result 
mat list  r(table)

* A possible solution with rclass
* To understand the difference between rclass and eclass, see the Stata manual(http://www.stata.com/manuals13/rstoredresults.pdf)

capture program drop myreg
program define myreg, rclass
drop _all

set obs 200
gen x= 2*uniform()
gen z  = rnormal(0,1)
gen e = (invnorm(uniform()))^2
qui sum e
replace e=e-r(mean)
gen y = 1 + 1*x +1*z + 1*e
reg y  x z

mat output=e(b)
return scalar b0=output[1,3]
return scalar b1=output[1,1]
return scalar b2=output[1,2]
end
simulate b_0=r(b0) b_1=r(b1) b_2=r(b2), rep(1000) seed (123): myreg
return list

*P.S. You should read all the comments as suggested by @Nick to fully understand what I did here. .

Metrics
  • 15,172
  • 7
  • 54
  • 83
  • Metrics: This is helpful. I am leaving my comments in place for the moment because they explain some of what is wrong with the OP's code. In the limit, posters may not learn much if you write the program for them. You are welcome to copy any of my comments into your answer; I will delete any so copied. – Nick Cox Feb 23 '15 at 19:09
  • Nick : I don't think you need to delete your comments. Those are valid comments and should be helpful to the OP. – Metrics Feb 23 '15 at 19:15
  • Thanks, but they would go as well or better integrated into your answer. If you wanted to add more comments that would be even better. I am concerned that these threads be more than someone getting personal help in public. To be any use to anybody else, explanations are helpful. – Nick Cox Feb 23 '15 at 19:19
  • Nick: Thank you. I have added some details as per your suggestion. – Metrics Feb 23 '15 at 19:42