Skip to content

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.

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 1.5IQR1.5\operatorname{IQR} rule

Section titled “The 1.5IQR⁡1.5\operatorname{IQR}1.5IQR rule”

The interquartile range is

IQR=Q3Q1.\operatorname{IQR}=Q_3-Q_1.

A common convention classifies xx as an outlier when

x<Q11.5IQRorx>Q3+1.5IQR.\boxed{x<Q_1-1.5\operatorname{IQR}} \qquad\text{or}\qquad \boxed{x>Q_3+1.5\operatorname{IQR}}.

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 22 standard deviations from the mean, for example.

For a set of journey times,

Q1=18,qquadQ3=30.Q_1=18,qquad Q_3=30.

First calculate

IQR=3018=12.\operatorname{IQR}=30-18=12.

The fences are

lower fence=181.5(12)=0,upper fence=30+1.5(12)=48.\begin{aligned} \text{lower fence}&=18-1.5(12)=0,\\ \text{upper fence}&=30+1.5(12)=48. \end{aligned}

Therefore a time below 00 or above 4848 minutes is an outlier. The values 00 and 4848 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

2,4,5,7,8,9,10,12,13,30.2, 4, 5, 7, 8, 9, 10, 12, 13, 30.

There are 1010 values. Using the medians of the lower and upper halves,

Q1=5,qquadQ3=12.Q_1=5,qquad Q_3=12.

Hence

IQR=125=7\operatorname{IQR}=12-5=7

and

lower fence=51.5(7)=5.5,upper fence=12+1.5(7)=22.5.\begin{aligned} \text{lower fence}&=5-1.5(7)=-5.5,\\ \text{upper fence}&=12+1.5(7)=22.5. \end{aligned}

Only 3030 lies outside the fences, so only 3030 is flagged. Notice that 22 is the same distance from 55 as 1515 would be from 1212, but neither would be an outlier. Outlier status is determined by the fences, not by appearance.

A data set has Q1=24Q_1=24 and Q3=36Q_3=36. Decide which of 5,6,54,555,6,54,55 are outliers under the 1.5IQR1.5\operatorname{IQR} rule.

Answer IQR=3624=12.\operatorname{IQR}=36-24=12.

The fences are

241.5(12)=6,qquad36+1.5(12)=54.24-1.5(12)=6,qquad 36+1.5(12)=54.

Thus 55 and 5555 are outliers. The boundary values 66 and 5454 are not.

An alternative rule may classify xx as an outlier if it lies more than kk standard deviations from the mean:

xxˉ>kσ.|x-\bar{x}|>k\sigma.

Equivalently,

x<xˉkσorx>xˉ+kσ.x<\bar{x}-k\sigma \qquad\text{or}\qquad x>\bar{x}+k\sigma.

Worked example 3: a standard deviation rule

Section titled “Worked example 3: a standard deviation rule”

Suppose xˉ=72.4\bar{x}=72.4 and σ=6.3\sigma=6.3, and the rule is that values more than 22 standard deviations from the mean are outliers.

xˉ2σ=72.42(6.3)=59.8,xˉ+2σ=72.4+2(6.3)=85.0.\begin{aligned} \bar{x}-2\sigma&=72.4-2(6.3)=59.8,\\ \bar{x}+2\sigma&=72.4+2(6.3)=85.0. \end{aligned}

Therefore 59.859.8 is not an outlier, 59.759.7 is, 85.085.0 is not, and 85.185.1 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.

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

8,9,10,10,11,12,45,8, 9, 10, 10, 11, 12, 45,

the mean is

xˉ=1057=15,\bar{x}=\frac{105}{7}=15,

while the median is 1010. Also,

range=458=37.\operatorname{range}=45-8=37.

If investigation establishes that 4545 was a transcription error for 1515, the corrected data have

xˉ=757=10.714,qquadmedian=10,qquadrange=158=7.\bar{x}=\frac{75}{7}=10.714\ldots,qquad \operatorname{median}=10,qquad \operatorname{range}=15-8=7.

The mean and range change greatly, but the median does not change. This does not justify replacing 4545 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.

When a value is flagged, ask:

  1. Is it possible? A negative age or a percentage above 100%100\% is usually impossible in its stated context.
  2. Are the units consistent? A height recorded as 1.721.72 among centimetre values may be in metres.
  3. Does the source confirm it? Check the original form, instrument reading or database record.
  4. Is it a duplicate or a distinct case? Matching values do not necessarily mean matching observations.
  5. Could it be genuine? A record temperature or very long waiting time can be valid and important.
  6. Does it belong to another population? An adult measurement in a children’s sample may indicate a selection error.

Possible decisions include:

EvidenceDefensible action
source confirms a typing errorcorrect it and record the correction
impossible value, correct value unknownmark it missing or exclude that case from the relevant calculation
inconsistent units with known conversionconvert to common units and document it
genuine extreme observationretain it, possibly reporting resistant summaries too
cause remains uncertainflag it and compare analyses with and without it

Preserve the raw data and work on a copy. For each variable:

  1. check its meaning, type and units;
  2. standardise category labels, such as Female, female and F, only when they mean the same thing;
  3. distinguish zero from missing values;
  4. test valid ranges and logical relationships;
  5. identify duplicates using the fields that define one case;
  6. calculate outlier flags where appropriate;
  7. record every correction, exclusion and unresolved query.

A useful cleaning log contains

(case identifier, original value, new value, reason).(\text{case identifier},\ \text{original value},\ \text{new value},\ \text{reason}).

This gives an audit trail and allows another analyst to reproduce the cleaned data.

Suppose one row represents one runner in a 55 km race.

Runner IDTime (min)AgeClub
10124.624.63131North
102002828South
10327.127.14-4north
10327.127.14-4north
10462.862.87373North

Reason through each issue:

  • A race time of 00 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 4-4 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.
  • North and north can be standardised if the data dictionary confirms they are the same club.
  • 62.862.8 minutes is plausible for a 7373 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.

Daily machine outputs are

98,99,100,101,102,150.98, 99, 100, 101, 102, 150.

With all values,

xˉ=6506=108.33,qquadmedian=100.5.\bar{x}=\frac{650}{6}=108.33\ldots,qquad \operatorname{median}=100.5.

Without the disputed 150150,

xˉ=5005=100,qquadmedian=100.\bar{x}=\frac{500}{5}=100,qquad \operatorname{median}=100.

The conclusion about typical output depends strongly on 150150 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 150150 could not be verified. It should not quietly choose the version that supports a preferred claim.

  • 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.

A sample has

Q1=14.2,qquadQ3=19.8.Q_1=14.2,qquad Q_3=19.8.
  1. Find the lower and upper outlier fences.
  2. Classify 5.75.7, 5.85.8, 28.228.2 and 28.328.3.
  3. A value of 28.328.3 is confirmed by the original measuring instrument. Should it automatically be removed?
  4. A value of 18.618.6 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,

IQR=19.814.2=5.6.\operatorname{IQR}=19.8-14.2=5.6.

Therefore

lower fence=14.21.5(5.6)=5.8,upper fence=19.8+1.5(5.6)=28.2.\begin{aligned} \text{lower fence}&=14.2-1.5(5.6)=5.8,\\ \text{upper fence}&=19.8+1.5(5.6)=28.2. \end{aligned}
  1. The fences are 5.85.8 and 28.228.2.
  2. Values 5.75.7 and 28.328.3 are outliers. Values equal to the fences, 5.85.8 and 28.228.2, are not.
  3. No. It is unusual but confirmed, so it should normally be retained and its effect discussed.
  4. 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.