public

Change to memisc to support lme4 models

  • Download Gist
memisc_update_for_lme4.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
## ----------------------------------------------------------------------------
## Author: Jason Morgan (borrowing heavily from code contained in 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(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@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@coefs) == 3) {
p <- (1 - pnorm(smry@coefs[, 3])) * 2
coef <- cbind(coef, p, lower, upper)
}
else {
coef <- cbind(coef, lower, upper)
}
RE <- smry@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@frame[,c])
 
## Model fit statistics.
ll <- logLik(obj)[1]
deviance <- deviance(obj)
AIC <- AIC(obj)
BIC <- BIC(obj)
N <- as.numeric(smry@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,
contrasts = attr(model.matrix(obj), "contrasts"),
xlevels = xlevels, call = obj@call)
}
 
## ----------------------------------------------------------------------------

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.