【问题标题】:Multi-level regression model on multiply imputed data set in R (Amelia, zelig, lme4)R(Amelia,zelig,lme4)中多重插补数据集的多级回归模型
【发布时间】:2013-05-15 17:41:31
【问题描述】:

我正在尝试对多重插补数据(使用 Amelia 创建)运行多级模型;该样本基于组 = 24,N= 150 的聚类样本。

library("ZeligMultilevel")
ML.model.0 <- zelig(dv~1 + tag(1|group), model="ls.mixed",
data=a.out$imputations)
summary(ML.model.0)

此代码产生以下错误代码:

Error in object[[1]]$result$call : 
$ operator not defined for this S4 class

如果我运行 OLS 回归,它会起作用:

model.0 <- zelig(dv~1, model="ls", data=a.out$imputations)
m.0 <- coef(summary(model.0)) 
print(m.0, digits = 2)

      Value Std. Error t-stat  p-value
[1,]    45       0.34    130 2.6e-285

我很高兴提供一个工作示例

require(Zelig)
require(Amelia)
require(ZeligMultilevel)

data(freetrade)
length(freetrade$country) #grouping variable

#Imputation of missing data

a.out <- amelia(freetrade, m=5, ts="year", cs="country")

# Models: (1) OLS; (2) multi-level 

model.0 <- zelig(polity~1, model="ls", data=a.out$imputations)
m.0 <- coef(summary(model.0)) 
print(m.0, digits = 2)

ML.model.0 <- zelig(polity~1 + tag(1|country), model="ls.mixed", data=a.out$imputations)
summary(ML.model.0)

我认为问题可能在于 Zelig 如何与 Amelia 的 mi 类交互。因此,我转向了另一种 R 包:lme4。

require(lme4)
write.amelia(obj=a.out, file.stem="inmi", format="csv", na="NA")
diff <-list(5)  # a list to store each model, 5 is the number of the imputed datasets

for (i in 1:5) {
file.name <- paste("inmi", 5 ,".csv",sep="")
data.to.use <- read.csv(file.name)
diff[[5]] <- lmer(polity ~ 1 + (1 | country),
data = data.to.use)}
diff

结果如下:

[[1]]
[1] 5

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
Linear mixed model fit by REML 
Formula: polity ~ 1 + (1 | country) 
   Data: data.to.use 
  AIC  BIC logLik deviance REMLdev
 1006 1015 -499.9     1002   999.9
Random effects:
 Groups   Name        Variance Std.Dev.
 country  (Intercept) 14.609   3.8222  
 Residual             17.839   4.2236  
Number of obs: 171, groups: country, 9

Fixed effects:
            Estimate Std. Error t value
(Intercept)    2.878      1.314    2.19

当我将diff[[5]] 替换为diff[[4]]diff[[3]] 等时,结果保持不变。不过,我想知道这实际上是组合数据集的结果还是单个估算数据集的结果。有什么想法吗?谢谢!

【问题讨论】:

  • 愿意提供一个我们可以摆弄的工作示例吗?
  • 谢谢罗曼。我提供了一个工作示例。您知道如何解决错误吗?那太棒了!
  • summary方法肯定有bug。如果有帮助,您可以单独访问每个插补的系数(例如coef(ML.model.0$imp1$result))。
  • 谢谢。不幸的是,我需要组合数据集的结果。让我们希望其他人有这个问题的答案。

标签: r regression missing-data multi-level r-zelig


【解决方案1】:

我修改了该对象的摘要函数(获取源并打开 ./R/summary.R 文件)。我添加了一些花括号以使代码流畅,并将getcoef 更改为coef。这应该适用于这种特殊情况,但我不确定它是否普遍。函数getcoef 搜索槽coef3,我从来没有见过这个。也许@BenBolker 可以在这里关注一下?我不能保证这是结果的样子,但输出在我看来是合法的。也许您可以联系包作者以在将来的版本中更正此问题。

总结(ML.model.0)

  Model: ls.mixed
  Number of multiply imputed data sets: 5 

Combined results:

Call:
zelig(formula = polity ~ 1 + tag(1 | country), model = "ls.mixed", 
    data = a.out$imputations)

Coefficients:
        Value Std. Error   t-stat    p-value
[1,] 2.902863   1.311427 2.213515 0.02686218

For combined results from datasets i to j, use summary(x, subset = i:j).
For separate results, use print(summary(x), subset = i:j).

修改功能:

summary.MI <- function (object, subset = NULL, ...) {
  if (length(object) == 0) {
    stop('Invalid input for "subset"')
  } else {
    if (length(object) == 1) {
      return(summary(object[[1]]))
    }
  }

  # Roman: This function isn't fecthing coefficients robustly. Something goes wrong. Contact package author. 
  getcoef <- function(obj) {
    # S4
    if (!isS4(obj)) {
      coef(obj)
    } else {
      if ("coef3" %in% slotNames(obj)) {
        obj@coef3
      } else {
        obj@coef
      }
    }
  }

    #
    res <- list()

    # Get indices
    subset <- if (is.null(subset)) {
      1:length(object)
    } else {
      c(subset)
    }

    # Compute the summary of all objects
    for (k in subset) {
      res[[k]] <- summary(object[[k]])
    }


    # Answer
    ans <- list(
      zelig = object[[1]]$name,
      call = object[[1]]$result@call,
      all = res
    )

    #
    coef1 <- se1 <- NULL

    #
    for (k in subset) {
#       tmp <-  getcoef(res[[k]]) # Roman: I changed this to coef, not 100% sure if the output is the same
      tmp <- coef(res[[k]])
      coef1 <- cbind(coef1, tmp[, 1])
      se1 <- cbind(se1, tmp[, 2])
    }

    rows <- nrow(coef1)
    Q <- apply(coef1, 1, mean)
    U <- apply(se1^2, 1, mean)
    B <- apply((coef1-Q)^2, 1, sum)/(length(subset)-1)
    var <- U+(1+1/length(subset))*B
    nu <- (length(subset)-1)*(1+U/((1+1/length(subset))*B))^2

    coef.table <- matrix(NA, nrow = rows, ncol = 4)
    dimnames(coef.table) <- list(rownames(coef1),
                                 c("Value", "Std. Error", "t-stat", "p-value"))
    coef.table[,1] <- Q
    coef.table[,2] <- sqrt(var)
    coef.table[,3] <- Q/sqrt(var)
    coef.table[,4] <- pt(abs(Q/sqrt(var)), df=nu, lower.tail=F)*2
    ans$coefficients <- coef.table
    ans$cov.scaled <- ans$cov.unscaled <- NULL

    for (i in 1:length(ans)) {
      if (is.numeric(ans[[i]]) && !names(ans)[i] %in% c("coefficients")) {
        tmp <- NULL
        for (j in subset) {
          r <- res[[j]]
          tmp <- cbind(tmp, r[[pmatch(names(ans)[i], names(res[[j]]))]])
        }
        ans[[i]] <- apply(tmp, 1, mean)
      }
    }

    class(ans) <- "summaryMI"
    ans
  }

【讨论】:

  • 非常感谢您为寻找解决方案付出的巨大努力。这很棒!! :-) 需要一些时间来考虑这个功能。
  • 谢谢!这挽救了我的理智。我注意到这个函数还提供了 p 值,即使在非 MI 数据集上运行混合模型时,zelig 也不会这样做。我认为这是因为关于如何计算 df 存在分歧。您能否为您使用的公式提供参考?
  • 这对我不起作用。但事实证明,唯一的错误出现在call = object[[1]]$result@call, 行中。变量call 再也不会被引用,所以我能够注释掉这一行而没有明显的后果。
猜你喜欢
  • 2017-12-03
  • 1970-01-01
  • 2018-09-06
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-21
  • 2023-04-07
  • 2018-04-08
相关资源
最近更新 更多