Skip to content

Instantly share code, notes, and snippets.

@paulovillarroel
Created January 7, 2024 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulovillarroel/704d271f8d391414cb44d416d26656e3 to your computer and use it in GitHub Desktop.
Save paulovillarroel/704d271f8d391414cb44d416d26656e3 to your computer and use it in GitHub Desktop.
Small script to explain the central limit theorem
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