Search code examples
rlme4lmer

How to (quickly) extract t values from lmer model in lme4?


I am working with a script that calls lmer function of the lme4 package thousands of times (do not worry, relevant correction for multiple comparisons is performed later) and would like to save as much time during a single call as possible.

I want to extract t-values from the fitted model, what is the fastest (computation time) way to do this? I tried using summary(model) but it seems to take (much) longer than calling lmer itself. Is it possible to get the t-values from the obtained model without using summary()?


Solution

  • The best way to answer this question is to look at the code of lme4:::summary.merMod to figure out how to get the pieces you need. This ought to do it:

    library(lme4)
    fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
    t.stat <- function(x) fixef(x)/sqrt(diag(vcov(x)))
    t.stat(fm1)
    ##    (Intercept)        Days 
    ##     36.838311    6.771485 
    coef(summary(fm1))[,"t value"]  ## identical
    

    Depending on what characteristics are common across your thousands of calls to lmer there may be other opportunities for computational efficiency: see e.g. ?refit and ?modular.

    (Looking at the code of lme4:::summary.merMod doesn't reveal anything obviously time-consuming: I would be interested in profiling results that say what's slow in those computations.)