********************************************************************* **This programme is applying the tetrad method of Head et al. 2008.** **to run gravity regressions on large panels of trade flows, when ** **simple methods as LSDV are not practically doable ** **It also makes use of Cameron et al. (2006) multiway clustering, ** **and uses their stata ado file that must be installed. ** ********************************************************************* * *Please cite: *Head, K. Ries, J. and T. Mayer, 2008, "The erosion of colonial trade linkages after independence", CEPR DP 6951 *when using this code. * *First version: August 2008 *contact: tmayer@univ-paris1.fr * *Requirements: * *1) The programme requires a list of variables where the first is the log of flows *and then the list of RHS variables. Those should vary by dyad and time, although *including variables like distance won't make the programme crash. * *2) The programme looks for three variables iso_o iso_d and year, that must be there. *iso_o designates the exporting country, iso_d the importer, and both variables should *be string * *3) The programme needs a reference importer and a reference exporter to be specified *as k and ell options respectively. For instance, when taking USA as a reference importer *and Germany as a reference exporter, syntax looks like: *tetrad lx rta gatt cu, k(USA) ell(DEU) * *4) All variables should be listed distinctively without shortcuts such as * or - * *Start of programme: program define tetrad, eclass byable(onecall) sortpreserve syntax anything [if] [in], K(string) ELL(string) *************** *1) Tetrading * *************** *variables to be tetraded global vars "`anything'" local numvars : word count `anything' *set reference countries global k = "`k'" global ell = "`ell'" qui gen byte k = iso_d == "$k" di in white "***************" di in green "Reference importer: " "`k'" di in green "Reference exporter: " "`ell'" sort year iso_o k foreach x of varlist $vars { qui by year iso_o : gen `x'r=`x'-`x'[_N] if iso_d[_N]=="$k" } qui save ttemp,replace * use ttemp, clear qui keep if iso_o=="$ell" foreach x in $vars { rename `x'r `x'r_rec } qui keep iso_d year *_rec sort year iso_d qui save ttemp1,replace * use ttemp,clear sort year iso_d qui merge year iso_d using ttemp1 * foreach x in $vars { qui replace `x'r = `x'r - `x'r_rec label var `x'r " `x' tetraded" } drop *r_rec _merge k * qui drop if iso_d=="`k'"|iso_o=="`k'"|iso_d=="`ell'"|iso_o=="`ell'" qui save ttest, replace erase ttemp.dta erase ttemp1.dta di in white "***************" di in ye "End of tetrading" * **************** *2) Regression * **************** *generating the varlist of tetraded variables to be demeaned* tokenize `anything' local anythingr "`1'" + "r " forvalues i = 2(1)`numvars'{ local anythingr = "`anythingr'" + "``i''" + "r " } *defining variables global tvar = "`anythingr'" global RHS = "$tvar" egen iso_oy=group(iso_o year) egen iso_dy=group(iso_d year) egen dyad=group(iso_o iso_d) qui tab year, gen(yr) qui sum year local numyr = r(max) - r(min) +1 * di in green "First year: " r(min) di in green "Last year: " r(max) foreach x of varlist $tvar{ qui drop if `x'==. } foreach x of varlist $tvar yr1-yr`numyr'{ qui egen mn1 = mean(`x'), by(dyad) qui gen double `x'_d = `x'- mn1 qui drop mn1 label var `x'_d " `x' tetraded and demeaned" } * *generating the varlist of tetraded + demeaned variables tokenize `anything' local anythingrd "`1'" + "r_d " forvalues i = 2(1)`numvars'{ local anythingrd = "`anythingrd'" + "``i''" + "r_d " } global tvar_d = "`anythingrd'" di in ye "Original list of RHS variables: " "`anything'" di in ye "Tetraded-demeaned RHS variables: " "`anythingr'" di in white "Start of regression:" di in white "********************" cgmreg `anythingrd' yr2_d-yr`numyr'_d, cluster(dyad iso_oy iso_dy) end