The Week 6 readings cover Chapter 9 (Statistical Inference: Hypothesis Testing for Single Populations) and Chapter 16 (Analysis of Categorical Data) from Ken Black’s Business Statistics, alongside R implementations from Kabacoff (Chapter 7), Davies (Chapter 18), and Stowell (Chapter 10). By the end of this module, you should be able to:
Hypothesis testing is a formal inferential procedure used to evaluate competing claims about a population parameter based on sample evidence.
| Decision | \(H_0\) is Actually True | \(H_0\) is Actually False |
|---|---|---|
| Fail to Reject \(H_0\) | Correct Decision (\(1 - \alpha\)) | Type II Error (\(\beta\)) (False Negative) |
| Reject \(H_0\) | Type I Error (\(\alpha\)) (False Positive) | Correct Decision / Power (\(1 - \beta\)) |
Applicable when \(\sigma\) is known and the population is normally distributed (or \(n \ge 30\) by the Central Limit Theorem): \[z = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}\]
Base R does not include a built-in one-sample \(z\)-test function, though
BSDA::z.test() provides one in an external package. Below
we write a reusable base-R one_sample_z_test() function and
apply it to Demonstration Problem 9.1 (Dental Tooth Fillings,
p. 281): historical mean out-of-pocket expenditure is \(\$141\) (\(\sigma
= \$30\)), and a researcher tests whether current costs are
higher (\(H_a: \mu > 141\)) using
\(n = 32\) fillings at \(\alpha = 0.05\).
one_sample_z_test <- function(x, mu0, sigma, alternative = "two.sided", alpha = 0.05) {
n <- length(x)
x_bar <- mean(x, na.rm = TRUE)
se <- sigma / sqrt(n)
z_obs <- (x_bar - mu0) / se
if (alternative == "greater") {
crit_z <- qnorm(1 - alpha)
p_val <- pnorm(z_obs, lower.tail = FALSE)
decision <- ifelse(z_obs > crit_z, "Reject H0", "Fail to Reject H0")
} else if (alternative == "less") {
crit_z <- qnorm(alpha)
p_val <- pnorm(z_obs, lower.tail = TRUE)
decision <- ifelse(z_obs < crit_z, "Reject H0", "Fail to Reject H0")
} else {
crit_z <- qnorm(1 - alpha / 2)
p_val <- 2 * pnorm(abs(z_obs), lower.tail = FALSE)
decision <- ifelse(abs(z_obs) > crit_z, "Reject H0", "Fail to Reject H0")
}
data.frame(
n = n,
Sample_Mean = round(x_bar, 4),
z_Statistic = round(z_obs, 4),
Critical_z = round(crit_z, 4),
p_Value = round(p_val, 5),
Decision = decision
)
}
fillings <- c(147, 145, 175, 163, 152, 166, 125, 175, 119, 149, 147, 120, 142,
130, 210, 135, 163, 158, 132, 207, 151, 175, 156, 141, 123, 142,
139, 170, 118, 160, 122, 110)
print(one_sample_z_test(fillings, mu0 = 141, sigma = 30, alternative = "greater", alpha = 0.05))## n Sample_Mean z_Statistic Critical_z p_Value Decision
## 1 32 148.9688 1.5026 1.6449 0.06647 Fail to Reject H0
Because \(z = 1.5026 < z_{\text{crit}} = 1.6449\) (\(p = 0.06647 > 0.05\)), we fail to reject \(H_0\); there is insufficient evidence at \(\alpha = 0.05\) that dental filling expenditures exceed \(\$141\).
When \(\sigma\) is unknown (the typical practical case) and is estimated by the sample standard deviation \(s\), we use Student’s \(t\)-distribution (assuming a normal population or \(n \ge 30\)): \[t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}, \quad \text{with } df = n - 1\]
In Demonstration Problem 9.2 (U.S. Farm Sizes,
p. 287), a researcher tests whether average farm size now
exceeds the 1997 average of \(471\)
acres (\(H_a: \mu > 471\)) using
\(n = 23\) randomly sampled farms at
\(\alpha = 0.05\). Additionally, when
testing multiple subgroups simultaneously (such as comparing three hot
dog types in hot_dogs.csv against a benchmark), analysts
should be aware that running multiple classical tests increases
family-wise Type I error and warrants adjusting critical values when
multiple significant results emerge.
acres <- c(445, 557, 545, 489, 502, 511, 474, 449, 590, 505, 438, 561, 553, 500,
560, 477, 466, 454, 477, 463, 557, 466, 433)
t_acres <- t.test(x = acres, mu = 471, alternative = "greater", conf.level = 0.95)
print(t_acres)##
## One Sample t-test
##
## data: acres
## t = 2.8384, df = 22, p-value = 0.004782
## alternative hypothesis: true mean is greater than 471
## 95 percent confidence interval:
## 481.9747 Inf
## sample estimates:
## mean of x
## 498.7826
t_manual <- (mean(acres) - 471) / (sd(acres) / sqrt(length(acres)))
p_manual <- pt(q = t_manual, df = length(acres) - 1, lower.tail = FALSE)
print(sprintf("Manual t = %.4f, p-value = %.6f", t_manual, p_manual))## [1] "Manual t = 2.8384, p-value = 0.004782"
hotdogs <- read.csv("https://raw.githubusercontent.com/telpirion/CumulativeStudyGuide/refs/heads/main/data/hot_dogs.csv")
boxplot(Calories ~ Type, data = hotdogs, horizontal = TRUE,
col = c("salmon", "tan", "lightblue"),
main = "Hot Dog Calories by Type (Benchmark = 140)",
xlab = "Calories", ylab = "Type")
abline(v = 140, lty = 2, lwd = 2, col = "darkred")poultry <- subset(hotdogs, Type == "Poultry")
print(t.test(poultry$Calories, mu = 140, alternative = "less", conf.level = 0.95))##
## One Sample t-test
##
## data: poultry$Calories
## t = -3.8825, df = 16, p-value = 0.0006608
## alternative hypothesis: true mean is less than 140
## 95 percent confidence interval:
## -Inf 128.3139
## sample estimates:
## mean of x
## 118.7647
Applicable for large samples where \(n p_0 \ge 5\) and \(n(1 - p_0) \ge 5\): \[z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0 (1 - p_0)}{n}}}, \quad \text{where } \hat{p} = \frac{x}{n}\]
In R, prop.test() computes Pearson’s chi-square
statistic (\(X^2\)) rather than \(z\) directly; without Yates’ continuity
correction (correct = FALSE), the two statistics satisfy
\(z^2 = X^2\), so \(|z| = \sqrt{X^2}\). For small or moderate
counts, correct = TRUE (the default in
prop.test()) applies Yates’ continuity correction.
In Demonstration Problem 9.3 (Wisconsin Milk Consumption, p. 294), a producer tests whether more than \(17\%\) of Wisconsin residents choose milk as their primary breakfast beverage (\(H_a: p > 0.17\)) using \(x = 116\) out of \(n = 550\) residents at \(\alpha = 0.01\).
z_test_milk <- prop.test(x = 116, n = 550, p = 0.17, alternative = "greater",
conf.level = 0.99, correct = FALSE)
p_hat_milk <- 116 / 550
z_milk_manual <- (p_hat_milk - 0.17) / sqrt(0.17 * (1 - 0.17) / 550)
print(z_test_milk)##
## 1-sample proportions test without continuity correction
##
## data: 116 out of 550, null probability 0.17
## X-squared = 6.5234, df = 1, p-value = 0.005323
## alternative hypothesis: true p is greater than 0.17
## 99 percent confidence interval:
## 0.1733579 1.0000000
## sample estimates:
## p
## 0.2109091
print(sprintf("Manual z = %.4f | Recovered sqrt(X^2) = %.4f (p = %.5f)",
z_milk_manual, sqrt(z_test_milk$statistic), z_test_milk$p.value))## [1] "Manual z = 2.5541 | Recovered sqrt(X^2) = 2.5541 (p = 0.00532)"
Because \(z = 2.5541 > z_{0.01} = 2.3263\) (\(p = 0.00532 < 0.01\)), we reject \(H_0\) and conclude that the proportion of Wisconsin residents choosing milk as their primary breakfast beverage is significantly higher than \(17\%\).
Requires the underlying population to be strictly normally distributed: \[\chi^2 = \frac{(n - 1)s^2}{\sigma_0^2}, \quad \text{with } df = n - 1\]
Base R’s chisq.test() and var.test() do not
perform a single-sample variance test (which is provided by
EnvStats::varTest()). However, we can implement both the
exact \(\chi^2\) hypothesis test and
the exact \((1 - \alpha)\) variance
confidence interval directly using qchisq() and
pchisq().
In Demonstration Problem 9.4 (Overtime Hours Variance), a small business tests whether weekly overtime variance differs from \(\sigma_0^2 = 25\) (\(H_a: \sigma^2 \neq 25\)) across \(n = 16\) weeks at \(\alpha = 0.10\).
one_sample_var_test <- function(x, sigma2_0, alternative = "two.sided", conf.level = 0.95) {
n <- length(x)
df <- n - 1
s2 <- var(x)
chi2_obs <- df * s2 / sigma2_0
alpha <- 1 - conf.level
if (alternative == "greater") {
p_val <- pchisq(chi2_obs, df = df, lower.tail = FALSE)
ci <- c(df * s2 / qchisq(1 - alpha, df = df), Inf)
} else if (alternative == "less") {
p_val <- pchisq(chi2_obs, df = df, lower.tail = TRUE)
ci <- c(0, df * s2 / qchisq(alpha, df = df))
} else {
p_val <- 2 * min(pchisq(chi2_obs, df = df, lower.tail = TRUE),
pchisq(chi2_obs, df = df, lower.tail = FALSE))
ci <- c(df * s2 / qchisq(1 - alpha / 2, df = df),
df * s2 / qchisq(alpha / 2, df = df))
}
list(sample_var = s2, chi_squared = chi2_obs, df = df, p_value = p_val, conf_int = ci)
}
overtime_hrs <- c(57, 46, 48, 63, 56, 53, 51, 53, 52, 44, 55, 51, 44, 44, 48, 50)
ot_res <- one_sample_var_test(overtime_hrs, sigma2_0 = 25, alternative = "two.sided", conf.level = 0.90)
print(sprintf("Overtime s^2 = %.4f | chi^2 = %.4f (df = %d) | p-value = %.4f",
ot_res$sample_var, ot_res$chi_squared, ot_res$df, ot_res$p_value))## [1] "Overtime s^2 = 28.0625 | chi^2 = 16.8375 (df = 15) | p-value = 0.6573"
print(sprintf("90%% CI for variance: [%.4f, %.4f] | Critical chi^2 bounds: [%.4f, %.4f]",
ot_res$conf_int[1], ot_res$conf_int[2],
qchisq(0.05, 15), qchisq(0.95, 15)))## [1] "90% CI for variance: [16.8403, 57.9728] | Critical chi^2 bounds: [7.2609, 24.9958]"
Because \(\chi^2 = 16.8375\) lies well inside the nonrejection region \([7.2609, 24.9958]\) (\(p = 0.6573 > 0.10\)), and \(\sigma_0^2 = 25\) lies inside the \(90\%\) CI \([17.34, 57.97]\), we fail to reject \(H_0\).
To compute \(\beta\) for a specific true alternative value \(\mu_a\):
mu0 <- 30
sigma <- 0.63
n <- 49
alpha <- 0.05
se <- sigma / sqrt(n)
x_crit <- mu0 - qnorm(1 - alpha) * se
mu_target <- 29.6
z_beta_target <- (x_crit - mu_target) / se
beta_target <- pnorm(z_beta_target, lower.tail = FALSE)
power_target <- 1 - beta_target
print(sprintf("At mu_a = %.1f -> x_c = %.4f | z_beta = %.4f | beta = %.4f | Power = %.4f",
mu_target, x_crit, z_beta_target, beta_target, power_target))## [1] "At mu_a = 29.6 -> x_c = 29.8520 | z_beta = 2.7996 | beta = 0.0026 | Power = 0.9974"
mu_alt <- seq(29.4, 30.0, by = 0.01)
beta_vals <- pnorm(x_crit, mean = mu_alt, sd = se, lower.tail = FALSE)
power_vals <- 1 - beta_vals
curve_df <- data.frame(
mu_a = rep(mu_alt, 2),
Probability = c(power_vals, beta_vals),
Curve = rep(c("Power (1 - beta)", "Type II Error (beta)"), each = length(mu_alt))
)
ggplot(curve_df, aes(x = mu_a, y = Probability, color = Curve)) +
geom_line(linewidth = 1.1) +
geom_vline(xintercept = 29.6, linetype = "dashed", color = "gray40") +
labs(title = "Power and Operating Characteristic (OC) Curves (Left-Tailed Test)",
subtitle = expression(paste(mu[0], " = 30, ", sigma, " = 0.63, n = 49, ", alpha, " = 0.05")),
x = expression("True Alternative Mean (" * mu[a] * ")"),
y = "Probability") +
theme_minimal()Chapter 16 introduces nonparametric Chi-Square (\(\chi^2\)) procedures for analyzing nominal or ordinal frequency counts. Unlike parametric statistics (which rely on restrictive assumptions about population parameters and distributions, such as normality), nonparametric methods require fewer distributional assumptions.
Tests whether observed sample frequencies (\(f_o\)) across \(k\) categories conform to a hypothesized theoretical or historical distribution (\(f_e\)).
Hypotheses:
Test Statistic: \[\chi^2 = \sum_{i=1}^{k} \frac{(f_{o,i} - f_{e,i})^2}{f_{e,i}}, \quad \text{where } f_{e,i} = n \cdot p_{i,0}\]
Degrees of Freedom: \[df = k - 1 - m\] (where \(k\) is the number of categories and \(m\) is the number of population parameters estimated from the sample data):
Sample Size Requirement: Every category must have an expected frequency of \(f_{e,i} \ge 5\) (combine adjacent categories if necessary). Goodness-of-fit tests are always right-tailed.
In Demonstration Problem 16.1 (Supermarket Milk Sales), producers test at \(\alpha = 0.01\) whether monthly milk sales (in gallons) are uniformly distributed across the 12 months of the year (\(p_i = 1/12\) for each month, \(df = 12 - 1 = 11\)).
gallons <- c(1610, 1585, 1649, 1590, 1540, 1397, 1410, 1350, 1495, 1564, 1602, 1655)
expected_gal <- rep(sum(gallons) / length(gallons), length(gallons))
chi2_gof_manual <- sum((gallons - expected_gal)^2 / expected_gal)
gof_milk <- chisq.test(x = gallons, p = rep(1 / length(gallons), length(gallons)))
print(gof_milk)##
## Chi-squared test for given probabilities
##
## data: gallons
## X-squared = 74.376, df = 11, p-value = 1.785e-11
print(sprintf("Manual chi^2 = %.4f | Critical chi^2 (alpha = 0.01, df = 11): %.4f",
chi2_gof_manual, qchisq(0.99, df = 11)))## [1] "Manual chi^2 = 74.3758 | Critical chi^2 (alpha = 0.01, df = 11): 24.7250"
Because \(\chi^2 = 74.3758 > \chi^2_{0.01, 11} = 24.7250\) (\(p < 0.0001\)), we reject \(H_0\); monthly milk sales are not uniformly distributed across the year (notice the summer dip in June–August).
Tests whether two categorical variables organized in an \(r \times c\) contingency table (two-way frequency table with \(r\) rows and \(c\) columns) are statistically independent.
Hypotheses:
Expected Frequency for Cell \((i, j)\): \[f_{e,ij} = \frac{(\text{Row } i \text{ Total}) \times (\text{Column } j \text{ Total})}{N}\]
Test Statistic & Degrees of Freedom: \[\chi^2 = \sum_{i=1}^{r} \sum_{j=1}^{c} \frac{(f_{o,ij} - f_{e,ij})^2}{f_{e,ij}}, \quad \text{with } df = (r - 1)(c - 1)\]
Decision Rule: Reject \(H_0\) if \(\chi^2 > \chi^2_{\alpha, \, df}\) (or if \(p\text{-value} < \alpha\)). Requires \(f_{e,ij} \ge 5\) in all cells.
In Demonstration Problem 16.3 (Age vs. Lunch Beverage
Preference), \(N = 309\) lunch
customers are cross-classified by age group (21-34,
35-55, >55) and preferred beverage
(Coffee/Tea, Soft Drink, Other)
at \(\alpha = 0.05\).
bev_mat <- matrix(data = c(26, 95, 18,
41, 40, 20,
24, 13, 32),
nrow = 3, byrow = TRUE,
dimnames = list(Age = c("21-34", "35-55", ">55"),
Beverage = c("Coffee/Tea", "Soft Drink", "Other")))
print(bev_mat)## Beverage
## Age Coffee/Tea Soft Drink Other
## 21-34 26 95 18
## 35-55 41 40 20
## >55 24 13 32
##
## Pearson's Chi-squared test
##
## data: bev_mat
## X-squared = 59.405, df = 4, p-value = 3.869e-12
## Beverage
## Age Coffee/Tea Soft Drink Other
## 21-34 40.94 66.58 31.49
## 35-55 29.74 48.38 22.88
## >55 20.32 33.05 15.63
## [1] "Critical chi^2 (alpha = 0.05, df = 4): 9.4877"
Because \(\chi^2 = 59.4046 > \chi^2_{0.05, 4} = 9.4877\) (\(p = 3.87 \times 10^{-12} < 0.05\)), and all expected cell counts exceed \(5\), we reject \(H_0\) and conclude that preferred lunch beverage depends strongly on customer age.
| Test / Concept | Test Statistic Formula | Degrees of Freedom (\(df\)) | Key Conditions & R Implementation |
|---|---|---|---|
| 1-Sample Mean (\(\sigma\) known) | \(z = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}\) | — | Normal pop. or \(n \ge 30\); custom
one_sample_z_test() or BSDA::z.test() |
| 1-Sample Mean (\(\sigma\) unknown) | \(t = \frac{\bar{x} - \mu_0}{s / \sqrt{n}}\) | \(n - 1\) | Normal pop. or \(n \ge 30\);
t.test(x, mu = mu0, alternative = ...) |
| 1-Sample Proportion (\(p\)) | \(z = \frac{\hat{p} - p_0}{\sqrt{p_0(1 - p_0)/n}}\) | — | \(np_0 \ge 5\) and \(n(1 - p_0) \ge 5\);
prop.test(x, n, p = p0, correct = FALSE) |
| 1-Sample Variance (\(\sigma^2\)) | \(\chi^2 = \frac{(n - 1)s^2}{\sigma_0^2}\) | \(n - 1\) | Underlying pop. strictly normal; qchisq(),
pchisq(), or EnvStats::varTest() |
| Type II Error & Power | \(z_\beta = \frac{\bar{x}_c - \mu_a}{\sigma / \sqrt{n}}\) | — | \(\text{Power} = 1 - \beta\);
pnorm() around \(\bar{x}_c\) under \(\mu_a\) |
| \(\chi^2\) Goodness-of-Fit | \(\chi^2 = \sum \frac{(f_o - f_e)^2}{f_e}\) | \(k - 1 - m\) | \(m = 0\) (fixed/uniform), \(m = 1\) (Poisson), \(m = 2\) (Normal); \(f_e \ge 5\);
chisq.test(x, p = ...) |
| \(\chi^2\) Test of Independence | \(\chi^2 = \sum \sum \frac{(f_{o,ij} - f_{e,ij})^2}{f_{e,ij}}\) | \((r - 1)(c - 1)\) | \(f_{e,ij} = \frac{R_i \cdot C_j}{N} \ge
5\); right-tailed; chisq.test(matrix) |