Raking 多重推算数据集

Raking Multiple Imputed dataset

这是我之前 post

的延续

我想 运行 在我的估算数据集中使用 rake() 函数。但是,它似乎没有找到输入变量。下面是一个示例代码:

library(mitools)
library(survey)
library(mice)

data(nhanes)

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

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

imp_list <- lapply( 1:5 , function( n ) complete( imp , action = n ) )


des<-svydesign(id=~1, data=imputationList(imp_list))


age.dist <- data.frame(age =  c("20-39","40-59", "60-99"),
                   Freq = nrow(des) * c(0.5, 0.3, .2))


small.svy.rake <- rake(design = des, 
                   sample.margins = list(~age),
                   population.margins = list(age.dist))

Error in eval(expr, envir, enclos) : object 'age' not found

如果我将输入数据更改为单个数据集,代码就可以工作。也就是说,我有这个

而不是 des<-svydesign(id=~1, data=imputationList(imp_list))
data3 <- complete(imp,1)

des<-svydesign(id=~1, data=data3)

我如何编辑代码,使其能够识别 rake() 函数中的输入数据集是多重插补类型?

# copy over the structure of your starting multiply-imputed design
small.svy.rake <- des

# loop through each of the implicates
# applying the `rake` function to each
small.svy.rake$designs <- 
    lapply( 
        des$designs , 
        rake ,
        sample.margins = list(~age),
        population.margins = list(age.dist)
    )

# as you'd expect, the overall number changes..
MIcombine( with( des , svymean( ~ bmi ) ) )
MIcombine( with( small.svy.rake , svymean( ~ bmi ) ) )

# ..but the within-age-category numbers do not
MIcombine( with( des , svyby( ~ bmi , ~ age , svymean ) ) )
MIcombine( with( small.svy.rake , svyby( ~ bmi , ~ age , svymean ) ) )