[R-sig-ME] lmer and latex

Jason Morgan jwm-r-mixed-models at skepsi.net
Sun Mar 13 18:48:25 CET 2011


On 2011.03.13, Sebastián Daza wrote:
> Hi everyone,
> Does anyone know a fancy way to combine several lmer models and to 
> create a latex table?
> 
> It is possible to do it with xtable, but maybe someone knows a better 
> way to do it.
> Thank you in advance,

Hello Sebastian,

Check out the mtable() and toLatex() functions in the memisc package. The (very
basic) methods for setSummaryTemplate and getSummary I have included below
should work with the released version of lme4. However, the beta version,
lme4a, appears to make significant changes to results objects. You will need to
make changes to the below code if you are using that version.

~Jason

## ----------------------------------------------------------------------------
## Additional methods for mtable formatting.
## ----------------------------------------------------------------------------

setSummaryTemplate(mer = c("Log-likelihood" = "($logLik:f#)",
                     "Deviance" = "($deviance:f#)",
                     "AIC" = "($AIC:f#)",
                     "BIC" = "($BIC:f#)",
                     "N" = "($N:d)",
                     "Groups" = "($Groups:d)"))

getSummary.mer <- function (obj, alpha = 0.05, ...) {
  smry <- summary(obj)
  coef <- smry at coefs
  lower <- qnorm(p = alpha/2, mean = coef[, 1], sd = coef[,2])
  upper <- qnorm(p = 1 - alpha/2, mean = coef[, 1], sd = coef[,2])
  if (ncol(smry at coefs) == 3) {
    p <- (1 - pnorm(smry at coefs[, 3])) * 2
    coef <- cbind(coef, p, lower, upper)
  }
  else {
    coef <- cbind(coef, lower, upper)
  }
  RE <- smry at REmat
  ranef <- cbind(as.numeric(RE[,3]), as.numeric(RE[,4]), NA,NA,NA,NA)
  rownames(ranef) <- paste("Ranef", RE[,1], sep = " - ")
  coef <- rbind(coef, ranef)
  colnames(coef) <- c("est", "se", "stat", "p", "lwr", "upr")

  ## Factor levels.
  xlevels <- list()
  Contr <- names(attr(model.matrix(obj), "contrasts"))
  for (c in Contr) xlevels[[c]] <- levels(obj at frame[,c])

  ## Model fit statistics.
  ll <- logLik(obj)[1]
  deviance <- deviance(obj)
  AIC <- AIC(obj)
  BIC <- BIC(obj)
  N <- as.numeric(smry at dims["n"])
  G <- as.numeric(smry at ngrps)
  sumstat <- c(logLik = ll, deviance = deviance, AIC = AIC,
               BIC = BIC, N = N, Groups = G)

  ## Return model summary.
  list(coef = coef, sumstat = sumstat,
       contrasts = attr(model.matrix(obj), "contrasts"),
       xlevels = xlevels, call = obj at call)
}

## ----------------------------------------------------------------------------

-- 
Jason W. Morgan
Ph.D. Candidate
Department of Political Science
*The Ohio State University*
154 North Oval Mall
Columbus, Ohio 43210




More information about the R-sig-mixed-models mailing list