/*******************************************************************************

COLLECTIVE MODEL -- EU-SILC DATA
Fall 2022

Filename: 	01_Basics.do
Authors: 	Jorge Velilla, José Alberto Molina, José Ignacio Giménez-Nadal
			Department of Economic Analysis
			MSc in Economics 2022, University of Zaragoza (Spain)
________________________________________________________________________________

This file provides instructions to compute baseline sample preparation, summary
statistics of main variables, t-type tests and tabulates, baseline estimates, 
and export estimation results.
________________________________________________________________________________

Licencia Creative Commons "http://creativecommons.org/licenses/by-nc-sa/4.0/"
Materiales de software en abierto para estudiantes del Master de Economía: 
Manejo de datos y obtención de evidencia empírica socioeconómica a través de 
STATA por José Alberto Molina, José Ignacio Giménez-Nadal y Jorge Velilla se 
distribuye bajo una Licencia Creative Commons Atribución-NoComercial-
CompartirIgual 4.0 Internacional.

*******************************************************************************/

* 	Clear memory
clear all

*	Set working directory
cd "C:\Users\Jorge Velilla\Desktop\UNIVERSIDAD\OTRO\2023\ClaseMaster_2022-23\Master_StataLatex\Stata"


/*******************************************************************************
	Request data and make some adjustments:
		- Keep a given year or a given country
		- Identify outliers 
		- Delete observations (outliers)
		- Delete variables
		- Redefine variables
		- Generate variables
		- Label variables
		- Order variables
		- Rename variables
*******************************************************************************/

*	Request data
use "Data/Sample.dta", clear

*	Keep Portugal, Italy, Greece, Spain
keep if country=="Portugal" | country=="Italy" | country=="Greece" | country=="Spain"

*	Keep data from 2010 (try both commands)
*drop if year<2010
keep if year>=2010

*	Identify outliers in multivariate data
*help bacon
bacon age workhours wage nlabinc w_age w_workhours w_wage w_nlabinc numfu numch hh_nlabinc, p(0.05) gen(todrop)

*	Delete outlier observations 
drop if todrop==1

*	Delete 'todrop' variable
drop todrop

*	Redefine occupation
replace occupation_ISCO08 	= occupation_ISCO08/10
replace occupation_ISCO08 	= floor(occupation_ISCO08)
replace w_occupation_ISCO08 = w_occupation_ISCO08/10
replace w_occupation_ISCO08 = floor(w_occupation_ISCO08)

*	Generate household earnings
generate hh_inc = 52*workhours*wage + 52*w_workhours*w_wage

*	Label household earnings
label variable hh_inc "Household labor income"

*	Generate and label log wages
generate lwage 				= log(wage)
generate lw_wage 			= log(w_wage)
generate cross_lwage		= lwage*lw_wage
label variable lwage		"H: log wage"
label variable lw_wage		"W: log wage"
label variable cross_lwage	"Cross log wages"

*	Order variables
order id-wage lwage 
order id-w_wage lw_wage
order id-numch hh_inc cross_lwage

*	Rename variables for simplicity
rename hh_inc 	  faminc
rename hh_nlabinc famnlinc

*	Summary statistics of all variables
summarize 

*	Occupation missing for some individuals! Keep non-missing occupations
drop if occupation_ISCO08==. | w_occupation_ISCO08==.
*keep if occupation_ISCO08!=. & w_occupation_ISCO08!=.

*	Generate country, year, occupation dummies
tabulate country, gen(cc_)
tabulate year, gen(yr_)
tabulate occupation_ISCO08, gen(oc_)
tabulate w_occupation_ISCO08, gen(w_oc_)


/*******************************************************************************
	Compute summary statistics and baseline tests
		- Compute sum stats table
		- Compute additional sum stats (frequencies, tables...)
		- Compute t-test p-values for differences between groups
*******************************************************************************/

*	Summary statistics of main variables, by country
*	- Portugal:
summarize 	age edu1 edu2 edu3 workhours wage nlabinc w_age w_edu1 w_edu2 	///
			w_edu3 w_workhours w_wage w_nlabinc married numfu numch faminc 	///
			famnlinc w_share_nlab share_wparl edu_gap w_managerial sexratio	///
			if country=="Portugal"
*	- Italy:
summarize 	age edu1 edu2 edu3 workhours wage nlabinc w_age w_edu1 w_edu2 	///
			w_edu3 w_workhours w_wage w_nlabinc married numfu numch faminc 	///
			famnlinc w_share_nlab share_wparl edu_gap w_managerial sexratio	///
			if country=="Italy"
*	- Greece:
summarize 	age edu1 edu2 edu3 workhours wage nlabinc w_age w_edu1 w_edu2 	///
			w_edu3 w_workhours w_wage w_nlabinc married numfu numch faminc 	///
			famnlinc w_share_nlab share_wparl edu_gap w_managerial sexratio	///
			if country=="Greece"
*	- Spain:
summarize 	age edu1 edu2 edu3 workhours wage nlabinc w_age w_edu1 w_edu2 	///
			w_edu3 w_workhours w_wage w_nlabinc married numfu numch faminc 	///
			famnlinc w_share_nlab share_wparl edu_gap w_managerial sexratio	///
			if country=="Spain"
*	- full sample:
summarize 	age edu1 edu2 edu3 workhours wage nlabinc w_age w_edu1 w_edu2 	///
			w_edu3 w_workhours w_wage w_nlabinc married numfu numch faminc 	///
			famnlinc w_share_nlab share_wparl edu_gap w_managerial sexratio	

*	Tabulate observations by year
tabulate year

*	Tabulate observations by country
tabulate country

*	Tabulate observations by year X country
tabulate year country

*	Test for gender inequality in age, Univ education, wages, labor supply
ttest age		== w_age
ttest edu3		== w_edu3
ttest wage		== w_wage
ttest workhours	== w_workhours


/*******************************************************************************
	Compute some figures
*******************************************************************************/

*	Density of wages
kdensity wage, addplot((kdensity w_wage))
			
*	Density of wages but more paper-style
kdensity	wage if wage<50, lcolor(black) lpattern(solid) 					///
			addplot((kdensity w_wage if w_wage<50, 							///
					lcolor(black) lpattern(dash))) 							///
			ytitle(Density) xtitle(Wage) 									///
			ylabel(, angle(zero))											///
			legend(order(1 "Male wage" 2 "Female wage") ring(0) pos(2)) 	///
			scheme(s1mono) 													///
			title(,size(zero))			

*	Density of work hours
kdensity	workhours if workhours<80, lcolor(black) lpattern(solid)		///
			addplot((kdensity w_workhours if w_workhours<80,				///
					lcolor(black) lpattern(dash))) 							///
			ytitle(Density) xtitle(Weekly work hours) 						///
			ylabel(, angle(zero))											///
			legend(order(1 "Male hours" 2 "Female hours") ring(0) pos(10)) 	///
			scheme(s1mono) 													///
			title(,size(zero))	
			
*	Wages by country
graph bar 	(mean) wage (mean) w_wage, over(country) 						///
			bar(1, fcolor(gs12) lcolor(black))								///
			bar(2, fcolor(gs8) lcolor(black))								///
			ytitle(Wage) 													///
			ylabel(, angle(zero))											///
			legend(order(1 "Male wage" 2 "Femnale wage")) 					///
			scheme(s1mono)


/*******************************************************************************
	OLS estimates
		- Compute estimates
		- Export estimates
*******************************************************************************/

*	Install command 'outreg2'
*ssc install outreg2

*	No distribution factors
*	- i) males
regress workhours	lwage lw_wage cross_lwage famnlinc 						///
					age edu2 edu3 oc_* 										///
					married numfu numch yr_2-yr_10 cc_1 cc_2 cc_3
outreg2 using "Output/Baseline.xls", bdec(3) ctitle(Males) drop (oc_* yr_* cc_* o.*) se replace
*	- ii) females
regress w_workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_age w_edu2 w_edu3 w_oc_* 								///
					married numfu numch yr_2-yr_10 cc_1 cc_2 cc_3					
outreg2 using "Output/Baseline.xls", bdec(3) ctitle(Females) drop (w_oc_* yr_* cc_* o.*) se append

*	Introducing distribution factors
*	- i) males
regress workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					age edu2 edu3 oc_* 										///
					married numfu numch yr_2-yr_10 cc_1 cc_2 cc_3
outreg2 using "Output/DFactors.xls", bdec(3) ctitle(Males) drop (oc_* yr_* cc_* o.*) se replace
*	- ii) females
regress w_workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					w_age w_edu2 w_edu3 w_oc_* 								///
					married numfu numch yr_2-yr_10 cc_1 cc_2 cc_3					
outreg2 using "Output/DFactors.xls", bdec(3) ctitle(Females) drop (w_oc_* yr_* cc_* o.*) se append

*	Estimates by country
*	- Portugal
regress workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					age edu2 edu3 oc_* 										///
					married numfu numch yr_2-yr_10 if country=="Portugal"
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Portugal - Males) drop (oc_* yr_* o.*) se replace
regress w_workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					w_age w_edu2 w_edu3 w_oc_* 								///
					married numfu numch yr_2-yr_10 if country=="Portugal"					
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Portugal - Females) drop (w_oc_* yr_* o.*) se append
*	- Italy
regress workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					age edu2 edu3 oc_* 										///
					married numfu numch yr_2-yr_10 if country=="Italy"
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Italy - Males) drop (oc_* yr_* o.*) se append
regress w_workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					w_age w_edu2 w_edu3 w_oc_* 								///
					married numfu numch yr_2-yr_10 if country=="Italy"					
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Italy - Females) drop (w_oc_* yr_* o.*) se append
*	- Greece
regress workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					age edu2 edu3 oc_* 										///
					married numfu numch yr_2-yr_10 if country=="Greece"
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Greece - Males) drop (oc_* yr_* o.*) se append
regress w_workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					w_age w_edu2 w_edu3 w_oc_* 								///
					married numfu numch yr_2-yr_10 if country=="Greece"					
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Greece - Females) drop (w_oc_* yr_* o.*) se append
*	- Spain
regress workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					age edu2 edu3 oc_* 										///
					married numfu numch yr_2-yr_10 if country=="Spain"
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Spain - Males) drop (oc_* yr_* o.*) se append
regress w_workhours	lwage lw_wage cross_lwage famnlinc 						///
					w_share_nlab sexratio edu_gap share_wparl w_managerial	///
					w_age w_edu2 w_edu3 w_oc_* 								///
					married numfu numch yr_2-yr_10 if country=="Spain"					
outreg2 using "Output/ByCountry.xls", bdec(3) ctitle(Spain - Females) drop (w_oc_* yr_* o.*) se append


/*******************************************************************************
	Define list of variables to simplify code
*******************************************************************************/

*	Main regressors
global depvars	lwage lw_wage cross_lwage famnlinc

*	Distribution factors
global dfvars	w_share_nlab sexratio edu_gap share_wparl w_managerial

*	Male dependent variables
global Hvars	age edu2 edu3 oc_* married numfu numch

*	Female dependent variables
global Wvars	w_age w_edu2 w_edu3 w_oc_* married numfu numch

*	Year fixed effecs
global yrfe		yr_2-yr_10


/*******************************************************************************
	SURE estimates
		- Loop over countries of interest to simplify code
		- Compute estimates
		- Export estimates
*******************************************************************************/

*	Define local list of countries; start loop
local ctrylist "Greece Italy Portugal Spain"
foreach cc of local ctrylist {
	sureg	(workhours		lwage lw_wage cross_lwage famnlinc 				///
							w_share_nlab sexratio edu_gap share_wparl 		///
							w_managerial 									///
							age edu2 edu3 oc_* married numfu numch			///
							yr_2-yr_10 )									///
			(w_workhours	lwage lw_wage cross_lwage famnlinc 				///
							w_share_nlab sexratio edu_gap share_wparl 		///
							w_managerial									///
							w_age w_edu2 w_edu3 w_oc_* married numfu numch	///
							yr_2-yr_10 )									///
			if country=="`cc'"
	outreg2 using 	"Output/SURE.xls", bdec(3) ctitle(`cc') 				///
					drop(oc_* w_oc_* yr_* o.*) se append	
}

*** End of dofile