automatically generate LaTeX tables of lme model results

2,021 views
Skip to first unread message

Bonnie Dixon

unread,
Jun 14, 2014, 7:37:17 PM6/14/14
to davi...@googlegroups.com
Dear D-RUG friends;

I am using the function, lme, within the package, nlme, to create mixed effects models.  I would like to include tables of the model results in PDF documents that I create with R Markdown.  Does anyone know of a simple way to generate a LaTeX table of lme model results?  You would think there would be a package that does this, but so far my googling of the topic has revealed that neither xtable, nor stargazer, has a method for lme objects.  texreg does handle lme objects, but the resulting table does not include the random effects, which are an essential part of the results.

Here is the closest I have come to a solution:

require(nlme)
require(texreg)
mod <- lme(distance~age, ~age|Subject, data=Orthodont)
texreg(mod)

When run in an R Markdown document to generate a PDF, this results in a nice table, but it is not complete because it doesn't include the variance components of the random effects.  If anyone knows of a way to get texreg to include random effects, or a different way of doing this, please let me know.  Thanks!

Bonnie

Joel Schwartz

unread,
Jun 14, 2014, 9:11:45 PM6/14/14
to davi...@googlegroups.com
I haven't used it, but the stargazer package is specifically designed to create Latex (and HTML) tables of R regression model results.

CRAN page: http://cran.r-project.org/web/packages/stargazer/index.html

Vignette: http://cran.r-project.org/web/packages/stargazer/vignettes/stargazer.pdf

Joel

Bonnie Dixon

unread,
Jun 14, 2014, 9:29:53 PM6/14/14
to davi...@googlegroups.com
Yeah, I tried the stargazer package.  Unfortunately, it does not have a method for handling lme objects.  That is not one of the types of regression models it supports.

Bonnie


--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Noam Ross

unread,
Jun 14, 2014, 10:33:38 PM6/14/14
to Davis R Users Group

I had no idea on this one so I posted the question to twitter, and none other than Ben Bolker (!) (Also, Dave Harris) replied to use VarCorr(mod). VarCorr returns a matrix of random effects, but in nlme it’s tricky because it has the class "VarCorr.lme", which xtable doesn’t recognize. So do this:

tbl = VarCorr(mod)
class(tbl) = "matrix"
xtable(tbl)

If you use VarCorr for an lme4 model it is returned as a list of matrices, so you’ll want to extract the list elements but you don’t have to modify the class.

It’s not as pretty as a stargazer table and you’ll have to include it in addition to your standard table, but it has the info you are looking for, I think.

I note that there are a few other packages that generate pretty tables in R Markdown. I like pander. Andrew MacDonald (@poleasunder) made this handy reference chart:

Inline image 2

- Noam



--

Bonnie Dixon

unread,
Jun 14, 2014, 11:13:44 PM6/14/14
to davi...@googlegroups.com
Nice!  That flow chart is, indeed, handy.  I'll have to save it for reference.

OK, I tried that method using VarCorr and xtable, and in combination with the texreg table I already had I've got this:

Inline image 1
Inline image 2

These two tables in combination will provide complete reporting of my results.  Thanks, Noam!

Bonnie

Bonnie Dixon

unread,
Jun 14, 2014, 11:28:23 PM6/14/14
to davi...@googlegroups.com
Meanwhile, I have also been working on making a user-defined function for this, which sets up a matrix of character values and uses pander to make it into a LaTeX table.  When I give it an lme model, it generates this:

Inline image 2
This looks pretty similar to the layout that I've commonly seen in the literature.  Now if I could figure out how to extract p-values for the random effects from the lme object, it would be looking pretty good.

Bonnie
Reply all
Reply to author
Forward
0 new messages