rmissing-datalme4imputationr-mice

Using imputed datasets from library mice() to fit a multi-level model in R


I'm new to package mice in R. But I'm trying to impute 5 datasets from popmis and then fit an lmer() model with() each and finally pool() across them.

I think the pool() function in mice() doesn't work with the lmer() call from lme4 package, right?

If that is the case, is there a way to write a custom-made function that acts like pool() for my case below?

library(mice)
library(lme4)

imp <- mice(popmis, m = 5) # `popmis` is a dataset from `mice`

fit <- with(imp, lme4::lmer(popular ~ sex + (1|school))) # works fine.

pool(fit) # BUT this one fails, should I loop here?

Solution

  • I have the solution for you. It is as simple as install.packages("broom.mixed") and then library(broom.mixed).broom.mixed package is providing the proper glance method

    # install.packages("broom.mixed")
    library(mice)
    library(lme4)
    library(broom.mixed)
    imp <- mice(popmis, m = 5) # `popmis` is a dataset from `mice`
    
    fit <- with(data = imp, exp = lme4::lmer(popular ~ sex + (1|school)))
    
    pool(fit) 
    

    Results:

    > pool(fit)
    Class: mipo    m = 5 
             term m  estimate        ubar            b           t dfcom       df        riv     lambda        fmi
    1 (Intercept) 5 4.9122016 0.007589694 0.0003823641 0.008048531  1996 743.8691 0.06045526 0.05700878 0.05953397
    2         sex 5 0.8378947 0.001187606 0.0002937859 0.001540149  1996  72.7305 0.29685175 0.22890184 0.24926611
    

    Ben Bolker is the author of broom.mixed