Course: Statistics and Probability for Data Science (DS14270). Source deck: Samer El Kababji, SD02 — Exploratory Data Analysis.
This lesson uses small fictional datasets to connect each descriptive formula to its base R implementation. Each code cell follows the mathematics it implements.
Population parameters and sample statistics
A population contains every observation of interest. A sample contains the observations actually collected from that population. A numerical summary of a population is called a parameter; a numerical summary of a sample is called a statistic.
The same data values can be treated as a complete population in one question and as a sample in another. The question determines which formula is suitable.
Arithmetic mean
For observations \(x_1,\ldots,x_n\), the sample mean is
\[
\bar{x}=\frac{1}{n}\sum_{i=1}^{n}x_i.
\]
Worked example — eight deliveries. For the values used below,
Worked example — the same three values, two interpretations. Take \(x=(2,4,6)\), with mean \((2+4+6)/3=4\). The squared deviations sum to
\[
(2-4)^2+(4-4)^2+(6-4)^2=4+0+4=8.
\]
If these three values are the entire population, \(\sigma^2=8/3\approx2.667\). If they are a sample, \(s^2=8/(3-1)=4\). Their sample standard deviation is \(s=\sqrt{4}=2\), in the original units. The denominator changes with the population or sample interpretation.
Base R’s var() computes sample variance, so its denominator is \(n-1\).
Squaring prevents positive and negative deviations from cancelling. Variance is measured in squared units; the standard deviation sd(delivery_minutes) is the square root of sample variance and returns to the original units.
Proportion
Suppose \(x\) of the \(n\) sampled observations have a characteristic. The sample proportion is
\[
\widehat p=\frac{x}{n}.
\]
Worked example — defective items. Two defective items among ten give \(\widehat p=2/10=0.20=20\%\). In indicator notation, the same calculation is \((0+0+1+0+0+1+0+0+0+0)/10=0.20\).
In R, logical values behave like 1 for TRUE and 0 for FALSE when averaged.
defective_items sample_size formula R
2.0 10.0 0.2 0.2
Median
First sort the values:
\[
x_{(1)}\leq x_{(2)}\leq\cdots\leq x_{(n)}.
\]
For odd \(n\), the median is the middle ordered value. For even \(n\), it is the mean of the two middle ordered values.
Worked example — find the middle after sorting. For \((2,4,7,10,12,15,18,20,25)\), \(n=9\) and the middle position is \((9+1)/2=5\), so the median is \(x_{(5)}=12\). For \((3,5,8,9,14,21)\), \(n=6\) and the two middle positions are 3 and 4, giving \((8+9)/2=8.5\).
The fractional position is one quarter of the way from the second value to the third. Similarly, for \(Q_3\), \(h=1+5(0.75)=4.75\), so \(Q_3=9+0.75(14-9)=12.75\).
Code
linear_quantile <-function(values, p) { ordered <-sort(values) n <-length(ordered) h <-1+ (n -1) * p j <-floor(h) g <- h - jif (g ==0) {return(ordered[j]) } ordered[j] + g * (ordered[j +1] - ordered[j])}probabilities <-c(Q1 =0.25, Q2 =0.50, Q3 =0.75)by_formula <-vapply(probabilities,function(p) linear_quantile(odd_values, p),numeric(1))by_R <-quantile(odd_values, probabilities, type =7, names =FALSE)rbind(formula = by_formula, R = by_R)
Q1 Q2 Q3
formula 7 12 18
R 7 12 18
For the nine-value example, the positions are integers, giving \(Q_1=7\), \(Q_2=12\), and \(Q_3=18\).
For the six-value example, the median position is \(h=3.5\), so it lies halfway between the third and fourth ordered observations:
quartile p formula R
Q1 Q1 0.25 5.75 5.75
Q2 Q2 0.50 8.50 8.50
Q3 Q3 0.75 12.75 12.75
R supports several quantile conventions. Always state the method when exact quartile values matter. The linear method in this lesson is R’s default type = 7.
Try it: add a very large value such as 100 to even_values. Compare how the mean and median change.