You are on page 1of 14

10.

2 Hypothesis Testing in R
Winter 2021
Mon: 4:30-6:20pm
Wed: 5:00-6:50pm

Professor: Clarence Au
Date: Apr 14th, 2021
Learning Objectives
12.7  Perform hypothesis tests using R.

Textbook

Practice Questions
Review – Finding P-values

Two-Sided Positive (Upper) Negative (Lower)


One-Sided One-Sided

Test Positive or negative Positive Negative


statistic
PTAB Find closest value in Find closest value in Find closest value
positive or negative positive table in negative table
table
P-value Positive:
Negative:

Image
R CODE
n<-2000
p<-0.86/100
p.hat<-0.53/100

Example SD<-sqrt(p*(1-p)/n)
z<-(p.hat-p)/SD
p.value<-pnorm(z, 0, 1,
lower.tail=TRUE)

A supplier of stainless steel kitchen utensils is having 0.86% of its merchandise


returned as a result of corrosion of the steel. The company improves the quality
control on the production process, monitors 2000 shipments chosen at random,
and has 0.53% of the merchandise returned for corrosion issues. We are hoping
that the proportion of merchandise returned will go down. Test at the 0.05 level of
significance.
1) H0 : p = 0.0086 HA : p < 0.0086

2) Independence + Randomization (Yes), 2000 shipments <10% population, 0.0086x2000 = 17.2>10


(1-0.0086)x2000 = 1982.8 > 10
= 0.0053
= 0.0053
p = 0.0086
SD() .= sqrt ((0.0086) (1-0.0086) / 2000) = 0.002064708

= (0.0053 – 0.0086) / 0.002064708 = -1.60


p= 0.0086
p-value = 0.0548 > 0.05, fail to reject null hypothesis. SD()= 0.002
No evidence that merchandize return has decreased.
A Guide to R – Using Functions to Preform
Hypothesis Tests

input
function_name<-function(var){

Do something a function

return(new_variable) }
output
function_name(var1, var2…)
Building a Hypothesis Testing Function

n p0 ^α
𝑝 Alternative (One sided/two sided)

q = 1- p 1) Negative (do
SD() nothing)
2) Positive (set to
negative z)
p-value = pnorm(z, 0, 1, lower.tail= TRUE)
1) One sided (<, >) =
1xpnorm
2) Two sided (≠) =
2xpnorm

p-value, the decision


Example

Using α = 0.05, make a decision.


90% of all Canadian homes have at least one smoke detector.
Building inspectors visit 400 randomly selected homes and find
that 376 have smoke detectors. Is this strong evidence that the
local rate is higher than the national rate?

p.test(376/400, 400, 0.90, 0.05, "one")


Example

A supplier of stainless steel kitchen utensils is having 0.86% of its


merchandise returned as a result of corrosion of the steel. The
company improves the quality control on the production process,
monitors 2000 shipments chosen at random, and has 0.53% of
the merchandise returned for corrosion issues. We are hoping
that the proportion of merchandise returned will go down. Test at
the 0.05 level of significance.

p.test(0.0053, 2000, 0.0086, 0.05, "one")


Example

The analyst at SmartWool makes another change to the website,


puts it online, selects 200 recent web visits at random, and finds
that 29% of them have resulted in a sale. Would this be a
surprising proportion of sales if the true proportion of sales were
20%? We are interested in whether the proportion of visits
resulting in a sale has changed since the new website went live.
Test at the 0.01 significance level.

p.test(0.29, 200, 0.20, 0.01, "two")


Using an R Function

Instead of developing our own function the prop.test() function


can be used:

Function Use Arguments


prop.test()= Hypothesis x = number of successes in the sample
tests for n = sample size
proportions p = population proportion (parameter)
alternative = “two.sided”, “less”, “greater”
conf.level = 0.95 which corresponds to α =
0.05 by default
correct = FALSE for every test in this course
Example

A supplier of stainless steel kitchen utensils is having 0.86% of its


merchandise returned as a result of corrosion of the steel. The
company improves the quality control on the production process,
monitors 2000 shipments chosen at random, and has 0.53% of
the merchandise returned for corrosion issues. We are hoping
that the proportion of merchandise returned will go down. Test at
the 0.05 level of significance.

prop.test(0.0053*2000, 2000, 0.0086, alternative


= "less", correct = FALSE)
Example

Using α = 0.05, make a decision.


90% of all Canadian homes have at least one smoke detector.
Building inspectors visit 400 randomly selected homes and find
that 376 have smoke detectors. Is this strong evidence that the
local rate is higher than the national rate?

prop.test(376, 400, 0.90, alternative =


"greater", correct = FALSE)
Example

The analyst at SmartWool makes another change to the website,


puts it online, selects 200 recent web visits at random, and finds
that 29% of them have resulted in a sale. Would this be a
surprising proportion of sales if the true proportion of sales were
20%? We are interested in whether the proportion of visits
resulting in a sale has changed since the new website went live.
Test at the 0.01 significance level.

prop.test(0.29*200, 200, 0.20,


alternative="two.sided", conf.level=0.99,
correct=FALSE)
Learning Check

In a recent year, Canadian households in the territories and


provinces other than Quebec accounted for 75% of national
households. You interview a random sample of households this
year to see whether the proportion of non-Quebec based
households has increased. Suppose that all assumptions are met.
A sample of size 100 results in a sample proportion of 0.80. Test
at the 0.05 level of significance. Use both functions.

Solutions within R Script.

You might also like