Search code examples
rdataframesurveyimputation

svydesign in R survey package won't accept imputationList


I'm trying to analyze complex survey data with Survey. I imputed missing data with mice and, following the instructions in the documentation, have converted the imputations to an imputationList object with imputationList()in mitools. But when I try to use that object as data in svydesign(), I get this error message:

Error in as.data.frame.default(yrbs_complex_imputationList) : 
  cannot coerce class ‘"imputationList"’ to a data.frame

Following an example provided elsewhere in StackOverflow, I tried to incorporate the mitools function directly into the svydesign formula:

yrbs_svyimputationList<-svydesign(ids="psu", probs = NULL, strata = "stratum", variables = NULL, fpc = NULL, data=imputationList(yrbs_complex_imputations), nest = TRUE, check.strata = !nest, weights, pps=FALSE)

But this resulted in a different error message:

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘"function"’ to a data.frame

How can I incorporate the multiple-imputed data into a survey design object?


Solution

  • Here's the example from the documentation

    > library(mitools)
    > data.dir<-system.file("dta",package="mitools")
    > files.men<-list.files(data.dir,pattern="m.\\.dta$",full=TRUE)
    > men<-imputationList(lapply(files.men, foreign::read.dta,
    +   warn.missing.labels=FALSE))
    > files.women<-list.files(data.dir,pattern="f.\\.dta$",full=TRUE)
    > women<-imputationList(lapply(files.women, foreign::read.dta,
    +   warn.missing.labels=FALSE))
    > men<-update(men, sex=1)
    > women<-update(women,sex=0)
    > all<-rbind(men,women)
    > 
    > designs<-svydesign(id=~id, strata=~sex, data=all)
    > designs
    Multiple (5) imputations: svydesign(id = ~id, strata = ~sex, data = all)
    

    The big difference is that you need to use ~ rather than quotation marks to quote variables, just as in regression models. Nowadays this might get implemented using the non-standard evaluation from the tidyverse, but the survey package is considerably older than the tidyverse.