Outliers and cleaning data
An outlier is an observation unusually far from the main body of data. It may be a recording error, a genuine rare event, or evidence that several populations have been mixed. Its cause matters: deleting every unusual value makes data look tidy but can destroy important evidence.
Data cleaning means finding and resolving errors, inconsistencies and ambiguities before analysis. Every change needs a reason that could be explained and reproduced.
Prerequisites
Section titled “Prerequisites”You should be able to:
- order data and find quartiles;
- calculate the interquartile range, mean and standard deviation;
- interpret box plots and summary statistics;
- distinguish qualitative, discrete and continuous variables.
Review averages and measures of spread and presenting and interpreting data if needed.
The interquartile range is
A common convention classifies as an outlier when
The two boundary values are called the lower fence and upper fence. A value equal to a fence is not an outlier under these strict inequalities.
This is a flagging rule, not a definition that applies in every problem. If a question gives another rule, use that rule. Some questions define an outlier as more than standard deviations from the mean, for example.
Worked example 1: calculate the fences
Section titled “Worked example 1: calculate the fences”For a set of journey times,
First calculate
The fences are
Therefore a time below or above minutes is an outlier. The values and themselves are not outliers.
Worked example 2: raw data from start to finish
Section titled “Worked example 2: raw data from start to finish”Consider the ordered data
There are values. Using the medians of the lower and upper halves,
Hence
and
Only lies outside the fences, so only is flagged. Notice that is the same distance from as would be from , but neither would be an outlier. Outlier status is determined by the fences, not by appearance.
Self-check 1
Section titled “Self-check 1”A data set has and . Decide which of are outliers under the rule.
Answer
The fences are
Thus and are outliers. The boundary values and are not.
Other stated outlier rules
Section titled “Other stated outlier rules”An alternative rule may classify as an outlier if it lies more than standard deviations from the mean:
Equivalently,
Worked example 3: a standard deviation rule
Section titled “Worked example 3: a standard deviation rule”Suppose and , and the rule is that values more than standard deviations from the mean are outliers.
Therefore is not an outlier, is, is not, and is.
The mean and standard deviation are themselves sensitive to extreme values. This rule can therefore behave differently from the IQR rule, which uses resistant quartiles. Never switch rules simply because one produces the answer you expected.
What an outlier does to statistics
Section titled “What an outlier does to statistics”The mean, range and standard deviation are sensitive to extreme observations. The median and IQR are more resistant because they depend mainly on ordered positions.
Worked example 4: comparing before and after
Section titled “Worked example 4: comparing before and after”For
the mean is
while the median is . Also,
If investigation establishes that was a transcription error for , the corrected data have
The mean and range change greatly, but the median does not change. This does not justify replacing merely because it has a large effect. The correction is justified only by evidence about the recorded value.
For skewed data or data containing genuine extremes, reporting the median and IQR may represent centre and spread better than reporting the mean and standard deviation.
Investigate before changing
Section titled “Investigate before changing”When a value is flagged, ask:
- Is it possible? A negative age or a percentage above is usually impossible in its stated context.
- Are the units consistent? A height recorded as among centimetre values may be in metres.
- Does the source confirm it? Check the original form, instrument reading or database record.
- Is it a duplicate or a distinct case? Matching values do not necessarily mean matching observations.
- Could it be genuine? A record temperature or very long waiting time can be valid and important.
- Does it belong to another population? An adult measurement in a children’s sample may indicate a selection error.
Possible decisions include:
| Evidence | Defensible action |
|---|---|
| source confirms a typing error | correct it and record the correction |
| impossible value, correct value unknown | mark it missing or exclude that case from the relevant calculation |
| inconsistent units with known conversion | convert to common units and document it |
| genuine extreme observation | retain it, possibly reporting resistant summaries too |
| cause remains uncertain | flag it and compare analyses with and without it |
A reproducible cleaning process
Section titled “A reproducible cleaning process”Preserve the raw data and work on a copy. For each variable:
- check its meaning, type and units;
- standardise category labels, such as
Female,femaleandF, only when they mean the same thing; - distinguish zero from missing values;
- test valid ranges and logical relationships;
- identify duplicates using the fields that define one case;
- calculate outlier flags where appropriate;
- record every correction, exclusion and unresolved query.
A useful cleaning log contains
This gives an audit trail and allows another analyst to reproduce the cleaned data.
Worked example 5: clean a small table
Section titled “Worked example 5: clean a small table”Suppose one row represents one runner in a km race.
| Runner ID | Time (min) | Age | Club |
|---|---|---|---|
| 101 | North | ||
| 102 | South | ||
| 103 | north | ||
| 103 | north | ||
| 104 | North |
Reason through each issue:
- A race time of is impossible for a finisher. Check whether it means missing, did not finish, or a recording error. Do not include it as a genuine time.
- Age is impossible. The source is needed before it can be corrected.
- The repeated row for runner 103 is a likely duplicate because the identifier and all fields match. Confirm that runner ID uniquely identifies an entry before deleting one copy.
Northandnorthcan be standardised if the data dictionary confirms they are the same club.- minutes is plausible for a year old runner. Even if an outlier rule flags it, retain it unless evidence shows an error.
The example separates validity checks, which use context, from outlier checks, which use the distribution.
Analysing with and without a disputed value
Section titled “Analysing with and without a disputed value”If the status of a suspected value cannot be resolved, a sensitivity analysis shows whether it controls the conclusion.
Worked example 6: sensitivity analysis
Section titled “Worked example 6: sensitivity analysis”Daily machine outputs are
With all values,
Without the disputed ,
The conclusion about typical output depends strongly on if the mean is used, but hardly changes if the median is used. A responsible report might state both analyses and explain that the source record for could not be verified. It should not quietly choose the version that supports a preferred claim.
Common misconceptions
Section titled “Common misconceptions”- Every largest or smallest value is an outlier. Extremes are outliers only if they meet the stated rule.
- Every outlier should be deleted. Genuine extremes belong to the data and may be the most informative observations.
- A value inside the fences must be correct. Plausible values can still be mistyped, duplicated or assigned to the wrong case.
- A zero is the same as a blank. Zero is a numerical value; a blank may mean missing or not applicable.
- Cleaning is harmless preparation. Corrections and exclusions change results, so they must be justified and recorded.
- The IQR rule proves a distribution is abnormal. It only flags observations relative to the quartiles of that sample.
Final self-check
Section titled “Final self-check”A sample has
- Find the lower and upper outlier fences.
- Classify , , and .
- A value of is confirmed by the original measuring instrument. Should it automatically be removed?
- A value of was entered twice for the same unique case identifier. Is the fact that it is not an outlier enough reason to retain both rows?
Answer
First,
Therefore
- The fences are and .
- Values and are outliers. Values equal to the fences, and , are not.
- No. It is unusual but confirmed, so it should normally be retained and its effect discussed.
- No. Outlier detection and duplicate detection are different checks. If the identifier truly defines a unique case, investigate the duplicate and retain only the genuine record or records.
Next steps
Section titled “Next steps”- Apply cleaning decisions in working with a large data set.
- Display outliers and compare groups in presenting and interpreting data.
- Examine how extremes affect correlation and regression.
- Review how biased collection can create misleading data in populations, samples and sampling.