Error with svydesign using imputed data sets

2019-06-14 02:21发布

问题:

I am analyzing an imputed dataset using svydesign but I am getting an error. Below is the code:

library(mitools)

library(survey)

data(nhanes)

nhanes$hyp <- as.factor(nhanes$hyp)

imp <- mice(nhanes,method=c("polyreg","pmm","logreg","pmm"), seed = 23109)

des<-svydesign(id=~1, strat=~age, data=imputationList(imp))


Error in as.data.frame.default(data, optional = TRUE) : cannot coerce class ""call"" to a data.frame

I am following the tutorial from this page: http://r-survey.r-forge.r-project.org/survey/svymi.html

how do i modify the code for it to work?

EDIT:

I change data=imputationList(imp) to data=complete(imp,1) and i was able to make the code work. However, this is not efficient since I have to do this to all my imputed sets. Is there something worng with using imputationList?

回答1:

mice() produces the results and the imputationList requires a list of all five data.frame with the imputed values, but you need to use mice::complete to construct those five completed data.frame objects

library(mitools)
library(survey)
library(mice)
data(nhanes)
nhanes$hyp <- as.factor(nhanes$hyp)
imp <- mice(nhanes,method=c("polyreg","pmm","logreg","pmm"), seed = 23109)
imp_list <- lapply( 1:5 , function( n ) complete( imp , action = n ) )
des<-svydesign(id=~1, strat=~age, data=imputationList(imp_list))


标签: r survey r-mice