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

时间:2017-06-30 01:34:45

标签: r lme4 imputation r-zelig

我一直在尝试分析R中多级模型中多个插补的结果。我知道我的lme4模型有效。我知道我的归责是有效的。但是使用Zelig OR使用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

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

我已经查看了this previous post,其中@RomanLuštrik在四年前为确定摘要做了大量工作 - 但这似乎又是一个问题。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

要扩展JDB的建议,请尝试

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