Hello,
I am new to Stata and for a project I have to peform a monte carlo simulation with 500 replications using three different sample sizes, being $N_1$ = 50, $N_2$ = 200 and $N_3$ = 2000
Given: y1 = b0 + b1*y2 + b2*x1 + b2*x2
and y2 = a0 + a1*x1 + a2*x1+a2*x2 + a3*z1 + a4*z2+a5*z3+a6*z6
where y2 is the endogenious variable.
Below is what I have programmed so far. The problem is that I keep getting the error "no observations". Though, I really do not see where the problem is as I have almost no experience in Stata. Can somebody please help me? Many thanks in advance!
tempfile mc30 mc100 mc200 mc1600
foreach n in 50 200 2000 {
local j = 1
while `j' <= 500 {
quietly {
preserve
sample `n', count
noisily di in yellow "." _continue
gen x1= 2*uniform()
gen x2 = rnormal(0,1)
gen z1 = rnormal(4,10)
gen z2 = (invnorm(uniform()))^2
gen z3 = rnormal(2,3)
gen z4 = rnormal(2,3)
gen e = rnormal(0,1)
gen v = rnormal(0,1)
gen y2_50 = 1 + 1*x1 +1*x2+1*z1+1*z2+e
gen y1_50 = 1 + y2_50 + x1 +x2 + v
reg y2_50 x1 x2 z1 z2
reg y1_50 x1 x2 y2_50
matrix b1=e(b)
svmat b1, name(ols) // save the regression coefficients and collapse in
// order to only have one observation per coefficient
collapse (mean) ols*
gen N = `n'
if `j' > 1 {
append using `mc`n''
}
save `mc`n'', replace
restore
local j = `j' + 1
}
}
di "`n'"
}
use `mc50', clear
append using `mc100'
append using `mc200'
append using `mc2000'
// here we append the four mc simulations (each with 1000 observations for B1
// and B2) of four different sample sizes together to compare them in task 2.
I am new to Stata and for a project I have to peform a monte carlo simulation with 500 replications using three different sample sizes, being $N_1$ = 50, $N_2$ = 200 and $N_3$ = 2000
Given: y1 = b0 + b1*y2 + b2*x1 + b2*x2
and y2 = a0 + a1*x1 + a2*x1+a2*x2 + a3*z1 + a4*z2+a5*z3+a6*z6
where y2 is the endogenious variable.
Below is what I have programmed so far. The problem is that I keep getting the error "no observations". Though, I really do not see where the problem is as I have almost no experience in Stata. Can somebody please help me? Many thanks in advance!
tempfile mc30 mc100 mc200 mc1600
foreach n in 50 200 2000 {
local j = 1
while `j' <= 500 {
quietly {
preserve
sample `n', count
noisily di in yellow "." _continue
gen x1= 2*uniform()
gen x2 = rnormal(0,1)
gen z1 = rnormal(4,10)
gen z2 = (invnorm(uniform()))^2
gen z3 = rnormal(2,3)
gen z4 = rnormal(2,3)
gen e = rnormal(0,1)
gen v = rnormal(0,1)
gen y2_50 = 1 + 1*x1 +1*x2+1*z1+1*z2+e
gen y1_50 = 1 + y2_50 + x1 +x2 + v
reg y2_50 x1 x2 z1 z2
reg y1_50 x1 x2 y2_50
matrix b1=e(b)
svmat b1, name(ols) // save the regression coefficients and collapse in
// order to only have one observation per coefficient
collapse (mean) ols*
gen N = `n'
if `j' > 1 {
append using `mc`n''
}
save `mc`n'', replace
restore
local j = `j' + 1
}
}
di "`n'"
}
use `mc50', clear
append using `mc100'
append using `mc200'
append using `mc2000'
// here we append the four mc simulations (each with 1000 observations for B1
// and B2) of four different sample sizes together to compare them in task 2.
Comment