//  financial constraint and GVC upgrading //

clear
  set more off
  global PATH1 "E:\Research\GVCs\data\financial_constraint\by_year_upstreamness2"
  global PATH2 "E:\Research\GVCs\data\financial_constraint\by_year_upstreamness2\results"
  cd $PATH1
  use workhouse_bench.dta	 

merge m:1 IO using alpha_90.dta
drop _merge

sum alpha
return list
gen x=r(max)

sort alpha
_pctile alpha, nq(4)
return list   

gen alpha_0=1 if alpha<r(r1)
replace alpha_0=0 if alpha_0==.	

forvalues i=1(1)2{
  local j=`i'+1
  gen alpha_`i'=1 if alpha>=r(r`i') & alpha<r(r`j')
  replace alpha_`i'=0 if alpha_`i'==.
  }	
  
gen alpha_3=1 if alpha>=r(r3) & alpha<x
replace alpha_3=0 if alpha_3==.

forvalues i=0(1)3{
gen fin1_up_alpha_`i'=alpha_`i'*upstream_fin1
}
forvalues i=0(1)3{
gen fin2_up_alpha_`i'=alpha_`i'*upstream_fin2
}
forvalues i=0(1)3{
gen fin3_up_alpha_`i'=alpha_`i'*upstream_fin3
}
forvalues i=0(1)3{
gen fin4_up_alpha_`i'=alpha_`i'*upstream_fin4
}


forvalues i=0(1)3{
gen fin1_up_tfp_lp_alpha_`i'=alpha_`i'*upstream_fin1*tfp_lp
}

forvalues i=0(1)3{
gen fin2_up_tfp_lp_alpha_`i'=alpha_`i'*upstream_fin2*tfp_lp
}

forvalues i=0(1)3{
gen fin3_up_tfp_lp_alpha_`i'=alpha_`i'*upstream_fin3*tfp_lp
}

forvalues i=0(1)3{
gen fin4_up_tfp_lp_alpha_`i'=alpha_`i'*upstream_fin4*tfp_lp
}


duplicates drop id year,force
xtset id year
xtreg dvar    fin1_up_alpha_* in_I in_K in_S tfp_lp $dum_con if dvar<0.985,fe 
est store m1
xtreg dvar    fin2_up_alpha_* in_I in_K in_S tfp_lp $dum_con if dvar<0.985,fe 
est store m2
xtreg dvar    fin4_up_alpha_* in_I in_K in_S tfp_lp $dum_con if dvar<0.985,fe 
est store m3
esttab  m1 m2 m3  using "$PATH2\Table_2.1.rtf", ///
   p star(* 0.1 ** 0.05 *** 0.01) r2 drop($dum_con )  nogap replace
