繁体   English   中英

如何将多重插补数据与小鼠相结合?

[英]How to combine multiply imputed data with mice?

我将一个数据集分为男性和女性,然后使用mice包分别对其进行插补。

#Generate predictormatrix
pred_gender_0<-quickpred(data_gender_0, include=c("age","weight_trunc"),exclude=c("ID","X","gender"),mincor = 0.1)
pred_gender_1<-quickpred(data_gender_1, include=c("age","weight_trunc"),exclude=c("ID","X","gender"),mincor = 0.1)

#impute the data with mice 
imp_pred_gen0 <- mice(data_gender_0,
                 pred=pred_gender_0,
                 m=10,
                 maxit=5,            
                 diagnostics=TRUE,
                 MaxNWts=3000) #i had to set this to 3000 because of an problematic unordered categorical variable 

imp_pred_gen1 <- mice(data_gender_1,
                 pred=pred_gender_1,
                 m=10,
                 maxit=5,            
                 diagnostics=TRUE,
                 MaxNWts=3000)

现在,我有两个对象,其中包含 10 个插补数据集。 一种给男人,一种给女人。 我的问题是,如何将它们结合起来? 通常,我只会使用:

comp_imp<-完整(imp,“长”)

我是不是该:

  1. 使用rbind.mids()合并男女数据,然后将其转换为长格式?
  2. 我首先转换为长格式,然后使用rbind.mids()rbind()吗?

感谢您的任何提示! =)

-------------------------------------------------- -------------------------

更新 - 可复制的例子

library("dplyr")
library("mice")

# We use nhanes-dataset from the mice-package as example

# first: combine age-category 2 and 3 to get two groups (as example)
nhanes$age[nhanes$age == 3] <- "2"
nhanes$age<-as.numeric(nhanes$age)
nhanes$hyp<-as.factor(nhanes$hyp)

#split data into two groups
nhanes_age_1<-nhanes %>% filter(age==1)
nhanes_age_2<-nhanes %>% filter(age==2)

#generate predictormatrix
pred1<-quickpred(nhanes_age_1, mincor=0.1, inc=c('age','bmi'), exc='chl')
pred2<-quickpred(nhanes_age_2, mincor=0.1, inc=c('age','bmi'), exc='chl')

# seperately impute data
set.seed(121012)
imp_gen1 <- mice(nhanes_age_1,
                 pred=pred1,
                 m=10,
                 maxit=5,            
                 diagnostics=TRUE,
                 MaxNWts=3000)

imp_gen2 <- mice(nhanes_age_2,
                 pred=pred2,
                 m=10,
                 maxit=5,            
                 diagnostics=TRUE,
                 MaxNWts=3000)


#------ ALTERNATIVE 1:

#combine imputed data
combined_imp<-rbind.mids(imp_gen1,imp_gen2)
complete_imp<-complete(combined_imp,"long")

#output
   > combined_imp<-rbind.mids(imp_gen1,imp_gen2)
Warning messages:
1: In rbind.mids(imp_gen1, imp_gen2) :
  Predictormatrix is not equal in x and y; y$predictorMatrix is ignored
.
2: In x$visitSequence == y$visitSequence :
  longer object length is not a multiple of shorter object length
3: In rbind.mids(imp_gen1, imp_gen2) :
  Visitsequence is not equal in x and y; y$visitSequence is ignored
.
   
> complete_imp<-complete(combined_imp,"long")
Error in inherits(x, "mids") : object 'combined_imp' not found


#------ ALTERNATIVE 2:

complete_imp1<-complete(imp_gen1,"long")
complete_imp2<-complete(imp_gen2,"long")
combined_imp<-rbind.mids(complete_imp1,complete_imp2)

#Output
> complete_imp1<-complete(imp_gen1,"long")
> complete_imp2<-complete(imp_gen2,"long")
> combined_imp<-rbind.mids(complete_imp1,complete_imp2)
Error in if (ncol(y) != ncol(x$data)) stop("The two datasets do not have the same number of columns\n") : 
  argument is of length zero

您可以使用以下命令创建一个新的 mids 对象,其中包含 10 个男性和女性的插补数据集。

comp_imp <- rbind(pred_gender_0, pred_gender_1)

这样做会调用 rbind.mids,而不是 R 中的常规绑定函数。可以以通常的方式分析返回的新对象,例如使用 with.mids 将您想要的模型拟合到每个插补数据集。

老实说,我对包mice一无所知,对插补的概念只有一个模糊的概念。

我不知道你想进行什么样的分析,但你说通常你会这样做: comp_imp<-complete(imp,"long") ,所以我会尝试相应地回答。

对我来说,第一种方法返回一个 data.frame,但没有任何遗漏。 这很奇怪,因为在complete(imp_gen1,"long")hyp缺少数据。 我不知道rbind.mids在那里做什么。

因此,我会采用您的第二种方法。

complete(., "long")是一个普通的 data.frame,因此不需要将其与rbind.mids绑定。

我会将您的第二种方法更改为:

library(dplyr)
complete_imp1 <- complete(imp_gen1, "long")
complete_imp2 <- complete(imp_gen2, "long")
combined_imp <- bind_rows(complete_imp1, complete_imp2)

complete_imp1 <- complete(imp_gen1, "long")已经返回了 10 个( m参数)插补数据帧,只需计算complete_imp1的总行数并乘以m

暂无
暂无

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

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