Problem 1
Problem 1.a
State the null hypothesis:
# The average number of gallons of oil in the drums that the company distributes is <= 200 gallons.
Problem 1.b
State the alternative hypothesis:
# The average number of gallons of oil in the drums that the company distributes is > 200 gallons.
Problem 1.c
# μ0 is the population mean of the null hypothesis. In this case:
population_mean <- 200
Problem 1.d
# The population parameter is related to the population mean, which is average number of gallons of oil in the drums in this case.
Problem 1.e
# The sample mean (202.52 gallons) can be used to estimate the population mean.
sample_mean <- 202.52
Problem 1.f
# Yes. We can use the parametric tests.
# Both t-test and z-test can be used. The sample size of 35 drums is large enough. According to Central Limit Theorem, the sample mean is approximately normal. We also have the mean and standard deviation of the sample. Although we don't have the population standard deviation for z-test, the sample size is over 30 and we can just use the sample standard deviation (which is 1.23). The only thing we need to make sure is that these 35 drums is selected randomly, and each of them is sampled independently.
Problem 1.g
n <- 35
sample_mean <- 202.52
sample_sd <- 1.23
population_mean <- 200
confidence_level <- 0.9
alpha <- 1-confidence_level
df <- n-1
standard_error <- sample_sd/sqrt(n)
critical_t_value <- qt(1 - alpha, df)
critical_z_value <- qnorm (1-alpha)
lower_bound_t <- population_mean + critical_t_value * standard_error
upper_bound_t <- Inf
cat("Range of sample means for t-test - 90% confidence: (", lower_bound_t, ", Inf)\n")
## Range of sample means for t-test - 90% confidence: ( 200.2717 , Inf)
lower_bound_z <- population_mean + critical_z_value * standard_error
upper_bound_z <- Inf
cat("Range of sample means for z-test - 90% Confidence: (", lower_bound_z, ", Inf)\n")
## Range of sample means for z-test - 90% Confidence: ( 200.2664 , Inf)
Problem 1.h
n <- 35
sample_mean <- 202.52
sample_sd <- 1.23
population_mean <- 200
confidence_level <- 0.95
alpha <- 1-confidence_level
df <- n-1
standard_error <- sample_sd/sqrt(n)
critical_t_value <- qt(1 - alpha, df)
critical_z_value <- qnorm (1-alpha)
lower_bound_t <- population_mean + critical_t_value * standard_error
upper_bound_t <- Inf
cat("Range of sample means for t-test - 95% Confidence: (", lower_bound_t, ", Inf)\n")
## Range of sample means for t-test - 95% Confidence: ( 200.3516 , Inf)
lower_bound_z <- population_mean + critical_z_value * standard_error
upper_bound_z <- Inf
cat("Range of sample means for z-test - 95% confidence: (", lower_bound_z, ", Inf)\n")
## Range of sample means for z-test - 95% confidence: ( 200.342 , Inf)
Problem 1.i
n <- 35
sample_mean <- 202.52
sample_sd <- 1.23
population_mean <- 200
confidence_level <- 0.99
alpha <- 1-confidence_level
df <- n-1
standard_error <- sample_sd/sqrt(n)
critical_t_value <- qt(1 - alpha, df)
critical_z_value <- qnorm (1-alpha)
lower_bound_t <- population_mean + critical_t_value * standard_error
upper_bound_t <- Inf
cat("Range of sample means for t-test - 99% Confidence: (", lower_bound_t, ", Inf)\n")
## Range of sample means for t-test - 99% Confidence: ( 200.5075 , Inf)
lower_bound_z <- population_mean + critical_z_value * standard_error
upper_bound_z <- Inf
cat("Range of sample means for z-test - 99% confidence: (", lower_bound_z, ", Inf)\n")
## Range of sample means for z-test - 99% confidence: ( 200.4837 , Inf)
Problem 1.j
x_bar <- 202.52
mu_0 <- 200
s <- 1.23
n <- 35
df <- n - 1
T_90 <- qt(0.90, df)
T_90
## [1] 1.306952
z_90 <- qnorm(0.90)
z_90
## [1] 1.281552
Problem 1.k
x_bar <- 202.52
mu_0 <- 200
s <- 1.23
n <- 35
df <- n - 1
T_95 <- qt(0.95, df)
z_95 <- qnorm(0.95)
T_95
## [1] 1.690924
z_95
## [1] 1.644854
Problem 1.l
x_bar <- 202.52
mu_0 <- 200
s <- 1.23
n <- 35
df <- n - 1
T_99 <- qt(0.99, df)
z_99 <- qnorm(0.99)
T_99
## [1] 2.44115
z_99
## [1] 2.326348
Problem 1.m
n <- 35
df <- n - 1
sample_mean <- 202.52
sample_sd <- 1.23
population_mean <- 200
confidence_level <- 0.99
t_value <- (sample_mean-population_mean)/(sample_sd/sqrt(n))
z_value <- (sample_mean - population_mean)/sample_sd
p_value_t <- 1- pt(t_value, df=34)
p_value_z <- 1-pnorm(z_value)
t_value
## [1] 12.12075
p_value_t
## [1] 3.408385e-14
p_value_z
## [1] 0.02024179
Problem 1.n
confidence_level_t <- 1 - p_value_t
confidence_level_z <- 1 - p_value_z
confidence_level_t
## [1] 1
confidence_level_z
## [1] 0.9797582
Problem 1.o
#P-value :Calculate the test statistic (z-score or t-score), in this case, n > 30, regarding as large sample and using sample standard deviation to replace population standard deviation. Calculate the p-value associated with the test statistic.Compare the p-value to α (α = 1 - confidence level)in this case, p-value on t test (close to 0) is lower than α(0.05 or 0.01),so we reject the null hypothesis.
#Rejection region approach:rejection region: The steps are exactly the same. However, we calculate a critical value, instead of calculating a p-value. In this case, the test statistic(>12) is greater than the critical value(2.44), so we reject the null hypothesis.
#These two methods should lead to the same conclusion at a given confidence level.
Problem 2
One concern in Major League Baseball is that the time to play games
has become too long. There have been recent efforts to enforce rules
that will speed up the game. In the data set BaseballGames we have data
reporting a set of games that were played on a given day, where the
columns, in order, report the game index, the teams playing the game,
the league (either AL or NL), the margin of victory, the number of
pitchers, the attendance at the game, and the amount of time that the
game took, in minutes. We will conduct a statistical test to determine
if the average length of games is less than 213 minutes at the 99%
confidence level.
Problem 2.a
State the null hypothesis:
# Null hypothesis is the length of games is greater than or equal to 213
#H0: mu >= 213 (mean of the time of the baseball game)
Problem 2.b
State the alternative hypothesis:
# Alternative hypothesis is the length of games is less than 213
#H1: mu < 213 (mean of the time of the baseball game)
Problem 2.c
Can we use a parametric test? Explain in detail.
We cannot use parametric test in this case. One of the basic
assumptions of parametric test is normal distribution of the data.
Results of Shapiro Wilk test will allow us to decide normality, and as
seen below ‘’react’‘time’ in the Golf game is not normally
distributed.
test_time <- shapiro.test(readData$Time)
print(test_time)
##
## Shapiro-Wilk normality test
##
## data: readData$Time
## W = 0.80801, p-value = 0.004667
pval_time <- test_time$p.value
cat("2c. The p.value from Shapiro Wilk test is", pval_time, "which is much smaller than 0.05 and so the time is not normally distributed and hence cannot be evaluated by a parametric test\n")
## 2c. The p.value from Shapiro Wilk test is 0.004666685 which is much smaller than 0.05 and so the time is not normally distributed and hence cannot be evaluated by a parametric test
Problem 2.d
What is the p-value for the statistical test? Show all steps.
Ttest1 <- t.test(readData$Time, mu = 213, alternative = "less", conf.level = 0.99)
print(Ttest1)
##
## One Sample t-test
##
## data: readData$Time
## t = -2.5426, df = 14, p-value = 0.01173
## alternative hypothesis: true mean is less than 213
## 99 percent confidence interval:
## -Inf 213.9775
## sample estimates:
## mean of x
## 182.6667
Pvalue <- Ttest1$p.value
print(Pvalue)
## [1] 0.01172658
Problem 2.e
At a confidence level of 99%, would you reject or fail to reject the
null hypothesis?
time <- readData$Time
n <- length(time)
x_bar <- mean(time)
s <- sd(time)
mu <- 213
df <- n-1
t_statistic <- (x_bar - mu) / (s / sqrt(n))
p_value <- pt(t_statistic, df)
alpha <- 0.01
p_value_threshold <- alpha / 2
if (p_value < p_value_threshold) {
cat("Reject the null hypothesis at a 99% confidence level.\n")
} else {
cat("Fail to reject the null hypothesis at a 99% confidence level.\n")
}
## Fail to reject the null hypothesis at a 99% confidence level.
Problem 2.g
The League is mostly concerned that at least 80% of games will
conclude within 4 hours. They would like to make the statement ‘80% of
our games conclude within 4 hours.’ Based on this data set, conduct a
hypothesis test to evaluate if this claim is true at 90% and 98%
confidence.
prop_conclude_within_4_hours <- sum(time <= 240) / n
alpha_90 <- 0.10
alpha_98 <- 0.02
# 90% confidence interval
z_score_90 <- qnorm(1 - alpha_90 / 2)
margin_of_error_90 <- z_score_90 * sqrt((prop_conclude_within_4_hours * (1 - prop_conclude_within_4_hours)) / n)
ci_90 <- c(prop_conclude_within_4_hours - margin_of_error_90, prop_conclude_within_4_hours + margin_of_error_90)
# 98% confidence interval
z_score_98 <- qnorm(1 - alpha_98 / 2)
margin_of_error_98 <- z_score_98 * sqrt((prop_conclude_within_4_hours * (1 - prop_conclude_within_4_hours)) / n)
ci_98 <- c(prop_conclude_within_4_hours - margin_of_error_98, prop_conclude_within_4_hours + margin_of_error_98)
cat("Proportion of games concluding within 4 hours:", prop_conclude_within_4_hours, "\n")
## Proportion of games concluding within 4 hours: 0.9333333
cat("90% confidence interval:", ci_90, "\n")
## 90% confidence interval: 0.8273947 1.039272
cat("98% confidence interval:", ci_98, "\n")
## 98% confidence interval: 0.7835023 1.083164
#2.g Conclusion:
#Hypothesis Test at 90% Confidence Level: Since the lower limit of the 90% confidence interval (0.8273947) is greater than 80%, we fail to reject the null hypothesis at a 90% confidence level. This suggests that there is sufficient evidence to support the claim that at least 80% of games conclude within 4 hours.
#Hypothesis Test at 98% Confidence Level: Since the lower limit of the 98% confidence interval (0.7835023) is less than 80%, we reject the null hypothesis at a 98% confidence level. This suggests that there is not enough evidence to support the claim that at least 80% of games conclude within 4 hours at a 98% confidence level.
Problem 3
Consider the react data set in the ISwR package, which is a single
vector, not a data frame. It contains differences between two nurses’
determinations of 334 tuberculin reaction sizes. We are interested in
conducting a hypothesis test for evaluating if the differences are
negligible (i.e., there is no difference in the accuracy of their
measurements).
3a. State the null hypothesis.
# Null hypothesis: mean of reaction data is zero. There is no difference in the measurements taken by the two nurses. Hence:
#H0: mu = 0 (mean of the Population)
3b. State the alternative hypothesis.
# Alternate hypothesis: mean of reaction data is not zero.There is typically difference in the measurements taken by the two nurses. Hence:
#H1: mu != 0 (mean of the Population)
3c. Can we use a parametric test? Explain in detail
We cannot use parametric test in this case. One of the basic
assumptions of parametric test is normal distribution of the data.
Results of Shapiro Wilk test will allow us to decide normality, and as
seen below ‘react’ is not normally distributed.
library(ISwR)
stest <- shapiro.test(react)
print(stest)
##
## Shapiro-Wilk normality test
##
## data: react
## W = 0.95701, p-value = 2.512e-08
pval <- stest$p.value
pval <- format(pval, scientific= F)
cat("3c. The p.value from Shapiro Wilk test is", pval, "which is almost zero and much smaller than 0.05 and so 'react' is not normally distributed and hence cannot be evaluated by a parametric test\n")
## 3c. The p.value from Shapiro Wilk test is 0.00000002511781 which is almost zero and much smaller than 0.05 and so 'react' is not normally distributed and hence cannot be evaluated by a parametric test
3d. What is the p-value for the statistical test? Show all
steps.
meanmu <- 0
ttest <- t.test(x=react,mu=meanmu)
p_val <- ttest$p.value
p_val <- format(p_val, scientific= F)
cat("d. The p.value of the statistical test is", p_val, "which is almost zero\n")
## d. The p.value of the statistical test is 0.0000000000001114557 which is almost zero
3e. At a confidence level of 95%, would you reject or fail to reject
the null hypothesis?
#null hypothesis - H0: meanmu = 0
#alternative hypothesis H1: meanmu != 0
print(ttest)
##
## One Sample t-test
##
## data: react
## t = -7.7512, df = 333, p-value = 1.115e-13
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.9985214 -0.5942930
## sample estimates:
## mean of x
## -0.7964072
cat ("e. Based on t.test the 95% confidence for the mean is to be between -0.999 and -0.594. This is faraway from the null hypothesis of the mean being zero and the p-value is almost zero. Hence we are rejecting the null hypothesis ")
## e. Based on t.test the 95% confidence for the mean is to be between -0.999 and -0.594. This is faraway from the null hypothesis of the mean being zero and the p-value is almost zero. Hence we are rejecting the null hypothesis