IV-2SLS output

Instrumental variables: \LaTeX output code

(xt)ivreg2 is an excellent Stata package for estimating two stage least squares models. Generating output suitable for typesetting in Latex can be tedious, particularly for first stage results and supplementary statistics, like the Angrist-Pischke multivariate F statistic.

Here is a helpful snippet of code. This solution is scalable for a number of related, specialized statistics that are not immediately available in standard .ado scripts. You can download a working version of the .do (as txt) file here.

** First stage + AP stats

tsset unit year, yearly

eststo: quietly xtivreg2 DV (endogenousreg1  endogenousreg2  = IV1 IV2 IV3),  fe ffirst savefirst savefprefix(first1)

*extract matrix from e(first)
     mat first1=e(first)
*build scalars
     scalar apf_1a=first1[7,1]
     scalar apfp_1a=first1[10,1]
     scalar apf_1b=first1[7,2]
     scalar apfp_1b=first1[10,2]
*restore first stage results of first endogenous regressor
     est restore first1endogenousreg1
**call scalars
     estadd scalar apf_a=scalar(apf_1a)
     estadd scalar apfp_a=scalar(apfp_1a)
***add scalars to first stage results matrix
          estimates store vector1a
****restore first stage results of second endogenous regressor
     est restore first1endogenousreg2
     estadd scalar apf_b=scalar(apf_1b)
     estadd scalar apfp_b=scalar(apfp_1b)
          estimates store vector1b

eststo: quietly xtivreg2 DV (endogenousreg1  endogenousreg2  = IV1 IV2 IV3 IV4), fe ffirst savefirst savefprefix(first2)
     mat first2=e(first)
     scalar apf_2a=first2[7,1]
     scalar apfp_2a=first2[10,1]
     scalar apf_2b=first2[7,2]
     scalar apfp_2b=first2[10,2]
     
     est restore first2endogenousreg1
     estadd scalar apf_a=scalar(apf_2a)
     estadd scalar apfp_a=scalar(apfp_2a)
          estimates store vector2a
     est restore first2endogenousreg2
     estadd scalar apf_b=scalar(apf_2b)
     estadd scalar apfp_b=scalar(apfp_2b)
          estimates store vector2b

eststo: quietly xtivreg2 DV (endogenousreg1  endogenousreg2  = IV1 IV2 IV3 IV4 IV5), fe ffirst savefirst savefprefix(first3)
     mat first3=e(first)
     scalar apf_3a=first3[7,1]
     scalar apfp_3a=first3[10,1]
     scalar apf_3b=first3[7,2]
     scalar apfp_3b=first3[10,2]

     est restore first3endogenousreg1
     estadd scalar apf_a=scalar(apf_3a)
     estadd scalar apfp_a=scalar(apfp_3a)
          estimates store vector3a
     est restore first3endogenousreg2
     estadd scalar apf_b=scalar(apf_3b)
     estadd scalar apfp_b=scalar(apfp_3b)
          estimates store vector3b

eststo: quietly xtivreg2 DV (endogenousreg1  endogenousreg2  = IV1 IV2 IV3 IV4 IV5 IV6), fe ffirst savefirst savefprefix(first4)
     mat first4=e(first)
     scalar apf_4a=first4[7,1]
     scalar apfp_4a=first4[10,1]
     scalar apf_4b=first4[7,2]
     scalar apfp_4b=first4[10,2]

     est restore first4endogenousreg1
     estadd scalar apf_a=scalar(apf_4a)
     estadd scalar apfp_a=scalar(apfp_4a)
          estimates store vector4a
     est restore first4endogenousreg2
     estadd scalar apf_b=scalar(apf_4b)
     estadd scalar apfp_b=scalar(apfp_4b)
          estimates store vector4b

esttab vector1a vector2a vector3a vector4a using "/Users/austinlw/Dropbox (Personal)/.../main_first_stage_endog1.tex", style(tex) keep(IV1 IV2 IV3 IV4 IV5 IV6) star("\gamma" 0.10 * 0.05 ** 0.01 *** 0.001) stats(apf_a  apfp_a labels("AP F statistic", "AP p-value"))
esttab vector1b vector2b vector3b vector4b using "/Users/austinlw/Dropbox (Personal)/.../main_first_stage_endog2.tex", style(tex) keep(IV1 IV2 IV3 IV4 IV5 IV6) star("\gamma" 0.10 * 0.05 ** 0.01 *** 0.001) stats(apf_b apfp_b labels("AP F statistic", "AP p-value"))