Why Flag Observations Using the IQR?
An unusually small or large value can affect summaries and may indicate a data-entry problem. But an extreme value can also be a genuine observation from a skewed population. The 1.5 IQR rule gives a consistent way to flag values for investigation without deciding in advance that they are errors.
The rule uses the first and third quartiles, which describe the lower and upper quarters of an ordered data set. The interquartile range measures the width of the middle half. Because the rule is based on quartiles rather than the mean and standard deviation, it is less influenced by extreme observations. It is a screening convention, not a statistical test of whether an observation is wrong.
Calculating and Applying the Fences
The procedure is short, but each step matters: establish the quartiles, calculate the IQR, calculate both fences, and compare every observation with those fences. The fence values need not be values that appear in the data. An observation equal to a fence is not flagged, because the rule uses strict inequalities.
Use a stated, consistent quartile convention. If using R’s default sample quantiles, specify type 7 when reproducibility matters.
Subtract the first quartile from the third: \(IQR=Q_3-Q_1\).
Extend 1.5 IQR below \(Q_1\) and above \(Q_3\).
Flag values below the lower fence or above the upper fence; do not flag values that equal either fence.
Here is why the formula has this form. The middle half of the data spans \(Q_1\) to \(Q_3\), a distance of \(IQR\). The rule extends from each endpoint by 1.5 times that distance. Starting at \(Q_1\), extending downward gives \(Q_1-1.5IQR\); starting at \(Q_3\), extending upward gives \(Q_3+1.5IQR\). The rule therefore identifies values unusually far from the central half on either side. The choice of 1.5 is a conventional screening threshold; it does not guarantee a particular error rate for every population.
Worked Example: Flagging a Large Recorded Amount
A hypothetical data extract contains eight recorded amounts, in thousands of dollars: 12, 14, 15, 16, 18, 19, 20, and 35. Use R’s type 7 sample quantiles to find the fences and determine which values are flagged.
Method. Sort the observations as given and calculate the type 7 quartiles. For a sample of \(n=8\), type 7 uses position \(1+(n-1)p\) for quantile probability \(p\), interpolating between adjacent ordered observations when the position is fractional.
For \(Q_1\), \(p=0.25\), so the position is \(1+7(0.25)=2.75\). This is three-quarters of the way from the second value, 14, to the third value, 15:
For \(Q_3\), \(p=0.75\), so the position is \(1+7(0.75)=6.25\). This is one-quarter of the way from the sixth value, 19, to the seventh value, 20:
The IQR is \(19.25-14.75=4.50\). The lower fence is \(14.75-1.5(4.50)=14.75-6.75=8.00\). The upper fence is \(19.25+1.5(4.50)=19.25+6.75=26.00\). All observations from 12 through 20 lie between 8 and 26; 35 is greater than 26 and is flagged. No observation is below 8.
Check. The IQR is positive, the lower fence is below \(Q_1\), and the upper fence is above \(Q_3\). The flagged value is therefore on the high side, as expected from the comparison. A flag indicates that 35 should be reviewed; it does not establish that the recorded amount is wrong.
Using the Rule in R
R’s quantile calculation can make the quartile convention explicit. For a numeric vector named x, the following expression requests the first and third quartiles using type 7 and excludes missing values from the calculation: quantile(x, probs = c(0.25, 0.75), type = 7, na.rm = TRUE). If missing values are not removed, the calculation may return missing results.
After obtaining \(Q_1\) and \(Q_3\), calculate \(IQR=Q_3-Q_1\), then set the lower fence to \(Q_1-1.5IQR\) and the upper fence to \(Q_3+1.5IQR\). An observation is flagged when x is less than the lower fence or greater than the upper fence. In R, the corresponding logical comparison is x < lower or x > upper. The comparison should use the same quartiles and fences as the reported analysis.
The quartile convention matters most in smaller samples, where different methods can give different interpolated values. R’s boxplot calculations use hinges, which may differ from the type 7 quantiles returned by quantile(). Consequently, a hand calculation based on type 7 quantiles need not reproduce every detail of a boxplot made with boxplot(). When comparing calculations, first check that both use the same quartile convention rather than assuming a discrepancy means an error.
Worked Example: A Flag in a Small Sample With a Missing Value
A hypothetical claims review records amounts, in thousands of dollars: 6, 7, 7, 8, 9, 10, 11, 24, and one missing value. Use type 7 quantiles, excluding the missing value, and identify the flagged observation.
Method. Remove the missing value for the calculation, leaving \(n=8\) observed amounts. The sorted values are 6, 7, 7, 8, 9, 10, 11, 24. For \(Q_1\), the type 7 position is \(1+7(0.25)=2.75\). The second and third values are both 7, so \(Q_1=7\).
For \(Q_3\), the position is \(1+7(0.75)=6.25\). The sixth value is 10 and the seventh is 11, giving \(Q_3=10+0.25(11-10)=10.25\). Thus \(IQR=10.25-7=3.25\).
The lower fence is \(7-1.5(3.25)=7-4.875=2.125\). The upper fence is \(10.25+1.5(3.25)=10.25+4.875=15.125\). The observed values from 6 through 11 fall within the fences, while \(24>15.125\), so 24 is flagged. The missing value is not itself classified as inside or outside the fences; it was excluded from the quartile calculation.
Check. The eight observed values were used, not nine. The only value outside the calculated interval is 24. The result should be reported with the quartile method and missing-value treatment, so another analyst can reproduce it.
What a Flag Means—and What It Does Not
A flagged observation is unusual relative to the quartile spread of the data being assessed. It is a reason to inspect the record and its context. Useful follow-up questions include whether the value was entered in the correct units, whether a decimal point or category code was misread, whether the record belongs to the intended population, and whether the value is supported by the source data.
A flag is not a decision to delete, cap, or change a value. For example, a large claim amount may be unusual but valid. Removing it solely because it exceeds a fence can distort the population being described, especially when high values are a genuine feature of the process. Conversely, a value within the fences can still be a data error; the rule is not a comprehensive validation check.
The rule also depends on the group being examined. A value that is flagged in a mixed population may be ordinary within a relevant subgroup, while a value that is not flagged overall may be unusual within that subgroup. Decide which population the question concerns before applying the rule. The fences do not identify the cause of an extreme value or establish a causal explanation.
Worked Example: Correcting an Arithmetic Trap
A reviewer reports \(Q_1=8\) and \(Q_3=14\) for a variable measured in thousands of dollars. The observed values include 22 and 23. Determine the upper fence and which of those two values is flagged.
Method. First calculate \(IQR=14-8=6\). The upper fence is \(Q_3+1.5IQR=14+1.5(6)=14+9=23\). The lower fence, for completeness, is \(Q_1-1.5IQR=8-9=-1\).
The value 22 is below the upper fence and above the lower fence, so it is not flagged. The value 23 equals the upper fence, so it is also not flagged: the rule flags values strictly greater than the upper fence. Neither of these two observations is flagged.
Common mistake. Writing \(14+1.5(6)=20\) is an arithmetic error: \(1.5(6)=9\), and \(14+9=23\). A separate interpretation mistake would be to flag 23 because it “reaches” the fence. The correction is to calculate the fence carefully and apply the strict comparison \(x>23\), not \(x\geq23\).
Check. The upper fence exceeds \(Q_3=14\), as it must when the IQR is positive. Both candidate values are at or below 23, so neither meets the strict upper-fence condition.
Practical Interpretation
In a boxplot, the fences are thresholds used to identify possible outliers; they are not necessarily drawn as whisker endpoints. Whiskers typically extend to the most extreme observed values still within the fences, and observations beyond them are plotted individually. Thus, a plotted point beyond a whisker corresponds to a value beyond a fence under the boxplot’s quartile convention, but the whisker itself need not end at the numerical fence.
The rule makes no distributional assumption. It can be applied to a numeric variable even when the distribution is skewed, although skewness can lead to many flags on one side. Under a normal population model, the population quartiles are approximately \(0.6745\) standard deviations below and above the mean. Since the population IQR is then about \(1.349\) standard deviations, the upper fence is approximately \(2.698\) standard deviations above the mean and the lower fence approximately \(2.698\) standard deviations below it. This illustrates how the convention behaves under a normal model; it does not make the rule a normality test or imply that all populations have the same flag rate.
For a reproducible analysis, state the quartile method, the treatment of missing values, the fence values, and which observations were flagged. Then describe what checks are warranted in context. This keeps a useful screening signal from being mistaken for proof of error or a reason for automatic data alteration.
Check Your Understanding
For each question, calculate or apply the stated fences carefully. Quartiles are given unless the question specifies a sample calculation.
- A data set has \(Q_1=4\) and \(Q_3=10\), with observations \(-6\), \(-5\), and 18. Find the fences and identify the flagged observations. (Answer: \(IQR=6\), fences \(-5\) and 19; only \(-6\) is flagged.)
- A numeric variable has \(Q_1=12\), \(Q_3=20\), and candidate observations 0, 32, and 32.1. Calculate both fences and state which candidates are flagged. (Answer: \(IQR=8\), fences 0 and 32; only 32.1 is flagged.)
- Using type 7 quantiles, find the fences for the sorted sample 2, 4, 6, 8, 10, 30. Which value is flagged? (Answer: \(Q_1=4.5\), \(Q_3=9.5\), \(IQR=5\), fences \(-3\) and 17; 30 is flagged.)
- Using type 7 quantiles, calculate the fences for 5, 6, 8, 9, 12, 13, 15, 40. State whether 15 and 40 are flagged. (Answer: \(Q_1=7.5\), \(Q_3=13.5\), \(IQR=6\), fences \(-1.5\) and 22.5; 15 is not flagged and 40 is flagged.)
- A flagged value is confirmed against its source record and is valid, but it is much larger than most values. Should it automatically be deleted before reporting the data? Explain. (Answer: No. A flag prompts review, but a valid observation should not be deleted solely for exceeding a fence; its treatment depends on the analysis question and should be justified.)