Descriptive statistics — ME1 exam-style exercises with the campus bus: tables with cut, the interpolated median and the variance of several groups (and the median that escaped from its class)

In an ME1 mock test in class I got a median of 3.03 minutes in a class going from 4 to 8, and a median outside its own class is impossible. I did not notice at the time. I had the bus company’s waiting-time table, found the median class by looking at the cumulative frequencies and put into the formula the cumulative count of that same class instead of the ones before it. The subtraction came out negative, the median ended up below the lower limit and I calmly wrote it in the answer box. The lecturer circled it in red with a single word: “Where?”. Since then, every time I interpolate, I check that the result falls inside its class.

This article is the training for the descriptive statistics part of the first continuous assessment test (EC1) of Statistical Methods I: three exercises with the same context (the campus bus), with chained parts that mix R and paper, the marks for each part and a bonus, as in the exam. Before the solutions you have the statements on their own, so you can try them. If you are missing some theory, go back to the theory, the proofs or the R practice.

How to use this article

  1. Do the exercises as in the exam: RStudio (or the R playground) open, an .R script with the code for each part, paper for the calculations and proofs, and the results with 2 decimals.
  2. Time yourself: the EC1 is 4 hours for 4 exercises worth 50 points in total. These three are worth 35, so about 2 h 45 min is a good pace.
  3. Mark yourself with the solutions, part by part. Each one says how the points are split (it is my guide split, not the official one).
  4. Finish with the cheat sheet and the script: the cheat sheet has results only, what you can take to the EC1 on your handwritten sheet; the script has all the code of the topic for your exam .R file.

In the ME1 EC1 you can bring a handwritten cheat sheet of one sheet, both sides (results yes, proofs no), and at the end you upload the .R file with the code of each exercise: without it, that exercise is not marked. If you want to know how much you need in the EC2 depending on what you get here, use the ME1 grade calculator.

Exercise 1: the journey from campus to Santa Catalina (10 points)

For a month I timed with my phone how long the bus took from the campus stop to Santa Catalina. These are the 36 durations, in minutes:

journey <- c(38.4, 31.4, 28.9, 27.3, 34.0, 40.7, 35.6, 37.6, 39.8, 31.8,
             44.6, 31.8, 37.1, 43.9, 35.2, 36.0, 33.4, 36.9, 26.6, 32.8,
             42.6, 36.4, 34.5, 37.1, 37.2, 36.1, 37.9, 28.5, 38.0, 32.7,
             38.8, 35.4, 31.3, 35.2, 52.4, 58.6)

(a) Using R, compute the mean and the median of the journey time. (2 points)

(b) Using R, build the frequency table (absolute, relative and cumulative relative frequencies) with the classes (25, 30], (30, 35], (35, 40], (40, 45] and (45, 60]. (3 points)

(c) From the table in the previous part, compute the mean, the standard deviation and the median with the grouped data. Compare with the results obtained with the original data and comment. (3 points)

(d) Detect the possible outliers with the 1.5 1.5 times interquartile range rule and interpret the result in the context of the problem. (2 points)

Exercise 2: the company’s monthly report (10 points)

The bus company publishes a monthly report with the waits recorded at the campus stop, but it only gives the grouped table:

Wait (minutes)[0, 4)[4, 8)[8, 12)[12, 20)[20, 30)
Number of waits3658312312

(a) Complete the table with the class marks, the relative frequencies and the cumulative relative frequencies. (2 points)

(b) Compute by hand, with the shortcut formula, the mean and the standard deviation of the waits. (3 points)

(c) Compute by interpolation the median and the 90th percentile of the waits. Interpret both values. (3 points)

(d) Assuming the waits are spread uniformly within each class, estimate the percentage of waits longer than 15 minutes. (2 points)

Exercise 3: three time slots, one summary (15 points)

A classmate gave me the summary of their waits over the term by time slot, but not the data:

SlotNumber of waits nj n_j Mean xˉj \bar{x}_j (min)Variance sj2 s_j^2 (min²)
Morning2510.412.25
Midday205.64.41
Evening159.816.00

(a) Compute the sample mean and variance of the 60 waits together. (5 points)

(b) Prove that, for k k groups of sizes nj n_j and means xˉj \bar{x}_j , the mean of all the data is xˉ=∑j=1knjxˉj/n \bar{x} = \sum_{j=1}^{k} n_j\bar{x}_j / n , with n=∑j=1knj n = \sum_{j=1}^{k} n_j . (3 points)

(c) Because of roadworks, every wait next term gets 1.5 minutes longer, and the company reports the results in seconds. Assuming nothing else changes, compute the mean, the variance and the standard deviation of the 60 transformed waits, justifying the properties you use. (4 points)

(d) The company wants to audit 40 of the term’s 1200 trips with a systematic sample. Compute the step K K and, if the random number is 17, give the first five trips in the sample and the last one. (3 points)

Bonus (2.5 points)

Prove that the sample variance s2 s^2 is 0 if and only if all the data are equal, and interpret the result with the waits of a bus.

Solutions

Exercise 1

(a) With mean and median, rounding to 2 decimals:

round(c(mean = mean(journey), median = median(journey)), 2)

Output:

  mean median 
 36.57  36.05 

The journey takes 36.57 minutes on average and half the days it takes 36.05 minutes or less. The mean is half a minute above the median: some long journey pulls it up (we will see it in part d).

How it is marked: 1 point for each value with the code that computes it.

(b) cut with the class breaks, table for the absolute frequencies, prop.table for the relative ones and cumsum for the cumulative ones. I put it all in a data frame:

breaks <- c(25, 30, 35, 40, 45, 60)
f <- table(cut(journey, breaks))
sum(f)                                  # check: 36
freq_table <- data.frame(class = names(f), f = as.vector(f),
                         fr = round(as.vector(f) / 36, 2),
                         Fr = round(cumsum(as.vector(f)) / 36, 2))
freq_table

Output:

[1] 36
    class  f   fr   Fr
1 (25,30]  4 0.11 0.11
2 (30,35]  9 0.25 0.36
3 (35,40] 17 0.47 0.83
4 (40,45]  4 0.11 0.94
5 (45,60]  2 0.06 1.00

The frequencies add up to 36: nothing has been left out. Almost half of the journeys (17, that is 47%) last between 35 and 40 minutes, and 83% last 40 minutes or less (Fr of the third class).

How it is marked: 1 point for the absolute table, 1 for the relative frequencies and 1 for the cumulative ones.

How to approach (c), before the calculation. Grouping means acting as if you only had the table: each journey takes the value of its class mark. The mean and the standard deviation come from there.

The median needs something more. The obvious route, median() on the repeated marks, gives 37.5: journeys 18 and 19 both fall in the third class, which only tells you which class it is in. Interpolation sharpens it: it assumes the 17 journeys in that class are spread evenly over its 5 minutes. Before it you already have 13; to reach 18 you need 5 of those 17, so you move 517⋅5=1.47 \frac{5}{17} \cdot 5 = 1.47 minutes on from 35.

(c) The class marks are the midpoints: 27.5, 32.5, 37.5, 42.5 and 52.5 (the last class is wider: (45+60)/2 (45 + 60)/2 ). The median class is the first whose cumulative count reaches n/2=18 n/2 = 18 : the cumulative counts are 4, 13, 30…, so it is the third, (35, 40], with L=35 L = 35 , w=5 w = 5 , fm=17 f_m = 17 and Fb=13 F_b = 13 :

M=35+517 (18−13)=35+1.47=36.47 M = 35 + \frac{5}{17}\,(18 – 13) = 35 + 1.47 = 36.47
fa <- as.vector(f); m <- c(27.5, 32.5, 37.5, 42.5, 52.5); n <- sum(fa)
mean_gr <- sum(fa * m) / n
s_gr <- sqrt(sum(fa * (m - mean_gr)^2) / (n - 1))
M_gr <- 35 + 5 / 17 * (n / 2 - 13)
round(rbind(grouped  = c(mean = mean_gr, s = s_gr, median = M_gr),
            original = c(mean(journey), sd(journey), median(journey))), 2)

Output:

          mean    s median
grouped  36.53 5.71  36.47
original 36.57 6.32  36.05

Comment: the grouped mean and median (36.53 and 36.47) almost match those of the data (36.57 and 36.05): the central classes are narrow and the data are well spread inside them. The standard deviation, however, drops from 6.32 to 5.71, because in the wide class (45, 60] the two journeys (52.4 and 58.6) both become 52.5: the longest one “moves closer” to the rest and the spread goes down. Grouped measures are approximations: information is lost when each value is replaced by its mark.

How it is marked: 1 point for the grouped mean and standard deviation, 1 for the interpolated median and 1 for the reasoned comparison (saying “they are close” is not enough: you have to explain why they do not match).

Why quartiles and not mean ± 2 standard deviations. To judge an odd value you need a yardstick that value cannot stretch. The mean and the standard deviation are computed with all the journeys, including 52.4 and 58.6, so the outliers pull them in their favour. Q1 Q_1 and Q3 Q_3 only depend on the journeys in the middle: it makes no difference whether the longest one takes 58.6 or 90 minutes. The rule says “possible outlier”; the explanation (a traffic jam) comes from the context.

(d) The fences, with the quartiles from quantile and the IQR:

q <- quantile(journey, c(0.25, 0.75)); IQR_j <- IQR(journey)
q; IQR_j
fences <- unname(c(q[1] - 1.5 * IQR_j, q[2] + 1.5 * IQR_j))
round(fences, 2)
journey[journey < fences[1] | journey > fences[2]]
round(c(mean_without = mean(journey[journey < 50]),
        median_without = median(journey[journey < 50])), 2)

Output:

   25%    75% 
32.775 38.100 
[1] 5.325
[1] 24.79 46.09
[1] 52.4 58.6
  mean_without median_without 
         35.46          35.80 

Q1=32.775 Q_1 = 32.775 , Q3=38.1 Q_3 = 38.1 and IQR=5.325 IQR = 5.325 . The fences are at 24.79 and 46.09 minutes: below, no journey falls outside, and above, 52.4 and 58.6 are possible outliers. Interpretation: they are two days when the journey took between 8 and 14 minutes longer than the longest of the “normal” ones (44.6): a traffic jam or an accident, not the usual running of the line. Without them, the mean drops to 35.46 and the median to 35.8: they are what separates the mean from the median in part a.

How it is marked: 1 point for the correctly computed fences and the two outliers; 1 for the interpretation in context.

Boxplot of the 36 journeysHorizontal boxplot of the journeys: left whisker at 26.6, box from 32.75 to 38.2 with the median at 36.05, right whisker up to 44.6 and two red circles at 52.4 and 58.6. A dashed line marks the boxplot fence at 46.375.2530354045505560journey minutesfence 46.375end of whisker: 44.6maximum: 58.652.4median 36.05
The whisker does not reach the maximum: it stops at 44.6, and the maximum (58.6) is the highest loose point.

Exercise 2

(a) Mark = midpoint; relative = fi/160 f_i / 160 ; cumulative = sum of the relative frequencies up to that class:

ClassMark mi m_i fi f_i fi/n f_i / n Cumulative
[0, 4)2360.2250.225
[4, 8)6580.36250.5875
[8, 12)10310.193750.78125
[12, 20)16230.143750.925
[20, 30)25120.0751
Total1601

How it is marked: 0.5 points for the marks (watch the last two: 16 and 25), 0.75 for the relative frequencies and 0.75 for the cumulative ones.

Why the shortcut formula. The definition forces you to subtract 8.7375 from every mark and square five numbers with decimals. The shortcut, s2=1n−1(∑fimi2−(∑fimi)2n) s^2 = \frac{1}{n-1}\left(\sum f_i m_i^2 – \frac{(\sum f_i m_i)^2}{n}\right) , only needs two sums of whole numbers and leaves the division for the end.

Plugging the rounded mean into the shortcut. With 160⋅8.742=12222.02 160 \cdot 8.74^2 = 12222.02 instead of 13982/160=12215.025 1398^2/160 = 12215.025 you get s2=40.87 s^2 = 40.87 and not 40.91. Subtracting two large, similar numbers magnifies any rounding.

(b) The two sums the shortcut formula needs:

∑fimi=36⋅2+58⋅6+31⋅10+23⋅16+12⋅25=72+348+310+368+300=1398 \sum f_i m_i = 36 \cdot 2 + 58 \cdot 6 + 31 \cdot 10 + 23 \cdot 16 + 12 \cdot 25 = 72 + 348 + 310 + 368 + 300 = 1398
∑fimi2=36⋅4+58⋅36+31⋅100+23⋅256+12⋅625=144+2088+3100+5888+7500=18720 \sum f_i m_i^2 = 36 \cdot 4 + 58 \cdot 36 + 31 \cdot 100 + 23 \cdot 256 + 12 \cdot 625 = 144 + 2088 + 3100 + 5888 + 7500 = 18720
xˉ=1398160=8.74s2=1159(18720−13982160)=18720−12215.025159=40.91s=40.91=6.40 \bar{x} = \frac{1398}{160} = 8.74 \qquad s^2 = \frac{1}{159}\left(18720 – \frac{1398^2}{160}\right) = \frac{18720 – 12215.025}{159} = 40.91 \qquad s = \sqrt{40.91} = 6.40

The mean wait is 8.74 minutes (8.7375 unrounded) and the standard deviation 6.40 minutes.

How it is marked: 1 point for the mean, 1.5 for the variance with the shortcut formula (with n−1=159 n – 1 = 159 ) and 0.5 for the standard deviation.

How to approach (c). The median is the wait that leaves 80 below it. After [0, 4) you have 36; after [4, 8), 94: wait number 80 is inside [4, 8). You already have the 36 before it (that is Fb F_b ); you need 80−36=44 80 – 36 = 44 of the 58 in the class, and spread evenly over its 4 minutes, you move 4458⋅4=3.03 \frac{44}{58} \cdot 4 = 3.03 on from 4. Fb F_b is always what you have already left behind, never the cumulative count of the class you are in: that was my mistake in the mock test.

Check you’ve got it: what is the 25th percentile of the waits?

About 4.28 minutes. 25% of 160 is 40 waits; 36 does not reach it and 94 does, so the percentile is in [4, 8), with Fb=36 F_b = 36 : P25=4+458 (40−36)=4+0.28=4.28 P_{25} = 4 + \frac{4}{58}\,(40 – 36) = 4 + 0.28 = 4.28 . It falls between 4 and 8, as it must: one wait in four lasted 4.28 minutes or less.

(c) Absolute cumulative counts: 36, 94, 125, 148 and 160. The median leaves 80 waits on each side (n/2=80 n/2 = 80 ); the first class accumulates 36 and the second reaches 94, so it is in [4, 8): L=4 L = 4 , w=4 w = 4 , fm=58 f_m = 58 , Fb=36 F_b = 36 .

M=4+458 (80−36)=4+3.03=7.03 M = 4 + \frac{4}{58}\,(80 – 36) = 4 + 3.03 = 7.03

The 90th percentile follows the same idea with 0.9⋅160=144 0.9 \cdot 160 = 144 instead of n/2 n/2 . The third class accumulates 125 and the fourth reaches 148, so it is in [12, 20): L=12 L = 12 , w=8 w = 8 , f=23 f = 23 , Fb=125 F_b = 125 .

P90=12+823 (144−125)=12+6.61=18.61 P_{90} = 12 + \frac{8}{23}\,(144 – 125) = 12 + 6.61 = 18.61

Interpretation: half of the month’s waits lasted 7.03 minutes or less, and 90% of them, 18.61 minutes or less (only one in ten went past 18.61). Both fall inside their class, which is the check I was missing in the story at the top.

Median and 90th percentile of the company reportCumulative frequency polygon of the exercise 2 table: 36 at 4, 94 at 8, 125 at 12, 148 at 20 and 160 at 30 minutes. The line at 80 crosses at 7.03 (the median) and the line at 144 crosses at 18.61 (the 90th percentile).0481216202428minutes04080120160cumulative3694125148160M = 7.0380P90 = 18.61144
Each interpolated value lies inside its segment: 7.03 between 4 and 8; 18.61 between 12 and 20.

How it is marked: 1.5 points for each value (1 for the calculation and 0.5 for the interpretation).

Why the class has to be split. 15 falls in the middle of [12, 20), and the two obvious routes both fail. Counting the whole class (23 + 12 = 35 waits) gives 21.88%: it counts waits of 13 or 14 minutes as “over 15”. Ignoring it (only the 12 in [20, 30)) gives 7.5%. The even spread stated in the question is what lets you keep just the right part: 5 of its 8 minutes are above 15.

(d) From 15 to 20 minutes there is 5/8 5/8 of the class [12, 20). If its 23 waits are spread uniformly, 23⋅5/8=14.375 23 \cdot 5/8 = 14.375 are longer than 15 minutes. Adding the 12 in the last class:

14.375+12160⋅100=26.375160⋅100=16.48% \frac{14.375 + 12}{160} \cdot 100 = \frac{26.375}{160} \cdot 100 = 16.48\%

Around 16.48% of the waits were longer than 15 minutes.

How it is marked: 1 point for the proportional part of the class [12, 20) and 1 for the final percentage.

The whole exercise, checked with R:

fn <- c(36, 58, 31, 23, 12); mn <- c(2, 6, 10, 16, 25); nn <- sum(fn)
xn <- sum(fn * mn) / nn
round(c(mean = xn, s2 = (sum(fn * mn^2) - nn * xn^2) / (nn - 1)), 2)
cumsum(fn)
round(c(median = 4 + 4 / 58 * (nn / 2 - 36),
        p90 = 12 + 8 / 23 * (0.9 * nn - 125),
        over_15 = (23 * 5 / 8 + 12) / nn * 100), 2)

Output:

 mean    s2 
 8.74 40.91 
[1]  36  94 125 148 160
 median     p90 over_15 
   7.03   18.61   16.48 

Exercise 3

How to approach it, before the calculation. There are no data, only three summaries, and the obvious thing is to average them: 8.6 for the mean and (12.25+4.41+16)/3=10.89 (12.25 + 4.41 + 16)/3 = 10.89 for the variance. Both fail. The morning has 25 waits and midday 20: it has to weigh more. And the spread of the 60 waits has two parts: how much they vary within each time slot (you recover it with (nj−1)sj2 (n_j – 1)s_j^2 ) and how far each slot is from the overall mean. Averaging variances forgets the second part, and that is why 10.89 is so far from 14.99.

Check you’ve got it: if the three time slots had the same mean, 8.65, what would the variance of the 60 waits be?

601.79/59=10.20 601.79/59 = 10.20 . If all the means equal the overall one, every xˉj−xˉ \bar{x}_j – \bar{x} is 0 and the between part disappears. The gap from 10.20 to 14.99 is what midday being much faster than the other two slots contributes.

(a) With n=25+20+15=60 n = 25 + 20 + 15 = 60 , the mean is the weighted mean of the means:

xˉ=25⋅10.4+20⋅5.6+15⋅9.860=260+112+14760=51960=8.65 \bar{x} = \frac{25 \cdot 10.4 + 20 \cdot 5.6 + 15 \cdot 9.8}{60} = \frac{260 + 112 + 147}{60} = \frac{519}{60} = 8.65

And the variance, within + between (proof 6):

∑(nj−1)sj2=24⋅12.25+19⋅4.41+14⋅16=294+83.79+224=601.79 \sum (n_j – 1)s_j^2 = 24 \cdot 12.25 + 19 \cdot 4.41 + 14 \cdot 16 = 294 + 83.79 + 224 = 601.79
∑nj(xˉj−xˉ)2=25⋅1.752+20⋅(−3.05)2+15⋅1.152=76.5625+186.05+19.8375=282.45 \sum n_j(\bar{x}_j – \bar{x})^2 = 25 \cdot 1.75^2 + 20 \cdot (-3.05)^2 + 15 \cdot 1.15^2 = 76.5625 + 186.05 + 19.8375 = 282.45
s2=601.79+282.4559=884.2459=14.99 s^2 = \frac{601.79 + 282.45}{59} = \frac{884.24}{59} = 14.99
nj <- c(25, 20, 15); mj <- c(10.4, 5.6, 9.8); vj <- c(12.25, 4.41, 16.00)
xbar <- sum(nj * mj) / sum(nj)
s2 <- (sum((nj - 1) * vj) + sum(nj * (mj - xbar)^2)) / (sum(nj) - 1)
round(c(mean = xbar, variance = s2, s = sqrt(s2)), 2)

Output:

    mean variance        s 
    8.65    14.99     3.87 

The 60 waits have a mean of 8.65 minutes and a variance of 14.99 min² (standard deviation 3.87). Notice the mean is not (10.4+5.6+9.8)/3=8.6 (10.4 + 5.6 + 9.8)/3 = 8.6 : the morning has more waits and weighs more.

How it is marked: 2 points for the mean (1 if it is the simple mean) and 3 for the variance (1.5 for the within part, 1.5 for the between part).

(b) Let xij x_{ij} be wait i i of group j j . The sum of all the data splits by groups (S1) and, within each group, the sum is njxˉj n_j\bar{x}_j by the definition of its mean:

xˉ=1n∑j=1k∑i=1njxij=1n∑j=1knjxˉj=∑j=1knjxˉj∑j=1knj \bar{x} = \frac{1}{n}\sum_{j=1}^{k}\sum_{i=1}^{n_j} x_{ij} = \frac{1}{n}\sum_{j=1}^{k} n_j\bar{x}_j = \frac{\sum_{j=1}^{k} n_j\bar{x}_j}{\sum_{j=1}^{k} n_j}

How it is marked: 1 point for splitting the sum by groups, 1.5 for using ∑ixij=njxˉj \sum_i x_{ij} = n_j\bar{x}_j and 0.5 for the conclusion.

Write the transformation before calculating anything. The order matters: first 1.5 minutes are added and then everything is converted to seconds, so y=60 (x+1.5) y = 60\,(x + 1.5) . If you write 60x+1.5 60x + 1.5 , you are adding 1.5 seconds, and the mean would come out as 520.5 instead of 609.

(c) The transformation is y=60 (x+1.5)=90+60 x y = 60\,(x + 1.5) = 90 + 60\,x , that is, a=90 a = 90 and b=60 b = 60 . By the change of origin and scale (proof 3): yˉ=a+bxˉ \bar{y} = a + b\bar{x} , sy2=b2sx2 s_y^2 = b^2 s_x^2 and sy=∣b∣ sx s_y = |b|\,s_x , because when the mean is subtracted the constant a a cancels out: yi−yˉ=b(xi−xˉ) y_i – \bar{y} = b(x_i – \bar{x}) .

yˉ=90+60⋅8.65=609 ssy2=3600⋅14.98712=53 953.63 s2sy=60⋅3.8713=232.28 s \bar{y} = 90 + 60 \cdot 8.65 = 609 \text{ s} \qquad s_y^2 = 3600 \cdot 14.98712 = 53\,953.63 \text{ s}^2 \qquad s_y = 60 \cdot 3.8713 = 232.28 \text{ s}

(With the unrounded variance, 14.98712…; with the rounded one, 14.99, you would get 53,964, which is accepted in the exam if you say so.) The roadworks’ minute and a half raises the mean but does not change the spread; converting to seconds multiplies the mean and the deviation by 60 and the variance by 3600.

round(c(mean_s = 90 + 60 * xbar, var_s = 60^2 * s2, s_s = 60 * sqrt(s2)), 2)

Output:

  mean_s    var_s      s_s 
  609.00 53953.63   232.28 

How it is marked: 1 point for writing the transformation, 1 for each result and 1 for the justification (adding a constant does not change the variance).

(d) K=N/n=1200/40=30 K = N/n = 1200/40 = 30 . Starting at 17 and going up 30 at a time: trips 17, 47, 77, 107 and 137. The last one is 17+39⋅30=1187 17 + 39 \cdot 30 = 1187 (40 trips are 39 jumps).

How it is marked: 1 point for K K , 1 for the first five and 1 for the last one.

Bonus

If all the data are equal, xi=c x_i = c for every i i , the mean is c c and every deviation xi−xˉ x_i – \bar{x} is 0, so s2=0 s^2 = 0 . Conversely: if s2=0 s^2 = 0 , then ∑(xi−xˉ)2=0 \sum (x_i – \bar{x})^2 = 0 . It is a sum of terms that are never negative (they are squares), and such a sum can only be 0 if every term is 0: (xi−xˉ)2=0 (x_i – \bar{x})^2 = 0 , that is, xi=xˉ x_i = \bar{x} for every i i . All the data are equal to the mean.

Interpretation: a variance of 0 would be a perfectly regular bus, one that always makes you wait the same. As soon as a single wait is different, s2>0 s^2 > 0 .

How it is marked: 1 point for each implication and 0.5 for the interpretation.

What’s wrong here?

Three solutions from classmates with one mistake each. Before reading the explanation, try to find it.

Snippet 1. For part 1(c), a classmate computes the variance of the original data “with the formula” and gets something different from R:

variance <- sum((journey - mean(journey))^2) / length(journey)
round(c(their_variance = variance, R_var = var(journey)), 2)

Output:

their_variance          R_var 
         38.84          39.95 

They divide by n=36 n = 36 instead of n−1=35 n – 1 = 35 . Their result is 3536 \frac{35}{36} of the right one (39.95⋅35/36=38.84 39.95 \cdot 35/36 = 38.84 ).

The ME1 sample variance divides by n−1 n – 1 . If one of your calculations does not match var(), check the denominator before anything else.

Snippet 2. For the median in exercise 2, another solution:

The median is in the class [4, 8), which accumulates 94.
M = 4 + 4/58 · (80 − 94) = 4 − 0.97 = 3.03 minutes.

They used as Fb F_b the cumulative count of the class itself (94) instead of the ones before it (36). The symptom is clear: 3.03 is outside [4, 8). An interpolated median always falls inside its class; with Fb=36 F_b = 36 you get 7.03.

After interpolating, check that L≤M≤L+w L \le M \le L + w . If not, the mistake is in Fb F_b or in L L .

Snippet 3. In part 1(d), someone reads the boxplot like this:

According to the boxplot, the longest journey was 44.6 minutes,
which is where the upper whisker ends. There are no outliers.

The upper whisker does not reach the maximum: it reaches the largest value that is still inside the fence (46.09), which is 44.6. The two loose points above it, 52.4 and 58.6, are the possible outliers, and the maximum is 58.6. Look at the drawing in part (d).

Confusing the end of the whisker with the maximum. If there are loose points, the maximum is the highest of them.

Practice quiz

Before moving on to the next topic, check you have got it. This quiz is here to consolidate what this article explains: the ME1 exam is not a multiple-choice test (you solve problems by hand and with R), so no mark here counts. There are 10 questions on descriptive statistics: key ideas, typical mistakes and many «what does this R code print?», with the outputs from really running it. In the short answers, a decimal point or a decimal comma both work. Answer without running anything, press Check and read the explanation for each option; you can repeat it as many times as you like.

Question 1Single choice

A classmate works out the variance of 36 journeys as sum((x - mean(x))^2) / length(x) and gets 38.84, while var(x) gives 39.95. What has gone wrong?

  1. They divided by n = 36, and the ME1 sample variance divides by n − 1 = 35
  2. They subtracted the median instead of the mean
  3. R’s var() is wrong: trust the formula
  4. The brackets of the square are missing
Solution

Answer: They divided by n = 36, and the ME1 sample variance divides by n − 1 = 35

If one of your calculations does not match var(), check the denominator first: in ME1 the variance always divides by n − 1.

Question 2Short answer

What number does it print? (Rounded to 2 decimals, as in the exam.)

x <- c(12, 15, 15, 18, 20)
round(var(x), 2)
Solution

Answer: 9.5

Mean = 80 / 5 = 16. Squared deviations: (12 − 16)² = 16, (15 − 16)² = 1 twice, (18 − 16)² = 4 and (20 − 16)² = 16; they add up to 38. Divided by n − 1 = 4: 38 / 4 = 9.5. Dividing by 5 would give 7.6, which is not the ME1 variance.

Question 3Single choice

In a table with classes [0, 4), [4, 8), [8, 12)… and frequencies 36, 58, 31…, a solution says: «the median is in [4, 8), which accumulates 94; M = 4 + 4/58 · (80 − 94) = 3.03». Where is the mistake?

  1. It uses the class’s own cumulative frequency (94) instead of that of the previous classes (36)
  2. The median is in the class [0, 4)
  3. The class width is 8, not 4
  4. With grouped data the median is not interpolated
Solution

Answer: It uses the class’s own cumulative frequency (94) instead of that of the previous classes (36)

After interpolating, check that L ≤ M ≤ L + w: an interpolated median always falls inside its class. If not, the mistake is in the previous cumulative frequency or in L.

Question 4Single choice

In a box plot, the upper whisker ends at 44.6 and above it there are two loose points, 52.4 and 58.6. What is the maximum of the data?

  1. 58.6
  2. 44.6
  3. 46.09
  4. 52.4
Solution

Answer: 58.6

Mistaking the end of the whisker for the maximum is snippet 3 of «What’s wrong here?». If there are loose points, the maximum is the highest of them.

Question 5Short answer

With Q1 = 32.77 and Q3 = 38.10, what is the upper fence for outliers? (2 decimals.)

Q1 <- 32.77; Q3 <- 38.10
IQR_ <- Q3 - Q1
round(Q3 + 1.5 * IQR_, 2)
Solution

Answer: 46.09

IQR = 38.10 − 32.77 = 5.33. Upper fence = Q3 + 1.5 · IQR = 38.10 + 7.995 = 46.095 → 46.09 with R’s rounding (it is the one in exercise 1). Any value above it is a possible outlier.

Question 6Short answer

Waiting times have mean 8.65 and standard deviation 3.87 (minutes). If they are converted to seconds and a fixed 90 s is added, y = 90 + 60x, what is the standard deviation of y?

s <- 3.87
round(abs(60) * s, 2)
Solution

Answer: 232.2

With y = a + b·x, the mean is transformed in full (a + b·mean) but the spread only feels the scale: s_y = |b| · s_x = 60 · 3.87 = 232.2 s. The added 90 disappears.

Question 7Single choice

Three groups have n = 25, 20 and 15 and means 10.4, 5.6 and 9.8. What is the mean of all of them together?

  1. 8.65
  2. 8.6
  3. 9.8
  4. It cannot be known without the original data
Solution

Answer: 8.65

The pooled variance does not need the detail either: s² = [Σ (n_j − 1)s_j² + Σ n_j(mean_j − mean)²] / (n − 1). It is the groups() function in «For your script».

Question 8Match

Match each R function with what it returns:

var(x)
IQR(x)
quantile(x, 0.25)
table(cut(x, breaks))

Definitions:

  • The first quartile
  • The mode of the data
  • The interquartile range, Q3 − Q1
  • The frequency of each class
  • The sample variance (divides by n − 1)
Solution
  • var(x) → The sample variance (divides by n − 1)
  • IQR(x) → The interquartile range, Q3 − Q1
  • quantile(x, 0.25) → The first quartile
  • table(cut(x, breaks)) → The frequency of each class

R has no mode function for numeric data. cut splits the data into classes and table counts them; always check that the frequencies add up to n.

Question 9True or false

True or false?

  1. The sum of the deviations from the mean, Σ(x_i − mean), is 0.
  2. If the mean and the median are far apart, there is skewness or outliers.
  3. Adding a constant to all the data changes their variance.
  4. With bell-shaped data, about 95 % falls within mean ± 2s.
Solution
  • T — The sum of the deviations from the mean, Σ(x_i − mean), is 0.
  • T — If the mean and the median are far apart, there is skewness or outliers.
  • F — Adding a constant to all the data changes their variance.
  • T — With bell-shaped data, about 95 % falls within mean ± 2s.

Adding a constant moves all the data equally: the mean changes, but the distances between values do not, so neither does the variance. The empirical rule is 68 %, 95 % and 99.7 % for 1, 2 and 3 standard deviations.

Question 10Short answer

Systematic sampling: N = 1200 journeys, n = 40 in the sample and you start at r = 17. Which journey is the third in the sample?

N <- 1200; n <- 40; r <- 17
K <- N / n
r + K * 2
Solution

Answer: 77

K = N / n = 30. The sample is r, r + K, r + 2K…: 17, 47, 77. The third is 77.

Try it in the R playground

Open the R playground and paste this script to do exercise 1 yourself with other classes:

journey <- c(38.4, 31.4, 28.9, 27.3, 34.0, 40.7, 35.6, 37.6, 39.8, 31.8,
             44.6, 31.8, 37.1, 43.9, 35.2, 36.0, 33.4, 36.9, 26.6, 32.8,
             42.6, 36.4, 34.5, 37.1, 37.2, 36.1, 37.9, 28.5, 38.0, 32.7,
             38.8, 35.4, 31.3, 35.2, 52.4, 58.6)
summary(journey)
boxplot(journey, horizontal = TRUE)
f <- table(cut(journey, seq(25, 60, by = 5)))
f
hist(journey, breaks = seq(25, 60, by = 5))

What to look at:

  1. summary already gives you almost all of part (a) and the quartiles for (d).
  2. In Plots, the boxplot: the whisker ends at 44.6 and there are two loose points.
  3. With classes of 5 up to 60, the table has 7 classes: which ones have 0 values?
  4. In the equal-class histogram, the vertical axis is Frequency: compare it with breaks = c(25, 30, 35, 40, 45, 60).

Cheat sheet: descriptive statistics

Results only, no proofs: it is what you can take to the EC1.

The essentials in 30 seconds: mean and median for the center (if they drift apart, there is skewness or outliers), s s and IQR IQR for the spread, fences Q1−1.5 IQR Q_1 – 1.5\,IQR and Q3+1.5 IQR Q_3 + 1.5\,IQR for outliers, and with tables, class marks and interpolation. Variance always with n−1 n – 1 . Results with 2 decimals.

MeasureDataGrouped data
Meanxˉ=1n∑xi \bar{x} = \frac{1}{n}\sum x_i xˉ=1n∑fimi \bar{x} = \frac{1}{n}\sum f_i m_i
Variances2=∑xi2−nxˉ2n−1 s^2 = \frac{\sum x_i^2 – n\bar{x}^2}{n-1} s2=∑fimi2−nxˉ2n−1 s^2 = \frac{\sum f_i m_i^2 – n\bar{x}^2}{n-1}
MedianCentral value (mean of the two central ones if n n is even)M=L+wfm(n2−Fb) M = L + \frac{w}{f_m}\left(\frac{n}{2} – F_b\right)
Percentile p p quantile(x, p)P=L+wf (p n−Fb) P = L + \frac{w}{f}\,(p\,n – F_b)

Other results:

  • ∑(xi−xˉ)=0 \sum (x_i – \bar{x}) = 0 ; ∑fi(mi−xˉ)=0 \sum f_i(m_i – \bar{x}) = 0 .
  • If y=a+bx y = a + bx : yˉ=a+bxˉ \bar{y} = a + b\bar{x} , sy2=b2sx2 s_y^2 = b^2 s_x^2 , sy=∣b∣ sx s_y = |b|\,s_x .
  • Several groups: xˉ=∑njxˉjn \bar{x} = \frac{\sum n_j\bar{x}_j}{n} , s2=∑(nj−1)sj2+∑nj(xˉj−xˉ)2n−1 s^2 = \frac{\sum (n_j – 1)s_j^2 + \sum n_j(\bar{x}_j – \bar{x})^2}{n-1} .
  • ∑(xi−a)2=∑(xi−xˉ)2+n(xˉ−a)2 \sum (x_i – a)^2 = \sum (x_i – \bar{x})^2 + n(\bar{x} – a)^2 : minimum at a=xˉ a = \bar{x} .
  • Empirical rule (bell-shaped data): 68%, 95% and 99.7% within xˉ±s \bar{x} \pm s , ±2s \pm 2s , ±3s \pm 3s .
  • Systematic sampling: K=N/n K = N/n ; sample r,r+K,…,r+(n−1)K r, r + K, \dots, r + (n-1)K .
I want…CodeResult
Mean, medianmean(x), median(x)A number
Variance and standard deviationvar(x), sd(x)With n−1 n – 1
Quartiles and IQR IQR quantile(x), IQR(x)0%, 25%, 50%, 75%, 100%
Everything at oncesummary(x)Min., Q1, median, mean, Q3, max.
Frequency tabletable(cut(x, breaks))Classes (a,b] and their fi f_i
Relative and cumulativeprop.table(f), cumsum(...)Proportions
Outliersboxplot(x)$outThe loose points
By grouptapply(x, g, mean)One mean per group
Row proportionsprop.table(table, margin = 1)Each row adds up to 1
SymptomLikely cause
The cut table adds up to less than n n A value on the first break or outside the range
My variance does not match var()I divided by n n
The interpolated median falls outside its classFb F_b including its own class, or the wrong L L
A class label comes out trimmed (14.2])dig.lab is missing in cut
The histogram says DensityClasses of different widths: what counts is the area

Before handing in, ask yourself:

  1. Have I given every result with 2 decimals and its units?
  2. Have I checked that the frequencies add up to n n and that the interpolated median falls inside its class?
  3. Have I commented on the difference between grouped and original, and interpreted in context (minutes, buses)?
  4. Is all the code for each exercise in my .R file?
ME1 exam coming up?The script for every topic, the printable cheat sheet and the pack for each exam, for free.See the pack

For your script

The topic’s code as functions, commented and with no statements, to copy into your exam .R file. It is tested with the data in this article.

## ===== ME1 · Topic 1: descriptive statistics (sergiolearns.com) =====

# Named numerical summary with 2 decimals
num_summary <- function(x) {
  round(c(n = length(x), mean = mean(x), median = median(x),
          s2 = var(x), s = sd(x), Q1 = unname(quantile(x, 0.25)),
          Q3 = unname(quantile(x, 0.75)), IQR = IQR(x)), 2)
}

# 1.5 IQR fences and possible outliers
outliers <- function(x) {
  q <- quantile(x, c(0.25, 0.75)); r <- IQR(x)
  v <- unname(c(q[1] - 1.5 * r, q[2] + 1.5 * r))
  list(fences = round(v, 2), outliers = x[x < v[1] | x > v[2]])
}

# Complete frequency table (checks that no value is lost)
freq_table <- function(x, breaks) {
  f <- table(cut(x, breaks, dig.lab = 6))
  if (sum(f) != length(x)) warning("some values fall outside the classes")
  fa <- as.vector(f)
  data.frame(class = names(f), mark = (head(breaks, -1) + tail(breaks, -1)) / 2,
             f = fa, fr = round(fa / sum(fa), 4), Fr = round(cumsum(fa) / sum(fa), 4))
}

# Measures with grouped data: f = frequencies, breaks = class limits
grouped <- function(f, breaks) {
  m <- (head(breaks, -1) + tail(breaks, -1)) / 2; n <- sum(f)
  mean <- sum(f * m) / n
  s2 <- (sum(f * m^2) - n * mean^2) / (n - 1)
  round(c(mean = mean, s2 = s2, s = sqrt(s2)), 2)
}

# Percentile p (0.5 = median) by interpolation in grouped data
percentile_gr <- function(f, breaks, p) {
  n <- sum(f); k <- which(cumsum(f) >= p * n)[1]
  Fb <- sum(f[seq_len(k - 1)]); L <- breaks[k]; w <- breaks[k + 1] - breaks[k]
  round(L + w / f[k] * (p * n - Fb), 2)
}

# Mean and variance of several groups from their summaries
groups <- function(nj, mj, vj) {
  n <- sum(nj); xbar <- sum(nj * mj) / n
  s2 <- (sum((nj - 1) * vj) + sum(nj * (mj - xbar)^2)) / (n - 1)
  round(c(mean = xbar, s2 = s2, s = sqrt(s2)), 2)
}

# Change of origin and scale y = a + b x
transform_lin <- function(mean, s, a, b) round(c(mean = a + b * mean, s2 = (b * s)^2, s = abs(b) * s), 2)

# Systematic sampling: N elements, n in the sample, start r between 1 and K
systematic <- function(N, n, r) { K <- N / n; r + K * (0:(n - 1)) }

Tested with the exercises above:

num_summary(journey)
outliers(journey)
grouped(c(36, 58, 31, 23, 12), c(0, 4, 8, 12, 20, 30))
c(median = percentile_gr(c(36, 58, 31, 23, 12), c(0, 4, 8, 12, 20, 30), 0.5),
  p90 = percentile_gr(c(36, 58, 31, 23, 12), c(0, 4, 8, 12, 20, 30), 0.9))
groups(c(25, 20, 15), c(10.4, 5.6, 9.8), c(12.25, 4.41, 16.00))
head(systematic(1200, 40, 17), 5)

Output:

     n   mean median     s2      s     Q1     Q3    IQR 
 36.00  36.57  36.05  39.95   6.32  32.77  38.10   5.33 
$fences
[1] 24.79 46.09

$outliers
[1] 52.4 58.6

 mean    s2     s 
 8.74 40.91  6.40 
median    p90 
  7.03  18.61 
 mean    s2     s 
 8.65 14.99  3.87 
[1]  17  47  77 107 137

The functions give the same as the solutions: the summary and outliers of exercise 1, the mean (8.74), standard deviation (6.4), median (7.03) and 90th percentile (18.61) of exercise 2, the pooled mean and variance of exercise 3 and the first five trips of the sample. transform_lin applies the change of origin and scale from the mean and the standard deviation (in 3(c), transform_lin(8.65, sqrt(14.98712), 90, 60)). The freq_table function warns you if a value falls outside the classes, the mistake from the practice.

What comes next?

With this topic you have the first part of exercise 1 of the EC1. The next ME1 topic is probability: events, counting, conditional probability and Bayes’ theorem, the other big block of the EC1. Meanwhile, if you want to review R, you have R from scratch and the guide How to use RStudio.

Keep goingAll Statistics articles

Did this article help you? Here is how you can support SergioLearns →

Ready for the exam?ME1 EC1 mock exam — the 4-hour exam with R alongside, four versions, a mark out of 50 and every solution with its code (and why the .R counts too)