Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: saving simulation results (was: Any capability)


From   Gary Longton <[email protected]>
To   [email protected]
Subject   st: Re: saving simulation results (was: Any capability)
Date   Fri, 10 Sep 2004 10:46:20 -0700

victor michael zammit wrote:

Dear Statalist,
given a  do-file ,such as the one that follows , is there any capability to
save  the valid  variables before the program gets to  the next  < set obs
`1' > ,and thus be able to find the relevant variables again after the
program is through ?
Victor included code for a program which saved the mean and sd from 1000 simulated samples, sample size given as an argument, drawn from ~N(mu=50,sigma=10).

Though it is not clear what you mean by "valid variables", I'm guessing that you want to save a file with the *results* for each set of simulations rather than variables generated for the actual samples.

See the -postfile- commands ([P]postfile) for an easy way to write results from each simulation to a file. This should speed things up substantially in addition to making for a tidier solution; you will not need to save and merge results from each simulation.

Though I prefer to use the -postfile- commands, see [R]simulate for an even easier and higher-level solution.

Here's a re-write which uses the -postfile- command to save results in the specified result file, and includes arguments for the sample size (as before), number of simulations, Normal distn parameters, and the result file name.

******************
cap pro drop meansd
pro def meansd
version 8.2
args nobs nsim mu sigma resfile
clear
set obs `nobs'

tempname pfile
postfile `pfile' mean sd simno using `resfile', replace

qui gen x = .
forvalues i = 1/`nsim' {
qui replace x = `mu' + `sigma'*invnorm(uniform())
qui sum x
post `pfile' (r(mean)) (r(sd)) (`i')
}
postclose `pfile'
end
**********************

meansd 100 1000 50 10 result1
meansd 200 1000 50 10 result2
meansd 300 1000 50 10 result3
meansd 400 1000 50 10 result4

use result1, clear
l if mean>49.9 & mean<50.1 & sd>9.9 & sd<10.1

etc.
-------------------------------------------------------
Alternatively, a solution using -simul-:

***********************
cap pro drop meansd
pro def meansd, rclass
version 8.2
args nobs mu sigma
drop _all
set obs `nobs'
gen x = `mu' + `sigma'*invnorm(uniform())
sum x
return scalar mean = r(mean)
return scalar sd = r(sd)
end
**********************

simul "meansd 100 50 10" mean=r(mean) sd=r(sd), reps(1000) saving(result1) replace

simul "meansd 200 50 10" mean=r(mean) sd=r(sd), reps(1000) saving(result2) replace

simul "meansd 300 50 10" mean=r(mean) sd=r(sd), reps(1000) saving(result3) replace

simul "meansd 400 50 10" mean=r(mean) sd=r(sd), reps(1000) saving(result4) replace

forvalues i = 1/4 {
use result`i', clear
l if mean>49.9 & mean<50.1 & sd>9.9 & sd<10.1
}

-----------
- Gary



*
* 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