Dear Sir or Madam,
I use monte carlo with stata to generate samples of two variables y and z respectively. I need to report the sample mean and sample variance of the 500 sample means. I am sure my process of generate the two variables y and z is correct. But when I use postfile command and simulate command to repeat it 500 times to report the sample mean and sample variance of the 500 sample means, neither work.
Here is my codes with postfile command:
set seed 123
postfile dm yhat zhat using result, replace
forvalues i=1/500{
clear
mat sigma=[1, 0.2\0.2, 1]
mat m = (0, 0)
drawnorm u v, n(75) means(m) cov(sigma)
generate y=.
generate t=_n
qui tsset t
replace y=u if t==1
replace y=0.23*l.y+u if t>1
mean y
post dm (_b[y])
generate z=.
replace z=v if t==1
replace z=0.23*l.z+v if t>1
mean z
post dm (_c[z])
}
postclose dm
use result, clear
sum, detail
Here is my code with simulate command:
program dm, rclass
drop all
set seed 123
mat sigma=[1, 0.2\0.2, 1]
mat m = (0, 0)
drawnorm u v, n(75) means(m) cov(sigma)
generate y=.
generate t=_n
qui tsset t
replace y=u if t==1
replace y=0.23*l.y+u if t>1
sum y
return scalar yhat=r(mean)
generate z=.
replace z=v if t==1
replace z=0.23*l.z+v if t>1
sum z
return scalar zhat=r(mean)
end
clear
set seed 123
simulate yhat=r(mean) zhat=r(mean), reps(500) nodots: dm
describe *
sum, detail
Could you help me with either of the programs?
Thank you very much for your help!
Best,
Cherry
I use monte carlo with stata to generate samples of two variables y and z respectively. I need to report the sample mean and sample variance of the 500 sample means. I am sure my process of generate the two variables y and z is correct. But when I use postfile command and simulate command to repeat it 500 times to report the sample mean and sample variance of the 500 sample means, neither work.
Here is my codes with postfile command:
set seed 123
postfile dm yhat zhat using result, replace
forvalues i=1/500{
clear
mat sigma=[1, 0.2\0.2, 1]
mat m = (0, 0)
drawnorm u v, n(75) means(m) cov(sigma)
generate y=.
generate t=_n
qui tsset t
replace y=u if t==1
replace y=0.23*l.y+u if t>1
mean y
post dm (_b[y])
generate z=.
replace z=v if t==1
replace z=0.23*l.z+v if t>1
mean z
post dm (_c[z])
}
postclose dm
use result, clear
sum, detail
Here is my code with simulate command:
program dm, rclass
drop all
set seed 123
mat sigma=[1, 0.2\0.2, 1]
mat m = (0, 0)
drawnorm u v, n(75) means(m) cov(sigma)
generate y=.
generate t=_n
qui tsset t
replace y=u if t==1
replace y=0.23*l.y+u if t>1
sum y
return scalar yhat=r(mean)
generate z=.
replace z=v if t==1
replace z=0.23*l.z+v if t>1
sum z
return scalar zhat=r(mean)
end
clear
set seed 123
simulate yhat=r(mean) zhat=r(mean), reps(500) nodots: dm
describe *
sum, detail
Could you help me with either of the programs?
Thank you very much for your help!
Best,
Cherry
Comment