Linear regression in 3D

Orange spheres scattered along three axes above a white base, with a straight dark line fitted through the points.Drag to turn

A scatter plot shows paired measurements as points on two axes. Linear regression fits a straight line to those points, usually by least squares, to describe and predict how one variable changes with the other.

What a scatter plot shows

A scatter plot places one point for each observation. The horizontal axis holds the explanatory variable x, and the vertical axis holds the response variable y. The pattern of the points shows the direction, form and strength of the relationship, and it also shows outliers.

A cloud rising from left to right shows a positive association. A falling cloud shows a negative one. Points hugging a line show a strong linear relationship, while a wide blob shows a weak one.

How the least squares line is fitted

The regression line has the form y-hat = a + bx, where b is the slope and a is the intercept. A residual is the vertical gap between an observed y and the value y-hat on the line. Least squares chooses a and b so that the sum of the squared residuals is as small as possible.

The slope is b = Sxy / Sxx, where Sxy is the sum of (x minus the mean of x) times (y minus the mean of y), and Sxx is the sum of squared x deviations. The intercept is a = mean of y minus b times mean of x. The correlation coefficient r runs from -1 to 1 and measures linear strength. Its square, r squared, is the share of the variation in y accounted for by the line.

Worked example with four points

Take the points (1, 2), (2, 3), (3, 5) and (4, 6). The mean of x is 10/4 = 2.5 and the mean of y is 16/4 = 4. The x deviations are -1.5, -0.5, 0.5, 1.5 and the y deviations are -2, -1, 1, 2.

Sxy = 3 + 0.5 + 0.5 + 3 = 7. Sxx = 2.25 + 0.25 + 0.25 + 2.25 = 5. The slope is 7/5 = 1.4, and the intercept is 4 - 1.4 x 2.5 = 0.5. The line is y-hat = 0.5 + 1.4x, so at x = 3.5, inside the observed range, the prediction is 0.5 + 1.4 x 3.5 = 0.5 + 4.9 = 5.4.

Syy = 4 + 1 + 1 + 4 = 10, so r = 7 / sqrt(5 x 10) = 7/7.07, which is about 0.99. Then r squared = 49/50 = 0.98, so the line accounts for 98 percent of the variation in y in this small data set.

Common misconceptions

Correlation does not establish causation. Two variables can move together because of a third variable, or by coincidence. A high r shows association only.

A correlation near zero does not mean there is no relationship. A curved pattern can have r close to 0 while the variables are strongly linked. Plotting the data first reveals this.

Predictions outside the range of observed x values, called extrapolation, are unreliable because the trend may not continue. A prediction at x = 5 in the example above would be extrapolation. A single outlier can also pull the line noticeably, especially in a small sample.

Where scatter plots and regression appear

Scientists use them to relate measurements such as dose and response. Economists relate income to spending, and engineers calibrate instruments against known values. Fields that study how one quantity varies with another often use these tools as a first step before more complex models.

Key terms

Scatter plot
A graph of paired data in which each observation is one point.
Explanatory variable
The variable x used to predict or explain the response.
Response variable
The variable y whose value is predicted from x.
Residual
The observed y minus the y value predicted by the line.
Least squares
The method that picks the line with the smallest sum of squared residuals.
Correlation coefficient
The number r, between -1 and 1, measuring the strength and direction of a linear relationship.
Extrapolation
Predicting for x values outside the range of the data.

Common questions

Why are residuals squared in least squares?

Squaring stops positive and negative residuals from cancelling and gives larger misses more weight. It also leads to simple formulas for the slope and intercept.

What does the slope mean in the example?

The slope of 1.4 means the line predicts y rises by 1.4 units for each one unit increase in x, within the range of the data.

Does a high r squared prove the line is a good model?

No. A high r squared shows the line fits the sample well, but a residual plot may still show curvature, and outliers can inflate the value.

Learn this with a tutor

Sidechair's AI tutor will teach this topic in 3D. Join the waitlist for early access.

We'll only email you about Sidechair. Read our privacy policy.