Submind YouTube summaries
Thumbnail for R Challenge: Confused when applying population-based weights? I show you how!

R Challenge: Confused when applying population-based weights? I show you how!

Watch on YouTube

Video summary

This video tutorial by Monica from Death Wench Professional Services guides viewers through the process of applying population-based weights in R using real-world surveillance data. The primary goal is to demonstrate how to construct a binary flag variable, specifically identifying women within a dataset called "enhanced," and then integrating this into a survey design to calculate accurate weighted percentages. Monica begins by explaining that while many people discuss adding weight variables, the practical application often requires specific steps involving the `survey` package in R to ensure analyses reflect true population proportions rather than just raw sample counts. The technical demonstration starts with creating necessary variables within an R script imported from a GitHub repository. A crucial step involves generating a binary variable named "woman" that assigns a value of one to female respondents and zero otherwise, which serves as the numerator for calculating weighted proportions. Following this data preparation, Monica constructs a basic survey design object using the `svydesign` function, setting options like primary sampling units (PSUs) correctly before modifying the design with an update command. This modification converts the binary flag into a labeled factor variable to ensure clear interpretation of results during analysis. Once the design is finalized and variables are properly formatted, the video shows how to calculate weighted proportions using the `svy_mean` function, which reveals that women represent approximately 51% of the population in this specific dataset. The tutorial also covers calculating total weighted counts by utilizing a helper variable filled with ones for all rows; this allows users to determine the estimated number of individuals or subgroups within the broader population represented by the sample. By using `svy_total` and `svy_by`, viewers learn how to derive that while the dataset represents roughly 320 million people overall, it specifically estimates a population of about 164 million women when weights are applied correctly. In conclusion, the video emphasizes that weighted analysis involves several distinct steps but is essential for public health data science where samples may not perfectly mirror the actual demographics. Monica encourages viewers to engage with her content by liking and subscribing, while also offering resources such as a blog post about the enhanced dataset documentation and links to online courses on application basics. The tutorial serves both beginners learning R syntax and experienced analysts looking to refine their understanding of how to properly implement complex weighting strategies using standard packages like `survey`.
Read the full video transcript
everyone talks about adding weight variables to a populationbased analysis but how do you actually do that if you are learning R use this video to challenge yourself we'll start by creating a binary flag so we can tell what goes in the numerator and what goes in the denominator next we'll construct a design using the survey package after that I'll show you how to modify the design so it accommodates the binary flag we made and that is how we will get our weighted percentages and if you stick around to the end of the video I'll show you how to calculate weighted counts hi I'm Monica of death wench Professional Services and I teach Public Health Data science if you are new to the channel welcome and if you are returning thank you for coming back I try to post a new video every Friday at 10:00 a.m. so don't forget to be a regular visitor so you can be the first to check out my new video every week today I'm I'm going to demonstrate how to apply weights from a real world data set called enhan using our GUI if you want to learn more about enhan read my blog post about enhan the link is in the description as you'll see in my blog post the enhan data and documentation come from this online portal the enhan is one surveillance effort but for whatever reason they split up the data into different data sets for this demonstration we will be using the demographics data I'll click on it we only have one choice of data set as you can see you can read the documentation under the doc file heading and download the data from clicking under the data file heading let's click on the documentation here we get to the data dictionary see this variable Ria gender that is the variable we are going to use to create our binary flag so we can see the proportion of women in the data set let's go okay here we are in argui if you want this code I'm using just click on the link in the video description to go to my GitHub folder make sure you download the code R1 0607 uncore apply weights as I showed you before we are going to use the demographic data set because that is the data set that has the weight variables let's look at the top of this code I started with some commands which are kind of a cheat sheet for the variables we are going to use next I imported the native data set pcore demo into the data frame dgor a and so that's where we will start first we are going to make a few variables okay so actually we start with a clu are you familiar with the word clu it means a sketchy workaround that's what this one line of code is it creates a variable in our data set named one spelled o NE and it has a one in it every row has a one in it it's to make it easy to use the survey package for counting stick around to the end of the video and you'll see where we use it next we are going to make our binary variable the one we will use in a weighted percentage to be easy I decided to just identify the women so as you can see here we make a binary variable called woman the first line of code populates all rows with a zero then the next line of code updates it to a one if render equals 2 which means woman and then we run a two-way frequency to make sure we constructed the variable properly let's run all this code and take a look at our woman variable here is our output and yes it looks like we recorded our woman variable properly that's step one now let's go back and construct our survey design okay let's take this in a few steps first let's call up the survey library and set up the design we start by calling up the survey package next I'm setting this option which is the survey. only. PSU option I'm setting it to adjust this helps correct for small primary sampling units or psus in the native data now the code under it creates an object called enhan unor design to do that we use the svy design command then see how we set all these options in that command the ID strata and weight commands relate to those variables I put up in the comment the data set we are working with is obviously dgor a and we set set Nest to True let's run this basic design code to set up the design okay great we finished the next step constructing the survey design hey if you ever want advice about public health or data science schedule a free 30- minute Zoom appointment with me using the link in the description and don't forget to follow me and the death wench Professional Services Company page on Linked In all right now that we made our woman flag and we constructed our enhan design we now need to modify our enhance design to add our woman variable to it that's what we are doing here we use an update command and then we do something interesting we actually create a factor variable on the Fly which I called woman uncore F we say woman uncore f equals a factor variable which is our woman flag now notice that since we are creating a factor variable we can use the levels and the labels option to label the levels you'll see I set the levels as 0o and one and the labels as notom and woman this is the last transformation step we need to do before the analysis so let's run the code okay great it ran now finally we get to calculate our percentages actually we are calculating weighted proportions and this is how we are doing it we are using the svy mean command you'll see we list the arguments in this order first we put a Tilda and then the name of the factor variable we just created then we add the name of the design object which is nesor design and then we add the na. RM equals true option to tell R to remove any na meaning any empty rows okay ready to see the weighted percentages let's run this svy mean code all right there we go this data set is overweighted for women see the weighted proportion for women is 0.511 to1 so basically 51% see how much work this is just to get a weighted proportion these analyses have so many steps in them have you ever done a weighted analysis if so how did it go did you use r with the survey package or some other tool tell us all in the comments if you like this content please be sure to hit the like button because then my videos will come up in your feed more often or better yet hit subscribe it's free and it really helps me out thank you okay here is your special bonus I've already showed you how to do the weighted propor now I'll show you how to do those weighted counts and you can see where I use this mysterious one variable okay first we need to count up all the people the data set represents that's where we use this svy total command so what variable are we counting well we are counting that one variable so our arguments are a Tilda followed by the one variable and then our design which is nanor design so let's run this and see how many people are in the the entire data set this is our denominator here it is in the console we can see it about 320 million people because this is supposed to represent the entire us but now how many women does it represent let's look at the next code okay so here is how you get the weighted counts of women we will use the svy by command notice the arguments first we have a Tilda and the one variable then Tilda and our woman F Factor variable and then the nanor design object then svy total let's run this and look at the console and there we go this data set represents about 164 million women how about that want to continue your data science Journey with me an experienced professional at your side take my online course application Basics and learn business application terminology demonstrated through unique case studies so you are empowered to Divine novel insights from analyzing application data better yet register for the workshop click on the link in the description to access the course and workshop and don't forget to follow me on the socials links to all my social media accounts are in the description let's stay connected because you never know where your data science Journey will lead have a data emergency or just need advice remember to let death wiench do your data