Bookmark and Share

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


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: monte carlo simulation


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: monte carlo simulation
Date   Mon, 25 Feb 2013 00:48:53 +0000

You are writing for Stata 10, in which it is possible and simpler to
write -rnormal()- rather than the older nested call. There is no
problem with writing what you did otherwise.

Nick

On Sun, Feb 24, 2013 at 9:38 PM, Benjamin Hong <benjamin1459@gmail.com> wrote:
> Dear Nick,
>
> Thank you very much! and thanks for spelling corrections :)
> Can I ask one more question?
> why did you change "gen u=invnorm(uniform())" to "gen u = rnormal()"?
>
> Thanks!
>
> Best
> Ben
>
> 2013/2/24 Nick Cox <njcoxstata@gmail.com>:
>> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index