Q1:

mean_sample <- 110
mean_population <- 109
std_dev <- 6
sample_size <- 190
alpha <- 0.05

z <- (mean_sample - mean_population) / (std_dev / sqrt(sample_size))

p_value <- 2 * (1 - pnorm(abs(z)))

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" Z-score:", z, "\n","P-value:", p_value, "\n","Decision:", decision, "\n")
##  Z-score: 2.297341 
##  P-value: 0.0215993 
##  Decision: reject

Q2:

mean_sample <- 5.0
mean_population <- 5.3
std_dev <- 1.1
sample_size <- 5
alpha <- 0.05

z <- (mean_sample - mean_population) / (std_dev / sqrt(sample_size))

p_value <- pnorm(z)

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" Z-score:", z, "\n","P-value:", p_value, "\n","Decision:", decision, "\n")
##  Z-score: -0.6098367 
##  P-value: 0.270985 
##  Decision: fail to reject

Extension:

mean_sample <- 5.0 
mean_population <- 5.3 
std_dev <- 1.1 
sample_size <- 5
alpha <- 0.05

z <- (mean_sample - mean_population) / (std_dev / sqrt(sample_size))

p_value <- 2 * (1 - pnorm(abs(z)))

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" Z-score:", z, "\n","P-value:", p_value, "\n","Decision:", decision, "\n")
##  Z-score: -0.6098367 
##  P-value: 0.54197 
##  Decision: fail to reject

Q3:

mean_sample <- 7.1
mean_population <- 7.3 
variance <- 0.49 
sample_size <- 51
alpha <- 0.01

std_dev <- sqrt(variance)

t_score <- (mean_sample - mean_population) / (std_dev / sqrt(sample_size))

df <- sample_size - 1

p_value <- 2 * (1 - pt(abs(t_score), df))

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" t-score:", t_score, "\n","P-value:", p_value, "\n", "Decision:", decision)
##  t-score: -2.040408 
##  P-value: 0.04660827 
##  Decision: fail to reject

Q4:

p_reported <- 0.36
p_sample <- 0.29 
sample_size <- 100 
alpha <- 0.02 

std_error <- sqrt(p_reported * (1 - p_reported) / sample_size)
z_score <- (p_sample - p_reported) / std_error

p_value <- pnorm(z_score)

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" z-score:", z_score, "\n","P-value:", p_value, "\n", "Decision:", decision)
##  z-score: -1.458333 
##  P-value: 0.07237434 
##  Decision: fail to reject

Q5:

p_expected <- 0.31
sample_size <- 380 
number_uninsured <- 95
p_sample <- number_uninsured / sample_size 
alpha <- 0.05

std_error <- sqrt(p_expected * (1 - p_expected) / sample_size)
z_score <- (p_sample - p_expected) / std_error
                 
p_value <- pnorm(z_score)

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" Z-score:", z, "\n","P-value:", p_value, "\n","Decision:", decision)
##  Z-score: -0.6098367 
##  P-value: 0.005720462 
##  Decision: reject

Q6:

historical_sd <- 24 
sample_sd <- 15.4387 
sample_size <- 22  
alpha <- 0.1 

chi_squared <- (sample_size - 1) * sample_sd^2 / historical_sd^2

df <- sample_size - 1

p_value <- pchisq(chi_squared, df, lower.tail = TRUE)

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" Chi-squared statistic:", chi_squared, "\n", "P-value:", p_value, "\n","Decision:", decision)
##  Chi-squared statistic: 8.68997 
##  P-value: 0.008549436 
##  Decision: reject

Q7:

mean_smokers <- 87
mean_nonsmokers <- 84
std_dev_smokers <- 9
std_dev_nonsmokers <- 10
sample_size_smokers <- 32
sample_size_nonsmokers <- 31
alpha <- 0.1

std_error <- sqrt((std_dev_smokers^2 / sample_size_smokers) + (std_dev_nonsmokers^2 / sample_size_nonsmokers))
t_score <- (mean_smokers - mean_nonsmokers) / std_error

df <- ((std_dev_smokers^2 / sample_size_smokers + std_dev_nonsmokers^2 / sample_size_nonsmokers)^2) / (((std_dev_smokers^2 / sample_size_smokers)^2 / (sample_size_smokers - 1)) + ((std_dev_nonsmokers^2 / sample_size_nonsmokers)^2 / (sample_size_nonsmokers - 1)))

p_value <- 2 * (1 - pt(abs(t_score), df))

decision <- ifelse(p_value < alpha, "reject", "fail to reject")

cat(" t-score:", t_score, "\n", "Degrees of freedom:", df, "\n", "P-value:", p_value, "\n","Decision:", decision)
##  t-score: 1.25032 
##  Degrees of freedom: 59.87528 
##  P-value: 0.2160473 
##  Decision: fail to reject

Q8:

n1 <- 11
x_bar1 <- 127
s1 <- 33
n2 <- 18
x_bar2 <- 157
s2 <- 27

confidence_level <- 0.95

# Calculating the standard error of the difference in means
std_error_diff <- sqrt((s1^2 / n1) + (s2^2 / n2))

# Calculating degrees of freedom using the Welch-Satterthwaite equation
df <- ((s1^2 / n1 + s2^2 / n2)^2) / (((s1^2 / n1)^2 / (n1 - 1)) + ((s2^2 / n2)^2 / (n2 - 1)))

t_critical <- qt((1 + confidence_level) / 2, df)

margin_of_error <- t_critical * std_error_diff

lower_bound <- (x_bar1 - x_bar2) - margin_of_error
upper_bound <- (x_bar1 - x_bar2) + margin_of_error

cat("95% Confidence Interval: [", lower_bound, ", ", upper_bound, "]")
## 95% Confidence Interval: [ -54.80655 ,  -5.193452 ]

Q9:

# Travel times for Route I and Route II
route_I <- c(32, 27, 34, 24, 31, 25, 30, 23, 27, 35)
route_II <- c(28, 28, 33, 25, 26, 29, 33, 27, 25, 33)

differences <- route_I - route_II

mean_diff <- mean(differences)
std_dev_diff <- sd(differences)
n <- length(differences)

confidence_level <- 0.98
df <- n - 1

t_critical <- qt((1 + confidence_level) / 2, df)
margin_of_error <- t_critical * (std_dev_diff / sqrt(n))

# Calculating the confidence interval
lower_bound <- mean_diff - margin_of_error
upper_bound <- mean_diff + margin_of_error

cat("98% Confidence Interval: [", lower_bound, ", ", upper_bound, "]", "\n")
## 98% Confidence Interval: [ -2.766534 ,  2.966534 ]

10.

n1 <- 391
x1 <- 195
n2 <- 510
x2 <- 193
alpha <- 0.05

# Sample proportions
p1 <- x1 / n1
p2 <- x2 / n2

pooled_p <- (x1 + x2) / (n1 + n2)

std_error_diff <- sqrt(pooled_p * (1 - pooled_p) * (1/n1 + 1/n2))

z_score <- (p1 - p2) / std_error_diff

p_value <- 1 - pnorm(z_score)

decision <- ifelse(p_value < alpha, "reject", "fail to reject")


cat("z-score:", z_score, "\n", "P-value:", p_value, "\n", "Decision:", decision)
## z-score: 3.614018 
##  P-value: 0.000150744 
##  Decision: reject