0% found this document useful (0 votes)
11 views40 pages

R Tables for Publication Ready Manuscripts

This document discusses how to use the R packages sjPlot and stargazer to create publication-ready tables from R output. It provides examples of using sjPlot to create descriptive statistics tables, contingency tables, correlation matrices, and summaries of linear models. It also demonstrates using stargazer to create descriptive statistics tables and summaries of linear models. The document recommends these packages as ways to save time and errors compared to manually creating tables in Word.

Uploaded by

Jake Tolentino
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views40 pages

R Tables for Publication Ready Manuscripts

This document discusses how to use the R packages sjPlot and stargazer to create publication-ready tables from R output. It provides examples of using sjPlot to create descriptive statistics tables, contingency tables, correlation matrices, and summaries of linear models. It also demonstrates using stargazer to create descriptive statistics tables and summaries of linear models. The document recommends these packages as ways to save time and errors compared to manually creating tables in Word.

Uploaded by

Jake Tolentino
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Creating publication-ready Word tables in R

Sara Weston and Debbie Yee

12/09/2016
Has this happened to you?

You’re working on a draft of a manuscript with your adviser, and


one of her edits is something like, “Did we control for time of day?
If not we should.”
So you rerun the results in R (or whatever) and the coefficients and
the standard errors and the t-values change. And you spend 2 hours
updating the manuscript.
Has this happened to you?

And then you send it back, and her next edit is, “Oh, we also have
to control for gender.”
You scream into the void.
Oh, and then you find that you skipped a row, so all your numbers
are off and have to be reentered.
Your time is valuable

You have better things to do than constantly re-update tables in


manuscripts and posters and talks.
In fact, it’s not worth your time to make any table by hand ever.
You are a human and you will make mistakes

No matter how smart you are, how careful you are, how much coffee
you have had to drink, you will make mistakes when you create
tables by hand.
Sorry, but them’s the facts.
Have we mentioned that R is amazing?

You might not have heard that R can do anything.


Before today, you were thinking that anything meant any analyses.
Or, if you were generous, any analyses and any graph.
Did you know that R can make your tables for you? Formatted,
with stars and everything? And by tables, I mean descriptive tables,
correlation matrices, summaries of regressions, summaries of
mixed-effects models.
Packages you should download

I sjPlot
I stargazer

These packages are the key to saving time and saving face.
sjPlot

Benefits

I Easier to use
I Can format correlation matrices
I A greater variety of descriptive options
I Colors!
I Also includes functions to plot regression diagnostics and results

Cons

I Less customizable
I Standardization doesn’t work
sjPlot

I descriptive tables
I contingency tables
I correlation matrices
I linear models

(These are just some of the table types.)


sjPlot

library(sjPlot)
data(iris)
head(iris)

## [Link] [Link] [Link] [Link] Spec


## 1 5.1 3.5 1.4 0.2 set
## 2 4.9 3.0 1.4 0.2 set
## 3 4.7 3.2 1.3 0.2 set
## 4 4.6 3.1 1.5 0.2 set
## 5 5.0 3.6 1.4 0.2 set
## 6 5.4 3.9 1.7 0.4 set
sjPlot - descriptive table

If you type the following code into Rstudio, you will see what the
table looks like in the Viewer window.

[Link](iris,
file="sjt_des.doc") #CRUCIAL - save this as a
#word document
sjPlot - descriptive table

Figure 1:
sjPlot - descriptive table

There are lots of ways you can customize the table.

[Link](iris,
[Link] = T, # this colors the rows
title = "Descriptive statistics", #always give
#your tables
#titles
file = "sjt_des_2.doc")
sjPlot - descriptive table

Figure 2:
sjPlot - contingency table

First, create a new variable with me

iris$LongPetal <- ifelse(iris$[Link] > 4.5, 1, 0)

Now we can create a contingency table to see whether one species


of flower is more likely to have long petals.

[Link](iris$Species, #rows
iris$LongPetal, #columns
file = "sjt_contingency.doc")
sjPlot - contingency table

Figure 3:
sjPlot - correlation matrix

Specify which variables you want to see in the correlation matrix


using any method of subsetting or indexing you like.

[Link](iris[,c("[Link]", "[Link]",
"[Link]", "[Link]")],
file="sjt_corr.doc")
sjPlot - correlation matrix

Figure 4:

Note: * = p < .05, ** = p < .01, *** = p < .001


sjPlot - correlation matrix

Note the defaults for this are the spearman method of calculating
correlations and list-wise deletion. This is different from other
functions you may be familiar with, which often default to the
pearson method and pairwise deletion. This will be printed with the
table, in case you forget.

[Link](iris[,c("[Link]", "[Link]",
"[Link]", "[Link]")],
[Link] = "pearson",
[Link] = "pairwise",
file="sjt_corr_2.doc")
sjPlot - correlation matrices

Figure 5:
a quick side note
One of the more useful but never discussed features of R is the
ability to assign attributes to objects. Often you want your column
names to be short (this helps keep code clean) but you also want to
know exactly what the variable is. Also, you probably want to have
some punctuation and spaces in the variable name. You can have
both with attributes.

# create a fake data frame


[Link] <- [Link](age = c(18, 20, 24, 17, 43),
edu = c(1, 3, 1, 5, 2),
rt = c(.01, .04, .02,
.10, .06))
#assign label to columns
attr([Link]$age, "label") <- "Age in years"
attr([Link]$edu, "label") <- "Education"
attr([Link]$rt, "label") <- "Reaction time"
a quick side note
sjPlot will use the labels as column and row names.

[Link]([Link], [Link]=F,
[Link] = "pearson",
file="sjt_corr_3.doc")
sjPlot - linear model
Estimate a few linear models.

mod1 <- lm([Link] ~ Species,


data=iris)
mod2 <- lm([Link] ~ Species + [Link],
data=iris)
mod3 <- lm([Link] ~ Species + [Link] +
Species:[Link],
data=iris)

Put all models into one table simultaneously (just like you might in
a manuscript).

[Link](mod1, mod2, mod3,


file="sjt_linear.doc")
sjPlot - linear models

Figure 7:
sjPlot - linear models

[Link](mod1, mod2, mod3,


[Link] = c("Veriscolor", "Virginica",
"Petal Width",
"Versicolor x Petal Width",
"Virginica x Petal Width"),
[Link] = F, [Link] = T,
[Link] = c("Petal Length",
"Petal Length",
"Petal Length"),
file="sjt_linear_2.doc")
sjPlot - linear models

Figure 8:
sjPlot - linear models
But what if I’m predicting multiple outcomes with the same models?
Go for it!

mod4 <- lm([Link] ~ Species,


data=iris)
mod5 <- lm([Link] ~ Species + [Link],
data=iris)
mod6 <- lm([Link] ~ Species + [Link] +
Species:[Link],
data=iris)

[Link](mod1, mod2, mod3, mod4, mod5, mod6,


[Link] = c("Veriscolor", "Virginica",
"Petal Width",
"Versicolor x Petal Width",
"Virginica x Petal Width"),
[Link] = F, [Link] = T,
file="sjt_linear_3.doc")
sjPlot - linear models

Figure 9:
sjPlot - more models

This package can handle general linear models (e.g., binary models,
probit models). To create these tables, use the function [Link].
Also, you can create summaries of mixed effects models using
[Link] and [Link].
stargazer
Pros

I more control over output


I can select which predictors you want in the table, more options
for fit statistics
I can import different estiamtes for coefficients, standard errors,
confidence intervals and p values (if you want to use odds
ratios or adjusted pvalues)
I one function, no matter what you’re tabling
I cleaner descriptive table
I works with Sweave and knitr

Cons

I more options = more complicated


I can’t create formatted correlation tables
I more limited in terms of descriptives
I can’t see output in viewer
stargazer - descriptives

library(stargazer)
stargazer(iris,
#note you have to specify type
type = "html",
#note that the argument is "out" not "file"
out="star_descriptive.doc")
stargazer - descriptive

Figure 10:
stargazer - descriptives

stargazer(iris,
digits=2,
title="Descriptive Statistics",
type = "html",
out="star_descriptive_2.doc")
stargazer - descriptive

Figure 11:
stargazer - linear model

stargazer(mod1, mod2, mod3,


type="html",
out="star_linear.doc")
stargazer - linear model

Figure 12:
stargazer - linear model

stargazer(mod1, mod2, mod3, mod4, mod5, mod6,


type="html",
out="star_linear_2.doc",
[Link] = F,
[Link] = T,
ci = T, digits=2,
notes = "This is a caption.",
[Link] = T,
[Link] = T,
[Link] = c("Constant","Veriscolor",
"Virginica", "Petal Width",
"Versicolor x Petal Width",
"Virginica x Petal Width"))
stargazer - linear model

Figure 13:
stargazer - general linear model

mod7 <- glm(LongPetal ~ Species + [Link],


data=iris,
family = "binomial")

stargazer(mod1, mod2, mod7,


type="html",
out="star_linear_3.doc",
[Link] = F,
[Link] = T,
digits=2)
stargazer - linear model

Figure 14:

You might also like