I am using R and plain English to express my question. Let us say I have a "true"/made up population, which is normally distributed with a mean of 500000 and a standard deviation of 13000:
mean <- 500000
sd <- 13000
population <- rnorm(10000000, mean=mean, sd=sd)
I can then repeatedly sample from this population using different samples sizes ranging from 3 to 150. For each sample, I also calculate the 95% confidence interval using a t distribution. I think this is safe as at the beginning my sample size is pretty low (i.e. 3). This is not really exploited here but I think this would be correct?
results <- NULL
for (sample_size_ in 3:150) {
for (sample_number_ in 1:10) {
sample_ <- sample(population, sample_size_)
ci_test <- t.test(sample_, conf.level=0.95)
df <- data.frame(
sample_size = sample_size_
, low_ci = ci_test$conf.int[[1]]
, up_ci = ci_test$conf.int[[2]]
, mean = mean(sample_)
)
results <- rbind(results, df)
}
}
I can then plot the data frame, which nicely shows how the sample means gets closer to the population mean (red dotted line) as the sample size increases:
ggplot(results, aes(y = mean, x = sample_size)) +
geom_point() +
geom_hline(yintercept=mean, linetype="dotted", color = "red", size=3)
This is the output (the actual code uses a theme):
I guess this is straightforward but how can I determine these blue lines expressing the 95% theoretical uncertainty of the sample mean being inside the blue area?
Is there a formula and does it depend on the knowledge of that the true distribution is normal? I guess it should not, as the sample means will be normally distributed according to the CLT?
Thanks.
PS:
This is the current/final code:
mu_ <- 500000
sd_ <- 13000
z_value <- qnorm(.975)
max_sample_size = 500
repeats = 2
results <- NULL
upper_and_lower <- NULL
for (sample_size_ in 3:max_sample_size) {
for (sample_number_ in 1:repeats) {
sample_ <- rnorm(sample_size_, mean=mu_, sd=sd_)
ci_test <- t.test(sample_, conf.level=0.95)
df <- data.frame(
sample_size = sample_size_
, low_ci = ci_test$conf.int[[1]]
, up_ci = ci_test$conf.int[[2]]
, mean = mean(sample_)
)
results <- rbind(results, df)
}
df <- data.frame(
sample_size = sample_size_
, upper = mu_ + (z_value * (sd_/sqrt(sample_size_)))
, lower = mu_ - (z_value * (sd_/sqrt(sample_size_)))
)
upper_and_lower <- rbind(upper_and_lower, df)
}
ggplot() +
geom_point(data=results, aes(y = mean, x = sample_size)) +
geom_line(data=upper_and_lower, aes(y = upper, x = sample_size), color = "blue", size=2) +
geom_line(data=upper_and_lower, aes(y = lower, x = sample_size), color = "blue", size=2)
Resulting graph with theoretical 95% uncertainty band:
t.test
in R will be about $50000 \pm 2 S_n/\sqrt{n}$ for $n > 30.$ For smooth curves, maybe use upper curve $50000 + 2(13000)/\sqrt{n}$ from $n = 30$ to $150.$ [Lower curve with $-.$] $\endgroup$