
| Part of a series on |
| Regression analysis |
|---|
| Models |
| Estimation |
| Background |
Instatistics,ordinary least squares (OLS) is a type oflinear least squares method for choosing the unknownparameters in alinear regression model (with fixed level-one[clarification needed] effects of alinear function of a set ofexplanatory variables) by the principle ofleast squares: minimizing the sum of the squares of the differences between the observeddependent variable (values of the variable being observed) in the inputdataset and the output of the (linear) function of theindependent variable. Some sources consider OLS to be linear regression.[1]
Geometrically, this is seen as the sum of the squared distances, parallel to the axis of the dependent variable, between each data point in the set and the corresponding point on the regression surface—the smaller the differences, the better the model fits the data. The resultingestimator can be expressed by a simple formula, especially in the case of asimple linear regression, in which there is a singleregressor on the right side of the regression equation.
The OLS estimator isconsistent for the level-one fixed effects when the regressors areexogenous and forms perfectcolinearity (rank condition), consistent for the variance estimate of the residuals when regressors have finite fourth moments[2] and—by theGauss–Markov theorem—optimal in the class of linear unbiased estimators when theerrors arehomoscedastic andserially uncorrelated. Under these conditions, the method of OLS providesminimum-variance mean-unbiased estimation when the errors have finitevariances. Under the additional assumption that the errors arenormally distributed with zero mean, OLS is themaximum likelihood estimator that outperforms any non-linear unbiased estimator.
Suppose the data consists ofobservations. Each observation includes a scalar response and a column vector of parameters (regressors), i.e.,. In alinear regression model, the response variable,, is a linear function of the regressors:
or invector form,
where, as introduced previously, is a column vector of the-th observation of all the explanatory variables; is a vector of unknown parameters; and the scalar represents unobserved random variables (errors) of the-th observation. accounts for the influences upon the responses from sources other than the explanatory variables. This model can also be written in matrix notation as
where and are vectors of the response variables and the errors of the observations, and is an matrix of regressors, also sometimes called thedesign matrix, whose row is and contains the-th observations on all the explanatory variables.
Typically, a constant term is included in the set of regressors, say, by taking for all. The coefficient corresponding to this regressor is called theintercept. Without the intercept, the fitted line is forced to cross the origin when.
Regressors do not have to be independent for estimation to be consistent e.g. they may be non-linearly dependent. Short of perfect multicollinearity, parameter estimates may still be consistent; however, as multicollinearity rises the standard error around such estimates increases and reduces the precision of such estimates. When there is perfect multicollinearity, it is no longer possible to obtain unique estimates for the coefficients to the related regressors; estimation for these parameters cannot converge (thus, it cannot be consistent).
As a concrete example where regressors are non-linearly dependent yet estimation may still be consistent, we might suspect the response depends linearly both on a value and its square; in which case we would include one regressor whose value is just the square of another regressor. In that case, the model would bequadratic in the second regressor, but none-the-less is still considered alinear model because the modelis still linear in the parameters ().
Consider anoverdetermined system
oflinear equations in unknowncoefficients,, with. This can be written inmatrix form as
where
(Note: for a linear model as above, not all elements in contains information on the data points. The first column is populated with ones,. Only the other columns contain actual data. So here is equal to the number of regressors plus one).
Such a system usually has no exact solution, so the goal is instead to find the coefficients which fit the equations "best", in the sense of solving thequadraticminimization problem
where the objective function is given by
A justification for choosing this criterion is given inProperties below. This minimization problem has a unique solution, provided that the columns of the matrix arelinearly independent, given by solving the so-callednormal equations:
The matrix is known as thenormal matrix orGram matrix and the matrix is known as themoment matrix of regressand by regressors.[3] Finally, is the coefficient vector of the least-squareshyperplane, expressed as
or
Supposeb is a "candidate" value for the parameter vectorβ. The quantityyi −xiTb, called theresidual for thei-th observation, measures the vertical distance between the data point(xi,yi) and the hyperplaney =xTb, and thus assesses the degree of fit between the actual data and the model. Thesum of squared residuals (SSR) (also called theerror sum of squares (ESS) orresidual sum of squares (RSS))[4] is a measure of the overall model fit:
whereT denotes the matrixtranspose, and the rows ofX, denoting the values of all the independent variables associated with a particular value of the dependent variable, areXi = xiT. The value ofb which minimizes this sum is called theOLS estimator forβ. The functionS(b) is quadratic inb with positive-definiteHessian, and therefore this function possesses a unique global minimum at, which can be given by the explicit formula[5][proof]
The productN =XTX is aGram matrix, and its inverse,Q =N−1, is thecofactor matrix ofβ,[6][7][8] closely related to itscovariance matrix,Cβ.The matrix (XT X)−1 XT =Q XT is called theMoore–Penrose pseudoinverse matrix ofX. This formulation highlights the point that estimation can be carried out if, and only if, there is no perfectmulticollinearity between the explanatory variables (which would cause the Gram matrix to have no inverse).
After we have estimatedβ, thefitted values (orpredicted values) from the regression will be
whereP =X(XTX)−1XT is theprojection matrix onto the spaceV spanned by the columns ofX. This matrixP is also sometimes called thehat matrix because it "puts a hat" onto the variabley. Another matrix, closely related toP is theannihilator matrixM =In −P; this is a projection matrix onto the space orthogonal toV. Both matricesP andM aresymmetric andidempotent (meaning thatP2 =P andM2 =M), and relate to the data matrixX via identitiesPX =X andMX = 0.[9] MatrixM creates theresiduals from the regression:
The variances of the predicted values are found in the main diagonal of thevariance-covariance matrix of predicted values:
whereP is the projection matrix ands2 is the sample variance.[10]The full matrix is very large; its diagonal elements can be calculated individually as:
whereXi is thei-th row of matrixX.
Using these residuals we can estimate the sample variances2 using thereduced chi-squared statistic:
The denominator,n−p, is thestatistical degrees of freedom. The first quantity,s2, is the OLS estimate forσ2, whereas the second,, is the MLE estimate forσ2. The two estimators are quite similar in large samples; the first estimator is alwaysunbiased, while the second estimator is biased but has a smallermean squared error. In practices2 is used more often, since it is more convenient for the hypothesis testing. The square root ofs2 is called theregression standard error,[11]standard error of the regression,[12][13] orstandard error of the equation.[9]
It is common to assess the goodness-of-fit of the OLS regression by comparing how much the initial variation in the sample can be reduced by regressing ontoX. Thecoefficient of determinationR2 is defined as a ratio of "explained" variance to the "total" variance of the dependent variabley, in the cases where the regression sum of squares equals the sum of squares of residuals:[14]
where TSS is thetotal sum of squares for the dependent variable,, and is ann×n matrix of ones. ( is acentering matrix which is equivalent to regression on a constant; it simply subtracts the mean from a variable.) In order forR2 to be meaningful, the matrixX of data on regressors must contain a column vector of ones to represent the constant whose coefficient is the regression intercept. In that case,R2 will always be a number between 0 and 1, with values close to 1 indicating a good degree of fit.
If the data matrixX contains only two variables, a constant and a scalar regressorxi, then this is called the "simple regression model". This case is often considered in the beginner statistics classes, as it provides much simpler formulas even suitable for manual calculation. The parameters are commonly denoted as(α,β):
The least squares estimates in this case are given by simple formulas
In the previous section the least squares estimator was obtained as a value that minimizes the sum of squared residuals of the model. However it is also possible to derive the same estimator from other approaches. In all cases the formula for OLS estimator remains the same:^β = (XTX)−1XTy; the only difference is in how we interpret this result.

This sectionmay need to be cleaned up. It has beenmerged fromLinear least squares. |
For mathematicians, OLS is an approximate solution to an overdetermined system of linear equationsXβ ≈y, whereβ is the unknown. Assuming the system cannot be solved exactly (the number of equationsn is much larger than the number of unknownsp), we are looking for a solution that could provide the smallest discrepancy between the right- and left- hand sides. In other words, we are looking for the solution that satisfies
where‖·‖ is the standardL2 norm in then-dimensionalEuclidean spaceRn. The predicted quantityXβ is just a certain linear combination of the vectors of regressors. Thus, the residual vectory −Xβ will have the smallest length wheny isprojected orthogonally onto thelinear subspacespanned by the columns ofX. The OLS estimator in this case can be interpreted as the coefficients ofvector decomposition of^y =Py along the basis ofX.
In other words, the gradient equations at the minimum can be written as:
A geometrical interpretation of these equations is that the vector of residuals, is orthogonal to thecolumn space ofX, since thedot product is equal to zero forany conformal vector,v. This means that is the shortest of all possible vectors, that is, the variance of the residuals is the minimum possible. This is illustrated at the right.
Introducing and a matrixK with the assumption that a matrix is non-singular andKTX = 0 (cf.Orthogonal projections), the residual vector should satisfy the following equation:
The equation and solution of linear least squares are thus described as follows:
Another way of looking at it is to consider the regression line to be a weighted average of the lines passing through the combination of any two points in the dataset.[15] Although this way of calculation is more computationally expensive, it provides a better intuition on OLS.
The OLS estimator is identical to themaximum likelihood estimator (MLE) under the normality assumption for the error terms.[16][proof] This normality assumption has historical importance, as it provided the basis for the early work in linear regression analysis byYule andPearson.[citation needed] From the properties of MLE, we can infer that the OLS estimator is asymptotically efficient (in the sense of attaining theCramér–Rao bound for variance) if the normality assumption is satisfied.[17]
Iniid case the OLS estimator can also be viewed as aGMM estimator arising from the moment conditions
These moment conditions state that the regressors should be uncorrelated with the errors. Sincexi is ap-vector, the number of moment conditions is equal to the dimension of the parameter vectorβ, and thus the system is exactly identified. This is the so-called classical GMM case, when the estimator does not depend on the choice of the weighting matrix.
Note that the original strict exogeneity assumptionE[εi | xi] = 0 implies a far richer set of moment conditions than stated above. In particular, this assumption implies that for any vector-functionƒ, the moment conditionE[ƒ(xi)·εi] = 0 will hold. However it can be shown using theGauss–Markov theorem that the optimal choice of functionƒ is to takeƒ(x) =x, which results in the moment equation posted above.
There are several different frameworks in which thelinear regression model can be cast in order to make the OLS technique applicable. Each of these settings produces the same formulas and same results. The only difference is the interpretation and the assumptions which have to be imposed in order for the method to give meaningful results. The choice of the applicable framework depends mostly on the nature of data in hand, and on the inference task which has to be performed.
One of the lines of difference in interpretation is whether to treat the regressors as random variables, or as predefined constants. In the first case (random design) the regressorsxi are random and sampled together with theyi's from somepopulation, as in anobservational study. This approach allows for more natural study of theasymptotic properties of the estimators. In the other interpretation (fixed design), the regressorsX are treated as known constants set by adesign, andy is sampled conditionally on the values ofX as in anexperiment. For practical purposes, this distinction is often unimportant, since estimation and inference is carried out while conditioning onX. All results stated in this article are within the random design framework.
The classical model focuses on the "finite sample" estimation and inference, meaning that the number of observationsn is fixed. This contrasts with the other approaches, which study theasymptotic behavior of OLS, and in which the behavior at a large number of samples is studied.To prove finite sample unbiasedness of the OLS estimator, we require the following assumptions.



Violations of these assumptions can result in biased estimations ofβ, biased standard errors, untrustworthy confidence intervals and significance tests. Beyond these assumptions, several other statistical properties of the data strongly influence the performance of different estimation methods:
First of all, under thestrict exogeneity assumption the OLS estimators ands2 areunbiased, meaning that their expected values coincide with the true values of the parameters:[23][proof]
If the strict exogeneity does not hold (as is the case with manytime series models, where exogeneity is assumed only with respect to the past shocks but not the future ones), then these estimators will be biased in finite samples.
Thevariance-covariance matrix (or simplycovariance matrix) of is equal to[24]
In particular, the standard error of each coefficient is equal to square root of thej-th diagonal element of this matrix. The estimate of this standard error is obtained by replacing the unknown quantityσ2 with its estimates2. Thus,
It can also be easily shown that the estimator is uncorrelated with the residuals from the model:[24]
TheGauss–Markov theorem states that under thespherical errors assumption (that is, the errors should beuncorrelated andhomoscedastic) the estimator is efficient in the class of linear unbiased estimators. This is called thebest linear unbiased estimator (BLUE). Efficiency should be understood as if we were to find some other estimator which would be linear iny and unbiased, then[24]
in the sense that this is anonnegative-definite matrix. This theorem establishes optimality only in the class of linear unbiased estimators, which is quite restrictive. Depending on the distribution of the error termsε, other, non-linear estimators may provide better results than OLS.
The properties listed so far are all valid regardless of the underlying distribution of the error terms. However, if you are willing to assume that thenormality assumption holds (that is, thatε ~N(0,σ2In)), then additional properties of the OLS estimators can be stated.
The estimator is normally distributed, with mean and variance as given before:[25]
This estimator reaches theCramér–Rao bound for the model, and thus is optimal in the class of all unbiased estimators.[17] Note that unlike theGauss–Markov theorem, this result establishes optimality among both linear and non-linear estimators, but only in the case of normally distributed error terms.
The estimators2 will be proportional to thechi-squared distribution:[26]
The variance of this estimator is equal to2σ4/(n − p), which does not attain theCramér–Rao bound of2σ4/n. However it was shown that there are no unbiased estimators ofσ2 with variance smaller than that of the estimators2.[27] If we are willing to allow biased estimators, and consider the class of estimators that are proportional to the sum of squared residuals (SSR) of the model, then the best (in the sense of themean squared error) estimator in this class will be~σ2 = SSR / (n − p + 2), which even beats the Cramér–Rao bound in case when there is only one regressor (p = 1).[28]
Moreover, the estimators ands2 areindependent,[29] the fact which comes in useful when constructing the t- and F-tests for the regression.
As was mentioned before, the estimator is linear iny, meaning that it represents a linear combination of the dependent variablesyi. The weights in this linear combination are functions of the regressorsX, and generally are unequal. The observations with high weights are calledinfluential because they have a more pronounced effect on the value of the estimator.
To analyze which observations are influential we remove a specificj-th observation and consider how much the estimated quantities are going to change (similarly to thejackknife method). It can be shown that the change in the OLS estimator forβ will be equal to[30]
wherehj =xjT (XTX)−1xj is thej-th diagonal element of the hat matrixP, andxj is the vector of regressors corresponding to thej-th observation. Similarly, the change in the predicted value forj-th observation resulting from omitting that observation from the dataset will be equal to[30]
From the properties of the hat matrix,0 ≤hj ≤ 1, and they sum up top, so that on averagehj ≈p/n. These quantitieshj are called theleverages, and observations with highhj are calledleverage points.[31] Usually the observations with high leverage ought to be scrutinized more carefully, in case they are erroneous, or outliers, or in some other way atypical of the rest of the dataset.
Sometimes the variables and corresponding parameters in the regression can be logically split into two groups, so that the regression takes form
whereX1 andX2 have dimensionsn×p1,n×p2, andβ1,β2 arep1×1 andp2×1 vectors, withp1 +p2 =p.
TheFrisch–Waugh–Lovell theorem states that in this regression the residuals and the OLS estimate will be numerically identical to the residuals and the OLS estimate forβ2 in the following regression:[32]
whereM1 is theannihilator matrix for regressorsX1.
The theorem can be used to establish a number of theoretical results. For example, having a regression with a constant and another regressor is equivalent to subtracting the means from the dependent variable and the regressor and then running the regression for the de-meaned variables but without the constant term.
The least squares estimators arepoint estimates of the linear regression model parametersβ. However, generally we also want to know how close those estimates might be to the true values of parameters. In other words, we want to construct theinterval estimates.
Since we have not made any assumption about the distribution of error termεi, it is impossible to infer the distribution of the estimators and. Nevertheless, we can apply thecentral limit theorem to derive theirasymptotic properties as sample sizen goes to infinity. While the sample size is necessarily finite, it is customary to assume thatn is "large enough" so that the true distribution of the OLS estimator is close to its asymptotic limit.
We can show that under the model assumptions, the least squares estimator forβ isconsistent (that isconverges in probability toβ) and asymptotically normal:[proof]
where
Using this asymptotic distribution, approximate two-sided confidence intervals for thej-th component of the vector can be constructed as
whereq denotes thequantile function of standard normal distribution, and [·]jj is thej-th diagonal element of a matrix.
Similarly, the least squares estimator forσ2 is also consistent and asymptotically normal (provided that the fourth moment ofεi exists) with limiting distribution
These asymptotic distributions can be used for prediction, testing hypotheses, constructing other estimators, etc.. As an example consider the problem of prediction. Suppose is some point within the domain of distribution of the regressors, and one wants to know what the response variable would have been at that point. Themean response is the quantity, whereas thepredicted response is. Clearly the predicted response is a random variable, its distribution can be derived from that of:
which allows construct confidence intervals for mean response to be constructed:
This sectionneeds expansion. You can help byadding missing information.(February 2017) |
Two hypothesis tests are particularly widely used. First, one wants to know if the estimated regression equation is any better than simply predicting that all values of the response variable equal its sample mean (if not, it is said to have no explanatory power). Thenull hypothesis of no explanatory value of the estimated regression is tested using anF-test. If the calculated F-value is found to be large enough to exceed its critical value for the pre-chosen level of significance, the null hypothesis is rejected and thealternative hypothesis, that the regression has explanatory power, is accepted. Otherwise, the null hypothesis of no explanatory power is accepted.
Second, for each explanatory variable of interest, one wants to know whether its estimated coefficient differs significantly from zero—that is, whether this particular explanatory variable in fact has explanatory power in predicting the response variable. Here the null hypothesis is that the true coefficient is zero. This hypothesis is tested by computing the coefficient'st-statistic, as the ratio of the coefficient estimate to itsstandard error. If the t-statistic is larger than a predetermined value, the null hypothesis is rejected and the variable is found to have explanatory power, with its coefficient significantly different from zero. Otherwise, the null hypothesis of a zero value of the true coefficient is accepted.
In addition, theChow test is used to test whether two subsamples both have the same underlying true coefficient values. The sum of squared residuals of regressions on each of the subsets and on the combined data set are compared by computing an F-statistic; if this exceeds a critical value, the null hypothesis of no difference between the two subsets is rejected; otherwise, it is accepted.
In atime series model, we require thestochastic process {xi,yi} to bestationary andergodic; if {xi,yi} is nonstationary, OLS results are often biased unless {xi,yi} isco-integrating.[33]
We still require the regressors to bestrictly exogenous: E[xiεi] = 0 for alli = 1, ...,n. If they are onlypredetermined, OLS is biased in finite sample;
Finally, the assumptions on the variance take the form of requiring that {xiεi} is amartingale difference sequence, with a finite matrix of second momentsQxxε² = E[ εi2xi xiT ].
Suppose it is known that the coefficients in the regression satisfy a system of linear equations
whereQ is ap×q matrix of full rank, andc is aq×1 vector of known constants, whereq < p. In this case least squares estimation is equivalent to minimizing the sum of squared residuals of the model subject to the constraintA. Theconstrained least squares (CLS) estimator can be given by an explicit formula:[34]
This expression for the constrained estimator is valid as long as the matrixXTX is invertible. It was assumed from the beginning of this article that this matrix is of full rank, and it was noted that when the rank condition fails,β will not be identifiable. However it may happen that adding the restrictionA makesβ identifiable, in which case one would like to find the formula for the estimator. The estimator is equal to[35]
whereR is ap×(p − q) matrix such that the matrix[Q R] is non-singular, andRTQ = 0. Such a matrix can always be found, although generally it is not unique. The second formula coincides with the first in case whenXTX is invertible.[35]
The following data set gives average heights and weights for American women aged 30–39 (source:The World Almanac and Book of Facts, 1975).
| Height (m) | 1.47 | 1.50 | 1.52 | 1.55 | 1.57 | |
|---|---|---|---|---|---|---|
| Weight (kg) | 52.21 | 53.12 | 54.48 | 55.84 | 57.20 | |
| Height (m) | 1.60 | 1.63 | 1.65 | 1.68 | 1.70 | |
| Weight (kg) | 58.57 | 59.93 | 61.29 | 63.11 | 64.47 | |
| Height (m) | 1.73 | 1.75 | 1.78 | 1.80 | 1.83 | |
| Weight (kg) | 66.28 | 68.10 | 69.92 | 72.19 | 74.46 |
When only one dependent variable is being modeled, ascatterplot will suggest the form and strength of the relationship between the dependent variable and regressors. It might also reveal outliers, heteroscedasticity, and other aspects of the data that may complicate the interpretation of a fitted regression model. The scatterplot suggests that the relationship is strong and can be approximated as a quadratic function. OLS can handle non-linear relationships by introducing the regressorHEIGHT2. The regression model then becomes a multiple linear model:

The output from most popularstatistical packages will look similar to this:
| Method | Least squares | |||
| Dependent variable | WEIGHT | |||
| Observations | 15 | |||
| Parameter | Value | Std error | t-statistic | p-value |
|---|---|---|---|---|
| 128.8128 | 16.3083 | 7.8986 | 0.0000 | |
| −143.1620 | 19.8332 | −7.2183 | 0.0000 | |
| 61.9603 | 6.0084 | 10.3122 | 0.0000 | |
| R2 | 0.9989 | S.E. of regression | 0.2516 | |
| Adjusted R2 | 0.9987 | Model sum-of-sq. | 692.61 | |
| Log-likelihood | 1.0890 | Residual sum-of-sq. | 0.7595 | |
| Durbin–Watson stat. | 2.1013 | Total sum-of-sq. | 693.37 | |
| Akaike criterion | 0.2548 | F-statistic | 5471.2 | |
| Schwarz criterion | 0.3964 | p-value (F-stat) | 0.0000 | |
In this table:

Ordinary least squares analysis often includes the use of diagnostic plots designed to detect departures of the data from the assumed form of the model. These are some of the common diagnostic plots:
An important consideration when carrying out statistical inference using regression models is how the data were sampled. In this example, the data are averages rather than measurements on individual women. The fit of the model is very good, but this does not imply that the weight of an individual woman can be predicted with high accuracy based only on her height.
This example also demonstrates that coefficients determined by these calculations are sensitive to how the data is prepared. The heights were originally given rounded to the nearest inch and have been converted and rounded to the nearest centimetre. Since the conversion factor is one inch to 2.54 cm this isnot an exact conversion. The original inches can be recovered by Round(x/0.0254) and then re-converted to metric without rounding. If this is done the results become:
| Const | Height | Height2 | |
|---|---|---|---|
| Converted to metric with rounding. | 128.8128 | −143.162 | 61.96033 |
| Converted to metric without rounding. | 119.0205 | −131.5076 | 58.5046 |

Using either of these equations to predict the weight of a 5' 6" (1.6764 m) woman gives similar values: 62.94 kg with rounding vs. 62.98 kg without rounding. Thus a seemingly small variation in the data has a real effect on the coefficients but a small effect on the results of the equation.
While this may look innocuous in the middle of the data range it could become significant at the extremes or in the case where the fitted model is used to project outside the data range (extrapolation).
This highlights a common error: this example is an abuse of OLS which inherently requires that the errors in the independent variable (in this case height) are zero or at least negligible. The initial rounding to nearest inch plus any actual measurement errors constitute a finite and non-negligible error. As a result, the fitted parameters are not the best estimates they are presumed to be. Though not totally spurious the error in the estimation will depend upon relative size of thex andy errors.
We can use the least square mechanism to figure out the equation of a two body orbit in polar base co-ordinates. The equation typically used is where is the radius of how far the object is from one of the bodies. In the equation the parameters and are used to determine the path of the orbit. We have measured the following data.
| (in degrees) | 43 | 45 | 52 | 93 | 108 | 116 |
|---|---|---|---|---|---|---|
| 4.7126 | 4.5542 | 4.0419 | 2.2187 | 1.8910 | 1.7599 |
We need to find the least-squares approximation of and for the given data.
First we need to represent e and p in a linear form. So we are going to rewrite the equation as.
Furthermore, one could fit forapsides by expanding with an extra parameter as, which is linear in both and in the extra basis function.
We use the original two-parameter form to represent our observational data as:
where:
;; contains the coefficients of in the first column, which are all 1, and the coefficients of in the second column, given by; and, such that:
On solving we get,
so and
{{cite book}}:ISBN / Date incompatibility (help)