Sampling, the mean, and visualising discrete data in R
0.1
Where this week is heading
We build one connected story rather than five loose topics:
Variable types & frequency — what kind of data we have, and how we count it.
Population vs sample — the quantity we care about (\(\mu\)) versus the number we can actually compute (\(\bar{x}\)).
The mean — the estimator itself, its formula, and computing it in R.
Sampling for a fair estimate — how to sample so \(\bar{x}\) is a trustworthy estimate of \(\mu\).
Visualising discrete data in R — frequency tables, needle charts, and bar charts.
The thread that ties it together
We almost never know the population mean \(\mu\). We estimate it with the sample mean \(\bar{x}\). Everything else this week is about making that estimate fair and precise.
1.0
Section 1
Variable Types & Frequency
A quick recap of Week 1, plus the vocabulary of counting: frequency, relative frequency, and proportion.
1.1
1.1 Types of variable
Every variable falls into one of two families, and each family splits in two:
Click the statement you think is false. The answer and reasoning appear below.
Phones come in five colours: white, black, silver, gold, pink. Which statement is FALSE?
These colours have no inherent order, so colour is nominal, not ordinal. It is categorical / qualitative, and certainly not continuous.
A farmer counts the oranges in each of ten 3 kg bags. Which statement is FALSE?
A count is numerical / quantitative and discrete. Counts have a natural order, but that does not make them categorical.
1.3
1.3 Self-check — continuous data
Students report homework time in minutes (to 0.1 of a minute). Which statement is FALSE?
Time is a measured quantity — quantitative and continuous — not qualitative. It has a true zero and cannot be negative.
Why this matters for visualisation
The variable type decides the chart. Categorical & discrete data get bar / needle charts (this week); continuous data get histograms and box-plots (Week 3).
1.4
1.4 Frequency, relative frequency & proportion
Definitions
For a category, the frequency is how many times it occurs. The relative frequency is that count divided by the total number of observations. The proportion is the same number as the relative frequency.
$$\text{relative frequency} = \text{proportion} = \frac{\text{count in the category}}{\text{total count}}$$
Worked example — fire-safety audits
Of 1456 audits, 375 were satisfactory and 1081 unsatisfactory.
Frequency of "satisfactory" = 375.
Relative frequency = proportion = \( \dfrac{375}{1456} = 0.258 \) — divide by the total, not by 1081.
Equivalently: \(0.258 = 25.8\% = 258{,}000\) parts per million (ppm).
Common trap: writing 375/1081 (satisfactory ÷ unsatisfactory). The denominator is always the total.
1.5
1.5 Self-check — relative frequency
1456 audits: 375 satisfactory, 1081 unsatisfactory. Which statement is FALSE?
Relative frequency is count ÷ total = 375 / 1456 = 0.258. The value 1081 is the unsatisfactory count, not the total. Proportion = relative frequency = 25.8% = 258,000 ppm.
2.0
Section 2
Population vs Sample
The core idea of the week: the quantity we want (\(\mu\)) versus the quantity we can compute from data (\(\bar{x}\)).
2.1
2.1 Population, sample, variable
Definitions
The population is the entire collection we want to describe (size \(N\)). A sample is a subset we actually observe (size \(n\)). The variable is the measurement we record for each unit.
A population can be:
Finite and real — e.g. all KBS students enrolled in 2025.
Effectively infinite or hypothetical — e.g. "all 2-litre bottles this filling process would ever produce on its current settings." We can never list them all.
Same data, different label
A tax agent computes the mean fee income over her 242 clients. If she treats those 242 as the whole population, she has computed \(\mu\). If she treats the year as one sample from all possible years (at today's prices), she has computed \(\bar{x}\). The question decides which.
2.2
2.2 Parameter vs statistic: \(\mu\) and \(\bar{x}\)
A parameter describes the population. A statistic is computed from a sample and is used to estimate the parameter.
Population mean \(\mu\) — usually unknown.
Sample mean \(\bar{x}\) — computable, our estimate of \(\mu\).
The estimation problem
We rarely know \(\mu\), because we can't measure every member of the population. So we take a sample and report \(\bar{x}\).
3.0
Section 3
The Mean
The estimator itself — its meaning, its formula for samples and populations, one key property, and how to compute it in R.
3.1
3.1 The mean as a measure of centre
Understanding the centre of a dataset is fundamental. The most common measure of centre is the mean — the average value.
In words
To find the mean, add up all the values and divide by how many there are.
Example
Data \(\{4, 6, 2, 8\}\): sum \(= 20\), count \(= 4\), so mean \(= 20 / 4 = 5\).
3.2
3.2 Formula for the mean
Data are written \(\{x_1, x_2, \ldots\}\), or compactly \(\{x_i\}\). The choice of letter is arbitrary; \(x\) is conventional.
Sample mean (read "x-bar")
Sample of size \(n\):
$$\bar{x} = \frac{\sum x_i}{n}, \quad i = 1, \ldots, n$$
Population mean (read "mu")
Population of size \(N\) (possibly infinite):
$$\mu = \frac{\sum x_i}{N}, \quad i = 1, \ldots, N$$
Same recipe, different scope
The formula is identical — sum divided by count. Only the letter (\(n\) vs \(N\)) and the interpretation (estimate vs true value) differ.
3.3
3.3 The mean in R
Manual averaging gets tedious as data grow. R has a built-in mean().
# A dataset of ten values
example_data <- c(24, 16, 30, 10, 12, 28, 38, 2, 4, 36)
# Calculate the mean
example_average <- mean(example_data)
print(paste("The average is:", example_average)) # 20
R also ships with datasets you can use directly:
data(iris) # load the built-in Iris data
mean_sepal_length <- mean(iris$Sepal.Length)
print(paste("Average sepal length is", mean_sepal_length))
3.4
3.4 A key property: deviations sum to zero
Subtract the mean from every value (this is mean-corrected or centred data). The corrected values always sum to exactly zero:
$$\sum (x_i - \bar{x}) = 0$$
Deviations: (−3) + (−1) + (+1) + (+3) = 0. Amounts below the mean exactly cancel amounts above it.
Create a vector data with the values 12, 18, NA, 14, 20, NA, 16, 10. Compute its mean, ignoring the missing values, and print the result with a message.
By default mean() returns NA if any value is NA. The argument na.rm = TRUE tells R to drop missing values first.
Solution
# Step 1: create the vector
data <- c(12, 18, NA, 14, 20, NA, 16, 10)
# Step 2: mean, ignoring missing values
mean_data <- mean(data, na.rm = TRUE)
# Step 3: print the result
print(paste("The mean value is:", mean_data)) # 15
The six present values are 12, 18, 14, 20, 16, 10 — sum 90, count 6, mean 15.
4.0
Section 4
Sampling for a Fair Estimate
How we choose a sample determines whether \(\bar{x}\) can be trusted as an estimate of \(\mu\).
4.1
4.1 Simple random sampling (SRS)
Give every one of the \(N\) population members a unique ticket. Mix the tickets thoroughly in a hat and draw \(n\) of them — on a computer, we do this with random numbers.
Two defining properties
Every member has the same chance of selection, equal to \(n/N\).
Every possible subset of \(n\) members is equally likely to be the one chosen.
Watch the second property
Equal individual chances are not enough on their own to make a scheme "simple random" — the equal-subsets condition is the stricter part (we return to this in 4.5).
4.2
4.2 Bias and convenience sampling
Definitions
A sampling scheme is biased if it systematically over- or under-represents part of the population. A convenience sample takes whoever is easiest to reach — and is usually biased.
Scenario
Verdict
Weigh 8 bottles at hourly intervals across a shift
Reasonable to treat as fair.
Ask 500 adults leaving the Oval about a sport & recreation budget
Likely biased — race-goers aren't typical of all adults.
Ask the five people ahead of you in the post-office queue
Convenience sample — biased.
4.3
4.3 Self-check — the post-office queue
You ask the five people ahead of you in the queue whether they prefer Liberal or Labor promises. Which statement is FALSE?
Taking whoever happens to be nearby is a convenience sample, not SRS — people not in that queue had no chance of selection. It is therefore biased and need not represent SA voters.
4.4
4.4 What SRS does — and does not — promise
The distinction that trips people up
SRS is an unbiased procedure: on average, over many possible samples, it neither favours nor disfavours any group. But any single sample can still turn out unrepresentative by chance.
SRS guarantees fairness in the long run, not representativeness of one draw.
Equal selection probability alone does not imply SRS was used — other schemes (e.g. stratified with proportional allocation, coming next) also give equal probabilities.
4.5
4.5 Self-check — what SRS guarantees
You take a simple random sample of Kaplan students. Which statement is FALSE?
SRS is unbiased and gives equal selection chances, but it cannot guarantee that any single sample is representative — that only holds on average. And equal chances alone don't imply the scheme was SRS.
4.6
4.6 Stratified sampling
The scheme
Split the population into sub-populations called strata.
Take a simple random sample within each stratum.
A good choice when the strata are expected to differ substantially in their answers — sampling each one separately guarantees all groups are represented.
4.7
4.7 Combining strata: the weighted estimator
Suppose stratum \(h\) has size \(N_h\) and its sample gives proportion \(\hat{p}_h\) in favour. The fair population estimate weights each stratum by its true size:
Simply pooling everyone — \(\hat{p} = \dfrac{\text{total in favour}}{\text{total sampled}}\) — is only unbiased when every stratum is sampled at the same fraction \(n_h/N_h\). If you over-sample one stratum, pooling is biased and you must use the size-weighted formula above.
The next three slides are one running example (the "Adelaide 500" survey) that pins down exactly when pooling works and when it fails.
4.8
4.8 Worked example I — equal strata, equal samples
20,000 voters: 10,000 City + 10,000 North Adelaide. SRS of 250 from each. In favour: 60/250 City (0.24), 190/250 North (0.76).
Which statement is FALSE?
Both strata are sampled at the same fraction (250/10,000 = 0.025), so selection chances are equal and pooling is unbiased. Pooled = 250/500 = 0.50; weighted = (0.24·10,000 + 0.76·10,000)/20,000 = 0.50 — they agree. It is stratified, and nothing here is biased, so "biased" is the false statement.
4.9
4.9 Worked example II — equal strata, unequal samples
Same 10,000 + 10,000 strata. Now SRS of 400 City + 100 North. In favour: 100/400 City (0.25), 75/100 North (0.75).
Which statement is FALSE?
City is over-sampled (400/10,000 = 0.04 vs 100/10,000 = 0.01, so 4× — true). That makes the naive pool 175/500 = 0.35 biased toward City's low rate. The correct size-weighted estimate is (0.25·10,000 + 0.75·10,000)/20,000 = 0.50. So the 0.35 claim is the false one.
4.10
4.10 Worked example III — unequal strata AND unequal samples
Strata are now 12,000 City + 8,000 North. SRS of 400 City + 100 North. In favour: 100/400 (0.25), 75/100 (0.75).
Which statement is TRUE?
Weight each stratum's rate by its true size: (0.25·12,000 + 0.75·8,000)/20,000 = (3,000 + 6,000)/20,000 = 0.45. Option d swaps the weights; the pool (0.35) is biased; 0.25 + 0.75 = 1.00 (not 0.25); and unequal strata are exactly what the weighting is for.
4.11
4.11 Fairness vs precision
Two separate questions about an estimate:
Fair (unbiased)?
Precise?
Does the procedure hit \(\mu\) on average? Fixed by good sampling design (SRS, or probability sampling with correct weights).
How much would the estimate wobble from sample to sample? Improved by a larger sample size \(n\).
Threats to watch
Non-response (people who don't answer the phone may differ systematically) can reintroduce bias even in a well-designed sample. A skewed gender or age mix in the responders is a warning sign.
5.0
Section 5
Visualising Discrete Data in R
Frequency tables, needle charts, and bar charts — using base R (we meet ggplot in Weeks 10–11).
5.1
5.1 Binary data and the sample proportion
Many discrete variables are binary — two outcomes:
good / defective component
agree / disagree with a statement
approve / disapprove of a policy
Coding
Quantify the two outcomes as 0 and 1. For a sample of size \(n\), the sample proportion of 1s is
$$\hat{p} = \frac{\text{number of 1s}}{n}$$
This is exactly the relative frequency of the "1" category — and, as in Section 4, the quantity we estimate in surveys.
5.2
5.2 Frequency distribution with table()
A corner shop orders 20 baguettes a day. Over 10 days the numbers unsold were: 0, 0, 1, 0, 1, 0, 0, 0, 2, 0.
x = c(0, 0, 1, 0, 1, 0, 0, 0, 2, 0)
T = table(x) # tally each distinct value
T.df = as.data.frame(T)
colnames(T.df) <- c("unsold", "frequency")
Unsold = as.numeric(as.character(T.df$unsold))
Frequency = T.df$frequency
Result
Unsold → 0 1 2 Frequency → 7 2 1
Note the as.numeric(as.character(...)) step: table() stores the values as factor labels, so we convert them back to numbers before plotting.
Needle vs bar is a stylistic choice — both carry the same information for discrete data.
5.6
5.6 Applied task — loss of separation
Your turn — using LOS.xlsx
Plot a line (needle) chart showing the frequencies of high- or very-high-risk loss-of-separation incidents per quarter in Australian controlled airspace, from Q1 2008 to Q2 2012.
Suggested approach
Read the spreadsheet — e.g. library(readxl); los <- read_excel("LOS.xlsx").
Build a frequency table of the incident counts with table(), as in 5.2.
Reuse the needle-chart recipe from 5.3: plot(..., type = "n"), then segments(), then axis().
This applies the whole Section 5 pipeline — tabulate, then draw — to a real dataset.
6.0
Bringing it together
The Week in One Slide
6.1
6.1 Synthesis
We usually cannot calculate \(\mu\) — we don't know the variable for every member of the population.
So we compute the sample mean \(\bar{x}\) as an estimate of \(\mu\).
If we sample through simple random sampling — or a probability scheme with the right weights (stratification) — that estimate is fair.
The accuracy of the estimate increases as the sample size grows.
And for visualisation
Once we have discrete data, a frequency table plus a needle or bar chart tells the story at a glance — the foundation we build on in the weeks ahead.