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





*FIRST DO ALL WORKERS;
*I'M USING LESSONS LEARNED FROM EXPERIMENT.DO TO JUST RUN REGRESSIONS IN DIFFERENCED FORMAT;


use /volumes/ddisk/bgh/programs/wage_mf_state.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=state*10 + edcode;

keep if edcode<=2;

xi: reg lweekly lwork i.edcode*i.survey i.survey*i.state [aw=sizew];
xi: reg lweekly lwork i.survey*i.state i.rs*trend [aw=sizew], robust;

/*
xi: reg lweekly lwork i.survey*i.state;

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

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

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



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

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

tab survey, gen(year);
tab state, 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);

/*

*FIRST TWO OLS REGRESSIONS SHOULD REPLICATE THE REGRESSIONS ABOVE;

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



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

xi: reg dwage21 dquant21 year* i.state [aw=wsample];
xi: reg dwage21 dquant21 trend i.state [aw=wsample];
xi: reg dwage21 dquant21 trend t92 i.state [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 i.state [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 trend3 i.state [aw=wsample];
xi: reg dwage21 dquant21 trend t80 i.state [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.state*trend [aw=varweightlog], robust cluster(state);
xi: reg dwage21 dquant21 i.state*trend i.state*t92 [aw=varweightlog], robust cluster(state);
xi: reg dwage21 dquant21 i.state*trend i.state*t80 [aw=varweightlog], robust cluster(state);
xi: reg dwage21 dquant21 i.state*trend i.state*trend2 [aw=varweightlog], robust cluster(state);



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

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








*NOW DO FULL-TIME WORKERS;







*FIRST DO ALL WORKERS;
*I'M USING LESSONS LEARNED FROM EXPERIMENT.DO TO JUST RUN REGRESSIONS IN DIFFERENCED FORMAT;


use /volumes/ddisk/bgh/programs/wage_mf_ft_state.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=state*10 + edcode;

keep if edcode<=2;

xi: reg lweekly lwork i.edcode*i.survey i.survey*i.state [aw=sizew];
xi: reg lweekly lwork i.survey*i.state i.rs*trend [aw=sizew], robust;

/*
xi: reg lweekly lwork i.survey*i.state;

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

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

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



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

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

tab survey, gen(year);
tab state, 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);

/*

*FIRST TWO OLS REGRESSIONS SHOULD REPLICATE THE REGRESSIONS ABOVE;

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



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

xi: reg dwage21 dquant21 year* i.state [aw=wsample];
xi: reg dwage21 dquant21 trend i.state [aw=wsample];
xi: reg dwage21 dquant21 trend t92 i.state [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 i.state [aw=wsample];
xi: reg dwage21 dquant21 trend trend2 trend3 i.state [aw=wsample];
xi: reg dwage21 dquant21 trend t80 i.state [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.state*trend [aw=varweightlog], robust cluster(state);
xi: reg dwage21 dquant21 i.state*trend i.state*t92 [aw=varweightlog], robust cluster(state);
xi: reg dwage21 dquant21 i.state*trend i.state*t80 [aw=varweightlog], robust cluster(state);
xi: reg dwage21 dquant21 i.state*trend i.state*trend2 [aw=varweightlog], robust cluster(state);



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

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



log close;
