在函数中调用 amelia 插补以计算多重插补后的 y-hat

Call amelia imputations in a function to calculate y-hat after multiple imputation

我最终想做的是在多重插补后得到 y-hat 分数,但 Amelia 没有提供拟合值。我有针对特定数据集执行此操作的代码,但我正在尝试创建一个函数来执行此操作,而不管数据集如何。类似于:

yhat<-function(a.out,num.obs,num.imp,model.qe){}

其中 num.impameliam 中使用的插补数。不过,这就是我卡住的地方:

如果 a.out 是一个 amelia 对象并且 names(a.out$imputations)[1] return 是 "imp1",为什么 a.out$imputations$names(a.out$imputations)[1] 不是 return与 a.out$imputations$"imp1"?

相同

对于a.out$imputations$names(a.out$imputations)[1],R 表示:Error: attempt to apply non-function

如何为单个插补和其中的变量创建通用调用?

示例取自 Amelia 文档

library(Amelia)

data(africa)
a.out <- amelia(x = africa, cs = "country", ts = "year", logs = "gdp_pc")

a.out$imputations 是一个列表

str(a.out$imputations)

列表中的元素可以通过名称或数字索引来调用

head(a.out$imputations$imp1)
head(a.out$imputations[["imp1"]])
head(a.out$imputations[[1]])

每个插补都是一个数据框。数据框只是一种特殊的列表,元素可以以这种方式调用。

head(a.out$imputations[[1]]$country)
head(a.out$imputations[[1]][["country"]])
head(a.out$imputations[[1]][[2]])
head(a.out$imputations[[1]][2])

最后一种方法与其他方法的不同之处在于它 return 是单列数据框,而其他方法 return 是向量。

要完全通过数字索引提取单个值,您可以执行 f.ex

a.out$imputations[[1]][3, 2]

a.out$imputations[[1]][[2]][3]