library(tidyverse)
# In your own class folder this path is just "Data/anes.csv"
anes <- read_csv("../Slides/Data/anes.csv", show_col_types = FALSE)You Try Answers: Estimators
POSC 8410
You Try
As in the sampling exercise, questions 2 and 3 involve random draws, so your numbers will differ slightly from mine. I use set.seed(8410) so this document gives the same numbers every time it renders.
1. Treat ft_science as a population and compute its true variance (divide by \(N\)).
Build the population the same way we built pop in class: drop the missing values and pull out the column as a vector.
sci <- anes |> filter(!is.na(ft_science)) |> pull(ft_science)
sig2_sci <- mean((sci - mean(sci))^2) # average squared deviation: divides by N
sig2_sci[1] 406.675
The population variance is 406.7. Taking the mean() of the squared deviations adds them up and divides by \(N\), which is exactly the population formula.
If you used var(sci) instead, you got a slightly different number:
var(sci) # divides by N - 1[1] 406.7302
var() always divides by \(N - 1\), because it assumes you are handing it a sample rather than a population. With \(N\) = 7,367, the difference is trivial. It matters a great deal in small samples.
2. Draw 10,000 samples of size 8. Compute the average of \(\sum(x_i - \bar{x})^2/n\) and of \(\sum(x_i - \bar{x})^2/(n-1)\). Which matches the truth?
Each run of replicate() draws 8 people, computes the sum of squared deviations once, and divides it two ways. I also keep the sample median and the first observation from each sample, because questions 3 and 4 need them. This lets us compare several estimators on the very same samples.
set.seed(8410)
yt <- replicate(10000, {
s <- sample(sci, 8)
ss <- sum((s - mean(s))^2)
c(divide_by_n = ss / 8, ## I am just creating different metrics to report here
divide_by_n_minus_1 = ss / 7,
median = median(s),
first = s[1])
})
# yt has one row per estimator and one column per sample
c(rowMeans(yt[1:2, ]), truth = sig2_sci) divide_by_n divide_by_n_minus_1 truth
356.1829 407.0662 406.6750
Dividing by \(n - 1\) matches the truth (407.1 against 406.7, a gap well within simulation noise).
Dividing by \(n\) runs 12.4% too low. That is the size the proof predicts. We showed that \(E\left[\sum(x_i - \bar{x})^2\right] = (n-1)\sigma^2\), so dividing by \(n\) gives, on average,
\[\frac{n-1}{n}\,\sigma^2 = \frac{7}{8}\,\sigma^2,\]
a shortfall of \(1/8\), or 12.5%. The bias comes from measuring deviations around \(\bar{x}\), which sits as close to the sample as possible, rather than around the true \(\mu\). The shortfall is \(1/n\), which is why it is large with \(n = 8\) and negligible with \(n = 1{,}000\).
3. Is the sample median an unbiased estimator of the population median here? Check by simulation.
pop_median <- median(sci)
pop_median # population median[1] 85
mean(yt["median", ]) # average sample median, n = 8[1] 82.8357
No. The population median is 85, but medians from samples of 8 average 82.8. They are biased downward.
Why? Look at how the population sits around its median:
c(below = mean(sci < pop_median),
at = mean(sci == pop_median),
above = mean(sci > pop_median)) below at above
0.4045066 0.2341523 0.3613411
85 is the median because a large pile of respondents answered exactly 85 (one of the labelled thermometer points), and that pile straddles the 50% mark. But the population is not balanced around it:
- More people sit below 85 than above it (40% versus 36%). So a sample of 8 is more likely to have its middle values fall below 85 than above.
- Values below can be far below. They stretch all the way down to 0, while values above can be at most 100, only 15 points away.
So the sample median misses low more often, and by more, than it misses high:
c(below = mean(yt["median", ] < pop_median),
at = mean(yt["median", ] == pop_median),
above = mean(yt["median", ] > pop_median)) below at above
0.4002 0.3105 0.2893
The median is unbiased when the population is symmetric. ft_science is neither symmetric nor smooth.
Does the bias go away with more data? Try bigger samples:
set.seed(8410)
mean(replicate(10000, median(sample(sci, 50))))[1] 84.38205
mean(replicate(2000, median(sample(sci, 500))))[1] 85
The bias shrinks as \(n\) grows. With 50 respondents the average median is much closer to 85, and with 500 it is essentially there. So the sample median here is biased but consistent: biased in small samples, with the bias vanishing as \(n\) grows. That is Case 1 from the slides, the same pattern as \(s\) for \(\sigma\).
4. “My estimator is unbiased, so my estimate is correct.” What is wrong with this sentence?
It confuses the estimator (the recipe) with the estimate (the dish). Unbiasedness is a property of the procedure, averaged over all the samples you might have drawn. It says nothing about whether the one sample you actually drew landed near the truth.
The simulation from question 2 makes this concrete. Dividing by \(n - 1\) is unbiased, yet look at the individual estimates:
s2 <- yt["divide_by_n_minus_1", ]
mean(abs(s2 / sig2_sci - 1) < 0.10) # share within 10% of the truth[1] 0.1527
mean(s2 < sig2_sci) # share below the truth[1] 0.6295
- Only 15% of the unbiased estimates land within 10% of the true variance.
- 63% of them are too low. Unbiased does not even mean “too high half the time and too low half the time.” The sampling distribution here is skewed: many modest underestimates are balanced by a few very large overestimates, so the average comes out right while the typical estimate is low.
The first-observation estimator from the slides makes the same point more starkly:
c(mean = mean(yt["first", ]), # centered on the true mean
sd = sd(yt["first", ])) # but a typical miss is huge mean sd
79.38660 20.27791
It is centered on the true mean of 79.3, so it is unbiased, but a typical estimate misses by about 20 points. It is unbiased and still useless.
The useful question about a single estimate is not “is the estimator unbiased?” but “how far from the truth is an estimate like this likely to be?” That is what the standard error measures, and when there is bias as well, the mean squared error.