Linear Regression Calculator — Line of Best Fit & Correlation

StatisticsFreeNo sign-up

For hours studied (1 to 8) and test scores (55, 62, 63, 71, 74, 78, 86, 87), the least-squares line is ŷ = 51 + 4.667x: each extra hour predicts about 4.7 more points. The correlation is r = 0.991 and r² = 0.981. The linear regression calculator below gives this line for your data, plus standard errors, residuals and prediction intervals.

Linear regression: line of best fit, r and r²
Try:
Least-squares regression line
ŷ = 51 + 4.66667x
Slope b₁4.66667 (SE 0.262265)
Intercept b₀51 (SE 1.32437)
Correlation r0.990657
r²0.981402 (98.1%)
n8
Std. error of regression s1.69967
Slope t and p-valuet = 17.7937, p < 0.0001
95% CI for the slope4.02493 to 5.30841

In words. Each 1-unit increase in x goes with a rise of about 4.66667 in predicted y. r = 0.990657 shows a very strong positive linear relationship, and the line explains 98.1% of the variation in y. The slope is statistically different from 0 at the 5% level (p < 0.0001).

Prediction at x = 5.5
Predicted ŷ76.6667
95% confidence interval (mean y)75.0623 to 78.271
95% prediction interval (one new y)72.209 to 81.1243
24685060708090ŷ(5.5) = 76.6667
data pointsline of best fit95% confidence band (mean)95% prediction band (single y)
Residuals table and plot (8 points)
# x y ŷ Residual y − ŷ
1 1 55 55.6667 −0.666667
2 2 62 60.3333 1.66667
3 3 63 65 −2
4 4 71 69.6667 1.33333
5 5 74 74.3333 −0.333333
6 6 78 79 −1
7 7 86 83.6667 2.33333
8 8 87 88.3333 −1.33333
2468−2−112Residual = y − ŷ. A random cloud around 0 supports a straight-line model; a curve or a funnel shape means the line is missing something.
Show steps (sums table and formulas)
  1. Sums tableAdd up each column (n = 8):
    x y x² y² xy
    1 55 1 3,025 55
    2 62 4 3,844 124
    3 63 9 3,969 189
    4 71 16 5,041 284
    5 74 25 5,476 370
    6 78 36 6,084 468
    7 86 49 7,396 602
    8 87 64 7,569 696
    Σx = 36 Σy = 576 Σx² = 204 Σy² = 42,404 Σxy = 2,788
  2. Meansx̄ = Σx / n = 36 / 8 = 4.5; ȳ = Σy / n = 576 / 8 = 72
  3. Sums of squaresSxx = Σx² − (Σx)²/n = 42; Syy = Σy² − (Σy)²/n = 932; Sxy = Σxy − (Σx)(Σy)/n = 196
  4. Slopeb₁ = Sxy / Sxx = 196 / 42 = 4.66667
  5. Interceptb₀ = ȳ − b₁·x̄ = 72 − (4.66667)(4.5) = 51. The line is ŷ = 51 + 4.66667x.
  6. Correlationr = Sxy / √(Sxx·Syy) = 196 / √(42 × 932) = 0.990657, so r² = 0.981402.
  7. Residual spreadSSE = Σ(y − ŷ)² = 17.3333. With n − 2 = 6 degrees of freedom, s = √(SSE / (n − 2)) = 1.69967.
  8. Standard errorsSE(b₁) = s / √Sxx = 0.262265; SE(b₀) = s·√(1/n + x̄²/Sxx) = 1.32437.
  9. t test for the slopet = b₁ / SE(b₁) = 17.7937 with 6 df, two-sided p < 0.0001. The 95% critical value is t* = 2.44691, so the slope interval is b₁ ± t*·SE(b₁) = 4.02493 to 5.30841.
  10. Prediction at x = 5.5ŷ = 51 + 4.66667·5.5 = 76.6667. Mean response: ŷ ± t*·s·√(1/n + (x₀ − x̄)²/Sxx) = 75.0623 to 78.271. One new observation: ŷ ± t*·s·√(1 + 1/n + (x₀ − x̄)²/Sxx) = 72.209 to 81.1243.

Related: describe each variable · standard deviation · p-values

Linear Regression Calculator — Line of Best Fit & Correlation
Embed this calculator on your site

Free to use on class pages, blogs and course pages. Copy the code below and paste it into your page:

How to use the linear regression calculator

  1. Enter your data in one of three ways: two comma-separated lists, pairs pasted from a spreadsheet (one x, y pair per line), or a table you fill in row by row. Switching tabs carries the data over.
  2. Optionally enter an x value to predict at, and choose a 90%, 95% or 99% confidence level.
  3. Read the equation ŷ = b₀ + b₁x, then the slope and intercept with their standard errors, r, r², the slope’s t test and its confidence interval.
  4. Check the scatter plot with the fitted line and the confidence and prediction bands, then open the residuals table and residual plot.
  5. Open Show steps for the sums table (Σx, Σy, Σx², Σy², Σxy) and every formula with your numbers in it.

The least-squares line

Simple linear regression fits the straight line ŷ = b₀ + b₁x that makes the sum of squared residuals, Σ(y − ŷ)², as small as possible. The formulas use the means x̄ and ȳ and three sums of squares:

  • Sxx = Σx² − (Σx)²/n, Syy = Σy² − (Σy)²/n, Sxy = Σxy − (Σx)(Σy)/n
  • Slope: b₁ = Sxy / Sxx
  • Intercept: b₀ = ȳ − b₁x̄ (so the line always passes through the point (x̄, ȳ))
  • Correlation: r = Sxy / √(Sxx·Syy), and r² is the share of the variation in y explained by the line

The slope is the predicted change in y for a 1-unit increase in x. The intercept is the predicted y when x = 0, which only has a practical meaning if x = 0 is inside or near your data.

Worked example by hand

Take the five points (1, 2), (2, 4), (3, 5), (4, 4), (5, 5).

x y x² y² xy
1 2 1 4 2
2 4 4 16 8
3 5 9 25 15
4 4 16 16 16
5 5 25 25 25
Σ = 15 Σ = 20 Σ = 55 Σ = 86 Σ = 66

The means are x̄ = 3 and ȳ = 4. Then Sxx = 55 − 15²/5 = 10, Syy = 86 − 20²/5 = 6 and Sxy = 66 − 15·20/5 = 6. The slope is b₁ = 6/10 = 0.6 and the intercept is b₀ = 4 − 0.6·3 = 2.2, so ŷ = 2.2 + 0.6x. The correlation is r = 6/√(10·6) ≈ 0.775 and r² = 0.6: the line explains 60% of the variation in y.

Standard errors, the t test and intervals

Real data scatter around the line, so the slope and intercept are estimates. The residual standard error s = √(SSE/(n − 2)) measures the typical vertical distance from the points to the line. In the example the residuals are −0.8, 0.6, 1, −0.6 and −0.2, so SSE = 2.4 and s = √(2.4/3) ≈ 0.894.

  • Standard error of the slope: SE(b₁) = s/√Sxx ≈ 0.894/√10 ≈ 0.283.
  • t test for the slope: t = b₁/SE(b₁) ≈ 2.12 with n − 2 = 3 degrees of freedom gives a two-sided p ≈ 0.124. With five points the upward trend is not statistically significant at the 5% level, even though r² = 0.6.
  • Confidence interval for the slope: b₁ ± t*·SE(b₁), where t* is the Student t quantile for n − 2 degrees of freedom (3.182 for 95% and 3 df).
  • Confidence interval for the mean response at x₀: ŷ ± t*·s·√(1/n + (x₀ − x̄)²/Sxx). It describes where the average y lies for that x.
  • Prediction interval for one new y at x₀: ŷ ± t*·s·√(1 + 1/n + (x₀ − x̄)²/Sxx). It is always wider, because a single observation also carries its own scatter.

Both intervals are narrowest at x̄ and widen as x₀ moves away from it, which is why the bands on the scatter plot curve outward.

Reading r and r²

|r| Usual description r²
0.8 to 1 very strong 0.64 to 1
0.6 to 0.8 strong 0.36 to 0.64
0.4 to 0.6 moderate 0.16 to 0.36
0.2 to 0.4 weak 0.04 to 0.16
0 to 0.2 very weak or none 0 to 0.04

These labels are rules of thumb, not fixed standards; fields differ. The sign of r matches the sign of the slope. A high r² does not prove that x causes y, and a low r² does not rule out a strong curved relationship.

Check the residuals before you trust the line

Linear regression assumes a straight-line trend, independent observations and residuals with roughly constant spread (and roughly normal, for the t intervals). The residual plot is the quickest check: a patternless band around 0 is what you want. A curve means a straight line is the wrong shape; a funnel means the spread grows with x; one point far from the rest may be an outlier that pulls the line. The largest residual is highlighted in the table.

Common mistakes

Mistake Fix
Swapping x and y Put the explanatory variable in x; regressing y on x and x on y give different lines
Predicting far outside the data Extrapolation is risky; the calculator warns when x₀ is outside your range
Using the confidence interval for one new value Use the prediction interval for a single observation
Reading r² = 0.81 as “r = 0.81” r = ±0.9; r² is the share of variation explained
Rounding the slope before computing the intercept Keep full precision and round only the final answers

Linear Regression practice quiz: 10 questions

Test yourself with 10 multiple-choice questions. Choose an answer and press Submit to see if you are right and why. Your score appears at the end.

Question 1 of 10
1. What is the least-squares line for the points (1, 3), (2, 5), (3, 7), (4, 9)?

Found a mistake? Let us know!

2. A data set has Sxy = 24 and Sxx = 8. What is the slope b₁?

Found a mistake? Let us know!

3. The slope is b₁ = 2.5, x̄ = 4 and ȳ = 15. What is the intercept b₀?

Found a mistake? Let us know!

4. If r = −0.7, what is r²?

Found a mistake? Let us know!

5. For ŷ = 12 + 3x, what is the residual for the point (5, 30)?

Found a mistake? Let us know!

6. Which interval is wider at the same x₀ and confidence level?

Found a mistake? Let us know!

7. A regression on 12 points uses a t distribution with how many degrees of freedom?

Found a mistake? Let us know!

8. What does a slope of −1.8 in ŷ = 40 − 1.8x mean?

Found a mistake? Let us know!

9. For (1, 2), (2, 4), (3, 5), (4, 4), (5, 5), what is the correlation r (to 3 decimals)?

Found a mistake? Let us know!

10. A residual plot shows a clear U-shaped curve. What does it suggest?

Found a mistake? Let us know!

Frequently asked questions

What does linear regression calculate?

It finds the straight line that best fits paired data by least squares, ŷ = b₀ + b₁x, along with the correlation r and r². This calculator also gives the standard errors of the slope and intercept, a t test for the slope, residuals and confidence and prediction intervals.

What is a linear regression equation?

It is the fitted line written as ŷ = b₀ + b₁x. In ŷ = 52 + 4.5x for hours studied and test score, each extra hour predicts about 4.5 more points, and 0 hours predicts about 52.

How do you find linear regression on a TI-84?

Enter the x values in L1 and the y values in L2 (STAT, EDIT). Then press STAT, CALC, 4:LinReg(ax+b) and calculate. To see r and r², turn on DiagnosticOn from the CATALOG once (or turn Stat Diagnostics on in MODE on newer models).

How do you find linear regression without a calculator?

Make a table with columns x, y, x², y² and xy, add each column, then compute Sxx, Sxy, the slope b₁ = Sxy/Sxx and the intercept b₀ = ȳ − b₁x̄, as in the worked example above. The steps in the calculator show the same table for your data.

How do you find r in linear regression?

Use r = Sxy / √(Sxx·Syy). It always lies between −1 and 1. For example, r = 0.9 gives r² = 0.81, so the line explains about 81% of the variation in y.

Is there a linear regression calculator with steps?

Yes. The calculator on this page shows the full sums table and every formula with your numbers: means, sums of squares, slope, intercept, r, the residual standard error, standard errors, the t test and the intervals.

Related calculators

More statistics calculators

Browse all 206 calculators

Related to This Article

What people say about "Linear Regression Calculator: Line of Best Fit, r, r²"?

No one replied yet.

Leave a Reply