Simple linear regression

Okun's law in macroeconomics is an example of the simple linear regression. Here the dependent variable (GDP growth) is presumed to be in a linear relationship with the changes in the unemployment rate.

In statistics, simple linear regression is a linear regression model with a single explanatory variable.[1][2][3][4] That is, it concerns two-dimensional sample points with one independent variable and one dependent variable (conventionally, the x and y coordinates in a Cartesian coordinate system) and finds a linear function (a non-vertical straight line) that, as accurately as possible, predicts the dependent variable values as a function of the independent variables. The adjective simple refers to the fact that the outcome variable is related to a single predictor.

It is common to make the additional hypothesis that the ordinary least squares method should be used to minimize the residuals. Under this hypothesis, the accuracy of a line through the sample points is measured by the sum of squared residuals (vertical distances between the points of the data set and the fitted line), and the goal is to make this sum as small as possible. Other regression methods that can be used in place of ordinary least squares include least absolute deviations (minimizing the sum of absolute values of residuals) and the Theil–Sen estimator (which chooses a line whose slope is the median of the slopes determined by pairs of sample points). Deming regression (total least squares) also finds a line that fits a set of two-dimensional sample points, but (unlike ordinary least squares, least absolute deviations, and median slope regression) it is not really an instance of simple linear regression, because it does not separate the coordinates into one dependent and one independent variable and could potentially return a vertical line as its fit.

The remainder of the article assumes an ordinary least squares regression. In this case, the slope of the fitted line is equal to the correlation between y and x corrected by the ratio of standard deviations of these variables. The intercept of the fitted line is such that it passes through the center of mass (x, y) of the data points.

Fitting the regression line

Suppose there are n data points {(xi, yi), i = 1, ..., n}. The function that describes x and y is:

${\displaystyle y_{i}=\alpha +\beta x_{i}+\varepsilon _{i}.}$

The goal is to find the equation of the straight line

${\displaystyle y=\alpha +\beta x,}$

which would provide a "best" fit for the data points. Here the "best" will be understood as in the least-squares approach: a line that minimizes the sum of squared residuals of the linear regression model. In other words, α (the y-intercept) and β (the slope) solve the following minimization problem:

${\displaystyle {\text{Find }}\min _{\alpha ,\,\beta }Q(\alpha ,\beta ),\qquad {\text{for }}Q(\alpha ,\beta )=\sum _{i=1}^{n}\varepsilon _{i}^{\,2}=\sum _{i=1}^{n}(y_{i}-\alpha -\beta x_{i})^{2}\ }$

By simply expanding to get a quadratic expression in α and β, it can be shown that the values of α and β that minimize the objective function Q[5] are

{\displaystyle {\begin{aligned}{\hat {\beta }}&={\frac {\sum _{i=1}^{n}(x_{i}-{\bar {x}})(y_{i}-{\bar {y}})}{\sum _{i=1}^{n}(x_{i}-{\bar {x}})^{2}}}\\[6pt]&={\frac {\sum _{i=1}^{n}(x_{i}y_{i}-x_{i}{\bar {y}}-{\bar {x}}y_{i}+{\bar {x}}{\bar {y}})}{\sum _{i=1}^{n}(x_{i}^{2}-2x_{i}{\bar {x}}+{\bar {x}}^{2})}}\\[6pt]&={\frac {\sum _{i=1}^{n}(x_{i}y_{i})-{\bar {y}}\sum _{i=1}^{n}x_{i}-{\bar {x}}\sum _{i=1}^{n}y_{i}+n{\bar {x}}{\bar {y}}}{\sum _{i=1}^{n}(x_{i}^{2})-2{\bar {x}}\sum _{i=1}^{n}x_{i}+n{\bar {x}}^{2}}}\\[6pt]&={\frac {{\frac {1}{n}}\sum _{i=1}^{n}x_{i}y_{i}-{\bar {x}}{\bar {y}}}{{\frac {1}{n}}\sum _{i=1}^{n}{x_{i}^{2}}-{\overline {x}}^{2}}}\\[6pt]&={\frac {{\overline {xy}}-{\bar {x}}{\bar {y}}}{{\overline {x^{2}}}-{\bar {x}}^{2}}}={\frac {\operatorname {Cov} [x,y]}{\operatorname {Var} [x]}}\\&=r_{xy}{\frac {s_{y}}{s_{x}}},\\[6pt]{\hat {\alpha }}&={\bar {y}}-{\hat {\beta }}\,{\bar {x}},\end{aligned}}}

where rxy is the sample correlation coefficient between x and y; and sx and sy are the sample standard deviation of x and y. A horizontal bar over a quantity indicates the average value of that quantity. For example:

${\displaystyle {\overline {xy}}={\frac {1}{n}}\sum _{i=1}^{n}x_{i}y_{i}.}$

Substituting the above expressions for ${\displaystyle {\hat {\alpha }}}$ and ${\displaystyle {\hat {\beta }}}$ into

${\displaystyle f={\hat {\alpha }}+{\hat {\beta }}x,}$

yields

${\displaystyle {\frac {f-{\bar {y}}}{s_{y}}}=r_{xy}{\frac {x-{\bar {x}}}{s_{x}}}}$

This shows that rxy is the slope of the regression line of the standardized data points (and that this line passes through the origin).

It is sometimes useful to calculate rxy from the data independently using this equation:

${\displaystyle r_{xy}={\frac {{\overline {xy}}-{\bar {x}}{\bar {y}}}{\sqrt {\left({\overline {x^{2}}}-{\bar {x}}^{2}\right)\left({\overline {y^{2}}}-{\bar {y}}^{2}\right)}}}}$

The coefficient of determination (R squared) is equal to ${\displaystyle r_{xy}^{2}}$ when the model is linear with a single independent variable. See sample correlation coefficient for additional details.

Linear regression without the intercept term

Sometimes it is appropriate to force the regression line to pass through the origin, because x and y are assumed to be proportional. For the model without the intercept term, y = βx, the OLS estimator for β simplifies to

${\displaystyle {\hat {\beta }}={\frac {\sum _{i=1}^{n}x_{i}y_{i}}{\sum _{i=1}^{n}x_{i}^{2}}}={\frac {\overline {xy}}{\overline {x^{2}}}}}$

Substituting (xh, yk) in place of (x, y) gives the regression through (h, k):

{\displaystyle {\begin{aligned}{\hat {\beta }}&={\frac {\overline {(x-h)(y-k)}}{\overline {(x-h)^{2}}}}\\[6pt]&={\frac {{\overline {xy}}+k{\bar {x}}-h{\bar {y}}-hk}{{\overline {x^{2}}}-2h{\bar {x}}+h^{2}}}\\[6pt]&={\frac {{\overline {xy}}-{\bar {x}}{\bar {y}}+({\bar {x}}-h)({\bar {y}}-k)}{{\overline {x^{2}}}-{\bar {x}}^{2}+({\bar {x}}-h)^{2}}}\\[6pt]&={\frac {\operatorname {Cov} [x,y]+({\bar {x}}-h)({\bar {y}}-k)}{\operatorname {Var} [x]+({\bar {x}}-h)^{2}}}\end{aligned}}}

The last form above demonstrates how moving the line away from the center of mass of the data points affects the slope.

Numerical properties

1. The regression line goes through the center of mass point, ${\displaystyle ({\bar {x}},\,{\bar {y}})}$, if the model includes an intercept term (i.e., not forced through the origin).
2. The sum of the residuals is zero if the model includes an intercept term:
${\displaystyle \sum _{i=1}^{n}{\hat {\varepsilon }}_{i}=0.}$
3. The residuals and x values are uncorrelated, meaning (whether or not there is an intercept term in the model):
${\displaystyle \sum _{i=1}^{n}x_{i}{\hat {\varepsilon }}_{i}\;=\;0}$

Model-cased properties

Description of the statistical properties of estimators from the simple linear regression estimates requires the use of a statistical model. The following is based on assuming the validity of a model under which the estimates are optimal. It is also possible to evaluate the properties under other assumptions, such as inhomogeneity, but this is discussed elsewhere.[clarification needed]

Unbiasedness

The estimators ${\displaystyle {\hat {\alpha }}}$ and ${\displaystyle {\hat {\beta }}}$ are unbiased. This requires that we interpret the estimators as random variables and so we have to assume that, for each value of x, the corresponding value of y is generated as a mean response α + βx plus an additional random variable ε called the error term. This error term has to be equal to zero on average, for each value of x. Under such interpretation, the least-squares estimators ${\displaystyle {\hat {\alpha }}}$ and ${\displaystyle {\hat {\beta }}}$ will themselves be random variables, and they will unbiasedly estimate the "true values" α and β.

Confidence intervals

The formulas given in the previous section allow one to calculate the point estimates of α and β — that is, the coefficients of the regression line for the given set of data. However, those formulas don't tell us how precise the estimates are, i.e., how much the estimators ${\displaystyle {\hat {\alpha }}}$ and ${\displaystyle {\hat {\beta }}}$ vary from sample to sample for the specified sample size. Confidence intervals were devised to give a plausible set of values to the estimates one might have if one repeated the experiment a very large number of times.

The standard method of constructing confidence intervals for linear regression coefficients relies on the normality assumption, which is justified if either:

1. the errors in the regression are normally distributed (the so-called classic regression assumption), or
2. the number of observations n is sufficiently large, in which case the estimator is approximately normally distributed.

The latter case is justified by the central limit theorem.

Normality assumption

Under the first assumption above, that of the normality of the error terms, the estimator of the slope coefficient will itself be normally distributed with mean β and variance ${\displaystyle \sigma ^{2}/\sum (x_{i}-{\bar {x}})^{2},}$ where σ2 is the variance of the error terms (see Proofs involving ordinary least squares). At the same time the sum of squared residuals Q is distributed proportionally to χ2 with n − 2 degrees of freedom, and independently from ${\displaystyle {\hat {\beta }}}$. This allows us to construct a t-statistic

${\displaystyle t={\frac {{\hat {\beta }}-\beta }{s_{\hat {\beta }}}}\ \sim \ t_{n-2},}$

where

${\displaystyle s_{\hat {\beta }}={\sqrt {\frac {{\frac {1}{n-2}}\sum _{i=1}^{n}{\hat {\varepsilon }}_{i}^{\,2}}{\sum _{i=1}^{n}(x_{i}-{\bar {x}})^{2}}}}}$

is the standard error of the estimator ${\displaystyle {\hat {\beta }}}$.

This t-statistic has a Student's t-distribution with n − 2 degrees of freedom. Using it we can construct a confidence interval for β:

${\displaystyle \beta \in \left[{\hat {\beta }}-s_{\hat {\beta }}t_{n-2}^{*},\ {\hat {\beta }}+s_{\hat {\beta }}t_{n-2}^{*}\right],}$

at confidence level (1 − γ), where ${\displaystyle t_{n-2}^{*}}$ is the ${\displaystyle \scriptstyle \left(1\;-\;{\frac {\gamma }{2}}\right){\text{-th}}}$ quantile of the tn−2 distribution. For example, if γ = 0.05 then the confidence level is 95%.

Similarly, the confidence interval for the intercept coefficient α is given by

${\displaystyle \alpha \in \left[{\hat {\alpha }}-s_{\hat {\alpha }}t_{n-2}^{*},\ {\hat {\alpha }}+s_{\hat {\alpha }}t_{n-2}^{*}\right],}$

at confidence level (1 − γ), where

${\displaystyle s_{\hat {\alpha }}=s_{\hat {\beta }}{\sqrt {{\frac {1}{n}}\sum _{i=1}^{n}x_{i}^{2}}}={\sqrt {{\frac {1}{n(n-2)}}\left(\sum _{j=1}^{n}{\hat {\varepsilon }}_{j}^{\,2}\right){\frac {\sum _{i=1}^{n}x_{i}^{2}}{\sum _{i=1}^{n}(x_{i}-{\bar {x}})^{2}}}}}}$
The US "changes in unemployment – GDP growth" regression with the 95% confidence bands.

The confidence intervals for α and β give us the general idea where these regression coefficients are most likely to be. For example, in the Okun's law regression shown at the beginning of the article the point estimates are

${\displaystyle {\hat {\alpha }}=0.859,\qquad {\hat {\beta }}=-1.817.}$

The 95% confidence intervals for these estimates are

${\displaystyle \alpha \in \left[0.76,0.96\right],\qquad \beta \in \left[-2.06,-1.58\right].}$

In order to represent this information graphically, in the form of the confidence bands around the regression line, one has to proceed carefully and account for the joint distribution of the estimators. It can be shown[citation needed] that at confidence level (1 − γ) the confidence band has hyperbolic form given by the equation

${\displaystyle \left.{\hat {y}}\right|_{x=\xi }\in \left[{\hat {\alpha }}+{\hat {\beta }}\xi \pm t_{n-2}^{*}{\sqrt {\left({\frac {1}{n-2}}\sum {\hat {\varepsilon }}_{i}^{\,2}\right)\cdot \left({\frac {1}{n}}+{\frac {(\xi -{\bar {x}})^{2}}{\sum (x_{i}-{\bar {x}})^{2}}}\right)}}\right].}$

Asymptotic assumption

The alternative second assumption states that when the number of points in the dataset is "large enough", the law of large numbers and the central limit theorem become applicable, and then the distribution of the estimators is approximately normal. Under this assumption all formulas derived in the previous section remain valid, with the only exception that the quantile t*n−2 of Student's t distribution is replaced with the quantile q* of the standard normal distribution. Occasionally the fraction 1/n−2 is replaced with 1/n. When n is large such a change does not alter the results appreciably.

Numerical example

This example concerns the data set from the ordinary least squares article. This data set gives average masses for women as a function of their height in a sample of American women of age 30–39. Although the OLS article argues that it would be more appropriate to run a quadratic regression for this data, the simple linear regression model is applied here instead.

 Height (m), xi Mass (kg), yi 1.47 1.5 1.52 1.55 1.57 1.6 1.63 1.65 1.68 1.7 1.73 1.75 1.78 1.8 1.83 52.21 53.12 54.48 55.84 57.2 58.57 59.93 61.29 63.11 64.47 66.28 68.1 69.92 72.19 74.46

There are n = 15 points in this data set. Hand calculations would be started by finding the following five sums:

{\displaystyle {\begin{aligned}&S_{x}=\sum x_{i}=24.76,\quad S_{y}=\sum y_{i}=931.17\\&S_{xx}=\sum x_{i}^{2}=41.0532,\quad S_{xy}=\sum x_{i}y_{i}=1548.2453,\quad S_{yy}=\sum y_{i}^{2}=58498.5439\end{aligned}}}

These quantities would be used to calculate the estimates of the regression coefficients, and their standard errors.

{\displaystyle {\begin{aligned}{\hat {\beta }}&={\frac {nS_{xy}-S_{x}S_{y}}{nS_{xx}-S_{x}^{2}}}=61.272\\{\hat {\alpha }}&={\frac {1}{n}}S_{y}-{\hat {\beta }}{\frac {1}{n}}S_{x}=-39.062\\s_{\varepsilon }^{2}&={\frac {1}{n(n-2)}}\left[nS_{yy}-S_{y}^{2}-{\hat {\beta }}^{2}(nS_{xx}-S_{x}^{2})\right]=0.5762\\s_{\hat {\beta }}^{2}&={\frac {ns_{\varepsilon }^{2}}{nS_{xx}-S_{x}^{2}}}=3.1539\\s_{\hat {\alpha }}^{2}&=s_{\hat {\beta }}^{2}{\frac {1}{n}}S_{xx}=8.63185\end{aligned}}}

The 0.975 quantile of Student's t-distribution with 13 degrees of freedom is t*13 = 2.1604, and thus the 95% confidence intervals for α and β are

{\displaystyle {\begin{aligned}&\alpha \in [\,{\hat {\alpha }}\mp t_{13}^{*}s_{\alpha }\,]=[\,{-45.4},\ {-32.7}\,]\\&\beta \in [\,{\hat {\beta }}\mp t_{13}^{*}s_{\beta }\,]=[\,57.4,\ 65.1\,]\end{aligned}}}

The product-moment correlation coefficient might also be calculated:

${\displaystyle {\hat {r}}={\frac {nS_{xy}-S_{x}S_{y}}{\sqrt {(nS_{xx}-S_{x}^{2})(nS_{yy}-S_{y}^{2})}}}=0.9945}$

This example also demonstrates that sophisticated calculations will not overcome the use of badly prepared data. The heights were originally given in inches, and have been converted to the nearest centimetre. Since the conversion factor is one inch to 2.54 cm, this is not a correct conversion. The original inches can be recovered by Round(x/0.0254) and then re-converted to metric: if this is done, the results become

${\displaystyle {\hat {\beta }}=61.6746,\qquad {\hat {\alpha }}=-39.7468.}$

Thus a seemingly small variation in the data has a real effect.

Derivation of simple regression estimators

We look for ${\displaystyle {\hat {\alpha }}}$ and ${\displaystyle {\hat {\beta }}}$ that minimize the sum of squared errors (SSE):

${\displaystyle \min _{{\hat {\alpha }},{\hat {\beta }}}\,\operatorname {SSE} \left({\hat {\alpha }},{\hat {\beta }}\right)\equiv \min _{{\hat {\alpha }},{\hat {\beta }}}\sum _{i=1}^{n}\left(y_{i}-{\hat {\alpha }}-{\hat {\beta }}x_{i}\right)^{2}}$

To find a minimum take partial derivatives with respect to ${\displaystyle {\hat {\alpha }}}$ and ${\displaystyle {\hat {\beta }}}$

{\displaystyle {\begin{aligned}&{\frac {\partial }{\partial {\hat {\alpha }}}}\left(\operatorname {SSE} \left({\hat {\alpha }},{\hat {\beta }}\right)\right)=-2\sum _{i=1}^{n}\left(y_{i}-{\hat {\alpha }}-{\hat {\beta }}x_{i}\right)=0\\\Rightarrow {}&\sum _{i=1}^{n}\left(y_{i}-{\hat {\alpha }}-{\hat {\beta }}x_{i}\right)=0\\\Rightarrow {}&\sum _{i=1}^{n}y_{i}=\sum _{i=1}^{n}{\hat {\alpha }}+{\hat {\beta }}\sum _{i=1}^{n}x_{i}\\\Rightarrow {}&\sum _{i=1}^{n}y_{i}=n{\hat {\alpha }}+{\hat {\beta }}\sum _{i=1}^{n}x_{i}\\\Rightarrow {}&{\frac {1}{n}}\sum _{i=1}^{n}y_{i}={\hat {\alpha }}+{\frac {1}{n}}{\hat {\beta }}\sum _{i=1}^{n}x_{i}\\\Rightarrow {}&{\bar {y}}={\hat {\alpha }}+{\hat {\beta }}{\bar {x}}\end{aligned}}}

Before taking partial derivative with respect to ${\displaystyle {\hat {\beta }}}$, substitute the previous result for ${\displaystyle {\hat {\alpha }}}$.

${\displaystyle \min _{{\hat {\alpha }},{\hat {\beta }}}\sum _{i=1}^{n}\left[y_{i}-\left({\bar {y}}-{\hat {\beta }}{\bar {x}}\right)-{\hat {\beta }}x_{i}\right]^{2}=\min _{{\hat {\alpha }},{\hat {\beta }}}\sum _{i=1}^{n}\left[\left(y_{i}-{\bar {y}}\right)-{\hat {\beta }}\left(x_{i}-{\bar {x}}\right)\right]^{2}}$

Now, take the derivative with respect to ${\displaystyle {\hat {\beta }}}$:

{\displaystyle {\begin{aligned}&{\frac {\partial }{\partial {\hat {\beta }}}}\left(\operatorname {SSE} \left({\hat {\alpha }},{\hat {\beta }}\right)\right)=-2\sum _{i=1}^{n}\left[\left(y_{i}-{\bar {y}}\right)-{\hat {\beta }}\left(x_{i}-{\bar {x}}\right)\right]\left(x_{i}-{\bar {x}}\right)=0\\\Rightarrow {}&\sum _{i=1}^{n}\left(y_{i}-{\bar {y}}\right)\left(x_{i}-{\bar {x}}\right)-{\hat {\beta }}\sum _{i=1}^{n}\left(x_{i}-{\bar {x}}\right)^{2}=0\\\Rightarrow {}&{\hat {\beta }}={\frac {\sum _{i=1}^{n}\left(y_{i}-{\bar {y}}\right)\left(x_{i}-{\bar {x}}\right)}{\sum _{i=1}^{n}\left(x_{i}-{\bar {x}}\right)^{2}}}={\frac {\operatorname {Cov} (x,y)}{\operatorname {Var} (x)}}\end{aligned}}}

And finally substitute ${\displaystyle {\hat {\beta }}}$ to determine ${\displaystyle {\hat {\alpha }}}$

${\displaystyle {\hat {\alpha }}={\bar {y}}-{\hat {\beta }}{\bar {x}}}$