Confidence Interval, Sigma Known


On your USB drive, create a new directory, copy model.R to that directory, rename the file in the new directory, double click on the file to open Rstudio. Then copy all of the text below the line and paste it into your Rstudio editor pane.
  
# Line 1: a small demonstration of getting a 
#   confidence interval for the mean 
#   of a population with known standard deviation.
#
#  First, we will get a population
#  In this case we will get a large population
source("../gnrnd5.R")
gnrnd5(170972734104,235001365)
#let us look at the head and tail values
head(L1)
tail(L1)
min(L1)
max(L1)
source("../pop_sd.R")
#  Now we will find the population standard deviation
sigma <- pop_sd( L1 )
sigma
# just a quick look at L1
hist(L1)
boxplot(L1, horizontal=TRUE)
source("../assess_normality.R")
assess_normality( L1 )
#
#  L1 sure looks like a Normal distribution.
#
# ##########################
# ##  Problem: find the 95% confidence interval
# ##      for the mean of the population
# ##########################

# take a simple random sample of size 23
#
#  Be careful:  Every time we do this we get 
#               a different random sample
#
L2 <- as.integer( runif(23, 1, 7343) )
# L2  holds the index values of our simple random sample
L2
L3 <- L1[ L2 ]   # L3 holds the simple random sample
L3
# we will get the mean of L3
xbar <- mean(L3)
xbar
#
#  Remember that the distribution of sample means
#  will be normal with the same mean as the population
#  and standard deviation equal to the population 
#  standard deviation divided by the square root of
#  the sample size.
#
#  The long way to generate the confidence interval
#  is to find the z-value in a standard normal
#  distribution such that there is 95% of the 
#  area between -z and z.  That means that 2.5% is 
#  less than -z and 2.5% is greater that z. We can find
#  that z value via qnorm.
#
z <- qnorm(0.025, lower.tail=FALSE)
z
#
#  Then our margin of error is z*sigma/sqrt(23)
#
moe <- z*sigma/sqrt(23)
moe
#
#  and our confidence interval is between
#  xbar-moe and xbar+moe
#
xbar - moe
xbar + moe
#
#   Of course, we could use the function
#   ci_known() to do this in
#   one easy step.
#
source("../ci_known.R")
ci_known( sigma, 23, xbar, 0.95 )
#
#
#################################
# go back and execute lines 36-77 many more times.
#    Each time you get a different random sample.
#    Therefore, each time you get a different 
#    confidence interval.  Note that the margin
#    of error stays the same because we know the
#    population standard deviation. By the way, the 
#    true mean of the population is 136.4781.  
#################################