1

这是我之前帖子的延续

使用估算数据集的 svydesign 错误

我想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()函数中的输入数据集是多重插补类型?

4

1 回答 1

1
# 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 ) ) )
于 2017-03-07T09:40:03.867 回答