Stata Technical Bulletin
23
There must be the same number of results following the post command as variable names following the global S_1 command.
Example
Make a data set containing means and variances of 100-observation samples from a log-normal distribution. Perform the
experiment 10,000 times:
program define Insim
version 3.1
ɪf h-1-h==h?h {
global S.1 "mean var,
exit
drop .all
set obs 100
gen z = exp(invnorm(unif orm() ) )
summarize z
post .result(3) .result(4)
end
It is instructive to compare this program to the same example in the previous insert. In any case, Insim can then be executed
10,000 times by typing:
. simul Insimt reps(10000)
. describe
Contains data
Obs: 10000 (max= 19997)
Vars: 2 (max= 99)
Width: 8 (max= 200)
1. mean float %9.0g
2. var float %9.0g
Sorted by:
. summarize
Variable ∣ Obs Mean Std. Dev. Min Max
---------+-----------------------------------------------------
mean ∣ 10000 1.648349 .2165937 1.022719 4.280587
var I 10000 4.720659 6.208903 .6215334 450.1076
The simul command took 14.5 minutes on a 25MHz DOS 486 computer.
Technical note: debugging a simulation
Before executing our Insim simulator, we can verify it works by typing
. Insim
. display $S_1
This verifies that the program sets the global macro $S_1 correctly on a query call. We can then try executing lnsim:
. postfile $S_1 using myfile
. Insim
The postfile command opens a file for the posted results (see insert above). Invoking Insim should perform a single simulation.
We could then close the result file and examine it:
. postclos
. use myfile, clear
Passing arguments to the simulation
Consider a more complicated problem: Let’s experiment with estimating
yj = a + bxj + Uj
when the true model has a = 1, b = 1, and Uj = 2(¾ + c¾∙) and Zj is N(0,1). We will keep the parameter estimates and
standard errors and experiment with varying c. Xj will be fixed across the experiments but will originally be generated as N(0,1).
We begin by interactively making the true data: