R Tutorial: Impact of weights

Learn how survey weights affect statistical analyses using NHANES data in R. The tutorial shows how to adjust two-year weights to four-year weights with dplyr, specify survey design with the survey package, and demonstrates that weights correct for oversampling of minority groups in visualizations and models.
Chapters
Now that we have a handle on common survey design structures, let's look at a real world survey to see how the survey design and the weights impact our analyses, we will explore the National Health and Nutrition Examination Survey. The goal of NHANES is to assess the health of people in the US because the survey includes a medical exam and a mobile health vehicle, the researchers have put a lot of care into developing a cost effective representative sampling design. The data are collected in four stages. First, the US Is stratified by geography and distribution of minority populations. Then, counties are randomly selected within each stratum where more populated counties are more likely to be sampled. From the sampled counties, city blocks are randomly selected. Where again, more populated blocks are more likely to be sampled. From the sampled city blocks, households are randomly selected based on demographic information. And lastly, within the sampled households, people are randomly selected for inclusion in the sample. The 2009-2012 sample data, called NHANES raw, can be found in the NHANES package. Running the DIM command returns the number of rows or observations and the number of columns or variables contained in the data set. We see that the NHANES RAW Data set contains 78 variables on 20,293 people. Before specifying the design, we need to modify the survey weights variable weightmec 2 year. WeightMech 2 year provides the number of people in the US each sampled person represents. Therefore, summing all the weights via the summarize command should provide a rough estimate of of the total number of people in the U.S. however, we get an estimate of 608 million people, about twice as many as we should. That is because these weights were constructed assuming you have two years of data. Since we have four years of data, we need to divide each weight by two. To do that, we use the mutate function to create a new column which WeightMech 4 year, where each value is half the value in WeightMech 2 year. Let's specify the design with the R function surveydesign in the arguments we need to provide the data set NHANESRAW and the strata column SDMV strata. Remember ID is where we specify the variables that represent the clusters. While the design actually had three levels of clusters, counties, city blocks, households, it's common practice to Only specify the first level denoted here by SDMVPSU. Running the distinct function on SDMVPSU, we see it only takes three values 12 3. This is because one to three counties were sampled within in each strata. Therefore, we must include nest equals true because the cluster IDs are nested within the strata. Lastly, the survey weights are given in weight Mech Fourier now for some analyses. Suppose we want to estimate the distribution of race in the us. I created these two plots using the race variable in the NHANES raw data set. In the top graph I counted for the survey weights and in the bottom graph I didn't notice how different the distribution of race is between these two plots. The survey weights account for the sampling design in which minority populations are over sampled. They adjust for non response and are calibrated to known information about the population. In essence if we ignore them we will get a very wrong graph. The moral of the story is that survey weights cannot be ignored and in the rest of this course we will learn to ensure that the graphs, the models, the analyses we conduct properly handle the weights. Alright, it's time to practice with the NHANES weights.
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







