Why IQR Fences Are a Screening Rule
A large or small observation can affect summaries and model fitting, so analysts need a reproducible way to identify values that merit review. For one numeric variable, Tukey’s interquartile-range (IQR) fences provide a simple rule: they set lower and upper thresholds from the middle half of the observed values. The rule identifies observations for examination; it does not determine whether they are errors.
The calculation depends on how quartiles are defined. In this tutorial, quartiles follow R’s default quantile convention, type 7. This convention interpolates between adjacent sorted observations when the quartile position is not an integer. Other quantile conventions can give different quartiles and therefore different flags, so a reproducible analysis should state the convention used.
Calculating Quartiles and Fences
Sort the non-missing observations in increasing order, writing them as \(x_{(1)},x_{(2)},\ldots,x_{(n)}\). Under R’s type 7 convention, the position for a proportion \(p\) is \(h=1+(n-1)p\). If \(h\) is not an integer, the quartile is linearly interpolated between the observations on either side of that position.
For example, if \(h=j+\gamma\), where \(j\) is the integer part and \(0<\gamma<1\), then the interpolated quantile is \((1-\gamma)x_{(j)}+\gamma x_{(j+1)}\). The fraction \(\gamma\) is the distance from the lower observation toward the next one. This matters: the increment is \(\gamma\) times the difference between adjacent observations, not an additional observation value.
Once \(Q_1\) and \(Q_3\) are calculated, the fence arithmetic follows directly. For instance, \(IQR=Q_3-Q_1\), so the lower fence subtracts one and a half IQRs from \(Q_1\), while the upper fence adds one and a half IQRs to \(Q_3\). These are thresholds, not necessarily observed values.
Worked Examples
Worked Example: Interpolating the First Quartile Correctly
A hypothetical portfolio review records eight claim amounts, in thousands of dollars: \(1,2,3,4,5,6,7,20\). Using R’s default type 7 convention, calculate the quartiles and fences, then identify any flagged observations. The method is to sort the data, find each quartile position, interpolate if needed, and compare every observation with both fences.
The values are already sorted, and \(n=8\). For \(Q_1\), use \(p=0.25\): \(h=1+(8-1)(0.25)=2.75\). This lies three quarters of the way from the second observation, 2, to the third, 3. Thus, \(Q_1=2+0.75(3-2)=2+0.75=2.75\).
For \(Q_3\), use \(p=0.75\): \(h=1+7(0.75)=6.25\). This lies one quarter of the way from 6 to 7, so \(Q_3=6+0.25(7-6)=6.25\).
Therefore, \(IQR=6.25-2.75=3.50\). The lower fence is \(2.75-1.5(3.50)=2.75-5.25=-2.50\). The upper fence is \(6.25+1.5(3.50)=6.25+5.25=11.50\). The observations 1 through 7 are within the fences; 20 is above 11.50 and is flagged. The calculation is checked by noting that \(Q_1\) lies between 2 and 3, \(Q_3\) lies between 6 and 7, and the upper fence is below 20.
Common mistake avoided: For the first quartile, writing \(2+0.75(3-2)=3.75\) is an arithmetic error: \(2+0.75=2.75\), not 3.75. The interpolation formula and the location of the result between 2 and 3 also confirm that 3.75 cannot be the correct value.
Worked Example: A High Observation and the Correct Fence Comparison
A hypothetical insurer reviews eight individual repair costs, in thousands of dollars: \(9,10,11,12,14,15,16,30\). Apply the type 7 quartiles, calculate both fences, and state which observations are flagged. The method is again interpolation followed by a strict comparison with each fence.
Here \(n=8\). The first-quartile position is \(1+7(0.25)=2.75\), between 10 and 11. Therefore, \(Q_1=10+0.75(11-10)=10.75\). The third-quartile position is \(1+7(0.75)=6.25\), between 15 and 16. Therefore, \(Q_3=15+0.25(16-15)=15.25\).
The IQR is \(15.25-10.75=4.50\), and \(1.5(IQR)=6.75\). The lower fence is \(10.75-6.75=4.00\); the upper fence is \(15.25+6.75=22.00\). All observations except 30 fall between 4.00 and 22.00, so 30 is flagged.
A common error is to calculate \(10+0.75(11-10)\) as 11.75. The correct result is 10.75, because the interpolated amount is 0.75 added to 10. Recomputing the IQR and fences from the correct quartile gives 4.50, 4.00, and 22.00, respectively. The flag for 30 remains, but the reported quartile and fence values must still be accurate.
Worked Example: Flagging Values in R When One Value Is Missing
A hypothetical claims extract contains values \(4,5,6,7,8,9,10,11,12,25\), in thousands of dollars, and one missing entry. Use R’s default quartile convention to calculate fences, flag the observed values, and report the missing entry appropriately.
First exclude the missing value from the quartile calculation, leaving \(n=10\) sorted observations. For \(Q_1\), \(h=1+9(0.25)=3.25\), so \(Q_1=6+0.25(7-6)=6.25\). For \(Q_3\), \(h=1+9(0.75)=7.75\), so \(Q_3=10+0.75(11-10)=10.75\). Thus, \(IQR=10.75-6.25=4.50\).
The lower fence is \(6.25-1.5(4.50)=6.25-6.75=-0.50\), and the upper fence is \(10.75+6.75=17.50\). The observed value 25 is above the upper fence and is flagged. The missing entry is not an observed numeric value and cannot be classified as inside or outside the fences.
In R, the calculation can be written as x <- c(4,5,6,7,8,9,10,11,12,25,NA), followed by qs <- quantile(x, c(0.25,0.75), na.rm=TRUE). The results are stored as q1 <- qs[1] and q3 <- qs[2]; then use iqr <- q3 - q1, lower <- q1 - 1.5*iqr, and upper <- q3 + 1.5*iqr.
A flag vector can be created with flag <- x < lower | x > upper. Its entry for 25 is TRUE; its entries for the other observed values are FALSE. Because comparisons involving NA return NA, the missing entry remains unclassified. To count observed flags, use sum(flag, na.rm=TRUE), which gives 1. The calculation is checked against the hand result: only 25 exceeds the upper fence.
Why the Rule Works as a Screen—and What It Does Not Establish
The fence formula follows from a chosen distance rule. The middle half of the data runs from \(Q_1\) to \(Q_3\), a distance of \(IQR=Q_3-Q_1\). Extending \(1.5\) such distances below \(Q_1\) and above \(Q_3\) gives the stated thresholds:
The multiplier 1.5 is part of Tukey’s screening convention. It is not derived from a claim that the observations follow a particular probability distribution, and a flag is not a hypothesis test or a probability statement. In particular, the rule does not say that a flagged observation is impossible, incorrect, or necessarily harmful to a model. It says only that the observation lies relatively far from the central half of this variable under the specified quartile convention.
This distinction matters in predictive analytics. A flagged value might reflect a recording mistake, a valid but uncommon exposure, or a genuine large claim. The IQR calculation alone cannot distinguish those explanations. It examines one variable at a time, so it also does not determine whether an observation is unusual in relation to other predictors. Keep the flag as a review signal and describe exactly which variable, quartile convention, and fences produced it.
Identify missing values and use a consistent policy for excluding them from the quartile calculation. Do not silently treat a missing value as a numeric observation.
Use the stated quantile convention. In R,
quantile(...) defaults to type 7; specify the convention if the analysis requires it to be explicit.Calculate the IQR and the lower and upper thresholds, then flag values strictly outside them. Values on a fence are not flagged.
State the observed value and threshold it exceeded. Do not label it an error based only on the fence calculation.
Several implementation details are easy to overlook. If \(IQR=0\), both fences equal the common quartile value; the rule still applies as written, but tied or highly discrete data may produce a large set of flags. If the data are rounded or heavily tied, a small change in quartile convention can also affect the thresholds. These are reasons to report the calculation transparently, not reasons to alter the rule after seeing which values it flags.
- Do not confuse a fence with a data value. A fence can lie between observed values; compare every observation numerically with the threshold.
- Do not reverse the interpolation fraction. At position \(j+0.25\), move one quarter of the gap from \(x_{(j)}\) toward \(x_{(j+1)}\).
- Do not flag equality. The rule concerns values below the lower fence or above the upper fence, not values equal to either one.
- Do not classify missingness as an outlier result. A missing observation has no numeric value to compare; retain its missing status.
- Do not treat a flag as a deletion instruction. It is a screening result that can support a later contextual review.
Check Your Understanding
Use R’s default type 7 convention unless a question says otherwise. For each problem, calculate the requested quantities before checking the final answer.
- A hypothetical dataset of eight policy durations, in years, is \(2,4,5,7,8,9,10,11\). Calculate \(Q_1\), \(Q_3\), the IQR, and both fences. State whether any observation is flagged. (Answer: \(Q_1=4.75\), \(Q_3=9.25\), \(IQR=4.50\), fences \(-2.00\) and \(16.00\); none flagged.)
- A hypothetical set of eight equipment repair costs, in hundreds of dollars, is \(3,4,5,6,7,8,9,22\). Calculate both fences and identify any flagged observation. (Answer: \(Q_1=4.75\), \(Q_3=8.25\), \(IQR=3.50\), fences \(-0.50\) and \(13.50\); 22 is flagged.)
- For the eight sorted values \(10,11,12,13,14,16,17,18\), calculate the type 7 quartiles and fences. Explain briefly why the lower fence need not be a possible observed value. (Answer: \(Q_1=11.75\), \(Q_3=16.25\), \(IQR=4.50\), fences \(5.00\) and \(23.00\); none flagged, and a fence is a calculated threshold rather than necessarily an observation.)
- In R, let
x <- c(2,5,6,7,8,9,10,19,NA). Calculate the fences after excluding the missing entry and state the result for 19 and forNA. (Answer: \(Q_1=5.75\), \(Q_3=9.25\), \(IQR=3.50\), fences \(0.50\) and \(14.50\); 19 is flagged andNAremains unclassified.) - A reviewer reports \(Q_1=11.75\) for eight sorted values whose second and third observations are 10 and 11, using the type 7 position \(h=2.75\). Check the interpolation, then state what must be recalculated before reporting flags. (Answer: \(Q_1=10+0.75(11-10)=10.75\); recalculate the IQR and both fences from the corrected quartile before deciding which observations are flagged.)