rstatisticslme4mixed-modelslmertest

How to extract information criterions from `lme4::lmer`-model fitted by ML and combine with model summary from REML-fitted model


I am trying to access AIC, BIC , logLik and deviance data from a model summary of an HLM fitted using maximum likelihood (ML) in lme4::lmer, and combine with essentially the same model fitted with restricted maximum likelihood (REML). The structure of the objects returned from lmer and summary is a mess, and I am unable to find out where/how this data is stored.

[Update:] Based on the responses I've gotten, I have updated the code to reflect the progress made:

Code example:

# Least working example
library(lme4)
library(lmerTest)
df <- lme4::sleepstudy
names(df)
# Example model
model <- lmer(Reaction ~ (1|Subject), df, REML = TRUE)
information_criterion <- data.frame(
            "AIC" = AIC(model),
            "BIC" = BIC(model),
            "logLik" = logLik(model),
            "deviance" = deviance(model, REML=FALSE),
            "df.residual" = df.residual(model)
            )
mod_sum <- list(summary(model), information_criterion)
I essentially want to modify the output to resemble the output of summary if REML = FALSE (not working):
> mod_sum

Linear mixed model fit by REML. t-tests use Satterthwaite's method [
lmerModLmerTest]
Formula: Reaction ~ (1 | Subject)
   Data: df

## Information criterion injected here: ##########################

     AIC      BIC   logLik deviance df.resid   # <-- THESE ARE THE LINES I WANT
  1916.5   1926.1   -955.3   1910.5      177   # <-- 

##################################################################

REML criterion at convergence: 1904.3

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.4983 -0.5501 -0.1476  0.5123  3.3446 

Random effects:
 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 1278     35.75   
 Residual             1959     44.26   
Number of obs: 180, groups:  Subject, 18

Fixed effects:
            Estimate Std. Error     df t value Pr(>|t|)    
(Intercept)   298.51       9.05  17.00   32.98   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Solution

  • There are a few points:

    1. You have a typo here:
        m2sum[["information_criterion"]] <- summary(model1)$information_criterion
    

    It should be m2_sum

    1. Instead of summary(model1)$information_criterion you can use:
         AIC(model1)
    

    So, the following should work:

    m2_sum[["information_criterion"]] <- AIC(model1)
    

    Update following change to the OP.

    This should work, although please see my last comment, because this may not be a sensible thing to do:

    > m2_sum$AICtab <- m1_sum$AICtab
    > m2_sum
    
    Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
    Formula: Reaction ~ (1 | Subject)
       Data: df
    
         AIC      BIC   logLik deviance df.resid 
      1916.5   1926.1   -955.3   1910.5      177 
    
    Scaled residuals: 
        Min      1Q  Median      3Q     Max 
    -2.4983 -0.5501 -0.1476  0.5123  3.3446