- Introduced by
- Legendre, A.-M. & Gauss, C.F.
- Year
- 1805–1809
- Type
- Supervised regression
- Family
- Machine learning
What does it do?
Linear regression fits a straight-line relationship between one or more input features and a continuous numeric outcome by minimising the sum of squared prediction errors.
As a machine-learning model, it is trained on labeled examples and evaluated on held-out data, which makes it the simplest supervised baseline for a regression task.
The intuition behind the method
Imagine drawing the best-fitting line through a scatter plot of data points. Linear regression does the same for many predictors at once: it finds the combination of feature weights that minimises the total squared distance between the model's predictions and the true values. Because the prediction is a weighted sum of inputs, each coefficient gives the expected change in the outcome for a one-unit increase in that feature, holding the other features constant. This makes the fitted model easy to explain and inspect, but it will miss curved relationships and interactions unless these are encoded by hand.
History
Least squares is generally credited to Adrien-Marie Legendre and Carl Friedrich Gauss. Linear regression is now used in two complementary traditions: as a classical statistical method for estimating associations and performing inference on coefficients, and as a supervised machine-learning model that is trained on labelled examples, evaluated on held-out data, and regularised to limit overfitting.
When to use it
Use linear regression when the outcome is continuous, the expected relationship between predictors and outcome is approximately linear, and interpretable coefficients are needed. It is a sensible starting baseline before trying more complex models, and a good final model when its assumptions hold. Do not use it for binary or categorical outcomes (use logistic regression instead), when strong nonlinear patterns or interactions dominate, or when the number of predictors exceeds the sample size without regularisation.
Limitations and considerations
- Assumes the outcome is linear in the parameters and in the features; curves, thresholds and interactions are missed unless they are encoded manually as additional terms.
- Sensitive to outliers because squared errors give extreme points disproportionate influence on the fitted coefficients.
- Multicollinearity among predictors inflates standard errors and destabilises coefficient estimates; perfect collinearity makes the estimates non-unique.
- Exact small-sample inference (t and F tests, confidence intervals) relies on approximately normal errors and constant error variance (homoscedasticity). The OLS coefficient estimates themselves do not require normality, but the reliability of their standard errors does depend on these conditions. When error variance is not constant, heteroskedasticity-robust (White) standard errors can be used in place of the classical ones.
- Standard inference rests on a consolidated set of assumptions: a correctly specified linear model, independent observations with uncorrelated errors, constant error variance, and, for small samples, approximately normal errors. Violating independence, as in time-ordered or clustered data, makes the reported standard errors and p-values unreliable even when predictive accuracy appears adequate.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Coefficients are directly interpretable: each value is an expected change in the outcome, in the outcome's units, per one-unit change in that feature with the others held fixed.
- The least-squares solution is closed-form for moderate-sized data, so fitting requires no iterative tuning in that regime.
- Provides standard errors and p-values, supporting formal hypothesis testing when the model's assumptions hold.
- Gives a reference point: if a more flexible model does not beat a fitted linear model on held-out data, the extra complexity is not capturing much additional structure.
- When the linearity assumption holds, the fitted coefficients are a direct and well-understood summary of the data.
- Ridge, Lasso and Elastic Net extend the same linear model by adding a penalty term to the least-squares objective, so the linear specification carries over to those variants when predictors are numerous.
Applications
- Predicting a continuous numeric outcome from tabular features in any domain.
- Establishing a performance baseline before testing gradient-boosting or neural-network models.
- Estimating conditional associations and adjusted effect sizes in observational studies when linearity holds. Coefficients describe associations after adjustment for the included predictors and do not establish causal effects unless confounding is addressed by study design or an explicit causal assumption.
- Demand forecasting, price modelling, and resource estimation in business analytics.
- Rapid exploratory modelling to identify the most important predictors before more complex analysis.
- Interpretable linear scoring rules where stakeholders need to see how each input contributes to a prediction.
How it works
How it works
The model expresses the prediction as a weighted sum of input features plus an intercept. The target variable must be continuous and numeric; categorical inputs must be encoded as dummy or numeric variables before fitting.
Formula
\hat{y} = \beta_0 + \beta_1 x_1 + \cdots + \beta_p x_pOrdinary least squares finds the coefficient vector that minimises the residual sum of squares. The closed-form expression requires that the columns of X are linearly independent (full column rank), so that X^T X is invertible; perfectly collinear predictors make the coefficients non-unique. For moderate-sized datasets the normal equations are solved directly. For very large datasets, iterative solvers or stochastic gradient descent are used instead.
Formula
\hat{\boldsymbol{\beta}} = (\mathbf{X}^\top \mathbf{X})^{-1} \mathbf{X}^\top \mathbf{y}R-squared (coefficient of determination) summarises how much of the variance in the outcome the model explains. It is complemented by root mean squared error and mean absolute error, which report errors in the original units of the outcome.
Formula
R^2 = 1 - \frac{\sum_i (y_i - \hat{y}_i)^2}{\sum_i (y_i - \bar{y})^2}Fit is evaluated on a hold-out test set or through k-fold cross-validation to detect overfitting. Residual plots, namely residuals versus fitted values and a Q-Q plot of residuals, are inspected to check linearity, constant error variance, and the approximate normality of errors before relying on standard errors, p-values, or confidence intervals. A formal test for heteroscedasticity, such as the White test or the Breusch–Pagan Lagrange-multiplier test, can complement the plots. If error variance is not constant, heteroskedasticity-robust (White) covariance estimates keep the standard errors asymptotically valid.
Common pitfalls
- Extrapolating predictions far outside the range of training data where the linear approximation breaks down.
- Ignoring residual diagnostics and assuming the model is valid without checking plots.
- Including highly correlated predictors without addressing multicollinearity (check VIF).
- Confusing R-squared with predictive accuracy on new data — always evaluate on a held-out set.
- Applying linear regression to a binary or count outcome instead of logistic or Poisson regression.
Frequently asked questions
How is linear regression different from logistic regression?
Linear regression predicts a continuous outcome directly. Logistic regression models the log-odds of a binary outcome and outputs probabilities bounded between 0 and 1. Using linear regression for a binary outcome can produce predictions outside [0, 1] and violates the constant-variance assumption.
Do I need to standardise my features?
Standardisation is not required for predictions, but it makes coefficients directly comparable when features have very different scales. It is especially recommended when using regularised variants such as Ridge or Lasso.
How do I handle many predictors relative to sample size?
Switch to regularised linear regression (Ridge for shrinkage, Lasso for sparsity, Elastic Net for both). OLS becomes unreliable or undefined when the number of predictors approaches or exceeds the number of observations.
What residual plots should I inspect?
At minimum: (1) residuals vs. fitted values to check for patterns suggesting nonlinearity or heteroscedasticity, and (2) a Q-Q plot of residuals to assess normality. A scale-location plot and leverage/influence statistics (Cook's distance) are also informative.
Is a high R-squared enough to declare a good model?
No. R-squared measures in-sample fit and can be inflated by irrelevant predictors. Always report cross-validated or test-set RMSE alongside R-squared, and inspect residual diagnostics to confirm the model's assumptions are satisfied.
Related methods
Sources
Hastie, T., Tibshirani, R. & Friedman, J. (2009). The Elements of Statistical Learning: Data Mining, Inference, and Prediction (2nd ed., Ch. 3). Springer.
ISBN: 978-0-387-84858-7James, G., Witten, D., Hastie, T. & Tibshirani, R. (2013). An Introduction to Statistical Learning (Ch. 3). Springer.
ISBN: 978-1-4614-7138-72017_Book_StatisticsAndAnalysisOfScienti, p. 162
Econometrics in Practice (2021), p. 339–340
Applied Choice Analysis, 2nd Edition, p. 1022