Skip to content
Tutorials Center
My learning

Topics

Lesson 52 of 57 ยท Projects

Project: Building a Regression Model

Build a regression model end to end: explore, fit, check diagnostics, handle a log transform, validate on a held-out test set and report predictions with intervals.

In this project you will build a model to predict apartment rent from size, distance to the city center, building age and whether the building has an elevator. You will follow the workflow professionals use: explore, fit a first model, read the diagnostics, fix what they reveal, check the model on data it has not seen, and report a prediction with honest uncertainty.

You will need the ideas from Simple Linear Regression and Multiple Linear Regression. The data is simulated with a fixed seed: rents follow a multiplicative pattern, which is typical of money data and will make the first model look slightly wrong on purpose.

flowchart LR
  A["Split train / test"] --> B["Explore the training set"]
  B --> C["Fit a first model"]
  C --> D["Diagnostics: residuals, influence, collinearity"]
  D --> E["Improve: log transform, new terms"]
  E --> F["Evaluate once on the test set"]
  F --> G["Report with prediction intervals"]

Step 1: split first

To measure how well the model predicts new apartments, set aside a test set before doing anything else. Explore, fit and tune on the training set only; touch the test set once at the end. Peeking at the test set while choosing the model quietly overfits to it.

Python Create the data and split it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 2: first model and diagnostics

Fit a plain linear model of rent on all four predictors, then inspect the residuals. For skewed, positive, multiplicative outcomes a typical problem is heteroscedasticity: expensive apartments have larger absolute errors. We check this by comparing the spread of residuals for low and high fitted values, and the skewness of the residuals.

Python Fit the first model and read the diagnostics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The residual spread grows with the fitted value (a funnel) and the residuals are right-skewed. Both point to modeling log(rent) instead, which turns multiplicative effects into additive ones.

Step 3: transform and refit

With log(rent) as the outcome, a coefficient b means that a one-unit increase in the predictor multiplies rent by e^b, about a 100ยทb percent change for small b. That is a more natural way to talk about prices.

Python Log model: compare diagnostics

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Now the spread is stable, the residuals are symmetric and no observation is overly influential. (Also glance at VIF; here the predictors are independent by construction.)

Step 4: evaluate on the test set

Predict the held-out apartments and compute errors in rent units (convert back with exp). Report the RMSE (typical error, penalizing big misses) and MAE (average absolute error). Compare with a baseline that always predicts the training mean: a model must beat that. Also report the out-of-sample Rยฒ, which can be lower than the training Rยฒ.

Python Honest test-set performance

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The log model should do best on the test set, and both models beat the baseline by a wide margin.

Step 5: predictions with intervals

A single number is not enough. For a 70 mยฒ flat, 4 km from the center, 20 years old, with an elevator, report a prediction interval (range for one new apartment) rather than only the point estimate. For a log model, compute the interval on the log scale and exponentiate the endpoints.

Python Predict one new apartment with a 95% prediction interval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Reporting checklist

  • Say how the data was split and that the test set was used once.
  • Show the final equation or coefficient table with confidence intervals and the interpretation in plain units.
  • Report test-set RMSE or MAE next to a baseline.
  • List limits: the model is valid only within the observed ranges (do not predict a 300 mยฒ penthouse), and relationships are associations.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why keep a test set untouched until the end?

    If you tune the model on the test set, its performance there is optimistic. A truly held-out set estimates real-world accuracy.
  2. 2. Residuals fan out as fitted rent grows. What is a common fix for positive, multiplicative outcomes?

    A log transform stabilizes variance and converts multiplicative effects to additive ones.
  3. 3. In the log-rent model, a coefficient of 0.012 for size roughly means:

    On the log scale, a coefficient b multiplies the outcome by exp(b), about a 100*b percent change for small b.
  4. 4. Which interval should you report for the rent of one specific new apartment?

    A prediction interval includes the apartment-level noise and is wider than the interval for the average rent.

Summary

  • Split into training and test sets first; explore and tune only on the training set.
  • Diagnose residuals for curvature, funnels, skew and influential points, then adjust the model (log transform, new terms).
  • Evaluate on the held-out test set against a baseline using RMSE, MAE and out-of-sample Rยฒ.
  • Report predictions with prediction intervals, and state the ranges where the model applies.

Next: Practice Set: Descriptive Statistics and Probability