Regression Line (Line of Best Fit)
Jayden is studying for a big math test. His teacher shows a scatter plot of hours studied and test scores from earlier students, with a line drawn through the dots. Jayden plans to study 7 hours. What score should he expect? By the end of this page you will be able to answer that, and to say how much to trust the answer.
Scatter plots and the line of best fit
In a scatter plot, each dot is one student. The $x$-value is the hours studied and the $y$-value is the score. When the dots drift upward from left to right, more hours go with higher scores. We call this a positive association.
A line of best fit is a line drawn through the cloud of dots that follows its trend. Once you have it, you can predict $y$ for a new $x$. Go up from $x$ to the line, then straight across to the $y$-axis.
The best-fit line used in statistics is the regression line (more precisely, the least-squares regression line). The next sections explain exactly how it is chosen. First, let's get comfortable using one.
The line below has two sliders. Move them and watch how $m$ and $b$ change the line.
Observe the following scatter plot showing the relationship between weekly study hours ($X$) and the score obtained on an exam ($Y$). The blue line represents the…
Observe the following scatter plot showing the relationship between weekly study hours ($X$) and the score obtained on an exam ($Y$). The blue line represents the regression line of $Y$ on $X$. Estimate graphically the approximate score a student who studies for $7$ hours would obtain.
The equation of the regression line of weight ($Y$, in pounds) on height ($X$, in inches) of a group of students is $y = 0.8x - 60$. Estimate the approximate weight of a…
The equation of the regression line of weight ($Y$, in pounds) on height ($X$, in inches) of a group of students is $y = 0.8x - 60$. Estimate the approximate weight of a student who is $170$ inches tall.
The regression line formula
Drawing by eye is not precise: two people draw two different lines. Statistics gives one exact line from three numbers.
Recall the mean of each variable, $\bar{x}$ and $\bar{y}$. Together they give the mean point $(\bar{x}, \bar{y})$, also called the center of gravity of the data. Recall also:
-
the variance of $X$: $\sigma_x^2 = \dfrac{1}{n}\sum (x - \bar{x})^2$, which measures how spread out the $x$-values are;
-
the covariance: $\sigma_{xy} = \dfrac{1}{n}\sum (x - \bar{x})(y - \bar{y})$, which is positive when $x$ and $y$ rise together and negative when one rises while the other falls.
Why this slope? The covariance measures how much $x$ and $y$ move together. Dividing by the variance of $x$ turns that into "how much $y$ moves per unit of $x$". Why the mean point? The line is built in point-slope form around that point, so it must pass through it.
Because the line always passes through the mean point, you can use it in two ways. If the line is $y = 2x + 1$ and $\bar{x} = 3$, then $\bar{y} = 2(3) + 1 = 7$. If a candidate line misses the mean point, it is not the regression line. Hitting the mean point is necessary but not enough: many lines pass through that point.
Calculate the equation of the regression line of $Y$ on $X$ for a two-dimensional distribution of which the following statistical parameters are known: the mean of $X$…
Calculate the equation of the regression line of $Y$ on $X$ for a two-dimensional distribution of which the following statistical parameters are known: the mean of $X$ is $\bar{x} = 5$, the mean of $Y$ is $\bar{y} = 12$, the variance of $X$ is $\sigma_x^2 = 4$ and the covariance is $\sigma_{xy} = 6$. Express the result in the form $y = mx + n$.
In a market research study, the regression line of sales ($Y$) on advertising investment ($X$) is given by the equation $y = 2.5x - 4$. If it is known that the mean…
In a market research study, the regression line of sales ($Y$) on advertising investment ($X$) is given by the equation $y = 2.5x - 4$. If it is known that the mean investment of the analyzed companies is $\bar{x} = 6$, what is the value of the mean sales $\bar{y}$ of the distribution?
The means of two statistical variables are $\bar{x} = 10$ and $\bar{y} = 25$. Determine mathematically whether the line $y = 1.5x + 10$ can be the valid regression line…
The means of two statistical variables are $\bar{x} = 10$ and $\bar{y} = 25$. Determine mathematically whether the line $y = 1.5x + 10$ can be the valid regression line for this data set, justifying your answer using a fundamental property that all regression lines must satisfy.
Computing the line from a data table
When you are given the data points, you compute the means, the deviations from the means, and two sums:
$$S_{xy} = \sum (x - \bar{x})(y - \bar{y}) \qquad S_{xx} = \sum (x - \bar{x})^2$$Since $\sigma_{xy} = S_{xy}/n$ and $\sigma_x^2 = S_{xx}/n$, the $n$'s cancel and the slope is simply
$$m = \frac{S_{xy}}{S_{xx}}.$$This is the same line a calculator gives, even though calculators sometimes divide by $n-1$ in the covariance and variance.
A company records the advertising expenditure $X$ (in thousands of dollars) and the sales revenue $Y$ (in thousands of dollars) for $4$ consecutive months. The data…
A company records the advertising expenditure $X$ (in thousands of dollars) and the sales revenue $Y$ (in thousands of dollars) for $4$ consecutive months. The data pairs $(x_i, y_i)$ obtained are: $(2, 5)$, $(3, 7)$, $(4, 9)$ and $(5, 11)$. Calculate the equation of the regression line of $Y$ on $X$.
Residuals: how far off is the line?
The line does not hit every dot. The vertical gap between a dot and the line is the residual.
The regression line is the line that makes the sum of the squares of the residuals as small as possible. That is why it is also called the least-squares line.
Given the data table formed by the points $(1, 2)$, $(2, 4)$, $(3, 5)$ and $(4, 7)$, it has been calculated that the equation of the regression line of $Y$ on $X$ is…
Given the data table formed by the points $(1, 2)$, $(2, 4)$, $(3, 5)$ and $(4, 7)$, it has been calculated that the equation of the regression line of $Y$ on $X$ is $y = 1.6x + 0.5$. Calculate the residual (or estimation error) corresponding to the observation $x = 3$.
Predicting with care
A prediction is only as good as the data behind it. Keep three ideas in mind.
1. Interpolation vs. extrapolation. Predicting for an $x$ inside the range of the data is interpolation, which is usually safe. Predicting outside that range is extrapolation, which is risky. Nothing guarantees the trend continues.
2. Do not "flip" the line. The line of $Y$ on $X$ minimizes errors in $Y$. To estimate $X$ from a known $Y$, you cannot just solve the equation for $x$. The rigorous way is to compute a different line, the regression line of $X$ on $Y$ (see the section on the two regression lines).
3. Interpret slope and intercept in context. The intercept is the predicted $y$ when $x = 0$. If $x = 0$ is far outside the data, it may mean nothing.
The linear correlation coefficient between the number of weekly training hours ($X$) and the time recorded in an obstacle course ($Y$) is $r = -0.92$. The regression…
The linear correlation coefficient between the number of weekly training hours ($X$) and the time recorded in an obstacle course ($Y$) is $r = -0.92$. The regression line obtained is $y = -1.5x + 50$. The sample data corresponds to athletes who train between $5$ and $15$ hours. Estimate the time for an athlete who trains for $10$ hours and justify whether this prediction is considered reliable.
The regression line of the number of ice creams sold ($Y$) on the maximum daily temperature in $^\circ\text{C}$ ($X$) is $y = 15x - 120$. If a temperature of…
The regression line of the number of ice creams sold ($Y$) on the maximum daily temperature in $^\circ\text{C}$ ($X$) is $y = 15x - 120$. If a temperature of $30^\circ\text{C}$ is forecast for tomorrow, estimate how many ice creams will be sold. Furthermore, if on another day $255$ ice creams were sold, explain whether it is theoretically correct to use this same line to estimate the temperature it was, or if the rigorous procedure would require calculating a different line.
The relationship between the age of a car model ($X$, in years) and its value in the used car market ($Y$, in thousands of dollars) has been studied. The regression line…
The relationship between the age of a car model ($X$, in years) and its value in the used car market ($Y$, in thousands of dollars) has been studied. The regression line obtained is $y = -1.2x + 18$. If a car is $5$ years old, what is its estimated value? Furthermore, interpret what the y-intercept of the line means in this real-world context.
The correlation coefficient $r$
How well do the dots hug the line? The correlation coefficient gives a number for that.
Why is the sign of $r$ the same as the sign of the slope? The slope is $\sigma_{xy}/\sigma_x^2$ and $r = \sigma_{xy}/(\sigma_x\sigma_y)$. Variances and standard deviations are positive, so both have the same sign as the covariance $\sigma_{xy}$. This also means the sign of the slope always matches the sign of the covariance.
That tells you which graph supports a reliable prediction. With a tight cloud around the line (large $|r|$), the residuals are small and predictions are trustworthy. With a loose cloud (small $|r|$), the line is only a rough guide.
In a two-dimensional study, the equation of the regression line of $Y$ on $X$ is $y = 0.2x + 15$. Knowing that the covariance calculated for this data is…
In a two-dimensional study, the equation of the regression line of $Y$ on $X$ is $y = 0.2x + 15$. Knowing that the covariance calculated for this data is $\sigma_{xy} = 3.5$, briefly justify whether the slope of the obtained line is consistent with the sign of the covariance.
The relationship between two variables has been analyzed and the equation $y = -3x + 20$ has been obtained as the regression line of $Y$ on $X$. Is it possible that the…
The relationship between two variables has been analyzed and the equation $y = -3x + 20$ has been obtained as the regression line of $Y$ on $X$. Is it possible that the linear correlation coefficient of this distribution is $r = 0.85$? Justify your answer based on the relationship between the slope and the correlation.
Observe the two scatter plots, A and B, which show the relationship between two variables and include their respective regression lines. In which of the two…
Observe the two scatter plots, A and B, which show the relationship between two variables and include their respective regression lines. In which of the two distributions will it be more reliable to make an estimate of $Y$ from a value of $X$ using the regression line? Justify your answer based on the visual concept of correlation and dispersion.
From $r$ to the slope, and back
Since $\sigma_{xy} = r\,\sigma_x\sigma_y$, the slope can be written with $r$ instead of the covariance:
The slope is the correlation times the ratio of the spreads. Squares are for variances ($\sigma^2$), plain $\sigma$'s are for standard deviations. Use whichever the problem gives you.
You can also go backward and find $r$ from a given line: $r = m\,\dfrac{\sigma_x}{\sigma_y}$.
The following statistical parameters of a two-dimensional distribution are known: the correlation coefficient is $r = 0.8$, the standard deviation of $X$ is…
The following statistical parameters of a two-dimensional distribution are known: the correlation coefficient is $r = 0.8$, the standard deviation of $X$ is $\sigma_x = 2$, the standard deviation of $Y$ is $\sigma_y = 5$, and the center of gravity is the point $(12, 40)$. From this data, calculate the equation of the regression line of $Y$ on $X$.
In a laboratory test, the volume of a gas ($Y$) is measured at different temperatures ($X$). The regression line obtained after analyzing the data is $y = 0.4x + 22$.…
In a laboratory test, the volume of a gas ($Y$) is measured at different temperatures ($X$). The regression line obtained after analyzing the data is $y = 0.4x + 22$. Furthermore, it is known that the variance of the recorded temperatures is $\sigma_x^2 = 25$ and the variance of the volumes is $\sigma_y^2 = 5$. From this information, calculate the correlation coefficient $r$ and interpret whether the linear model is a good fit for this data.
Using a calculator or spreadsheet
In practice, nobody does $S_{xy}$ by hand with large data sets. A graphing calculator or Desmos does it in seconds.
Try this made-up data set: days practiced per week $x$ and correct answers out of $20$ on a quiz $y$: $(1, 12)$, $(2, 13)$, $(3, 15)$, $(4, 14)$, $(5, 16)$.
-
TI-84: press STAT → Edit and enter $x$ in L1 and $y$ in L2. Turn Diagnostics On (in the CATALOG), then press STAT → CALC →
LinReg(ax+b)L1, L2. You get $a = 0.9$, $b = 11.3$ and $r = 0.9$. -
Desmos: make a table with columns $x_1$ and $y_1$, then type
y_1 ~ m x_1 + b. Desmos reports the same $m$ and $b$ and also shows the residuals.
By hand we can confirm: $\bar{x} = 3$, $\bar{y} = 14$, $S_{xy} = 9$, $S_{xx} = 10$, so $m = 0.9$ and $b = 14 - 0.9(3) = 11.3$. The line is $y = 0.9x + 11.3$. For $4$ practice days the line predicts $0.9(4) + 11.3 = 14.9$ correct answers. That is an estimate, not a promise.
The two regression lines
So far we predicted $Y$ from $X$. We can also predict $X$ from $Y$. Swapping the roles gives the regression line of $X$ on $Y$:
$$x - \bar{x} = \frac{\sigma_{xy}}{\sigma_y^2}\,(y - \bar{y})$$Both lines pass through the mean point $(\bar{x}, \bar{y})$. If you are given both equations, solving them as a system (substitution) gives that point. The two slopes (written as $y = m_1 x + \cdots$ and $x = m_2 y + \cdots$) multiply to give $r^2$:
$$m_1 m_2 = \frac{\sigma_{xy}}{\sigma_x^2}\cdot\frac{\sigma_{xy}}{\sigma_y^2} = \frac{\sigma_{xy}^2}{\sigma_x^2\sigma_y^2} = r^2$$Why $r = \pm 1$ makes the two lines the same. When $r^2 = 1$, the product of the slopes is $1$. The line of $X$ on $Y$ is $x - \bar{x} = m_2 (y - \bar{y})$. Solving for $y$ gives $y - \bar{y} = \dfrac{1}{m_2}(x - \bar{x})$, whose slope is $\dfrac{1}{m_2}$. Because $m_1 m_2 = 1$, we have $\dfrac{1}{m_2} = m_1$. So both lines have the same slope and the same point $(\bar{x}, \bar{y})$, which makes them one line. This is the case where all the dots lie exactly on that line.
In a study on the length of an elastic spring ($Y$, in inches) when hanging different weights ($X$, in pounds) on it, the two regression lines have been calculated. The…
In a study on the length of an elastic spring ($Y$, in inches) when hanging different weights ($X$, in pounds) on it, the two regression lines have been calculated. The regression line of $Y$ on $X$ is $y = 2.5x + 10$ and the regression line of $X$ on $Y$ is $x = 0.39y - 3.8$. Calculate the exact coordinates of the center of gravity $(\bar{x}, \bar{y})$ of the distribution.
The equations of the two regression lines of a two-dimensional distribution are known: $y = 0.5x + 2$ (regression line of $Y$ on $X$) and $x = 1.28y - 1.56$ (regression…
The equations of the two regression lines of a two-dimensional distribution are known: $y = 0.5x + 2$ (regression line of $Y$ on $X$) and $x = 1.28y - 1.56$ (regression line of $X$ on $Y$). Calculate the linear correlation coefficient $r$, knowing that the correlation between both variables is positive.
Prove analytically that if the linear correlation coefficient between two variables is perfect ($r = 1$ or $r = -1$), the equation of the regression line of $Y$ on $X$…
Prove analytically that if the linear correlation coefficient between two variables is perfect ($r = 1$ or $r = -1$), the equation of the regression line of $Y$ on $X$ and the equation of the regression line of $X$ on $Y$ coincide and represent the same geometric line.
Changing the data: shifts and rescaling
What happens to the line when you change units or add bonus points? Just substitute.
Why? If every $y$ goes up by $c$, every predicted value goes up by $c$ too, so the whole line shifts up. If $x$ is measured in units that are $k$ times as large, a single unit of $z$ is a bigger step, so $y$ changes by $k$ times as much per unit. We then divide by $k$.
For example, if Mr. Lee adds $5$ bonus points to every quiz score and the line was $y = 0.8x + 10$, the new line is $y = 0.8x + 15$.
In a class, the regression line of the Physics grade ($Y$) on the Math grade ($X$) is $y = 0.9x + 0.5$. After reviewing the exams, the teacher detects a general error…
In a class, the regression line of the Physics grade ($Y$) on the Math grade ($X$) is $y = 0.9x + 0.5$. After reviewing the exams, the teacher detects a general error and decides to add $1$ extra point to all the Physics grades. What will be the new equation of the regression line of $Y$ on $X$? Justify your answer.
The regression line of $Y$ on $X$ of an experiment has the equation $y = ax + b$. If, due to a change of scale, we define a new variable $Z = 3X - 2$, determine what the…
The regression line of $Y$ on $X$ of an experiment has the equation $y = ax + b$. If, due to a change of scale, we define a new variable $Z = 3X - 2$, determine what the slope of the new regression line of $Y$ on $Z$ will be. Express the result in terms of the original parameter $a$.
Adding or removing a point at the mean
Some points have no influence on the slope. To see why, use sums. The $n$'s cancel in $m = S_{xy}/S_{xx}$, and there is a handy fact: because $\sum (x - \bar{x}) = 0$,
$$S_{xy} = \sum (x - \bar{x})(y - \bar{y}) = \sum (x - \bar{x})\,y.$$If the point added or removed is exactly $(\bar{x}, \bar{y})$, then nothing at all changes: the mean point stays the same and so does the line.
In a two-dimensional distribution formed by $N$ data points, the center of gravity is the point $(4, 6)$ and the slope of the regression line of $Y$ on $X$ is $1.5$. If…
In a two-dimensional distribution formed by $N$ data points, the center of gravity is the point $(4, 6)$ and the slope of the regression line of $Y$ on $X$ is $1.5$. If a new data point that coincides exactly with the point $(4, 10)$ is added to the study, will the value of the slope of the regression line change? Prove your answer mathematically by analyzing how the formulas for covariance and variance are modified.
We have a two-dimensional distribution with $100$ data points. The mean of $X$ is $50$ and the mean of $Y$ is $80$. The covariance is $120$ and the variance of $X$ is…
We have a two-dimensional distribution with $100$ data points. The mean of $X$ is $50$ and the mean of $Y$ is $80$. The covariance is $120$ and the variance of $X$ is $60$. After a quality review, it is discovered that one of the data pairs, specifically the point $(50, 80)$, was recorded incorrectly due to a sensor failure and it is decided to remove it from the study. Calculate the new equation of the regression line of $Y$ on $X$ for the remaining $99$ data points and compare it with the original line.
Two special lines
Standardized variables. Suppose both variables have been standardized: $\bar{x} = \bar{y} = 0$ and $\sigma_x = \sigma_y = 1$. Then $\sigma_{xy} = r\sigma_x\sigma_y = r$, and the regression line $y - \bar{y} = r\dfrac{\sigma_y}{\sigma_x}(x - \bar{x})$ simplifies to
$$y = r\,x.$$If the variables are uncorrelated ($r = 0$), the line is $y = 0$: the horizontal $x$-axis.
A horizontal regression line. The slope is $\dfrac{\sigma_{xy}}{\sigma_x^2}$. If the line is horizontal, the slope is $0$, so $\sigma_{xy} = 0$ (since $\sigma_x^2 > 0$). Then $r = 0$ and the line is $y = \bar{y}$. Knowing $x$ does not improve the prediction of $y$: we always predict the mean. We conclude there is no linear relationship, though a curved one could still exist.
Two statistical variables $X$ and $Y$ have been standardized, which means that their means are $\bar{x} = 0$ and $\bar{y} = 0$, and their standard deviations are…
Two statistical variables $X$ and $Y$ have been standardized, which means that their means are $\bar{x} = 0$ and $\bar{y} = 0$, and their standard deviations are $\sigma_x = 1$ and $\sigma_y = 1$. Express the equation of the regression line of $Y$ on $X$ using only the correlation coefficient $r$. What geometric shape does the line take if both variables are linearly independent?
In the analysis of a data set, it is observed that the regression line of $Y$ on $X$ is completely horizontal. What exact numerical value does the covariance…
In the analysis of a data set, it is observed that the regression line of $Y$ on $X$ is completely horizontal. What exact numerical value does the covariance $\sigma_{xy}$ have in this case? What statistical conclusion can we draw about the linear relationship between the variables $X$ and $Y$? Justify your answer using the general equation of the regression line.
Correlation is not causation
A value of $r$ near $1$ says two variables move together very regularly. It does not say why.
On hot days more people buy ice cream, and more people get sunburned. The two have a high correlation, but ice cream does not burn anyone's skin. A third factor, the lurking variable (weather), drives both.
Back to Jayden: his teacher's line predicts $4(7) + 56 = 84$ for 7 hours of study. The prediction is reasonable because $7$ hours lies inside the data range and the dots hug the line. It is an estimate, not a guarantee. A strong correlation here does not prove that studying alone causes the score; things like prior knowledge and sleep matter too.