This is an example of an R Markdown Notebook, which can be edited in the R Studio software.

We are going to use the Russia data set from a recent round of the European Social Survey (ESS) to perform some hypothetical analyses, demonstrating alternative approaches that analysts can take when analyzing designed data.

First, let’s read the ESS-Russia .csv data set, available online, into an R object (russia):

russia <- read.csv('http://umich.edu/~bwest/ess_russia.csv', h=T)

The h=T argument means that variable names are in the first row of the .csv file.

Now, let’s load the contributed survey package in R, which enables appropriate analyses of designed survey data with weights and other sampling features, and create a new design object (russia.dsgn) containing the relevant sample design features of the ESS-Russia data (where the ids argument indicates the variable containing sampling cluster codes, or PSU; the strata argument indicates the variable containing sampling stratum codes, or STRATIFY; and the weights argument indicates the variable containing the survey weights, or PSPWGHT):

require(survey)
## Loading required package: survey
## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
## 
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
## 
##     dotchart
# set ALL design variables by creating a design object
russia.dsgn <- svydesign(ids = ~psu, strata = ~stratify, data = russia, nest = TRUE, weights = ~pspwght)

Next, let’s estimate the proportion of the Russian population that voted in the last election and its standard error, referring to the previous design object:

# estimate the proportion of the population that voted in the last election and its SE
svymean(~factor(voted_lastelection), russia.dsgn, na.rm=T)
##                                mean    SE
## factor(voted_lastelection)0 0.37078 0.017
## factor(voted_lastelection)1 0.62922 0.017

The estimate is 62.9%, with an estimated standard error of 1.7.

Now, let’s take a more naive approach, ignoring the sampling stratum codes (which generally help to make estimates more efficient, or reduce SEs) and cluster sampling codes (which reflect cluster sampling and tend to increase SEs), and only specifying the weights in the design object before estimating the proportion:

# set ONLY the survey weights, and ignore the other design codes
russia.dsgn.w <- svydesign(data = russia, ids = ~1, weights = ~pspwght)
svymean(~factor(voted_lastelection), russia.dsgn.w, na.rm=T)
##                                mean     SE
## factor(voted_lastelection)0 0.37078 0.0114
## factor(voted_lastelection)1 0.62922 0.0114

Note that the weighted estimate is the same, but the standard error has dropped to 1.1 (we are ignoring key features of the sample design that affect the uncertainty associated with our estimate!).

Finally, let’s see what happens when we completely ignore all of the design features:

# ignore all design features
russia.dsgn.u <- svydesign(data = russia, ids = ~1, weights = ~1)
svymean(~factor(voted_lastelection), russia.dsgn.u, na.rm=T)
##                                mean     SE
## factor(voted_lastelection)0 0.34984 0.0096
## factor(voted_lastelection)1 0.65016 0.0096

In the results above, we see that a failure to use the weights increases the estimate by more than two percentage points, and a failure to account for the sampling features makes that SE lower than it actually should be (overstating the precision of the estimates)! The use of weights in particular ensures that the estimate is unbiased with respect to the sample design that was used.

Note that this is a mild example; we saw in the case of the NCSES data from lecture that estimates can shift by 10 to 20 percentage points when weights are informative but ignored in the analysis. Feel free to try this out with some other categorical variables in the ESS-Russia data!