* Reproduces Tables 3, 4, OA1, OA2, OA3
clear all
cls
capture log close
log using summarize_BWE_DV.txt, text replace 
use "cogs_deflated.dta", clear

gsort gvkey_code -datacqtr
//BWEall DVall are computed for firms active in Q4 2014
replace bweall = . if datacqtr~=20144
replace sd_cogsqdall = . if datacqtr~=20144

//tabulating BWE and DV and running the significance test for BWE (Tables 4, A1, A2, A3)
local BWEvars = "bwed bwed4 bwed12 bweall"
foreach BWE of local BWEvars {	
	estpost tabstat `BWE', by(dist1)  statistics(n p50 p25 p75 mean sd) col(stat)
	esttab using "bylayer_`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
	*bys dist1: centile `BWE', level(99)
}

local DVvars = "sd_cogsqd sd_cogsqd4 sd_cogsqd12 sd_cogsqdall"
foreach DV of local DVvars {	
	estpost tabstat `DV', by(dist1)  statistics(n p50 p25 p75 mean sd ) col(stat)
	esttab using "bylayer_`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
}



//REGRESSIONS

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

//basic checks
reg `BWE' dist1 lsaleq i.year       if dist1<99, vce(cluster gvkey_code)
reg `BWE' dist1 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
reg `BWE' dist2 lsaleq i.year       if dist1<99, vce(cluster gvkey_code)
reg `BWE' dist2 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
reg `BWE' dist4 lsaleq i.year       if dist4<=3 & dist1<99, vce(cluster gvkey_code)
reg `BWE' dist1 dist4 lsaleq i.year if dist4<=3 & dist1<99, vce(cluster gvkey_code)
reg `BWE' up2007 lsaleq i.year       if dist1<99, vce(cluster gvkey_code)
reg `BWE' up2007 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)

reg `DV' dist1 lsaleq i.year       if dist1<99, vce(cluster gvkey_code)
reg `DV' dist1 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
reg `DV' dist2 lsaleq i.year       if dist1<99, vce(cluster gvkey_code)
reg `DV' dist2 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)
reg `DV' dist4 lsaleq i.year       if dist4<=3 & dist1<99, vce(cluster gvkey_code)
reg `DV' dist1 dist4 lsaleq i.year if dist4<=3 & dist1<99, vce(cluster gvkey_code)
reg `DV' up2007 lsaleq i.year       if dist1<99, vce(cluster gvkey_code)
reg `DV' up2007 lsaleq i.year `IFE' if dist1<99, vce(cluster gvkey_code)



//DETAILED REGRESSIONS with outregs
* BWE OLS (Table 4)
cap drop dd*

gen dd0=dist1==0
gen dd1=dist1==1
gen dd2=dist1==2
gen dd3=dist1==3
gen dd4=dist1==4
gen dd5=dist1==5
gen dd6=dist1==6
gen dd7=dist1==7
gen dd8=dist1==8


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

* Quantile regressions (no outreg) 

qreg `BWE' dd*   lsaleq i.year `IFE'     if dist1<99, vce(r) q(50) nolog
qreg `BWE' dist1 lsaleq i.year `IFE' if dist1<99, vce(r) q(50) nolog
qreg `BWE' dd*   lsaleq i.year `IFE'     if dist1<99, vce(r) q(25) nolog
qreg `BWE' dist1 lsaleq i.year `IFE' if dist1<99, vce(r) q(25) nolog
qreg `BWE' dd*   lsaleq i.year `IFE'     if dist1<99, vce(r) q(75) nolog
qreg `BWE' dist1 lsaleq i.year `IFE' if dist1<99, vce(r) q(75) nolog


* DV OLS (Table 4)
drop dd*

gen dd0=dist1>=0
gen dd1=dist1>=1
gen dd2=dist1>=2
gen dd3=dist1>=3
gen dd4=dist1>=4
gen dd5=dist1>=5
gen dd6=dist1>=6
gen dd7=dist1>=7
gen dd8=dist1>=8

label var dd0 "Layer=0"
label var dd1 "Layer=1"
label var dd2 "Layer=2"
label var dd3 "Layer=3"
label var dd4 "Layer=4"
label var dd5 "Layer=5"
label var dd6 "Layer=6"
label var dd7 "Layer=7"
label var dd8 "Layer=8"


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



* Quantile regressions (no outreg)
qreg `DV' dd*   lsaleq i.year `IFE' if dist1<99, vce(r) q(50) nolog
qreg `DV' dist1 lsaleq i.year `IFE' if dist1<99, vce(r) q(50) nolog
qreg `DV' dd*   lsaleq i.year `IFE' if dist1<99, vce(r) q(25) nolog
qreg `DV' dist1 lsaleq i.year `IFE' if dist1<99, vce(r) q(25) nolog
qreg `DV' dd*   lsaleq i.year `IFE' if dist1<99, vce(r) q(75) nolog
qreg `DV' dist1 lsaleq i.year `IFE' if dist1<99, vce(r) q(75) nolog

log close

