Linear regression calculator (r, r-squared, line)

By Jude Wallis · Published

A correlation and regression calculator takes your x,y pairs and returns the correlation coefficient r, r-squared, and the least-squares line y-hat = a + bx, plus each residual. Enter the pairs, then read r, the line, and how far each observed value sits from its prediction.

least-squares regression line

y-hat = 1.3 + 0.9x

r = 0.9, r² = 0.81: about 81% of the variation in y is explained by the linear relationship with x.

xypredictedresidual
122.2-0.2
233.1-0.1
3541
444.9-0.9
565.80.2

Steps

  1. 1.n = 5; x-bar = 3, y-bar = 4
  2. 2.sx = 1.5811, sy = 1.5811, r = 0.9
  3. 3.slope b = r(sy/sx) = 0.9 x (1.5811/1.5811) = 0.9
  4. 4.intercept a = y-bar - b(x-bar) = 4 - 0.9 x 3 = 1.3

AP Statistics: Unit 5 (topics 5.2 Correlation, 5.3 Linear Regression Models, 5.4 Residuals, 5.5 Least-Squares Regression). This calculator supports Fall 2026 Unit 5 (topics 5.2 to 5.5), where you use technology to find r, r-squared, and the least-squares line and then interpret them in context, while inference for the regression slope is not part of the Fall 2026 exam.

What the linear regression calculator gives you

This linear regression calculator and correlation coefficient calculator work on the same input: a list of paired values, one xx and one yy for each observation. Here xx is the explanatory variable (the predictor) and yy is the response variable (what you want to predict).

From your pairs the tool returns four things. The correlation coefficient rr measures the strength and direction of the linear association. The coefficient of determination r2r^2 (r-squared) is the proportion of the variation in yy explained by the linear relationship with xx.

The least-squares line y^=a+bx\hat{y} = a + bx is the best-fit line, where y^\hat{y} (read "y-hat") is the predicted response, aa is the y-intercept, and bb is the slope. Finally, the residual for each point is how far the observed yy sits above or below the line.

The correlation coefficient rr is always between 1-1 and 11, inclusive, and it is unit-free, so changing units does not change it.

The formulas behind r, the line, and residuals

Start with the means. Write xˉ\bar{x} ("x-bar") for the mean of the xx values and yˉ\bar{y} ("y-bar") for the mean of the yy values. Write sxs_x and sys_y for the sample standard deviations of xx and yy, each computed with n1n-1 in the denominator.

The correlation coefficient uses the paired deviations from the means:

r=(xixˉ)(yiyˉ)(xixˉ)2  (yiyˉ)2r = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sqrt{\sum (x_i - \bar{x})^2 \; \sum (y_i - \bar{y})^2}}

The slope and intercept of the least-squares line follow from rr and the spreads:

b=rsysxa=yˉbxˉb = r\,\frac{s_y}{s_x} \qquad a = \bar{y} - b\bar{x}

Because a=yˉbxˉa = \bar{y} - b\bar{x}, the line always passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}). The residual for a single point is the observed value minus the predicted value:

residual=yy^\text{residual} = y - \hat{y}

A positive residual means the model underpredicts that point, and a negative residual means it overpredicts.

How to use the calculator

  1. Paste or type your data as x,yx,y pairs, one pair per line or in two columns.
  2. Read the correlation coefficient rr and the coefficient of determination r2r^2.
  3. Read the least-squares line in the form y^=a+bx\hat{y} = a + bx, with the slope bb and intercept aa filled in.
  4. Check the residual for any point, or plug an xx value into the line to get a predicted y^\hat{y}.

Keep predictions inside the range of xx values you entered. Predicting for an xx beyond that range is extrapolation, and the estimate gets less reliable the further out you go. Predicting for an xx inside the range is interpolation. The worked examples below reproduce every number by hand so you can check the tool or redo it on paper.

How to read r and r-squared

The sign of rr gives direction and the size gives strength. A value near 11 is a strong positive linear association, a value near 1-1 is a strong negative linear association, and r=0r = 0 means no linear association. Values of r=1r = 1 or r=1r = -1 mean a perfect linear association.

Use this template for rr: "There is a [strong or moderate or weak] [positive or negative] linear association between [xx in context] and [yy in context]."

Use this template for r2r^2: "About [r2r^2 as a percent] of the variation in [yy in context] is explained by the linear relationship with [xx in context]."

Two cautions. A value of rr close to 11 or 1-1 does not by itself mean a linear model is appropriate, so you still check a residual plot. And a strong correlation does not mean xx causes yy, because correlation does not imply causation.

How to read the slope, intercept, and residuals

Interpret the slope bb with a rate of change: "For each additional 1 unit of [xx in context], the predicted [yy in context] changes by bb units." Use "increases" when bb is positive and "decreases" when bb is negative.

Interpret the intercept aa as the predicted response when x=0x = 0: "When [xx] is 0, the model predicts [yy] is aa." This only carries real meaning when x=0x = 0 is inside or near your data.

Interpret a residual with its sign: "The model underpredicts (positive residual) or overpredicts (negative residual) the observed [yy] by that amount." A residual plot, which graphs residuals against the predicted values or against xx, helps you judge whether the linear model fits. Apparent random scatter supports a linear form, while a clear curve suggests a linear model is not the best choice.

Find r, r-squared, the line, and one residual for 5 points

You collect five paired observations: (1,2)(1,2), (2,5)(2,5), (3,4)(3,4), (4,7)(4,7), (5,8)(5,8). Find the correlation coefficient rr, the coefficient of determination r2r^2, the least-squares line y^=a+bx\hat{y} = a + bx, and the residual for the point at x=2x = 2. Treat the data as a sample.

  1. Find the means. xˉ=1+2+3+4+55=155=3\bar{x} = \frac{1+2+3+4+5}{5} = \frac{15}{5} = 3 and yˉ=2+5+4+7+85=265=5.2\bar{y} = \frac{2+5+4+7+8}{5} = \frac{26}{5} = 5.2.

  2. List the deviations from the means. For xx: 2,1,0,1,2-2, -1, 0, 1, 2. For yy: 3.2,0.2,1.2,1.8,2.8-3.2, -0.2, -1.2, 1.8, 2.8.

  3. Multiply the paired deviations and add: (2)(3.2)+(1)(0.2)+(0)(1.2)+(1)(1.8)+(2)(2.8)=6.4+0.2+0+1.8+5.6=14.0(-2)(-3.2) + (-1)(-0.2) + (0)(-1.2) + (1)(1.8) + (2)(2.8) = 6.4 + 0.2 + 0 + 1.8 + 5.6 = 14.0.

  4. Square the deviations and add each column: (xixˉ)2=4+1+0+1+4=10\sum (x_i - \bar{x})^2 = 4 + 1 + 0 + 1 + 4 = 10 and (yiyˉ)2=10.24+0.04+1.44+3.24+7.84=22.8\sum (y_i - \bar{y})^2 = 10.24 + 0.04 + 1.44 + 3.24 + 7.84 = 22.8.

  5. Compute the correlation: r=14.010×22.8=14.0228=14.015.0997=0.9272r = \frac{14.0}{\sqrt{10 \times 22.8}} = \frac{14.0}{\sqrt{228}} = \frac{14.0}{15.0997} = 0.9272.

  6. Square it for the coefficient of determination: r2=196228=0.8596r^2 = \frac{196}{228} = 0.8596, so about 85.96% of the variation in yy is explained by the linear relationship with xx.

  7. Compute the slope. b=(xixˉ)(yiyˉ)(xixˉ)2=14.010=1.4b = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2} = \frac{14.0}{10} = 1.4.

  8. Compute the intercept: a=yˉbxˉ=5.2(1.4)(3)=5.24.2=1.0a = \bar{y} - b\bar{x} = 5.2 - (1.4)(3) = 5.2 - 4.2 = 1.0. The line is y^=1.0+1.4x\hat{y} = 1.0 + 1.4x.

  9. Find the residual at x=2x = 2, where the observed y=5y = 5. Predicted: y^=1.0+1.4(2)=3.8\hat{y} = 1.0 + 1.4(2) = 3.8. Residual =yy^=53.8=1.2= y - \hat{y} = 5 - 3.8 = 1.2.

r0.9272r \approx 0.9272, r20.8596r^2 \approx 0.8596, and the least-squares line is y^=1.0+1.4x\hat{y} = 1.0 + 1.4x. The residual at x=2x = 2 is +1.2+1.2, so the model underpredicts that observation by 1.2.

Confirm the slope with b = r times s_y over s_x

Using the same five points, confirm the slope b=1.4b = 1.4 with the formula b=rsysxb = r\,\frac{s_y}{s_x}, given r=0.9272r = 0.9272. Find the sample standard deviations first.

  1. From the previous example, (xixˉ)2=10\sum (x_i - \bar{x})^2 = 10 and (yiyˉ)2=22.8\sum (y_i - \bar{y})^2 = 22.8, with n=5n = 5.

  2. Sample standard deviation of xx: sx=1051=2.5=1.5811s_x = \sqrt{\frac{10}{5-1}} = \sqrt{2.5} = 1.5811.

  3. Sample standard deviation of yy: sy=22.851=5.7=2.3875s_y = \sqrt{\frac{22.8}{5-1}} = \sqrt{5.7} = 2.3875.

  4. Apply the formula: b=rsysx=0.9272×2.38751.5811=0.9272×1.5100=1.4000b = r\,\frac{s_y}{s_x} = 0.9272 \times \frac{2.3875}{1.5811} = 0.9272 \times 1.5100 = 1.4000.

  5. Recompute the intercept as a check: a=yˉbxˉ=5.2(1.4)(3)=1.0a = \bar{y} - b\bar{x} = 5.2 - (1.4)(3) = 1.0.

b=1.4b = 1.4 and a=1.0a = 1.0, matching the first example, so y^=1.0+1.4x\hat{y} = 1.0 + 1.4x.

Frequently asked questions

What counts as a good r value?

It depends on the context, not a fixed cutoff. The closer rr is to 11 or 1-1, the stronger the linear association, and rr near 00 means little or no linear association. A high rr still does not prove a line is the right model, so check a residual plot as well.

What is the difference between r and r-squared?

The correlation coefficient rr runs from 1-1 to 11 and shows both direction and strength of the linear association. The coefficient of determination r2r^2 runs from 00 to 11 and is the proportion of variation in yy explained by the linear relationship with xx. See r vs r-squared.

Does a strong correlation mean x causes y?

No. A strong correlation only says the two variables tend to move together in a linear pattern. A change in one variable causing a change in the other is a separate claim that a correlation alone cannot support.

Can I use a calculator like this on the AP exam?

On the AP Statistics exam a graphing calculator with statistical capabilities is expected for both sections, and it will report rr, r2r^2, and the regression line for you. This tool is for practice and for checking work by hand.