2.4 Tools for writing up results

2.4.1 R Markdown

R Markdown is a free tool within RStudio that allows you to weave together text and code (along with images and tables) into the same document. It can compile these documents (written inside R Studio) into html, pdf, or Word doc output files. R Markdown can be incredibly helpful for doing things like generating replication files, writing up problem sets, or even writing papers.

This site includes an introduction to R Markdown.

R Markdown has its own syntax, which includes functionality for writing mathematical equations. The pdf output option in R Markdown requires LaTex, described in the next section.

2.4.2 LaTex

LaTex is a typesetting program for drafting documents, much like Microsoft Word. Some advantages of using LaTex in writing empirical research papers is, once you learn the basics of the program, it can become easier to add tables, figures, and equations into a paper with little effort. The downside is that it has its own syntax, which takes a little time to learn. LaTex also has a feature called “beamer” which uses LaTex to generate slides. You can use this for presentations. LaTex “compiles” documents into pdf files.

Here is one introduction for getting started with LaTex that starts from the installation stage. Here also is a link to a set of slides from Overleaf discussing the basics of LaTex: Slides

You can download the Tex distribution for your operating system here. In addition to this, there are many programs available for running LaTex on your computer, which range from free, very basic tools (e.g., TexWorks) to tools with fancy capabilities.

Overleaf is an online program for drafting LaTex documents. It has a nice feature where it allows you to share a document so that multiple people can work on the document simultaneously. This makes Overleaf a great program for projects where you have co-authors. The basic tools in Overleaf are available for free, but if you want to start sharing documents with a lot of co-authors, it requires a paid account.

LaTex also has the ability to integrate citations into your documents. The part 2 tutorial from Overleaf goes over this.

RStudio also has a program built-in called Sweave (.Rnw) documents that works with knitR, which weave together R code and LaTex syntax, allowing you to compile them into pdf documents and slide presentations. This is very similar to how R Markdown works, but with somewhat different syntax. See here for an overview. Your problem sets are generally Sweave/knitR documents.

2.4.3 Formatting and Exporting R Results

R has a number of tools, including the packages texreg, xtable, and stargazer, which can be used to export tables made in R to nicely formatted LaTex or html output.

Here is a link to the texreg package documentation. Section 5 has examples of the texreg and htmlreg functions within the texreg package. These can be integrated into R Markdown and Sweave documents, and their output can be pasted into LaTex or Microsoft Word.

Your choice of function will depend on where you ultimately want your results to be compiled. If you are generating results that will be compiled to pdf using LaTex, then texreg works well. If you are exporting results to Word, than you may wish to use the htmlreg function within the texreg package, which will generate output that can be pasted into Word.

A simple example using R Markdown html output. (Note, if you wanted to export the table to Word, you would add an argument specifying file = "myfit.doc" to the function. See the above link for examples:

mydata <- read.csv("https://raw.githubusercontent.com/ktmccabe/teachingdata/main/resume.csv")
fit <- lm(call ~ race, data=mydata)
## First time you use texreg, install it
install.packages("texreg")
library(texreg)
htmlreg(list(fit),
        stars=c(0.001, 0.01, 0.05),
        caption = "Regression of Call Backs on Race")
Regression of Call Backs on Race
  Model 1
(Intercept) 0.06***
  (0.01)
racewhite 0.03***
  (0.01)
R2 0.00
Adj. R2 0.00
Num. obs. 4870
p < 0.001; p < 0.01; p < 0.05

You can add more arguments to the function to customize the name of the model and the coefficients. You can also add multiple models inside the list argument, for example, if you wanted to present a table with five regression models at once. Here is an example with two:

fit2 <- lm(call ~ race + sex, data=mydata)

library(texreg)
htmlreg(list(fit, fit2),
        stars=c(0.001, 0.01, 0.05),
        caption = "Regression of Call Backs on Race and Sex")
Regression of Call Backs on Race and Sex
  Model 1 Model 2
(Intercept) 0.06*** 0.07***
  (0.01) (0.01)
racewhite 0.03*** 0.03***
  (0.01) (0.01)
sexmale   -0.01
    (0.01)
R2 0.00 0.00
Adj. R2 0.00 0.00
Num. obs. 4870 4870
p < 0.001; p < 0.01; p < 0.05

2.4.4 Additional formatting examples

Here are some additional examples with different formats. You can run them on your own computer to see what the output looks like.

The package texreg has three primary formats

  • texreg() for LATEX output;
  • htmlreg() for HTML, Markdown-compatible and Microsoft Word-compatible output;
  • screenreg() for text output to the R console.

If you are working with a LaTex document, I recommend using texreg(), which will output LaTex syntax in your R console, which you can copy and paste into your article document.

Note: this function allows you to customize model and coefficient names.

library(texreg)
texreg(list(fit, fit2),
        stars=c(0.001, 0.01, 0.05),
        caption = "Regression of Call Backs on Race and Sex",
       custom.model.names = c("Bivariate", "Includes Sex"),
       custom.coef.names = c("Intercept",
                             "Race- White",
                             "Sex- Male"))

If you are working with a Microsoft Word document, I recommend using htmlreg() and specifying a file name for your output. This will export a file to your working directory, which you can copy and paste into your Word article document. Otherwise, the syntax is the same as above.

library(texreg)
htmlreg(list(fit, fit2), file = "models.doc",
        stars=c(0.001, 0.01, 0.05),
        caption = "Regression of Call Backs on Race and Sex",
       custom.model.names = c("Bivariate", "Includes Sex"),
       custom.coef.names = c("Intercept",
                             "Race- White",
                             "Sex- Male"))

If you are trying to read the output in your R console, that’s when I would use screenreg(). However, for professional manuscript submissions, I would recommend the other formats.

library(texreg)
screenreg(list(fit, fit2), 
        stars=c(0.001, 0.01, 0.05),
        caption = "Regression of Call Backs on Race and Sex",
       custom.model.names = c("Bivariate", "Includes Sex"),
       custom.coef.names = c("Intercept",
                             "Race- White",
                             "Sex- Male"))

The package stargazer allows similar options. I don’t think there are particular advantages to either package. Whatever comes easiest to you. The default for stargazer will output LaTex code into your R console.

  • Note that the syntax is similar but has slightly different argument names from the texreg package.
  • Also, the intercept is at the bottom by default for stargazer. Be careful of the covariate ordering when you add labels.
library(stargazer)
stargazer(list(fit, fit2), 
        star.cutoffs=c(0.05,0.01, 0.001),
        title= "Regression of Call Backs on Race and Sex",
        dep.var.labels.include = F,
       column.labels = c("Call Back", "Call Back"),
       covariate.labels = c("Race- White",
                             "Sex- Male",
                             "Intercept"))

You can adjust the type of output in stargazer for other formats, similar to texreg. Here is an example of Microsoft Word output.

library(stargazer)
stargazer(list(fit, fit2), out = "modelstar.doc", type="html",
        star.cutoffs=c(0.05,0.01, 0.001),
        dep.var.labels.include = F,
        title= "Regression of Call Backs on Race and Sex",
       column.labels = c("Call Back", "Call Back"),
       covariate.labels = c("Race- White",
                             "Sex- Male",
                             "Intercept"))

2.4.5 Additional Table Types

Sometimes you might want to create tables that are not from regression models, such as tables for descriptive statistics. R has other packages for tables of this type.

For example xtable can create simple html and latex tables. You just have to supply the function with a table object or matrix.

library(xtable)
table1 <- table(race = mydata$race, sex = mydata$sex)
## LaTeX
xtable(table1)
## Word
print(xtable(table1), type="html", file = "crosstab.doc")
## Html
print(xtable(table1), type="html")
female male
black 1886 549
white 1860 575