For publication-ready tables and graphics, R has many wonderful packages to automate the process. Some of these packages are good for html documents (websites) and some are good for making PDF documents (reports and papers). This document demonstrates different ways of making regression tables in your reports, and talks about some weird wrinkles with using them with multilevel modeling.
7.1 The basics of regression tables
For the basics we quickly illustrate regression tables using a subset of the Making Caring Common dataset, which we will eventually discuss in class. This dataset has a measure of emotional safety (our outcome) and we want to see, in a specific school, if this is predicted by gender and/or grade.
For our multilevel examples, we use the Making Caring Common data from Project A, and fit data to the 8th grade students only, but do it for all schools. We have made a High School dummy variable.
Our two models we use for demo purposes have a HS term and no HS term:
In the next sections we first show how to get better summary output (according to some folks) and then we walk through making regression tables in a bit more detail than above.
7.3 Getting p-values for lmer output
The lmerTest package is a way of making R give you more complete output. We are going to load it, and then put the new lmer models into new variables so we can see how the different model fitting packages work with the regression table packages below.
Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: esafe ~ 1 + HS + (1 | ID)
Data: dat.g8
REML criterion at convergence: 2746.8
Scaled residuals:
Min 1Q Median 3Q Max
-3.3883 -0.6156 0.2021 0.7628 1.7331
Random effects:
Groups Name Variance Std.Dev.
ID (Intercept) 0.04809 0.2193
Residual 0.46459 0.6816
Number of obs: 1305, groups: ID, 26
Fixed effects:
Estimate Std. Error df t value Pr(>|t|)
(Intercept) 3.52798 0.08637 29.91033 40.846 <2e-16 ***
HSTRUE -0.29480 0.10787 25.77814 -2.733 0.0112 *
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Correlation of Fixed Effects:
(Intr)
HSTRUE -0.801
7.4 The texreg package
In texreg there are two primary functions for table making, one is screenreg() and the other is texreg().
7.4.1 Using screenreg()
Screenreg is fine for MLMs. It looks a bit like raw output, but it is clear and clean. It will take models fit using lmer or lmerTest, no problem.
screenreg(list(modA,modB))
===============================================
Model 1 Model 2
-----------------------------------------------
(Intercept) 3.35 *** 3.53 ***
(0.06) (0.09)
HSTRUE -0.29 **
(0.11)
-----------------------------------------------
AIC 2756.78 2754.79
BIC 2772.30 2775.49
Log Likelihood -1375.39 -1373.40
Num. obs. 1305 1305
Num. groups: ID 26 26
Var: ID (Intercept) 0.07 0.05
Var: Residual 0.46 0.46
===============================================
*** p < 0.001; ** p < 0.01; * p < 0.05
Comment: Note that the number of stars are different for the display vs the summary output! (Look at the HS coefficient for example.) Not good, it would seem.
This is because the \(p\)-values are calculated using the normal approximation by the screenreg command, and using the \(t\)-test with approximate degrees of freedom by lmerTest. This makes a difference. Consider the following, using the \(t\) statistics for the HS variable:
2*pt( -2.733, df=25.77814 )
[1] 0.0111831
2*pnorm( -2.733 )
[1] 0.006276033
One is below 0.01, and one is not. An extra star!
7.4.2 Using texreg() and TeX
The texreg command is part of the texreg package and can be integrated with latex (which you would need to install). Once you do this, when you compile to a pdf, all is well. In the R code chunk you need to include results="asis" to get the latex to compile right. E.g., “r, results="asis"” when you declare a code chunk.
texreg(list(modA,modB), table=FALSE)
Note that the table=FALSE puts the table right where you want it, not at some random spot latex things is nice. Latex likes to have “floating tables,” where it puts the table where there is space; this makes it easier to make the entire formatted page look nice.
One issue is stargazer does not include the random effect variances, so the output is quite limited for multilevel modeling. It also has less stringent conditions for when to put down stars. One star is below 0.10, two is below 0.05, and three is below 0.01. This is quite generous. Also it is using the normal approximation.
7.5.1 Stargazer with lmerTest
Stargazer with lmerTest is a bit fussy. This shows how to make it work if you have loaded the lmerTest package. Recall the lmerTest package makes your lmer commands have p-values and whatnot. But this means your new lmer() command is not quite the same as the old—and stargazer is expecting the old. You gix this by lying to R, telling it the new thing is the old thing. This basically works.
Now for stargazer, we need to tell it that our models are the right type. First note: