0:55 to 1:12
data give the speed of cars and the distances taken to stop. Note that the data were recorded in the 1920s.
Ie how fucked are pedestrians if ur speeding?
# sample!
set.seed(1)
idx <- sample(
1:nrow(cars),
size = 40,
replace = FALSE
)
sample <- cars[idx, ]
test <- cars[-idx, ]# Common axis limits for all three plots
x_limits <- range(cars$speed)
y_limits <- c(0, max(cars$dist))
par(mfrow = c(1, 3))
# Training data
plot(
sample$speed,
sample$dist,
main = "Training Data",
xlab = "Speed (mph)",
ylab = "Stopping Distance (ft)",
col = "forestgreen",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
# Test data
plot(
test$speed,
test$dist,
main = "Test Data",
xlab = "Speed (mph)",
ylab = "Stopping Distance (ft)",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
# Training + test data
plot(
sample$speed,
sample$dist,
main = "Train + Test",
xlab = "Speed (mph)",
ylab = "Stopping Distance (ft)",
col = "forestgreen",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
# Overlay test data
points(
test$speed,
test$dist,
col = "black",
pch = 19
)Resource, Suggested Reading : Ch 5 (Resampling Methods), Sec 1 (Cross-Validation)
\[ \hat{y} = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^3 + \cdots + \beta_p x^p \]
\[ \hat{y} = \beta_0 + \sum_{j=1}^{p} \beta_j x^j \]
\[ \hat{y}=mx+b \]
# Plot training data
plot(
sample$speed,
sample$dist,
main = "Training",
xlab = "Speed (mph)",
ylab = "Stopping Distance (ft)",
col = "forestgreen",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
# Linear model
lines(
speed_grid,
pred_linear,
col = "red",
lwd = 2
)
# Quadratic model
lines(
speed_grid,
pred_quadratic,
col = "blue",
lwd = 2
)
# High-degree polynomial
lines(
speed_grid,
pred_poly,
col = "purple",
lwd = 2
)
# Legend
legend(
"topleft",
legend = c(
"Training Data",
"Linear Model",
"Quadratic Model",
"Degree-14 Polynomial"
),
col = c(
"forestgreen",
"red",
"blue",
"purple"
),
pch = c(19, NA, NA, NA),
lty = c(NA, 1, 1, 1),
lwd = c(NA, 2, 2, 2),
cex = 0.7
)par(mfrow = c(1, 3))
# Linear model
plot(
test$speed,
test$dist,
main = "Bias Model",
xlab = "Speed",
ylab = "Stopping Distance",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
lines(
test$speed[order(test$speed)],
predict(
lm_mdl,
newdata = test
)[order(test$speed)],
col = "red",
lwd = 2
)
# Quadratic model
plot(
test$speed,
test$dist,
main = "Physics Model",
xlab = "Speed",
ylab = "Stopping Distance",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
lines(
test$speed[order(test$speed)],
predict(
mdl_quadratic,
newdata = test
)[order(test$speed)],
col = "blue",
lwd = 2
)
# Degree-14 polynomial
plot(
test$speed,
test$dist,
main = "High Variance",
xlab = "Speed",
ylab = "Stopping Distance",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
lines(
test$speed[order(test$speed)],
predict(
mdl_poly,
newdata = test
)[order(test$speed)],
col = "purple",
lwd = 2
)\[ \text{MSE}(\hat{\theta})=\mathbb{E}(\hat{\theta}-\theta)^2 \]
\(\theta\) = parameter – ie. what u want to est.
\(\hat{\theta}=\) estimate
\[ \text{MSE}(\hat{\theta})=B^2_{\hat{\theta}}+V_{\hat{\theta}} + \epsilon \]
\[ B_{\hat{\theta}}=\mathbb{E}(\hat{\theta})-\theta \]
\[ \text{RMSE}(\hat{\theta})=\sqrt{\text{MSE}(\hat{\theta})} \]
# Sample performance
sample_performance <- data.frame(
Model = c(
"High Bias",
"Physics",
"High Variance"
),
MSE = c(
mean(residuals(lm_mdl)^2),
mean(residuals(mdl_quadratic)^2),
mean(residuals(mdl_poly)^2)
),
RMSE = c(
sqrt(mean(residuals(lm_mdl)^2)),
sqrt(mean(residuals(mdl_quadratic)^2)),
sqrt(mean(residuals(mdl_poly)^2))
)
)# Test performance
test_performance <- data.frame(
Model = c(
"High Bias",
"Physics",
"High Variance"
),
MSE = c(
mean((test$dist - predict(lm_mdl, newdata = test))^2),
mean((test$dist - predict(mdl_quadratic, newdata = test))^2),
mean((test$dist - predict(mdl_poly, newdata = test))^2)
),
RMSE = c(
sqrt(mean((test$dist - predict(lm_mdl, newdata = test))^2)),
sqrt(mean((test$dist - predict(mdl_quadratic, newdata = test))^2)),
sqrt(mean((test$dist - predict(mdl_poly, newdata = test))^2))
)
)| Model | MSE | RMSE |
|---|---|---|
| High Bias | 272.43 | 16.51 |
| Physics | 265.13 | 16.28 |
| High Variance | 164.24 | 12.82 |
# Plot training data
plot(
sample$speed,
sample$dist,
main = "Training",
xlab = "Speed (mph)",
ylab = "Stopping Distance (ft)",
col = "forestgreen",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
# Linear model
lines(
speed_grid,
pred_linear,
col = "red",
lwd = 2
)
# Quadratic model
lines(
speed_grid,
pred_quadratic,
col = "blue",
lwd = 2
)
# High-degree polynomial
lines(
speed_grid,
pred_poly,
col = "purple",
lwd = 2
)
# Legend
legend(
"topleft",
legend = c(
"Training Data",
"Linear Model",
"Quadratic Model",
"Degree-14 Polynomial"
),
col = c(
"forestgreen",
"red",
"blue",
"purple"
),
pch = c(19, NA, NA, NA),
lty = c(NA, 1, 1, 1),
lwd = c(NA, 2, 2, 2),
cex = 0.7
)par(mfrow = c(1, 3))
# Linear model
plot(
test$speed,
test$dist,
main = "Bias Model",
xlab = "Speed",
ylab = "Stopping Distance",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
lines(
test$speed[order(test$speed)],
predict(
lm_mdl,
newdata = test
)[order(test$speed)],
col = "red",
lwd = 2
)
# Quadratic model
plot(
test$speed,
test$dist,
main = "Physics Model",
xlab = "Speed",
ylab = "Stopping Distance",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
lines(
test$speed[order(test$speed)],
predict(
mdl_quadratic,
newdata = test
)[order(test$speed)],
col = "blue",
lwd = 2
)
# Degree-14 polynomial
plot(
test$speed,
test$dist,
main = "High Variance",
xlab = "Speed",
ylab = "Stopping Distance",
col = "black",
pch = 19,
xlim = x_limits,
ylim = y_limits
)
lines(
test$speed[order(test$speed)],
predict(
mdl_poly,
newdata = test
)[order(test$speed)],
col = "purple",
lwd = 2
)