Monte Carlo Simulations using Stata
Oklahoma State University州立大学的Lee C. Adkins教授的课件,
主要内容包括:
Introduction
Software Choices
two methods used for simulation in Stata.
some examples from the paper.
compare speed of simulate, postfile, and gretl.
Paper Examples
pdf文件里面有书签,
为减轻网站流量负担,请各位战友根据论坛币下载。
祝各位战友学习愉快!!!
附件里的code如下:
replace x = theta*L.x + rnormal() in 2
replace u = rho*L.u + rnormal(0,sigma) in 2
replace y = beta*x+delta*L.y + u in 2
reg y x /* b1 */
reg L(0/1).y x /* b2 */
prais L(0/1).y x /* b3 */
reg L(0/2).y L(0/1).x /* b4 */
program regIV, rclass
tempname sim
postfile `sim' gam r2 b biv using results, replace
quietly {
foreach gam of numlist 0.025 0.0375 0.05 0.1 0.15 {
forvalues i = 1/$nmc {
replace u = rnormal()
replace x = `gam'*z+rho*u+rnormal(0,sige)
replace y = slope*x + u
....
}
regIV
use results, clear
by gam, sort: summarize r2 F biv b
by gam, sort: summarize p_ls p_iv
by gam: egen Fbar = mean(F) /* Avg F by gamma */
by gam: egen tslsbar = mean(biv) /* Avg biv by gamma */
by gam: egen olsbar = mean(b) /* Avg b by gamma */
by gam: egen r2bar = mean(r2) /* Avg r2 by gamma */
gen tsls_bias = tslsbar-1 /* IV bias */
gen ols_bias = olsbar-1 /* OLS bias */
gen relb = tsls_bias/ols_bias /* relative bias */
gen rot = 1/(Fbar-1) /* rule of thumb */
gen t = _n /* observation numbers */
keep if mod(t,1000) == 0 /* keep 1 obs per design */
reg relb c.rot##c.rot, noconst /* rel. bias onto rot */
reg relb rot, noconst
test (rot=-1) /* directly proportional? */
}
}
postclose `sim'
end
# Set the sample size and save it in n
nulldata 100
scalar n = $nobs
set seed 3213799
# Set the values of the parameters
scalar slope = 10
scalar sigma = 20
scalar delta = .7
scalar rho = .9
# initialize variables
series u = normal()
series y = normal()
series x = uniform()
loop 400 --progressive --quiet
series e = normal(0,sigma)
series u=rho*u(-1)+e
series y = slope*x + delta*y(-1) + u
ols y const x y(-1)
ols y const x
ar 1; y const x y(-1)
ols y const x y(-1) x(-1) y(-2)
endloop