Bookmark and Share

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

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

Re: st: Saving simulation runs

From   John Antonakis <[email protected]>
To   [email protected]
Subject   Re: st: Saving simulation runs
Date   Sun, 10 Oct 2010 00:20:32 +0200

OK....thanks Stas!

Have a good weekend,


Prof. John Antonakis, Associate Dean
Faculty of Business and Economics (HEC)
Department of Organizational Behavior
University of Lausanne
Internef #618
CH-1015 Lausanne-Dorigny

Tel ++41 (0)21 692-3438
Fax ++41 (0)21 692-3305

Home page:

On 09.10.2010 16:12, Stas Kolenikov wrote:
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).

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

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'

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

forvalues r=1/`reps' {
    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.

*   For searches and help try:

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