简体   繁体   English

使用Amelia II / mitools / Zelig和lme4分析R中混合级别模型的估算数据

[英]analyzing imputed data for mixed-level models in R with Amelia II / mitools / Zelig and lme4

I've been trying to analyze the results of multiple imputations in a multi-level model in R. I know that my lme4 model works. 我一直在尝试在R中的多级模型中分析多个插补的结果。我知道我的lme4模型可以工作。 I know that my imputation is working. 我知道我的估算工作正常。 But using Zelig OR using mitools::MIcombine() both throw similar errors about S4 objects. 但是使用Zelig或使用mitools :: MIcombine()都会引发关于S4对象的类似错误。

library(lme4)
library(mitools)

data(smi)
# this model works fine
model.f.glm <- function(df) {
  glm(drinkreg~wave*sex,family=binomial(), data=df)
}
# but this one will not
model.f.glmer <- function(df) {
  glmer(drinkreg~wave*sex + (1|id),family=binomial(), data=df)
}

# check the base model first:
model.f.glm(smi$imputations[[1]])
model.f.glmer(smi$imputations[[1]])

# use the mitools methods:
models.glm <- lapply(smi$imputations, model.f.glm)
models.glmer <- lapply(smi$imputations, model.f.glmer)

summary(MIcombine(models.glm))
# error:
summary(MIcombine(models.glmer))
# Error in cbar + results[[i]] : non-numeric argument to binary operator

# get MIcombine for them:
betas.glm <- MIextract(models.glm, fun = coef)
vars.glm  <- MIextract(models.glm, fun = vcov)
summary(MIcombine(betas.glm, vars.glm))
betas.glmer <- MIextract(models.glmer, fun = fixef)
vars.glmer  <- MIextract(models.glmer, fun = vcov)
# error:
summary(MIcombine(betas.glmer, vars.glmer))
# Error in diag(vcov(object)) : 
    # no method for coercing this S4 class to a vector

With Zelig and Amelia: 使用Zelig和Amelia:

library(Zelig)
library(ZeligMultilevel)
library(Amelia)

data(africa)
a.out <- amelia(x = africa, cs = "country", ts = "year", logs = "gdp_pc")
# glm:
z.out <- zelig(gdp_pc ~ trade + civlib, model = "ls", data = a.out)
summary(z.out)
# glmer:
z.out <- zelig(gdp_pc ~ trade + civlib + (1 | year), model = "ls.mixed", data = a.out)
# ERROR:
summary(z.out)
#Error in diag(vcovlist[[i]]) : 
  # no method for coercing this S4 class to a vector

I've looked over this previous post , where @Roman Luštrik put a bunch of effort into fixing the summary about four years ago -- but it seems to be an issue again. 我看了以前的这篇文章 ,@ RomanLuštrik在大约四年前花费了大量精力来修复摘要,但这似乎又是一个问题。 Any thoughts? 有什么想法吗?

要扩展JDB的建议,请尝试

vars.glmer  <- MIextract(models.glmer, fun = function(mod) as.matrix(vcov(mod)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM