clear
set more off 
clear matrix
clear mata
set matsize 11000
set maxvar 15000
cap log close
estimates clear

#delimit ;

*****************************************************************************************************************;
*Paths;
*****************************************************************************************************************;

*access files in subdirectory generated by data creating dofiles;
local dataset "dtafiles/mergedallrecoded.dta";

*generate subdirectory to store csv files;
cap mkdir csvfiles;

*****************************************************************************************************************;
*LOCALS;
*****************************************************************************************************************;

local contamination="wfraction_mindistanceC_1m_G";
local clustervar="dhsidyear";
local trendvar="district";

local addedcontrols="";

	//Which tables to generate;
	local table3=1;
	local table4=1;
	local table5andA3=1;
	local table6=1;
	local jointtest=1;
	local table7=1;
	local table8=1;
	local tableA1=1;
	local tableA2=1;
	local tableA4=1;
	local tableA5=1;
	
	//Filenames of tables;
	local tablename3 "csvfiles/Table3sumstats";
	local tablename4 "csvfiles/Table4basicprepost";
	local figurename6 "csvfiles/Figure6estimates";
	local tablename5andA3 "csvfiles/Table5andA3bfbyage";
	local tablename6 "csvfiles/Table6switching";
	local tablename7 "csvfiles/Table7healthbyage";
	local tablename8 "csvfiles/Table8HetEffects";
	local tablenameA1  "csvfiles/TableA1alternativeAs";
	local tablenameA2  "csvfiles/TableA2pipedwater";
	local tablenameA4 "csvfiles/TableA4censoring";
	local tablenameA5 "csvfiles/TableA5heardofarsenic";
	
*****************************************************************************************************************;
*DATA FINALIZATION;
*****************************************************************************************************************;

use "`dataset'", clear;

*****************************************************************************************************************;
*MAIN TABLES;
*****************************************************************************************************************;

*****************************************************************************************************;
if `table3'==1 {;
**Table 3: Summary Stats**;
**NOTE: do not change order of variables unless you redo the excel sheet since it repopulates in a specific way;
**You can add variables at the end;
local varlist "age mothereducyrs fathereduc electricity muslim motherworks monthsbreast childdied pipedwater 
	tubewell surfacewater htasdreal wthtsdreal urban fractioncont5 numcontaminated5 as_walkable5_g
	fraction_mindistanceC_1m_G wfraction_mindistanceC_1m_G 
	exclusivebf childagewithdied  momwthtsd  exclusivebf6 exclusivebf614
	exclusivebf12 monthsbf6 monthsbf18 monthsbf36 monthsbf12 monthsbf24" ;
	
sum `contamination', d;
gen high=`contamination'>=r(p50) if `contamination'!=.;
local replace="replace";
foreach year in 1999 2007 {;

	local i=1;
	
	regress high if year==`year';
	est sto x`i';
	local i=`i'+1;
	
	foreach var of varlist `varlist' {;
		reg `var' high if year==`year', cluster(`clustervar');
		est sto x`i';
		local i=`i'+1;
		areg `var' high if year==`year', cluster(`clustervar') absorb(district);
		est sto x`i';
		local i=`i'+1;

	};
	esttab _all using "`tablename3'.csv", se b nonotes 
		nolines noeqlines compres `replace' star(* 0.10 ** 0.05 *** 0.01) staraux;
	estimates clear;
	local replace="append";
};
drop high;
};

*End(Table3);
************************************************************************************************************************;

if `table4'==1 {;
**Table 4 and Figure 6: Basic Pre-Post**;

local replace replace;
local i=1;
foreach depvar in "monthsbreast" "exclusivebf" {;

	local fex = "dhsidyear";

	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;

		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `contamination' childagewithdied childdied `addedcontrols' `contamination'_2002 `region'
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;
	};

	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;
	
		**with district-specific linear trends;
		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `contamination' childagewithdied childdied `addedcontrols' `contamination'_2002 i.`trendvar'*childbyear
			`region' 
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;

	};
	esttab x* using "`tablename4'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
	local replace append;
	estimates clear;
};

local i=1;
foreach depvar in  "monthsbf12" {;

	local fex = "dhsid04matched";

	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;

		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `contamination' childagewithdied i.year `addedcontrols' `contamination'_2002 `region'
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;
	};

	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;
	
		**with district-specific linear trends;
		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `contamination' childagewithdied `addedcontrols' `contamination'_2002 i.`trendvar'*childbyear i.year
			`region' 
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;

	};
	esttab x* using "`tablename4'_m12.csv", se b star(* 0.10 ** 0.05 *** 0.01) replace
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
	local replace append;
	estimates clear;
};


local i=1;
**Figure 6: Event Study**;
foreach depvar in "monthsbreast" {;

	gen fe=dhsidyear;
	if "`depvar'"=="monthsbf" {; replace fe=dhsid04matched; };
	
	xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 byear2005
	byear2006 byear2007 `addedcontrols' `contamination' `contamination'_b* childagewithdied
	childdied i.`trendvar'*childbyear if urbanrural==2, absorb(fe) cluster(`clustervar');
	test `contamination'_b96+`contamination'_b97+`contamination'_b98+`contamination'_b99+
		`contamination'_b00+`contamination'_b01=`contamination'_b02+`contamination'_b03+
		`contamination'_b04+`contamination'_b05+`contamination'_b06+`contamination'_b07;
	local f=r(F);
	local ftest=r(p);
	test (`contamination'_b97+`contamination'_b98+`contamination'_b99+
		`contamination'_b00+`contamination'_b01)/5=(`contamination'_b02+`contamination'_b03+
		`contamination'_b04+`contamination'_b05+`contamination'_b06+`contamination'_b07)/6;
	eststo x`i', addscalars(f `f' ftest `ftest' f1 r(F) ftest1 r(p));
	local i=`i'+1;
	
	drop fe;
};
	
esttab x* using "`figurename6'.csv", se b replace 
	plain keep(`contamination'_b*) stats(N r2 f ftest f1 ftest1);
estimates clear;
};

*End(Table4,Figure6);
************************************************************************************************************************;

if `table5andA3'==1 {;
** Table 5 and A3: Breastfeeding by age;

local replace replace;
local i=1;
foreach depvar in "exclusivebf" "plainwater" {;

	local fex = "dhsid04matched";
	foreach agerange in "if childagewithdied<6" "if childagewithdied>=6 & childagewithdied<=14" 
		"if childagewithdied>12" {;

		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `addedcontrols' `contamination' childagewithdied childdied `contamination'_2002 
			i.`trendvar'*childbyear i.year `agerange' & urbanrural==2
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;
	};

	esttab x* using "`tablename5andA3'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
	local replace append;
	estimates clear;
};

#delimit ;
local i=1;
foreach length in "6" "12" "18" "24" "36" {;

	local fex = "dhsid04matched";

	xi: areg monthsbf`length' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
		byear2005 byear2006 byear2007 `addedcontrols' `contamination' childagewithdied `contamination'_2002 
		i.`trendvar'*childbyear i.year if urbanrural==2
		, a(`fex') cluster(`clustervar');
	sum monthsbf`length' if e(sample);
	local dep=r(mean);
	sum `contamination' if e(sample);
	eststo x`i', addscalars(depmean `dep' indepmean r(mean));

	local i=`i'+1;
};

	esttab x* using "`tablename5andA3'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
	local replace append;
	estimates clear;

};

*End(Table 5 and A3)
************************************************************************************************************************;

if `table6'==1 {;
**Table 6: Switching**;

gen clean1= fraction_mindistanceU_1m_G ;
gen clean2= wfraction_mindistanceU_1m_G  ;
gen clean3= mean_mindistanceU_G ;
gen clean4= wmean_mindistanceU_G ;

if `jointtest'==1 {;
log using "csvfiles/jointtest.txt", text replace;
** jointly test four columns;
	local i=1;
	foreach switch in "clean1" "clean2" "clean3" "clean4" {;
	
		gen nonswitcher`switch'=`switch';
		gen nspost`switch'=nonswitcher`switch'*post2002;
		gen nsAs`switch'=nonswitcher`switch'*`contamination';
		gen nsAs_post`switch'=nsAs`switch'*post2002;
	
		local fex="dhsidyear";
			
		**with district-specific linear trends;
		xi: reg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `contamination' `addedcontrols' childagewithdied childdied i.`trendvar'*childbyear
			nonswitcher`switch' nspost`switch' nsAs`switch' `contamination'_2002 nsAs_post`switch' i.`fex'
			if urbanrural==2
			;
		eststo x`i';
		local i=`i'+1;
	};
	
xi: reg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
	byear2005 byear2006 byear2007 `contamination' `addedcontrols' childagewithdied childdied i.`trendvar'*childbyear
	`contamination'_2002 i.`fex'
	if urbanrural==2
	;
eststo x`i';

suest x1 x2 x3 x4, robust cluster(`clustervar');
test [x1_mean]nsAs_postclean1 [x2_mean]nsAs_postclean2 [x3_mean]nsAs_postclean3 [x4_mean]nsAs_postclean4;

sum clean2 if e(sample);
test [x2_mean]wfraction_mindistanceC_1m_G_2002+.25*[x2_mean]nsAs_postclean2=0;
sum clean4 if e(sample),d;
test [x4_mean]wfraction_mindistanceC_1m_G_2002+2*[x4_mean]nsAs_postclean4=0;

log close;

drop nonswitcherclean1-nsAs_postclean4;
estimates clear;
};

local replace replace;
foreach depvar in "monthsbreast" {;

	local i=1;
	foreach switch in "contaminatedorsurface" 
		"clean1" "clean2" "clean3" "clean4" {;
	
		gen nonswitcher=`switch';
		gen nspost=nonswitcher*post2002;
		gen nsAs=nonswitcher*`contamination';
		gen nsAs_post=nsAs*post2002;
	
		local fex="dhsidyear";
			
		**with district-specific linear trends;
		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `contamination' `addedcontrols' childagewithdied childdied i.`trendvar'*childbyear
			nonswitcher nspost nsAs `contamination'_2002 nsAs_post
			if urbanrural==2 
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		if _se[`contamination'_2002]!=0 {;
			eststo x`i', addscalars(depmean `dep' indepmean r(mean));
		};
		local i=`i'+1;
		drop nonswitcher nspost nsAs nsAs_post;
	};
		
	esttab x* using "`tablename6'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002 nsAs_post);
	local replace append;
	estimates clear;
	
	drop clean1-clean4;
};

};

*End(Table 6: switching);*/;
************************************************************************************************************************;

if `table7'==1 {;
**Table 7: Health by age;
local i=1;
foreach length in "6" "12" "24" "36" {;

	local fex = "dhsid04matched";

	gen died`length'=ageatdeath<`length';
	replace died`length'=0 if childdied==0;
	replace died`length'=. if childpotentialage<`length';

	xi: areg died`length' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
		byear2005 byear2006 byear2007 `addedcontrols' `contamination' childpotentialage `contamination'_2002 
		i.`trendvar'*childbyear i.year if urbanrural==2
		, a(`fex') cluster(`clustervar');
	sum died`length' if e(sample);
	local dep=r(mean);
	sum `contamination' if e(sample);
	eststo x`i', addscalars(depmean `dep' indepmean r(mean));

	local i=`i'+1;
};

	esttab x* using "`tablename7'.csv", se b star(* 0.10 ** 0.05 *** 0.01) replace 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
	estimates clear;
	
local replace replace;	
local i=1;
foreach depvar in "diarrhea" "wthtsdreal" "htasdreal" {;

	local fex = "dhsid04matched";
	foreach agerange in "if childagewithdied<=12" "if childagewithdied>12 & childagewithdied<=24" 
		"if childagewithdied>24 & childagewithdied<=36" "if childagewithdied>36 & childagewithdied<=48"
		{;

		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `addedcontrols' `contamination' childagewithdied childdied `contamination'_2002 
			i.`trendvar'*childbyear i.year `agerange' & urbanrural==2
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;
	};

	esttab x* using "`tablename7'.csv", se b star(* 0.10 ** 0.05 *** 0.01) append
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
	estimates clear;
};
};

*End(Table 7: Health by age)
************************************************************************************************************************;


if `table8'==1 {;

**Table 8 HET EFFECTS**;

local clean="wmean_mindistanceU_G";

gen Asclean=`clean'*`contamination';
foreach hetvar in "anyindivclubs" "anyeduc" "moreautonomy" {;
	gen post_`hetvar'=`hetvar'*post2002;
	gen As_`hetvar'=`hetvar'*`contamination';
	gen As_post_`hetvar'=As_`hetvar'*post2002;
	gen clean_`hetvar'=`clean'*`hetvar';
	gen Asclean_`hetvar'=`contamination'*`clean'*`hetvar';
};

foreach depvar of varlist contaminatedorsurface {;
local i=1;
local replace replace;
local fex "dhsidyear";
foreach hetvar in "anyindivclubs" "anyeduc" "moreautonomy" {;

	xi: areg `depvar' `hetvar' `clean' `contamination' Asclean As_`hetvar' clean_`hetvar' Asclean_`hetvar'
		if urbanrural==2, absorb(`fex') cluster(dhsidyear);
	eststo x`i';
	local i=`i'+1;

};

local hetvar1="anyeduc";
local hetvar2="moreautonomy";
local hetvar3="anyindivclubs";
xi: areg `depvar' `hetvar1' `hetvar2' `hetvar3' `clean' `contamination' Asclean As_`hetvar1' clean_`hetvar1' Asclean_`hetvar1'
	As_`hetvar2' clean_`hetvar2' Asclean_`hetvar2' As_`hetvar3' clean_`hetvar3' Asclean_`hetvar3'
	if urbanrural==2, absorb(`fex') cluster(dhsidyear);
eststo x`i';
local i=`i'+1;

esttab x* using "`tablename8'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
	stats(N r2, layout(@ @)) keep(Asclean_*);
local replace append;
estimates clear;

local i=1;
local fex "dhsidyear";
foreach hetvar in "anyindivclubs" "anyeduc" "moreautonomy" {;
	
	**with district-specific linear trends;
	xi: areg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
		byear2005 byear2006 byear2007 `contamination' `addedcontrols' childagewithdied childdied i.`trendvar'*childbyear i.year
		`hetvar' post_`hetvar' As_`hetvar' `contamination'_2002 As_post_`hetvar'
		if urbanrural==2 & year==2004 
		, a(`fex') cluster(`clustervar');
	sum monthsbreast if e(sample);
	local dep=r(mean);
	sum `contamination' if e(sample);
	eststo x`i', addscalars(depmean `dep' indepmean r(mean));

	local i=`i'+1;
};

local hetvar1="anyeduc";
local hetvar2="moreautonomy";
local hetvar3="anyindivclubs";
xi: areg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
	byear2005 byear2006 byear2007 `contamination' `addedcontrols' childagewithdied childdied i.`trendvar'*childbyear i.year
	`hetvar1' post_`hetvar1' As_`hetvar1' `hetvar2' post_`hetvar2' As_`hetvar2' `hetvar3' post_`hetvar3' As_`hetvar3' 
	`contamination'_2002 As_post_`hetvar1' As_post_`hetvar2' As_post_`hetvar3'
	if urbanrural==2 & year==2004
	, a(`fex') cluster(`clustervar');
sum monthsbreast if e(sample);
local dep=r(mean);
sum `contamination' if e(sample);
eststo x`i', addscalars(depmean `dep' indepmean r(mean));
local i=`i'+1;


esttab x* using "`tablename8'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
	stats(N r2, layout(@ @)) keep(`contamination'_2002 As_post*);
local replace append;
estimates clear;
};
drop Asclean-Asclean_moreautonomy;
};


*End(Table8 HetEffects)*/; 
************************************************************************************************************************;

if `tableA1'==1 {;
**Table A1:alternative AS measures**;

local i=1;
local replace="replace";
foreach depvar in "monthsbreast" "exclusivebf" {;

foreach measure in "as_walkable5_g" "numcontaminated5" "fraction_mindistanceC_1m_G" "fractioncont5" {;

	local fex "dhsidyear";
	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;

		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `addedcontrols' `measure'_2002 childagewithdied childdied `region'
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `measure' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));
		
		local i=`i'+1;
	};

	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;
	
		**with District-specific linear trends;
		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `addedcontrols' `measure'_2002 childagewithdied childdied i.`trendvar'*childbyear
			`region' 
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `measure' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;

	};
	esttab x* using "`tablenameA1'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`measure'_2002) ;
	estimates clear;
	local replace="append";
};
};

local i=1;
local replace="replace";
foreach depvar in "monthsbf12" {;

foreach measure in "as_walkable5_g" "numcontaminated5" "fraction_mindistanceC_1m_G" "fractioncont5" {;

	local fex "dhsid04matched";
	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;

		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `addedcontrols' `measure'_2002 childagewithdied childdied i.year `region'
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `measure' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));
		
		local i=`i'+1;
	};

	foreach region in "" "if urbanrural==1" "if urbanrural==2" {;
	
		**with District-specific linear trends;
		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 `addedcontrols' `measure'_2002 childagewithdied childdied i.year i.`trendvar'*childbyear
			`region' 
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `measure' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;

	};
	esttab x* using "`tablenameA1'_m12.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`measure'_2002) ;
	estimates clear;
	local replace="append";
};
};


};
*End(Table A1: alternative AS measures)
************************************************************************************************************************;

if `tableA2'==1 {;
**Table A2: Piped Water Connection**;

foreach water in "pipedwater" "villagepipedwater" {;

foreach ifs in "" "& urbanrural==1" "& urbanrural==2" {;

xi: areg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 byear2005 
byear2006 byear2007 `addedcontrols' `contamination' `contamination'_2002 childagewithdied childdied i.`trendvar'*childbyear if `water'==1 `ifs', absorb(dhsidyear) cluster(`clustervar');
sum monthsbreast if e(sample);
local dep=r(mean);
sum `contamination' if e(sample);
local indep=r(mean);
eststo x`i', addscalars(depmean `dep' indepmean r(mean));

local i=`i'+1;
xi: areg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 byear2005 
byear2006 byear2007 `contamination' `addedcontrols' `contamination'_2002 childagewithdied childdied i.`trendvar'*childbyear if `water'==0 `ifs', absorb(dhsidyear) cluster(`clustervar');
sum monthsbreast if e(sample);
local dep=r(mean);
sum `contamination' if e(sample);
eststo x`i', addscalars(depmean `dep' indepmean r(mean));

local i=`i'+1;
};
};
esttab _all using "`tablenameA2'.csv", se b star(* 0.10 ** 0.05 *** 0.01) replace 
	stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
estimates clear;
};

*End(Table A2: Piped Water Connection);
************************************************************************************************************************;

if `tableA4'==1 {;
**Table A4: Right Censoring;

estimates clear;

**Breastfeeding is right censored by the child's age;
***Just using those who have stopped breastfeeding;
xi: areg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
	byear2005 byear2006 byear2007 `addedcontrols' `contamination'  childagewithdied childdied 
	`contamination'_2002 i.`trendvar'*childbyear if urbanrural==2 & still==0, a(dhsidyear) cluster(`clustervar');
eststo x1;

***Replacing monthsbreast with max in data for those still breastfeeding;
gen monthsbreast_59=monthsbreast;
replace monthsbreast_59=59 if still==1 & childdied==0;
xi: areg monthsbreast_59 byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
	byear2005 byear2006 byear2007 `addedcontrols' `contamination'  childagewithdied childdied 
	`contamination'_2002 i.`trendvar'*childbyear if urbanrural==2, a(dhsidyear) cluster(`clustervar');
eststo x2;

esttab _all using "`tablenameA4'.csv", se b star(* 0.10 ** 0.05 *** 0.01) replace 
	stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);

estimates clear;

**Bounding mortality effect on months breastfed;
***Replace monthsbreast with max in data for those who died;
gen monthsbreastD_59=monthsbreast;
replace monthsbreastD_59=59 if childdied==1 & monthsbreast==childagewithdied;
xi: areg monthsbreastD_59 byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
	byear2005 byear2006 byear2007 `contamination' `addedcontrols' childpotentialage childdied 
	`contamination'_2002 i.`trendvar'*childbyear if urbanrural==2, a(dhsidyear) cluster(`clustervar');
eststo x1;

***Replace monthsbreast with child potential age for those who died;
gen monthsbreastD_cpa=monthsbreast;
replace monthsbreastD_cpa=childpotentialage if childdied==1 & monthsbreast==childagewithdied;
xi: areg monthsbreastD_cpa byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
	byear2005 byear2006 byear2007 `contamination' `addedcontrols' childpotentialage childdied 
	`contamination'_2002 i.`trendvar'*childbyear if urbanrural==2, a(dhsidyear) cluster(`clustervar');
eststo x3;

esttab _all using "`tablenameA4'.csv", se b star(* 0.10 ** 0.05 *** 0.01) append 
	stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);

estimates clear;
};
*End(Table A4: Right Censoring);*/;

************************************************************************************************************************;

if `tableA5'==1 {;
**Table A5: heardofarsenic**;

sum wfraction_mindistanceC_1m_G, d;
gen high=wfraction_mindistanceC_1m_G>=r(mean) if wfraction_mindistanceC_1m_G!=.;

local fex="dhsid2004_year";

local replace replace;
local i=1;

local fex="dhsidyear";

foreach region in "" "if urbanrural==1" "if urbanrural==2" {;

	**with district-specific linear trends;
	xi: areg monthsbreast byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
		byear2005 byear2006 byear2007 `contamination' childagewithdied childdied `addedcontrols' `contamination'_2002 i.dhsidyear*childbyear i.year
		`region' 
		, a(`fex') cluster(`clustervar');
	sum monthsbreast if e(sample);
	local dep=r(mean);
	sum `contamination' if e(sample);
	eststo x`i', addscalars(depmean `dep' indepmean r(mean));

	local i=`i'+1;

};
esttab x* using "`tablenameA5'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
	stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(`contamination'_2002);
local replace append;
estimates clear;

foreach depvar in "monthsbreast" {;

	foreach fex of varlist dhsidyear dhsid2004_year {;
	foreach otherifs in "" "& high==0" "& high ==1" {;	
		**with district-specific linear trends;
		xi: areg `depvar' byear1996 byear1997 byear1998 byear1999 byear2000 byear2001 byear2002 byear2003 byear2004 
			byear2005 byear2006 byear2007 heardofarsenic childagewithdied childdied `addedcontrols' heardofarsenic_2002 i.`trendvar'*childbyear
			if urbanrural==2 `otherifs'
			, a(`fex') cluster(`clustervar');
		sum `depvar' if e(sample);
		local dep=r(mean);
		sum `contamination' if e(sample);
		eststo x`i', addscalars(depmean `dep' indepmean r(mean));

		local i=`i'+1;

	};
	};
	esttab x* using "`tablenameA5'.csv", se b star(* 0.10 ** 0.05 *** 0.01) `replace' 
		stats(N r2 depmean indepmean, layout(@ @ " " @ @)) keep(heardofarsenic_2002);
	local replace append;
	estimates clear;
};

};
*End(Table A5: heardofarsenic);*/;
************************************************************************************************************************;

