library(tidyverse)
# In your own class folder this path is just "Data/resumes-2.csv"
resumes <- read_csv("../Slides/Data/resumes-2.csv", show_col_types = FALSE) |>
mutate(cb = as.numeric(call == "yes")) # 1 = callback, 0 = no callback
# Full-sample race comparison from class, for reference
w <- resumes$cb[resumes$ethnicity == "cauc"]
b <- resumes$cb[resumes$ethnicity == "afam"]
dif <- mean(w) - mean(b)
se <- sqrt(var(w) / length(w) + var(b) / length(b))You Try Answers: Confidence Intervals and t-Tests
POSC 8410
You Try
Question 3 involves a random subsample, so your numbers for that question will differ from mine. I use set.seed(8410) so this document gives the same numbers every time it renders. The other questions use the full data and should match exactly.
1. Compute a 95% CI for the callback gap by résumé quality. Does it include zero?
ci_q <- t.test(cb ~ quality, data = resumes)$conf.int
100 * ci_q # in percentage points[1] -0.122070 2.933557
attr(,"conf.level")
[1] 0.95
Yes, it includes zero, just barely. We estimate that a high-quality résumé raises the callback rate by about 1.4 percentage points (95% CI: -0.1 to 2.9).
This matches the hypothesis test from last class, where \(p = 0.07\). The 95% interval contains zero exactly when the two-sided test fails to reject at \(\alpha = 0.05\). Since \(p\) is between 0.05 and 0.10, the 90% interval should exclude zero:
100 * t.test(cb ~ quality, data = resumes, conf.level = 0.90)$conf.int[1] 0.1236372 2.6878500
attr(,"conf.level")
[1] 0.9
It does. The same data clear one threshold and miss the other, a reminder that 0.05 is a convention.
The data are consistent with a quality effect anywhere from essentially zero up to about 3 points, roughly the size of the race gap. So the study cannot rule out a quality effect as large as the race effect. Only reporting “Not significant” would hide that nuance.
2. Compute a 90% interval for the race gap. Is it wider or narrower? Why?
ci95 <- dif + c(-1, 1) * qnorm(0.975) * se
ci90 <- dif + c(-1, 1) * qnorm(0.950) * se
rbind(`95%` = 100 * ci95, `90%` = 100 * ci90) # percentage points [,1] [,2]
95% 1.677459 4.729111
90% 1.922772 4.483799
c(width_95 = 100 * diff(ci95), width_90 = 100 * diff(ci90))width_95 width_90
3.051652 2.561027
Narrower: 1.9 to 4.5 points, against 1.7 to 4.7 for the 95% interval.
The estimate and the standard error are the same in both. Only the multiplier changed: a 90% interval leaves 5% in each tail, so it uses \(z_{0.95} =\) 1.645 instead of \(z_{0.975} =\) 1.96. The width shrinks by that ratio, 0.84.
With t.test, set the level with an argument (its sign is flipped because R puts afam first):
100 * t.test(cb ~ ethnicity, data = resumes, conf.level = 0.90)$conf.int[1] -4.484051 -1.922520
attr(,"conf.level")
[1] 0.9
3. Take a subsample of 300 résumés and compute the CI. How does the width compare, and by roughly what factor should it change?
set.seed(8410)
sub <- slice_sample(resumes, n = 300)
ci_full <- t.test(cb ~ ethnicity, data = resumes)$conf.int
ci_sub <- t.test(cb ~ ethnicity, data = sub)$conf.int
rbind(full = 100 * ci_full, subsample = 100 * ci_sub) [,1] [,2]
full -4.729503 -1.677067
subsample -11.805550 1.387309
c(width_full = 100 * diff(ci_full), width_sub = 100 * diff(ci_sub),
ratio = diff(ci_sub) / diff(ci_full))width_full width_sub ratio
3.052436 13.192859 4.322076
The subsample interval is about four times as wide.
The factor comes from the \(\sqrt{n}\) rule. The standard error is proportional to \(1/\sqrt{n}\), and the interval width is a fixed multiple of the standard error. Going from 4,870 résumés to 300 should multiply the width by
\[\sqrt{\frac{4870}{300}} \approx 4.03\]
My subsample came out at 4.32. Yours will differ a little, unless you use the same seed.
set.seed(8410)
widths <- replicate(2000, {
s <- slice_sample(resumes, n = 300)
diff(t.test(cb ~ ethnicity, data = s)$conf.int)
})
c(average_ratio = mean(widths) / diff(ci_full), predicted = sqrt(4870 / 300))average_ratio predicted
4.049903 4.029061
Notice also that the subsample interval very likely includes zero. With 300 résumés, the study could not distinguish the 3-point gap from no gap at all. That is the same low-power result as last class.
4. Get the same CI two ways: t.test and confint(lm(...)). Do they agree?
tt <- t.test(cb ~ ethnicity, data = resumes)
fit <- lm(cb ~ ethnicity, data = resumes)
# t.test reports afam minus cauc; lm reports cauc minus afam. Flip t.test to match.
rbind(t.test = -rev(tt$conf.int),
lm = confint(fit)["ethnicitycauc", ]) 2.5 % 97.5 %
t.test 0.01677067 0.04729503
lm 0.01677080 0.04729491
Yes, to six decimal places (a difference of about one ten-thousandth of a percentage point).