#delimit;
set more 1;
set memory 750m;
set matsize 10000;
log using /volumes/ddisk/bgh/programs/subst_mf_smsa.log, replace;





*FIRST I AM GOING TO GENERATE SAMPLING VARIANCE AT SURVEY-STATE-EDCODE LEVEL;






*FIRST, REGRESSION USING SAMPLE OF ALL WORKERS;


use /volumes/ddisk/bgh/programs/wage_mf_smsa.dta, clear;

keep if imm==0;

generate lworked=log(immig+native);
generate lpimm=log(pimmig);

generate trend=survey-1960;
generate t92=max(survey-1992, 0);

generate trend1=(edcode==1)*trend;
generate trend2=(edcode==2)*trend;
generate trend3=(edcode==3)*trend;
generate trend4=(edcode==4)*trend;

generate rs=smsa*10 + edcode;

keep if edcode<=2;

/*
xi: reg lweekly lwork i.survey*i.smsa i.survey*i.edcode [aw=sizew], robust;
xi: ivreg lweekly i.survey*i.smsa i.survey*i.edcode (lwork = lpimm) [aw=sizew], robust;

xi: reg lweekly lwork i.survey*i.smsa i.rs*trend [aw=sizew], robust;
xi: ivreg lweekly i.survey*i.smsa i.rs*trend (lwork = lpimm) [aw=sizew], robust;

xi: reg lweekly lwork i.survey*i.smsa i.rs*trend i.rs*t92 [aw=sizew], robust;
xi: ivreg lweekly i.survey*i.smsa i.rs*trend i.rs*t92 (lwork = lpimm) [aw=sizew], robust;

*/

keep smsa survey edcode lweekly slweekly sizew native immig pnative pimmig;

reshape wide lweekly slweekly sizew native immig pnative pimmig, i(smsa survey) j(edcode);

tab survey, gen(year);
tab smsa, gen(reg);

*DEFINE QUANTITES BASED ON COUNTS OF WORKERS USING HOURS WORKED;
gen worked2=native2+immig2;
gen worked1=native1+immig1;
gen dquant21=log(worked2/worked1);
gen dwage21=lweekly2-lweekly1;

gen wsample=(sizew1*sizew2)/(sizew1+sizew2);
gen trend=survey-1960;
gen trend2=trend*trend;
gen trend3=trend*trend*trend;
gen t92=max(survey-1992, 0);
gen t80=max(survey-1980, 0);

generate dimm21=log(immig2/immig1);

/*

*THIS REGRESSION SHOULD REPLICATE THE REGRESSION ABOVE;
xi: reg dwage21 dquant21 i.smsa*trend [aw=wsample];
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t92 [aw=wsample];
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t80 [aw=wsample];
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*trend2 [aw=wsample];


*HERE ARE SOME BASIC INTERESTING REGRESSIONS;
*THAT BASICALLY ASSUME TREND IS THE SAME NATIONWIDE;

xi: reg dwage21 dquant21 year* i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend t92 i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 trend3 i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend t80 i.smsa [aw=wsample];

*/

generate varweightlog=(slweekly1^2/sizew1) + (slweekly1^2/sizew2);
replace varweightlog=1/varweightlog;


*FIRST TWO OLS REGRESSIONS SHOULD REPLICATE THE REGRESSIONS ABOVE;

xi: reg dwage21 dquant21 i.smsa*trend [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t92 [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t80 [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*trend2 [aw=varweightlog], robust cluster(smsa);



*HERE ARE SOME BASIC INTERESTING REGRESSIONS;
*THAT BASICALLY ASSUME TREND IS THE SAME NATIONWIDE;

xi: reg dwage21 dquant21 year* i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend t92 i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend trend2 i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend trend2 trend3 i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend t80 i.smsa [aw=varweightlog], robust cluster(smsa);








*FIRST, REGRESSION USING SAMPLE OF FT WORKERS;






*FIRST, REGRESSION USING SAMPLE OF FT WORKERS;


use /volumes/ddisk/bgh/programs/wage_mf_ft_smsa.dta, clear;

keep if imm==0;

generate lworked=log(immig+native);
generate lpimm=log(pimmig);

generate trend=survey-1960;
generate t92=max(survey-1992, 0);

generate trend1=(edcode==1)*trend;
generate trend2=(edcode==2)*trend;
generate trend3=(edcode==3)*trend;
generate trend4=(edcode==4)*trend;

generate rs=smsa*10 + edcode;

keep if edcode<=2;

/*
xi: reg lweekly lwork i.survey*i.smsa i.survey*i.edcode [aw=sizew], robust;
xi: ivreg lweekly i.survey*i.smsa i.survey*i.edcode (lwork = lpimm) [aw=sizew], robust;

xi: reg lweekly lwork i.survey*i.smsa i.rs*trend [aw=sizew], robust;
xi: ivreg lweekly i.survey*i.smsa i.rs*trend (lwork = lpimm) [aw=sizew], robust;

xi: reg lweekly lwork i.survey*i.smsa i.rs*trend i.rs*t92 [aw=sizew], robust;
xi: ivreg lweekly i.survey*i.smsa i.rs*trend i.rs*t92 (lwork = lpimm) [aw=sizew], robust;

*/

keep smsa survey edcode lweekly slweekly sizew native immig pnative pimmig;

reshape wide lweekly slweekly sizew native immig pnative pimmig, i(smsa survey) j(edcode);

tab survey, gen(year);
tab smsa, gen(reg);

*DEFINE QUANTITES BASED ON COUNTS OF WORKERS USING HOURS WORKED;
gen worked2=native2+immig2;
gen worked1=native1+immig1;
gen dquant21=log(worked2/worked1);
gen dwage21=lweekly2-lweekly1;

gen wsample=(sizew1*sizew2)/(sizew1+sizew2);
gen trend=survey-1960;
gen trend2=trend*trend;
gen trend3=trend*trend*trend;
gen t92=max(survey-1992, 0);
gen t80=max(survey-1980, 0);

generate dimm21=log(immig2/immig1);

/*

*THIS REGRESSION SHOULD REPLICATE THE REGRESSION ABOVE;
xi: reg dwage21 dquant21 i.smsa*trend [aw=wsample];
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t92 [aw=wsample];
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t80 [aw=wsample];
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*trend2 [aw=wsample];


*HERE ARE SOME BASIC INTERESTING REGRESSIONS;
*THAT BASICALLY ASSUME TREND IS THE SAME NATIONWIDE;

xi: reg dwage21 dquant21 year* i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend t92 i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 trend3 i.smsa [aw=wsample];
xi: reg dwage21 dquant21 trend t80 i.smsa [aw=wsample];

*/

generate varweightlog=(slweekly1^2/sizew1) + (slweekly1^2/sizew2);
replace varweightlog=1/varweightlog;


*FIRST TWO OLS REGRESSIONS SHOULD REPLICATE THE REGRESSIONS ABOVE;

xi: reg dwage21 dquant21 i.smsa*trend [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t92 [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*t80 [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 i.smsa*trend i.smsa*trend2 [aw=varweightlog], robust cluster(smsa);



*HERE ARE SOME BASIC INTERESTING REGRESSIONS;
*THAT BASICALLY ASSUME TREND IS THE SAME NATIONWIDE;

xi: reg dwage21 dquant21 year* i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend t92 i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend trend2 i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend trend2 trend3 i.smsa [aw=varweightlog], robust cluster(smsa);
xi: reg dwage21 dquant21 trend t80 i.smsa [aw=varweightlog], robust cluster(smsa);



log close;
