Skip to content

Use apsrtable with merMod objects #1

@jknowles

Description

@jknowles

I'm attempting to update this package to construct tables for mixed effect models fit of the class merMod. Below is a simple example and the corresponding error message. I am wondering if there is some way I can work around the fundamental reliance on the $ operator so that S4 objects like merMod objects can be easily adapted by creating a summary function.

(fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy))

# adapted from code in package already
apsrtableSummary.merMod <- function (object, ...) {
  obj <- summary(object)
  fcoef <- coef(obj)
  out <- list()
  useScale <- obj$useScale
  corF <- vcov(object)@factors$correlation
  coefs <- cbind(fcoef[, 1:2])
  if (length (fcoef) > 0){
      if (obj$useScale == FALSE) {
        coefs <- coefs[, 1:2, drop = FALSE]
        out$z.value <- coefs[, 1]/coefs[, 2]
        out$p.value <- 2 * pnorm(abs(out$z.value), lower = FALSE)
        coefs <- cbind(coefs,
                       `z value` = out$z.value,
                       `Pr(>|z|)` = out$p.value)
      }
      else {
        out$t.value <- coefs[, 1]/coefs[, 2]
        coefs <- cbind(coefs, `t value` = out$t.value)
      }
    dimnames(coefs)[[2]][1:2] <- c("coef.est", "coef.se")
#       if(detail){
#         pfround (coefs, digits)
#       }
#       else{
#         pfround(coefs[,1:2], digits)
#     }
  }
  out$coef <- coefs[,"coef.est"]
  out$se <- coefs[,"coef.se"]
#   vc <- as.matrix(VarCorr(object, digits))
#   vc[,1] <-
#   print (vc[,c(1:2,4:ncol(vc))], quote=FALSE)

  out$ngrps <- lapply(object@flist, function(x) length(levels(x)))
  ## Model fit statistics.
  ll <- logLik(object)[1]
  deviance <- deviance(object)
  AIC <- AIC(object)
  BIC <- BIC(object)
  N <- as.numeric(length(obj$residuals))
  G <- as.numeric(obj$ngrps)
  sumstat <- c(logLik = ll, deviance = deviance, AIC = AIC,
               BIC = BIC, N = N, Groups = G)

  ## Return model summary.
  list(coef = obj$coefficients, sumstat = sumstat,
       contrasts = attr(model.matrix(object), "contrasts"),
       xlevels = NULL, call = object@call)
}

apsrtableSummary(fm1)

Results in :

$coef
             Estimate Std. Error   t value
(Intercept) 251.40510   6.824556 36.838311
Days         10.46729   1.545789  6.771485

$sumstat
   logLik  deviance       AIC       BIC         N    Groups 
-871.8141 1743.6283 1755.6283 1774.7860  180.0000   18.0000 

$contrasts
NULL

$xlevels
NULL

$call
lmer(formula = Reaction ~ Days + (Days | Subject), data = sleepstudy)

Which I think is OK, but when I move on to apsrtable the object with apsrtable(fm1), I get:

Error in x$se : $ operator not defined for this S4 class

The traceback sheds some light on where things are not going well:

traceback()
3: FUN(X[[1L]], ...)
2: lapply(models, function(x) {
       s <- try(apsrtableSummary(x), silent = TRUE)
       if (inherits(s, "try-error")) {
           s <- summary(x)
       }
       if (!is.null(x$se) && se != "vcov") {
           est <- coef(x)
           if (class(x$se) == "matrix") {
               x$se <- sqrt(diag(x$se))
           }
           s$coefficients[, 3] <- tval <- est/x$se
           e <- try(s$coefficients[, 4] <- 2 * pt(abs(tval), length(x$residuals) - 
               x$rank, lower.tail = FALSE), silent = TRUE)
           if (inherits(e, "try-error")) {
               s$coefficients[, 4] <- 2 * pnorm(abs(tval), lower.tail = FALSE)
           }
           s$se <- x$se
       }
       if (se == "pval") {
           s$coefficients[, 2] <- s$coefficients[, 4]
       }
       return(s)
   })
1: apsrtable(fm1)

Somehow, the reliance on x$se is a problem. What am I doing wrong to allow getting around this step and moving to creating the summary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions