1 Introduction

As a data scientist you probably retain or reject hypothesis based on measurements of observed samples. The decision is often based on a statistical mechanism called hypothesis testing. Let’s watching the following video:


There are three conditions of having hypothesis testing included:

  • Left Tailed Test: When the \(\bar{x}\) is significantly below the hypothesised population mean \(µ_0\) then \(H_0\) will be rejected and the test used will be the left tailed test (lower tailed test) since the critical region (denoting rejection of \(H_0\)) will be in the left tail of the normal curve (representing sampling distribution of sample statistic \(\bar{x}\)).

\[\text{Left Tailed Test} = \begin{cases} {H_0: \mu \ge \mu_0} \\ {H1: \mu < \mu_0 } \end{cases}\]

  • Right Tailed Test: When the \(\bar{x}\) is significantly above the hypothesized population mean \(µ_0\) then \(H_0\) will be rejected and the test used will be right tailed test (upper tailed test) since the critical region (denoting rejection of \(H_0\) will be in the right tail of the normal curve (representing sampling distribution of sample statistic \(\bar{x}\) ).

\[\text{Right Tailed Test} = \begin{cases} {H_0: \mu \le \mu_0} \\ {H1: \mu > \mu_0 } \end{cases}\]

  • Two Tailed Test: When the \(\bar{x}\) is significantly different (significantly higher or lower than) from the hypothesis population mean \(µ_0\) then \(H_0\) will will be rejected. In this case, the two tailed test will be applicable because there will be two critical regions (denoting rejection of \(H_0\)) on both the tails of the normal curve (representing sampling distribution of sample statistic \(\bar{x}\)).

\[\text{Two Tailed Test} = \begin{cases} {H_0: \mu = \mu_0} \\ {H1: \mu \neq \mu_0 } \end{cases}\]

2 Hypothesis Testing

The critical regions for Hypothesis Testing are shown as shaded portions in the following figure:

Hypothesis Testing

Hypothesis Testing

On comparing the observed value of Test statistic with that of the critical value, we may identify whether the observed value lies in the critical region (reject \(H_0\)) or in the acceptance region (do not reject \(H_0\)) and decide accordingly.

  • Left Tailed Test: If \(Z_{Crit} < -1.645\), then reject \(H_0\) at 5% level of Significance (\(\alpha\) is taken as 5% in most of the analytic situations).
  • Right Tailed Test: If \(Z_{Crit} > 1.645\), then reject \(H_0\) at 5% level of Significance.
  • Two Tailed Test: If \(Z_{Crit}> 1.96\) or If \(Z_{Crit} < -1.96\), then reject \(H_0\) at 5% Level of Significance.

There is also an alternative approach for hypothesis testing, this approach is very much used in all the software packages. Here, you will fucos on the following statement:

  • If p-value \(< \alpha\): reject \(H_0\)
  • If p-value \(\ge \alpha\) : Fails to Reject \(H_0\)

Procedure for Finding P-Values:

P-values

P-values

3 Type of Error I & II

  • A Type I error is the mistake of rejecting the null hypothesis when it is true. The symbol \(\alpha\) (alpha) is used to represent the probability of a type I error.

  • A Type II error is the mistake of failing to reject the null hypothesis when it is false. The symbol \(\beta\) (beta) is used to represent the probability of a type II error.

Type of Error

Type of Error

4 Type I ~ One Tail Z-test

The null hypothesis of the One-tail (left/right) test of the population mean \(\mu\) and \(\sigma\) can be expressed as follows:

\[\text{Hypothesis Testing $H_0$} = \begin{cases} {\mu \ge \mu_0} & \text{Left Tail} \\ {\mu \le \mu_0} & \text{Right Tail} \end{cases}\]

where \(\mu_0\) is a hypothesized left/right bound of the true population mean \(\mu\).

Let us define the test statistic \(z\) in terms of the sample mean, the sample size and the population standard deviation \(\sigma\):

\[z={\bar{x}-\mu_0 \over \sigma/\sqrt{n}}\]

Then the null hypothesis of the left tail test is to be rejected if \(z \le −z_\alpha\) , where \(z_\alpha\) is the \(100(1-\alpha)\) percentile of the standard normal distribution.

4.1 Example 1

Left Tail: Suppose the manufacturer claims that the mean speed of a motorcycle is more than 100 km/hours. In a sample of 30 motorcycles, it was found that they only last 99 km/hours on average. Assume the population standard deviation is 1.2 km/hours. At .05 significance level, can we reject the claim by the manufacturer?

4.1.1 Z-test statistics

First, we calculate the z-test statistics according to the information that we have from the Example 1. In this case, we use z-statistics because we know the mean \(\mu\) and standard deviation \(\sigma\), also we know that the sample size \(\ge 30\).

mu0 = 100                                              # hypothesized value 
xbar = 99                                              # sample mean 
sigma = 1.2                                            # population standard deviation 
n = 30                                                 # sample size 
z = (xbar-mu0)/(sigma/sqrt(n));z                       # test statistic 
## [1] -4.564355

4.1.2 Critical value

Then, we calculate the left critical value.

alpha = .05                                            # .05 significance level
z.alpha = qnorm(1-alpha)                               # right tail critical value
-z.alpha                                               # left tail critical value 
## [1] -1.644854

Now, we can conclude that the test statistic -4.5644 is less than the critical value of -1.6449. Consequently, at .05 significance level, we reject the claim that mean lifetime of a motorcycle is above 100 km/hours.

4.1.3 P-value

Alternative solution: Instead of using the critical value, we apply the pnorm function to compute the left tail p-value of the test statistic. As it turns out to be less than the .05 significance level, we reject the null hypothesis that \(μ \ge 100\).

pval = pnorm(z)                                        # left tail p−value
pval                                                   # print p−value
## [1] 2.505166e-06

4.2 Exercise 1

Right Tail: A food company argue that for each a cookie bag of their products, there is at most 2 grams of saturated fat in a single cookie. In a sample of 40 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?

Solution:

The null hypothesis is \(μ ≤ 2\). Since, we know the sigma, then we use z statistics.After that, we compute the critical value at 0.05 significance level.

# Sample mean
xbar= 2.1 

# Sigma (Population standard deviation) 
sigma = 0.25

# Hypothesized value
mu0 = 2

# Sample size
n = 40

# Z- statistics 
z = (xbar - mu0)/(sigma/sqrt(n))
z
## [1] 2.529822
# Alpha
alpha = 0.05
z.alpha = qnorm(1-alpha)

# Critical Value
z.alpha
## [1] 1.644854

So, the test statistics 2.529822 is greater that the critical value of 1.644854. Right tailed test: if \(Z_{Crit}\) \(> Z\) then reject \(H_0\) at 5% level of significance. Therefore, at 0.05 significance level, we reject the claim that there is at most 2 grams of saturated fat in a cookie.

\(Alternative Solution\)

pval = 1- pnorm (z)
# Upper tail value
pval
## [1] 0.005706018

We also can use pnorm function to compute the upper tail p-value. And it turns out to be less than 0.05 significance level, so we reject the null hypothesis that \(μ ≤ 2\).

5 Type I ~ Two Tail Z-test

The null hypothesis of the two-tailed test of the population mean \(\mu\) and \(\sigma\) can be expressed as follows:

\[\mu_0 = \mu\]

where \(\mu_0\) is a hypothesized value of the true population mean \(\mu\).

Let us define the test statistic \(z\) in terms of the sample mean, the sample size and the population standard deviation \(\sigma\):

\[z={\bar{x}-\mu_0 \over \sigma/\sqrt{n}}\]

Then the null hypothesis of the two-tailed test is to be rejected if \(z \le - z_{\alpha/2}\) or \(z \ge z_{\alpha/2}\) , where \(z_{\alpha/2}\) is the \(100(1-\alpha/2)\) percentile of the standard normal distribution.

5.1 Example 2

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?

5.1.1 Z-test statistics

First, we calculate the z-test statistics according to the information that we have from the Example 2. In this case, we use z-statistics because we know the mean \(\mu\) and standard deviation \(\sigma\), also we know that the sample size \(\ge 30\).

mu0 = 15.4                                             # hypothesized value 
xbar = 14.6                                            # sample mean 
sigma = 2.5                                            # population standard deviation 
n = 35                                                 # sample size 
z = (xbar-mu0)/(sigma/sqrt(n));z                       # test statistic 
## [1] -1.893146

5.1.2 Critical value

Then, we calculate the left critical value.

alpha = .05                                            # .05 significance level
z.half.alpha = qnorm(1-alpha/2)                        # per-one tail .025 significance level
c(-z.half.alpha, z.half.alpha)                         # Two-Tailed 0.05 significance level   
## [1] -1.959964  1.959964

The test statistic -1.8931 lies between the critical values -1.9600 and 1.9600. Hence, at .05 significance level, we do not reject the null hypothesis that the mean penguin weight does not differ from last year.

5.1.3 P-value

Alternative solution: Instead of using the critical value, we apply the 2*pnorm() function to compute the two tail p-value of the test statistic.

pval=2*pnorm(-abs(z))                                  # two tail p−value
pval                                                   # print p−value
## [1] 0.05833852

As it turns out to be greater than the .05 significance level, we do not reject the null hypothesis that \(μ \ge 100\).

5.2 Exercise 2

To test the hypothesis that the mean systolic blood pressure in a certain population equals 140 mmHg. The standard deviation has a known value of 20 and a data set of 55 patients is available.

#Blood_pressure dataset
no <- seq(1:55)
status <- c(rep(0, 25), rep(1, 30))
mmhg <- c(120,115,94,118,111,102,102,131,104,107,115,139,115,113,114,105,
          115,134,109,109,93,118,109,106,125,150,142,119,127,141,149,144,
          142,149,161,143,140,148,149,141,146,159,152,135,134,161,130,125,
          141,148,153,145,137,147,169)
blood_pressure <-data.frame(no,status,mmhg)
blood_pressure
##    no status mmhg
## 1   1      0  120
## 2   2      0  115
## 3   3      0   94
## 4   4      0  118
## 5   5      0  111
## 6   6      0  102
## 7   7      0  102
## 8   8      0  131
## 9   9      0  104
## 10 10      0  107
## 11 11      0  115
## 12 12      0  139
## 13 13      0  115
## 14 14      0  113
## 15 15      0  114
## 16 16      0  105
## 17 17      0  115
## 18 18      0  134
## 19 19      0  109
## 20 20      0  109
## 21 21      0   93
## 22 22      0  118
## 23 23      0  109
## 24 24      0  106
## 25 25      0  125
## 26 26      1  150
## 27 27      1  142
## 28 28      1  119
## 29 29      1  127
## 30 30      1  141
## 31 31      1  149
## 32 32      1  144
## 33 33      1  142
## 34 34      1  149
## 35 35      1  161
## 36 36      1  143
## 37 37      1  140
## 38 38      1  148
## 39 39      1  149
## 40 40      1  141
## 41 41      1  146
## 42 42      1  159
## 43 43      1  152
## 44 44      1  135
## 45 45      1  134
## 46 46      1  161
## 47 47      1  130
## 48 48      1  125
## 49 49      1  141
## 50 50      1  148
## 51 51      1  153
## 52 52      1  145
## 53 53      1  137
## 54 54      1  147
## 55 55      1  169

So, first we calculate the sample mean and find the total of sample size. The, we set the null hypothesis and sigma to find z. Then, we also search pvalueA, B, and C.

# Sample mean and Total sample size
xbar = mean(blood_pressure$mmhg)
n = length(blood_pressure$mmhg)

# Set null hypothesis and sigma
mu0 = 140
sigma = 20

# z- statistics
z = (xbar-mu0)/(sigma/sqrt(n))
z
## [1] -3.708099
# p value A, B, and C
p_valueA = 2*pnorm(-abs(z)) #twotail
p_valueB = 1- pnorm (z) #righttail
p_valueC = pnorm(z) #left tail

p_valueA
## [1] 0.0002088208
p_valueB
## [1] 0.9998956
p_valueC
## [1] 0.0001044104

This is to test if the mean \(μ\) differ from a specific value \(μ_0\). The are three hypothesis are

A. \(H_0\) : \(μ=μ_0\) vs \(H_1\) : \(μ≠μ_0\). #Two tailed

B. \(H_0\) : \(μ≤μ_0\) vs \(H_1\) : \(μ>μ_0\). #Right tailed

C. \(H_0\) : \(μ≥μ_0\) vs \(H_1\) : \(μ<μ_0\). #Left tailed

Test decision: Rejection \(H_0\) if for the observed value \(z\) of \(Z\):

\(z<z_{α/2}\) or $z>z_{1−α/2}

\(z>z_{1−α}\)

\(z<z_α\)

P-value:

\(ρ=2ϕ(−|z|)\)

\(ρ=1−ϕ(z)\)

\(ρ=ϕ(z)\)

So, first we calculate the sample mean and find the total of sample size. The, we set the null hypothesis and sigma to find z. Then, we also search pvalueA, B, and C.

Then we got the result:

\(z\) = -3.708099 p_valueA = 0.0002088208 p_valueB = 0.9998956 p_valueC = 0.0001044104

As it turns out A dan C to be less than 0.05 significance level, so we reject the null hypothesis. Since, p value B turns out to be greater than 0.05 significance level, so we accept the null hypothesis ort \(H_0\) : \(μ≤μ_0\) or \(μ≤140\) for this exercise.

6 Type I ~ One Tail t-test

The null hypothesis of the one-tail (left/right) test of the population mean \(\mu\) and unknown \(\sigma\) can be expressed as follows:

\[\text{Hypothesis Testing $H_0$} = \begin{cases} {\mu \le \mu_0} & \text{Left Tail} \\ {\mu \ge \mu_0} & \text{Right Tail} \end{cases}\]

where \(\mu_0\) is a hypothesized left/right bound of the true population mean \(\mu\).

Let us define the test statistic \(t\) in terms of the sample mean, the sample size and the sample standard deviation \(s\):

\[t={\bar{x}-\mu_0 \over s/\sqrt{n}}\]

Then the null hypothesis of the lower tail test is to be rejected if \(t\le−t_\alpha\) , where \(t_\alpha\) is the \(100(1 − \alpha)\) percentile of the Student \(t\) distribution with \(n − 1\) degrees of freedom.

6.1 Example 3

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?

6.1.1 T-test statistics

First, we calculate the t-test statistics according to the information that we have from the Example 1. In this case, we use t-statistics because we dont know the mean \(\mu\) and standard deviation \(\sigma\) of pupulation, also we know that the sample size \(\ge 30\).

mu0 = 10000                                            # hypothesized value  
xbar = 9900                                            # sample mean 
s = 125                                                # sample standard deviation 
n = 30                                                 # sample size 
t = (xbar-mu0)/(s/sqrt(n));t                           # test statistic  
## [1] -4.38178

6.1.2 Critical value

Then, we calculate the left critical value.

alpha = .05                                            # use 0.05 left tail significant level 
t.alpha = qt(1-alpha, df=n-1)                          # right tail critical value    
-t.alpha                                               # left tail critical value 
## [1] -1.699127

The test statistic -4.3818 is less than the critical value of -1.6991. Hence, at .05 significance level, we can reject the claim that mean lifetime of a light bulb is above 10,000 hours.

6.1.3 P-value

Alternative Solution: Instead of using the critical value, we apply the pt function to compute the lower tail p-value of the test statistic.

pval = pt(t, df=n-1)                                   # left tail p−value
pval                                                   # print p−value
## [1] 7.035026e-05

As it turns out to be less than the .05 significance level, we reject the null hypothesis that \(\mu \ge 10000.\)

6.2 Exercise 3

Right tail: Garuda-food Indonesia claims that for each a cookie bag states of their product, there is at most 2 grams of saturated fat in a single cookie. In a sample of 40 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?

# sample mean
xbar= 2.1

# Hypothesized value
mu0 = 2

# Sample standard deviation
s= 0.3

# Sample size
n = 40

# T-test
t = (xbar-mu0)/(s/sqrt(n))
t
## [1] 2.108185
# Find the critical value
alpha = 0.05
t.alpha = qt(1-alpha, df = n-1)
t.alpha 
## [1] 1.684875

So, first we define the null hypothesis which is \(μ≤2\) . Then, we write the sample mean, sample standard deviation, sample size to find the t test value. After that we compare it with the critival value.

The t- test 2.108185 is freater than the critical value which is 1.684875. So, at the 0.05 significance level, we reject the claim that there is at most 2 grams of saturated fat in a cookie

\(Alternative Soltion\)

pval = pt(t, df= n-1, lower.tail = FALSE)
pval
## [1] 0.020746

It turns out that p value is 0.020746 which is less than 0.05 significance level. So, we reject the null hypothesis.

7 Type I ~ Two Tail T-test

The null hypothesis of the two-tailed test of the population mean \(\mu\) and unknown \(\sigma\) can be expressed as follows:

\[\mu_0 = \mu\]

where \(\mu_0\) is a hypothesized value of the true population mean \(\mu\).

Let us define the test statistic \(t\) in terms of the sample mean, the sample size and the sample standard deviation \(s:\)

\[t={\bar{x}-\mu_0 \over s/\sqrt{n}}\]

Then the null hypothesis of the two-tailed test is to be rejected if \(t\le-t_{\alpha∕2}\) or \(t\ge t_{\alpha∕2}\) , where \(t_{\alpha∕2}\) is the \(100(1-\alpha)\) percentile of the Student \(t\) distribution with \(n−1\) degrees of freedom.

7.1 Example 4

Some journals concluded that the average weight of Hachiko Dogs around the world last ten years was 15.4 kg. Researchers want to make sure if there a change in the average weight of these varieties after ten years. Therefore, they pick up a random sample of 35 Dogs from the same varieties and at the same time this year, they found that 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 Hachiko Dogs does not differ from last ten years?

7.1.1 T-test statistics

First, we calculate the t-test statistics according to the information that we have from the Example 2. In this case, we use t-statistics because we know the mean \(\mu\) and standard deviation \(\sigma\), also we know that the sample size \(\ge 35\).

mu0 = 15.4                                             # hypothesized value 
xbar = 14.6                                            # sample mean 
s = 2.5                                                # sample standard deviation 
n = 35                                                 # sample size 
t = (xbar-mu0)/(s/sqrt(n));t                           # test statistic 
## [1] -1.893146

7.1.2 Critical value

Then, we calculate the left critical value.

alpha = .05                                            # .05 significance level
t.half.alpha = qt(1-alpha/2, df=n-1)                   # per-one tail .025 significance level
c(-t.half.alpha, t.half.alpha)                         # Two-Tailed 0.05 significance level
## [1] -2.032245  2.032245

The test statistic -1.8931 lies between the critical values -2.04523 and 2.04523. Hence, at .05 significance level, we do not reject the null hypothesis that the mean penguin weight does not differ from last year.

7.1.3 P-value

Alternative solution: Instead of using the critical value, we apply the 2*pt() function to compute the two tail p-value of the test statistic.

pval = 2*pt(t, df=n-1)                                 # two tail p−value
pval                                                   # print p−value
## [1] 0.06687552

Since it turns out to be greater than the .05 significance level, we do not reject the null hypothesis that \(μ \ge 15.4\).

7.2 Exercise 4

To test the hypothesis that the mean systolic blood pressure in a certain population equals 140 mmHg. The dataset at hands has measurements on 55 patients.

#Blood_pressure dataset
no <- seq(1:55)
status <- c(rep(0, 25), rep(1, 30))
mmhg <- c(120,115,94,118,111,102,102,131,104,107,115,139,115,113,114,105,
          115,134,109,109,93,118,109,106,125,150,142,119,127,141,149,144,
          142,149,161,143,140,148,149,141,146,159,152,135,134,161,130,125,
          141,148,153,145,137,147,169)
blood_pressure <-data.frame(no,status,mmhg)

# Find the mean
xbar = mean(blood_pressure$mmhg)


# Find s
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
new_blood_pressure <- blood_pressure%>% mutate(xi_minus_xbar = mmhg-130, xi_minus_xbar_kuadrat = (mmhg-130)^2)
new_blood_pressure
##    no status mmhg xi_minus_xbar xi_minus_xbar_kuadrat
## 1   1      0  120           -10                   100
## 2   2      0  115           -15                   225
## 3   3      0   94           -36                  1296
## 4   4      0  118           -12                   144
## 5   5      0  111           -19                   361
## 6   6      0  102           -28                   784
## 7   7      0  102           -28                   784
## 8   8      0  131             1                     1
## 9   9      0  104           -26                   676
## 10 10      0  107           -23                   529
## 11 11      0  115           -15                   225
## 12 12      0  139             9                    81
## 13 13      0  115           -15                   225
## 14 14      0  113           -17                   289
## 15 15      0  114           -16                   256
## 16 16      0  105           -25                   625
## 17 17      0  115           -15                   225
## 18 18      0  134             4                    16
## 19 19      0  109           -21                   441
## 20 20      0  109           -21                   441
## 21 21      0   93           -37                  1369
## 22 22      0  118           -12                   144
## 23 23      0  109           -21                   441
## 24 24      0  106           -24                   576
## 25 25      0  125            -5                    25
## 26 26      1  150            20                   400
## 27 27      1  142            12                   144
## 28 28      1  119           -11                   121
## 29 29      1  127            -3                     9
## 30 30      1  141            11                   121
## 31 31      1  149            19                   361
## 32 32      1  144            14                   196
## 33 33      1  142            12                   144
## 34 34      1  149            19                   361
## 35 35      1  161            31                   961
## 36 36      1  143            13                   169
## 37 37      1  140            10                   100
## 38 38      1  148            18                   324
## 39 39      1  149            19                   361
## 40 40      1  141            11                   121
## 41 41      1  146            16                   256
## 42 42      1  159            29                   841
## 43 43      1  152            22                   484
## 44 44      1  135             5                    25
## 45 45      1  134             4                    16
## 46 46      1  161            31                   961
## 47 47      1  130             0                     0
## 48 48      1  125            -5                    25
## 49 49      1  141            11                   121
## 50 50      1  148            18                   324
## 51 51      1  153            23                   529
## 52 52      1  145            15                   225
## 53 53      1  137             7                    49
## 54 54      1  147            17                   289
## 55 55      1  169            39                  1521
# Sample mean and Total sample size
xbar = mean(new_blood_pressure$mmhg)
n = length(new_blood_pressure$mmhg)

# Set null hypothesis and sample standard
mu0 = 140
q = sum(new_blood_pressure$xi_minus_xbar_kuadrat)
s = sqrt((1/(n-1))*q)

# t- statistics
t = (xbar-mu0)/(s/sqrt(n))
t
## [1] -3.869272
# p value A, B, and C
p_valueA = 2*pt(abs(t), df = n-1, lower.tail=FALSE) #twotail
p_valueB = pt(t, df= n-1, lower.tail = FALSE) #righttail
p_valueC = pt(t, df= n-1, lower.tail = TRUE) #left tail

p_valueA
## [1] 0.0002961114
p_valueB
## [1] 0.9998519
p_valueC
## [1] 0.0001480557

First, we made new collumn in blood_pressure data set and we name it new_blood_pressure data set. This new data has 2 new columns which are xi_minus_xbar and xi_minus_xbar_kuadrat. The purposes for this new columns is to find the s.

Secondly, we calculate the sample mean and find the total of sample size. The, we set the null hypothesis and s to find t. Then, we also search pvalueA, B, and C.

This is to test if the mean \(μ\) differ from a specific value \(μ_0\). The are three hypothesis are

A. \(H_0\) : \(μ=μ_0\) vs \(H_1\) : \(μ≠μ_0\). #Two tailed

B. \(H_0\) : \(μ≤μ_0\) vs \(H_1\) : \(μ>μ_0\). #Right tailed

C. \(H_0\) : \(μ≥μ_0\) vs \(H_1\) : \(μ<μ_0\). #Left tailed

Then we got the result:

\(z\) = -3.869272 p_valueA = 0.0002961114 p_valueB = 0.9998519 p_valueC = 0.0001480557

As it turns out A dan C to be less than 0.05 significance level, so we reject the null hypothesis. Since, p value B turns out to be greater than 0.05 significance level, so we accept the null hypothesis ort \(H_0\) : \(μ≤μ_0\) or \(μ≤140\) for this exercise.

8 Type II ~ One Tail

In a left/right tail test of the population mean, the null hypothesis claims that the true population mean \(\mu\) is greater than a given hypothetical value \(\mu_0\).

\[\text{Type II Error $H_a$} = \begin{cases} {\mu \ge \mu_0} & \text{Left Tail} \\ {\mu \le \mu_0} & \text{Right Tail} \end{cases}\]

A type II error occurs if the hypothesis test based on a random sample fails to reject the null hypothesis even when the true population mean \(\mu\) is in fact less than \(\mu_0\).

8.1 Example 5

Suppose the manufacturer claims that the mean speed of a motorcycle is more than 100 km/hours. In Assume the population standard deviation is 1.2 km/hours. At .05 significance level, what is the probability of having type II error for a sample size of 30 motorcycles?

8.1.1 Standard Error of the Mean

We begin with computing the standard deviation of the mean, sem.

n = 30                                                 # sample size 
sigma = 1.2                                            # population standard deviation 
sem = sigma/sqrt(n); sem                               # standard error 
## [1] 0.219089

Next compute the lower bound of sample means for which the null hypothesis \(\mu \ge 10000\) would not be rejected.

8.1.2 Sample Mean

alpha = .05                                            # significance level 
mu0 = 100                                              # hypothetical lower bound 
q = qnorm(alpha, mean=mu0, sd=sem); q 
## [1] 99.63963

Therefore, so long as the sample mean is greater than 99.64 in a hypothesis test, the null hypothesis will not be rejected. Since we assume that the actual population mean is 99.50, we can compute the probability of the sample mean above 99.64, and thus found the probability of type II error.

8.1.3 Probability of Error

mu = 99.50                                              # assumed actual mean 
pnorm(q, mean=mu, sd=sem, lower.tail=FALSE) 
## [1] 0.261957

If the motorcycle sample size is 30, the actual mean motorcycles speed is 9,950 hours and the population standard deviation is 120 hours, then the probability of type II error for testing the null hypothesis \(\mu \ge 10000\) at .05 significance level is 26.2%, and the power of the hypothesis test is 73.8%.

8.2 Exercise 5

Right tail: Garuda-food Indonesia claims that for each a cookie bag states of their product, there is at most 2 grams of saturated fat in a single cookie. Assume the actual mean amount of saturated fat per cookie is 2.075 grams and the sample standard deviation is 0.25 grams. At .05 significance level, what is the probability of having a type II error for a sample size of 35 cookies?

\(Solution\):

We need to define the sample size, the population standard deviation and the standard error.

# Sample size 
n = 35

# Population standard deviation
sigma = 0.25

# Standard error
sem = sigma/sqrt(n)
sem
## [1] 0.04225771

Next, we compute the upper bound of sample means which is the null hypothesis \(μ ≤ 2\) would not be rejected.

#Sig nificance level
alpha = 0.05

# Hypothesis 
mu0 = 2
q = qnorm(alpha , mean = mu0, sd= sem, lower.tail = F)
q
## [1] 2.069508

If, the sample mean is less than 2.069508 in a hypothesis test, the null hypothesis will not be rejected. Since we assume that the actual population mean is 2.075, we can compute the probability of the sample mean below 2.0695, and found the probability of type II error.

# Actual mean
mu = 2.075
pnorm(q, mean=mu, sd=sem)
## [1] 0.448295

If the cookies sample size is 35, the actual mean amount of saturated fat per cookie is 2.075 grams and the population standard deviation is 0.25 grams, then the probability of type II error for testing the null hypothesis \(μ ≤ 2\) at 0.05 significance level is 44.8%, and the power of the hypothesis test is 55.2%.

9 Type II ~ Two Tail

In a two-tailed test of the population mean, the null hypothesis claims that the true population mean \(\mu\) is equal to a given hypothetical value \(\mu_0\).

\[\mu_0 = \mu\]

A type II error occurs if the hypothesis test based on a random sample fails to reject the null hypothesis even when the true population mean \(\mu\) is in fact different from \(\mu_0\).

Assume that the population has a known standard deviation \(\sigma\). By the Central Limit Theorem, the population of all possible means of samples of sufficiently large size n approximately follows the normal distribution. Hence we can compute the range of sample means for which the null hypothesis will not be rejected, and then obtain an estimate of the probability of type II error.

9.1 Example 6

Some journals concluded that the average weight of Hachiko Dogs around the world last ten years was 15.4 kg. Researchers want to make sure if there a change in the average weight of these varieties after ten years. Assume the actual mean population weight is 15.1 kg, and the population standard deviation is 2.5 kg. At .05 significance level, what is the probability of having type II error for a sample size of 35 Hachiko Dogs?

9.1.1 Standard Error of the Mean

We begin with computing the standard deviation of the mean, sem.

n = 35                                                # sample size 
sigma = 2.5                                           # population standard deviation 
sem = sigma/sqrt(n); sem                              # standard error 
## [1] 0.4225771

Next compute the left and right bounds of sample means for which the null hypothesis \(\mu = 15.4\) would not be rejected.

9.1.2 Sample Mean

alpha = .05                                           # significance level 
mu0 = 15.4                                            # hypothetical mean 
I = c(alpha/2, 1-alpha/2) 
q = qnorm(I, mean=mu0, sd=sem); q 
## [1] 14.57176 16.22824

Therefore, so long as the sample mean is between 14.572 and 16.228 in a hypothesis test, the null hypothesis will not be rejected. Since we assume that the actual population mean is 15.1, we can compute the lower tail probabilities of both end points.

9.1.3 Probability of Error

mu = 15.1                                             # assumed actual mean 
p = pnorm(q, mean=mu, sd=sem); p 
## [1] 0.1056435 0.9962062
diff(p)                                               # p[2]-p[1] 
## [1] 0.8905627

Finally, the probability of type II error is the probability between the two end points. If the sample size of Hachiko Dogs is 35, the actual mean population weight is 15.1 kg and the population standard deviation is 2.5 kg, then the probability of type II error for testing the null hypothesis \(\mu = 15.4\) at .05 significance level is 89.1%, and the power of the hypothesis test is 10.9%.

9.2 Exercise 6

Under same assumptions as case 27, if actual mean population weight is 14.9 kg, what is the probability of type II errors? What is the power of the hypothesis test?

Solution:

we bgin by defining the sample size, population standard deviation, and standard error.

# sample size
n = 35

# population standard deviation
sigma = 2.5

# standard error
sem = sigma/sqrt(n)
sem
## [1] 0.4225771

Nest, we compute the lower and upper bounds of sample means which the null hypothesis \(μ = 15.4\) would not be rejected.

#significance level
alpha= 0.05

# hypothetical mean
mu0 = 15.4

I = c(alpha/2, 1- alpha/2)
q= qnorm(I, mean = mu0, sd= sem)
q
## [1] 14.57176 16.22824

So, we know that as long as the sample mean is between 14.57176 and 16.22824, the null hypothesis will not be rejected. Since, we assume that the actual population mean is 14.9, we cimpute the lower tail probabilities of both end points

# Actual mean
mu = 14.9
p = pnorm(q, mean=mu, sd= sem)
p
## [1] 0.2186537 0.9991644

Finally, the probability of type II error is the probability between the two end points

# p[2]- p[1]
diff(p)
## [1] 0.7805107

If, the penguin sample size is 35, the actual mean population weight is 14.9 kg and the population standard deviation is 2.5 kg, then the probability of type II error for testing the null hypothesis \(μ = 15.4\) at 0.05 significance level is 78.1%, and the power of the hypothesis test is 21.9%.

10 Mind Map (Hypothesis)

Mind Map Hypothesis Testing

Mind Map Hypothesis Testing