# ferdo Lab — Proposed Week 5: INDEPENDENT worked example
# Fictional data. These public model answers are NOT official UCL solutions.
rt_ms <- c(620,610,590,605,615,600,625,595,608,612,598,603)
n <- length(rt_ms)
mean_rt <- mean(rt_ms)
sd_rt <- sd(rt_ms)
se_rt <- sd_rt / sqrt(n)
print(c(n=n, mean_ms=mean_rt, sd_ms=sd_rt, se_ms=se_rt))
stopifnot(n==12L, is.finite(se_rt), se_rt>0)
set.seed(46)
means_n4 <- replicate(1000, mean(sample(rt_ms, 4, replace=TRUE)))
means_n10 <- replicate(1000, mean(sample(rt_ms, 10, replace=TRUE)))
par(mfrow=c(1,2))
hist(means_n4, main="Resample size = 4", xlab="Mean RT (ms)")
hist(means_n10, main="Resample size = 10", xlab="Mean RT (ms)")
par(mfrow=c(1,1))
print(c(sd_n4=sd(means_n4),sd_n10=sd(means_n10)))
means_n12 <- replicate(1000, mean(sample(rt_ms, n, replace=TRUE)))
print(quantile(means_n12,c(.025,.975)))
stopifnot(length(means_n4)==1000L, all(is.finite(means_n12)))
# The size-12 bootstrap percentile interval is a teaching example only.
# It cannot resolve non-random sampling, bias, or real-world generalisability.
cat("WEEK 5 WORKED EXAMPLE PASS\n")
