Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: monte carlo simulation

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: monte carlo simulation Date Sun, 24 Feb 2013 21:20:36 +0000

```You are trying to call -benreg- from a call to -simulate- _within-
-benreg-. That's not the way to use -simulate-.

I see no -end- statement marking the end of the program definition.

You may have some previous version of the program in memory.

This will work

program benreg, rclass
version 10.0
syntax [, c(real 1)]
clear
set obs 10000
gen u = rnormal()
gen x = rnormal()
gen y = 2 + 3*x^3+u
sum
reg y x
return scalar b0=_b[_cons]
return scalar b1=_b[x]
end

simulate b_0=r(b0) b_1=r(b1), reps(10000): benreg

Standard spelling is Monte Carlo (a placename).

Nick

On Sun, Feb 24, 2013 at 8:57 PM, Benjamin Hong <benjamin1459@gmail.com> wrote:
> Dear all,
>
> Hi, I am trying to do basic monte carlo simulation with Stata, but for
> some reason it doesnt work.
> Would you tell me the reason?
>
> below is my code
> -------------------
> program define benreg, rclass
> version 10.0
> syntax [, c(real 1)]
> drop _all
> set obs 10000
> gen u=invnorm(uniform())
> gen x=invnorm(uniform())
> gen y = 2 + 3*x^3+u
> reg y x
> sum
> return scalar b0=_b[_cons]
> return scalar b1=_b[x]
> simulate b_0=r(b0) b_1=r(b1), reps(10000): benreg
> ----------------------------
> it always returns "non r-class program may not set r()" after reg y x.
> (before return scalar)
>
> I appreciate if somebody gives me advise!
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/
```