Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- d.s <- svydesign(ids=~1, data=df, weights=~weight)
- # 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' ) )
- 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
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement