R Tutorial: Detecting anomalies in data
Key Takeaways
This video tutorial demonstrates how to detect anomalies in data using R, covering methods such as the three sigma rule, 1.5 IQR rule, K nearest neighbors distance, and local outlier factor.
Full Transcript
dealing with a normal hose datapoints also known as outliers is an important step in the data preparation phase if not properly handled outliers could skew your analysis and produce misleading conclusions in this video you will learn how to detect and treat outliers in your data to detect outliers in the distribution of a single numerical variable you can resort to two simple rules the three sigma rule for normally distributed data and the more general 1.5 IQR rule in a multivariate setting there is a plethora of methods to choose from we will highlight two of them the K nearest neighbors distance and the local outlier factor if your variable is normally distributed the three sigma rule can be applied in this case you can label any observations three standard deviations above or below the mean as an outlier given that its probability will be less than 0.1% another popular method is the 1.5 IQR rule this method can be applied regardless of your underlying data distribution the interquartile range or IQR is the difference between the third and the first quantiles of your variable any observation below p1 minus 1.5 IQR or above q3 plus 1.5 IQR is considered an outlier multivariate methods can be broadly categorized as either distance based or density based they both rely on the reasonable assumption that anomalous observations frequently live far from their neighbors distance based methods often use the average distance to their K nearest neighbors as an indicator of the extremeness of the observation density based methods operate by counting the number of neighboring points within a predefined reachability distance this plot shows some potential outliers along the fuel consumption and distance axis we can expect they will have a larger average distance to their neighbors compared to those points that appear clumped together we will use the get Canaan function from the fnn package to efficiently compute the distance matrix for the K nearest neighbors distance based methods fail to properly detect outliers in regions exhibiting different spatial densities these are called local outliers for example while the data point in the middle top of the figure could be regarded as a global outlier there might exist multiple local outliers in different regions the local outlier factor or LOF is an algorithm that measures the local deviation of a data point relative to its neighbors outliers are defined as data points with substantially lower density than their neighbors each of servation receives an lofs score that indicates whether it is deemed to be a regular data point an in liar or an outlier we will use the LOF function from the DBA scan package to compute the lofs score after detecting the outliers we can proceed in several ways first try to retain them in your data by choosing algorithms that are robust to them if this is not possible you could try applying data imputation methods such as linear or kanan imputation hopefully they will replace the outliers with less extreme observations another option is capping the outliers by replacing the low values with a 5th percentile of the variable distribution and the high values with a 95th percentile excluding the entire observations from your data analysis pipeline is also possible all this is often frowned upon undef initely not recommended for small datasets or those whose distribution is not Gaussian in any case use your domain knowledge to understand why these extremist relations are happening in your data set okay let
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/practicing-machine-learning-interview-questions-in-r at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Dealing with anomalous data points (also known as outliers) is an important step in the data preparation phase. If not properly handled, outliers could skew your analysis and produce misleading conclusions. In this video, you will learn how to detect and treat outliers in your data.
To detect outliers in the distribution of a single numerical variable, you can resort to two simple rules:
- the 3 sigma rule (for normally distributed data)
- and the more general 1.5 IQR rule.
In a multivariate setting there is a plethora of methods to choose from. We will highlight two of them:
- the K-nearest neighbors distance
- and the local outlier factor.
If your variable is normally distributed, the 3-sigma rule can be applied. In this case, you can label any observation 3 standard deviations above or below the mean as an outlier given that its probability will be less than 0.1%.
Another popular method is the 1.5 IQR rule.
This method can be applied regardless of your underlying data distribution.
The Inter-Quartile Range or IQR is the difference between the third and the first quantiles of your variable.
Any observation below Q1 - 1.5IQR or above Q3 + 1.5IQR is considered an outlier.
Multivariate methods can be broadly categorized as either distance-based or density-based.
They both rely on the reasonable assumption that anomalous observations frequently lie far from their neighbors.
Distance-based methods often use the average distance to their K nearest neighbors as an indicator of the extremeness of the observation.
Density-based methods operate by counting the number of neighboring points within a predefined reachability distance.
This plot shows some potential outliers along the fuel consumption and distance axes. We
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: ML Pipelines
View skill →
🎓
Tutor Explanation
DeepCamp AI