The Week 9 readings complete Simple Linear Regression (Ken Black Sections 12.4–12.7 and 12.9), introduce Multiple Regression Analysis (Ken Black Chapter 13), and examine the properties, limitations, and diagnostic cautions of Pearson’s Correlation and the Coefficient of Determination (Wilcox Chapter 8, Section 8.3). By the end of this module, you should be able to:
A residual is the vertical distance between an observed value \(y_i\) and its predicted value \(\hat{y}_i\):
\[e_i = y_i - \hat{y}_i\]
Summing the squared residuals produces the Sum of Squares of Error (\(SSE\)), from which the Standard Error of the Estimate (\(s_e\)) measures the typical dispersion of observed data points around the regression line in the units of \(Y\):
\[SSE = \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} (y_i - \hat{y}_i)^2, \qquad s_e = \sqrt{\frac{SSE}{n - 2}}\]
Under normally distributed errors centered at \(0\), the Empirical Rule implies that roughly \(95\%\) of residuals should fall within \(0 \pm 2s_e\). Plotting residuals (\(e_i\)) against \(x_i\) or \(\hat{y}_i\) diagnoses violations of the LINE assumptions:
In the Chapter 12 R supplement, operating cost
(cost_1000, in \(\$1,000\text{s}\)) is modeled as a linear
function of the number of passengers (no_of_pass) across
\(n = 12\) flights. We fit the model,
visualize the vertical residual segments, calculate \(SSE\) and \(s_e\), and inspect the four standard R
diagnostic plots.
no_of_pass <- c(61, 63, 67, 69, 70, 74, 76, 81, 86, 91, 95, 97)
cost_1000 <- c(4.28, 4.08, 4.42, 4.17, 4.48, 4.3, 4.82, 4.7, 5.11, 5.13, 5.64, 5.56)
airline_df <- data.frame(no_of_pass = no_of_pass, cost_1000 = cost_1000)
airline_model <- lm(cost_1000 ~ no_of_pass, data = airline_df)
airline_df$fitted_cost <- fitted(airline_model)
airline_df$resid_cost <- residuals(airline_model)
sse_airline <- sum(airline_df$resid_cost^2)
n_airline <- nrow(airline_df)
rse_airline <- sqrt(sse_airline / (n_airline - 2))
print(sprintf("Fitted Equation: cost_hat = %.4f + %.6f * no_of_pass",
coef(airline_model)[1], coef(airline_model)[2]))## [1] "Fitted Equation: cost_hat = 1.5698 + 0.040702 * no_of_pass"
print(sprintf("SSE = %.6f | s_e (formula) = %.6f | summary()$sigma = %.6f",
sse_airline, rse_airline, summary(airline_model)$sigma))## [1] "SSE = 0.314060 | s_e (formula) = 0.177217 | summary()$sigma = 0.177217"
ggplot(airline_df, aes(x = no_of_pass, y = cost_1000)) +
geom_segment(aes(xend = no_of_pass, yend = fitted_cost), color = "gray45", linetype = "dashed") +
geom_smooth(method = "lm", se = FALSE, color = "steelblue", linewidth = 1) +
geom_point(color = "red2", size = 2.5) +
labs(title = "Airline Cost vs. Number of Passengers with Residual Segments",
x = "Number of Passengers",
y = "Cost ($1,000)") +
theme_minimal()To test whether \(X\) has a statistically significant linear effect on \(Y\), we test whether the population slope \(\beta_1\) differs from a hypothesized value \(\beta_{1,0}\) (typically \(\beta_{1,0} = 0\)).
\[s_{b_1} = \frac{s_e}{\sqrt{SS_{xx}}} = \frac{s_e}{\sqrt{\sum_{i=1}^{n} (x_i - \bar{x})^2}} = \frac{s_e}{\sqrt{\sum_{i=1}^{n} x_i^2 - \frac{(\sum_{i=1}^{n} x_i)^2}{n}}}\]
b1 <- coef(airline_model)[2]
ss_xx <- sum((no_of_pass - mean(no_of_pass))^2)
s_b1 <- rse_airline / sqrt(ss_xx)
t_slope <- (b1 - 0) / s_b1
p_slope <- 2 * pt(abs(t_slope), df = n_airline - 2, lower.tail = FALSE)
t_crit_95 <- qt(0.975, df = n_airline - 2)
ci_slope_manual <- c(b1 - t_crit_95 * s_b1, b1 + t_crit_95 * s_b1)
print(sprintf("SS_xx = %.2f | s_b1 = %.6f | t_obs = %.4f | p-value = %.6e",
ss_xx, s_b1, t_slope, p_slope))## [1] "SS_xx = 1689.00 | s_b1 = 0.004312 | t_obs = 9.4389 | p-value = 2.691644e-06"
## [1] "Manual 95% CI for beta_1: [0.031094, 0.050310]"
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.5697928 0.338082961 4.643218 9.174811e-04
## no_of_pass 0.0407016 0.004312128 9.438865 2.691644e-06
## 2.5 % 97.5 %
## (Intercept) 0.81649700 2.32308856
## no_of_pass 0.03109358 0.05030962
Because \(t = 9.439\) (\(p = 2.69 \times 10^{-6} < 0.05\)) and the \(95\%\) confidence interval \([0.0311, 0.0503]\) excludes \(0\), we reject \(H_0: \beta_1 = 0\) and conclude that number of passengers is a statistically significant linear predictor of operating cost.
For a specific predictor value \(x_0\), the point estimate is \(\hat{y}_0 = b_0 + b_1 x_0\). However, the margin of error depends on whether we are estimating the average outcome across all units at \(x_0\) or predicting a single individual outcome:
Confidence Interval for the Conditional Mean \(E(Y \mid x_0)\): \[\hat{y}_0 \pm t_{\alpha/2,\, n-2} \, s_e \sqrt{\frac{1}{n} + \frac{(x_0 - \bar{x})^2}{SS_{xx}}}\]
Prediction Interval for a Single Individual Value \(Y_0\): \[\hat{y}_0 \pm t_{\alpha/2,\, n-2} \, s_e \sqrt{1 + \frac{1}{n} + \frac{(x_0 - \bar{x})^2}{SS_{xx}}}\]
Key Insight: The prediction interval is always wider than the confidence interval because of the additional \(+1\) inside the square root (accounting for individual variation \(\epsilon_i\) around the mean). Both intervals are narrowest at \(x_0 = \bar{x}\) and widen as \(x_0\) moves away from \(\bar{x}\), making extrapolation beyond the observed range of \(X\) risky.
x0 <- 73
y0_hat <- coef(airline_model)[1] + b1 * x0
leverage_term <- (1 / n_airline) + ((x0 - mean(no_of_pass))^2 / ss_xx)
ci_manual <- y0_hat + c(-1, 1) * t_crit_95 * rse_airline * sqrt(leverage_term)
pi_manual <- y0_hat + c(-1, 1) * t_crit_95 * rse_airline * sqrt(1 + leverage_term)
new_pass <- data.frame(no_of_pass = x0)
ci_r <- predict(airline_model, newdata = new_pass, interval = "confidence", level = 0.95)
pi_r <- predict(airline_model, newdata = new_pass, interval = "prediction", level = 0.95)
print(sprintf("Point Estimate at x0 = 73: %.6f", y0_hat))## [1] "Point Estimate at x0 = 73: 4.541009"
print(sprintf("Manual 95%% CI: [%.6f, %.6f] | predict(): [%.6f, %.6f]",
ci_manual[1], ci_manual[2], ci_r[2], ci_r[3]))## [1] "Manual 95% CI: [4.419097, 4.662922] | predict(): [4.419097, 4.662922]"
print(sprintf("Manual 95%% PI: [%.6f, %.6f] | predict(): [%.6f, %.6f]",
pi_manual[1], pi_manual[2], pi_r[2], pi_r[3]))## [1] "Manual 95% PI: [4.127753, 4.954266] | predict(): [4.127753, 4.954266]"
grid_x <- data.frame(no_of_pass = seq(min(no_of_pass), max(no_of_pass), length.out = 100))
ci_band <- as.data.frame(predict(airline_model, newdata = grid_x, interval = "confidence"))
pi_band <- as.data.frame(predict(airline_model, newdata = grid_x, interval = "prediction"))
band_df <- data.frame(
no_of_pass = grid_x$no_of_pass,
fit = ci_band$fit,
ci_lwr = ci_band$lwr,
ci_upr = ci_band$upr,
pi_lwr = pi_band$lwr,
pi_upr = pi_band$upr
)
ggplot(band_df, aes(x = no_of_pass, y = fit)) +
geom_ribbon(aes(ymin = pi_lwr, ymax = pi_upr, fill = "95% Prediction Interval"), alpha = 0.2) +
geom_ribbon(aes(ymin = ci_lwr, ymax = ci_upr, fill = "95% Confidence Interval"), alpha = 0.35) +
geom_line(color = "navy", linewidth = 1) +
geom_point(data = airline_df, aes(x = no_of_pass, y = cost_1000), color = "red2", size = 2.5) +
geom_vline(xintercept = 73, linetype = "dotted", color = "gray30") +
scale_fill_manual(name = "Interval Type",
values = c("95% Confidence Interval" = "steelblue",
"95% Prediction Interval" = "orange")) +
labs(title = "Confidence Interval for Mean vs. Prediction Interval for Individual Flight",
x = "Number of Passengers",
y = "Operating Cost ($1,000)") +
theme_minimal()## Analysis of Variance Table
##
## Response: cost_1000
## Df Sum Sq Mean Sq F value Pr(>F)
## no_of_pass 1 2.79803 2.79803 89.092 2.692e-06 ***
## Residuals 10 0.31406 0.03141
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
ssr_airline <- anova_airline["no_of_pass", "Sum Sq"]
sst_airline <- ssr_airline + sse_airline
r2_manual <- ssr_airline / sst_airline
f_manual <- (ssr_airline / 1) / (sse_airline / (n_airline - 2))
p_f_manual <- pf(f_manual, df1 = 1, df2 = n_airline - 2, lower.tail = FALSE)
print(sprintf("R^2 (formula) = %.6f | summary()$r.squared = %.6f",
r2_manual, summary(airline_model)$r.squared))## [1] "R^2 (formula) = 0.899084 | summary()$r.squared = 0.899084"
## [1] "t^2 = 89.0922 | F_obs = 89.0922 | F p-value = 2.691644e-06"
Simple linear regression can also model a linear time trend by encoding ordered time periods (\(t = 1, 2, \dots, n\)) as the independent variable. In Demonstration Problem 12.7 (from the Chapter 12 R supplement), monthly sales from January (\(t = 1\)) through August (\(t = 8\)) are used to forecast sales for October (\(t = 10\)).
month_fac <- factor(c("January", "February", "March", "April", "May", "June", "July", "August"),
levels = month.name)
month_num <- as.numeric(month_fac)
sales_trend <- c(32569, 32274, 32583, 32304, 32149, 32077, 31989, 31977)
trend_df <- data.frame(month_fac = month_fac, month_num = month_num, sales = sales_trend)
ts_regression <- lm(sales ~ month_num, data = trend_df)
oct_pred <- predict(ts_regression, newdata = data.frame(month_num = 10), interval = "prediction")
print(summary(ts_regression))##
## Call:
## lm(formula = sales ~ month_num, data = trend_df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -181.786 -38.821 -6.643 29.875 213.429
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 32628.21 93.28 349.805 3.68e-14 ***
## month_num -86.21 18.47 -4.667 0.00344 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 119.7 on 6 degrees of freedom
## Multiple R-squared: 0.7841, Adjusted R-squared: 0.7481
## F-statistic: 21.79 on 1 and 6 DF, p-value: 0.00344
print(sprintf("October (t = 10) Forecast: %.2f (95%% PI: [%.2f, %.2f])",
oct_pred[1], oct_pred[2], oct_pred[3]))## [1] "October (t = 10) Forecast: 31766.07 (95% PI: [31368.18, 32163.96])"
ggplot(trend_df, aes(x = month_num, y = sales)) +
geom_point(color = "darkblue", size = 2.8) +
geom_smooth(method = "lm", se = FALSE, color = "firebrick") +
scale_x_continuous(breaks = 1:10, labels = c(month.abb[1:8], "Sep", "Oct")) +
geom_point(aes(x = 10, y = oct_pred[1]), color = "darkgreen", shape = 17, size = 3.5) +
labs(title = "Trend-Line Regression Forecast (Demonstration Problem 12.7)",
x = "Month",
y = "Sales") +
theme_minimal()Rand Wilcox (Basic Statistics, Section 8.3) provides crucial mathematical connections and practical warnings regarding Pearson’s product-moment correlation (\(r\)) and the coefficient of determination (\(r^2\)).
r_from_slope <- b1 * (sd(no_of_pass) / sd(cost_1000))
r_direct <- cor(no_of_pass, cost_1000)
r2_pre <- (sum((cost_1000 - mean(cost_1000))^2) - sum((cost_1000 - fitted(airline_model))^2)) /
sum((cost_1000 - mean(cost_1000))^2)
r2_var <- 1 - var(residuals(airline_model)) / var(cost_1000)
r2_y_yhat <- cor(cost_1000, fitted(airline_model))^2
print(sprintf("r via b1*(sx/sy) = %.6f | cor(x, y) = %.6f", r_from_slope, r_direct))## [1] "r via b1*(sx/sy) = 0.948200 | cor(x, y) = 0.948200"
print(sprintf("r^2 (PRE) = %.6f | r^2 (1 - Var(e)/Var(Y)) = %.6f | Cor(Y, Y_hat)^2 = %.6f",
r2_pre, r2_var, r2_y_yhat))## [1] "r^2 (PRE) = 0.899084 | r^2 (1 - Var(e)/Var(Y)) = 0.899084 | Cor(Y, Y_hat)^2 = 0.899084"
\[T = r \sqrt{\frac{n - 2}{1 - r^2}}, \quad \text{with } df = n - 2\]
interest_rate <- c(7.43, 7.48, 8, 7.75, 7.6, 7.63, 7.68, 7.67, 7.59, 8.07, 8.03, 8)
futures <- c(221, 222, 226, 225, 224, 223, 223, 226, 226, 235, 233, 241)
n_fut <- length(interest_rate)
r_fut <- cor(interest_rate, futures)
T_fut <- r_fut * sqrt((n_fut - 2) / (1 - r_fut^2))
p_fut <- 2 * pt(abs(T_fut), df = n_fut - 2, lower.tail = FALSE)
print(sprintf("r = %.6f | Manual T = %.4f (df = %d) | p-value = %.6f",
r_fut, T_fut, n_fut - 2, p_fut))## [1] "r = 0.815254 | Manual T = 4.4518 (df = 10) | p-value = 0.001232"
##
## Pearson's product-moment correlation
##
## data: interest_rate and futures
## t = 4.4518, df = 10, p-value = 0.001232
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.4535630 0.9463714
## sample estimates:
## cor
## 0.8152537
The derivation of Student’s \(T\) relies heavily on homoscedasticity. When heteroscedasticity is present, \(T\) uses the wrong standard error. Even when the true slope and correlation are zero (\(\rho = 0\)), the probability of falsely rejecting \(H_0\) can inflate well above \(\alpha = 0.05\). Thus, \(T\) is more accurately viewed as a test of independence rather than strictly a test that \(\rho = 0\).
set.seed(401)
x_curve <- seq(-5, 5, length.out = 21)
y_curve <- x_curve^2
curve_test <- cor.test(x_curve, y_curve)
print(sprintf("Deterministic Y = X^2: r = %.4f | p-value = %.4f",
curve_test$estimate, curve_test$p.value))## [1] "Deterministic Y = X^2: r = -0.0000 | p-value = 1.0000"
n_sim <- 1000
n_obs <- 100
reject_count <- 0
for (i in 1:n_sim) {
x_het <- runif(n_obs, -3, 3)
y_het <- (x_het^2) * rnorm(n_obs, mean = 0, sd = 1)
if (cor.test(x_het, y_het)$p.value < 0.05) {
reject_count <- reject_count + 1
}
}
print(sprintf("Heteroscedastic rho = 0 simulation (alpha = 0.05): empirical rejection rate = %.3f",
reject_count / n_sim))## [1] "Heteroscedastic rho = 0 simulation (alpha = 0.05): empirical rejection rate = 0.178"
Wilcox highlights five features that strongly influence \(r\), warning against interpreting \(r\) without plotting the data:
Wilcox’s Golden Rule: Always inspect a scatterplot with the fitted regression line overlaid. Even a high \(r^2\) close to \(1\) does not guarantee that \(\hat{Y}\) provides practically accurate predictions if both \(\hat{Y}\) and \(\bar{Y}\) have large errors.
set.seed(99)
x_base <- seq(1, 10, length.out = 25)
y_tight <- 2 + 1.5 * x_base + rnorm(25, 0, 1.0)
y_noisy <- 2 + 1.5 * x_base + rnorm(25, 0, 5.5)
x_out <- c(x_base[1:24], 25)
y_out <- c(y_tight[1:24], -15)
idx_sub <- x_base >= 4 & x_base <= 7
x_rest <- x_base[idx_sub]
y_rest <- y_noisy[idx_sub]
r_tight <- cor(x_base, y_tight)
r_noisy <- cor(x_base, y_noisy)
r_out <- cor(x_out, y_out)
r_rest <- cor(x_rest, y_rest)
print(sprintf("r (Tight Linear) = %.3f | r (High Noise) = %.3f | r (Single Outlier) = %.3f | r (Restricted Range) = %.3f",
r_tight, r_noisy, r_out, r_rest))## [1] "r (Tight Linear) = 0.970 | r (High Noise) = 0.437 | r (Single Outlier) = -0.317 | r (Restricted Range) = 0.425"
feat_df <- rbind(
data.frame(x = x_base, y = y_tight, scenario = sprintf("1. Tight Linear (r = %.2f)", r_tight)),
data.frame(x = x_base, y = y_noisy, scenario = sprintf("2. Large Residuals (r = %.2f)", r_noisy)),
data.frame(x = x_out, y = y_out, scenario = sprintf("3. Single Outlier (r = %.2f)", r_out)),
data.frame(x = x_rest, y = y_rest, scenario = sprintf("4. Range Restricted (r = %.2f)", r_rest))
)
ggplot(feat_df, aes(x = x, y = y)) +
geom_point(color = "steelblue", size = 2) +
geom_smooth(method = "lm", se = FALSE, color = "firebrick") +
facet_wrap(~ scenario, scales = "free") +
labs(title = "Wilcox Data Features Distorting Pearson's r",
x = "X",
y = "Y") +
theme_minimal()Multiple regression extends simple linear regression by modeling a dependent variable \(Y\) as a linear function of \(k\) independent predictor variables (\(X_1, X_2, \dots, X_k\)).
Using 15 even-numbered years over a 28-year span, we model the annual
U.S. prime interest rate (interest) as a linear function of
the annual unemployment rate (unemployment) and annual
personal saving rate (savings), and predict the prime
interest rate when unemployment = 6.5 and
savings = 5.0.
interest_rate <- c(8.33, 9.32, 10.01, 6.25, 7.15, 8.27, 8.35, 9.23, 4.67, 4.34,
7.96, 5.09, 3.25, 3.25, 3.25)
unemp_rate <- c(7, 5.5, 5.6, 7.5, 6.1, 5.4, 4.5, 4, 5.8, 5.5, 4.6, 5.8, 9.6, 8.1, 6.2)
pers_saving <- c(8.2, 7.3, 7, 7.7, 4.8, 4, 4.3, 2.3, 2.4, 2.1, 0.7, 1.8, 5.8, 7.6, 4.8)
demo_13.1 <- data.frame(interest = interest_rate,
unemployment = unemp_rate,
savings = pers_saving)
prime_model <- lm(interest ~ unemployment + savings, data = demo_13.1)
print(summary(prime_model))##
## Call:
## lm(formula = interest ~ unemployment + savings, data = demo_13.1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -3.1846 -0.7148 0.0754 1.0092 1.8082
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 13.5786 1.7276 7.860 4.5e-06 ***
## unemployment -1.6622 0.3372 -4.929 0.000349 ***
## savings 0.6586 0.1990 3.309 0.006234 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.496 on 12 degrees of freedom
## Multiple R-squared: 0.6719, Adjusted R-squared: 0.6172
## F-statistic: 12.29 on 2 and 12 DF, p-value: 0.001248
prime_pred <- predict(prime_model,
newdata = data.frame(unemployment = 6.5, savings = 5.0),
interval = "confidence")
print(sprintf("Predicted Prime Interest Rate (unemp = 6.5, savings = 5.0): %.4f%% (95%% CI: [%.4f%%, %.4f%%])",
prime_pred[1], prime_pred[2], prime_pred[3]))## [1] "Predicted Prime Interest Rate (unemp = 6.5, savings = 5.0): 6.0676% (95% CI: [5.1876%, 6.9476%])"
Tests whether at least one predictor variable contributes significantly to explaining \(Y\):
Tests whether a specific predictor \(X_j\) is significant after accounting for all other predictors in the model:
n_p <- nrow(demo_13.1)
k_p <- 2
sse_p <- sum(residuals(prime_model)^2)
sst_p <- sum((demo_13.1$interest - mean(demo_13.1$interest))^2)
ssr_p <- sst_p - sse_p
se_p <- sqrt(sse_p / (n_p - k_p - 1))
r2_p <- ssr_p / sst_p
adj_r2_p <- 1 - ((1 - r2_p) * (n_p - 1) / (n_p - k_p - 1))
f_p <- (ssr_p / k_p) / (sse_p / (n_p - k_p - 1))
p_f_p <- pf(f_p, df1 = k_p, df2 = n_p - k_p - 1, lower.tail = FALSE)
print(sprintf("s_e = %.4f | R^2 = %.4f | Adjusted R^2 = %.4f", se_p, r2_p, adj_r2_p))## [1] "s_e = 1.4959 | R^2 = 0.6719 | Adjusted R^2 = 0.6172"
## [1] "Global F(2, 12) = 12.2851 | p-value = 0.001248"
Categorical predictors with \(c\) levels are encoded using \(c - 1\) binary (\(0/1\)) dummy variables to avoid perfect collinearity (the “dummy variable trap”). The omitted category serves as the reference (baseline) group.
Using tableware.csv
from the Lesson 12 solutions, we compare: 1. Binary indicator
model (PRICE ~ BOWL + CASS + DISH + TRAY): Here
plate has \(0\) across all
four indicators and serves as the reference category (\(\hat{y}_{\text{plate}} = b_0 = 51.83\)),
while each coefficient measures the incremental price difference
relative to a plate. 2. Factor contrast model
(PRICE ~ TYPE): R automatically creates \(c - 1\) treatment contrasts using the
alphabetically first level (bowl) as the baseline (\(b_0 = 67.39\)). 3. Adding a
quantitative covariate (PRICE ~ DIAM + TYPE):
Including diameter (DIAM) increases \(R_{\text{adj}}^2\) from \(0.2876\) to \(0.7204\), though residual diagnostics show
increasing error variance (heteroscedasticity). 4. Logarithmic
transformation
(log10(PRICE) ~ log10(DIAM) + TYPE): Stabilizes
variance (\(R_{\text{adj}}^2 =
0.8187\)), confirmed by Bartlett’s test for homogeneity of
variances (\(p = 0.8267\)) and
cross-checked with robust regression (MASS::rlm()).
tableware <- read.csv("https://raw.githubusercontent.com/telpirion/CumulativeStudyGuide/refs/heads/main/data/tableware.csv")
tableware$TYPE <- factor(tableware$TYPE)
m_dummy <- lm(PRICE ~ BOWL + CASS + DISH + TRAY, data = tableware)
m_factor <- lm(PRICE ~ TYPE, data = tableware)
m_cov <- lm(PRICE ~ DIAM + TYPE, data = tableware)
m_log <- lm(log10(PRICE) ~ log10(DIAM) + TYPE, data = tableware)
print(summary(m_dummy))##
## Call:
## lm(formula = PRICE ~ BOWL + CASS + DISH + TRAY, data = tableware)
##
## Residuals:
## Min 1Q Median 3Q Max
## -76.950 -26.362 -2.333 26.109 90.050
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 51.83 12.11 4.281 7.68e-05 ***
## BOWL 15.56 14.28 1.089 0.28086
## CASS 75.09 16.69 4.499 3.67e-05 ***
## DISH 28.31 18.31 1.546 0.12785
## TRAY 47.12 16.69 2.823 0.00665 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 36.33 on 54 degrees of freedom
## Multiple R-squared: 0.3367, Adjusted R-squared: 0.2876
## F-statistic: 6.853 on 4 and 54 DF, p-value: 0.0001548
print(sprintf("Mean PRICE for plates (reference group): %.2f",
mean(tableware$PRICE[tableware$TYPE == "plate"])))## [1] "Mean PRICE for plates (reference group): 51.83"
print(sprintf("Adj R^2 comparison -> TYPE only: %.4f | DIAM + TYPE: %.4f | log10 model: %.4f",
summary(m_factor)$adj.r.squared,
summary(m_cov)$adj.r.squared,
summary(m_log)$adj.r.squared))## [1] "Adj R^2 comparison -> TYPE only: 0.2876 | DIAM + TYPE: 0.7204 | log10 model: 0.8187"
class_intervals <- cut(fitted(m_log), breaks = 5)
bartlett_res <- bartlett.test(residuals(m_log) ~ class_intervals)
print(bartlett_res)##
## Bartlett test of homogeneity of variances
##
## data: residuals(m_log) by class_intervals
## Bartlett's K-squared = 1.4999, df = 4, p-value = 0.8267
m_robust <- rlm(log10(PRICE) ~ log10(DIAM) + TYPE, data = tableware)
coef_compare <- cbind(OLS_log10 = round(coef(m_log), 3),
Robust_rlm = round(coef(m_robust), 3))
print(coef_compare)## OLS_log10 Robust_rlm
## (Intercept) 0.458 0.471
## log10(DIAM) 1.389 1.369
## TYPEcass 0.116 0.140
## TYPEdish 0.092 0.089
## TYPEplate -0.182 -0.174
## TYPEtray -0.080 -0.061
b_int <- coef(m_log)[1]
type_levels <- levels(tableware$TYPE)
line_df <- data.frame(
TYPE = type_levels,
intercept = c(b_int,
b_int + coef(m_log)["TYPEcass"],
b_int + coef(m_log)["TYPEdish"],
b_int + coef(m_log)["TYPEplate"],
b_int + coef(m_log)["TYPEtray"]),
slope = coef(m_log)["log10(DIAM)"]
)
ggplot(tableware, aes(x = log10(DIAM), y = log10(PRICE), color = TYPE)) +
geom_point(size = 2.2) +
geom_abline(data = line_df, aes(intercept = intercept, slope = slope, color = TYPE), linewidth = 0.9) +
labs(title = "Log10(PRICE) vs. Log10(DIAM) by Tableware Type",
x = "log10(Diameter)",
y = "log10(Price)") +
theme_minimal()Multicollinearity occurs when two or more independent predictors (\(X_j\) and \(X_m\)) are highly correlated with each other.
r2_unemp <- summary(lm(unemployment ~ savings, data = demo_13.1))$r.squared
r2_sav <- summary(lm(savings ~ unemployment, data = demo_13.1))$r.squared
vif_prime <- 1 / (1 - c(unemployment = r2_unemp, savings = r2_sav))
print(sprintf("Demo 13.1 VIF -> unemployment: %.3f | savings: %.3f",
vif_prime["unemployment"], vif_prime["savings"]))## [1] "Demo 13.1 VIF -> unemployment: 1.519 | savings: 1.519"
set.seed(42)
n_sim_vif <- 40
ad_spend <- runif(n_sim_vif, 5, 25)
ad_clicks <- 10 * ad_spend + rnorm(n_sim_vif, 0, 4)
store_size <- runif(n_sim_vif, 5, 20)
sales_vif <- 15 + 2.2 * ad_spend + 1.0 * store_size + rnorm(n_sim_vif, 0, 3)
collinear_df <- data.frame(sales = sales_vif, ad_spend = ad_spend,
ad_clicks = ad_clicks, store_size = store_size)
m_collinear <- lm(sales ~ ad_spend + ad_clicks + store_size, data = collinear_df)
print(summary(m_collinear))##
## Call:
## lm(formula = sales ~ ad_spend + ad_clicks + store_size, data = collinear_df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.8119 -1.8260 -0.1419 1.6719 7.3806
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 15.99878 2.01359 7.945 1.97e-09 ***
## ad_spend 1.89354 1.14398 1.655 0.107
## ad_clicks 0.03317 0.11368 0.292 0.772
## store_size 0.90502 0.11374 7.957 1.90e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.989 on 36 degrees of freedom
## Multiple R-squared: 0.9614, Adjusted R-squared: 0.9581
## F-statistic: 298.5 on 3 and 36 DF, p-value: < 2.2e-16
r2_spend <- summary(lm(ad_spend ~ ad_clicks + store_size, data = collinear_df))$r.squared
r2_clicks <- summary(lm(ad_clicks ~ ad_spend + store_size, data = collinear_df))$r.squared
r2_size <- summary(lm(store_size ~ ad_spend + ad_clicks, data = collinear_df))$r.squared
vif_collinear <- 1 / (1 - c(ad_spend = r2_spend, ad_clicks = r2_clicks, store_size = r2_size))
print(round(vif_collinear, 2))## ad_spend ad_clicks store_size
## 211.13 211.38 1.02
Notice that in the collinear model, the overall \(F\)-test is overwhelmingly significant
(\(p < 10^{-10}\), \(R^2 > 0.90\)), yet both
ad_spend and ad_clicks have inflated standard
errors and high \(VIF\) values (\(VIF > 10\)), masking their individual
\(t\)-test significance.
| Concept / Metric | Formula | Degrees of Freedom / R Implementation |
|---|---|---|
| Residual & \(s_e\) (SLR) | \(e_i = y_i - \hat{y}_i, \quad s_e = \sqrt{\dfrac{SSE}{n - 2}}\) | \(df = n - 2\);
residuals(m), summary(m)$sigma |
| Slope \(t\)-Test (SLR) | \(t = \dfrac{b_1 - \beta_{1,0}}{s_{b_1}}\), where \(s_{b_1} = \dfrac{s_e}{\sqrt{SS_{xx}}}\) | \(df = n - 2\);
coef(summary(m)), confint(m) |
| Correlation \(t\)-Test (Wilcox) | \(T = r \sqrt{\dfrac{n - 2}{1 - r^2}}\) | \(df = n - 2\);
cor.test(x, y); sensitive to heteroscedasticity |
| CI for Conditional Mean | \(\hat{y}_0 \pm t_{\alpha/2,\, n-2} \, s_e \sqrt{\dfrac{1}{n} + \dfrac{(x_0 - \bar{x})^2}{SS_{xx}}}\) | predict(m, newdata, interval = "confidence") |
| PI for Individual \(Y_0\) | \(\hat{y}_0 \pm t_{\alpha/2,\, n-2} \, s_e \sqrt{1 + \dfrac{1}{n} + \dfrac{(x_0 - \bar{x})^2}{SS_{xx}}}\) | predict(m, newdata, interval = "prediction") |
| Multiple Regression \(F\)-Test | \(F = \dfrac{MSR}{MSE} = \dfrac{SSR / k}{SSE / (n - k - 1)}\) | \(df_1 = k\), \(df_2 = n - k - 1\);
summary(m)$fstatistic |
| Partial \(t\)-Test & \(s_e\) (MLR) | \(t = \dfrac{b_j}{s_{b_j}}, \quad s_e = \sqrt{\dfrac{SSE}{n - k - 1}}\) | \(df = n - k - 1\);
summary(m)$coefficients |
| Adjusted \(R^2\) | \(R_{\text{adj}}^2 = 1 - \left[(1 - R^2)\dfrac{n - 1}{n - k - 1}\right]\) | summary(m)$adj.r.squared; penalizes extra
predictors |
| Variance Inflation Factor | \(VIF_j = \dfrac{1}{1 - R_j^2}\) | \(VIF > 5\) or \(10\) signals multicollinearity |