Chapter 14 ยท Statistics and data analysis

Fit a Line and Write Its Equation

Draw a line of best fit by eye so it runs close to all the points, then use two exact points on your line to write its equation.

16 min read

Earlier in this topic, you judged which line fits a scatter plot best. Now you will fit a line yourself and then write its equation.

A scatter plot shows paired measurements as points. When the points fall roughly along a straight path, the data have a linear trend, and a straight line can model it. A line of best fit is a straight line that follows that trend closely. You fit it informally, by eye, so it is an approximate model of the data. You judge it by how close it runs to all the points, not by whether it passes through them. It may pass through a few points or through none. Two careful people may draw slightly different lines that both fit well.

Here is a data set to fit. On a clear winter day, a weather station recorded the air temperature once an hour. Let xx be the number of hours after 6 a.m., and let yy be the temperature in degrees Celsius (ยฐC). The readings run from 6 a.m. (x=0x = 0) to 2 p.m. (x=8x = 8), so a line fitted to them describes the data only for xx from 00 to 88. Below freezing, Celsius temperatures are negative, so yy can be negative or positive. Call this day Day 1. The table lists the readings. The scatter plot after it shows them, with xx on the horizontal axis and yy on the vertical axis.

Day 1 temperature readings
Hours after 6 a.m., xxTemperature in ยฐC, yy
00
โˆ’7-7
11
โˆ’4-4
22
โˆ’2-2
33
โˆ’2-2
44
00
55
22
66
44
77
44
88
55
Coordinate grid from x -1 to 9 and y -8 to 8, with a grid step of 1, showing 12 plotted elements. Details: point at (0, -7); point at (1, -4); point at (2, -2); point at (3, -2); point at (4, 0); point at (5, 2); point at (6, 4); point at (7, 4); point at (8, 5); line 3 x plus -2 y equals 12, named Line A; point at (0, -6), named (0, -6); point at (8, 6), named (8, 6).
The nine dots without labels are the Day 1 readings from the table. The points marked (0,โˆ’6)(0, -6) and (8,6)(8, 6) lie on Line A. They are not readings.

Worked example

Fit a line to the Day 1 data

Fit a straight line to the Day 1 scatter plot above. The horizontal axis is xx (hours after 6 a.m.) and the vertical axis is yy (temperature in ยฐC). Then check how well your line fits.

  1. Check that a straight line makes sense. From x = 0 to x = 8 the temperatures climb from -7 to 5. The points rise along a roughly straight path rather than a curve, so a straight line can model this linear trend.

  2. Aim at the whole cloud of points. Imagine laying a ruler so it rises as the points do. Then slide it to the middle of the cloud, so the points sit close to it on both sides. To keep the line exact, settle on one that passes through grid points, where grid lines cross. Line A passes through (0, -6) and (8, 6).

  3. Find Line A's height (its y-value) at each hour. It rises 12 degrees over 8 hours, or 3 degrees every 2 hours, so it passes through (2, -3), (4, 0) and (6, 3). A straight line changes height at a steady rate. So at each odd hour it is halfway between the heights an hour before and after: -4.5 at x = 1, then -1.5, 1.5 and 4.5 at x = 3, 5 and 7.

    โˆ’6+(โˆ’3)2=โˆ’92=โˆ’4.5\frac{-6 + (-3)}{2} = \frac{-9}{2} = -4.5
  4. Measure each gap: the temperature minus the line's height at that hour. A positive gap means the point is above the line, and a negative gap means it is below. From x = 0 to x = 8 the gaps are -1, 0.5, 1, -0.5, 0, 0.5, 1, -0.5 and -1.

    โˆ’7โˆ’(โˆ’6)=โˆ’1,โˆ’4โˆ’(โˆ’4.5)=0.5,โˆ’2โˆ’(โˆ’3)=1-7 - (-6) = -1, \quad -4 - (-4.5) = 0.5, \quad -2 - (-3) = 1
  5. Judge the fit. Every gap is between -1 and 1, so every reading lies within 1 degree of Line A. Four points are above it and four are below. One point, (4, 0), happens to lie on it. The points stay close on both sides, so Line A is a reasonable fit, though not the only possible one.

Answer

Line A, through (0,โˆ’6)(0, -6) and (8,6)(8, 6), is a reasonable line of best fit for Day 1, because every reading lies within 11 degree of it.

Coordinate grid from x -1 to 9 and y -8 to 8, with a grid step of 1, showing 11 plotted elements. Details: point at (0, -7); point at (1, -4); point at (2, -2); point at (3, -2); point at (4, 0); point at (5, 2); point at (6, 4); point at (7, 4); point at (8, 5); line 3 x plus -2 y equals 12, named Line A; line 3 x plus -2 y equals 14, named Line B.
Line B joins the first reading (0,โˆ’7)(0, -7) and the last reading (8,5)(8, 5), and the other seven points all lie above it. Line A runs through the middle of the points.

Once you have chosen a line, you can write its equation. The equation must describe the line you drew, not the data. Data points usually sit off your line, and a line through two points that are off it is a different line.

  • The slope mm is the change in yy divided by the change in xx between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) on the line. The points need different xx-values, so you never divide by zero. A line that rises from left to right has a positive slope, and one that falls has a negative slope.
  • The yy-intercept bb is the value of yy where the line crosses the yy-axis, that is, where x=0x = 0.

Every line that is not vertical has an equation y=mx+by = mx + b. At x=0x = 0 it gives y=by = b, and each increase of 11 in xx changes yy by mm. Grid points on your line give you exact coordinates to use.

Key concept

An approximate fit, an exact line

A line of best fit is a straight line you choose by eye to follow a linear trend. You judge it by how close it runs to all the plotted points, and it need not pass through any of them. Having points fall roughly evenly on both sides is a helpful guide, not a rule: a line tilted the wrong way can still split the points evenly. The fit is approximate, but the line you choose is exact. Use two points on it to find mm and bb, and write y=mx+by = mx + b. The line describes how the two quantities change together. It does not show that one causes the other.

m=y2โˆ’y1x2โˆ’x1โ€…โ€Šโ€…โ€Š(x2โ‰ x1),y=mx+bm = \frac{y_2 - y_1}{x_2 - x_1} \;\; (x_2 \neq x_1), \qquad y = mx + b

Worked example

Write the equation of Line A

Write an equation for Line A, the Day 1 line of best fit through the grid points (0,โˆ’6)(0, -6) and (8,6)(8, 6).

  1. Choose two points on the line itself. Use (0, -6) and (8, 6): both lie exactly on Line A, and their x-values differ. The readings at those hours, (0, -7) and (8, 5), are off Line A. Using them would give the equation of Line B instead.

  2. Find the slope, subtracting in the same order on top and bottom. Subtracting -6 is the same as adding 6. The bottom is 8, not 0, so the division is defined. Dividing the top and bottom of 12/8 by 4 keeps its value. The result matches the 3 degrees every 2 hours you found while fitting.

    m=6โˆ’(โˆ’6)8โˆ’0=6+68=128=32=1.5m = \frac{6 - (-6)}{8 - 0} = \frac{6 + 6}{8} = \frac{12}{8} = \frac{3}{2} = 1.5
  3. Find the y-intercept. Line A crosses the y-axis exactly at the grid point (0, -6), where x = 0, so b = -6.

    b=โˆ’6b = -6
  4. Substitute m = 1.5 and b = -6 into y = mx + b. Adding -6 is the same as subtracting 6.

    y=1.5x+(โˆ’6)=1.5xโˆ’6y = 1.5x + (-6) = 1.5x - 6
  5. Check with a third point on Line A, the grid point (6, 3). Substituting x = 6 should give y = 3. It does, so the equation matches the line.

    1.5(6)โˆ’6=9โˆ’6=31.5(6) - 6 = 9 - 6 = 3

Answer

An equation for Line A is y=1.5xโˆ’6y = 1.5x - 6, where xx is hours after 6 a.m. (from 00 to 88) and yy is the temperature in ยฐC.

On Day 2, a cold front arrived at the same station, and the temperature fell instead. The variables are the same. The horizontal axis shows xx, hours after 6 a.m., from 00 to 88. The vertical axis shows yy, the temperature in ยฐC. The readings (x,y)(x, y) were (0,6)(0, 6), (1,3)(1, 3), (2,3)(2, 3), (3,2)(3, 2), (4,โˆ’1)(4, -1), (5,โˆ’3)(5, -3), (6,โˆ’3)(6, -3), (7,โˆ’4)(7, -4) and (8,โˆ’7)(8, -7).

Time passed in the same way on both days, yet the temperature rose on one day and fell on the other. A trend between two quantities does not show that one causes the other. Here the weather, not the clock, changed the temperature.

Coordinate grid from x -1 to 9 and y -8 to 8, with a grid step of 1, showing 12 plotted elements. Details: point at (0, 6); point at (1, 3); point at (2, 3); point at (3, 2); point at (4, -1); point at (5, -3); point at (6, -3); point at (7, -4); point at (8, -7); line 3 x plus 2 y equals 11, named Line C; point at (1, 4), named (1, 4); point at (7, -5), named (7, -5).
The nine dots without labels are the Day 2 readings. The points marked (1,4)(1, 4) and (7,โˆ’5)(7, -5) lie on Line C, which crosses the yy-axis between 55 and 66.

Worked example

Fit a line to Day 2 and write its equation

Fit a line to the Day 2 scatter plot above, then write an equation for your line.

  1. Fit the line first. The points fall from left to right along a roughly straight path, so tilt the ruler downward and slide it to the middle of the cloud. Line C passes through the grid points (1, 4) and (7, -5), and it runs close to the points on both sides.

  2. Find the slope from those two points on the line. The line falls, so expect a negative slope. The x-values differ by 6, not 0.

    m=โˆ’5โˆ’47โˆ’1=โˆ’96=โˆ’32=โˆ’1.5m = \frac{-5 - 4}{7 - 1} = \frac{-9}{6} = -\frac{3}{2} = -1.5
  3. Find b. Line C crosses the y-axis between grid lines, so reading b from the picture would give only an estimate. Every point on the line makes its equation true. So substitute m = -1.5 and the point (1, 4) into y = mx + b.

    4=โˆ’1.5(1)+bโ‡’4=โˆ’1.5+b4 = -1.5(1) + b \quad\Rightarrow\quad 4 = -1.5 + b
  4. Add 1.5 to both sides. Adding the same number to both sides keeps them equal, and it leaves b by itself.

    4+1.5=โˆ’1.5+b+1.5โ‡’5.5=b4 + 1.5 = -1.5 + b + 1.5 \quad\Rightarrow\quad 5.5 = b
  5. Write the equation, then check it with the other point, (7, -5). Substituting x = 7 gives -5, so the equation describes Line C exactly.

    y=โˆ’1.5x+5.5,โˆ’1.5(7)+5.5=โˆ’10.5+5.5=โˆ’5y = -1.5x + 5.5, \qquad -1.5(7) + 5.5 = -10.5 + 5.5 = -5
  6. Use the equation to check the fit. Substituting x = 0 to 8 gives the line's heights: 5.5, 4, 2.5, 1, -0.5, -2, -3.5, -5 and -6.5. The gaps from the readings are 0.5, -1, 0.5, 1, -0.5, -1, 0.5, 1 and -0.5. Every reading is within 1 degree, with five points above and four below.

    6โˆ’5.5=0.5,3โˆ’4=โˆ’1,โˆ’7โˆ’(โˆ’6.5)=โˆ’0.56 - 5.5 = 0.5, \quad 3 - 4 = -1, \quad -7 - (-6.5) = -0.5

Answer

Line C is a reasonable line of best fit for Day 2, and an equation for it is y=โˆ’1.5x+5.5y = -1.5x + 5.5, with xx in hours after 6 a.m. (from 00 to 88) and yy in ยฐC.

To fit a line, aim it along the trend of the whole cloud and check that the points sit close to it. Points roughly on both sides are a helpful guide. The fit is approximate, and the line shows a trend, not a cause. Once you choose a line through two exact points, though, its equation y=mx+by = mx + b is exact.