Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. year id y.b sex income married pens weight
  2. 2002 1 1950 F 100000 1 0 1.12
  3. 2002 2 1943 M 55000 1 1 0.55
  4. 2004 1 1950 F 88000 1 1 1.1
  5. 2004 2 1943 M 66000 1 1 0.6
  6. 2006 3 1966 M 12000 0 1 0.23
  7. 2008 3 1966 M 24000 0 1 0.23
  8. 2008 4 1972 F 33000 1 0 0.66
  9. 2010 4 1972 F 35000 1 0 0.67
  10.  
  11. d.s <- svydesign(ids=~1, data=df, weights=~weight)
  12.  
  13. # same setup
  14. library(survey)
  15.  
  16. df <- data.frame(sex = c('F', 'M', 'F', 'M', 'M', 'M', 'F', 'F'),
  17. married = c(1,1,1,1,0,0,1,1),
  18. pens = c(0, 1, 1, 1, 1, 1, 0, 0),
  19. weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))
  20.  
  21. d.s <- svydesign(ids=~1, data=df, weights=~weight)
  22.  
  23. # subset to women only then calculate the share with a pension
  24. svymean( ~ pens , subset( d.s , sex == 'F' ) )
  25.  
  26. df <- data.frame(sex = c('F', 'M', 'F', 'M', 'M', 'M', 'F', 'F'),
  27. married = c(1,1,1,1,0,0,1,1),
  28. pens = c(0, 1, 1, 1, 1, 1, 0, 0),
  29. weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))
  30.  
  31. d.s <- svydesign(ids=~1, data=df, weights=~weight)
  32.  
  33. # data frame of women with a pension
  34. women_with_pension <- d.s$variables %>%
  35. filter(sex == 'F' & pens == 1)
  36.  
  37. # number of rows (i.e. number of women with a pension) in that df
  38. n_women_with_pension <- nrow(women_with_pension)
  39.  
  40. # data frame of all women
  41. all_women <- d.s$variables %>%
  42. filter(sex == 'F')
  43.  
  44. # number of rows (i.e. number of women) in that df
  45. n_women <- nrow(all_women)
  46.  
  47. # divide the number of women with a pension by the total number of women
  48. proportion_women_with_pension <- n_women_with_pension/n_women
  49.  
  50. # data frame of women with a pension
  51. women_with_pension <- d.s$variables %>%
  52. filter(sex == 'F' & pens == 1) %>%
  53. summarise(total_weight = sum(weight))
  54.  
  55. # number of rows (i.e. number of women with a pension) in that df
  56. women_with_pension_weight = women_with_pension[[1]]
  57.  
  58. # data frame of all women
  59. all_women <- d.s$variables %>%
  60. filter(sex == 'F') %>%
  61. summarise(total_weight = sum(weight))
  62.  
  63. # number of rows (i.e. number of women) in that df
  64. all_women_weight <- all_women[[1]]
  65.  
  66. # divide the number of women with a pension by the total number of women
  67. # 0.3098592 for this sample data
  68. prop_weight_women_with_pension <- women_with_pension_weight/all_women_weight
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement