Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
  2. strata = c(6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8)
  3. weight = c(60, 75, 85, 140, 170, 175, 270, 310, 325, 785, 1450, 3920)
  4. fpc = c(8, 8, 8, 7, 7, 7, 6, 6, 6, 6, 6, 6)
  5. answer = c("2", "2", "3", "1", "2", NA, NA, 2, "3", NA, "1", NA)
  6. df = data.frame(id, strata, weight, fpc, answer)
  7. df <- df[complete.cases(df), ]
  8.  
  9. dstrat<-svydesign(id=~1,strata=~strata, weights=~weight, data=df, fpc=~fpc)
  10. svymean(~answer, dstrat)
  11.  
  12. mean SE
  13. answer1 0.60803 0.2573
  14. answer2 0.23518 0.1755
  15. answer3 0.15679 0.1479
  16.  
  17. answer1 = good
  18. answer2 = neutral
  19. answer3 = bad
  20.  
  21. library(survey)
  22. id <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
  23. strata <- c(6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 8, 8)
  24. weight <- c(60, 75, 85, 140, 170, 175, 270, 310, 325, 785, 1450, 3920)
  25. fpc <- c(8, 8, 8, 7, 7, 7, 6, 6, 6, 6, 6, 6)
  26. answer <- c("2", "2", "3", "1", "2", NA, NA, 2, "3", NA, "1", NA)
  27. df <- data.frame(id=id, strata=strata, weight=weight, fpc=fpc, answer=answer)
  28.  
  29.  
  30. # this is probably a mistake
  31. df <- df[complete.cases(df), ]
  32. # in most data sets, you should be using na.rm=TRUE later
  33. # and not tossing out statements before the `svydesign` gets run
  34.  
  35. df$ones <- as.numeric( df$answer %in% 1 )
  36.  
  37. df$threes <- as.numeric( df$answer %in% 3 )
  38.  
  39. dstrat<-svydesign(id=~1,strata=~strata, weights=~weight, data=df, fpc=~fpc)
  40.  
  41. a <- svymean( ~ ones + threes , dstrat , na.rm = TRUE )
  42.  
  43. svycontrast(a, list(avg=c(0,0), diff=c(1,-1)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement