← Economics
Universityeconometrics

Regression and Ordinary Least Squares

Fit a line to economic data by minimizing squared residuals, interpret its coefficients and fit carefully, and keep statistical association distinct from a causal effect.

Before this, you should know:

Suppose a city notices that places with higher bus fares tend to have fewer bus riders. How could we put a number on that pattern? We could draw a line through a scatterplot. But which line? A line can be placed in many ways, and “looks about right” is not a rule another researcher can reproduce.

Ordinary least squares, usually shortened to OLS, gives us a precise rule: choose the line that makes the sum of squared vertical prediction errors as small as possible. That rule produces a useful summary of the data. It does not, by itself, tell us that changing fares caused ridership to change.

A line as a compact description

Let xix_i be an observed explanatory variable for unit ii—for example, the bus fare in a city—and let yiy_i be an outcome such as weekly rides. A simple linear regression writes the fitted outcome as

y^i=b0+b1xi.\hat{y}_i = b_0 + b_1 x_i.

Here y^i\hat{y}_i is the fitted value: what the line predicts at xix_i. The number b0b_0 is the intercept, and b1b_1 is the slope. The observed value will usually not fall exactly on the line. Its residual is

ei=yi−y^i.e_i = y_i - \hat{y}_i.

A positive residual means the observed outcome is above the prediction; a negative residual means it is below. Residuals let us see where and by how much the line misses. They are not the same as the unobserved influences in an economic model: they are calculated from observed outcomes and fitted predictions.

Why square the residuals?

If we simply added residuals, positive and negative misses could cancel. OLS instead chooses b0b_0 and b1b_1 to minimize the residual sum of squares:

RSS(b0,b1)=∑i=1n[yi−(b0+b1xi)]2.RSS(b_0,b_1) = \sum_{i=1}^{n}\left[y_i-(b_0+b_1x_i)\right]^2.

Squaring makes every contribution nonnegative and gives larger misses more weight. This is a choice of loss function: OLS treats a miss twice as large as another as four times as costly in the objective. That can make the fitted line sensitive to unusual points, so it is worth looking at a plot rather than trusting a coefficient alone.

To find the minimizing line, take derivatives of RSSRSS with respect to the intercept and slope and set them equal to zero. For the intercept,

∂RSS∂b0=−2∑i=1n[yi−(b0+b1xi)]=0,\frac{\partial RSS}{\partial b_0} = -2\sum_{i=1}^{n}\left[y_i-(b_0+b_1x_i)\right] = 0,

so the residuals sum to zero. For the slope,

∂RSS∂b1=−2∑i=1nxi[yi−(b0+b1xi)]=0.\frac{\partial RSS}{\partial b_1} = -2\sum_{i=1}^{n}x_i\left[y_i-(b_0+b_1x_i)\right] = 0.

Solving these conditions gives

b1=∑i=1n(xi−xˉ)(yi−yˉ)∑i=1n(xi−xˉ)2,b0=yˉ−b1xˉ.b_1 = \frac{\sum_{i=1}^{n}(x_i-\bar{x})(y_i-\bar{y})} {\sum_{i=1}^{n}(x_i-\bar{x})^2}, \qquad b_0 = \bar{y} - b_1\bar{x}.

The slope is large in magnitude when xx and yy move together strongly relative to how much xx varies. The intercept then makes the fitted line pass through the sample means (xˉ,yˉ)(\bar{x},\bar{y}). In the usual model with an intercept, the fitted residuals also have zero sample mean and are uncorrelated with the included xx values. These are mathematical properties of the fitted line—not evidence that all relevant economic influences have been accounted for.

A scatterplot of four toy city observations, with bus fare on the horizontal axis and weekly bus rides in thousands on the vertical axis. A downward-sloping OLS line gives fitted values, and amber vertical segments show positive and negative residuals from observed points.

OLS chooses the line that minimizes the sum of squared vertical residuals. The residuals show the observed-minus-fitted gaps, not causal effects.

What the slope says—and what it does not

In a simple regression, b1b_1 is the change in the fitted outcome associated with a one-unit increase in xx. Its units are “units of yy per unit of xx.” If xx is dollars per ride and yy is thousands of rides per week, then b1=−2b_1=-2 means the fitted weekly ridership is 2,000 trips lower for a one-dollar higher fare in this sample’s line.

That is an association. It is not automatically the effect of raising a fare. Cities with high fares may also have different incomes, population densities, service quality, or commuting patterns. Those factors may be related to both fares and ridership. OLS has minimized a particular set of prediction errors; it has not created the missing comparison of what ridership would have been in the same city under a different fare.

With several explanatory variables, a linear fitted equation can be written

y^i=b0+b1xi1+b2xi2+⋯+bkxik.\hat{y}_i = b_0 + b_1x_{i1} + b_2x_{i2} + \cdots + b_kx_{ik}.

Now b1b_1 describes the change in the fitted outcome associated with a one-unit increase in x1x_1 while the other included variables are held fixed. This “holding fixed” is a comparison inside the regression model. It does not prove that every confounding difference has been controlled or that the remaining association is causal. The design and assumptions behind the comparison still matter.

Summarizing fit without worshipping it

For a simple in-sample regression with an intercept, one common fit statistic is R2R^2:

R2=1−RSSTSS,TSS=∑i=1n(yi−yˉ)2.R^2 = 1 - \frac{RSS}{TSS}, \qquad TSS = \sum_{i=1}^{n}(y_i-\bar{y})^2.

It compares the fitted line’s squared residuals with the squared deviations we would have if we predicted every observation using the sample mean alone. An R2R^2 of 0.80 means the line reduces that in-sample squared-error measure by 80% relative to the mean-only baseline, under this definition. It does not mean the predictor caused 80% of the outcome, that 80% of predictions are correct, or that the model will predict new observations equally well.

A high R2R^2 can coexist with a bad causal story, influential outliers, or an inappropriate straight-line shape. A low R2R^2 can still accompany a useful estimate of a particular slope. Plot the data, inspect residuals, check units and ranges, and ask whether the linear summary fits the question. Predictions far beyond the observed xx values are extrapolations; the data provide less support for them.

Worked example

Four fictional cities have the following average bus fares and weekly bus rides: (1,9)(1,9), (2,5)(2,5), (3,3)(3,3), and (4,3)(4,3), where fares are dollars per ride and ridership is in thousands. Find the OLS line, calculate its fitted values and residuals, and interpret the slope and R2R^2 without making a causal claim.

Step 1 — calculate the sample means. The average fare and average ridership are

xˉ=1+2+3+44=2.5,yˉ=9+5+3+34=5.\bar{x} = \frac{1+2+3+4}{4}=2.5, \qquad \bar{y} = \frac{9+5+3+3}{4}=5.

Step 2 — calculate the slope. Use the OLS formula:

∑(xi−xˉ)(yi−yˉ)=−10,∑(xi−xˉ)2=5,\sum (x_i-\bar{x})(y_i-\bar{y}) = -10, \qquad \sum (x_i-\bar{x})^2 = 5,

so

b1=−105=−2.b_1 = \frac{-10}{5}=-2.

The fitted line falls by two thousand weekly rides for each additional dollar of fare in this sample.

Step 3 — calculate the intercept. The line passes through (xˉ,yˉ)(\bar{x},\bar{y}):

b0=yˉ−b1xˉ=5−(−2)(2.5)=10.b_0 = \bar{y}-b_1\bar{x} = 5-(-2)(2.5)=10.

Therefore the fitted equation is

y^=10−2x.\hat{y} = 10-2x.

Step 4 — calculate predictions and residuals. For fares 1, 2, 3, and 4, the fitted ridership values are 8, 6, 4, and 2 thousand. Subtract fitted from observed:

| Fare xx | Observed rides yy | Fitted rides y^\hat{y} | Residual y−y^y-\hat{y} | | ---: | ---: | ---: | ---: | | 1 | 9 | 8 | 1 | | 2 | 5 | 6 | -1 | | 3 | 3 | 4 | -1 | | 4 | 3 | 2 | 1 |

The residual sum of squares is

RSS=12+(−1)2+(−1)2+12=4.RSS = 1^2+(-1)^2+(-1)^2+1^2=4.

Step 5 — calculate R2R^2. The total sum of squares around the mean ridership yˉ=5\bar{y}=5 is

TSS=(9−5)2+(5−5)2+(3−5)2+(3−5)2=24.TSS = (9-5)^2+(5-5)^2+(3-5)^2+(3-5)^2=24.

Thus

R2=1−424=56≈0.833.R^2 = 1-\frac{4}{24}=\frac{5}{6}\approx 0.833.

The fitted line reduces squared prediction error by about 83.3% relative to predicting the sample mean for every city. The negative slope describes a strong downward linear association in these four fictional observations. It does not show that raising a fare would cause the same ridership change: four cities may differ in many other ways, and the example supplies no causal design.

Where this leads

OLS gives economists a reproducible way to summarize conditional relationships, produce fitted values, and measure residual variation. Its algebra answers “which line best fits these data under squared error?” It does not answer “what would happen if we changed xx?” The next topic, Causal Inference: From Correlation to Credible Comparisons, will ask what additional comparison and assumptions are needed before a regression coefficient can support a causal interpretation.

Check yourself

4 questions

  1. In the regression y-hat = 10 - 2x, where x is a bus fare in dollars and y-hat is predicted weekly rides in thousands, what does the slope -2 describe?

  2. For a data point with observed outcome y = 5 and fitted value y-hat = 6, what is its residual under the convention e = y - y-hat?

  3. Why does ordinary least squares square residuals when choosing the fitted line?

  4. A regression has an R-squared of 0.90. Which conclusion is justified by that number alone for an ordinary in-sample model with an intercept?