- 5 hours ago
Learn how to spot statistical outliers using the 1.5× IQR rule, then see why unusual values need context before you decide what to do with them. This practical introduction to statistics explains quartiles, the interquartile range, and how to calculate upper and lower fences for customer lifetime value data.
Follow the example from a box plot to hands-on analysis: identify values beyond the fences, adjust the rule for a dataset, and consider whether an outlier is a valid observation or a data issue. A useful guide to outlier detection, exploratory data analysis, and statistics for data professionals.
Follow the example from a box plot to hands-on analysis: identify values beyond the fences, adjust the rule for a dataset, and consider whether an outlier is a valid observation or a data issue. A useful guide to outlier detection, exploratory data analysis, and statistics for data professionals.
Category
📚
LearningTranscript
00:00What's going on, everybody? Welcome back to another video. Today, we are continuing our statistics series here on YouTube, and
00:06we are going to be looking at outliers.
00:08Now, in the most simplest sense, an outlier is just a piece of data that falls outside of the pattern
00:14of the rest of your data.
00:15There is no predetermined rule that defines what an outlier is. It depends on your data.
00:21But right here, we have something called an outlier rule.
00:24It's a generalized rule, but it doesn't have to be exactly what you use, but that's what we're going to
00:30be kind of looking at in this lesson today.
00:32Now, this rule is 1.5 times IQR fences. Now, IQR stands for interquartile range.
00:39Now, if we look down below, we have this teal box. It's quartile 1 to quartile 3. That is our
00:45IQR. That's our middle 50% of our data.
00:49Then you'll see on the left-hand side and the right-hand side, we have an outlier zone past our
00:56little formula down here.
00:57It's quartile 3 plus 1.5 times the IQR.
01:00Now, for this, we're assuming we're just using one variable.
01:04We can have two, three, four, five different variables as part of this equation as it gets more advanced.
01:10We're just going to be taking a look at, you know, a base of using only one column of data
01:14to keep the variables at a minimum.
01:16Now, this 1.5 is just an arbitrary number. It doesn't mean that you have to use it every time.
01:22Depending on what you're looking for, you may increase it, you may decrease it.
01:25So, with that being said, let's get hands-on and see how we can actually use this outlier rule to
01:31detect outliers in our data.
01:33So, what we have right here is our customer's data. If we look down here, we have about 5,000
01:37rows of data.
01:38We have the customer ID, age, gender, region, sign-up days ago, lifetime value, and loyalty tier.
01:44Now, this was a data set that we looked at in a previous lesson.
01:47So, if you've been following along, then you should be familiar with this data set.
01:51Now, once again, we're going to be looking at this lifetime value right here.
01:56If we scroll down, we're actually setting up just a quick box plot.
01:59Now, box plots are fantastic for determining outliers, at least visually seeing outliers.
02:05That doesn't always mean that it is an outlier.
02:08For example, when we're looking at this, I think all of our eyes go to this one right here.
02:13And we say, oh man, this is so outside of the norm, this must be an outlier.
02:18And you're probably right as it pertains to this data.
02:23But data even here could be an outlier depending on, you know, our data.
02:28For example, if there's a lifetime value and we have someone up here who's at like $13,000 or more,
02:35yeah, that might be odd, but if our products are very low costing and we even have one that's,
02:40you know, $5,000 or $4,000, those might be outliers as well.
02:44And so, what we're going to do is use that outlier rule to actually determine what are
02:48real outliers in this data.
02:51It doesn't mean that we have to get rid of this data.
02:53In fact, I'll talk about this later on.
02:55What do you do with these outliers?
02:57We'll talk about that at the end.
02:58But you can do several different things with them.
03:02So, let's come right down here and let's start taking a look.
03:05The very first thing that we need to do is determine those quartiles.
03:08That's our IQR.
03:09So, we have to determine that 25 to 75% to get that middle 50% of our quartile.
03:14It's actually quite easy.
03:15I'm just going to say determine IQR.
03:20Now, to do this, we're just going to write something super simple.
03:24QR and Q3.
03:26That's going to be equal to the customers.
03:28And then we're pulling in that lifetime underscore value.
03:33And then, we're going to use something called a quantile.
03:36And this is just a really great function where we can just pull in two different quartiles.
03:42So, we're going to say 0.25 comma 0.75.
03:47And then we'll close that.
03:49So, for this data, if we look at even just quartile 1, it's going to be 134.91.
03:55And let's look at quartile 3, it's going to be 466.
04:00So, that's our 25th percent and our 75th percent quartile.
04:03Now, all we have to do to determine our IQR interquartile range is subtract them.
04:10So, we're going to say IQR is equal to Q3 minus Q1.
04:16And then, if we do IQR, just to take a look at it, this is our interquartile range, 331.4.
04:23That's from the 25th percentile all the way up to the 75th percentile.
04:28If we look at this one more time, we've just done this.
04:31Okay, that's all we've done.
04:33But now, we have to do this part.
04:35We have to determine our upper limit and our lower limit.
04:39Now, our lower limit is not as important because we know that nothing goes below zero in our data set.
04:44But if it did, if somebody had a negative $500 balance, that might be an outlier.
04:49Maybe it meant to be a positive $500.
04:51And it was just a data error.
04:53So, let's determine our lower fence and our upper fence.
04:57So, let's come here.
04:58We'll call this our upper fence.
05:00And we're going to do that calculation.
05:01We're going to say quartile 3 plus, and then we do 1.5 times our IQR.
05:08So, we're going to do times IQR.
05:10And I just realized that this says quartile R, and it should be quartile 3.
05:16So, now we'll do this.
05:18And let's just see what this looks like.
05:20Our upper fence, if something is over $963, that's going to be our upper fence.
05:25Now, let's get our lower fence.
05:26And it's almost the exact same thing.
05:28So, let's copy this right down here.
05:30And we're going to say lower fence.
05:33It's quartile 1.
05:34And then, we're doing minus 1.5 times IQR.
05:39So, now, let's take this.
05:42And let's run this.
05:43And that is meant to be a minus.
05:45I don't know how I put a 1 there.
05:47Let's do this again.
05:48It's minus 362.1875.
05:51Now, again, this outliers rule is generalized.
05:54You can customize it for your data set.
05:57But this is now going to be our upper fence and our lower fence.
06:00Now, I'm going to pull in this over here because I've already written this out.
06:03And it's going to take a little while.
06:04But I'm going to paste this in here.
06:06We're going to determine our outliers by saying if the customer's lifetime value is greater than that upper fence,
06:12if it's over this, or if it is lower than our lower fence.
06:18Then, we are sorting the values by lifetime value ascending equals false, which just means descending highest to lowest.
06:26So, let's go ahead and run this.
06:28And then, we'll take a look at our outliers.
06:30And let's run this.
06:31And so, here we are showing that we have 348 rows of data that are considered outliers based off of
06:39this rule.
06:40The biggest one being this customer right here who's ordered $13,000 worth of our product.
06:48If we go back up to our data and look right up here, you can see just at a glance,
06:52I mean, we could open this up and look at all the data.
06:54But most customers are like $70, $500, $132, $647.
07:02$13,000 is really far outside of our range.
07:05But based off of this outlier rule, even a lot of these are still considered outliers.
07:11So, if we go back up here, this fence right here, a little over this fence, a lot of these
07:17are outliers.
07:19Now, like I said, right now we're only taking in one variable, but you can have multiple variables in here.
07:26For our data set, we might want to create a lot larger, lower, and upper fence.
07:32For example, we still have our upper quartiles, and that's totally fine, but what if we want to multiply it
07:39times, let's say, 2, for example.
07:41Let's just go ahead and run this.
07:43Before we had 348, let's go ahead and run this.
07:47Now we only have 245, and our range starts at around 1100 or so.
07:53Outliers are not just kind of a generalized thing.
07:56They're very specific to your data set.
07:59For example, we may want to add in this variable right here.
08:01Sign up days ago.
08:03So, this customer, they've been with us for almost three years or so, so a higher number makes sense.
08:09But maybe we want to say, hey, if they're above that upper fence, and they've only been with us for
08:14less than 50 days, then that would be an outlier.
08:17So, that's an example of multiple variables added in, and you need to set those conditions and those rules for
08:24your data set.
08:24Now, what do we do once we have identified this?
08:27Is this a problem?
08:29Is this something that we need to handle?
08:31Well, for this data set, just looking at it for lifetime value, probably not.
08:35You know, this is well within reason of, you know, something that they would maybe purchased or bought.
08:40There could be other variables that we take a look at.
08:43But let's say we find outliers that maybe didn't make sense.
08:46Maybe at the bottom here, we had one that's like negative 8,000.
08:51What would we do to that?
08:52The first thing I would do, especially as a data analyst, is I'm going to dig into that data.
08:57I'm going to go into the database.
08:58I'm going to say, okay, where did this come from?
09:00Was it like this in our raw data when we pulled that data in?
09:03If so, then I need to go to the customer or the client, and I need to say, hey, there's
09:08some weird data in here.
09:09Can you guys help me understand that?
09:10Maybe it's some refund process that they have on their end, and that makes perfect sense to them, and they
09:14want to keep that data in.
09:15Or there was an issue with the data ingestion, and that actually was supposed to be, you know, a positive
09:20number.
09:21And we need to manually go in and change that, or they need to change it in their system.
09:25Sometimes with outliers, we actually just want to delete this data altogether.
09:28I mean, for this data, of course not, right?
09:30This looks like perfectly reasonable outliers.
09:34These aren't anything too crazy.
09:35But sometimes outliers are actually just bad data, and we want to get rid of that.
09:40And so I might determine these outliers, and then I might come in here and just say, let's delete this.
09:45Yeah, of course I'm going to write the actual code, but I'm going to come in here, and I'm just
09:48going to delete this from our data set,
09:49so when we use it for dashboards or whatever down the line, we don't have these outliers sitting in our
09:54data.
09:55And so that is how we calculate outliers.
09:57Again, this is generalized.
09:58This is kind of how you might start it, and then you, you know, craft it and change it for
10:03your use case.
10:04But this is how we can determine outliers within our data.
10:07If you like this video, be sure to like and subscribe.
10:10If you haven't already, I have a full statistics course on analystbuilder.com.
10:14I will leave a link down in the description where I go a lot more in-depth into a lot
10:18of statistical concepts,
10:19and you'll learn a lot more about statistics, which I personally love.
10:22I think I'm going to make a lot more of this content in the future because I love making these
10:26videos on statistics.
10:27They're really, really fun.
10:29So if you like this video, subscribe, and I will see you next week.
10:37Bye.