Hello fellow stata-ticians,
I have been working on a cost effectiveness analysis in which I wrote program with xtreg. The next step is to let this program loop over 1000 simulations, and to do that I built a forvalues loop. However, the loop stops right away after the first analysis. What can I do to make it work? (I suspect the bootsrap program (in bold) that ends my loop, i also tried it with removing 'end' after 'restore' but it still does not work. Thanks in advance!
Find below the stata code:
local n = 10
forvalues j=1(1) `n' {
local y = `j'
use Dataset`y', clear
gen ID_dataset = `y'
* step 1: Produce initial estimates and store in Matrix observe!
quietly xtreg LROT trtGrp, i(idHospital) mle
mat betas = e(b)
scalar betaEFFECT = betas[1,1]
quietly xtreg Costs trtGrp, i(idHospital) mle
mat betas = e(b)
scalar betaCOST = betas[1,1]
matrix observe = (betaEFFECT, betaCOST)
*Step 2: Write bootstrap program myboot --> Bootstrap (bsample)
capture program drop myboot
program define myboot, rclass
preserve
bsample, str(idHospital)
xtreg LROT trtGrp, i(idHospital) mle
mat betasE = e(b)
return scalar betaEFFECT = betasE[1,1]
xtreg Costs trtGrp, i(idHospital) mle
mat betasC = e(b)
return scalar betaCOST = betasC[1,1]
restore
end
* Step 3: simulate with monte carlo simulations!
simulate BETAEFFECT = r(betaEFFECT) BETACOST = r(betaCOST), reps(50) saving(bootsamples_imp`y', replace) seed(30): myboot
*Step 4: give bootstrap results!
bstat, stat(observe) n(2664)
estat bootstrap, all
*** Extract data from bivariate regression analysis
mat betaCE= e(b) /* extract the matrix of regression coefficients */
mat se = e(se) /* extract standard errors */
mat vari = e(V) /* extract the variance-covariance matrix within-imputation variability (uncertainty per imp) and between-imputation variability (uncertainty due to missing information) */
mat limits = e(ci_bc) /* extract lower en upper limit bc interval */
gen CD = betaCE[1,colnumb(betaCE,"BETACOST")] /* create differential costs */
gen ED = betaCE[1,colnumb(betaCE,"BETAEFFECT")] /* create differential EFFECTS */
gen varCD = vari[colnumb(vari,"BETACOST"),rownumb(vari,"BETACOST")] /* extract the variance of the mean differential costs from the VC matrix */
gen varED = vari[colnumb(vari,"BETAEFFECT"),rownumb(vari,"BETAEFFEC T")] /* extract the variance of the mean differential EFFECTS from the VC matrix */
gen cov = vari[colnumb(vari,"BETACOST"),rownumb(vari,"BETAEFFECT" )] /* extract the covariance between mean differential costs and EFFECTS */
gen N = e(N) /* extract sample size GECHECKT */
gen cor = cov /(varED*varCD) /* calculate the correlation coefficient, UITLEG: "[COVARIANCE MEAN DIFFERENTAL COSTS AND EFFECTS] / ([variance of the mean differential COSTS])* ([variance of the mean differential EFFECTS]) */
gen LLcost = limits[1, colnumb(limits,"BETACOST")]
gen UPcost = limits[2, colnumb(limits,"BETACOST")]
gen LLeffect = limits[1, colnumb(limits,"BETAEFFECT")]
gen UPeffect = limits[2, colnumb(limits,"BETAEFFECT")]
gen SE_CD = se[1,1]
gen SE_ED = se[1,2]
save postBoots_samples_imp`y', replace
}
I have been working on a cost effectiveness analysis in which I wrote program with xtreg. The next step is to let this program loop over 1000 simulations, and to do that I built a forvalues loop. However, the loop stops right away after the first analysis. What can I do to make it work? (I suspect the bootsrap program (in bold) that ends my loop, i also tried it with removing 'end' after 'restore' but it still does not work. Thanks in advance!
Find below the stata code:
local n = 10
forvalues j=1(1) `n' {
local y = `j'
use Dataset`y', clear
gen ID_dataset = `y'
* step 1: Produce initial estimates and store in Matrix observe!
quietly xtreg LROT trtGrp, i(idHospital) mle
mat betas = e(b)
scalar betaEFFECT = betas[1,1]
quietly xtreg Costs trtGrp, i(idHospital) mle
mat betas = e(b)
scalar betaCOST = betas[1,1]
matrix observe = (betaEFFECT, betaCOST)
*Step 2: Write bootstrap program myboot --> Bootstrap (bsample)
capture program drop myboot
program define myboot, rclass
preserve
bsample, str(idHospital)
xtreg LROT trtGrp, i(idHospital) mle
mat betasE = e(b)
return scalar betaEFFECT = betasE[1,1]
xtreg Costs trtGrp, i(idHospital) mle
mat betasC = e(b)
return scalar betaCOST = betasC[1,1]
restore
end
* Step 3: simulate with monte carlo simulations!
simulate BETAEFFECT = r(betaEFFECT) BETACOST = r(betaCOST), reps(50) saving(bootsamples_imp`y', replace) seed(30): myboot
*Step 4: give bootstrap results!
bstat, stat(observe) n(2664)
estat bootstrap, all
*** Extract data from bivariate regression analysis
mat betaCE= e(b) /* extract the matrix of regression coefficients */
mat se = e(se) /* extract standard errors */
mat vari = e(V) /* extract the variance-covariance matrix within-imputation variability (uncertainty per imp) and between-imputation variability (uncertainty due to missing information) */
mat limits = e(ci_bc) /* extract lower en upper limit bc interval */
gen CD = betaCE[1,colnumb(betaCE,"BETACOST")] /* create differential costs */
gen ED = betaCE[1,colnumb(betaCE,"BETAEFFECT")] /* create differential EFFECTS */
gen varCD = vari[colnumb(vari,"BETACOST"),rownumb(vari,"BETACOST")] /* extract the variance of the mean differential costs from the VC matrix */
gen varED = vari[colnumb(vari,"BETAEFFECT"),rownumb(vari,"BETAEFFEC T")] /* extract the variance of the mean differential EFFECTS from the VC matrix */
gen cov = vari[colnumb(vari,"BETACOST"),rownumb(vari,"BETAEFFECT" )] /* extract the covariance between mean differential costs and EFFECTS */
gen N = e(N) /* extract sample size GECHECKT */
gen cor = cov /(varED*varCD) /* calculate the correlation coefficient, UITLEG: "[COVARIANCE MEAN DIFFERENTAL COSTS AND EFFECTS] / ([variance of the mean differential COSTS])* ([variance of the mean differential EFFECTS]) */
gen LLcost = limits[1, colnumb(limits,"BETACOST")]
gen UPcost = limits[2, colnumb(limits,"BETACOST")]
gen LLeffect = limits[1, colnumb(limits,"BETAEFFECT")]
gen UPeffect = limits[2, colnumb(limits,"BETAEFFECT")]
gen SE_CD = se[1,1]
gen SE_ED = se[1,2]
save postBoots_samples_imp`y', replace
}
Comment