#################Hypothesis Testing##########################################
################################
###Lower Tail Test of Population Mean with Known Variance###
##Suppose the manufacturer claims that the mean lifetime of a light bulb is
# more than 10,000 hours. In a sample of 30 light bulbs, it was found that they
# only last 9,900 hours on average. Assume the population standard deviation is 120 hours.
# At .05 significance level, can we reject the claim by the manufacturer?
xbar = 9900 # sample mean
mu0 = 10000 # hypothesized value
sigma = 120 # population standard deviation
n = 30
# sample size
z = (xbar-mu0)/(sigma/sqrt(n))
z # test statistic
# alpha = .05
# [Link] = qnorm(1-alpha)
# -[Link] # critical value
###Alternate Solution
pval = pnorm(z) # lower tail
pval
alpha = .01
[Link] = qnorm(1-alpha)
-[Link] # critical value
#[Link](17)
x = [Link](rnorm(30, mean = 9900, sd = 120)); x
mean(x)
#x = scan("[Link]")
xbar = mean(x) # sample mean
mu0 = 10000 # hypothesized value
sigma = 120 # population standard deviation
n = length(x) # sample size
z = (xbar-mu0)/(sigma/sqrt(n))
pval = pnorm(z)
pval
library(TeachingDemos)
test1 = [Link](x, mu=mu0, stdev=sigma,alternative="less")
test$[Link]
####Upper Tail Test of Population Mean with Known Variance
################################
# Suppose the food label on a cookie bag states that there is at most
# 2 grams of saturated fat in a single cookie. In a sample of 35 cookies,
# it is found that the mean amount of saturated fat per cookie is 2.1 grams.
# Assume that the population standard deviation is 0.25 grams.
# At .05 significance level, can we reject the claim on food label?
xbar = 2.1 # sample mean
mu0 = 2 # hypothesized value
sigma = 0.25 # population standard deviation
n = 35 # sample size
z = (xbar-mu0)/(sigma/sqrt(n))
z # test statistic
# alpha = .05
# [Link] = qnorm(1-alpha)
# [Link] # critical value
pval = pnorm(z, [Link]=FALSE)
pval
# alpha = .01
# [Link] = qnorm(1-alpha)
# [Link] # critical value
[Link](17)
x = rnorm(35, mean = 2, sd = 0.25); x
#x = scan("[Link]")
xbar = mean(x) # sample mean
mu0 = 2 # hypothesized value
sigma = 0.25 # population standard deviation
n = length(x) # sample size
z = (xbar-mu0)/(sigma/sqrt(n))
pval = pnorm(z, [Link]=FALSE)
pval
library(TeachingDemos)
test = [Link](x, mu=mu0, stdev=sigma, alternative="greater")
test$[Link]
####Two-Tailed Test of Population Mean with Known Variance
################################
# Suppose the mean weight of King Penguins found in an Antarctic colony last year
# was 15.4 kg. In a sample of 35 penguins same time this year in the same colony,
# the mean penguin weight is 14.6 kg. Assume the population standard deviation is
# 2.5 kg. At .05 significance level, can we reject
# the null hypothesis that the mean penguin weight does not differ from last year?
xbar = 14.6 # sample mean
mu0 = 15.4 # hypothesized value
sigma = 2.5 # population standard deviation
n = 35 # sample size
z = (xbar-mu0)/(sigma/sqrt(n))
z # test statistic
# alpha = .05
# [Link] = qnorm(1-alpha/2)
# c(-[Link], [Link])
pval = 2 * pnorm(z) # lower tail
pval # two-tailed p-value
# pval = 2*ifelse(z < 0, pnorm(z),
# pnorm(z, [Link]=T))
# pval
#
alpha = .01
[Link] = qnorm(1-alpha/2)
c(-[Link], [Link])
[Link](17)
x = rnorm(35, mean = 14.6, sd = 2.5); x
x = scan("[Link]")
xbar = mean(x)
xbar # sample mean
mu0 = 15.4 # hypothesized value
sigma = 2.5 # population standard deviation
n = length(x) # sample size
z = (xbar-mu0)/(sigma/sqrt(n))
pval = 2 * pnorm(z) # as xbar <= mu0
pval # two-tailed p-value
library(TeachingDemos)
test = [Link](x, mu=mu0, stdev=sigma)
test$[Link]
####Lower Tail Test of Population Mean with Unknown Variance
################################
# Suppose the manufacturer claims that the mean lifetime of a light bulb is
# more than 10,000 hours. In a sample of 30 light bulbs, it was found that
# they only last 9,900 hours on average. Assume the sample standard deviation
# is 125 hours. At .05 significance level, can we reject the claim by the manufacturer?
xbar = 9900 # sample mean
mu0 = 10000 # hypothesized value
s = 125 # sample standard deviation
n = 30 # sample size
[Link] = (xbar-mu0)/(s/sqrt(n))
[Link] # test statistic
# alpha = .05
# [Link] = qt(1-alpha, df=n-1)
# -[Link] # critical value
pval = pt([Link], df=n-1)
pval # lower tail p-value
# Ex 1
alpha = .01
n = 30
[Link] = qt(1-alpha, df=n-1)
-[Link] # critical value
# Ex 2
#[Link](17)
#x = [Link](rnorm(30, mean = 9900, sd = 120)); x
x = scan("[Link]")
xbar = mean(x) # sample mean
mu0 = 10000 # hypothesized value
s = sd(x) # sample standard deviation
n = length(x) # sample size
[Link] = (xbar-mu0)/(s/sqrt(n))
pval = pt([Link], df=n-1)
pval # lower tail p-value
test = [Link](x, mu=mu0, alternative="less")
test$[Link]
###Upper Tail Test of Population Mean with Unknown Variance
################################
# Suppose the food label on a cookie bag states that there is at most 2 grams of
# saturated fat in a single cookie. In a sample of 35 cookies, it is found that the
# mean amount of saturated fat per cookie is 2.1 grams. Assume that the sample
# standard deviation is 0.3 gram.
# At .05 significance level, can we reject the claim on food label?
xbar = 2.1 # sample mean
mu0 = 2 # hypothesized value
s = 0.3 # sample standard deviation
n = 35 # sample size
[Link] = (xbar-mu0)/(s/sqrt(n))
[Link] # test statistic
alpha = .05
[Link] = qt(1-alpha, df=n-1)
[Link] # critical value
pval = pt([Link], df=n-1, [Link]=FALSE)
pval # upper tail p-value
# Ex 1
alpha = .01
n = 35
[Link] = qt(1-alpha, df=n-1)
[Link] # critical value
# Ex 2
#[Link](17)
#x = rnorm(35, mean = 2, sd = 0.25); x
x = scan("[Link]")
xbar = mean(x) # sample mean
mu0 = 2 # hypothesized value
s = sd(x) # sample standard deviation
n = length(x) # sample size
[Link] = (xbar-mu0)/(s/sqrt(n))
pval = pt([Link], df=n-1, [Link]=FALSE)
pval # upper tail p-value
test = [Link](x, mu=mu0, alternative="greater")
test$[Link]
#####Two-Tailed Test of Population Mean with Unknown Variance
################################
# Suppose the mean weight of King Penguins found in an Antarctic colony last year
# was 15.4 kg. In a sample of 35 penguins same time this year in the same colony,
# the mean penguin weight is 14.6 kg. Assume the sample standard deviation is 2.5 kg.
# At .05 significance level, can we reject the null
# hypothesis that the mean penguin weight does not differ from last year?
xbar = 14.6 # sample mean
mu0 = 15.4 # hypothesized value
s = 2.5 # sample standard deviation
n = 35 # sample size
[Link] = (xbar-mu0)/(s/sqrt(n))
[Link] # test statistic
alpha = .05
[Link] = qt(1-alpha/2, df=n-1)
c(-[Link], [Link])
pval = 2 * pt([Link], df=n-1) # lower tail
pval # two-tailed p-value
pval = 2 * ifelse([Link] < 0, pt([Link], df=n-1),
pt([Link], df=n-1, [Link]=FALSE))
pval # two-tailed p-value
# Ex 1
alpha = .01
n = 35
[Link] = qt(1-alpha/2, df=n-1)
c(-[Link], [Link])
# Ex 2
#[Link](17)
#x = rnorm(35, mean = 14.6, sd = 2.5); x
x = scan("[Link]")
xbar = mean(x)
xbar # sample mean
mu0 = 15.4 # hypothesized value
s = sd(x) # sample standard deviation
n = length(x) # sample size
[Link] = (xbar-mu0)/(s/sqrt(n))
pval = 2 * pt([Link], df=n-1) # as xbar <= mu0
pval # two-tailed p-value
test = [Link](x, mu=mu0)
test$[Link]
###Lower Tail Test of Population Proportion
################################
# Suppose 60% of citizens voted in last election. 85 out of 148 people in a
# telephone survey said that they voted in current election. At 0.05 significance
# level, can we reject the null hypothesis
# that the proportion of voters in the population is above 60% this year?
pbar = 85/148 # sample proportion
p0 = .6 # hypothesized value
n = 148 # sample size
z = (pbar-p0)/sqrt(p0*(1-p0)/n)
z # test statistic
# alpha = .05
# [Link] = qnorm(1-alpha)
# -[Link] # critical value
pval = pnorm(z)
pval # lower tail p-value
[Link](85, 148, p=.6, alt="less", correct=F)
# Ex
alpha = .01
[Link] = qnorm(1-alpha)
-[Link] # critical value
#####Upper Tail Test of Population Proportion
################################
# Suppose that 12% of apples harvested in an orchard last year was rotten.
# 30 out of 214 apples in a harvest sample this year turns out to be rotten.
# At .05 significance level, can we reject the null hypothesis
# that the proportion of rotten apples in harvest stays below 12% this year?
pbar = 30/214 # sample proportion
p0 = .12 # hypothesized value
n = 214 # sample size
z = (pbar-p0)/sqrt(p0*(1-p0)/n)
z # test statistic
alpha = .05
[Link] = qnorm(1-alpha)
[Link] # critical value
pval = pnorm(z, [Link]=FALSE)
pval # upper tail p-value
[Link](30, 214, p=.12, alt="greater", correct=FALSE)
# Ex
alpha = .01
[Link] = qnorm(1-alpha)
[Link] # critical value
###Two-Tailed Test of Population Proportion
################################
# Suppose a coin toss turns up 12 heads out of 20 trials. At .05 significance level,
# can one reject the null hypothesis that the coin toss is fair?
pbar = 12/20 # sample proportion
p0 = .5 # hypothesized value
n = 20 # sample size
z = (pbar-p0)/sqrt(p0*(1-p0)/n)
z # test statistic
alpha = .05
[Link] = qnorm(1-alpha/2)
c(-[Link], [Link])
pval = 2 * pnorm(z, [Link]=FALSE) # upper tail
pval # two-tailed p-value
[Link](12, 20, p=0.5, correct=FALSE)
# Ex
alpha = .01
[Link] = qnorm(1-alpha/2)
c(-[Link], [Link])