Skip to content

Linear Regression Explained: Assumptions, Residuals and Pitfalls

A straight line fitted through scatter points with a residual plot scattered around zero below it.

Linear regression is the first model most people learn, and one of the most useful. It predicts a number as a weighted sum of inputs, and because every weight has a plain meaning, it doubles as a way to understand your data. It also has assumptions that are easy to ignore and expensive to break.

What the model is

Linear regression predicts a target as an intercept plus a weighted sum of features. With one feature, that is a straight line. With several, it is a flat surface in higher dimensions. Each weight tells you how much the prediction changes for a one-unit increase in that feature, with the other features held fixed.

How it is fitted

Ordinary least squares chooses the weights that minimize the sum of the squared differences between predictions and actual values. For this model there is a direct mathematical solution, and the same answer can be found by stepping downhill with gradient descent. The squared errors mean that large misses weigh heavily, which makes the fit sensitive to outliers.

A short example

from sklearn.linear_model import LinearRegression

model = LinearRegression().fit(X_train, y_train)

print(model.intercept_)
print(dict(zip(X_train.columns, model.coef_)))
predictions = model.predict(X_test)

Here X_train is a DataFrame of numeric features and y_train the target. scikit-learn gives you predictions and coefficients. If you want p-values and confidence intervals for the coefficients, the statsmodels library provides a fuller statistical summary.

The assumptions that matter

  • Linearity. The relationship between each feature and the target is roughly straight. If it curves, the model underfits unless you add transformed features.
  • Independent errors. Each row’s error should not depend on another’s. Time series and grouped data often break this.
  • Constant variance. The size of the errors should not grow or shrink as predictions change. A fan shape in the residual plot signals a problem.
  • Roughly normal errors. This matters for p-values and confidence intervals more than for the fit itself.
  • No severe multicollinearity. Strongly correlated features make individual coefficients unstable, as explained in multicollinearity in regression.

Check the residuals

The residuals are the differences between actual and predicted values. Plot them against the predictions. A healthy plot looks like a shapeless cloud around zero. A curve means you missed a nonlinear pattern, a fan means unequal variance, and a few far-out points deserve a look, as discussed in outlier detection methods.

Common pitfalls

  • Extrapolating. A fitted line can predict anywhere, but the pattern may not hold outside the range of your data.
  • Reading coefficients as causes. A coefficient describes association within your data, not what would happen if you changed the feature, a limit covered in correlation vs causation.
  • Comparing coefficients on different scales. A larger number does not mean a bigger effect unless the features share a scale.
  • Trusting a high R-squared. It says how much variation the model explains, not whether the model is right. See regression metrics for better ways to judge it.

Extending the model

When you have many features or limited data, adding a penalty on the weights gives ridge or lasso regression, described in regularization in machine learning. When the target is a category instead of a number, the closest relative is logistic regression.

A quick checklist

  1. Does a plot of each feature against the target look roughly straight?
  2. Do the residuals look like a shapeless cloud around zero?
  3. Did you check for strongly correlated features and extreme points?
  4. Are you scoring on held-out data, not training data?
  5. Are you describing coefficients as associations, not causes?

FAQ

What is linear regression used for?
It predicts a numeric outcome from one or more inputs and helps explain how each input relates to the outcome, such as how price changes with size.

What are the assumptions of linear regression?
The main ones are a roughly linear relationship, independent errors, constant error variance, roughly normal errors for inference, and no severe multicollinearity.

Does linear regression need feature scaling?
Not to fit the model. Scaling helps when you use regularization or want to compare coefficient sizes, and it helps gradient-based fitting converge.

Related posts

Leave a comment

Your email address will not be published. Required fields are marked *