【问题标题】:Calculate percentage/frequency of a value in a survey object计算调查对象中值的百分比/频率
【发布时间】:2017-02-15 07:59:19
【问题描述】:

我有一个由许多变量组成的全国性调查,比如这个(为了简洁起见,我省略了一些变量):

year  id  y.b   sex   income  married   pens   weight
2002  1   1950   F    100000     1       0      1.12
2002  2   1943   M    55000      1       1      0.55
2004  1   1950   F    88000      1       1      1.1
2004  2   1943   M    66000      1       1      0.6
2006  3   1966   M    12000      0       1      0.23
2008  3   1966   M    24000      0       1      0.23
2008  4   1972   F    33000      1       0      0.66
2010  4   1972   F    35000      1       0      0.67

其中 id 是受访者,y.b 是出生年份,married 是一个虚拟变量(1 个已婚,0 个单身),pens 是一个虚拟变量,如果该人投资于补充养老金形式,则价值为 1; weight 是调查权重。

假设原始调查由 2002 年至 2014 年的 40k 观察组成(我对其进行了过滤,以便仅包含出现多次的个人)。我使用这个命令来创建一个调查对象:

d.s <- svydesign(ids=~1, data=df, weights=~weight)

既然 df 是加权的,我想找到例如投资补充养老金的女性百分比或已婚人士百分比;我在 R 帮助和网络上阅读以找到获取百分比的命令,但我没有找到正确的。

【问题讨论】:

  • 所以这个百分比是number of women that invest in complementary pension/total number of women,对吧?已婚人士也一样。到目前为止你有什么代码?
  • 正确的@not_a_robot。我使用了 svytable(~woman+obs, d.s),其中 obs 是观察的总数(我创建了一个变量 obs,其数字序列从 1 到结尾);我也使用了 svymean(~woman, d.s)svyratio(~donna, ~obs, d.s) 但我没有得到我需要的东西。

标签: r percentage survey


【解决方案1】:
# same setup
library(survey)

df <- data.frame(sex = c('F', 'M', 'F', 'M', 'M', 'M', 'F', 'F'),
                married = c(1,1,1,1,0,0,1,1),
                pens = c(0, 1, 1, 1, 1, 1, 0, 0),
                weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))

d.s <- svydesign(ids=~1, data=df, weights=~weight)

# subset to women only then calculate the share with a pension
svymean( ~ pens , subset( d.s , sex == 'F' ) )

【讨论】:

  • 这就是为什么我喜欢 SO,多年后帮助我的答案的宝石
  • @Anthony,虽然我有疑问,如果我要在函数中使用它。它无法正常工作,请检查下表 1
【解决方案2】:

我不完全知道你想用weight 做什么,但是对于dplyr 中拥有养老金的女性比例,这是一个非常简单的解决方案:

df <- data.frame(sex = c('F', 'M', 'F', 'M', 'M', 'M', 'F', 'F'),
                married = c(1,1,1,1,0,0,1,1),
                pens = c(0, 1, 1, 1, 1, 1, 0, 0),
                weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))

d.s <- svydesign(ids=~1, data=df, weights=~weight)

# data frame of women with a pension
women_with_pension <- d.s$variables %>%
  filter(sex == 'F' & pens == 1)

# number of rows (i.e. number of women with a pension) in that df
n_women_with_pension <- nrow(women_with_pension)

# data frame of all women
all_women <- d.s$variables %>%
  filter(sex == 'F')

# number of rows (i.e. number of women) in that df
n_women <- nrow(all_women)

# divide the number of women with a pension by the total number of women
proportion_women_with_pension <- n_women_with_pension/n_women

这将为您提供基本比例的领取养老金的女性。应用同样的逻辑来获得拥有养老金的已婚人士的百分比。

weight 变量而言,您是否尝试做某种加权比例?在这种情况下,您可以将每个班级(包括养老金和所有女性)中女性的 weight 值相加,如下所示:

# data frame of women with a pension
women_with_pension <- d.s$variables %>%
  filter(sex == 'F' & pens == 1) %>%
  summarise(total_weight = sum(weight))

# number of rows (i.e. number of women with a pension) in that df
women_with_pension_weight = women_with_pension[[1]]

# data frame of all women
all_women <- d.s$variables %>%
  filter(sex == 'F') %>%
  summarise(total_weight = sum(weight))

# number of rows (i.e. number of women) in that df
all_women_weight <- all_women[[1]]

# divide the number of women with a pension by the total number of women
# 0.3098592 for this sample data
prop_weight_women_with_pension <- women_with_pension_weight/all_women_weight

【讨论】:

  • 谢谢你,你的答案就是我要找的那个。我想使用权重来正确表示样本(因为调查是对样本进行的,使用调查权重应该可以更好地代表整个人口)。
  • @LauraR。我投反对票是因为这种闯入调查对象的策略是荒谬的。并且不允许用户计算置信区间。看我的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
相关资源
最近更新 更多