frac_correct <- tapply(
rosling_responses$response == "correct",
rosling_responses$question,
mean
)
frac_correct
n <- table(rosling_responses$question)
n
expected <- tapply(
rosling_responses$prob_random_correct,
rosling_responses$question,
mean
)
# Construct confidence intervals.
se <- sqrt(frac_correct * (1 - frac_correct) / n)
# Lower bounds.
frac_correct - 1.96 * se
# Upper bounds.
frac_correct + 1.96 * se
# Construct Z-scores and p-values.
z <- (frac_correct - expected) / se
pt(z, df = n - 1)
Run the code above in your browser using DataLab