Interactive R programming · Machine learning

Polynomial Regression in R, the game

When a straight line isn’t enough, bend it. Turn the degree dial on real car data, watch overfitting happen live, and fit it in R.

~12 minIntermediateR · lm()5 missions285 XP
Mission 1+25 XP✓ Complete

When a line isn’t enough

Linear regression fits a straight line, so it does poorly on curved data. Polynomial regression adds powers of the feature (x², x³…) so the model can bend:

y = β₀ + β₁x + β₂x² + β₃x³ + … + βₙxⁿ + εn is the degree of the polynomial

It is still called linear regression because the model is linear in its coefficients; only the features are curved.

👀Spot the curve
+25 XP

Which dataset needs polynomial regression?

Mission 2+90 XP✓ Complete

Turn the degree dial

A higher degree makes the curve more flexible. That lowers training error every time, but the error on new data tells the real story.

Below: real mtcars data, mpg against horsepower. Black cars are used for training and hollow ones are held out as a test set.

🎛️Degree dial
+90 XP
–Training RMSE (mpg)
–Test RMSE (mpg)
training carstest carsTest error per degree you have tried:
  • ✓After exploring, go back to degree 1: a straight line underfits+20 XP
  • ✓Find the degree with the lowest test error+40 XP
  • ✓Push to degree 8 and watch the curve go wild+30 XP
Mission 3+30 XP✓ Complete

Code it in R

The article uses R’s built-in pressure dataset (vapour pressure of mercury against temperature). This version fixes three issues in the original: it uses all 19 rows (not just head()), trains on the training set only, and predicts on the test set.

Step 1Load and split

library(caTools)
library(Metrics)
library(ggplot2)

data <- pressure                       # all 19 rows
set.seed(123)
split    <- sample.split(data$pressure, SplitRatio = 2/3)
training <- subset(data, split == TRUE)
testing  <- subset(data, split == FALSE)

Step 2Fit the model

# Degree-4 polynomial in temperature
poly_reg <- lm(pressure ~ poly(temperature, 4, raw = TRUE), data = training)
summary(poly_reg)

poly(x, 4, raw = TRUE) adds x, x², x³ and x⁴, the same as the I(x^2) columns in the original article.

Step 3Evaluate on the test set and plot

pred <- predict(poly_reg, newdata = testing)
rmse(testing$pressure, pred)
mae(testing$pressure, pred)

grid <- data.frame(temperature = seq(0, 360, 1))
ggplot() +
  geom_point(aes(temperature, pressure), data = data, colour = "red") +
  geom_line(aes(grid$temperature, predict(poly_reg, newdata = grid)), colour = "blue") +
  ggtitle("Polynomial regression (degree 4)")
🧠Quick check
+30 XP

Why do we call predict(poly_reg, newdata = testing) instead of predict(poly_reg)?

Mission 4+50 XP✓ Complete

Strength or weakness?

Sort each statement about polynomial regression.

⚡Sort the cards
1 / 5
correct on the first try
Final boss+90 XP✓ Defeated

Defeat the Curve Boss 🐲

Six questions, three hearts.

Question 1 / 6
❤️❤️❤️

💥 The boss winsReview the missions above, then try again.

You’re a Curve Master!

Share on LinkedIn
What’s next

Keep levelling up

You learned when polynomial regression beats a straight line, how the degree controls the bias–variance trade-off, how to spot overfitting with a test set, and how to fit it in R.

Lv 1 Rookie0 / 285 XP