我並不確切地知道你想要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
所以這個比例是'投資於補充養老金/總婦女人數女人數量,對吧?對於已婚人士也是如此。你目前有什麼代碼? – blacksite
Right @not_a_robot。我使用了** svytable(〜woman + obs,d.s)**,其中obs是觀察總數(我創建了一個從1到最後一個數字序列的變量obs);我還用** svymean(〜女,d.s)**和** svyratio(〜donna,〜obs,d.s)**,但我沒有得到我所需要的。 –