#delimit;
set more 1;
set memory 750m;
set matsize 400;

*THIS CREATES THE AVERAGE WAGE IN CELL FOR SAMPLE OF WAGE & SALARY WORKERS;
*THIS USES THE PREFERRED SPECIFICATION;

log using /volumes/ddisk/bgh/programs/wage_mf_ft.log, replace text;

*THIS FILE ESTIMATES THE INVERSE ELASTICITY OF SUBSTITUTION FOR NATIVES AND IMMIGRANTS USING A PREFERRED SPECIFICATION;
*18-64, POSITIVE HOURS WORKED, CORRECT DEFINITION OF IMMIGRANTS, EDUCREC FOR EDUCATION, ADJUST FOR TOP CODING;
*ESTIMATE EITHER MEN ONLY OR MEN AND WOMEN AND WITH INCWAGE;


use /volumes/ddisk/bgh/data/ipums60.dta, clear;

generate byte imm=(bpld>=15000);   
replace imm=0 if bpld==90011 | bpld==90021;
drop if wkswork2==0;

generate hours=7.5*(hrswork2==1) + 22*(hrswork2==2) + 32*(hrswork2==3) + 37*(hrswork2==4) 
	+ 40*(hrswork2==5) + 44.5*(hrswork2==6) 
	+ 54*(hrswork2==7) + 70*(hrswork2==8) ;
generate weeks=6.5*(wkswork2==1) + 20*(wkswork2==2) + 33*(wkswork2==3) + 43.5*(wkswork2==4) 
	+ 48.5*(wkswork2==5) + 51*(wkswork2==6) ;

*generate pdeflator=4.75;
*REPLACE WITH OP DEFLATOR;
generate pdeflator=5.725;
drop if incwage==0 | incwage==999999;
replace incwage=25000*1.5 if incwage==25000;
replace incbusfm=25000*1.5 if incbusfm==25000;
gen incearn=incwage+incbusfm;

*SELECT FULL-TIME WORKERS;
drop if weeks<40;
drop if hours<35;

* DEFINE SAMPLE RESTRICTIONS, COMMON VARIABLES;
do /volumes/ddisk/bgh/programs/restriction_wage_mf.do;

generate survey=1960;
sort survey edcode expcode;
save /volumes/ddisk/data/rep60.dta, replace;



use /volumes/ddisk/bgh/data/ipums70.dta, clear;

generate school=higraded - (int(higraded/10))*10;
generate byte imm=(citizen==2 | citizen==3);
drop if wkswork2==0;

generate hours=7.5*(hrswork2==1) + 22*(hrswork2==2) + 32*(hrswork2==3) 	+ 37*(hrswork2==4) 
	+ 40*(hrswork2==5) + 44.5*(hrswork2==6) 
	+ 54*(hrswork2==7) + 70*(hrswork2==8) ;
generate weeks=6.5*(wkswork2==1) + 20*(wkswork2==2) + 33*(wkswork2==3) + 43.5*(wkswork2==4) 
	+ 48.5*(wkswork2==5) + 51*(wkswork2==6) ;

*generate pdeflator=3.63;
*REPLACE WITH OP DEFLATOR;
generate pdeflator=4.54;
drop if incwage==0 | incwage==999999;
replace incwage=50000*1.5 if incwage==50000;
replace incbus=50000*1.5 if incbus==50000;
replace incfarm=50000*1.5 if incfarm==50000;
gen incearn=incwage+incbus+incfarm;

*SELECT FULL-TIME WORKERS;
drop if weeks<40;
drop if hours<35;

* DEFINE SAMPLE RESTRICTIONS, COMMON VARIABLES;
do /volumes/ddisk/bgh/programs/restriction_wage_mf.do;

generate survey=1970;
sort survey edcode expcode;
save /volumes/ddisk/data/rep70.dta, replace;



use /volumes/ddisk/bgh/data/ipums80.dta, clear;

generate byte imm=(citizen==2 | citizen==3);
drop if wkswork1==0;

*generate pdeflator=1.85;
*REPLACE WITH OP DEFLATOR;
generate pdeflator=2.314;
drop if incwage==0 | incwage==999999;
replace incwage=75000*1.5 if incwage==75000;
replace incbus=75000*1.5 if incbus==75000;
replace incfarm=75000*1.5 if incfarm==75000;
gen incearn=incwage+incbus+incfarm;

generate weeks=wkswork1;
generate hours=uhrswork;

*SELECT FULL-TIME WORKERS;
drop if weeks<40;
drop if hours<35;

* DEFINE SAMPLE RESTRICTIONS, COMMON VARIABLES;
do /volumes/ddisk/bgh/programs/restriction_wage_mf.do;

generate survey=1980;
sort survey edcode expcode;
save /volumes/ddisk/data/rep80.dta, replace;



use /volumes/ddisk/bgh/data/ipums90.dta, clear;

generate byte imm=(citizen==2 | citizen==3);
drop if wkswork1==0;

*generate pdeflator=1.22;
*REPLACE WITH OP DEFLATOR;
generate pdeflator=1.344;
drop if incwage==0 | incwage==999999;
generate weeks=wkswork1;
generate hours=uhrswork;

*SELECT FULL-TIME WORKERS;
drop if weeks<40;
drop if hours<35;

* DEFINE SAMPLE RESTRICTIONS, COMMON VARIABLES;
do /volumes/ddisk/bgh/programs/restriction_wage_mf.do;

generate survey=1990;
sort survey edcode expcode;
save /volumes/ddisk/data/rep90, replace;



use /volumes/ddisk/bgh/data/ipums00.dta, clear;

generate byte imm=(citizen==2 | citizen==3);
drop if wkswork1==0;

*THIS IS ALSO THE BASE YEAR WITH OP DEFLATOR;
generate pdeflator=1;
drop if incwage==0 | incwage==999999;
generate weeks=wkswork1;
generate hours=uhrswork;

*SELECT FULL-TIME WORKERS;
drop if weeks<40;
drop if hours<35;

* DEFINE SAMPLE RESTRICTIONS, COMMON VARIABLES;
do /volumes/ddisk/bgh/programs/restriction_wage_mf.do;

generate survey=2000;
sort survey edcode expcode;
save /volumes/ddisk/data/rep00.dta, replace;



use /volumes/ddisk/bgh/data/acs06.dta, clear;

generate survey=2000+year;
keep if survey==2006;

generate byte imm=(citizen==2 | citizen==3);
drop if wkswork1==0;

*generate pdeflator=0.85;
*REPLACE WITH OP DEFLATOR;
generate pdeflator=0.853;
drop if incwage==0 | incwage==999999;
generate weeks=wkswork1;
generate hours=uhrswork;

*SELECT FULL-TIME WORKERS;
drop if weeks<40;
drop if hours<35;

* DEFINE SAMPLE RESTRICTIONS, COMMON VARIABLES;
do /volumes/ddisk/bgh/programs/restriction_wage_mf.do;

gen survey=2006;
sort survey edcode expcode;
save /volumes/ddisk/data/rep06.dta, replace;



*POOL DATA ACROSS YEARS;
use /volumes/ddisk/data/rep60.dta, clear;
append using /volumes/ddisk/data/rep70.dta;
append using /volumes/ddisk/data/rep80.dta;
append using /volumes/ddisk/data/rep90.dta;
append using /volumes/ddisk/data/rep00.dta;
append using /volumes/ddisk/data/rep06.dta;

reshape wide sumweight lweekly weekly sizew slweekly sweekly, i(survey edcode expcode) j(imm);

for any sumweight lweekly weekly sizew slweekly sweekly : ren X0 Xn;
for any sumweight lweekly weekly sizew slweekly sweekly : ren X1 Xi;

*MERGE IN DATA ON COUNTS BASED ON HOURS WORKED;
*THE COUNTS INCLUDE THE SELF-EMPLOYED;
sort survey edcode expcode;
merge survey edcode expcode using /volumes/ddisk/bgh/programs/count_se.dta;
tab _merge; drop _merge;
sort survey edcode expcode;
merge survey edcode expcode using /volumes/ddisk/bgh/programs/pop.dta;
tab _merge; drop _merge;
save /volumes/ddisk/bgh/programs/wage_mf_ft.dta, replace;

gen code1=survey*10 + edcode;
gen code2=survey*100 + expcode;
gen code3=expcode*10 + edcode;
tab edcode, gen(educ);
tab survey, gen(year);
tab expcode, gen(xp);
tab code1, gen(inter);
tab code2, gen(intra);
tab code3, gen(extra);

*DEFINE QUANTITES BASED ON COUNTS OF WORKERS USING HOURS WORKED;
gen lmrelm=log(immigm/nativem);
gen lmrelf=log((immig-immigm)/(native-nativem));
gen lmrelmf=log(immig/native);
gen lmpm=log(pimmigm/pnativem);
gen lmpf=log(pimmig-pimmigm)-log(pnative-pnativem);
gen lmpmf=log(pimmig/pnative);

gen logrelweek1=lweeklyi-lweeklyn;
gen relweek1=log(weeklyi/weeklyn);

*OP USE A WEIGHT CALLED EMPLOYMENT, WHICH IS THE SUM OF PERWT ACROSS EVERYONE, MALE AND FEMALE;
*IN A PARTICULAR SKILL GROUP;
*THIS WEIGHT IS USED IN ALL REGRESSIONS, REGARDLESS OF WHETHER DEP VARIABLE;
*IS MALE WAGE OR FEMALE WAGE OR TOTAL WAGE;

gen employment=(nimmig+nnative);

reg relweek1 lmrelmf [aw=employment], cluster(code3);
reg relweek1 lmrelmf year* [aw=employment], cluster(code3);
reg relweek1 lmrelmf year* extra* [aw=employment], cluster(code3);
reg relweek1 lmrelmf inter* intra* extra* [aw=employment], cluster(code3);
reg logrelweek1 lmrelmf [aw=employment], cluster(code3);
reg logrelweek1 lmrelmf year* [aw=employment], cluster(code3);
reg logrelweek1 lmrelmf year* extra* [aw=employment], cluster(code3);
reg logrelweek1 lmrelmf inter* intra* extra* [aw=employment], cluster(code3);


gen wsample=(sizewn*sizewi)/(sizewn+sizewi);

reg relweek1 lmrelmf [aw=wsample], cluster(code3);
reg relweek1 lmrelmf year* [aw=wsample], cluster(code3);
reg relweek1 lmrelmf year* extra* [aw=wsample], cluster(code3);
reg relweek1 lmrelmf inter* intra* extra* [aw=wsample], cluster(code3);
reg logrelweek1 lmrelmf [aw=wsample], cluster(code3);
reg logrelweek1 lmrelmf year* [aw=wsample], cluster(code3);
reg logrelweek1 lmrelmf year* extra* [aw=wsample], cluster(code3);
reg logrelweek1 lmrelmf inter* intra* extra* [aw=wsample], cluster(code3);

generate varweightlog=(slweeklyn^2/sizewn) + (slweeklyi^2/sizewi);
replace varweightlog=1/varweightlog;
generate varweight=(sweeklyn^2/sizewn) + (sweeklyi^2/sizewi);
replace varweight=1/varweight;

reg relweek1 lmrelmf [aw=varweight], cluster(code3);
reg relweek1 lmrelmf year* [aw=varweight], cluster(code3);
reg relweek1 lmrelmf year* extra* [aw=varweight], cluster(code3);
reg relweek1 lmrelmf inter* intra* extra* [aw=varweight], cluster(code3);
reg logrelweek1 lmrelmf [aw=varweightlog], cluster(code3);
reg logrelweek1 lmrelmf year* [aw=varweightlog], cluster(code3);
reg logrelweek1 lmrelmf year* extra* [aw=varweightlog], cluster(code3);
reg logrelweek1 lmrelmf inter* intra* extra* [aw=varweightlog], cluster(code3);


*OP REGRESSIONS WITH CORRECT SAMPLE WEIGHT, AS DERIVED BY JEFF;

generate numer=(sizewi*weeklyi^2)*(sizewn*weeklyn^2);
generate denom= (sweeklyi^2)*sizewi*(weeklyi^2) + (sweeklyn^2)*sizewn*(sweeklyn^2);
generate opweight=numer/denom;

reg relweek1 lmrelmf [aw=opweight], cluster(code3);
reg relweek1 lmrelmf year* [aw=opweight], cluster(code3);
reg relweek1 lmrelmf year* extra* [aw=opweight], cluster(code3);
reg relweek1 lmrelmf inter* intra* extra* [aw=opweight], cluster(code3);


*IV WITH SAMPLE WEIGHTS;

ivreg relweek1 (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg relweek1 lmrelm year* (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg relweek1 lmrelm year* extra* (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg relweek1 lmrelm inter* intra* extra* (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg logrelweek1 (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg logrelweek1 lmrelm year* (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg logrelweek1 lmrelm year* extra* (lmrelmf=lmpmf) [aw=wsample], cluster(code3);
ivreg logrelweek1 lmrelm inter* intra* extra* (lmrelmf=lmpmf) [aw=wsample], cluster(code3);


log close;

