Skip to main content
\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)
Mathematics LibreTexts

4.3: Fitting Linear Models to Data

Skills to Develop

  • Draw and interpret scatter diagrams.
  • Use a graphing utility to find the line of best fit.
  • Distinguish between linear and nonlinear relations.
  • Fit a regression line to a set of data and use the linear model to make predictions.

 A professor is attempting to identify trends among final exam scores. His class has a mixture of students, so he wonders if there is any relationship between age and final exam scores. One way for him to analyze the scores is by creating a diagram that relates the age of each student to the exam score received. In this section, we will examine one such diagram known as a scatter plot.

Drawing and Interpreting Scatter Plots

A scatter plot is a graph of plotted points that may show a relationship between two sets of data. If the relationship is from a linear model, or a model that is nearly linear, the professor can draw conclusions using his knowledge of linear functions. Figure 4.3.1 shows a sample scatter plot.

Scatter plot, titled 'Final Exam Score VS Age'. The x-axis is the age, and the y-axis is the final exam score. The range of ages are between 20s - 50s, and the range for scores are between upper 50s and 90s.

Figure 4.3.1 A scatter plot of age and final exam score variables.

Notice this scatter plot does not indicate a linear relationship. The points do not appear to follow a trend. In other words, there does not appear to be a relationship between the age of the student and the score on the final exam.

Example

Using a Scatter Plot to Investigate Cricket Chirps

Table 4.3.1 shows the number of cricket chirps in 15 seconds, for several different air temperatures, in degrees Fahrenheit1. Plot this data, and determine whether the data appears to be linearly related.

Cricket Chirps vs Air Temperature
Chirps 44 35 20.4 33 31 35 18.5 37 26
Temperature 80.5 70.5 57 66 68 72 52 73.5 53

Table 4.3.1

Solution:

Plotting this data, as depicted in Figure 4.3.2 suggests that there may be a trend. We can see from the trend in the data that the number of chirps increases as the temperature increases. The trend appears to be roughly linear, though certainly not perfectly so.

Scatter plot, titled 'Cricket Chirps vs. Air Temperature'. The x-axis is the Cricket Chirps in 15 Seconds, and the y-axis is the Temperature (F). The line regression is generally positive.

Figure 4.3.2

Finding the Line of Best Fit

Once we recognize a need for a linear function to model that data, the natural follow-up question is “what is that linear function?” One way to approximate our linear function is to sketch the line that seems to best fit the data. Then we can extend the line until we can verify the y-intercept. We can approximate the slope of the line by extending it until we can estimate the \(\dfrac{rise}{run}\) .

Example

Finding a Line of Best Fit

Find a linear function that fits the data in Table 4.3.1 by “eyeballing” a line that seems to fit.

Solution

On a graph, we could try sketching a line. Using the starting and ending points of our hand drawn line, points (0, 30) and (50, 90), this graph has a slope of

\[m=\dfrac{60}{50}=1.2\]

and a y-intercept at 30. This gives an equation of

\[T(c)=1.2c+30\]

where \(c\) is the number of chirps in 15 seconds, and \(T(c)\) is the temperature in degrees Fahrenheit. The resulting equation is represented in Figure 4.3.3.

Scatter plot, showing the line of best fit: T(c) = 1.2c + 30. It is titled 'Cricket Chirps Vs Air Temperature'. The x-axis is 'c, Number of Chirps', and the y-axis is 'T(c), Temperature (F)'.

Figure 4.3.3

Analysis

This linear equation can then be used to approximate answers to various questions we might ask about the trend.

Recognizing Interpolation or Extrapolation

While the data for most examples does not fall perfectly on the line, the equation is our best guess as to how the relationship will behave outside of the values for which we have data. We use a process known as interpolation when we predict a value inside the domain and range of the data. The process of extrapolation is used when we predict a value outside the domain and range of the data.

Figure 4.3.4 compares the two processes for the cricket-chirp data addressed in Example. We can see that interpolation would occur if we used our model to predict temperature when the values for chirps are between 18.5 and 44. Extrapolation would occur if we used our model to predict temperature when the values for chirps are less than 18.5 or greater than 44.

There is a difference between making predictions inside the domain and range of values for which we have data and outside that domain and range. Predicting a value outside of the domain and range has its limitations. When our model no longer applies after a certain point, it is sometimes called model breakdown. For example, predicting a cost function for a period of two years may involve examining the data where the input is the time in years and the output is the cost. But if we try to extrapolate a cost when \(x=50\), that is in 50 years, the model would not apply because we could not account for factors fifty years in the future.

Scatter plot, showing the line of best fit. It is titled 'Cricket Chirps Vs Air Temperature'. The x-axis is 'c, Number of Chirps', and the y-axis is 'T(c), Temperature (F)'.  The area around the scattered points is enclosed in a box labeled: Interpolation.  The area outside of this box is labeled: Extrapolation.

Figure 4.3.4 Interpolation occurs within the domain and range of the provided data whereas extrapolation occurs outside.

A General Note

INTERPOLATION AND EXTRAPOLATION
  • Different methods of making predictions are used to analyze data.
  • The method of interpolation involves predicting a value inside the domain and/or range of the data.
  • The method of extrapolation involves predicting a value outside the domain and/or range of the data.
  • Model breakdown occurs at the point when the model no longer applies.

Example

Understanding Interpolation and Extrapolation

Use the cricket data from Table 4.3.1 to answer the following questions:

a. Would predicting the temperature when crickets are chirping 30 times in 15 seconds be interpolation or extrapolation? Make the     prediction, and discuss whether it is reasonable.

b. Would predicting the number of chirps crickets will make at 40 degrees be interpolation or extrapolation? Make the prediction, and     discuss whether it is reasonable.

Solution

The number of chirps in the data provided varied from 18.5 to 44. A prediction at 30 chirps per 15 seconds is inside the domain of our data, so would be interpolation. Using our model:

\[T (30)=30 + 1.2(30)\]

\[=66\space degrees\]

Based on the data we have, this value seems reasonable.

The temperature values varied from 52 to 80.5. Predicting the number of chirps at 40 degrees is extrapolation because 40 is outside the range of our data. Using our model:

\[40=30+1.2c\]

\[10=1.2c\]

\[c≈8.33\]

We can compare the regions of interpolation and extrapolation using Figure 4.3.5.

Scatter plot, showing the line of best fit and where interpolation and extrapolation occurs. It is titled 'Cricket Chirps vs. Air Temperature'. The x-axis is 'c, Number of Chirps', and the y-axis is 'T(c), Temperature (F)'.  An additional point is plotted inside of the box to represent an interpolated point.  There is another additional point plotted outside of the box to represent an extrapolated point.

Figure 4.3.5

Analysis

Our model predicts the crickets would chirp 8.33 times in 15 seconds. While this might be possible, we have no reason to believe our model is valid outside the domain and range. In fact, generally crickets stop chirping altogether below around 50 degrees.

Exercise

According to the data from Table 4.3.1, what temperature can we predict it is if we counted 20 chirps in 15 seconds?

Solution:

\(54°\)

Finding the Line of Best Fit Using a Graphing Utility

While eyeballing a line works reasonably well, there are statistical techniques for fitting a line to data that minimize the differences between the line and data values2. One such technique is called least squares regression and can be computed by many graphing calculators, spreadsheet software, statistical software, and many web-based calculators3. Least squares regression is one means to determine the line that best fits the data, and here we will refer to this method as linear regression.

How to

Given data of input and corresponding outputs from a linear function, find the best fit line using linear regression.

  1. Enter the input in List 1 (L1).
  2. Enter the output in List 2 (L2).
  3. On a graphing utility, select Linear Regression (LinReg).

Example

Finding a Least Squares Regression Line

Find the least squares regression line using the cricket-chirp data in Table 4.3.2.

Solution:

1. Enter the input (chirps) in List 1 (L1).

2. Enter the output (temperature) in List 2 (L2). See Table 4.3.2.

L1 44 35 20.4 33 31 35 18.5 37 26
L2 80.5 70.5 57 66 68 72 52 73.5 53

Table 4.3.2

3. On a graphing utility, select Linear Regression (LinReg). Using the cricket chirp data from earlier, with technology we obtain the     equation:

\[T(c)=30.281+1.143c\]

Analysis

Notice that this line is quite similar to the equation we “eyeballed” but should fit the data better. Notice also that using this equation would change our prediction for the temperature when hearing 30 chirps in 15 seconds from 66 degrees to:

\[T(30)=30.281+1.143(30)\]

\[=64.571\]        

\[≈64.6\space degrees\]

The graph of the scatter plot with the least squares regression line is shown in Figure 4.3.6.

Scatter plot, showing the line of best fit: T(c) = 30.281 + 1.143c. It is titled 'Cricket Chirps vs. Air Temperature'. The x-axis is 'c, Number of Chirps', and the y-axis is 'T(c), Temperature (F)'.

Figure 4.3.6

Q&A

Will there ever be a case where two different lines will serve as the best fit for the data?

No. There is only one best fit line.

Distinguishing Between Linear and Nonlinear Models

As we saw above with the cricket-chirp model, some data exhibit strong linear trends, but other data, like the final exam scores plotted by age, are clearly nonlinear. Most calculators and computer software can also provide us with the correlation coefficient, which is a measure of how closely the line fits the data. Many graphing calculators require the user to turn a ”diagnostic on” selection to find the correlation coefficient, which mathematicians label as r The correlation coefficient provides an easy way to get an idea of how close to a line the data falls.

We should compute the correlation coefficient only for data that follows a linear pattern or to determine the degree to which a data set is linear. If the data exhibits a nonlinear pattern, the correlation coefficient for a linear regression is meaningless. To get a sense for the relationship between the value of r and the graph of the data, Figure 4.3.7 shows some large data sets with their correlation coefficients. Remember, for all plots, the horizontal axis shows the input and the vertical axis shows the output.

Correlation coefficients values range from -1.0 - 1.0.  Collections of dots representing an example of each kind of correlation coefficient are plotted underneath them.  The closer to 1.0 the more the points are grouped tightly to form a line in the positive direction.  The closer to -1.0 the more the points are grouped tightly to form a line in the negative direction.  The closer to 0 the points are very scattered and do not form a line.  Several shapes are displayed at the bottom row, none of which are lines, but all of them have values of 0.

Figure 4.3.7 Plotted data and related correlation coefficients. (credit: “DenisBoigelot,” Wikimedia Commons)

Correlation Coefficient

The correlation coefficient is a value, r, between –1 and 1.

  • \(r>0\) suggests a positive (increasing) relationship
  • \(r<0\) suggests a negative (decreasing) relationship
  • The closer the value is to 0, the more scattered the data.
  • The closer the value is to 1 or –1, the less scattered the data is.

Example

Finding a Correlation Coefficient

Calculate the correlation coefficient for cricket-chirp data in Table 4.3.1.

Solution:

Because the data appear to follow a linear pattern, we can use technology to calculate r Enter the inputs and corresponding outputs and select the Linear Regression. The calculator will also provide you with the correlation coefficient, \(r=0.9509\). This value is very close to 1, which suggests a strong increasing linear relationship.

Note: For some calculators, the Diagnostics must be turned "on" in order to get the correlation coefficient when linear regression is performed: [2nd]>[0]>[alpha][x–1], then scroll to DIAGNOSTICSON.

Fitting a Regression Line to a Set of Data

Once we determine that a set of data is linear using the correlation coefficient, we can use the regression line to make predictions. As we learned above, a regression line is a line that is closest to the data in the scatter plot, which means that only one such line is a best fit for the data.

Example

Using a Regression Line to Make Predictions

Gasoline consumption in the United States has been steadily increasing. Consumption data from 1994 to 2004 is shown in Table.4 Determine whether the trend is linear, and if so, find a model for the data. Use the model to predict the consumption in 2008.

Year '94 '95 '96 '97 '98 '99 '00 '01 '02 '03 '04
Consumption (billions of gallons) 113 116 118 119 123 125 126 128 131 133 136

The scatter plot of the data, including the least squares regression line, is shown in Figure 4.3.8.

Scatter plot, showing the line of best fit. It is titled 'Gas Consumption VS Year'. The x-axis is 'Year After 1994', and the y-axis is 'Gas Consumption (billions of gallons)'. The points are strongly positively correlated and the line of best fit goes through most of the points completely.

Figure 4.3.8

Solution:

We can introduce new input variable, \(t\), representing years since 1994.

The least squares regression equation is:

\[C(t)=113.318+2.209t\]

Using technology, the correlation coefficient was calculated to be 0.9965, suggesting a very strong increasing linear trend.

Using this to predict consumption in 2008 \((t=14)\),

\[C(14)=113.318+2.209(14)\]         
\[=144.244\]

The model predicts 144.244 billion gallons of gasoline consumption in 2008.

Exercise

Use the model we created using technology in Example to predict the gas consumption in 2011. Is this an interpolation or an extrapolation?

Solution:

150.871 billion gallons; extrapolation

Media

Access these online resources for additional instruction and practice with fitting linear models to data.

Key Concepts

  • Scatter plots show the relationship between two sets of data. See Example.
  • Scatter plots may represent linear or non-linear models.
  • The line of best fit may be estimated or calculated, using a calculator or statistical software. See Example.
  • Interpolation can be used to predict values inside the domain and range of the data, whereas extrapolation can be used to predict values outside the domain and range of the data. See Example.
  • The correlation coefficient, \(r\), indicates the degree of linear relationship between data. See Example.
  • A regression line best fits the data. See Example.
  • The least squares regression line is found by minimizing the squares of the distances of points from a line passing through the data and may be used to make predictions regarding either of the variables. See Example.