查找调查中分类数据的比例
Finding proportions for categorical data in a survey
我对尝试使用 R 分析调查数据还很陌生。我有一个问题,我认为应该很容易,但尽管进行了很多 google 搜索,我还是无法弄清楚。
基本上我正在尝试从 STATA 复制 svy: proportion
命令,但我没有看到优雅地完成它的好方法。我希望能够在加权调查中得出分类组所有级别的估计比例和置信区间。因此,例如,如果潜在答案是 1、2、3、4;我希望能够获得每个答案的比例和 CI。我知道你可以用 svyciproportion
做到这一点,但你必须通过并指定每个级别,有没有更优雅的方法来做到这一点?
'ci' 和 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
编辑:感谢 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
我对尝试使用 R 分析调查数据还很陌生。我有一个问题,我认为应该很容易,但尽管进行了很多 google 搜索,我还是无法弄清楚。
基本上我正在尝试从 STATA 复制 svy: proportion
命令,但我没有看到优雅地完成它的好方法。我希望能够在加权调查中得出分类组所有级别的估计比例和置信区间。因此,例如,如果潜在答案是 1、2、3、4;我希望能够获得每个答案的比例和 CI。我知道你可以用 svyciproportion
做到这一点,但你必须通过并指定每个级别,有没有更优雅的方法来做到这一点?
'ci' 和 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
编辑:感谢 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