***************************************************************
***************************************************************
*** Titel des Projekts: Estimating the causal effect of collective bargaining on the wage distribution
*** Datengrundlage: Gehalt- und Lohnstrukturerhebung (GLS) 2001 und Verdienststrukturerhebung (VSE) 2006
***
*** Dateiname des Programmcodes: 02_Fairlie_Ost.do
*** Dateiname des Log-Files: 	 02_Fairlie_selbst_Ost.log
*** Dateiname des ado-files:	 02_Fairlieselbst_ost_reversed.ado
***
*** von: Katrin Sommerfeld
*** E-Mail: sommerfeld@zew.de
*** Tel: 0621-1235 216
***
***
*** Grundriss des Programms:
***
***
*********************************************************
*
* 03 _ Fairlie-Zerlegung, hier selbst programmiert
*
* 0. Vorbereitung: Sample und Matrizen generieren
* 1. Simple Zerlegung: a: Vorbereiten, b: Anzeigen
* 2. Sequentielle Zerlegung: a: Vorbereiten, b: Anzeigen
*
**********************************************************

display "ado: 02 Fairlie selbst programmiert: OST reversed"

capture program drop fairlieselbstrev
program define fairlieselbstrev

set rmsg on


scalar drop app_x06_b01 app_x01_b06
scalar drop app_counter1 app_counter2 app_counter3 app_counter4 app_counter5 app_counter6
scalar drop delta1 delta2 delta3 delta4 delta5 delta6 delta7
scalar drop delta1__prozent delta2__prozent delta3__prozent delta4__prozent delta5__prozent delta6__prozent delta7__prozent

scalar list


*******************************************************************
*** 1.b Grobe Zerlegung
display "1. Grobe Zerlegung: Reverse direction: x01 * b06"
cap drop p_x01_b06 np_x01_b06
qui gen p_x01_b06 = b06_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm + b06_6*size1_dm + b06_7*size2_dm + b06_8*size3_dm + b06_9*size4_dm + b06_10*male_dm + b06_11*region8_dm + b06_12*region10_dm + b06_13*region11_dm + b06_14*region12_dm + b06_15*w1_dm + b06_16*w2_dm + b06_17*w3_dm + b06_18*w4_dm + b06_19*w5_dm + b06_20*w6_dm + b06_21*w7_dm + b06_22*w8_dm   + b06_23*w10_dm + b06_24*w11_dm + b06_25*w12_dm + b06_26*w13_dm + b06_27*w14_dm + b06_28*w15_dm + b06_29*w16_dm + b06_30*w17_dm + b06_31*w18_dm + b06_32*w19_dm + b06_33*w20_dm + b06_34*w21_dm + b06_35*w22_dm + b06_36*w23_dm + b06_37*w24_dm + b06_38*w25_dm + b06_39*w26_dm + b06_40*w27_dm + b06_41*w28_dm    if dsample_01==1
qui gen np_x01_b06 = normal(p_x01_b06) 		// Normalverteilung des Matrixproduktes
tabstat np_x01_b06 	[aweight=weight],  stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum np_x01_b06  [aweight=weight]		// Durchschnittliche geschätzte Tarifbindungsquote
scalar app_x01_b06 = r(mean)     		/* average predicted probability  */

scalar Char_Ef_rev = app_x06_b06 - app_x01_b06
scalar Coef_Ef_rev = app_x01_b06 - app_x01_b01
scalar Residual_rev = diff - Char_Ef_rev - Coef_Ef_rev     /* Residual = delta_3 per definition */

scalar Char_Ef_rev__prozent =  Char_Ef_rev / diff * 100
scalar Coef_Ef_rev__prozent =  Coef_Ef_rev / diff * 100
scalar Residual_rev__prozent = Residual_rev/ diff * 100   /* Residual = delta_3 per definition */


display "diff = " diff
display "Coef_Ef reverse = " Coef_Ef_rev " Prozent:  " Coef_Ef_rev__prozent
display "Char_Ef reverse = " Char_Ef_rev " Prozent:  " Char_Ef_rev__prozent
*display "Residual reverse = " Residual_rev " Prozent:  " Residual_rev__prozent






********************************************************************
*** 2.a Sequentielle Zerlegung vorbereiten

/* Zunächst werden die Counterfactuals berechnet */

/*** Matching required ***/
cap gen uni = uniform()
sort uni

tab dtreat, m

gen dtreat_rev = .
replace dtreat_rev = 1 if dtreat==0
replace dtreat_rev = 0 if dtreat==1

tab dtreat dtreat_rev, m


display "Step 1 reversed (Step 6 normal): Change individual characteristics from 2006 to 2001 by PS-Matching"
* Change individual characteristics from 2006 to 2001
/*** Zu delta1 bzw. delta2 ***/
* Match to every 2001-observation (='treatment') a 2006-observation (='control')



display "1. Matching by kernel"
gsort /*-dtreat*/ uni 		// Note: gsort allows sorting in descending order with "-"
psmatch2 dtreat_rev i.size1 i.size2 i.size3 i.size4  i.region8 i.region10 i.region11 i.region12    i.w1 i.w2 i.w3 i.w4 i.w5 i.w6 i.w7 i.w8  i.w10 i.w11 i.w12 i.w13 i.w14 i.w15 i.w16 i.w17 i.w18 i.w19 i.w20 i.w21 i.w22 i.w23 i.w24 i.w25 i.w26 i.w27 i.w28,   kernel kerneltype(normal)
tab _support
tabstat _weight if _treated==0, stat(N mean sd p50)
cap drop weight1
qui gen weight1 = weight * _weight
qui replace weight1 = . if _treated==1
display "B Note: N.obs for _weight and for weight1 have to  be identical in the control group!"
tabstat weight1, 	stat(n mean sd p50)

cap drop counter1help n_counter1
qui gen counter1help = b06_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm    + b06_6*size1_dm + b06_7*size2_dm + b06_8*size3_dm + b06_9*size4_dm + b06_10*male_dm + b06_11*region8_dm + b06_12*region10_dm + b06_13*region11_dm + b06_14*region12_dm    + b06_15*w1_dm + b06_16*w2_dm + b06_17*w3_dm + b06_18*w4_dm + b06_19*w5_dm + b06_20*w6_dm + b06_21*w7_dm + b06_22*w8_dm   + b06_23*w10_dm + b06_24*w11_dm + b06_25*w12_dm + b06_26*w13_dm + b06_27*w14_dm + b06_28*w15_dm + b06_29*w16_dm + b06_30*w17_dm + b06_31*w18_dm + b06_32*w19_dm + b06_33*w20_dm + b06_34*w21_dm + b06_35*w22_dm + b06_36*w23_dm + b06_37*w24_dm + b06_38*w25_dm + b06_39*w26_dm + b06_40*w27_dm + b06_41*w28_dm    if weight1<.   // if dsample_06==1
qui gen n_counter1 = normal(counter1help)
tabstat n_counter1 	 	[aweight=weight1]	, 	stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum n_counter1 		[aweight=weight1]
scalar app_counter1 = r(mean)





********************************************************************************************************************
display "Step 2 reversed (Step 5 normal): Additionally change Firm characteristics from 2006 to 2001 by PS-Matching"
* Match to every 2001-observation (='treatment') a 2006-observation (='control')


display "2. Matching by kernel"
sort  uni
psmatch2 dtreat_rev i.w1 i.w2 i.w3 i.w4 i.w5 i.w6 i.w7 i.w8  i.w10 i.w11 i.w12 i.w13 i.w14 i.w15 i.w16 i.w17 i.w18 i.w19 i.w20 i.w21 i.w22 i.w23 i.w24 i.w25 i.w26 i.w27 i.w28, kernel kerneltype(normal)
tab _support

cap drop weight2
qui gen weight2 = weight * _weight
qui replace weight2 = . if _treated==1
tabstat weight2, 	stat(n mean sd p50)

cap drop counter2help n_counter2
qui gen counter2help = b06_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm    + b06_6*size1_dm + b06_7*size2_dm + b06_8*size3_dm + b06_9*size4_dm + b06_10*male_dm + b06_11*region8_dm + b06_12*region10_dm + b06_13*region11_dm + b06_14*region12_dm     + b06_15*w1_dm + b06_16*w2_dm + b06_17*w3_dm + b06_18*w4_dm + b06_19*w5_dm + b06_20*w6_dm + b06_21*w7_dm + b06_22*w8_dm   + b06_23*w10_dm + b06_24*w11_dm + b06_25*w12_dm + b06_26*w13_dm + b06_27*w14_dm + b06_28*w15_dm + b06_29*w16_dm + b06_30*w17_dm + b06_31*w18_dm + b06_32*w19_dm + b06_33*w20_dm + b06_34*w21_dm + b06_35*w22_dm + b06_36*w23_dm + b06_37*w24_dm + b06_38*w25_dm + b06_39*w26_dm + b06_40*w27_dm + b06_41*w28_dm    if weight2<.   // if dsample_06==1
qui gen n_counter2 = normal(counter2help)
tabstat n_counter2 	 	[aweight=weight2]	, 	stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum n_counter2 		[aweight=weight2]
scalar app_counter2 = r(mean)





*********************************************
* Step 3: Charact 2001 with Coeff 2006
* counter3 = xpersonal_01 * bpersonal_06' + xfirm_01 * bfirm_06' + xwz_01 * bwz_06' + const06
cap drop counter3help n_counter3
qui gen counter3help = b06_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm    + b06_6*size1_dm + b06_7*size2_dm + b06_8*size3_dm + b06_9*size4_dm + b06_10*male_dm + b06_11*region8_dm + b06_12*region10_dm + b06_13*region11_dm + b06_14*region12_dm    + b06_15*w1_dm + b06_16*w2_dm + b06_17*w3_dm + b06_18*w4_dm + b06_19*w5_dm + b06_20*w6_dm + b06_21*w7_dm + b06_22*w8_dm   + b06_23*w10_dm + b06_24*w11_dm + b06_25*w12_dm + b06_26*w13_dm + b06_27*w14_dm + b06_28*w15_dm + b06_29*w16_dm + b06_30*w17_dm + b06_31*w18_dm + b06_32*w19_dm + b06_33*w20_dm + b06_34*w21_dm + b06_35*w22_dm + b06_36*w23_dm + b06_37*w24_dm + b06_38*w25_dm + b06_39*w26_dm + b06_40*w27_dm + b06_41*w28_dm    if dsample_01==1
qui gen n_counter3 = normal(counter3help)
tabstat n_counter3 	[aweight=weight]	, 			stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum n_counter3  [aweight=weight]
scalar app_counter3 = r(mean)




* Step 4: Change constant from 2006 to 2001
* counter4 = xpersonal_01 * bpersonal_06' + xfirm_01 * bfirm_06' + xwz_01 * bwz_06' + mat_c01
cap drop counter4help n_counter4
qui gen counter4help = b01_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm    + b06_6*size1_dm + b06_7*size2_dm + b06_8*size3_dm + b06_9*size4_dm + b06_10*male_dm + b06_11*region8_dm + b06_12*region10_dm + b06_13*region11_dm + b06_14*region12_dm    + b06_15*w1_dm + b06_16*w2_dm + b06_17*w3_dm + b06_18*w4_dm + b06_19*w5_dm + b06_20*w6_dm + b06_21*w7_dm + b06_22*w8_dm   + b06_23*w10_dm + b06_24*w11_dm + b06_25*w12_dm + b06_26*w13_dm + b06_27*w14_dm + b06_28*w15_dm + b06_29*w16_dm + b06_30*w17_dm + b06_31*w18_dm + b06_32*w19_dm + b06_33*w20_dm + b06_34*w21_dm + b06_35*w22_dm + b06_36*w23_dm + b06_37*w24_dm + b06_38*w25_dm + b06_39*w26_dm + b06_40*w27_dm + b06_41*w28_dm    if dsample_01==1
qui gen n_counter4 = normal(counter4help)
tabstat n_counter4 	[aweight=weight]	, 			stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum n_counter4  [aweight=weight]
scalar app_counter4 = r(mean)



* Step 5: Change WZ coefficients from 2006 to 2001
* counter5 = xpersonal_01 * bpersonal_06' + xfirm_01 * bfirm_06' + xwz_01 * bwz_01' + mat_c01
cap drop counter5help n_counter5
qui gen counter5help = b01_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm    + b06_6*size1_dm + b06_7*size2_dm + b06_8*size3_dm + b06_9*size4_dm + b06_10*male_dm + b06_11*region8_dm + b06_12*region10_dm + b06_13*region11_dm + b06_14*region12_dm     + b01_15*w1_dm + b01_16*w2_dm + b01_17*w3_dm + b01_18*w4_dm + b01_19*w5_dm + b01_20*w6_dm + b01_21*w7_dm + b01_22*w8_dm   + b01_23*w10_dm + b01_24*w11_dm + b01_25*w12_dm + b01_26*w13_dm + b01_27*w14_dm + b01_28*w15_dm + b01_29*w16_dm + b01_30*w17_dm + b01_31*w18_dm + b01_32*w19_dm + b01_33*w20_dm + b01_34*w21_dm + b01_35*w22_dm + b01_36*w23_dm + b01_37*w24_dm + b01_38*w25_dm + b01_39*w26_dm + b01_40*w27_dm + b01_42*w28_dm    if dsample_01==1
qui gen n_counter5 = normal(counter5help)
tabstat n_counter5 	[aweight=weight]	, 			stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum n_counter5  [aweight=weight]
scalar app_counter5 = r(mean)



* Step 6: Change firm coefficients from 2006 to 2001
* counter6 = xpersonal_01 * bpersonal_06' + xfirm_01 * bfirm_01' + xwz_01 * bwz_01' + mat_c01
cap drop counter6help n_counter6
qui gen counter6help = b01_0*one + b06_1*educ_low_dm + b06_2*educ_high_dm + b06_3*educ_na_dm + b06_4*age_dm + b06_5*tenure_dm    + b01_6*size1_dm + b01_7*size2_dm + b01_8*size3_dm + b01_9*size4_dm + b01_10*male_dm + b01_11*region8_dm + b01_12*region10_dm + b01_13*region11_dm + b01_14*region12_dm     + b01_15*w1_dm + b01_16*w2_dm + b01_17*w3_dm + b01_18*w4_dm + b01_19*w5_dm + b01_20*w6_dm + b01_21*w7_dm + b01_22*w8_dm   + b01_23*w10_dm + b01_24*w11_dm + b01_25*w12_dm + b01_26*w13_dm + b01_27*w14_dm + b01_28*w15_dm + b01_29*w16_dm + b01_30*w17_dm + b01_31*w18_dm + b01_32*w19_dm + b01_33*w20_dm + b01_34*w21_dm + b01_35*w22_dm + b01_36*w23_dm + b01_37*w24_dm + b01_38*w25_dm + b01_39*w26_dm + b01_40*w27_dm + b01_41*w28_dm    if dsample_01==1
qui gen n_counter6 = normal(counter6help)
tabstat n_counter6 	[aweight=weight]	, 			stat(n mean sd p1 p5 p10 p25 p50 p75 p90 p95 p99)
qui sum n_counter6  [aweight=weight]
scalar app_counter6 = r(mean)










********************************************************************
*** 2.b Sequentielle Zerlegung anzeigen
/* Absolute Changes */
scalar delta1 = app_x06_b06  - app_counter1 	// X individuell
scalar delta2 = app_counter1 - app_counter2		// X Firm
scalar delta3 = app_counter2 - app_counter3		// X Wirtschaftszweig
scalar delta4 = app_counter3 - app_counter4		// Beta Konstante
scalar delta5 = app_counter4 - app_counter5		// Beta Wirtschaftszweig
scalar delta6 = app_counter5 - app_counter6		// Firmen-Betas
scalar delta7 = app_counter6 - app_x01_b01		// individuelle Betas


/* Percentages */
scalar delta1__prozent = delta1/ diff * 100
scalar delta2__prozent = delta2/ diff * 100
scalar delta3__prozent = delta3/ diff * 100
scalar delta4__prozent = delta4/ diff * 100
scalar delta5__prozent = delta5/ diff * 100
scalar delta6__prozent = delta6/ diff * 100
scalar delta7__prozent = delta7/ diff * 100

scalar list

/* Display */
display "diff = " diff
display "Char_Ef rev = " Char_Ef_rev " Prozent:  " Char_Ef_rev__prozent
display "Coef_Ef rev = " Coef_Ef_rev " Prozent:  " Coef_Ef_rev__prozent
display "***"
display "Individuelle Charakteristika: delta1 = " delta1 " Prozent:  " delta1__prozent
display "Firmen-Charakteristika: delta2 = " delta2 " Prozent:  " delta2__prozent
display "Charakteristikum Wirtschaftszweig: delta3 = " delta3 " Prozent:  " delta3__prozent
display "Beta Konstante: delta4 = " delta4 " Prozent:  " delta4__prozent
display "Koeff. Wirtschaftszweig: delta5 = " delta5 " Prozent:  " delta5__prozent
display "Koeff. Firma: delta6 = " delta6 " Prozent:  " delta6__prozent
display "Koeff. individuell: delta7 = " delta7 " Prozent:  " delta7__prozent



end