我是在 R 中打包
mice
的新手。但我正在尝试从 popmis
中估算 5 个数据集,然后分别拟合一个 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?
我有适合你的解决方案。它就像
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)
结果:
> 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
@polkas 对不起。您能告诉我为什么在混合效应模型的结果合并后,我们没有看到随机效应项吗?