Error with svydesign using imputed data sets

ぃ、小莉子 提交于 2019-12-08 09:56:39

问题


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))


来源:https://stackoverflow.com/questions/42502400/error-with-svydesign-using-imputed-data-sets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!