Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: Saving simulation runs


From   Stas Kolenikov <[email protected]>
To   [email protected]
Subject   Re: st: Saving simulation runs
Date   Sat, 9 Oct 2010 09:12:49 -0500

On Sat, Oct 9, 2010 at 5:41 AM, John Antonakis <[email protected]> wrote:
> My code here, which seemed to work fine before, stopped working (I am sure
> that I did not change it, though can't find anything wrong with it).
>
> ***************************************
> clear
> capture program drop sim
>  version 11.1
> program define sim, rclass
>        drop _all
> syntax , nobs(integer )
> set obs `nobs'
>
> gen x1 = rnormal()
> gen x2 = 2*x1 + .1*rnormal()
> gen y = x1 + x2 + rnormal()
> reg y x1 x2
> end
>
> mat b=e(b)

* if there were no estimation results in the memory, this would break down
* with an error message "estimates not found"

> mat save = b

* and if that is the case, this will break down with error message
* "matrix b not found"

> foreach nobs of numlist 100(100)1000 {
> simulate _b _se, reps(100) seed (123) : sim, nobs(`nobs')
> mat b=e(b)
> mat save = save \ b
> }
>
> mat list save
> ***************************************
>
> Also, how would I save the mean of the standard errors, along with the mean
> of the coefficients, from each of the monte carlo runs?

Collecting results in matrices is cumbersome. Looks like a port of
code from R or Matlab. Stata has an appropriate mechanics for
simulations with -post-. Read the help file, you can post anything you
like. I usually collect the expressions I need like:

program define PreparePost, rclass
   * receives the data in memory
   reg y x1 x2
   foreach x in _cons x1 x2 {
      local temp _b[`x']
      local topost `topost' (`temp')
      local temp _se[`x']
      local topost `topost' (`temp')
   }
   return local `topost'
end

and then within my simulation code, I would have something like

forvalues r=1/`reps' {
   PrepareData
   PreparePost
   post <handle> `r(topost)'
}

Computing means is a post-simulation task, but you can add a few lines
to the -PreparePost- if you need to.

-- 
Stas Kolenikov, also found at http://stas.kolenikov.name
Small print: I use this email account for mailing lists only.

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index