set.seed(123)
pop_size <- 10000000
pop <- rchisq(pop_size, df = 2)
# Histogram of the entire population
hist(pop, breaks = 50, col = "lightblue",
main = "Population of 10,000,000 from Chi-square(2)",
xlab = "Values from Chi-square(2)")

# Mean and standard deviation of the population
pop_mean <- mean(pop)
pop_sd <- sd(pop)
pop_mean
## [1] 2.000466
pop_sd
## [1] 2.00042
n <- 50
sims <- 10000
xbars <- numeric(sims)
# Draw a random sample of size 50 from the right-skewed population
for(i in 1:sims){
samp <- sample(pop, size = n, replace = TRUE)
xbars[i] <- mean(samp)
}
# 3a) Histogram of the 10,000 sample means
hist(xbars, breaks = 30, col = "lightgreen", main = "Sampling Distribution of the Sample Mean" ,xlab = "Sample Means")

# 4a) Mean of the sample means
mean_of_xbars <- mean(xbars)
# 4b) Standard deviation of the sample means
sd_of_xbars <- sd(xbars)
mean_of_xbars
## [1] 1.997132
sd_of_xbars
## [1] 0.2808969