【问题标题】:Finding proportions for categorical data in a survey在调查中查找分类数据的比例
【发布时间】:2017-03-20 14:15:48
【问题描述】:

我对尝试使用 R 分析调查数据还很陌生。我有一个问题,我认为应该很容易,但尽管谷歌搜索很多,但我还是无法弄清楚。

基本上,我正在尝试从 STATA 复制 svy: proportion 命令,但我看不到优雅地执行此操作的好方法。我希望能够在加权调查中为分类组的所有级别吐出估计的比例和置信区间。例如,如果潜在答案是 1、2、3、4;我希望能够获得每个答案的比例和 CI。我知道你可以用svyciproportion 做到这一点,但你必须通过并指定每个级别,有没有更优雅的方法来做到这一点?

【问题讨论】:

    标签: r survey


    【解决方案1】:

    svyciprop 的 'ci' 和 value 有不同的形式。

    > str( svyciprop(~I(stype %in% "E"), dclus1, method="lo", df=degf(dclus1)) )
    Class 'svyciprop'  atomic [1:1] 0.787
      ..- attr(*, "var")= num [1, 1] 0.00215
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : chr "as.numeric(I(stype %in% \"E\"))"
      .. .. ..$ : chr "as.numeric(I(stype %in% \"E\"))"
      ..- attr(*, "ci")= Named num [1:2] 0.671 0.87
      .. ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%"
    

    要以紧凑的形式提供它们,需要从属性中提取“ci”向量并将其附加到级别值。还需要制定一个公式以允许在svyciprop 的第一个参数之外进行替换,这不会进行就地替换。

    library(survey) # using the `dclus1` object that is standard in the examples.
    sapply( levels(dclus1$variables$stype),
            function(x){ 
               form <- as.formula( substitute( ~I(stype %in% x), list(x=x)))
               z <- svyciprop(form, dclus1, method="lo", df=degf(dclus1))
               c( z, c(attr(z,"ci")) )}  )
                              E          H         M
    I(stype %in% "E") 0.7868852 0.07650273 0.1366120
    2.5%              0.6712011 0.03540883 0.0844893
    97.5%             0.8697648 0.15750112 0.2133950
    

    编辑:感谢 Anthony 的认可,因为他对这个软件包的经验比我丰富得多。“me”方法赋予 CI 的值略有不同:

    sapply( levels(dclus1$variables$stype), function(x){ 
         form <- as.formula( substitute( ~I(stype %in% x), list(x=x)))
         z <- svyciprop(form, dclus1, method="me", df=degf(dclus1))
         c( z, c(attr(z,"ci")) )}  )
                              E          H          M
    I(stype %in% "E") 0.7868852 0.07650273 0.13661202
    2.5%              0.6875032 0.01900053 0.07302114
    97.5%             0.8862673 0.13400493 0.20020290
    

    【讨论】:

    • 不错。 ?svyciprop 表示要精确复制 stata 的行为,请使用 method="me"
    • 对于数据丢失的情况,我发现如果不考虑丢失数据,上面的函数不会给出与Stata的me方法相同的结果。当使用一些变量时,这很好,但如果你想循环很多呢?有没有办法适应上面的忽略缺失的观察?
    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多