简体   繁体   English

如何使用估算的 MICE 数据创建新变量

[英]How to create a new variable with the imputed MICE data

I have imputed my data with mice, but now I need to create an indexes with the imputed variables.我已经用老鼠估算了我的数据,但现在我需要用估算的变量创建一个索引。 I want this new variable to be present in all of my iterations of the imputed data in order to pool the regression results afterwards.我希望这个新变量出现在我对估算数据的所有迭代中,以便之后汇集回归结果。 I'll be using the dataset (nhanes) included with mice to explain my problem我将使用鼠标附带的数据集 (nhanes) 来解释我的问题

library(mice)
data_imp <- mice(nhanes, 
            m = 5, 
            maxit = 10, 
            seed = 23109)

This data set has 4 variables (age, bmi, hyp, chl).该数据集有 4 个变量(age、bmi、hyp、chl)。 Imagine I would like to add to my imputed list for all 5 models, a new variable with the mean of bmi and chl which would be bmi_chl.想象一下,我想在我的所有 5 个模型的估算列表中添加一个新变量,其平均值为 bmi 和 chl,即 bmi_chl。

I want to use a for loop, so it does the operation for all 5 of the models.我想使用 for 循环,所以它对所有 5 个模型执行操作。


for (i in 1:length(data_imp)) {data_imp$imp[[i]]$bmi_chl <-
    rowMeans(data_imp$imp[[i]][,c("bmi","chl")], na.rm = T)
}

First the code doesn't work.首先代码不起作用。 But I've noticed another problem in the way the data is stored.但我注意到数据存储方式的另一个问题。 There is the original data in one item, and the imputed lists in another.一项中包含原始数据,另一项中包含估算列表。 However, the imputed list does not have the original data, only the imputed data entries... How could I manage to get my new variable in all 5 imputed models with all observations?但是,推算列表没有原始数据,只有推算数据条目......我如何设法在所有 5 个具有所有观察值的推算模型中获得我的新变量?

Once you have extracted the imputed data frames, you can use lapply to add a new column to the rest of the data:提取估算数据帧后,您可以使用lapply向数据的 rest 添加新列:

library(mice)
data_imp <- mice(nhanes, 
            m = 5, 
            maxit = 10, 
            seed = 23109)
imputed.dfs <- complete(data_imp, "all")
imputed.dfs <- lapply(imputed.dfs, function(x) data.frame(x, bmi_chl=rowMeans(x[,c("bmi", "chl")])))

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

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