Created
January 7, 2024 22:17
-
-
Save paulovillarroel/704d271f8d391414cb44d416d26656e3 to your computer and use it in GitHub Desktop.
Small script to explain the central limit theorem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(2006) | |
sample_sizes <- c(1, 10, 30, 1000) | |
nsims <- 10000 | |
# Helper function: Mean of one sample of X | |
one_mean <- function(n, p = c(0.8, 0.2)) { | |
mean(sample(0:1, n, replace = TRUE, prob = p)) | |
} | |
# Simulate and plot | |
par(mfrow = c(2, 2), mai = rep(0.4, 4)) | |
for (n in sample_sizes) { | |
means <- replicate(nsims, one_mean(n)) | |
hist(means, breaks = "FD", | |
main = sprintf("n=%i", n)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment