* Reproduces Tables OA13, OA14, OA15, OA16, OA17, OA18
clear all
cls
log using robustness.txt, text replace 
use "cogs_deflated.dta", clear

*UPSTREAMNESS 2007 (OA15, OA16)
gen dist3 = round(up2007)

local BWE = "bwed"
local DV = "sd_cogsqd"
local IFE = "i.sic2"


estpost tabstat `BWE', by(dist3)  statistics(n p50 p25 p75 mean sd) col(stat)
esttab using "byUp2007_`BWE'.tex" , cells("count p50(f(%8.4f)) p25(f(%8.4f)) p75(f(%8.4f)) mean(f(%8.4f)) sd(f(%8.4f))")  noobs nomtitles label replace

estpost tabstat `DV', by(dist3)  statistics(n p50 p25 p75 mean sd) col(stat)
esttab using "byUp2007_`DV'.tex" , cells("count p50(f(%8.4f)) p25(f(%8.4f)) p75(f(%8.4f)) mean(f(%8.4f)) sd(f(%8.4f))")  noobs nomtitles label replace

reg `BWE' up2007 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_up2007.tex, replace dec(4) lab drop(i.year `IFE') addtext(Industry FE, No, Year FE, Yes) 
reg `BWE' up2007 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_up2007.tex, append dec(4) lab drop(i.year `IFE') addtext(Industry FE, Yes, Year FE, Yes) 

reg `DV' up2007 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_up2007.tex, append dec(4) lab drop(i.year `IFE') addtext(Industry FE, No, Year FE, Yes) 
reg `DV' up2007 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_up2007.tex, append dec(4) lab drop(i.year `IFE') addtext(Industry FE, Yes, Year FE, Yes) sortvar(up2007 lsaleq) tex(frag)



*LayerALL (OA13, OA14)
replace dist2d=9 if dist2d>9 & dist2d<99
estpost tabstat `BWE', by(dist2d)  statistics(n p50 p25 p75 mean sd) col(stat)
esttab using "byDist2d_`BWE'.tex" , cells("count p50(f(%8.4f)) p25(f(%8.4f)) p75(f(%8.4f)) mean(f(%8.4f)) sd(f(%8.4f))" )  noobs nomtitles label replace

estpost tabstat `DV', by(dist2d)  statistics(n p50 p25 p75 mean sd) col(stat)
esttab using "byDist2d_`DV'.tex" , cells("count p50(f(%8.4f)) p25(f(%8.4f)) p75(f(%8.4f)) mean(f(%8.4f)) sd(f(%8.4f))" )  noobs nomtitles label replace


reg `BWE' dist2 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist2.tex, replace dec(4) lab drop(i.year `IFE') addtext(Industry FE, No, Year FE, Yes) 
reg `BWE' dist2 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist2.tex, append dec(4) lab drop(i.year `IFE') addtext(Industry FE, Yes, Year FE, Yes) 

reg `DV' dist2 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist2.tex, append dec(4) lab drop(i.year `IFE') addtext(Industry FE, No, Year FE, Yes) 
reg `DV' dist2 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist2.tex, append dec(4) lab drop(i.year `IFE') addtext(Industry FE, Yes, Year FE, Yes) sortvar(dist2 lsaleq) tex(frag)


*LM (OA17, OA18)
keep if dist4<=2
estpost tabstat `BWE', by(dist4)  statistics(n p50 p25 p75 mean sd) col(stat)
esttab using "byDist4_`BWE'.tex" , cells("count p50(f(%8.4f)) p25(f(%8.4f)) p75(f(%8.4f)) mean(f(%8.4f)) sd(f(%8.4f))" )  noobs nomtitles label replace

estpost tabstat `DV', by(dist4)  statistics(n p50 p25 p75 mean sd) col(stat)
esttab using "byDist4_`DV'.tex" , cells("count p50(f(%8.4f)) p25(f(%8.4f)) p75(f(%8.4f)) mean(f(%8.4f)) sd(f(%8.4f))" )  noobs nomtitles label replace

reg `BWE' dist4 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist4.tex, replace dec(4) lab drop(i.year) addtext(Year FE, Yes) 
reg `BWE' dist1 dist4 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist4.tex, append dec(4) lab drop(i.year) addtext(Year FE, Yes) 

gen dd0=dist4==0
gen dd1=dist4==1
gen dd2=dist4==2
label var dd0 "LM=0"
label var dd1 "LM=1"
label var dd2 "LM=2"
reg `BWE' dist1 dd* lsaleq i.year if dist1<99, noconstant vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist4.tex, append dec(4) lab drop(i.year) addtext(Year FE, Yes) 

reg `DV' dist4 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist4.tex, append dec(4) lab drop(i.year) addtext(Year FE, Yes) 
reg `DV' dist1 dist4 lsaleq i.year if dist1<99, vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist4.tex, append dec(4) lab drop(i.year) addtext(Year FE, Yes)  sortvar(dist1 dist4 lsaleq) tex(frag)

cap drop dd*
gen dd0=dist4>=0
gen dd1=dist4>=1
gen dd2=dist4>=2
label var dd0 "LM=0"
label var dd1 "LM=1"
label var dd2 "LM=2"
reg `DV' dist1 dd* lsaleq i.year if dist1<99, noconstant vce(cluster gvkey_code)
outreg2 using res_bwe_ols_dist4.tex, append dec(4) lab drop(i.year) addtext(Year FE, Yes)  sortvar(dist1 dist4 lsaleq) tex(frag)

log close
