Exporting Results To LaTeX (Topic)

From TaDa Wiki
Jump to: navigation, search

Transferring results from a statistical program to LaTeX is one of the most likely places for errors to sneak into your papers, both because transcription errors are always probable. Moreover, paper writing is iterative, and having to re-copy results every time you change your code in response to feedback at a seminar or a referee can be excruciating.

With that in mind, a number of utilities have been developed to directly output the results of regressions or other statistical analyses directly into LaTeX. That way every time you run your statistical program and recompile your LaTeX document, the results will be updated, no transcription or work required.

Exporting from Statistical Software

Stata

There are a number of tools for exporting results in Stata directly into LaTeX. Most of them are for exporting regression results, which can be extremely easy, but there are also (admittedly slightly more complicated) ways to export arbitrary results.

Regressions

The two most popular add-ons are outreg2 and esttab. Both can be found and installed using the findit command. Tutorials and documentation can be found:

Non-Regression Tables

Exporting arbitrary tables is a little more challenging in Stata, but can be done. The trick is to store the results you want to export in a matrix, then export the matrix using outtable . Here's an example of creating a table (stored in a matrix called "balance") to show whether a few variables (age, gender, income) are statistically different between treatment and control groups (defined by treatmentVariable). outtable can be found here. Store in matrix, then export matrix.


* Create a 3x3 matrix and fill all the cells with values of "0"
matrix balance = J(3, 3,0)

local i=1

foreach x in ageVariable genderVariable income {
	ttest `x', by(treatmentVariable)

	matrix balance[`i',1]=r(mu_1)
	matrix balance[`i',2]=r(mu_2)
	matrix balance[`i',3]=r(p)
	local i=`i'+1
}

matrix rownames balance=Age Gender income
matrix colnames balance=Control Treat PValue
matrix list balance

outtable using balance , mat(balance) replace caption("Treatment Balance") 


R

There are two ways to get R results into LaTeX. The first, as with Stata, is to just export the results to .tex files, using a tool like Stargazer. (Cheat sheet available here).

The second option is to use a tool called Sweave that allows users to directly embed R code into LaTeX documents, such that the code is run every time the LaTeX file is compiled.

Exporting from Pandas / Python

Importing into LaTeX

Most export programs will create a .tex file. To add this .tex file to your LaTeX document, just add the line: \input{filename.tex} to your document where you want to output to appear!