Machine Learning I: Training, Test Data, Dummy Variables, and Residual Plots
November 23, 2026
hms_heavy_days: Days with a heavy smoke plume over the county that month.
pm25: Monthly mean PM2.5 at the county’s EPA monitors, in µg/m³.
hms_heavy_days and pm25 by estimating a true value of b1.b1 is denoted by \(\hat{\texttt{b1}}\). This coefficient describes an association unless a causal design supports a causal interpretation.pm25[i] for new county-month ipm25[i] is denoted by \(\widehat{\texttt{pm25}}\texttt{[i]}\), where\[\widehat{\texttt{pm25}}\texttt{[i]} \;=\quad \hat{\texttt{b0}} \,+\, \hat{\texttt{b1}}\texttt{*hms_heavy_days[i]}\]
Training data: When we’re building a linear regression model, we need data to train the model.
Test data: We also need data to test whether the model works well on new data.



The probability density function for the uniform distribution looks like:
With the uniform distribution, any values of \(x\) between 0 and 1 is equally likely drawn.

pm25 > 20 are in the training data and the observations with pm25 <= 20 are in the test data.
Linear regression models require numerical predictors, but many variables are categorical.
The Approach:
Convert categorical variables into numerical format using dummy variables.
Why Do This?
Definition: Binary indicators (0 or 1) representing categories.
Purpose: Transform qualitative data into a quantitative form for regression analysis.
Example: \[ D_i = \begin{cases} 1, & \text{if the observation belongs to the category} \\ 0, & \text{otherwise} \end{cases} \]
Consider a regression model including a dummy variable: \[ y_i = \beta_0 + \beta_1 x_i + \beta_2 D_i + \epsilon_i \]
\(x_i\): A continuous predictor.
\(D_i\): Dummy variable (e.g., calendar month, county).
Interpretation: \(\beta_2\) captures the difference in the response \(y\) when the category is present (i.e., \(D_i=1\)) versus absent.
🔗 Multicollinearity
Problem: For a categorical variable with \(k\) levels, using all \(k\) dummies causes perfect multicollinearity:
This is problematic, because one dummy is completely predictable from the others.
The intercept already captures the constant part (1), making one of the dummy variables redundant.
Solution: Drop one dummy (choose a reference category)
The reference category is represented by a combination of \(\texttt{month}\) variables.
Proper model: \[ y_i = \beta_0 + \beta_1 D_{1, i} + \beta_2 D_{2, i} + \cdots + \beta_{k-1} D_{(k-1), i} + \epsilon_i \]
Interpretation:
In R, factors automatically generate dummies and avoid the dummy variable trap.
\[ \epsilon_i \sim N(0, \sigma^2) \]
Errors have a mean value of 0 with constant variance \(\sigma^2\).
Errors are uncorrelated with \(X_{1,i}\) and with \(X_{2, i}\).
If we re-arrange the simple regression equation, \[\begin{align} {\epsilon}_{i} \,=\, Y_{i} \,-\, (\, {\beta}_{0} \,+\, {\beta}_{1}X_{1,i} \,). \end{align}\]
\(\texttt{residual_error}_{i}\) is the estimated counterpart of the unobserved error \(\epsilon_{i}\), denoted by \(\hat{\epsilon_{i}}\).
\[ \begin{align} \hat{\epsilon_{i}} \,=\, &Y_i \,-\, \hat{Y}_i\\ \,=\, &Y_{i} \,-\, (\, \hat{\beta_{0}} \,+\, \hat{\beta_{1}}X_{1,i} \,) \end{align} \]
Residual plot: scatterplot of
A residual plot can be used to diagnose the quality of model results.
We assume that \(\epsilon_{i}\) have a mean value of 0 with constant variance \(\sigma^2\):


No systematic mean pattern: mean residual is ~0 within thin vertical strips
Homoskedastic: similar spread of residuals across fitted values
If residual variance changes with fitted values ⇒ heteroscedasticity
# The wildfire regression on the dummy-variable slide, using training rows
model_month <- lm(pm25 ~ hms_heavy_days + tmax + month, data = dtrain)
aug_model <- broom::augment(model_month)
ggplot(aug_model, aes(x = .fitted, y = .resid)) +
geom_point(alpha = 0.25) +
geom_hline(yintercept = 0, linetype = "dashed") +
geom_smooth(se = FALSE, method = "loess", formula = y ~ x) +
labs(
x = "Fitted PM2.5 (µg/m³)",
y = "Residual (µg/m³)",
title = "Wildfire PM2.5: Training Residuals"
)