Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Saving intermediate results (variables) when running -simulate-


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Saving intermediate results (variables) when running -simulate-
Date   Wed, 22 Aug 2007 22:29:23 +0100 (BST)

--- Rachel <[email protected]> wrote:
> I do want to save the dataset, which is randomly generated with each
> repetition.

You can save the datasets as I show below (you will have to save
separate files, otherwise you will run into memory problems when you
try to do a serious simulation). However as I also show below this is
completely unnecesary and a waste of resources. You can exactly
reproduce the random datasets as long as you set the seed.

*------------------ begin example -----------------
capture program drop simprog
program simprog, eclass
    drop _all
    set obs 1000
    gen x=uniform()
    drawnorm resid
    local beta=2
    gen y=`beta'*x+resid
    regress y x
    matrix b = e(b)
    matrix V = e(V)
    local rmse = e(rmse)
    ereturn post b V
    ereturn scalar rmse = `rmse'
    save temp$i, replace
    global i = $i + 1
end

set seed 12345
global i = 1
cd c:\temp
simulate _b _se rmse=e(rmse), reps(20): simprog
list

forvalues i = 1/20 {
    use temp`i', clear
    qui reg y x
    matrix list e(b) 
}

* by setting the seed you can exactly reproduce
* the datasets without saving them
set seed 12345
forvalues i = 1/20 {
    quietly {
        drop _all
        set obs 1000
        gen x=uniform()
        drawnorm resid
        local beta=2
        gen y=`beta'*x+resid
        regress y x
    }
    matrix list e(b)
}
*------------------ end example -------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )

Hope this helps,
Maarten

-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      ___________________________________________________________
Yahoo! Answers - Got a question? Someone out there knows the answer. Try it
now.
http://uk.answers.yahoo.com/ 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index