Forked from johnjosephhorton/memisc_update_for_lme4.R
Last active
August 29, 2015 14:05
-
-
Save SolomonMg/718864981f0d882404d5 to your computer and use it in GitHub Desktop.
Allows one to produce tables for lme4::glmer model object using the latest lme4 code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## ---------------------------------------------------------------------------- | |
| ## Author: Solomon Messing, forked from Jason Morgan's update to Martin Elff's | |
| ## memisc package). | |
| ## | |
| ## Notes: Additional methods for mtable formatting of lme4 model | |
| ## objects. Requires that the memisc package be loaded prior to | |
| ## sourcing these functions. | |
| ## ---------------------------------------------------------------------------- | |
| setSummaryTemplate(glmerMod = c("Log-likelihood" = "($logLik:f#)", | |
| "Deviance" = "($deviance:f#)", | |
| "AIC" = "($AIC:f#)", | |
| "BIC" = "($BIC:f#)", | |
| "N" = "($N:d)", | |
| "Groups" = "($Groups:d)")) | |
| getSummary.glmerMod <- function (obj, alpha = 0.05, ...) { | |
| # obj <- m2 | |
| smry <- summary(obj) | |
| coef <- smry$coefficients | |
| 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(coef) == 3) { | |
| p <- (1 - pnorm(smry@coefs[, 3])) * 2 | |
| coef <- cbind(coef, p, lower, upper) | |
| } | |
| else { | |
| coef <- cbind(coef, lower, upper) | |
| } | |
| # RE <- smry$varcor | |
| # ranef <- cbind(as.numeric(RE[,1]), as.numeric(RE[,3]), 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@frame[,c]) | |
| ## Model fit statistics. | |
| ll <- logLik(obj)[1] | |
| deviance <- deviance(obj) | |
| AIC <- AIC(obj) | |
| BIC <- BIC(obj) | |
| N <- as.numeric(smry$devcomp$dims["n"]) | |
| G <- as.numeric(smry$ngrps) | |
| sumstat <- c(logLik = ll, deviance = deviance, AIC = AIC, | |
| BIC = BIC, N = N, Groups = G) | |
| ## Return model summary. | |
| list(coef = coef, sumstat = sumstat, | |
| ranef = smry$varcor, | |
| contrasts = attr(model.matrix(obj), "contrasts"), | |
| xlevels = xlevels, call = obj@call) | |
| } | |
| ## ---------------------------------------------------------------------------- | |
| ## Try it out: | |
| library("memisc") | |
| library("lme4") | |
| m1 = glmer(select ~ recraceSES + (1|guid), | |
| data=ldatc2, family=binomial) | |
| m2 = glmer(select ~ recraceSES*black + (1|guid), | |
| data=ldatc2, family=binomial) | |
| m3 = glmer(select ~ recraceSES*income3 + (1|guid), | |
| data=ldatc2, family=binomial) | |
| mtab <- mtable(m1,m2,m3) | |
| mtab | |
| # Output to latex, getting rid of extra &'s | |
| gsub("&\\s*&", "&", toLatex(mtab)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment