*
* This STATA code produces -- in csv or txt format -- the regression results of Klerman et al, Legal Origins or Colonial History?*
*


set more off
capture: log close

log using LO_v_CO.log, replace

local extracontrol "" /* this allows insertion of another control, like LogSettMort, into all regressions */

local sample_0 " "
local sample_1 "if FColExNE==1"
local sample_2 "if FCol60==1"

local LOCOgroups " LCO_FrFr LCO_FrBr LCO_FrBe LCO_FrNC LCO_CoBr LCO_CoOth LCO_CoNC LCO_MiBr LCO_MiBe LCO_MiOth LCO_MiNC LCO_GeBe LCO_GeOth LCO_GeNC LCO_ScOth LCO_ScNC LCO_IsOth "

local test_1 " LCO_FrFr-LCO_FrBe "
local test_2 " LCO_CoBr-LCO_FrBe "
local test_3 " LCO_CoBr-LCO_MiBr "

local testnumber=3

local controls_1 " "
local controls_2 "p60s lifee060s"
local controls_3 "SCt_tenure_1960 caselawDK jury60DK"

local controlsets=3

local depvars " mktcapgdp_19982007 crdgdp_19982007 unempl_total_19982007 corrupt_19982007 cont_enf_days_2006 "
local depvars_short " crdgdp_19982007 unempl_total_19982007 corrupt_19982007 cont_enf_days_2006 "
local depvarnames "MarketCap/GDP Credit/GDP Corruption Unemployment CourtDuration"
local depvarnumber=5
local gdpcontrol " ln_gdppcppp_19982007 "


*** Growth: saturated regressions ***

local i=1

foreach s of numlist 0 1 2 {
forvalues c=1/3 {

reg gro6007p `LOCOgroups' lgdppc_1960_pwt63 `extracontrol' `controls_`c'' `sample_`s'', noconstant

forvalues t=1/`testnumber' {
capture: lincom `test_`t''
capture: estadd scalar b_test_`t'=r(estimate)
capture: estadd scalar se_test_`t'=r(se)
capture: estadd scalar t_test_`t'=r(estimate)/r(se)
capture: estadd scalar p_test_`t'=2*ttail(e(df_r),abs(r(estimate)/r(se)))
}

testparm `LOCOgroups', equal
estadd scalar F_LOCO=r(F)
estadd scalar p_LOCO=r(p)

if `c'>1 {

test `controls_`c''   /* normal F-test */
estadd scalar F_controls=r(F)
estadd scalar p_controls=r(p)

}

estimates store results_`i++'

}
}

esttab results_* using growthregs_saturated, csv replace b(a2) se(a2) noobs star(* .1 ** .05 *** .01) drop(LCO_MiBe LCO_MiOth LCO_MiNC LCO_GeNC LCO_ScOth LCO_ScNC LCO_FrNC LCO_CoNC) coeflabels(LCO_FrFr "French Law X French Colony" LCO_FrBe "French Law X Other Colony" LCO_CoBr "Common Law X British Colony" LCO_MiBr "Mixed Law X British Colony" lpgdp60P "GDP pc PPP 1960" p60s "Primary Schooling 1960" lifee060s "Life Expectancy 1960" jury60DK "Juries" caselawDK "Case Law" SCt_tenure_1960 "S.Ct. Tenure")  scalar(N r2 b_test_1 se_test_1 p_test_1 b_test_2 se_test_2 p_test_2  b_test_3 se_test_3 p_test_3 F_LOCO p_LOCO F_controls p_controls) nonotes



estimates drop _all



*** Growth: competing sets of LO dummies, CO dummies ***

local i=1

forvalues s=0/2 {
forvalues c=1/3 {

xi: reg gro6007p i.LO i.CO lgdppc_1960_pwt63 `extracontrol' `controls_`c'' `sample_`s''

testparm _ILO*
estadd scalar F_LO=r(F)
estadd scalar p_LO=r(p)

testparm _ICO*
estadd scalar F_CO=r(F)
estadd scalar p_CO=r(p)

testparm _ILO* _ICO*
estadd scalar F_LOCO=r(F)
estadd scalar p_LOCO=r(p)

capture: lincom _ILO_2-_ILO_3
capture: estadd scalar b_Com_v_Mixed=r(estimate)
capture: estadd scalar se_Com_v_Mixed=r(se)
capture: estadd scalar p_Com_v_Mixed=2*ttail(e(df_r),abs(r(estimate)/r(se)))

if `c'>1 {
test `controls_`c''
estadd scalar F_controls=r(F)
estadd scalar p_controls=r(p)


}

estimates store results_`i++'


}
}

esttab results_* using growthregs_competingdummies, csv replace b(a2) se(a2) noobs star(* .1 ** .05 *** .01) coeflabels(_ILO_2 "Common" _ILO_3 "Mixed" _ILO_4 "German Civil" _ILO_5 "Scandinavian Civil" _ILO_6 "Islamic" _ICO_2 "Britain" _ICO_3 "Other French Civil Law Country" _ICO_4 "Other" _ICO_5 "None" _ICO_6 "Austro-Hungary" lpgdp60P "GDP pc PPP 1960" p60s "Primary Schooling 1960" lifee060s "Life Expectancy 1960" jury60DK "Juries" caselawDK "Case Law" SCt_tenure_1960 "S.Ct. Tenure")  scalar(r2 N F_LO p_LO F_CO p_CO F_LOCO p_LOCO b_Com_v_Mixed se_Com_v_Mixed p_Com_v_Mixed F_controls p_controls) nonotes

estimates drop _all
capture: drop _I*



***
** Other dependent variables
***

*** other depvars: saturated regressions

* create the matrix to store the results in *
local rows=`controlsets'*(3*`testnumber'+6)-2
local columns=`depvarnumber'+2

matrix M=J(`rows',`columns',.)
matrix coln M=`depvarnames' Cross-Equation-p-values Without-MktCap

local row_names "N r2 b(1) se(1) p(1)" // this starts to create the string required to name the rows
forvalues t=2/`testnumber' {
local row_names "`row_names' b(`t') se(`t') p(`t')"
}
local row_names "`row_names' F(LOCO) p(LOCO)"
local row_names_c "`row_names' F(controls) p(controls)"
forvalues c=2/`controlsets' {
local row_names "`row_names' `row_names_c'"
}

matrix rown M=`row_names'


* start looping over samples

foreach s of numlist 0 1 2 {

matrix M=M-M // this empties the matrix from previous results

local i=1
forvalues c=1/`controlsets' {
local d=1

foreach depvar of varlist `depvars' {
local j=`i'

capture: reg `depvar' `gdpcontrol' `extracontrol' `LOCOgroups' `controls_`c'' `sample_`s''

matrix M[`j++',`d']=e(N)
matrix M[`j++',`d']=e(r2)

forvalues t=1/`testnumber' {
capture: lincom `test_`t''
matrix M[`j++',`d']=r(estimate)
matrix M[`j++',`d']=r(se)
matrix M[`j++',`d']=2*ttail(e(df_r),abs(r(estimate)/r(se)))
}

capture: testparm `LOCOgroups'
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)

if `c'>1 {
capture: test `controls_`c''
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)


}  // closes if-clause for control variable tests

local ++d

} // ends loop over dependent variables


foreach depvarlist in depvars depvars_short {   // do the cross-equation tests twice: once without mktcap/GDP

local j=`i'

capture: mvreg ``depvarlist'' = `gdpcontrol' `extracontrol' `LOCOgroups' `controls_`c'' `sample_`s''

local neq=e(k_eq)

matrix M[`j++',`d']=e(N)
local ++j

forvalues t=1/`testnumber' {
tokenize "`test_`t''", parse("-")
forvalues e=1/`neq' {
capture: if `e'==1 test `1'=`3'
capture: else test [#`e']`1'=[#`e']`3', accumulate
}
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(df_r)
matrix M[`j++',`d']=r(p)
}

local counter=1
foreach dummy of varlist `LOCOgroups' {
capture: if `counter++'==1 test `dummy'
capture: else test `dummy', accumulate
}
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)


if `c'>1 {
local counter=1
foreach control of varlist `controls_`c'' {
capture: if `counter++'==1 test `control'
capture: else test `control', accumulate
}
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)


} // ends if clause for control tests

local ++d

} // closes loop over different depvarset: short and long

local i=`j'

} // ends loop over controlsets

mat2txt , matrix(M) saving(othermvreg_saturated_sample_`s') title(Other dependent variables. Saturated regressions. Sample `s') replace

} // ends loop over samples


*** other depvars: competing sets of LO, CO dummies

* create the matrix to store the results in *
local rows=`controlsets'*(2*2+2*4+2)-2 // we need for each set of controls two (b, se) times two (LO_common, CO_British) lines for the regression coefficients plus two (F, p) times four (LO, CO, LO&CO, controls) lines for the F-tests, and two lines for the sum states (N, r2), minus two lines for missing controls in first block

local columns=`depvarnumber'+2

matrix M=J(`rows',`columns',.)
matrix coln M=`depvarnames' Cross-Equation-p-values Without-MktCap

local row_names "b(Common) se(Common) b(British) se(British) N r2 F(LO) p(LO) F(CO) p(CO) F(LOCO) p(LOCO)" // this starts to create the string required to name the rows
local row_names_c "`row_names' F(controls) p(controls)"
forvalues c=2/`controlsets' {
local row_names "`row_names' `row_names_c'"
}

matrix rown M=`row_names'


foreach s of numlist 0 1 2 {  // start looping over samples
matrix M=M-M

local i=1
forvalues c=1/3 {  // start looping over blocks of control variables
local d=1

foreach depvar of varlist `depvars' {  // start looping over dependent variables
local j=`i'

xi: reg `depvar' `gdpcontrol' `extracontrol' i.LO i.CO `controls_`c'' `sample_`s''

matrix M[`j++',`d']=_b[_ILO_2]
matrix M[`j++',`d']=_se[_ILO_2]

matrix M[`j++',`d']=_b[_ICO_2]
matrix M[`j++',`d']=_se[_ICO_2]

matrix M[`j++',`d']=e(N)
matrix M[`j++',`d']=e(r2)

testparm _ILO*
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)

testparm _ICO*
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)

testparm _ILO* _ICO*
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)


if `c'>1 {
test `controls_`c''
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)


} // end loop over if-clause for control variable tests


local ++d

} // end loop over dependent variables


foreach depvarlist in depvars depvars_short {   // do the cross-equation tests twice: once without mktcap/GDP

local j=`i'

capture: xi: mvreg ``depvarlist'' = `gdpcontrol' `extracontrol' i.LO i.CO `controls_`c'' `sample_`s''

foreach dummy of varlist _ILO_2 _ICO_2 {
test `dummy'
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)
}


matrix M[`j++',`d']=e(N)
local ++j

foreach block in _ILO* _ICO* "_ILO* _ICO*" {
local counter=1
foreach dummy of varlist `block' {
if `counter++'==1 test `dummy'
else test `dummy', accumulate
}
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)
}


if `c'>1 {
local counter=1
foreach control of varlist `controls_`c'' {
if `counter++'==1 test `control'
else test `control', accumulate
} // ends loop over control tests
matrix M[`j++',`d']=r(F)
matrix M[`j++',`d']=r(p)


} // closes if statement

local ++d

} // end loop over long and short depvarlist cross-equation tests

local i=`j'

} // end loop over control variable blocks

capture: drop _I*

mat2txt , matrix(M) saving(othermvregs_LO_v_CO_sample_`s') title(Other dependent variables. LO v CO. Sample `s') replace

}  // end loop over samples



log close

* the end *