estimates clear

*** 1) Incarceration & homicides
local depvars "i_ln_pris ln_IHMEviol firearms"
use `depvars' USA arms $notimputable $imputable year numISO if inrange(year,2004,2005) using paneldata.dta, clear
replace ln_IHMEviol=F.ln_IHMEviol if year==2004
replace firearms=ln(F.firearms) if year==2004
drop if year==2005 | mi(arms)
drop year numISO

foreach var of varlist $imputable {
	quietly misstable sum `var'
	if r(N_eq_dot)!=. {
		gen md_`var'=mi(`var')
		recode `var' (.=0)
		}
	}

* test if coefficients change when including control for arms
eststo firearms:		reg firearms USA $notimputable $imputable md*
eststo firearms_plus:	reg firearms USA arms $notimputable $imputable md*
eststo murder:			reg ln_IHME USA $notimputable $imputable md*
eststo murder_plus:		reg ln_IHME USA arms $notimputable $imputable md*
suest firearms firearms_plus murder murder_plus
test [firearms_mean=murder_mean]:			$imputable $notimputable
test [firearms_mean=firearms_plus_mean]:	$imputable $notimputable
test [murder_mean=murder_plus_mean]:		$imputable $notimputable
estimates drop firearms* murder*
	
* regressions for table
foreach depvar of local depvars {
	_eststo: reg `depvar' USA arms $notimputable $imputable md*, robust
	corr `depvar' arms
	estadd scalar rho=r(rho)
}

*** 2) ICVS
local depvars "victim9 C09A100 C12A100"
use `depvars' $imputable $notimputable I002A I003 numISO USA using MI_ICVS_dataset.dta, clear
sort numISO I002A
collapse (last) `depvars' $imputable $notimputable I002A I003, by(numISO USA)	// retain only most recent instead of all obs. for better match with arms data
preserve
use numISO arms year if year==2004 & !mi(arms) using paneldata.dta, clear
drop year
tempfile arms
save `arms', replace
restore
merge 1:1 numISO using `arms', keep(3) keepusing(arms) nogenerate		// this does NOT really merge on year (or sweep (I002A)). It's a hack -- arms data are available only for 2004.
foreach var of varlist $imputable { // creates missing dummies
	quietly misstable sum `var'
	if r(N_eq_dot)!=. {
		gen md_`var'=mi(`var')
		recode `var' (.=0)
		}
	}

foreach depvar of local depvars {
	_eststo: reg `depvar' USA arms $notimputable $imputable i.I002A i.I003 md*, robust
	corr `depvar' arms
	estadd scalar rho=r(rho)
}


*** 3) Output

esttab * using results/guns, replace csv label b(2) se(2) r2(2) scalars(rho) sfmt(2) star(* 0.10 ** 0.05 *** 0.01) ///
	nogaps nonumbers drop(md* _cons) order(USA arms $notimputable $imputable) ///
	title(Robustness: Guns) mtitles(Incarceration Homicides "Gun homicides" "Overall victimization" Robbery Assault) coeflabels($variabledescriptions) nonotes ///
	addnotes("Robust standard errors in parentheses. Dummies for missing values.")
