查找调查中分类数据的比例

时间:2016-11-07 09:28:32

标签: r survey

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

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

1 个答案:

答案 0 :(得分:4)

&#c;'和svyciprop的价值有不同的形式。

> 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

编辑:欣赏安东尼的认可,因为他对这个套餐的经验远远超过我。&#34; me&#34;方法给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