Hypothesis Test, 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 hypothesis testing,
#   in this case for 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(170972412404,184000696)
#let us look at the head and tail values
head(L1)
tail(L1)
min(L1)
max(L1)
source("../pop_sd.R")
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: test the null hypothesis
# ##      that the population mean is equal to 60.0
# ##      against the alternative hypothesis that
# ##      the population mean is greater than 60.0.
# ##      Run the test at the 0.05 level of significance.
# ##########################

# 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, 4126) )
# 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

#  The long way to do the test is to find the probability 
#  getting this mean or higher if the true mean is 60.0 
#  given that the standard deviation of the population is
#  stored in sigma and that we have a sample of size 23 
#  from a population that we know to be normally distributed.
#
#  Since the means of samples of size 23 are normally 
#  distributed with mean = population mean and standard
#  deviation = pop sd/sqrt( n )  for the 
#  attained significance approach we just need to find
pnorm( xbar, mean=60.0, sd=sigma/sqrt(23),
       lower.tail=FALSE)
#  If that value is less than 0.05 then we reject the 
#  null hypothesis in favor of the alternative

#  For the critical value approach we first need to find the
#  value of x that has P(X>x)=0.05
x <- qnorm( 0.05, mean=60.0, sd=sigma/sqrt(23),
            lower.tail=FALSE)
x
# Then if xbar is greater than this value we reject the
#    null hypothesis in favor of the alternative
#
#   Of course, we could use the function
#   hypoth_test_known() to do both of these approaches in
#  one easy step.
#
source("../hypo_known.R")
hypoth_test_known( 60.0, sigma, 1, 0.05,
                   23, xbar)
#
#
#################################
# go back and execute lines 37-75 many more times.
#    Each time you get a different random sample.
#    Keep track of the number of times that you reject or
#    do not reject the null hypothesis.  By the way, the 
#    true mean of the population is 68.60962.  
#################################

#
#  now we will do the same thing for a different population
#
gnrnd5(146723412404,184000600)
#let us look at the head and tail values
head(L1)
tail(L1)
min(L1)
max(L1)

sigma <- pop_sd( L1 )
sigma
# just a quick look at L1
hist(L1)
boxplot(L1, horizontal=TRUE)

assess_normality( L1 )
#
#  L1 sure looks like a Normal distribution.
#
# ##########################
# ##  Problem: test the null hypothesis
# ##      that the population mean is equal to 60.0
# ##      against the alternative hypothesis that
# ##      the population mean is greater than 60.0.
# ##      Run the test at the 0.05 level of significance.
# ##########################

# 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, 4126) )
# 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

#  The long way to do the test is to find the probability 
#  getting this mean or higher if the true mean is 60.0 
#  given that the standard deviation of the population is
#  stored in sigma and that we have a sample of size 23 
#  from a population that we know to be normally distributed.
#
#  Since the means of samples of size 23 are normally 
#  distributed with mean = population mean and standard
#  deviation = pop sd/sqrt( n )  for the 
#  attained significance approach we just need to find
pnorm( xbar, mean=60.0, sd=sigma/sqrt(23),
       lower.tail=FALSE)
#  If that value is less than 0.05 then we reject the 
#  null hypothesis in favor of the alternative

#  For the critical value approach we first need to find the
#  value of x that has P(X>x)=0.05
x <- qnorm( 0.05, mean=60.0, sd=sigma/sqrt(23),
            lower.tail=FALSE)
x
# Then if xbar is greater than this value we reject the
#    null hypothesis in favor of the alternative
#
#   Of course, we could use the function
#   hypoth_test_known() to do both of these approaches in
#  one easy step.
#
source("../hypo_known.R")
hypoth_test_known( 60.0, sigma, 1, 0.05,
                   23, xbar)
#
#
#################################
# go back and execute lines 119-157 many more times.
#    Each time you get a different random sample.
#    Keep track of the number of times that you reject or
#    do not reject the null hypothesis.  By the way, the 
#    true mean of the population is 60.03343.  
#################################