The Week 8 readings cover Chapter 11 (Analysis of Variance) and Chapter 12, Sections 1–3 (Simple Linear Regression) from Ken Black’s Business Statistics. By the end of this module, you should be able to:
ANOVA is a statistical technique used to test whether the means of three or more populations are equal by analyzing sample variances.
If you want to compare \(k = 4\) group means, there are \(\binom{4}{2} = 6\) pairwise comparisons. Conducting each test at \(\alpha = 0.05\) compounds the experiment-wise Type I error rate: \[\alpha_{\text{overall}} = 1 - (1 - \alpha)^c\] For \(c = 6\) comparisons, \(\alpha_{\text{overall}} = 1 - (0.95)^6 \approx 0.265\) (a \(26.5\%\) chance of at least one false positive). ANOVA tests all means simultaneously at a single \(\alpha\) level.
Total variability in the data (\(SST\)) is partitioned into variability between treatment groups (\(SSC\)) and variability within treatment groups (\(SSE\)):
\[SST = SSC + SSE\]
(where \(k\) is the number of groups, \(n_j\) is the sample size of group \(j\), \(N = \sum n_j\) is total observations, \(\bar{x}_j\) is the group mean, and \(\bar{\bar{x}}\) is the grand mean).
| Source of Variation | Sum of Squares (\(SS\)) | Degrees of Freedom (\(df\)) | Mean Square (\(MS\)) | \(F\)-Ratio |
|---|---|---|---|---|
| Between (Treatments) | \(SSC\) | \(k - 1\) | \(MSC = \frac{SSC}{k - 1}\) | \(F = \frac{MSC}{MSE}\) |
| Within (Error) | \(SSE\) | \(N - k\) | \(MSE = \frac{SSE}{N - k}\) | |
| Total | \(SST\) | \(N - 1\) |
If \(H_0\) is rejected, ANOVA tells us that a difference exists, not where it exists. Tukey’s Honestly Significant Difference (HSD) tests all pairwise differences \(|\bar{x}_i - \bar{x}_j|\) against a critical range (for equal sample sizes \(n\)):
\[\text{HSD} = q_{\alpha, \, k, \, N-k} \sqrt{\frac{MSE}{n}}\]
where \(q\) is obtained from the Studentized Range Distribution table. If \(|\bar{x}_i - \bar{x}_j| > \text{HSD}\), the two group means differ significantly.
k_groups <- 4
c_pairs <- choose(k_groups, 2)
alpha_overall <- 1 - (1 - 0.05)^c_pairs
print(sprintf("Experiment-wise Type I Error (k = %d, c = %d comparisons, alpha = 0.05): %.4f",
k_groups, c_pairs, alpha_overall))## [1] "Experiment-wise Type I Error (k = 4, c = 6 comparisons, alpha = 0.05): 0.2649"
display_data <- data.frame(
design = factor(rep(c("Endcap", "Aisle", "Checkout"), each = 5)),
sales = c(42, 45, 40, 46, 44,
35, 38, 34, 37, 36,
50, 54, 49, 52, 55)
)
grand_mean <- mean(display_data$sales)
grp_means <- tapply(display_data$sales, display_data$design, mean)
grp_ns <- tapply(display_data$sales, display_data$design, length)
ssc <- sum(grp_ns * (grp_means - grand_mean)^2)
sse <- sum((display_data$sales - grp_means[display_data$design])^2)
sst <- sum((display_data$sales - grand_mean)^2)
df_between <- length(grp_means) - 1
df_within <- nrow(display_data) - length(grp_means)
msc <- ssc / df_between
mse <- sse / df_within
f_ratio <- msc / mse
hsd_crit <- qtukey(0.95, nmeans = length(grp_means), df = df_within) * sqrt(mse / grp_ns[1])
print(sprintf("Manual ANOVA -> SSC = %.2f | SSE = %.2f | SST = %.2f | F(%d,%d) = %.4f",
ssc, sse, sst, df_between, df_within, f_ratio))## [1] "Manual ANOVA -> SSC = 641.20 | SSE = 59.20 | SST = 700.40 | F(2,12) = 64.9865"
## [1] "Manual Tukey HSD critical difference (alpha = 0.05): 3.7477"
## Df Sum Sq Mean Sq F value Pr(>F)
## design 2 641.2 320.6 64.99 3.65e-07 ***
## Residuals 12 59.2 4.9
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Tukey multiple comparisons of means
## 95% family-wise confidence level
##
## Fit: aov(formula = sales ~ design, data = display_data)
##
## $design
## diff lwr upr p adj
## Checkout-Aisle 16.0 12.252308 19.747692 0.0000002
## Endcap-Aisle 7.4 3.652308 11.147692 0.0005378
## Endcap-Checkout -8.6 -12.347692 -4.852308 0.0001413
rbd_data <- data.frame(
method = factor(rep(c("Method A", "Method B", "Method C"), times = 4)),
store = factor(rep(paste("Store", 1:4), each = 3)),
productivity = c(72, 78, 84,
68, 74, 79,
75, 81, 88,
70, 76, 82)
)
rbd_model <- aov(productivity ~ method + store, data = rbd_data)
print(summary(rbd_model))## Df Sum Sq Mean Sq F value Pr(>F)
## method 2 288.00 144.00 648.0 9.79e-08 ***
## store 3 94.92 31.64 142.4 5.78e-06 ***
## Residuals 6 1.33 0.22
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
two_way_data <- data.frame(
promo = factor(rep(c("Coupon", "No_Coupon"), each = 6)),
tier = factor(rep(rep(c("Budget", "Standard", "Premium"), each = 2), times = 2)),
units = c(48, 52, 55, 59, 60, 64,
34, 38, 46, 50, 58, 62)
)
two_way_model <- aov(units ~ promo * tier, data = two_way_data)
print(summary(two_way_model))## Df Sum Sq Mean Sq F value Pr(>F)
## promo 1 208.3 208.3 26.042 0.002215 **
## tier 2 648.7 324.3 40.542 0.000327 ***
## promo:tier 2 72.7 36.3 4.542 0.062945 .
## Residuals 6 48.0 8.0
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Simple linear regression models the linear relationship between a single independent (predictor) variable (\(X\)) and a dependent (response) variable (\(Y\)).
The Ordinary Least Squares (OLS) method finds \(b_0\) and \(b_1\) by minimizing the Sum of Squared Errors (Residuals): \[\min \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2\]
Just like ANOVA, regression partitions total variation in \(Y\) (\(SST\)) into explained variation (\(SSR\)) and unexplained variation (\(SSE\)):
\[SST = SSR + SSE\]
Standard Error of the Estimate (\(s_e\)): Measures the typical dispersion of observed data points around the regression line (in the units of \(Y\)): \[s_e = \sqrt{\frac{SSE}{n - 2}}\] (Note: \(df = n - 2\) because two parameters, \(b_0\) and \(b_1\), are estimated).
Coefficient of Determination (\(R^2\)): Represents the proportion of total variation in \(Y\) explained by \(X\): \[R^2 = \frac{SSR}{SST} = 1 - \frac{SSE}{SST}\]
ad_data <- data.frame(
ad_spend = c(2, 4, 5, 6, 8, 10, 11, 14),
revenue = c(15, 22, 24, 29, 34, 41, 43, 52)
)
x_bar_ad <- mean(ad_data$ad_spend)
y_bar_rev <- mean(ad_data$revenue)
ss_xy <- sum((ad_data$ad_spend - x_bar_ad) * (ad_data$revenue - y_bar_rev))
ss_xx <- sum((ad_data$ad_spend - x_bar_ad)^2)
b1_manual <- ss_xy / ss_xx
b0_manual <- y_bar_rev - b1_manual * x_bar_ad
y_hat <- b0_manual + b1_manual * ad_data$ad_spend
sst_reg <- sum((ad_data$revenue - y_bar_rev)^2)
ssr_reg <- sum((y_hat - y_bar_rev)^2)
sse_reg <- sum((ad_data$revenue - y_hat)^2)
se_manual <- sqrt(sse_reg / (nrow(ad_data) - 2))
r2_manual <- ssr_reg / sst_reg
reg_model <- lm(revenue ~ ad_spend, data = ad_data)
print(summary(reg_model))##
## Call:
## lm(formula = revenue ~ ad_spend, data = ad_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.7991 -0.5312 -0.1607 0.4107 1.1205
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 9.39732 0.58528 16.06 3.71e-06 ***
## ad_spend 3.08036 0.06983 44.11 9.09e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.739 on 6 degrees of freedom
## Multiple R-squared: 0.9969, Adjusted R-squared: 0.9964
## F-statistic: 1946 on 1 and 6 DF, p-value: 9.087e-09
b0 <- coef(reg_model)[1]
b1 <- coef(reg_model)[2]
r_sq <- summary(reg_model)$r.squared
s_e <- summary(reg_model)$sigma
print(sprintf("Manual OLS -> b0 = %.4f, b1 = %.4f | lm() -> b0 = %.4f, b1 = %.4f",
b0_manual, b1_manual, b0, b1))## [1] "Manual OLS -> b0 = 9.3973, b1 = 3.0804 | lm() -> b0 = 9.3973, b1 = 3.0804"
print(sprintf("SST = %.2f | SSR = %.2f | SSE = %.2f | s_e = %.4f | R^2 = %.4f (r^2 = %.4f)",
sst_reg, ssr_reg, sse_reg, se_manual, r_sq, cor(ad_data$ad_spend, ad_data$revenue)^2))## [1] "SST = 1066.00 | SSR = 1062.72 | SSE = 3.28 | s_e = 0.7390 | R^2 = 0.9969 (r^2 = 0.9969)"
ggplot(ad_data, aes(x = ad_spend, y = revenue)) +
geom_point(color = "darkblue", size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "red") +
labs(title = "Simple Linear Regression: Ad Spend vs. Revenue",
x = "Ad Spend ($100s)",
y = "Weekly Revenue ($1000s)") +
theme_minimal()| Concept / Metric | Formula | Notes |
|---|---|---|
| Experiment-Wise Error | \(\alpha_{\text{overall}} = 1 - (1 - \alpha)^c\) | \(c = \frac{k(k-1)}{2}\) pairwise comparisons |
| One-Way ANOVA \(F\)-Stat | \(F = \frac{MSC}{MSE} = \frac{SSC / (k - 1)}{SSE / (N - k)}\) | Always right-tailed |
| Tukey’s HSD | \(\text{HSD} = q_{\alpha, \, k, \, N-k} \sqrt{\frac{MSE}{n}}\) | Equal group sizes \(n\) |
| OLS Slope (\(b_1\)) | \(b_1 = \frac{SS_{xy}}{SS_{xx}} = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}\) | Marginal change in \(\hat{y}\) per unit \(x\) |
| OLS Intercept (\(b_0\)) | \(b_0 = \bar{y} - b_1 \bar{x}\) | Line always passes through \((\bar{x}, \bar{y})\) |
| Standard Error (\(s_e\)) | \(s_e = \sqrt{\frac{SSE}{n - 2}}\) | Standard deviation of residuals |
| Coeff. of Determination (\(R^2\)) | \(R^2 = \frac{SSR}{SST} = 1 - \frac{SSE}{SST}\) | Proportion of variance in \(Y\) explained |