Created
April 23, 2020 12:24
-
-
Save acarril/588ba045c2bc9c56053d4f9b1c79d1a5 to your computer and use it in GitHub Desktop.
ECO524 - PS3 - Million Dollar Plants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Working directory | |
if "`c(username)'"=="alvaro" { | |
cd "~/Dropbox/Princeton/2020-Spring/524-PF/PS3/" | |
} | |
else di as error "DUMMY U HAVE TO CHANGE THE PATH" | |
// Color scheme (from SSC) | |
set scheme plotplain | |
// Political data | |
import delimited countypres_2000-2016.csv, clear numericcols(5 9) | |
drop if mi(fips) | |
drop if year == 2000 // Not in SZ data | |
gen share = candidatevotes / totalvotes | |
keep year fips party share | |
replace party = "_" + party + "s" | |
reshape wide share, i(year fips) j(party) string | |
expand 4 | |
sort fips year | |
bys fips: gen deal_year = 2004+_n-1 | |
tempfile partiez | |
save `partiez' | |
// Load data and merge it with subsidy size and jobs | |
use "MDP_data_do_not_distrib/million_dollar_plants/SZdeals.dta", clear | |
merge 1:1 deal_id runnerup_id using "SZsubsidydealsize/dealid_subsidy_jobs.dta", /// | |
nogen keep(master match) | |
gen subsidy_perjob = subsidy/jobs | |
// Reshape data for estimation | |
bys deal_id: gen competitors = _N | |
keep if (competitors > 1 & runnerup_id <=1) | |
drop competitors *naics2* naics4 | |
encode stateabbrev, gen(state_num) | |
reshape long pop ln_hpi emp_naics1_ qp1_naics1_ ap_naics1_ emp_naics3_ qp1_naics3_ ap_naics3_, i(deal_id winner deal_year state_num) j(year) | |
renvars _all, postsub("_" "") // remove trailing underscores (from SSC) | |
merge m:1 fips deal_year using `partiez', nogen keep(master match) | |
// Generate time-event variable, truncating the tails (Q: should we drop these instead?) | |
// NOTE: the pre-treatment period is 8. | |
cap drop Period | |
gen Period = year - deal_year + 9 | |
replace Period = 1 if Period < 1 | |
replace Period = 17 if Period > 17 | |
forvalues i = -8 / 8 { | |
label define period `=`i'+9' "`i'", modify | |
} | |
label values Period period | |
// Rescale variables | |
foreach v of varlist pop emp_naics? { | |
replace `v' = `v'/1e3 | |
} | |
foreach v of varlist qp1_naics? ap_naics? { | |
replace `v' = `v'/1e6 | |
} | |
// Variable labels | |
lab var subsidy "Subsidy" | |
lab var jobs "Jobs" | |
lab var subsidy_perjob "Subsidy per job" | |
lab var share_democrats "Share of Democrat votes" | |
lab var share_republicans "Share of Republican votes" | |
lab var ln_hpi "log House Price Index" | |
lab var pop "Population (thousands)" | |
lab var emp_naics1 "NAICS1 Employment (thousands)" | |
lab var qp1_naics1 "NAICS1 Q1 Payroll (bn USD)" | |
lab var ap_naics1 "NAICS1 Annual Payroll (bn USD)" | |
lab var emp_naics3 "NAICS3 Employment (thousands)" | |
lab var qp1_naics3 "NAICS3 Q1 Payroll (bn USD)" | |
lab var ap_naics3 "NAICS3 Annual Payroll (bn USD)" | |
// Value labels | |
label define winner 0 "Runner-up" 1 "Winner" | |
label values winner winner | |
// Balance table | |
// estpost tabstat pop emp_naics1_ qp1_naics1_ ap_naics1_ if Period==8, by(winner) /// | |
// statistics(mean sd) columns(statistics) listwise nototal | |
// esttab using Writeup/Tabs/SZ_balance_pretreat.tex, replace booktabs /// | |
// main(mean) aux(sd) nostar unstack nonote nomtitle nonumber label noobs | |
eststo clear | |
bys winner: eststo: estpost summarize pop share_dem share_rep ln_hpi emp_naics? qp1_naics? ap_naics? if Period==8 | |
esttab using Writeup/Tabs/SZ_balance_pretreat.tex, replace booktabs /// | |
cells("mean(fmt(a3) label(Mean)) sd(fmt(a1) label(SD)) count(label(\$N\$))") label nodepvar noobs /// | |
mtitle("Runner-up" "Winner") nonumbers | |
// Relationship between subsidy and political & economic characteristics | |
eststo clear | |
foreach depvar of varlist subsidy subsidy_perjob { | |
eststo: reg `depvar' pop share_dem share_rep ln_hpi *_naics1* if Period==8 & winner, r | |
} | |
esttab using Writeup/Tabs/reg_subsidy_chars.tex, replace booktabs /// | |
label se r2 | |
// Construct interacted variables | |
// Note: this could be made more efficiently using Stata's # notation, but it | |
// makes it way to hard to manipulate results ex post. | |
tab Period, gen(winXperiod) | |
foreach v of varlist winXperiod* { | |
replace `v' = `v' * winner | |
} | |
unab interactedvars : winXperiod* | |
local baseline "winXperiod7" | |
local interactedvars : list interactedvars - baseline | |
local interactedvars `interactedvars' `baseline' | |
di "`interactedvars'" | |
areg ln_hpi winner i.Period `interactedvars' year, abs(deal_id) cluster(deal_id) | |
// Carry out regressions, saving point estimates and SEs in separate datasets | |
unab depvars : ln_hpi *_naics1 | |
foreach depvar in `depvars' { | |
statsby _b _se , saving(Out/TE_`depvar', replace) noisily: /// | |
areg `depvar' winner i.Period `interactedvars' i.year pop, abs(deal_id) cluster(deal_id) | |
} | |
statsby _b _se , saving(Out/TE_pop, replace) noisily: /// | |
areg pop winner i.Period `interactedvars' i.year, abs(deal_id) cluster(deal_id) | |
local depvars `depvars' pop | |
// Save main estimation dataset | |
save Out/estimation_data, replace | |
// Clean estimates, keeping only the relevant parameters and computing CIs | |
foreach depvar in `depvars' { | |
// Copy depvar label from main estimation dataset | |
use Out/estimation_data, clear | |
local lab : variable label `depvar' | |
// Load estimates | |
use Out/TE_`depvar', clear | |
// Add baseline for plotting | |
gen _b_`baseline' = 0 | |
gen _se_`baseline' = 0 | |
keep *winXperiod* | |
// Reshape | |
gen i = _n | |
reshape long _b_winXperiod _se_winXperiod, i(i) j(Period) | |
drop i | |
rename (_b_winXperiod _se_winXperiod) (b se) | |
// Rescale Period | |
replace Period = Period - 8 | |
// Generate CIs | |
gen ci_lo = b - invnormal(.975) * se | |
gen ci_hi = b + invnormal(.975) * se | |
// Save | |
label variable b "`lab'" | |
save Out/TE_`depvar', replace | |
} | |
// Plot ES plots | |
foreach depvar in `depvars' { | |
use Out/TE_`depvar', clear | |
keep if inrange(Period, -5, 5) | |
twoway (rcap ci_hi ci_lo Period, sort) (connected b Period, sort msymbol(circle)), /// | |
ytitle("`: var label b'", size(small)) yline(0) xlabel(#11) legend(off) scale(1.75) | |
graph export Writeup/Figs/ES_`depvar'.pdf, replace | |
} | |
******************************************************************************** | |
// reghdfe ln_hpi winner##Period i.year, abs(deal_id) vce(cluster deal_id) | |
/* | |
// load data same as before | |
sysuse auto, clear | |
egen t = fill(1,2,3,4,1,2,3,4) | |
// get coefficients and standard errors of regressions over foreign | |
statsby _b _se , clear by(foreign): regress price ib2.t trunk weight | |
// there are some extra variables we don't need/want | |
drop *_trunk *_weight *_cons | |
// generate confidence intervals and rename coefficient variables | |
forvalues i = 1/4 { | |
local j = `i'+7 | |
gen ci_low`i' = _stat_`i' - 1.96*_stat_`j' | |
gen ci_high`i' = _stat_`i' + 1.96*_stat_`j' | |
rename _stat_`i' coef`i' | |
} | |
// no longer in need of standard error variables | |
drop _stat_8 _stat_9 _stat_10 _stat_11 | |
// now, we want our data in long format so we can do a twoway graph | |
reshape long coef ci_low ci_high, i(foreign) j(t) | |
// we can label the t values so that they start below 1 | |
lab def timeseries 1 "-1" 2 "0" 3 "1" 4 "2" | |
lab values t timeseries | |
// now graph, note each factor has two pieces, a scatter (with connecting lines) | |
// and an rcap for the confidence intervals | |
twoway (sc coef t if foreign == 1, mcolor(navy) lcolor(navy) connect(direct)) /// | |
(rcap ci_low ci_high t if foreign == 1, lcolor(navy)) /// | |
(sc coef t if foreign == 0, mcolor(maroon) lcolor(maroon) connect(direct)) /// | |
(rcap ci_low ci_high t if foreign == 0, lcolor(maroon)), /// | |
legend(lab(1 "Foreign") lab(2 "Foreign CI") lab(3 "Domestic") lab(4 "Domestic CI")) /// | |
xlab(,val) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment