A normal sample of size 500 is used. We create 5000 such samples and record their mean values.
The calculated mean will be slightly different from the population mean within margin of error (moe).
For 5 to 95% confidence level, the moe is found and the proportion of means inside the range. We expect a linear line with slope of 1 (y = x)
# ex13.R
N <- 5000
n <- 500
p <- 0.6
val <- numeric(N)
pop.sd <- runif(1,10,25)
pop.mn <- p*n
for (j in 1:N) {
s <- rnorm(n, mean = pop.mn, sd = pop.sd)
val[j] <- mean(s)
}
# In general, population values are not known.
# Only 1 sample will be used in practice.
# Many samples are used for the CLT
use.samp <- TRUE
if (use.samp) {
mn1 <- mean(val)
sd1 <- sd(val)
} else {
mn1 <- pop.mn
sd1 <- pop.sd/sqrt(n)
}
conf.range <- seq(5,95,5)
v <- numeric(length(conf.range))
for (i in 1:length(conf.range)) {
per <- conf.range[i]
z <- qnorm((100+per)/200)
moe <- z * sd1
ins <- (val > mn1 - moe) & (val < mn1 + moe)
v[i] <- sum(ins)/N
}
plot(conf.range,v*100, type='l', col='blue',
xlab = 'Confidence level',
ylab = 'Proportion mean within moe',
xlim = c(0,100), ylim = c(0,100))
Output:
No comments:
Post a Comment