import ipystata

"""
Explanation: Replicating Ashraf, Karlan and Yin (2006)

Ashraf, N., D. Karlan, and W. Yin. 2006. “Tying Odysseus to the Mast: Evidence From a Commitment Savings Product in the Philippines.” The Quarterly Journal of Economics 121 (2): 635–672. https://doi.org/10.1162/qjec.2006.121.2.635.

The following is based on the replication files for this paper (available from Harvard Dataverse).  This is written in a jupyter notebook using the ipystata python library.   The following reproduces only parts of the analysis from the do files:  SEED.QJEtable.impact.do and SEED.QJEtables.takeup.do
End of explanation
"""


%%stata
cd G:\GC\Dev-II\notebooks
use AKY06\seedanalysis_011204_080404_1_v11.dta

tab group
tab group if treatment !=.

"""
Explanation: Observations about the datasets
These are the original datasets. They have non-obvious names:


seedanalysis_011204_1.dta: -- Six month impacts


seedanalysis_011204_080404_1.dta:  -- Twelve month impacts


seedanalysis_080404_1.dta: -- not sure yet..


The dataset has 3153 observations but the baseline survey appears to have been administered to only 1777 (if call==1).
End of explanation
"""


%%stata
use AKY06\seedanalysis_011204_1_v11.dta
xi: reg balchange treatment marketing, robust
xi: reg balchange treatment if (treatment == 1 | marketing == 1), robust

"""
Explanation: Table 6: Impact
OLS 6 months
Note that the 6 months data is in the file:
AKY06\seedanalysis_011204_1.dta
End of explanation
"""


%%stata
use AKY06\seedanalysis_011204_080404_1_v11.dta

xi: reg balchange treatment marketing, robust

xi: reg balchange treatment if (treatment == 1 | marketing == 1), robust

"""
Explanation: OLS 12 months
Note that the 12 months data is in the file:
AKY06\seedanalysis_011204_080404_1.dta
End of explanation
"""


%%stata

xi: reg balchange treatment marketing, robust
    
**economic impact #1: % of hh_inc 
summ hh_inc if e(sample)==1
disp _b[treatment] / (_result(3)*10000)

**economic impact #2: % of prior balance
summ totbal if e(sample)==1
disp _b[treatment] / (_result(3)/100)

"""
Explanation: In the analysis that follows e(sample)==1 restricts the calculations to the sample that was used in the lsat regression and _result(3) refers to the mean of hh_inc
End of explanation
"""


%%stata
xi: dprobit frac_change_00 treatment marketing, robust
xi: dprobit frac_change_00 treatment if (treatment == 1 | marketing == 1), robust
xi: dprobit frac_change_20 treatment marketing, robust
xi: dprobit frac_change_20 treatment  if (treatment == 1 | marketing == 1), robust

"""
Explanation: Probit regressions
End of explanation
"""


%%stata
use AKY06\seedanalysis_011204_080404_1_v11.dta
sqreg balchange treatment marketing, q(.1 .2 .3 .4 .5 .6 .7 .8 .9)

sqreg balchange treatment if (treatment == 1 | marketing == 1), q(.1 .2 .3 .4 .5 .6 .7 .8 .9)

"""
Explanation: Quantile Regressions
6 months
End of explanation
"""


%%stata
use AKY06\seedanalysis_011204_080404_v11.dta, clear
sqreg balchange treatment marketing, q(.1 .2 .3 .4 .5 .6 .7 .8 .9)
sqreg balchange treatment if (treatment == 1 | marketing == 1), q(.1 .2 .3 .4 .5 .6 .7 .8 .9)

"""
Explanation: 12 months
End of explanation
"""


%%stata
xi: reg balchange treatment marketing female female_treat, robust 
xi: reg balchange treatment marketing active active_treat, robust
xi: reg balchange treatment marketing edhi edhi_treat, robust
xi: reg balchange treatment marketing hi_hh_inc hi_hh_inc_treat, robust
xi: reg balchange treatment marketing hyper_mon_new2 hyper_mon_new2_treat, robust
xi: reg balchange treatment marketing silly_mon_new2 silly_mon_new2_treat, robust

"""
Explanation: Table VIII: ITT effect of subgroups
12 months
End of explanation
"""


%%stata

use AKY06\seedanalysis_011204_080404_1_v11.dta, clear
gen control = 1 if group == "C"
replace control = 0 if control ==.
macro define GB "GBloan GBloandefault"
macro define fullset "married edhi numhh unemployed age $GB hh_inc hh_inc2"
macro define fullset_noGB "married edhi numhh unemployed age hh_inc hh_inc2"

gen dormant_new = 1- active
drop dormant
rename dormant_new dormant
replace totbal = totbal/100
replace newtotbal = newtotbal/100
gen dist_GB = dbutuan if butuan ==1
replace dist_GB = dampayon if ampayon == 1
destring pop, ignore(",") replace
gen brgy_penetration = no_clients /pop
bysort brgy: egen sd_totbal = sd(totbal)
bysort brgy: egen mean_totbal = mean(totbal)

%%stata
tab active if call==1,summ(totbal)

"""
Explanation: There is a lot more to the impact results in the original do file.... but for now the analysis here turns to the takeup questions:
TAKEUP
End of explanation
"""


%%stata
tab goal_category if seedtakeup==1
tab goal_type if seedtakeup==1
tab box if seedtakeup==1

"""
Explanation: Table 1: Savings Goals
End of explanation
"""


%%stata
tab call

"""
Explanation: Table II: Summary Statistics
End of explanation
"""


%%stata
tabstat totbal active if call==1, by(group) stats(mean sem)
reg totbal treatment control if call ==1
reg active treatment control if call ==1
sort brgy
tabstat dist_GB brgy_penetration sd_totbal mean_totbal pop if call==1, by(group) stats(mean sem)
reg dist_GB treatment control if call ==1
reg brgy_penetration treatment control if call ==1
reg sd_totbal treatment control if call ==1
reg mean_totbal treatment control if call ==1
reg pop treatment control if call ==1

"""
Explanation: Panel A
End of explanation
"""


%%stata
tabstat yearsed female age impatient_mon01 hyper_mon_new2 if call==1, by(group) stats(mean sem) 
reg yearsed treatment control if call ==1
reg female treatment control if call ==1
reg age treatment control if call ==1
reg impatient_mon01 treatment control if call ==1
reg hyper_mon_new2 treatment control if call ==1

"""
Explanation: Panel B
End of explanation
"""


%%stata
tab impatient_mon01 impatient_mon67 if call==1

"""
Explanation: Table III: Time preference questions
0 = patient, 1 = somewhat impatient, 2=most impatient
End of explanation
"""


%%stata
gen female_hyper_mon_new2 = female * hyper_mon_new2

%%stata
xi: dprobit seedtakeup hyper_mon_new2 female impatient_200p250_01 impatient_250p200_01 impatient_200p250_67 impatient_250p200_67 fem_married $fullset fem_frac_veryown_inc_0_25 fem_frac_veryown_inc_25_50 fem_frac_veryown_inc_50_75 fem_frac_veryown_inc_75_100 frac_veryown_inc_0_25 frac_veryown_inc_25_50 frac_veryown_inc_50_75 frac_veryown_inc_75_100 active if group=="T" & call==1 & reached==1, robust 

"""
Explanation: Table V: Determinants of Takeup
End of explanation
"""


%%stata
tab seedtakeup if call==1

%%stata
tab impatient_mon01
tab active impatient_mon01,row col

%%stata --graph
tab zerototbal if call==1
summ howlongopen
hist howlongopen

%%stata
tab active if call==1,summ( nonseedbalchange)
summ nonseedbalchange,detail
tab active active_treat if call==1

%%stata --graph
hist balchange_total if balchange <2500
count if balchange_total==0
tab savedathome

%%stata
tab active if call==1, summ(time_acct_tot)
tab active,summ (housedebt)

%%stata
tab active seedtakeup if call==1
tab active seedtakeup if call==1,summ(balchange)

%%stata
tab group active,summ(balchange)

"""
Explanation: More Exploratory
End of explanation
"""


%%stata
xtile decile = balchange if call==1, n(10)
tab decile group if call==1, summ(balchange)

"""
Explanation: Decile plots
This isn't quite the Decile plots on page 661 but close
End of explanation
"""


