Hello,
I'm seeking some general tips on writing a STATA routine, hopefully this is the right place.
I'm working on a simulation program using cross-sectional data of households. The idea is to feed the program input parameters (for example, if X1=1234, X2=123 and X3=12, pull 1234, 123 and 12 observations from my data and classify them as group 1, 2 and 3). I do need the program to be flexible enough such that sometimes I want to draw from a sub-sample of my entire dataset (for example, for group2, only draw from households with a female head of household).
Currently, I'm using things like "gsort variable" and a lot of transforms to achieve this and it's mostly working. However, given how many "if" statements I have, it's quickly becoming very hard to debug issues and work through the logic. I've tried using "subsample", but unfortunately couldn't get it to work as if X1=0, it gives me an error (I could parse through the possible combinations and use a lot of "if" statements again, but then it wouldn't really make the code easier to work with. Would someone have some tips/examples on this type of program? I've (hopefully correctly) copied in my code below, it's quite a doosey! This is just the random draw part and is part of a larger program.
Thanks in advance for any tips/ideas!
Best regards,
HH
I'm seeking some general tips on writing a STATA routine, hopefully this is the right place.
I'm working on a simulation program using cross-sectional data of households. The idea is to feed the program input parameters (for example, if X1=1234, X2=123 and X3=12, pull 1234, 123 and 12 observations from my data and classify them as group 1, 2 and 3). I do need the program to be flexible enough such that sometimes I want to draw from a sub-sample of my entire dataset (for example, for group2, only draw from households with a female head of household).
Currently, I'm using things like "gsort variable" and a lot of transforms to achieve this and it's mostly working. However, given how many "if" statements I have, it's quickly becoming very hard to debug issues and work through the logic. I've tried using "subsample", but unfortunately couldn't get it to work as if X1=0, it gives me an error (I could parse through the possible combinations and use a lot of "if" statements again, but then it wouldn't really make the code easier to work with. Would someone have some tips/examples on this type of program? I've (hopefully correctly) copied in my code below, it's quite a doosey! This is just the random draw part and is part of a larger program.
Thanks in advance for any tips/ideas!
Best regards,
HH
PHP Code:
*********************************************************************
* unstar below if you want to run this do.file only
clear
set seed 1234
local iso="COD" // Country iso code
local simNb=4 // Simulation number
local MC_iterations=1 // Montecarlo iterations
qui use "${PathWorkfiles}/`iso'_`simNb'_temp-import-shock-factors.dta", clear
* Setting global variables for targeting
global targeting_fcs 1
*Enter "0" to turn off targeting by FCS
*Enter "1" for FCS targeting
global lasso_PMT 0
*Enter "1" for a lasso based scorecard method of determining who gets assistance
*Enter "0" to turn this off
global targeting_error 0
global error_rate 0.20
*********************************************************************
/* NOTE: This do file assigns or removes assistance from a pool of housheolds that are considered eligible for assistance (variable Targeting=1). This is done with three prioritization procedures:
1. Random prioritization from the eligible pool (variable "Targeting"): ${targeting_fcs}=0 AND global ${lasso_PMT}=0
2. Ideal prioritization based on FCS: ${targeting_fcs}=1 AND global ${lasso_PMT}=1
3. Scorecard prioritization based on LASSO procedure: global ${lasso_PMT}=1 AND ${targeting_fcs}=1
*/
********************************************************************************
if "`it'"==""{
loc it=1
}
********************************************************************************
** Baseline assistance montecarlo
gl spillover_sh = 0.21 // Filipski et al. 2022 - Most conservative estimate within a review of LEWIE studies in 13 countries (https://doi.org/10.1111/agec.12687)
loc seed2=`it'+1000
set seed `seed2'
if ${targeting_fcs}==1 & ${lasso_PMT}==1 {
di in red "You cannot target FCS and use a PMT targeting approach at the same time, shutting down"
exit
}
********************************************************************************
gen sample_pop=_N
cap drop s_pop
egen s_pop=total(HHWeight)
label var s_pop "Shocked population value"
********************************************************************************
* This one is problematic, because it artificially reduces the number of people assisted
*gl capassist = 6 //Maximum number of household members assisted
cap gen HHSize_capped=HHSize
* replace HHSize_capped=${capassist} if HHSize>=${capassist}
* gen HHWeight_capped=HHWeight/HHSize*HHSize_capped // to use HHWeight for capping
********************************************************************************
if base_BeneficiariesNbUnique>s_pop {
di in red "More beneficiaries than population in Simulation ${SimID} ISO=`iso' "
exit
}
*
********************************************************************************
capture egen IndNb=total(HHWeight) // population. Whenever there is 'Ind' the variable indicates individuals, if 'HH' the variable indicates households
capture gen SampleHHNb=_N
gen base_BeneficiariesNbCombo=cond(base_BeneficiariesNbUnique>=base_BeneficiariesNbIK+base_BeneficiariesNbCbt,0,-(base_BeneficiariesNbUnique-(base_BeneficiariesNbIK+base_BeneficiariesNbCbt))) // overlapping beneficiaries for base
gen sample_base_BenHHUnique = round(base_BeneficiariesNbUnique* SampleHHNb/IndNb)
gen sample_base_BenHHIK = round(base_BeneficiariesNbIK * SampleHHNb/IndNb)
gen sample_base_BenHHCbt = round(base_BeneficiariesNbCbt * SampleHHNb/IndNb)
gen sample_base_BenHHCombo = round(base_BeneficiariesNbCombo * SampleHHNb/IndNb)
gen BeneficiariesNbCombo=cond(BeneficiariesNbUnique2>=BeneficiariesNbIK+BeneficiariesNbCbt,0,-(BeneficiariesNbUnique2-(BeneficiariesNbIK+BeneficiariesNbCbt))) // overlapping beneficiaries for other sims
gen sample_sim_BenHHUnique = round(BeneficiariesNbUnique2* SampleHHNb/IndNb)
gen sample_sim_BenHHIK = round(BeneficiariesNbIK * SampleHHNb/IndNb)
gen sample_sim_BenHHCbt = round(BeneficiariesNbCbt * SampleHHNb/IndNb)
gen sample_sim_BenHHCombo = round(BeneficiariesNbCombo * SampleHHNb/IndNb)
***
************************************************
*** Defining beneficiary households for base ***
************************************************
* Prioritization/1. Random prioritization from the eligible pool
if ${targeting_fcs}==0 & ${lasso_PMT}==0 {
*set seed 1234
gen RNB_base=runiform() if Targeting==1 // this will change at each iteration of the Montecarlo
gsort RNB_base
gen Cumulative_base=_n // this is needed to randomly assign assistance to the number of households as indicated by the variables sample_BenHH*
** Both IK and CBT recipients
capture gen treated_sample_base_BenHHCombo=0
replace treated_sample_base_BenHHCombo=cond(Cumulative_base<=sample_base_BenHHCombo,1,0) if Targeting==1 & sample_base_BenHHCombo>0
** IK recipients
capture gen treated_sample_base_BenHHIK=0
replace treated_sample_base_BenHHIK = 1 if Targeting==1 & treated_sample_base_BenHHCombo==1
replace treated_sample_base_BenHHIK = cond(Cumulative_base<=sample_base_BenHHIK,1,0) if Targeting==1 & treated_sample_base_BenHHCombo==0
** CBT recipients
capture gen treated_sample_base_BenHHCbt=0
replace treated_sample_base_BenHHCbt=1 if Targeting==1 & treated_sample_base_BenHHCombo==1
replace treated_sample_base_BenHHCbt=cond(Cumulative_base<=sample_base_BenHHCbt+sample_base_BenHHIK-sample_base_BenHHCombo,1,0) if Targeting==1 & treated_sample_base_BenHHCombo==0 & treated_sample_base_BenHHIK==0
** Creating treated_base variable
gen treated_base=.
replace treated_base=0 if Targeting==0 // non-eligible
replace treated_base=1 if Targeting==1 // non-treated
replace treated_base=2 if Targeting==1 & treated_sample_base_BenHHCombo==1 // CBT+IK
replace treated_base=3 if Targeting==1 & treated_base!=2 & treated_sample_base_BenHHIK==1 & treated_sample_base_BenHHCbt==0 // IK
replace treated_base=4 if Targeting==1 & treated_base!=2 & treated_sample_base_BenHHIK==0 & treated_sample_base_BenHHCbt==1 // CBT
}
*
* Prioritization/2. Ideal targeting based on FCS
if ${targeting_fcs}==1 & ${lasso_PMT}==0 {
gsort -Targeting FCS
gen Cumulative_base=_n
** Both IK and CBT recipients
capture gen treated_sample_base_BenHHCombo=0
replace treated_sample_base_BenHHCombo=cond(Cumulative_base<=sample_base_BenHHCombo,1,0) if Targeting==1 & sample_base_BenHHCombo>0
** IK recipients
capture gen treated_sample_base_BenHHIK=0
replace treated_sample_base_BenHHIK = 1 if Targeting==1 & treated_sample_base_BenHHCombo==1
replace treated_sample_base_BenHHIK = cond(Cumulative_base<=sample_base_BenHHIK,1,0) if Targeting==1 & treated_sample_base_BenHHCombo==0
** CBT recipients
capture gen treated_sample_base_BenHHCbt=0
replace treated_sample_base_BenHHCbt=1 if Targeting==1 & treated_sample_base_BenHHCombo==1
replace treated_sample_base_BenHHCbt=cond(Cumulative_base<=sample_base_BenHHCbt+sample_base_BenHHIK-sample_base_BenHHCombo,1,0) if Targeting==1 & treated_sample_base_BenHHCombo==0 & treated_sample_base_BenHHIK==0
*
** Creating treated_base variable
gen treated_base=.
replace treated_base=0 if Targeting==0 // non-eligible
replace treated_base=1 if Targeting==1 // non-treated
replace treated_base=2 if Targeting==1 & treated_sample_base_BenHHCombo==1 // CBT+IK
replace treated_base=3 if Targeting==1 & treated_base!=2 & treated_sample_base_BenHHIK==1 & treated_sample_base_BenHHCbt==0 // IK
replace treated_base=4 if Targeting==1 & treated_base!=2 & treated_sample_base_BenHHIK==0 & treated_sample_base_BenHHCbt==1 // CBT
}
*
* Prioritization/3. Scorecard prioritization based on LASSO procedure
if ${targeting_fcs}==0 & ${lasso_PMT}==1 {
di in red "not yet implemented"
exit
}
* Exclusion error at base
if (${lasso_PMT}==1 | ${targeting_fcs}==1) & ${targeting_error}==1 {
gen eligible_replacement_base=0
forval i= 2/4 {
gen temp_treated=1 if treated_base==`i'
egen float Cumulative_treated = total(temp_treated)
set seed 1234
gen RNG_exclusion_error=runiform() if temp_treated==1
gsort RNG_exclusion_error
replace eligible_replacement_base=-1 if RNG_exclusion_error<=${error_rate} & temp_treated==1
drop temp_treated Cumulative_treated RNG_exclusion_error
egen float dropped_treated_`i'=total(abs(eligible_replacement_base)) if treated_base==`i' // this is the number of those that don't get assistance for the exclusion error by transfer modality
sum dropped_treated_`i'
scalar scalar_dropped_number_`i'=r(max)
gen dropout_`i'=scalar_dropped_number_`i'
}
gen dropin_2=dropout_2
egen float dropin_3=rowtotal(dropout_2 dropout_3)
egen float dropin_4=rowtotal(dropout_2 dropout_3 dropout_4)
*
gen float dropout=dropin_4 // total number of households in the error
gen temp_nontreated=1 if treated_base==1
*gen temp_nontreated=1 if treated_base<=1 // if we want to relax the condition above and allow the inclusion error also to non-eligible households
egen float Cumulative_nontreated = total(temp_nontreated)
set seed 1234
gen RNG_inclusion_error=runiform() if temp_nontreated==1
gsort RNG_inclusion_error
gen Progressive_nontreated=_n
replace eligible_replacement_base=1 if Progressive_nontreated<=dropout & temp_nontreated==1
gsort -eligible_replacement_base
replace treated_base=2 if eligible_replacement_base==1 & treated_base==1 & _n<=dropin_2 & dropin_2!=.
replace treated_base=3 if eligible_replacement_base==1 & treated_base==1 & _n<=dropin_3 & dropin_3!=.
replace treated_base=4 if eligible_replacement_base==1 & treated_base==1 & _n<=dropin_4 & dropin_4!=.
replace treated_base=1 if eligible_replacement_base==-1
drop temp_nontreated dropped_treated_2 dropped_treated_3 dropped_treated_4 dropped_treated_2 dropout dropout_2 dropin_2 dropout_3 dropin_3 dropout_4 dropin_4 Progressive_nontreated RNG_inclusion_error Cumulative_nontreated
tab eligible_replacement_base treated_base
}
*
tabstat HHWeight, statistics( sum ) by(treated_base)
*************************************************************
*** Defining beneficiary Households for other simulations ***
*************************************************************
** Creating a variable for HH receiving assistance (treated)
capture gen treated_sample_sim_BenHHCombo=treated_sample_base_BenHHCombo
capture gen treated_sample_sim_BenHHIK=treated_sample_base_BenHHIK
capture gen treated_sample_sim_BenHHCbt=treated_sample_base_BenHHCbt
/*
***Step1. check if the number of beneficiaries in the simulation as compared to the baseline increases (scale-up), decreases (scale-down) or stays put:
Scale-up aka Increased number of beneficiaries: 1
No change aka Same number of beneficiaries: 0
Scale-down aka Decreased number of beneficiaries: -1
*/
gen cash_benef=.
gen ik_benef=.
gen combo_benef=.
*define values
replace cash_benef=1 if BeneficiariesNbCbt>base_BeneficiariesNbCbt
replace cash_benef=0 if BeneficiariesNbCbt==base_BeneficiariesNbCbt
replace cash_benef=-1 if BeneficiariesNbCbt<base_BeneficiariesNbCbt
replace ik_benef=1 if BeneficiariesNbIK>base_BeneficiariesNbIK
replace ik_benef=0 if BeneficiariesNbIK==base_BeneficiariesNbIK
replace ik_benef=-1 if BeneficiariesNbIK<base_BeneficiariesNbIK
replace combo_benef=1 if BeneficiariesNbCombo>base_BeneficiariesNbCombo
replace combo_benef=0 if BeneficiariesNbCombo==base_BeneficiariesNbCombo
replace combo_benef=-1 if BeneficiariesNbCombo<base_BeneficiariesNbCombo
***Step2. generate treated_sim, initialize as treated_base
gen treated_sim=treated_base
*gen treated_sim=treated_base // here we assume that the same beneficiaries assisted at the base continue to receive assistance, meaning no full retargeting applies
***Step3. First we need to deal with reductions, as this moves people back to eligible
*************************
* ASSISTANCE SCALE-DOWN *
*************************
* Prioritization/1. Random prioritization from the eligible pool
if ${targeting_fcs}==0 & ${lasso_PMT}==0 {
if combo_benef==-1 {
set seed 1234
gen RNG=runiform() if treated_sim==2
sort RNG
replace treated_sim=1 if treated_sim==2 & (_n<=abs(sample_sim_BenHHCombo-sample_base_BenHHCombo))
drop RNG
}
*
if ik_benef==-1 {
set seed 1234
gen RNG=runiform() if treated_sim==3
sort RNG
replace treated_sim=1 if treated_sim==3 & (_n<=abs(sample_sim_BenHHIK-sample_base_BenHHIK))
drop RNG
}
*
if cash_benef==-1 {
set seed 1234
gen RNG=runiform() if treated_sim==4
sort RNG
replace treated_sim=1 if treated_sim==4 & (_n<=abs(sample_sim_BenHHCbt-sample_base_BenHHCbt))
drop RNG
}
}
*
* Prioritization/2. Ideal targeting based on FCS
if ${targeting_fcs}==1 & ${lasso_PMT}==0 {
if combo_benef==-1 {
gsort treated_sim -FCS
gen temp=cond(treated_sim==2,1,0)
gsort -temp -FCS
gen Cumulative_base_fcs_scaledown=0
replace Cumulative_base_fcs_scaledown=_n if treated_sim==2
replace treated_sim=1 if treated_base==2 & (Cumulative_base_fcs_scaledown<=sample_base_BenHHCombo-sample_sim_BenHHCombo)
drop temp Cumulative_base_fcs_scaledown
}
*
if ik_benef==-1 {
gsort treated_sim -FCS
gen temp=cond(treated_sim==3,1,0)
gsort -temp -FCS
gen Cumulative_base_fcs_scaledown=0
replace Cumulative_base_fcs_scaledown=_n if treated_sim==3
replace treated_sim=1 if treated_base==3 & (Cumulative_base_fcs_scaledown<=sample_base_BenHHIK-sample_sim_BenHHIK)
drop temp Cumulative_base_fcs_scaledown
}
*
if cash_benef==-1 {
gsort treated_sim -FCS
gen temp=cond(treated_sim==4,1,0)
gsort -temp -FCS
gen Cumulative_base_fcs_scaledown=0
replace Cumulative_base_fcs_scaledown=_n if treated_sim==4
replace treated_sim=1 if treated_base==4 & (Cumulative_base_fcs_scaledown<=sample_base_BenHHCbt-sample_sim_BenHHCbt)
drop temp Cumulative_base_fcs_scaledown
}
}
*
* Prioritization/3. Scorecard prioritization based on LASSO procedure
if ${targeting_fcs}==0 & ${lasso_PMT}==1 {
di in red "not yet implemented"
exit
}
*
tabstat HHWeight, statistics( sum ) by(treated_base)
tabstat HHWeight, statistics( sum ) by(treated_sim)
*
*******************************************
***Step4. Next apply additively random increases (here only drawing from the updated eligible pool)
***********************
* ASSISTANCE SCALE-UP *
***********************
* Prioritization/1. Random prioritization from the eligible pool
if ${targeting_fcs}==0 & ${lasso_PMT}==0 {
if ik_benef==1 {
set seed 1234
gen RNG=runiform() if treated_sim==1
sort RNG
replace treated_sim=3 if treated_sim==1 & (_n<=abs(sample_sim_BenHHIK-sample_base_BenHHIK)) // we assign the additional assistance to those not assisted at baseline (i.e. treated_sim==1 which is equal to treated_base==1 as per line 134)
drop RNG
}
if cash_benef==1 {
set seed 1234
gen RNG=runiform() if treated_sim==1
sort RNG
replace treated_sim=4 if treated_sim==1 & (_n<=abs(sample_sim_BenHHCbt-sample_base_BenHHCbt))
drop RNG
}
if combo_benef==1 {
set seed 1234
gen RNG=runiform() if treated_sim==1
sort RNG
replace treated_sim=2 if treated_sim==2 & (_n<=abs(sample_sim_BenHHCombo-sample_base_BenHHCombo))
drop RNG
}
tab treated_base treated_sim
}
*
* Prioritization/2. Ideal targeting based on FCS
if ${targeting_fcs}==1 & ${lasso_PMT}==0 {
*calculate total number of people that needs to be drawn (all assistance types)
gen fcs_draw_IK=(sample_sim_BenHHIK-sample_base_BenHHIK)
replace fcs_draw_IK=0 if fcs_draw_IK<0
gen fcs_draw_Cbt=(sample_sim_BenHHCbt-sample_base_BenHHCbt)
replace fcs_draw_Cbt=0 if fcs_draw_Cbt<0
gen fcs_draw_Combo=(sample_sim_BenHHCombo-sample_base_BenHHCombo)
replace fcs_draw_Combo=0 if fcs_draw_Combo<0
egen fcs_draw_tot=rowtotal(fcs_draw_Combo fcs_draw_Cbt fcs_draw_IK)
sum fcs_draw_tot
scalar sfcs_draw_tot=r(mean)
*first draw a full pool
gen low_fcs=0
gen FCS_target=FCS if treated_sim==1
sort FCS_target
replace low_fcs=1 if _n<=sfcs_draw_tot
*break the sort
set seed 1234
gen RNG=runiform() if low_fcs==1
sort RNG
drop RNG
if ik_benef==1 {
replace treated_sim=3 if treated_sim==1 & (_n<=fcs_draw_IK)
}
if cash_benef==1 {
replace treated_sim=4 if treated_sim==1 & (_n<=fcs_draw_Cbt+fcs_draw_IK)
}
if combo_benef==1 {
replace treated_sim=2 if treated_sim==1 & (_n<=fcs_draw_Combo+fcs_draw_Cbt+fcs_draw_IK)
}
tab treated_base treated_sim
}
* Prioritization/3. Scorecard prioritization based on LASSO procedure
if ${targeting_fcs}==0 & ${lasso_PMT}==1 {
*calculate total number of poeple that needs to be drawn (all assistance types)
gen fcs_draw_IK=(sample_sim_BenHHIK-sample_base_BenHHIK)
replace fcs_draw_IK=0 if fcs_draw_IK<0
gen fcs_draw_Cbt=(sample_sim_BenHHCbt-sample_base_BenHHCbt)
replace fcs_draw_Cbt=0 if fcs_draw_Cbt<0
gen fcs_draw_Combo=(sample_sim_BenHHCombo-sample_base_BenHHCombo)
replace fcs_draw_Combo=0 if fcs_draw_Combo<0
egen fcs_draw_tot=rowtotal(fcs_draw_IK fcs_draw_Cbt fcs_draw_Combo)
sum fcs_draw_tot
scalar sfcs_draw_tot=r(mean)
*applies lasso regression to FCS (as a comparison point to current FCS)
lasso2 FCS lasso_*
lasso2, lic(ebic) postres
reg FCS `e(selected)', cluster(Adm2Name)
predict latent_fcs
***generate potential list of beneficiaries
gen low_fcs_hat=0
gen latent_fcs_target=latent_fcs if treated_sim==1
sort latent_fcs_target
replace low_fcs_hat=1 if _n<=sfcs_draw_tot
*break the sort
set seed 1234
gen RNG=runiform() if low_fcs_hat==1
sort RNG
drop RNG
if ik_benef==1 {
replace treated_sim=3 if treated_sim==1 & (_n<=fcs_draw_IK)
}
if cash_benef==1 {
replace treated_sim=4 if treated_sim==1 & (_n<=fcs_draw_Cbt+fcs_draw_IK)
}
if combo_benef==1 {
replace treated_sim=2 if treated_sim==1 & (_n<=fcs_draw_Combo+fcs_draw_Cbt+fcs_draw_IK)
}
}
* Exclusion error at simulation
if (${lasso_PMT}==1 | ${targeting_fcs}==1) & ${targeting_error}==1 {
gen eligible_replacement_sim=0
forval i= 2/4 {
gen temp_treated=1 if treated_sim==`i'
egen float Cumulative_treated = total(temp_treated)
set seed 1234
gen RNG_exclusion_error=runiform() if temp_treated==1
gsort RNG_exclusion_error
replace eligible_replacement_sim=-1 if RNG_exclusion_error<=${error_rate} & temp_treated==1
drop temp_treated Cumulative_treated RNG_exclusion_error
egen float dropped_treated_`i'=total(abs(eligible_replacement_sim)) if treated_sim==`i' // this is the number of those that don't get assistance for the exclusion error by transfer modality
sum dropped_treated_`i'
scalar scalar_dropped_number_`i'=r(max)
gen dropout_`i'=scalar_dropped_number_`i'
*drop dropped_treated_`i'
}
gen dropin_2=dropout_2
egen float dropin_3=rowtotal(dropout_2 dropout_3)
egen float dropin_4=rowtotal(dropout_2 dropout_3 dropout_4)
*
gen float dropout=dropin_4 // total number of households in the error
gen temp_nontreated=1 if treated_sim==1
*gen temp_nontreated=1 if treated_sim<=1 // if we want to relax the condition above and allow the inclusion error also to non-eligible households
egen float Cumulative_nontreated = total(temp_nontreated)
set seed 1234
gen RNG_inclusion_error=round(runiform()) if temp_nontreated==1
gsort RNG_inclusion_error
gen Progressive_nontreated=_n
replace eligible_replacement_sim=1 if Progressive_nontreated<=dropout & temp_nontreated==1
gsort -eligible_replacement_sim
replace treated_sim=2 if eligible_replacement_sim==1 & treated_sim==1 & _n<=dropin_2 & dropin_2!=.
replace treated_sim=3 if eligible_replacement_sim==1 & treated_sim==1 & _n<=dropin_3 & dropin_3!=.
replace treated_sim=4 if eligible_replacement_sim==1 & treated_sim==1 & _n<=dropin_4 & dropin_4!=.
replace treated_sim=1 if eligible_replacement_sim==-1
drop temp_nontreated dropped_treated_2 dropped_treated_3 dropped_treated_4 dropped_treated_2 dropout dropout_2 dropin_2 dropout_3 dropin_3 dropout_4 dropin_4 Progressive_nontreated RNG_inclusion_error Cumulative_nontreated
tab eligible_replacement_sim treated_sim
}
*
***************************END of targeting error section***********************
cap drop cash_benef
cap drop ik_benef
cap drop combo_benef
capture label define treated_lab 0 "non-eligibile" 1 "non-treated" 2 "CBT+IK" 3 "IK" 4 "CBT"
label values treated_base treated_lab
label values treated_sim treated_lab
*tabstat HHWeight, statistics( sum ) by(treated_base)
*tabstat HHWeight, statistics( sum ) by(treated_sim)
*************************************************************
*** Defining Transfer value ***
*************************************************************
*** Base
rename PCAssistanceValueIK_1M sim_PCAssistanceValueIK_1M
rename PCAssistanceValueCbt_1M sim_PCAssistanceValueCbt_1M
gen base_HHAssistanceValueIK_1M = base_PCAssistanceValueIK_1M * HHSize_capped if (treated_base==2 | treated_base==3)
gen base_HHAssistanceValueCbt_1M = base_PCAssistanceValueCbt_1M * HHSize_capped if (treated_base==2 | treated_base==4)
egen base_HHAssistanceValueTot_1M = rowtotal(base_HHAssistanceValueCbt_1M base_HHAssistanceValueIK_1M)
gen sim_HHAssistanceValueIK_1M = sim_PCAssistanceValueIK_1M * HHSize_capped if (treated_sim==2 | treated_sim==3)
gen sim_HHAssistanceValueCbt_1M = sim_PCAssistanceValueCbt_1M * HHSize_capped if (treated_sim==2 | treated_sim==4)
***Step1. intialize changes to income at 0
gen delta_HHAssistanceValueIK_1M=0
gen delta_HHAssistanceValueCbt_1M=0
***Step2. For those who were on assistance and then lost assistance, remove base transfer value
replace delta_HHAssistanceValueIK_1M=-base_HHAssistanceValueIK_1M if treated_sim==1 & (treated_base==2 | treated_base==3)
replace delta_HHAssistanceValueCbt_1M=-base_HHAssistanceValueCbt_1M if treated_sim==1 & (treated_base==2 | treated_base==4)
***Step3. For those who have been added to assistance, add simulated transfer value
replace delta_HHAssistanceValueIK_1M=sim_HHAssistanceValueIK_1M if treated_base==1 & (treated_sim==2 | treated_sim==3)
replace delta_HHAssistanceValueCbt_1M=sim_HHAssistanceValueCbt_1M if treated_base==1 & (treated_sim==2 | treated_sim==4)
***Step4. Those who stay on assistance, we need to adjust assistance levels (if no changes, then the levels shouldn't change)
replace delta_HHAssistanceValueIK_1M=sim_HHAssistanceValueIK_1M-base_HHAssistanceValueIK_1M if (treated_base==2 | treated_base==3) & (treated_sim==2 | treated_sim==3)
replace delta_HHAssistanceValueCbt_1M=sim_HHAssistanceValueCbt_1M-base_HHAssistanceValueCbt_1M if (treated_base==2 | treated_base==4) & (treated_sim==2 | treated_sim==4)
egen delta_HHAssistanceValueTot_1M = rowtotal(delta_HHAssistanceValueCbt_1M delta_HHAssistanceValueIK_1M)
*************************************************************
*** Defining spillover effects ***
*************************************************************
cap drop base_transfer_spill_sample
gen base_spill_HHValueIK_1M = base_PCAssistanceValueIK_1M*HHSize_capped if (treated_base==2 | treated_base==3)
gen base_spill_HHValueCbt_1M = base_PCAssistanceValueCbt_1M*HHSize_capped if (treated_base==2 | treated_base==4)
egen base_spill_HHValueTot_1M = rowtotal(base_spill_HHValueCbt_1M base_spill_HHValueIK_1M)
bys Adm1Code: egen base_transfer_spill_sample=mean(base_spill_HHValueTot_1M)
replace base_transfer_spill_sample=base_transfer_spill_sample*(${spillover_sh}) // this one is > 0 and quantifies the spillover effects that have already materialized in the income (HSI) at the base
recode base_transfer_spill_sample (.=0)
cap drop sim_transfer_spill_sample
gen sim_spill_HHValueIK_1M = sim_PCAssistanceValueIK_1M*HHSize_capped if (treated_sim==2 | treated_sim==3)
gen sim_spill_HHValueCbt_1M = sim_PCAssistanceValueCbt_1M*HHSize_capped if (treated_sim==2 | treated_sim==4)
egen sim_spill_HHValueTot_1M = rowtotal(sim_spill_HHValueCbt_1M sim_spill_HHValueIK_1M)
gen delta_spill_HHValueTot_1M=sim_spill_HHValueTot_1M-base_spill_HHValueTot_1M
bys Adm1Code: egen delta_transfer_spill_sample=mean(delta_spill_HHValueTot_1M)
replace delta_transfer_spill_sample=delta_transfer_spill_sample*(${spillover_sh}) // this one is <= 0
recode delta_transfer_spill_sample (.=0)
gen HSI_S=0
replace HSI_S=HSI+delta_HHAssistanceValueTot_1M+delta_transfer_spill_sample // this removes part of the spillover effects as a result of budget cuts
replace HSI_S=0 if HSI_S<0
gen r_inc=HSI_S/HSI
drop r_inc
Comment