rno*_*ian 8 r lme4 missing-data r-mice imputation
我是 R 包装新手mice
。但我正在尝试从中估算 5 个数据集popmis
,然后为每个数据集拟合一个模型,最后在它们之间拟合一个lmer()
模型。with()
pool()
我认为pool()
中的函数mice()
不适用于lmer()
来自lme4
包的调用,对吧?
如果是这种情况,有没有办法编写一个定制的函数,其作用类似于pool()
我下面的案例?
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?
Run Code Online (Sandbox Code Playgroud)
我有给你解决方案。它就像install.packages("broom.mixed")
然后一样简单library(broom.mixed)
。broom.mixed
包提供了正确的glance
方法
# 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)
Run Code Online (Sandbox Code Playgroud)
结果:
> 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
Run Code Online (Sandbox Code Playgroud)