Tutorials › Exam PA - Predictive Analytics › Quantiles and Interquartile Range

Exam PA · Data Structures and Exploratory Summaries · Tutorial 113 of 966

Quantiles and Interquartile Range

Use R’s default sample-quantile method to calculate quartiles and the interquartile range, then interpret what those summaries say about a distribution.

Beginner 10 min read

What You'll Learn

  • Define sample quartiles and the interquartile range.
  • Calculate R’s default type 7 quantiles using interpolation between ordered observations.
  • Use quantile() and IQR() with explicit probabilities and missing-value handling.
  • Interpret quartiles and IQR in policy-age and hypothetical claim-cost examples.
  • Recognize why extreme observations may affect the range more than the IQR.
  • Avoid common errors involving rank-only calculations and missing values.

Why Quantiles and the IQR Matter

A mean or median gives one summary of a numeric distribution, but neither tells us directly how widely its central observations are spread. Quartiles add that information: they mark locations below which specified proportions of the data fall. The interquartile range (IQR) measures the distance between the first and third quartiles. Together, these summaries help describe the distribution of variables such as policy age or claim cost without letting one extreme observation dominate the measure of spread.

In R, quantile() calculates quantiles and IQR() calculates the difference between the third and first quartiles. The calculations depend on the chosen sample-quantile convention. R uses type 7 by default; this tutorial focuses on that method so that the hand calculations agree with the default function results.

Definition: For a numeric sample, the first quartile \(Q_1\) is the sample 25th percentile, the second quartile \(Q_2\) is the sample 50th percentile (the median), and the third quartile \(Q_3\) is the sample 75th percentile. The interquartile range is \(IQR=Q_3-Q_1\). Under R’s default type 7 method, quartiles can be interpolated between ordered observations rather than being required to equal observed values.

R’s Default: Type 7 Quantiles

To calculate a sample quantile, first order the \(n\) observed numeric values from smallest to largest. Write them as \(x_{(1)},x_{(2)},\ldots,x_{(n)}\), where the subscripts in parentheses indicate ranks in the ordered sample. For a requested proportion \(p\), type 7 locates the quantile at the fractional rank \(h=1+(n-1)p\). Here \(p\) is between 0 and 1: for example, \(p=0.25\) requests the first quartile.

$$ h=1+(n-1)p,\qquad j=\lfloor h\rfloor,\qquad \gamma=h-j $$

When \(0<p<1\) and \(h\) is not an integer, the type 7 quantile is a linear interpolation between the observations at ranks \(j\) and \(j+1\):

$$ Q(p)=(1-\gamma)x_{(j)}+\gamma x_{(j+1)} $$

If \(h\) is an integer, the quantile is \(x_{(h)}\). At the endpoints, \(Q(0)=x_{(1)}\) and \(Q(1)=x_{(n)}\). The method therefore returns the minimum and maximum at probabilities 0 and 1, and may return a value between observations for other probabilities.

Why the interpolation formula holds: The rank \(h=j+\gamma\) lies a fraction \(\gamma\) of the way from rank \(j\) to rank \(j+1\). Type 7 assigns the same fraction of the distance between the corresponding values: start at \(x_{(j)}\), then add \(\gamma\bigl(x_{(j+1)}-x_{(j)}\bigr)\). Expanding gives \(x_{(j)}+\gamma(x_{(j+1)}-x_{(j)})=(1-\gamma)x_{(j)}+\gamma x_{(j+1)}\), the stated formula.

A direct R call for the three quartiles is quantile(x, probs = c(0.25, 0.5, 0.75), type = 7). The default is already type = 7, but writing it explicitly can make the method clear when reviewing code. The IQR call is IQR(x, type = 7); it returns \(Q_3-Q_1\), not all three quartiles.

1
Check the input.
Confirm that the variable is numeric and that missing values are handled deliberately. Decide whether the requested summaries concern the observed nonmissing values.
2
Order the observations.
For a hand calculation, write the nonmissing values from smallest to largest and count \(n\).
3
Find the requested quantiles.
For each \(p\), calculate \(h=1+(n-1)p\), then use the observation at that rank or interpolate between adjacent ranks.
4
Calculate and interpret the IQR.
Subtract \(Q_1\) from \(Q_3\), retaining the variable’s units. Explain that the interval from \(Q_1\) to \(Q_3\) spans the middle half of the observations.

Worked Example: Quartiles of Policy Age

A hypothetical insurer reviews the ages, in years, of eight policies in a small sample: 2, 3, 4, 5, 7, 10, 16, and 30. Calculate the quartiles and IQR using R’s default type 7 method, and interpret the results. The data are already in increasing order.

Method. Use \(n=8\) and calculate \(h=1+(n-1)p\) for each requested probability.

First quartile. For \(p=0.25\), \(h=1+7(0.25)=2.75\). Thus \(j=2\) and \(\gamma=0.75\). The second and third observations are 3 and 4, so \(Q_1=(1-0.75)(3)+0.75(4)=0.25(3)+0.75(4)=0.75+3=3.75\) years.

Median. For \(p=0.50\), \(h=1+7(0.50)=4.5\). The fourth and fifth observations are 5 and 7, so \(Q_2=(1-0.5)(5)+0.5(7)=2.5+3.5=6\) years.

Third quartile. For \(p=0.75\), \(h=1+7(0.75)=6.25\). The sixth and seventh observations are 10 and 16, so \(Q_3=(1-0.25)(10)+0.25(16)=7.5+4=11.5\) years.

IQR and interpretation. \(IQR=11.5-3.75=7.75\) years. Under this sample-quantile convention, the first and third quartiles are 3.75 and 11.5 years; their difference describes a central spread of 7.75 years. The R calls are quantile(age, probs = c(0.25, 0.5, 0.75), type = 7) and IQR(age, type = 7).

Check and trap avoided. The calculation uses all eight ordered values and gives \(Q_1=3.75\), \(Q_3=11.5\), and \(IQR=7.75\). Selecting observations only by rounding \(np\) would instead pick 3 and 10 here, producing an IQR of 7. That rank-only calculation is not R’s default type 7 method.

Interpreting the Middle Half

The quartiles describe cut points in the ordered sample, not exact percentages of observations that must lie strictly below them. In particular, an interpolated quartile may not be an observed value, and ties can affect how many observations equal a quartile. A careful interpretation is that \(Q_1\) and \(Q_3\) mark the sample’s 25th- and 75th-percentile locations under the stated convention; the interval from \(Q_1\) to \(Q_3\) represents the middle half of the distribution in this percentile sense.

The IQR is expressed in the same units as the variable. An IQR of 7.75 years is a spread in years, while an IQR of 295 dollars is a spread in dollars. It is not a percentage, and it is not the distance from the minimum to the maximum. A larger IQR indicates a wider central spread when comparing the same variable measured on the same scale. Comparing IQRs across variables with different units or scales requires additional care.

Because the IQR depends on quartiles rather than the most extreme observations, it is often less affected by a very large or small value than the range is. That does not mean the IQR is unchanged by every unusual observation: in a small sample, an observation can affect the quartile ranks or interpolation. Nor does the IQR explain the entire shape of a distribution; it should be interpreted alongside the variable’s context and other summaries.

Worked Example: Claim-Cost Spread and an Extreme Value

Suppose a hypothetical review records seven claim costs, in dollars: 120, 180, 250, 310, 420, 600, and 1,500. Calculate the first and third quartiles and IQR using type 7. Then consider what happens if the largest cost is instead 3,000 dollars.

First quartile. With \(n=7\) and \(p=0.25\), \(h=1+6(0.25)=2.5\). Hence \(j=2\), \(\gamma=0.5\), and the adjacent observations are 180 and 250. Thus \(Q_1=0.5(180)+0.5(250)=90+125=215\) dollars.

Third quartile. With \(p=0.75\), \(h=1+6(0.75)=5.5\). The fifth and sixth observations are 420 and 600, so \(Q_3=0.5(420)+0.5(600)=210+300=510\) dollars.

IQR and comparison. The IQR is \(510-215=295\) dollars. Replacing 1,500 with 3,000 leaves the first six ordered observations, and therefore both quartile calculations, unchanged; the new IQR is still 295 dollars. The range does change: it increases from \(1{,}500-120=1{,}380\) dollars to \(3{,}000-120=2{,}880\) dollars. The R calls are quantile(claim_cost, probs = c(0.25, 0.75), type = 7) and IQR(claim_cost, type = 7).

Check and trap avoided. The arithmetic gives \(Q_1=215\), \(Q_3=510\), and IQR \(=295\) dollars in both versions of the data. Do not say the IQR always ignores extreme values; this example shows only that this particular change to the largest value does not alter these quartiles.

Missing Values and R Calls

By default, quantile() and IQR() do not remove missing values. If a numeric vector contains NA, a summary can return an error or missing result unless missing-value handling is specified. Setting na.rm = TRUE tells R to omit missing entries from that calculation. The resulting \(n\) is the number of nonmissing observations, so the type 7 ranks must be based on that count.

Omitting missing values is a computational choice, not a claim that the missing values are harmless. It does not fill them in, explain why they are missing, or establish that the remaining observations represent the intended population. The analyst should distinguish “calculate quartiles among observed values” from “estimate quartiles for all intended records.”

Worked Example: Computing Quartiles After Omitting Missing Costs

A hypothetical data extract contains claim costs in dollars recorded as 40, 55, 70, 80, 95, 110, 130, 160, 200, and NA. Find the quartiles and IQR among the observed costs, and identify the relevant R option.

Count and order. After omitting the one missing entry, \(n=9\), with ordered values 40, 55, 70, 80, 95, 110, 130, 160, and 200.

First quartile. \(h=1+(9-1)(0.25)=1+2=3\), an integer. Therefore \(Q_1=x_{(3)}=70\) dollars.

Third quartile. \(h=1+(9-1)(0.75)=1+6=7\), also an integer. Therefore \(Q_3=x_{(7)}=130\) dollars.

IQR and R. \(IQR=130-70=60\) dollars. Use quantile(claim_cost, probs = c(0.25, 0.75), na.rm = TRUE, type = 7) or IQR(claim_cost, na.rm = TRUE, type = 7). Both calculations use the nine observed values, not an imputed tenth value.

Check and trap avoided. The rank calculations use \(n=9\), yielding quartiles 70 and 130 and an IQR of 60 dollars. Setting na.rm = TRUE omits the missing entry; it does not replace it with zero or with an estimated cost.

Common Mistakes to Avoid

  • Using a different quartile convention without saying so. Sample-quantile definitions can differ. For calculations intended to match R’s default, use type 7 and state it when the convention matters.
  • Rounding the fractional rank and selecting one observation. Type 7 interpolates when the rank is fractional. Replacing interpolation with a rounded rank can change the answer.
  • Reporting the IQR without units or context. State both quartiles and the subtraction, then describe the result in the variable’s units.
  • Treating NA as zero. A missing value is not an observed zero. Use na.rm = TRUE only when the intended calculation is based on the nonmissing observations.
  • Calling the IQR the full spread. The IQR measures the gap between the first and third quartiles; it does not report the minimum, maximum, or full range.
Key takeaway: For R’s default type 7 method, calculate the fractional rank \(h=1+(n-1)p\), interpolate between adjacent ordered observations when needed, and compute \(IQR=Q_3-Q_1\). Interpret the quartiles and IQR in the original variable’s units, and make missing-value handling explicit.

Check Your Understanding

For each problem, use type 7 quantiles unless stated otherwise. Treat all monetary amounts as hypothetical.

  1. A policy-age sample, in years, is 1, 4, 6, 9, and 14. Find \(Q_1\), \(Q_3\), and the IQR. (Answer: \(Q_1=4\), \(Q_3=9\), IQR \(=5\) years.)
  2. Six hypothetical repair costs, in dollars, are 10, 20, 30, 50, 80, and 130. Calculate the type 7 quartiles and IQR, showing the fractional ranks. (Answer: \(h_{0.25}=2.25\), \(Q_1=22.50\); \(h_{0.75}=4.75\), \(Q_3=72.50\); IQR \(=50.00\) dollars.)
  3. Seven hypothetical claim severities, in thousands of dollars, are 12, 18, 25, 40, 55, 90, and 150. Find the two quartiles and the IQR. (Answer: \(Q_1=21.5\), \(Q_3=72.5\), IQR \(=51\) thousand dollars.)
  4. A numeric vector contains 5, 10, 20, 35, 60, 100, NA, and 140. Find the quartiles and IQR among nonmissing values, and state the R option needed. (Answer: use \(na.rm=TRUE\); \(Q_1=15\), \(Q_3=80\), IQR \(=65\).)
  5. Seven hypothetical annual costs are 4, 6, 8, 10, 12, 14, and 16. Recalculate the IQR if the largest value is changed to 1,000. Explain whether the IQR changes. (Answer: both versions have \(Q_1=7\), \(Q_3=13\), and IQR \(=6\); the IQR does not change.)
Clinical Biostats