Submind YouTube summaries
Thumbnail for R Challenge: Change it! Transform a Continuous Variable into Categories!

R Challenge: Change it! Transform a Continuous Variable into Categories!

Watch on YouTube

Video summary

The video addresses the common challenge faced by analysts who need to perform categorical data analysis but only possess continuous variables, such as systolic blood pressure readings from the NHANES dataset. The presenter begins by importing this real-world data and examining its distribution through a histogram and quantile calculations to establish a baseline understanding of the variable's range and central tendency. This initial step is crucial because it allows analysts to visualize where the median falls—in this specific case, at 120 mmHg—before deciding how best to group the continuous values into meaningful categories for further statistical investigation. To transform the continuous blood pressure data into a categorical format, the presenter demonstrates an empirical classification strategy rather than relying on complex data-driven methods. The process involves creating a new variable that assigns specific codes based on defined thresholds: normal readings below 120 mmHg are coded as one, elevated pressures between 120 and less than 130 mmHg receive code two, and hypertensive cases at or above 130 mmHg get code three. To ensure the recoding was executed correctly without errors, a cross-tabulation frequency table is generated to compare the newly created categorical variable against the original continuous data, visually confirming that values switch categories exactly where the specified cut-points are reached. The tutorial concludes with an advanced tip on how to calculate custom percentiles using R's quantile function by adding a specific probability vector option. While the default setting displays quartiles at 0.25 and 0.75, users can easily generate tertiles or quintiles by inputting a numeric sequence of values ranging from zero to one that corresponds to their desired percentile breaks. The presenter illustrates this with an example for tertiles using probabilities of approximately 0.333 and 0.666, encouraging viewers to experiment further with different interval sizes like quintiles or deciles to suit their specific analytical needs before wrapping up with a call to action for additional courses and resources.
Read the full video transcript
hey there if you want to do a categorical data analysis but you have a continuous variable on your hands what do you do let's classify it if you are learning R use this video to challenge yourself first we'll look at the distribution of a continuous variable systolic blood pressure from a real world data set then we will create a categorical variable to classify it and then if you stick around until the end of the video I'll show you a trick to get R to calculate any percentile of a continuous variable you want 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 going to demonstrate real world data set called anhs using are guey if you want to learn more about enhs read my blog post about enhs the link is in the description as you'll learn 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 one of the examination data sets we'll be using the data set called blood pressure oscillometric measurement as you can see you can read the doc 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 for this demonstration we are going to use the continuous variable bpx o y1 which refers to the first systolic blood pressure reading they do in the examination because they take the blood pressure more than once this is the continuous variable we are going to classify okay here we are in our GUI 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 r104 07 make catv as I showed you before we started with the enhan blood pressure data set which is called pbpx o. xpt I imported that into our GUI and named the data frame B L psor A okay let's start by examining the distribution of this continuous variable first I want to look at the distribution of our continuous variable BP y1 so you can see I use the hist command to make a histogram I'll run this okay here's the histogram of systolic blood pressure can you guess where the median is from the distribution well you don't have to guess because next I'll show you the quanti command next we have the quantal command I had to add the na. RM equals true option to have R suppress the empty variables let's run this and look at the console we see the core tiles in the console what was your guess for the median well the answer is the 50th percentile which is 117 looking at the distribution of a continuous variable before you classify it is a good idea which is why I ran both the histogram and the cord tiles hey if 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 LinkedIn but for Simplicity today we are not going to make datadriven classifications we will use empirical classifications here are the classifications we are going to make normal elevated and hypertensive and here is my code to make the categorical variable I named the variable BL lpsg RP see how in the first line I set the variable to nine for unknown then I update that value to either one for normal two for elevated or three for hypertensive see how I set the criteria based on the original variable and brackets on the left side of the arrow then to check my work I follow it up with a two-way frequency between the new categorical variable and the original continuous variable Yes you heard me a two-way frequency on a continuous variable let me run this and show you how that works out okay let's scroll up to the top see our column headings that's our new grouping variable and on the left we have our continuous variable we are classifying our first classification was normal which is below 120 let's see if we recoded it right okay see what happens at 120 it switches to the second column and then for elevated which starts at 130 it switches to the next column so we know we did our recode right what strategies do you use to make sure you recoded a variable correctly and it doesn't have any errors put your advice 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's here's the special trick I promised I'd show you and that's how to get any quantile you want remember the quantile command defaulted to showing us quartiles but what if we wanted quintiles or desiles we can add an option so let's say instead of quantiles I want tertiles I can add the probs option see how I included it here I put probs equals and then a numeric Vector of values that go from 0 to one and take a stop at each of the percentiles I want for tertiles that's 0.333 and 0.666 let's run this and look at them and there are our tertiles in the console fantastic now challenge yourself try it with quintiles or desiles want to continue your data science Journey with me and experien 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 wench do your data