clear
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
drop 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
}
   
gen wage=main_wages/employment
bysort IO : egen q2=pctile(wage),p(50)

gen tfp_le=1 if wage<q2
replace tfp_le=0 if wage>q2 

   
forvalues i=0(1)3{
novarabbrev gen fin1_htfp_`i'=fin1_up_alpha_`i'*tfp_le
}

forvalues i=0(1)3{
novarabbrev gen fin2_htfp_`i'=fin2_up_alpha_`i'*tfp_le
}

forvalues i=0(1)3{
novarabbrev gen fin4_htfp_`i'=fin4_up_alpha_`i'*tfp_le
}


xtreg dvar     fin1_htfp_*   in_I in_K in_S  ///
  if dvar<0.985  ,fe
est store m1

xtreg dvar     fin2_htfp_*   in_I in_K in_S  ///
  if dvar<0.985  ,fe
est store m2

xtreg dvar     fin4_htfp_*   in_I in_K in_S  ///
  if dvar<0.985 ,fe
est store m3    

 esttab m1 m2 m3 using table_wage.rtf, ///
   p star(* 0.1 ** 0.05 *** 0.01) r2 drop(in_I in_K in_S ) nogap replace
