【问题标题】:Creating imputation list for use with svyglm创建用于 svyglm 的插补列表
【发布时间】:2018-07-07 12:24:39
【问题描述】:

使用survey 包,我在创建imputationList 时遇到问题svydesign 将接受。这是一个可重现的示例:

library(tibble)
library(survey)
library(mitools)


# Data set 1
# Note that I am excluding the "income" variable from the "df"s and creating  
# it separately so that it varies between the data sets. This simulates the 
# variation with multiple imputation. Since I am using the same seed
# (i.e., 123), all the other variables will be the same, the only one that 
# will vary will be "income."

set.seed(123)

df1 <- tibble(id      = seq(1, 100, by = 1),
              gender  = as.factor(rbinom(n = 100, size = 1, prob = 0.50)),
              working = as.factor(rbinom(n = 100, size = 1, prob = 0.40)),
              pweight = sample(50:500, 100,  replace   = TRUE))


# Data set 2

set.seed(123)

df2 <- tibble(id      = seq(1, 100, by = 1),
              gender  = as.factor(rbinom(n = 100, size = 1, prob = 0.50)),
              working = as.factor(rbinom(n = 100, size = 1, prob = 0.40)),
              pweight = sample(50:500, 100,  replace   = TRUE))


# Data set 3

set.seed(123)

df3 <- tibble(id      = seq(1, 100, by = 1),
              gender  = as.factor(rbinom(n = 100, size = 1, prob = 0.50)),
              working = as.factor(rbinom(n = 100, size = 1, prob = 0.40)),
              pweight = sample(50:500, 100,  replace   = TRUE))


 # Create list of imputed data sets

 impList <- imputationList(df1,
                           df2, 
                           df3)


# Apply NHIS weights

weights <- svydesign(id     = ~id, 
                     weight = ~pweight, 
                     data   = impList)

我收到以下错误:

Error in eval(predvars, data, env) : 
  numeric 'envir' arg not of length one

【问题讨论】:

  • 错误来自svydesign。我们不需要查看您是如何获取数据的,尝试创建一个小的reproducible data 会产生相同的错误,可能是dput(head(impList))
  • 是的,错误来自svydesign,但我不知道为什么。我正在关注?imputationList 中的示例,其中imputationList(datasets,...)。通常我会使用小的可重现示例,但这更复杂(例如,估算数据、调查权重),我认为最好使用真实世界的数据,因为很难重现确切的情况。
  • @zx8754 这不是重复的.. 问题特定于library(survey)
  • @AnthonyDamico 我从错误消息中说“可能”,以便 OP 可以探索链接的帖子是否有帮助。

标签: r survey imputation


【解决方案1】:

为了让它工作,我需要直接将imputationList添加到svydesign,如下所示:

weights <- svydesign(id = ~id, 
                         weight = ~pweight, 
                         data = imputationList(list(df1, 
                                                    df2, 
                                                    df3)) 

【讨论】:

    【解决方案2】:

    http://asdfree.com/national-health-interview-survey-nhis.html 上提供的分步说明详细介绍了如何创建多重插补 nhis 设计,下面的分析示例包括 svyglm 调用。避免将library(data.table)library(dplyr)library(survey) 一起使用

    【讨论】:

    • 谢谢@AnthonyDamico。为什么要避免将library(data.table)library(dplyr)library(survey) 一起使用?它们是我的首选数据整理库。
    • 都不使用调查设计对象。 library(srvyr) 允许一些 library(dplyr) 命令
    • 谢谢@AnthonyDamico。我有类似的问题,您可能对此感兴趣stackoverflow.com/questions/48506315/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 2012-06-19
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多