********************************************************************************
				*** reproduce of VS in Trade by Pahl & Timmer ***

global dir="..."	 // set directory		
				
use "$dir\repro_dta.dta", clear

********************************************************************************						
/// Figure 1:
egen yearmint=min(year) if VAXDr!=., by(CC)
egen yearmaxt=max(year) if VAXDr!=., by(CC)
// 1(a)
egen sumGX=sum(GX) if yearmint<1980 & yearmax==2013, by(year)
egen sumVAXD=sum(VAXD) if yearmint<1980 & yearmax==2013, by(year)
gen aggVAXDr_weigh=sumVAXD/sumGX
egen sumGX2=sum(GX) , by(year)
egen sumVAXD2=sum(VAXD) , by(year)
gen aggVAXDr_weigh2=sumVAXD2/sumGX2
twoway (line aggVAXDr_weigh year,  sort ) (line aggVAXDr_weigh2 year if year>=1995 & year<=2010,  sort ) , yscale(range(0.65 0.90)) ylabel(0.65(0.05)0.90)  ytitle(VAX-D ratio) xtitle(Year) legend(off) // Breakpoints obtained in EViews 10
graph export "$dir\fig1a.png", as(png) replace

//1(b)
egen aggregateVAXDr=mean(VAXDr) if yearmint<1980 & yearmax==2013, by(year)
twoway (line aggregateVAXDr year, xline(1980 1986 1995) sort ) , yscale(range(0.65 0.90)) ylabel(0.65(0.05)0.90)  ytitle(VAX-D ratio) xtitle(Year) legend(off) // Breakpoints obtained in EViews 10
graph export "$dir\fig1b.png", as(png) replace

/// Figure 3:
gen VAXD_Z=VAXDr_Z*GX
gen VAXD_va=VAXDr_va*GX
gen VAXD_e=VAXDr_e*GX
gen VAXD_Mc=VAXDr_Mc*GX
egen sumVAXD_Z=sum(VAXD_Z) if yearmint<1980 & yearmax==2013, by(year)
egen sumVAXD_va=sum(VAXD_va) if yearmint<1980 & yearmax==2013, by(year)
egen sumVAXD_e=sum(VAXD_e) if yearmint<1980 & yearmax==2013, by(year)
egen sumVAXD_Mc=sum(VAXD_Mc) if yearmint<1980 & yearmax==2013, by(year)
gen aggVAXDr_weigh_Z=sumVAXD_Z/sumGX
gen aggVAXDr_weigh_va=sumVAXD_va/sumGX
gen aggVAXDr_weigh_e=sumVAXD_e/sumGX
gen aggVAXDr_weigh_Mc=sumVAXD_Mc/sumGX
twoway (line aggVAXDr_weigh year, sort lcolor(navy)) (line aggVAXDr_weigh_Z year, lpattern(dash) sort lcolor(navy) )  (line aggVAXDr_weigh_va year, lpattern(shortdash) sort lcolor(navy))  (line aggVAXDr_weigh_e year, lpattern(longdash) sort lcolor(navy)) (line aggVAXDr_weigh_Mc year, lpattern(dash_dot) sort lcolor(navy)) , yscale(range(0.65 0.90)) ylabel(0.65(0.05)0.90)  ytitle(VAX-D ratio) xtitle(Year) legend(label(1 "All varying") label(2 "Z constant") label(3 "v constant") label(4 "e constant") label(5 "M constant") pos(7) ring(0) rows(5) order(5 3 4 2 1)) // Breakpoints obtained in EViews 10
graph export "$dir\fig3.png", as(png) replace
drop yearmint yearmaxt

/// Figure 4:
** BGD
twoway (line VAXDr year, xline(1995 2006) sort ) if iso3=="BGD" , yscale(range(0.5 1)) ylabel(0.5(0.1)1)  ytitle(VAX-D ratio) xtitle(Year) legend(off) // Breakpoints obtained in EViews 10
graph export "$dir\fig4_bgd.png", as(png) replace
** MYS
twoway (line VAXDr year, xline(1985 1991 2001) sort ) if iso3=="MYS", yscale(range(0.3 0.8)) ylabel(0.3(0.1)0.8)  ytitle(VAX-D ratio) xtitle(Year) legend(off) // Breakpoints obtained in EViews 10
graph export "$dir\fig4_mys.png", as(png) replace
** PHL
twoway (line VAXDr year, xline(2003) sort ) if iso3=="PHL", yscale(range(0.4 0.9)) ylabel(0.4(0.1)0.9)  ytitle(VAX-D ratio) xtitle(Year) legend(off) // Breakpoints obtained in EViews 10
graph export "$dir\fig4_phl.png", as(png) replace
** CHN
twoway (line VAXDr year, xline(1985 1994 1998 2005) sort ) if iso3=="CHN", yscale(range(0.5 1)) ylabel(0.5(0.1)1)  ytitle(VAX-D ratio) xtitle(Year) legend(off) // Breakpoints obtained in EViews 10
graph export "$dir\fig4_chn.png", as(png) replace

********************************************************************************
//// Construct table A4 on which table 2 and 3 are based \\\\\
use "$dir\repro_dta.dta", clear
rename breakt_05 breakpoint
*** data organization
egen yearmint=min(year) if VAXDr!=., by(CC)
egen yearmaxt=max(year) if VAXDr!=., by(CC)

gen breakID=1 if breakpoint!=.
egen seqID=max(breakpoint), by(iso3)
replace breakID=1 if year==yearmint
replace breakID=1 if year==yearmaxt

sort CC year
gen yeart=year if breakID==1
sort CC yeart
replace breakID=. if year==yearmaxt

sort CC yeart
sort iso3 year
by iso3: gen breaksum=sum(breakID)

egen group=group(iso3 breaksum)
forvalues i=1/7{
egen breakyearmax`i'=max(year) if breaksum==`i', by(iso3)
}

sort iso3 yeart
gen yeartdiff=yeart[_n+1]-yeart if yeart!=.
gen tmp_year=yeart+yeartdiff
egen tmp_yearm=max(tmp_year), by(group)
replace tmp_yearm=yeart if yeart!=.
sort CC year
xtset CC year

gen coef=.
gen t_val=.
gen df=.
gen p_val=.
forvalues i=1/7{
qui levelsof iso3 if breaksum==`i' & VAXDr!=., local(country)
foreach c of local country{
	quietly reg VAXDr year if iso3=="`c'" & breaksum==`i' 
	quietly replace coef=_b[year] if e(sample)
	quietly replace df=e(df_r)
	quietly replace t_val=_b[year]/_se[year] if e(sample)
	quietly replace p_val=2*ttail(df,abs(t_val))
}
}		

gen down=1 if p_val<=0.05 & coef<0
gen up=1 if p_val<=0.05 & coef>0 
gen cons=1 if p_val>0.05
rename yeartdiff yeartodiff
egen yeartdiff=max(yeartodiff), by(group)

keep CC iso3 year up down cons reg yearmint coef breaksum
drop if year==.
drop if CC==.
xtset CC year

by CC: ipolate yearmint year, gen(yearmin)
drop yearmint
drop CC

sort iso3 year
by iso3: replace down=2 if down==1 & down[_n-1]==1 & coef<coef[_n-1] 
by iso3: replace up=2 if up==1 & up[_n-1]==1 & coef>coef[_n-1] 

reshape wide up down cons breaksum coef, i(iso3 reg yearmin) j(year)
forvalues i=1970/2013{
replace cons`i'=0 if cons`i'==.
replace down`i'=0 if down`i'==.
replace up`i'=0 if up`i'==.
}
drop if yearmin==.
forvalues i=1970/2013{
gen ID`i'="0" if cons`i'==1
replace ID`i'="+" if up`i'==1
replace ID`i'="-" if down`i'==1
replace ID`i'="++" if up`i'==2
replace ID`i'="--" if down`i'==2
replace ID`i'="missing" if down`i'!=1 & cons`i'!=1 & up`i'!=1 & up`i'!=2 & down`i'!=2 // replace later in Excel file with empty field
}

drop down* up* cons*
order breaksum* coef* ID*
export excel iso3 reg ID* using "$dir\tableA4.xls", firstrow(variables) replace

********************************************************************************
use "$dir\repro_dta.dta", clear

/// Table 5
set more off
// column (1)
reg VAXDr lnGDPpc c.lnGDPpc#c.lnGDPpc i.CC if lnHumCap!=., robust
outreg2 using "$dir\table5.xls", replace drop(i.CC i.year) addtext(Country dummies, No, Time dummies, No) adjr2 ctitle(Full smaple)
//// slopes at min and max
margins, dydx(lnGDPpc) at(lnGDPpc=(6.1(0.1)12))
// column (2)
reg VAXDr lnGDPpc c.lnGDPpc#c.lnGDPpc i.CC if oilave<=5 & oecd==0 & lnHumCap!=., robust
outreg2 using "$dir\table5.xls", append drop(i.CC i.year) addtext(Country dummies, No, Time dummies, No) adjr2 ctitle(Non-Oil & non-OECD)
//// slopes at min and max
margins, dydx(lnGDPpc) at(lnGDPpc=(6.1(0.1)12))
// Lind-mehlum (2010)
reg VAXDr lnGDPpc lnGDPpc2 i.CC if oilave<=5 & oecd==0 & lnHumCap!=., robust
utest lnGDPpc lnGDPpc2
// column (3)
reg VAXDr lnGDPpc c.lnGDPpc#c.lnGDPpc i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=., robust
outreg2 using "$dir\table5.xls", append drop(i.CC i.year) addtext(Country dummies, No, Time dummies, No) adjr2 ctitle(Non-Oil & non-OECD)
//// slopes at min and max
margins, dydx(lnGDPpc) at(lnGDPpc=(6.1(0.1)12))
// Lind-mehlum (2010)
reg VAXDr lnGDPpc lnGDPpc2 i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=., robust
utest lnGDPpc lnGDPpc2
// column (4)
reg VAXDr lnGDPpc c.lnGDPpc#c.lnGDPpc lnHumCap i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=., robust
outreg2 using "$dir\table5.xls", append drop(i.CC i.year) addtext(Country dummies, No, Time dummies, No) adjr2 ctitle(Non-Oil & non-OECD)
//// slopes at min and max
margins, dydx(lnGDPpc) at(lnGDPpc=(6.1(0.1)12))
// Lind-mehlum (2010)
reg VAXDr lnGDPpc lnGDPpc2 lnHumCap i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=., robust
utest lnGDPpc lnGDPpc2
// column (5)
reg VAXDr lnGDPpc c.lnGDPpc#c.lnGDPpc lnHumCap lnK_EMP i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=., robust
outreg2 using "$dir\table5.xls", append drop(i.CC i.year) addtext(Country dummies, No, Time dummies, No) adjr2 ctitle(Non-Oil & non-OECD)
//// slopes at min and max
margins, dydx(lnGDPpc) at(lnGDPpc=(6.1(0.1)12))
// Lind-mehlum (2010)
reg VAXDr lnGDPpc lnGDPpc2 lnHumCap lnK_EMP i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=., robust
utest lnGDPpc lnGDPpc2
// column (6)
reg VAXDr_serv lnGDPpc c.lnGDPpc#c.lnGDPpc lnHumCap i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=. , robust
outreg2 using "$dir\table5.xls", append drop(i.CC i.year) addtext(Country dummies, Yes, Time dummies, Yes) adjr2 ctitle(Non-Oil & non-OECD)
//// slopes at min and max
margins, dydx(lnGDPpc) at(lnGDPpc=(6.1(0.1)12))
// Lind-mehlum (2010)
reg VAXDr_serv lnGDPpc lnGDPpc2 lnHumCap i.CC i.year if oilave<=5 & oecd==0 & lnHumCap!=. , robust
utest lnGDPpc lnGDPpc2

********************************************************************************

// Figure 2 and Table 1 from industry-level dataset
use "$dir\Pahl&Timmer_VSinTrade_Mar2019.dta", clear
egen yearmin=min(year) if VAXDr!=. & VAXDr!=0, by(iso3)
egen yearmax=max(year) if VAXDr!=. & VAXDr!=0, by(iso3)
egen sumGX=sum(GX) if yearmin<1980 & yearmax==2013, by(year ISIC)
egen sumVAXD=sum(VAXD) if yearmin<1980 & yearmax==2013, by(year ISIC)
gen VAXDr_wld=sumVAXD/sumGX

twoway (line VAXDr_wld year if ISIC=="17t19", lcolor(navy) lpattern(dash)) (line VAXDr_wld year if ISIC=="29t33", lcolor(navy)) if iso3=="ARG", legend(label(1 "Textiles") label(2 "Machinery") ring(0) position(7) rows(2)) ytitle(VAX-D ratio) xtitle(Year) yscale(range(0.65 0.95)) ylabel(0.65(0.05)0.95)
graph export "$dir\fig2.png", as(png) replace



























