Hi, guys,
I am trying to run a code as follows using bootstrap. The program "corand" is defined by myself. Why it always shows an error information that "literated ambiguous abbreviation
an error occurred when bootstrap executed corand, posting missing values"?
The problem should be lying in the first part before "predict X_b,xb" coz when I take this part out of the program, everything would be fine. Is there any restrictions on changing dataset when we use bootstrap command in Stata? Thanks so so much!
I am trying to run a code as follows using bootstrap. The program "corand" is defined by myself. Why it always shows an error information that "literated ambiguous abbreviation
an error occurred when bootstrap executed corand, posting missing values"?
The problem should be lying in the first part before "predict X_b,xb" coz when I take this part out of the program, everything would be fine. Is there any restrictions on changing dataset when we use bootstrap command in Stata? Thanks so so much!
/* For Compliers */
program corand, rclass
version 13.0
/* Get beta_hat */
foreach var of varlist mig_either fc_age preschild matime eduy f_eduy age f_age literated f_literated prefect1-prefect247 { /* Calculate E(Y|Z) and E(X|Z) */
sum `var' if fc_sex==0 & sc_sex==0 & numchi_hh==2
gen E_`var' = `r(mean)' if fc_sex==0 & sc_sex==0 & numchi_hh==2 /* For z=0,b=2,c=2 */
sum `var' if fc_sex==0 & sc_sex==1 & numchi_hh==2
replace E_`var' = `r(mean)' if fc_sex==0 & sc_sex==1 & numchi_hh==2 /* For z=0,b=1,c=2 */
sum `var' if fc_sex==1 & sc_sex==0 & numchi_hh==2
replace E_`var' = `r(mean)' if fc_sex==1 & sc_sex==0 & numchi_hh==2 /* For z=1,b=1,c=2 */
sum `var' if fc_sex==1 & sc_sex==1 & numchi_hh==2
replace E_`var' = `r(mean)' if fc_sex==1 & sc_sex==1 & numchi_hh==2 /* For z=1,b=0,c=2 */
sum `var' if fc_sex==0 & numchi_hh==1
replace E_`var' = `r(mean)' if fc_sex==0 & numchi_hh==1 /* For z=0,b=1,c=1 */
sum `var' if fc_sex==1 & numchi_hh==1
replace E_`var' = `r(mean)' if fc_sex==1 & numchi_hh==1 /* For z=1,b=0,c=1 */
}
foreach var of varlist mig_either fc_age preschild matime eduy f_eduy age f_age literated f_literated prefect1-prefect247 {
gen `var'_r=`var'-E_`var'
}
reg mig_either_r fc_age_r preschild_r matime_r eduy_r f_eduy_r age f_age_r literated_r f_literated_r prefect1_r-prefect247_r,r
drop fc_age_r preschild_r matime_r eduy_r f_eduy_r age f_age_r literated_r f_literated_r prefect1_r-prefect247_r
foreach var of varlist fc_age preschild matime eduy f_eduy age f_age literated f_literated prefect1-prefect247 {
rename `var' `var'_r
}
predict X_b,xb
gen mig_new=mig_either-X_b
count if fc_sex==0 & sc_sex==0 & numchi_hh==2
local n022 = `r(N)'
display `n022'
count if fc_sex==0 & sc_sex==1 & numchi_hh==2
local n012 = `r(N)'
display `n012'
count if fc_sex==1 & sc_sex==0 & numchi_hh==2
local n112 = `r(N)'
display `n112'
count if fc_sex==0 & numchi_hh==1
local n011 = `r(N)'
display `n011'
count if fc_sex==1 & numchi_hh==1
local n101 = `r(N)'
display `n101'
count if fc_sex==1 & sc_sex==1 & numchi_hh==2
local n102 = `r(N)'
display `n102'
sum mig_new if fc_sex==0 & sc_sex==0 & numchi_hh==2
local mig022 = `r(mean)'
display `mig022'
sum mig_new if fc_sex==0 & sc_sex==1 & numchi_hh==2
local mig012 = `r(mean)'
display `mig012'
sum mig_new if fc_sex==1 & sc_sex==0 & numchi_hh==2
local mig112 = `r(mean)'
display `mig112'
sum mig_new if fc_sex==0 & numchi_hh==1
local mig011 = `r(mean)'
display `mig011'
sum mig_new if fc_sex==1 & numchi_hh==1
local mig101 = `r(mean)'
display `mig101'
sum mig_new if fc_sex==1 & sc_sex==1 & numchi_hh==2
local mig102 = `r(mean)'
display `mig102'
sum fc_sex
local P0=1-`r(mean)'
sum sc_sex if at==1 & fc_sex==0
local Pa0=1-`r(mean)'
local nN=`n101'/(1-`P0')
local nC=(`n011'-`P0'*`nN')/`P0'
local nA=(`n022'+`n012')/`P0'
/* Bound */
local Pa1=0.871
local Pc=(`n112'-(1-`P0')*`Pa1'*`nA')/((1-`P0')*`nC')
sum X_b
local xb=`r(mean)'
local BLATEat=`mig022'-`mig012'
local BLATEco=((`mig112'+`xb')*(`Pa1'*`nA'+`Pc'*`nC')-`Pa1'*`nA'*(`mig012'+`xb'))/(`Pc'*`nC')-(((1-`Pa1')*`nA'+(1-`Pc')*`nC')*(`mig102'+`xb')-(1-`Pa1')*`nA'*(2*`mig012'-`mig022'+`xb'))/((1-`Pc')*`nC')
local BLATE=(`BLATEat'*(`nA'+`nN')+`BLATEco'*`nC')/(`nA'+`nC'+`nN')
local LATE=((`Pa1'*`nA'+`Pc'*`nC')*(`mig112'+`xb')-(`Pa1'*`nA'+`Pc'*`nN')*(`mig012'+`xb')-`Pc'*(`nN'+`nC')*(`mig011'+`xb')+`Pc'*`nN'*(`mig02 2'+`mig101'+2*`xb'))/(`Pc'*`nC')
return scalar LATE=`LATE'
return scalar BLATEco=`BLATEco'
return scalar BLATEat=`BLATEat'
return scalar BLATE=`BLATE'
return scalar Pa1=`Pa1'
return scalar Pc=`Pc'
end
bootstrap r(LATE) r(BLATEco) r(BLATEat) r(BLATE) r(Pa1) r(Pc), reps(2000) noisily nodrop: corand
estat bootstrap,all
program drop corand
program corand, rclass
version 13.0
/* Get beta_hat */
foreach var of varlist mig_either fc_age preschild matime eduy f_eduy age f_age literated f_literated prefect1-prefect247 { /* Calculate E(Y|Z) and E(X|Z) */
sum `var' if fc_sex==0 & sc_sex==0 & numchi_hh==2
gen E_`var' = `r(mean)' if fc_sex==0 & sc_sex==0 & numchi_hh==2 /* For z=0,b=2,c=2 */
sum `var' if fc_sex==0 & sc_sex==1 & numchi_hh==2
replace E_`var' = `r(mean)' if fc_sex==0 & sc_sex==1 & numchi_hh==2 /* For z=0,b=1,c=2 */
sum `var' if fc_sex==1 & sc_sex==0 & numchi_hh==2
replace E_`var' = `r(mean)' if fc_sex==1 & sc_sex==0 & numchi_hh==2 /* For z=1,b=1,c=2 */
sum `var' if fc_sex==1 & sc_sex==1 & numchi_hh==2
replace E_`var' = `r(mean)' if fc_sex==1 & sc_sex==1 & numchi_hh==2 /* For z=1,b=0,c=2 */
sum `var' if fc_sex==0 & numchi_hh==1
replace E_`var' = `r(mean)' if fc_sex==0 & numchi_hh==1 /* For z=0,b=1,c=1 */
sum `var' if fc_sex==1 & numchi_hh==1
replace E_`var' = `r(mean)' if fc_sex==1 & numchi_hh==1 /* For z=1,b=0,c=1 */
}
foreach var of varlist mig_either fc_age preschild matime eduy f_eduy age f_age literated f_literated prefect1-prefect247 {
gen `var'_r=`var'-E_`var'
}
reg mig_either_r fc_age_r preschild_r matime_r eduy_r f_eduy_r age f_age_r literated_r f_literated_r prefect1_r-prefect247_r,r
drop fc_age_r preschild_r matime_r eduy_r f_eduy_r age f_age_r literated_r f_literated_r prefect1_r-prefect247_r
foreach var of varlist fc_age preschild matime eduy f_eduy age f_age literated f_literated prefect1-prefect247 {
rename `var' `var'_r
}
predict X_b,xb
gen mig_new=mig_either-X_b
count if fc_sex==0 & sc_sex==0 & numchi_hh==2
local n022 = `r(N)'
display `n022'
count if fc_sex==0 & sc_sex==1 & numchi_hh==2
local n012 = `r(N)'
display `n012'
count if fc_sex==1 & sc_sex==0 & numchi_hh==2
local n112 = `r(N)'
display `n112'
count if fc_sex==0 & numchi_hh==1
local n011 = `r(N)'
display `n011'
count if fc_sex==1 & numchi_hh==1
local n101 = `r(N)'
display `n101'
count if fc_sex==1 & sc_sex==1 & numchi_hh==2
local n102 = `r(N)'
display `n102'
sum mig_new if fc_sex==0 & sc_sex==0 & numchi_hh==2
local mig022 = `r(mean)'
display `mig022'
sum mig_new if fc_sex==0 & sc_sex==1 & numchi_hh==2
local mig012 = `r(mean)'
display `mig012'
sum mig_new if fc_sex==1 & sc_sex==0 & numchi_hh==2
local mig112 = `r(mean)'
display `mig112'
sum mig_new if fc_sex==0 & numchi_hh==1
local mig011 = `r(mean)'
display `mig011'
sum mig_new if fc_sex==1 & numchi_hh==1
local mig101 = `r(mean)'
display `mig101'
sum mig_new if fc_sex==1 & sc_sex==1 & numchi_hh==2
local mig102 = `r(mean)'
display `mig102'
sum fc_sex
local P0=1-`r(mean)'
sum sc_sex if at==1 & fc_sex==0
local Pa0=1-`r(mean)'
local nN=`n101'/(1-`P0')
local nC=(`n011'-`P0'*`nN')/`P0'
local nA=(`n022'+`n012')/`P0'
/* Bound */
local Pa1=0.871
local Pc=(`n112'-(1-`P0')*`Pa1'*`nA')/((1-`P0')*`nC')
sum X_b
local xb=`r(mean)'
local BLATEat=`mig022'-`mig012'
local BLATEco=((`mig112'+`xb')*(`Pa1'*`nA'+`Pc'*`nC')-`Pa1'*`nA'*(`mig012'+`xb'))/(`Pc'*`nC')-(((1-`Pa1')*`nA'+(1-`Pc')*`nC')*(`mig102'+`xb')-(1-`Pa1')*`nA'*(2*`mig012'-`mig022'+`xb'))/((1-`Pc')*`nC')
local BLATE=(`BLATEat'*(`nA'+`nN')+`BLATEco'*`nC')/(`nA'+`nC'+`nN')
local LATE=((`Pa1'*`nA'+`Pc'*`nC')*(`mig112'+`xb')-(`Pa1'*`nA'+`Pc'*`nN')*(`mig012'+`xb')-`Pc'*(`nN'+`nC')*(`mig011'+`xb')+`Pc'*`nN'*(`mig02 2'+`mig101'+2*`xb'))/(`Pc'*`nC')
return scalar LATE=`LATE'
return scalar BLATEco=`BLATEco'
return scalar BLATEat=`BLATEat'
return scalar BLATE=`BLATE'
return scalar Pa1=`Pa1'
return scalar Pc=`Pc'
end
bootstrap r(LATE) r(BLATEco) r(BLATEat) r(BLATE) r(Pa1) r(Pc), reps(2000) noisily nodrop: corand
estat bootstrap,all
program drop corand
Comment