Linear Regression: Foundations, Estimation, and Diagnostics
Linear regression is a fundamental method for modeling the relationship between a continuous response variable and one or more explanatory variables. It is widely used for prediction, estimation, hypothesis testing, and the analysis of relationships between variables.
Despite its apparent simplicity, linear regression introduces several ideas that recur throughout statistics and machine learning: objective functions, probabilistic models, parameter estimation, regularization, uncertainty quantification, and model diagnostics.
This article develops the model formally, derives the ordinary least squares and maximum likelihood estimators, and explains the assumptions required to interpret the result correctly.
The linear model
Suppose a dataset contains observations and explanatory variables. For observation , let:
- denote the response;
- denote the feature vector, including a leading for the intercept;
- denote the unknown coefficient vector;
- denote the unobserved error.
The linear regression model is
For a single predictor , this becomes
where is the intercept and is the slope.
Stacking all observations produces the matrix form
with
The model is linear in its parameters. The predictors themselves need not appear only as raw values. Polynomial terms, interactions, logarithms, and other transformations may be included as columns of while the model remains a linear regression model. For example,
is linear in , , and , even though it is nonlinear in .
Ordinary least squares
For a candidate coefficient vector , the fitted values and residuals are
Ordinary least squares (OLS) selects the coefficients that minimize the residual sum of squares:
Equivalently, the objective is
Expanding the quadratic expression gives
Differentiating with respect to yields
Setting the gradient to zero produces the normal equations:
If has full column rank, then is invertible and the solution is unique:
This closed-form expression is valuable for analysis. In numerical software, however, explicitly computing the inverse is generally discouraged because it is less stable and less efficient than solving the least-squares system with QR decomposition or singular value decomposition (SVD).
Geometric interpretation
The vector of fitted values is
where
is the hat matrix.
OLS projects onto the column space of . At the optimum, the residual vector is orthogonal to every column of the design matrix:
This orthogonality condition is another form of the normal equations. When the model includes an intercept, it also implies that the residuals sum to zero.
Maximum likelihood estimation
OLS defines an optimization criterion without requiring a complete probability distribution for the errors. Maximum likelihood estimation (MLE), by contrast, begins with a probabilistic model.
Assume that the errors are independent and normally distributed with constant variance:
It follows that
and, in matrix form,
The likelihood is
Taking the logarithm gives
For fixed , the first two terms do not depend on . Maximizing the log-likelihood is therefore equivalent to minimizing the residual sum of squares:
Thus, OLS and MLE produce the same coefficient estimate under independent Gaussian errors with constant variance. They arrive there from different starting points: OLS minimizes a geometric loss, whereas MLE maximizes the probability assigned to the observed data.
Estimating the error variance
Substituting into the likelihood and optimizing with respect to gives
This is the maximum likelihood estimator, but it is biased downward because the same data were used to estimate the regression coefficients. Under the classical model, the commonly used unbiased estimator is
The denominator represents the residual degrees of freedom when the model contains predictors and one intercept.
Assumptions and their consequences
It is useful to distinguish assumptions needed to estimate coefficients from assumptions needed for optimality and statistical inference.
Linearity of the conditional mean
The model assumes
This does not require every observed point to lie near a straight line. It requires the expected response, conditional on the selected features and transformations, to have the specified linear form.
Exogeneity
The error must have conditional mean zero:
This assumption fails when relevant variables are omitted and correlated with included predictors, when predictors are measured with error, or when the response influences a predictor. Under such conditions, an estimated coefficient should not be interpreted causally without an appropriate research design.
Full column rank
No predictor column may be an exact linear combination of the others. Exact multicollinearity prevents a unique OLS solution. Strong but imperfect multicollinearity does not prevent estimation, but it increases coefficient uncertainty and makes individual estimates sensitive to small changes in the data.
Homoscedasticity and uncorrelated errors
The classical model assumes
Under linearity, exogeneity, full rank, and this covariance assumption, the Gauss–Markov theorem states that OLS is the best linear unbiased estimator: among linear unbiased estimators, it has the smallest variance.
Heteroscedasticity or correlated errors do not automatically bias the OLS coefficients when exogeneity still holds. They do, however, invalidate the usual standard errors and reduce efficiency. Heteroscedasticity-consistent or cluster-robust covariance estimators may be required.
Normality
Normally distributed errors are not required to compute OLS estimates or for the Gauss–Markov result. Normality provides the exact finite-sample distributions used for classical tests, tests, and confidence intervals. In sufficiently large samples, asymptotic inference may remain useful without exact normality, subject to the other assumptions and appropriate standard errors.
Interpreting coefficients
In a multiple regression model,
is the expected change in the response associated with a one-unit increase in , holding the other included predictors constant.
This interpretation requires care:
- The unit of measurement determines the numerical scale of a coefficient.
- A coefficient describes association unless the study design supports a causal claim.
- Interaction terms make a predictor’s effect depend on another predictor.
- Polynomial terms make the marginal effect vary with the predictor value.
- Extrapolation beyond the observed feature range can be unreliable even when the fitted line appears reasonable.
For example, if
then, within the model and observed data range, one additional unit of area is associated with an expected price increase of , holding age constant. The coefficient alone does not establish that increasing area would cause exactly that change in every case.
Statistical uncertainty
Under the classical homoscedastic model,
Replacing with yields estimated standard errors. These quantify sampling uncertainty in the coefficients and form the basis of confidence intervals and hypothesis tests.
A confidence interval for a mean response is narrower than a prediction interval for a new observation. The latter must account for both uncertainty in the estimated mean and the irreducible observation error.
Statistical significance should not be treated as practical significance. A small effect can have a very small -value in a large dataset, while an operationally important effect may remain uncertain in a small dataset. Coefficient magnitude, uncertainty, units, and domain context should be reported together.
Model evaluation
For observed responses , fitted values , and mean response , common metrics include:
Mean squared error
MSE strongly penalizes large errors and uses squared response units.
Root mean squared error
RMSE is expressed in the same units as the response, which often makes it easier to interpret.
Coefficient of determination
measures the proportion of sample variation explained relative to an intercept-only model. It does not measure causal validity, guarantee good out-of-sample prediction, or confirm that the model assumptions hold. Adding predictors cannot decrease the training , so adjusted or out-of-sample evaluation is more informative when comparing models of different sizes.
Predictive performance should be measured on validation or test data that were not used to estimate the coefficients. For time-dependent or grouped observations, the data split must preserve the relevant dependence structure.
Residual diagnostics
Residual analysis tests whether the fitted model is compatible with its assumptions.
- Residuals versus fitted values: curvature suggests a missing nonlinear term; a funnel shape suggests non-constant variance.
- Quantile–quantile plot: substantial deviations from a straight reference line indicate non-normal tails or outliers.
- Residuals versus time or sequence: systematic patterns suggest autocorrelation.
- Leverage and influence: observations with unusual predictor values can have a disproportionate effect on the fitted coefficients.
- Variance inflation factors: large values can reveal unstable estimation caused by multicollinearity.
Diagnostics should inform model revision, not serve as a mechanical pass-or-fail checklist. A visible pattern may indicate a missing transformation, interaction, group effect, nonlinear model, or different error structure.
Regularized linear regression
When the feature set is large or predictors are strongly correlated, regularization can improve out-of-sample performance.
Ridge regression
Ridge regression adds an penalty:
It shrinks coefficients toward zero and stabilizes estimates under multicollinearity.
Lasso regression
Lasso uses an penalty:
The penalty can set some coefficients exactly to zero, producing a sparse model. The regularization strength should be selected using validation data or cross-validation. Predictors are usually standardized before penalization, and the intercept is normally excluded from the penalty.
A numerically stable implementation
The analytical formula contains , but production code should solve the least-squares problem directly:
import numpy as np
def fit_linear_regression(features, targets):
"""Return OLS coefficients, including an intercept."""
x = np.asarray(features, dtype=float)
y = np.asarray(targets, dtype=float)
design = np.column_stack([np.ones(x.shape[0]), x])
coefficients, _, rank, singular_values = np.linalg.lstsq(
design,
y,
rcond=None,
)
if rank < design.shape[1]:
raise ValueError("The design matrix is rank deficient.")
return coefficients, singular_values
numpy.linalg.lstsq uses a numerically appropriate factorization and also exposes information about rank and singular values. A complete analysis should additionally retain residuals, estimate uncertainty, examine diagnostics, and evaluate predictions on unseen data.
Conclusion
Linear regression combines a clear mathematical structure with broad practical utility:
- OLS estimates coefficients by minimizing the sum of squared residuals.
- Under independent Gaussian errors with constant variance, the OLS coefficient estimate is also the maximum likelihood estimate.
- Normality is required for exact classical inference, not for computing the OLS solution.
- Coefficients describe conditional associations and require additional assumptions for causal interpretation.
- Residual diagnostics and out-of-sample evaluation are essential; a high training is not sufficient evidence of a reliable model.
- QR- or SVD-based least-squares solvers are preferable to explicitly inverting .
Linear regression is therefore more than a formula for fitting a line. It is a compact framework for understanding estimation, uncertainty, assumptions, and the distinction between fitting observed data and building a model that generalizes.
