#This is a second example from Topic 16
# The problem is:
# 1) we have a population with an unknown mean
# and unknown standard deviation.
# 2) we have a null hypothesis that the mean of the
# population is 135.3
# 3) we have an alternative hypothesis that the mean
# of the population is not equal to 135.3
# 4) we want to test the null hypothesis against the
# alternative hypothesis at the 0.03 level of
# significance.
# 5) to do this we take a random sample of size 32
# 6) compute the sample mean and standard deviation;
# sample mean = 138.8 sample standard deviation=9.2
# -----------------------
# 7) at this point we could use the critical value
# approach and find the critical low value and critical
# high values for the sample mean, the values that have
# probability that a sample mean will be either less than
# the critical low value or higher than the critical high
# value = 0.03, The probabilities need to be split evenly.
# 8) if the sample mean is less than the critical low
# or higher than the critical high
# value then we reject H0 in favor of H1, if not then
# we do not have enough evidence to reject H0 in favor
# of H1.
# ------- alternatively -----
# 9) we could use the attained significance approach and
# determine the probability of getting a sample mean
# as extreme or more extreme than we just got. Note
# that we need to specify extreme or more extreme, not
# just lower or higher.
# 10) if that probability is less than the level of
# significance then we reject H0 in favor of H1,
# if not the we do not have enough evidence to reject
# H0 in favor of H1
################################################
# for the critical value approach first
# find the t value with 0.03/2 to its right
# for 31 degrees of freedom
high_t <- qt( 0.03/2, 31, lower.tail=FALSE )
high_t
# by symmetry, the low t values will be the opposite
low_t <- -high_t
low_t
#
# our critical low will be
135.3 + low_t*9.2/sqrt( 32 ) # remember that low_t is negative
# our critical high value will be
135.3 + high_t*9.2/sqrt( 32 ) # remember that high_t is positive
# Because our sample mean, 138.8 is not less than the
# critical low value and not higher than the
# critical high we do not reject H0 in favor of H1
# or use the attained significance approach
# First normalize the sample mean so that we can use
# the pt() function.
#
standard_t <- (138.8-135.3)/(9.2/sqrt(32))
standard_t # this becomes our test statistic
# find the probability of getting that value or more extreme.
# because this is a high value we will use lower.tail=FALSE
p_val <- pt( standard_t, 31, lower.tail=FALSE )
# That answer now needs to be doubled to account for
# being this extreme on the other side (lower side in this case).
p_val*2
# that answer, 0.03929236, is not less than our level of
# significance, 0.03 so we do not reject H0
# in favor of H1
#
# Or we could have just used our
# hypoth_test_unknown function to compute both
# approaches
source("../hypo_unknown.R")
# note the 0 to indicate that H1: mean != 135.3
hypoth_test_unknown( 135.3, 0, 0.03, 32, 138.8, 9.2)