Survey weights

2016/06/24

Survey weights

### Survey computing your own post stratification weighhts in R ###
### https://www.r-bloggers.com/survey-computing-your-own-post-stratification-weights-in-r/
# check also: http://tophcito.blogspot.fi/2014/04/social-science-goes-r-weighted-survey.html

library(survey)

# Data
load(url("http://knutur.at/wsmt/R/RData/small.RData"))

str(small)


#  Then we create the unweighted survey object:
small.svy.unweighted <- svydesign(ids=~1, data=small)

# Population data frames, margins
sex.dist <- data.frame(sex = c("M", "F"),
                       Freq = nrow(small) * c(0.45, 0.55))
edu.dist <- data.frame(edu = c("Lo", "Mid", "Hi"),
                       Freq = nrow(small) * c(0.30, 0.50, 0.20))

# Compute the weights by putting together the marginal distributions from before with our data
small.svy.rake <- rake(design = small.svy.unweighted,
                       sample.margins = list(~sex, ~edu),
                       population.margins = list(sex.dist, edu.dist))
# How are the weights?
summary(weights(small.svy.rake))

# Trimming
# It works by setting weights below and above the limits to the limits’ values. 
# The weight that is now “missing”, is divided equally among the cases that were not affected by the trimming operation. 
# This can sometimes push cases above the limit. 
# To prevent this, specify the option strict=TRUE.
small.svy.rake.trim <- trimWeights(small.svy.rake, lower=0.3, upper=3, strict=TRUE)