Skip to content

Instantly share code, notes, and snippets.

@kleinschmidt
Created January 13, 2014 17:00
Show Gist options
  • Save kleinschmidt/8403790 to your computer and use it in GitHub Desktop.
Save kleinschmidt/8403790 to your computer and use it in GitHub Desktop.
Code to turn lmer models into a nice latex table. Specify model object or coefficients. Based on functions by Judith Degen (modified by Florian Jaeger): http://hlplab.wordpress.com/2010/06/15/r-code-for-latex-tables-of-lmer-model-effects/
mod.to.table <- function(mod.all=NA, prednames=NA, pred_name_subs=NA, file="",
coefs=as.data.frame(summary(mod.all)@coefs),
...) {
if (is.na(prednames)) {
if (is.na(pred_name_subs)) {
prednames <- row.names(coefs)
} else {
prednames <- str_replace_multi(row.names(coefs), pred_name_subs, replace.all=T)
}
}
coefs[,1] = round(coefs[,1],digits=2)
coefs[,2] = round(coefs[,2],digits=2)
coefs[,3] = round(coefs[,3],digits=1)
coefs[,4] = ifelse(coefs[,4] > .05, paste(round(coefs[,4],digits=1),sep=""),
ifelse(coefs[,4] < .0001, "\\textbf{<.0001}",
ifelse(coefs[,4] < .001,"\\textbf{<.001}",
ifelse(coefs[,4] < .01, "\\textbf{<.01}", "\\textbf{<.05}"))))
colnames(coefs) = c("Coef $\\beta$","SE($\\beta$)", "\\textbf{z}","\\textbf{p}")
prednames = data.frame(PName=row.names(coefs),NewNames=prednames)
row.names(coefs) = prednames$NewNames[prednames$PName == row.names(coefs)]
latex(coefs,file=file,title="",table.env=FALSE,booktabs=TRUE, ...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment