如何从R包“mice”中提取聚合的填充数据?

6
我有一个关于R包'mice'创建的填充数据聚合的问题。
据我所知,'mice'的'complete'命令用于提取例如第一次填充的填充值。然而,在运行了十次填充后,我不确定应该提取哪些填充值。有人知道如何提取跨所有填充的(汇总)填充数据吗?
由于我想将数据输入到MS Excel中,并在另一个软件工具中执行进一步的计算,因此这样的命令非常有帮助。
感谢您的评论。下面是一个简单的示例('mice'自己提供):
R> library("mice")
R> nhanes
R> imp <- mice(nhanes, seed = 23109) #create imputation
R> complete(imp) #extraction of the five imputed datasets (row-stacked matrix)

我该如何汇总五个填充的数据集并将填充值提取到Excel中?


我认为它代表国家健康和营养调查。 - Metrics
3个回答

5

我也遇到了类似的问题。 我使用了下面的代码,它对于数值变量足够好用。 对于其他变量,我考虑随机选择一个输入结果(因为平均可能会破坏数据)。

我的建议代码是(针对数值型变量):

tempData <- mice(data,m=5,maxit=50,meth='pmm',seed=500)
completedData <- complete(tempData, 'long')
a<-aggregate(completedData[,3:6] , by = list(completedData$.id),FUN= mean)
  1. 您应该将结果合并回来。
  2. 我认为'Hmisc'是一个更好的包。
  3. 如果您已经找到了更好/更优雅/内置的解决方案,请与我们分享。

1
你应该使用complete(imp, action="long")来获取每个插补的值。如果你看到?complete,你会发现:

complete(x, action = 1, include = FALSE)

Arguments

x   
An object of class mids as created by the function mice().

action  
If action is a scalar between 1 and x$m, the function returns the data with imputation number action filled in. Thus, action=1 returns the first completed data set, action=2 returns the second completed data set, and so on. The value of action can also be one of the following strings: 'long', 'broad', 'repeated'. See 'Details' for the interpretation.

include 
Flag to indicate whether the orginal data with the missing values should be included. This requires that action is specified as 'long', 'broad' or 'repeated'.

所以,默认情况下返回第一个填充值。此外,参数action也可以是一个字符串:longbroadrepeated。如果输入long,它将以长格式给出数据。如果您想要原始缺失数据,还可以设置include=TRUE

感谢您的回复。非常抱歉之前表述不够清晰,我想要针对所有m个填补数据的方式进行求值,因为我不确定哪一个填补数据的方式是最佳的。有没有一种方法可以提取平均值或其他什么方法呢? - user4624133
1
如我在答案中提到的,如果您键入complete(imp,action="long"),它将给出所有插补的输入值。 - Metrics
@user4624133 缺失值被存储在imp$imp对象中。你可以对它们取平均值,但那会破坏多重插补的整体概念。如果你想要一个单一的插补数据集,则设置m = 1(不推荐),但比追求“最佳值”更好,因为最佳值并不存在。 - Stef van Buuren

0

好的,但是你仍然需要选择一个输入数据集进行进一步分析...我认为最好的选择是使用你的complete(imp,action="long")进行分析,然后汇总结果。fit <- with(data=imp,exp=lm(bmi~hyp+chl)) pool(fit)

但我也认为只使用其中一个输入数据集并不是被禁止的;)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接