Linear Regression Fundamentals
Linear Regression, R2, and the F-Test
Linear regression: the central ideas
Linear regression describes how an outcome may vary with one or more predictors. It can be used to make predictions and to assess how well the predictors account for variation in the outcome.
Three central ideas are:
Fit a model using least squares. Choose the line or other linear model that minimizes the total squared prediction errors.
Calculate R2. Measure how much of the outcome’s variation is accounted for by the fitted model compared with a model that predicts the outcome’s mean.
Test statistical significance. Use an F-test to assess whether the reduction in variation is large enough to be unlikely under a model with no useful predictor effect.
A running example is predicting mouse size from mouse weight. Weight is the predictor; size is the outcome. The same ideas extend to predicting an outcome from several predictors.
Fitting a model by least squares
Residuals and squared errors
A fitted line gives a predicted outcome for each observation. The residual is the difference between an observed value and its prediction:
Here, yi is the observed outcome for observation i, and y^i is the value predicted by the model. A residual can be positive or negative, depending on whether the model underpredicts or overpredicts.
To measure the model’s overall error:
Calculate the residual for each observation.
Square each residual, so positive and negative errors do not cancel.
Add the squared residuals.
The result is the residual sum of squares, also called the sum of squares around the fitted model:
The symbol n is the number of observations.
The least-squares fit
A line can be rotated or adjusted to produce different predictions and residuals. Each candidate line has its own residual sum of squares. Least squares selects the line that makes that sum as small as possible.
For a simple linear regression with one predictor, the line has an intercept and a slope:
Intercept β0: the predicted outcome when the predictor x is zero.
Slope β1: the predicted change in the outcome for a one-unit increase in x.
If the fitted slope is nonzero, the fitted line represents a relationship between the predictor and outcome. In the mouse example, a relationship between weight and size can help predict size from weight. A relationship by itself does not establish that one variable causes the other.
Measuring variation and calculating R2
Variation around the mean
A simple baseline prediction is the mean outcome for every observation. To measure how much the observed outcomes vary around that mean, calculate each observation’s distance from the mean, square the distance, and add the squares:
Here, yˉ is the mean of the observed outcomes. This quantity is the sum of squares around the mean.
Dividing a sum of squares by the number of observations gives an average squared deviation, or a variance-like measure:
The fitted model also has a corresponding average squared residual:
Both quantities use the same divisor, n, when making this comparison. They can be understood as average squared deviations per observation: one around the mean and the other around the fitted values.
The meaning and formula for R2
A useful model should reduce the outcome’s variation compared with predicting the mean. R2, the coefficient of determination, measures the proportion of variation removed by the fitted model:
Because both variation measures are divided by the same n, the sample-size terms cancel. The equivalent sum-of-squares formula is:
Thus, the numerator is the reduction in variation after using the model rather than the mean alone. The denominator is the original variation around the mean.
Interpreting the mouse examples
A model explains some variation: Suppose variation around the mean is 11.1 and variation around the fitted line is 4.4. Then:
R2=11.111.1−4.4≈0.60The model accounts for about 60% of the variation in mouse size relative to the mean-only baseline. Equivalently, the remaining variation around the fitted line is about 40% of the baseline variation.
A perfect fit: If every observation lies exactly on the fitted line, then Vfit=0. Therefore, R2=1, or 100%. In this example, the fitted values account for all observed variation around the mean.
No reduction in variation: If the fitted model leaves the same variation as the mean-only baseline, then Vfit=Vmean. Therefore, R2=0. The predictor does not improve on predicting the mean in terms of squared error.
The same R2 can be calculated from sums of squares. For example, if SSmean=100 and SSfit=40, then:
Applying R2 beyond a straight line
The comparison behind R2 does not depend on the model being a simple line. It compares the sum of squared deviations around the mean with the sum of squared residuals around a fitted model. The model might use several predictors or otherwise have a more complicated equation; the same basic comparison applies.
Multiple regression and fitting a plane
With two predictors, the fitted model can be represented as a plane in a three-dimensional graph. For example, mouse body length could be predicted from mouse weight and tail length:
The outcome, body length, is plotted on one axis, while the two predictors occupy the other two axes. Least squares chooses the plane that minimizes the sum of squared residuals.
The intercept is the prediction when both predictors equal zero.
The weight coefficient describes how the prediction changes with weight while tail length is held fixed.
The tail-length coefficient describes how the prediction changes with tail length while weight is held fixed.
For a particular mouse, its weight and tail length are substituted into the equation to obtain a predicted body length. The difference between the observed and predicted body length is its residual.
Why adding predictors can inflate R2
Adding a predictor gives a least-squares model another way to fit the data. The model can always assign the new predictor a coefficient of zero, which makes the expanded model behave like the smaller model. Therefore, adding predictors cannot make the least-squares residual sum of squares worse; it can only leave it unchanged or reduce it. Since a smaller SSfit tends to produce a larger R2, ordinary R2 does not penalize the addition of predictors.
This property creates a risk of overfitting. A model can appear to improve because a predictor happens to line up with the outcome by chance in the particular data set, even if that predictor has no meaningful relationship with the outcome. Adding many irrelevant predictors creates more opportunities for such chance patterns.
For instance, a model could include weight, tail length, a coin-flip result, favorite color, or other variables. A useless predictor may receive a coefficient of zero and contribute nothing, but a chance association may reduce the residual sum of squares and make R2 appear better.
Adjusted R2 addresses this issue by accounting for the number of predictors or parameters in the model. Unlike ordinary R2, it penalizes unnecessary complexity, so adding a predictor does not automatically improve the adjusted measure.
Why a large R2 is not enough
A high R2 indicates that a model accounts for a large proportion of the variation in the observed data. It does not, by itself, show that the relationship is statistically reliable.
With only two distinct observations, a straight line can be drawn through both points, giving zero residual sum of squares and an apparent R2 of 100%. But that perfect fit is guaranteed by the geometry of two points; it is not strong evidence of a reliable relationship.
A statistical test is needed to assess whether the model’s reduction in variation is more than would be expected by chance. The test discussed here uses an F-statistic and produces a p-value.
The F-statistic for regression
Explained and unexplained variation
The F-statistic compares:
The variation explained by adding predictors to the mean-only model.
The variation that remains unexplained in the residuals.
For a model compared with a mean-only model, the regression F-statistic is:
The numerator measures the reduction in sum of squares per additional model parameter. The denominator measures the residual sum of squares per residual degree of freedom.
The numerator and denominator are therefore variance estimates adjusted for their respective degrees of freedom. A large F value results when the model explains substantial variation per added parameter while leaving relatively little unexplained variation per residual degree of freedom.
Parameters and degrees of freedom
A parameter is a quantity estimated by the model, such as an intercept or slope.
For a simple regression line:
there are two fitted parameters: the intercept and the slope. A mean-only model has one parameter: the mean, which functions as an intercept for a horizontal line. Thus, the line adds one parameter beyond the mean-only model.
For a plane with two predictors, the model has an intercept and two predictor coefficients, for three parameters total. Compared with the one-parameter mean-only model, it adds two parameters.
In the F formula:
pfit is the number of parameters in the fitted model.
pmean is the number of parameters in the mean-only model.
pfit−pmean is the number of added parameters.
n−pfit is the residual degrees of freedom used in the denominator.
More model parameters require more information to estimate reliably. For example, estimating a line requires less information than estimating a plane. The denominator accounts for the degrees of freedom remaining after estimating the fitted model’s parameters.
Relationship between R2 and F
Both R2 and F use the reduction in variation due to the model:
This quantity is the numerator of the R2 expression and the unscaled explained sum of squares in the F-test. The two statistics differ in purpose:
R2 describes the proportion of baseline variation accounted for by the model.
F compares explained variation with unexplained variation while accounting for the numbers of parameters and observations.
From the F-statistic to a p-value
The p-value assesses how unusual the observed F statistic would be under a null model in which the added predictors do not provide a genuine improvement. It is calculated from the appropriate F distribution, whose shape depends on its degrees of freedom.
Conceptually, the comparison works as follows:
Consider outcomes generated under the null situation, where the predictors do not genuinely improve on the mean-only model.
For each such data set, fit the mean-only model and the regression model.
Calculate the sums of squares, then calculate an F statistic.
Compare the observed F statistic with the distribution of statistics expected under the null.
Find the proportion of null statistics at least as extreme as the observed value. This proportion is the p-value.
In practice, it is generally unnecessary to generate millions of random data sets. The theoretical F distribution provides the reference distribution for calculating the tail probability.
A smaller p-value means that an F statistic as large as the observed one would be less common under the null model. The p-value is not the probability that the null hypothesis is true; it describes the extremeness of the data under the null model.
The residual degrees of freedom affect the shape of the F distribution. With more observations relative to the number of fitted parameters, the reference distribution changes, which can affect the p-value for a given test statistic.
Interpreting regression results together
A useful regression result needs to be considered from more than one angle:
R2 describes explanatory fit: how much of the observed outcome variation is accounted for by the model relative to predicting the mean.
The F-test and its p-value assess statistical evidence: whether the improvement over the mean-only model is sufficiently large relative to unexplained variation and model complexity.
Model complexity matters: ordinary R2 can rise when predictors are added, including predictors that only appear useful because of chance.
Both measures are important: a model can explain a substantial amount of variation but provide weak statistical evidence, or show statistical evidence for a relationship while explaining only a modest share of the total variation.
For example, a model predicting mouse size from weight may have an R2 of 0.60, meaning it accounts for about 60% of the variation relative to the mean-only baseline. The F-test then evaluates whether that reduction is large in relation to the residual variation, the number of added parameters, and the available observations.

Ask anything
We've read this whole study set, so questions about it get the best answers.