R Tutorial: Elements of a sampling design

Learn how to code three survey sampling designs in R using Thomas Lumley's survey package: simple random sampling, stratified sampling by county, and two-stage cluster sampling. The tutorial walks through creating svydesign objects with the right arguments for each design, using Pennsylvania social-media data as an example.
Chapters
Now that we understand survey weights, let's learn about some common design structures and how they are specified using the popular survey package. Suppose we want to estimate the average amount of time Pennsylvania residents spend on social media. This means our target population is the residents of Pennsylvania and our study variable is is the weekly hours spent on social media. Suppose we have a list of every Pennsylvania resident and we randomly pick 200 people to survey. These 200 people are denoted by the pink dots. This method is called simple random sampling. Everyone had an equal chance of being selected for our survey. Once we've collected the data and imported it into R, we now need to tell R about our sampling design. To do so, we will use the surveydesign function in Thomas Lumley's wonderful survey package. For simple random sampling, we need to specify the data frame, the column that stores the survey weights, the column that contains the population size, which statisticians call fpc, short for finite population correction, and the column that stores staging information. Since there's only one sampling stage in simple random sampling, we set ID equal to tilde 1. The tildes in front of weights and N imply that these are the names of columns in the data set. Pasample. To recap, the surveydesign function tells R what sampling design generated the data and creates an object that contains the data and important design information. Notice that if we randomly select 200 Pennsylvanians that many of these residents are concentrated around Pennsylvania's two largest cities, Philadelphia and Pittsburgh. Since these places have a higher concentration of of people, if we want to estimate the social media usage by county, we can't since some counties such as Warren and McKean have no observations. To fix this issue, we can group our population into counties and then take a simple random sample in each county. This is called stratified sample simple random sampling and the groups are called strata. This sampling design works well to get a more diverse sample and is useful in computing estimates for subgroups. To specify this sampling design, we need to map the column in the data set that contains the groupings to the strata argument in survey design. For our example, the groups are the counties. The entries of FPC should now reflect the population size of the associated county. Now, this sampling design can be expensive to implement. Imagine you plan to interview each sampled person at their home. In that case, this design will cost a lot of gas money and travel time. To cut down on costs, another commonly used design is a cluster sampling design. The population are grouped into what are called clusters. But now, instead of taking a sample within each cluster, a simple random sample of clusters shown in black are drawn. Within each sampled cluster, a simple random sample of people is selected for. In person surveys, it is very common to cluster by area as it cuts down on travel time. To specify a cluster sampling design, we need to change ID and FPC to reflect the sampling stages. For our example, we first sampled county and then we sampled people within county. So we set ID to county plus person id. For fpc, we provide the column with the number of counties denoted here by N1 plus the column with the number of people denoted here by N2. Let's practice specifying sampling designs.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







