Login
📚 Statistical Inference for Everyone
Chapters ▾

10.2 Simple Linear Regression

In simple linear regression, we are given data consisting of two variables, typically denoted x and y, where the value of one (y) depends on the other (x). For example, consider the following data of heights (x) and shoe sizes (y) of a small number of individuals1 shown in Table 10.1 and Figure 10.1. By eye we can see a direct correlation - the taller the person the larger shoe size.

Table 10.1. Heights (in inches) and shoe sizes from a subset of McLaren (2012) data.
Height [inches]Shoe Size
64.07
70.09
64.08
71.011
69.012
68.09
69.010
61.06
68.010
70.09
Scatter plot of shoe size versus height in inches for nine people from the McLaren (2012) subset: sizes 6.5-12 against heights 61-71 inches, drifting upward so taller people tend toward larger sizes.
Figure 10.1. Heights (in inches) and shoe sizes from a subset of McLaren (2012) data.

We propose a model of this data of the following linear form:

y = m x + b

where m is the slope and b is the intercept. Clearly this data doesn't form a perfect line, so there is some uncertainty in the slope, intercept, and predicted y values. We assume a Normal distribution for the uncertainties in the data, so the statistical model looks like, for each data point,

y i = m x i + b + N o r m a l ( 0 , σ )

where we want to obtain estimates, m^ and b^, of the “true” values of the slope and intercept, respectively, as well as their uncertainties. This is obtained by getting the posterior probability of the parameters,

P ( m , b | d a t a )

Following our standard procedure,

  1. Specify the prior probabilities for the parameters being considered. For most simple cases we begin with absolutely no knowledge of its value, and thus use a uniform prior probability for each parameter.
  2. Write the top of Bayes' Rule,

    P(m,b|data)P(data|m,b)Normal uncertainties×P(m,b)uniform prior

  3. Add up the values, and divide by this sum to get the final posterior probabilities. This is done by the mathematicians, and we simply summarize the results here.

we obtain the posterior distributions for the parameters m and b. The calculations get too detailed to do by hand, but are very easy with the computer. For the shoe size data in Table 10.1 we get the distributions shown in Figures 10.2 and 10.3 for the slope and intercept, respectively. The most probable values then lead to the best fit, shown in Figure 10.4.

The Student-t test clearly shows that the slope is non-zero (well over 95% of the distribution lies to the right of zero), denoting a statistically significant effect on shoe size from height. The magnitude of the slope, slope=0.42, can be interpreted that every inch of height leads to a 0.42 increase in shoe size on average.

Posterior distribution for the slope of the shoe-size line: a bell curve centered at 0.42 with dashed percentile lines from 0.11 (1%) to 0.73 (99%). Zero slope lies below the 1% line.
Figure 10.2. Posterior distribution for the slope for the linear model on the shoe size data subset.
Posterior distribution for the intercept of the shoe-size line: a broad bell centered at -19.26 with dashed percentile lines from -40.27 (1%) to 1.76 (99%).
Figure 10.3. Posterior distribution for the intercept for the linear model on the shoe size data subset.
The shoe-size scatter with the best-fit line y=0.422x-19.256 drawn through it, rising from about size 6 at 60 inches to size 11 at 72 inches.
Figure 10.4. Best linear fit for the shoe size data subset.

Mean Squared Error

Another way of looking at the same idea is to introduce the notion of Mean Squared Error (MSE). This is defined to be the number resulting from taking the predicted values minus the observed values, squaring them, and taking their mean. The squaring ensures that deviations from the predictions both too high and too low are considered the same. The closer the prediction overall, the smaller the resulting MSE. Mathematically this is written as

M S E i ( y i ( m ^ x i + b ^ ) ) 2 N

One can intuitively think of getting the best fit as adjusting the slopes and intercepts, calculating the MSE for each, and stopping when you reach a minimum value. An example of this is shown in Figure 10.5.

The shoe-size data with the same best line, presented as the line that minimizes the mean squared error: y=0.422x-19.256 passing centrally through the nine points.
Figure 10.5. Minimizing the Mean Squared Error (MSE) results in the best linear fit for the shoe size data subset.

An Educational Example

The following example is from a data set on school expenditures and SAT scores.2 We plot the total SAT scores as a function of expenditures, perform a linear model fit, and present the best values and their uncertainties in Figure 10.6. The model is

t o t a l = i n t e r c e p t + s l o p e · e x p e n d i t u r e

Composite figure: top, a scatter of state SAT totals against per-pupil expenditure with the downward line y=-20.892x+1089.294; bottom, posterior curves for the slope (centered at -20.89, 1%-99% from -38.53 to -3.26) and the intercept (centered at 1089.29, from 982.47 to 1196.12).
Figure 10.6. Total SAT score vs expenditure (top) and the distributions for the slope (bottom left) and intercept (bottom right).

What is immediately odd is that this result seems to suggest the following:

  1. The larger the expenditure per pupil the lower the SAT scores.
  2. For each thousand dollars spent per pupil, the total SAT score goes down 20 points.
  3. If you spent zero dollars per pupil, you'd reach a maximum of SAT score of 1089.

This seems counter intuitive to say the least. What is going on here? What is happening is that there are other variables that are related to the expenditure which then lead to lower SAT scores on average. Such a confounding variable needs to be taken into account in what is called controlling for a variable.

For example, if we look at the relationship between expenditure per pupil and the percent of students taking the SAT we see a pattern, shown in Figure 10.7. The more that is spent per pupil, the more students - both bad and good - take the SAT. Thus, even if expenditure helps students, the fact that the percentage of students taking the exam increases creates the illusion of the opposite. The next section states how you can overcome this problem.

Composite figure: top, a scatter of the percentage of students taking the SAT against per-pupil expenditure with the rising line y=11.638x-33.485; bottom, posteriors for the slope (centered at 11.64, 1%-99% from 6.14 to 17.13) and the intercept (centered at -33.48, from -66.77 to -0.20).
Figure 10.7. Percent of students taking the SAT vs per pupil expenditure (top) and the distributions for the slope (bottom left) and intercept (bottom right).

Adapted from Statistical Inference for Everyone, by Brian Blais (Bryant University), licensed under CC BY-SA 4.0 (dual-licensed under the GNU FDL 1.2 or later; this adaptation uses the CC BY-SA grant). Changes were made; this adaptation is distributed under the same license. License: CC-BY-SA-4.0.