set.seed(124)
p <- 0.4; n <- 100; X <- rbinom(n, 1, p)
p_hat <- mean(X)
# bootstrap
B <- 10^3
p_boot <- rep(0, B)
for(i in 1:B){
X_boot <- sample(X, size = n, replace = TRUE)
p_boot[i] <- mean(X_boot)
}
hist(p_boot, freq = FALSE, main = "Bootstrap Distribution", xlab = "p")
curve(dbeta(x, n*p_hat, n*(1-p_hat)), add = T, col = "red")
abline(v = p, lty = 2, col = "green", lwd = 4)
abline(v = p_hat, lty = 2, col = "blue", lwd = 4)