Hypothesis Testing

Komputasi Statistika


Email             :
RPubs            : https://rpubs.com/dhelaasafiani/
Jurusan          : Statistika
Address         : ARA Center, Matana University Tower
                         Jl. CBD Barat Kav, RT.1, Curug Sangereng, Kelapa Dua, Tangerang, Banten 15810.


Exercise 1

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?

Answer 1

Hypothesis
H0 : Jumlah rata-rata lemak jenuh per cookie sama dengan atau kurang dari 2 gram (µ ≤ 2).
H1 : Jumlah rata-rata lemak jenuh per cookie lebih dari 2 grams (µ > 2).

mu0 = 2
n = 40
xbar = 2.1
sigma = 0.25
z = (xbar-mu0)/(sigma/sqrt(n))
z
## [1] 2.529822
alpha = 0.05
z.alpha = qnorm(1-alpha)
z.alpha
## [1] 1.644854

Z-score didapat adalah 2.529822 dan daerah kritisnya adalah 1.644854 yang dimana z-score > daerah kritis artinya kita dapat menolak H0.

pnorm(z, lower.tail = FALSE)      # Z test in right tailed
## [1] 0.005706018

Atau untuk menarik kesimpulan dapat membandingkan antara p-value dengan signifikan level. p-value = 0.005706 < alpha = 0.05 yang artinya H0 ditolak.
Kita dapat menolak claim bahwa “Jumlah rata-rata lemak jenuh per cookie tidak lebih dari 2 gram”. Setelah, melakukan uji hipotesis didapatkan kesimpulannya adalah “Jumlah rata-rata lemak jenuh per cookie lebih dari 2 gram”.


Atau untuk melakukan uji z right tail dapat menggunakan koding dibawah ini.

library(BSDA)
zsum.test(mean.x=xbar, sigma.x = sigma, n.x = n,  
          alternative = "greater", mu = mu0,
          conf.level = 0.95)
## 
##  One-sample z-Test
## 
## data:  Summarized x
## z = 2.5298, p-value = 0.005706
## alternative hypothesis: true mean is greater than 2
## 95 percent confidence interval:
##  2.034981       NA
## sample estimates:
## mean of x 
##       2.1

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

Answer 2

Hypothesis
H0 : Tekanan darah sistolik rata-rata pada populasi sama dengan 140 mmHg (μ = 140).
H1 : Tekanan darah sistolik rata-rata pada populasi tidak sama dengan 140 mmHg (µ ≠ 140).

mu0 = 140
n = 55
xbar = mean(blood_pressure$mmhg)
sigma = 20
z = (xbar-mu0)/(sigma/sqrt(n))
z
## [1] -3.708099
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)
## [1] -1.959964  1.959964

Z-score didapat adalah -3.7081 dan daerah kritisnya adalah -1.959964 < z < 1.959964 yang dimana z-score tidak berada didalam daerah interval tersebut artinya kita dapat menolak H0.

2*pnorm(-abs(z))          # Z test in two tails
## [1] 0.0002088208

Atau untuk menarik kesimpulan dapat membandingkan antara p-value dengan signifikan level. p-value = 0.0002088 < alpha = 0.0025(0.05/2) yang artinya H0 ditolak.
Kita dapat menolak H0 yang berarti bahwa “Tekanan darah sistolik rata-rata pada populasi tidak sama dengan 140 mmHg”.


Atau untuk melakukan uji z two tails dapat menggunakan koding dibawah ini.

zsum.test(mean.x=xbar, sigma.x = 20, n.x = 55,  
          alternative = "two.sided", mu = mu0,
          conf.level = 0.95)
## 
##  One-sample z-Test
## 
## data:  Summarized x
## z = -3.7081, p-value = 0.0002088
## alternative hypothesis: true mean is not equal to 140
## 95 percent confidence interval:
##  124.7144 135.2856
## sample estimates:
## mean of x 
##       130

Exercise 3

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?

Answer 3

Hypothesis
H0 : Jumlah rata-rata lemak jenuh per cookie sama dengan atau lebih dari 2 gram (µ ≥ 2).
H1 : Jumlah rata-rata lemak jenuh per cookie kurang dari 2 gram (µ < 2).

mu0 = 2
n = 40
xbar = 2.1
s = 0.3
t = (xbar-mu0)/(s/sqrt(n))
t
## [1] 2.108185
alpha = 0.05
t.alpha = qt(1-alpha, df = n-1)
t.alpha
## [1] 1.684875

t-score didapat adalah 2.1082 dan daerah kritisnya adalah 1.684875 yang dimana t-score > daerah kritis artinya kita dapat tolak H0.

pt(t, df=n-1, lower.tail = FALSE)         # t test in right tailed
## [1] 0.020746

Atau untuk menarik kesimpulan dapat membandingkan antara p-value dengan signifikan level. p-value = 0.02075 > alpha = 0.05 yang artinya H0 ditolak.
Kita dapat menolak H0 yang berarti bahwa “Jumlah rata-rata lemak jenuh per cookie kurang dari 2 gram”.

Exercise 4

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

Answer 4

Hypothesis
H0 : Tekanan darah sistolik rata-rata pada populasi sama dengan 140 mmHg (μ = 140).
H1 : Tekanan darah sistolik rata-rata pada populasi tidak sama dengan 140 mmHg (µ ≠ 140).

mu0 = 140
n = 55
xbar = mean(blood_pressure$mmhg)
s = 20
t = (xbar-mu0)/(s/sqrt(n))
t
## [1] -3.708099
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)
## [1] -1.959964  1.959964

Exercise 5

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?

Answer 5

mu0 = 2
mu = 2.075
sigma = 0.25
alpha = 0.05
n = 35

sem = sigma/sqrt(n)
sem
## [1] 0.04225771
q = qnorm(alpha, mean=mu0, sd=sem)
q 
## [1] 1.930492
pnorm(q, mean=mu, sd=sem, lower.tail=FALSE) 
## [1] 0.9996865

Exercise 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?
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?

Answer 6

mu0 = 15.4
mu = 14.9
sigma = 2.5
alpha = 0.05
n = 35

sem = sigma/sqrt(n)
sem
## [1] 0.4225771
I = c(alpha/2, 1-alpha/2) 
q = qnorm(I, mean=mu0, sd=sem)
q 
## [1] 14.57176 16.22824
p = pnorm(q, mean=mu, sd=sem)
p
## [1] 0.2186537 0.9991644
diff(p) 
## [1] 0.7805107