Linear Regression Calculator

Find the least-squares line of best fit for your data: slope, intercept, correlation r and R², with predictions. Drag the points on the chart, tap to add more, and watch the line respond.

Answer

Best-fit line for 8 points

y = 4.5952x + 46.5714

Slope4.59524
Intercept46.57143
Correlation r0.9952
R²0.9904
Predicted y at x = 1092.5238
Std. error of estimate1.1985

R² = 99%: that share of the variation in y is explained by the straight line.

Your data and the line

−12.842.597.8−1.249.2y = 4.595x + 46.571

Drag any point, tap empty space to add a point, double-tap a point to remove it. The thin lines are the errors (residuals) the line keeps as small as possible.

Why this answer

  1. Averages. x̄ = 4.5, ȳ = 67.25. The best-fit line always passes through (x̄, ȳ).
  2. Slope. b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² = 4.59524: y changes by that much for each 1 unit of x.
  3. Intercept. a = ȳ − b·x̄ = 46.57143.
  4. Least squares. No other straight line has a smaller total of squared errors (here 8.619).

Why it works

What makes a line the best fit?

061204.59r = 0.95your error 34 · best 3

Step 1 of 3

Draw your own line

Drag the two red handles to place a line through these points. Each thin line is an error; its square is drawn too. Make the total squared error as small as you can. Yours: 34.0.
Get your error under 3.5.

Put it to use

Using a trend line well

  • Predict only within the range of your x values; a line that fits 1–8 hours of study says nothing about 30 hours.
  • Correlation isn’t causation: ice cream sales and sunburns rise together because of summer.
  • Look at the plot. A curved pattern or a single outlier can make a straight-line fit misleading even when R² looks fine.

How it works

The linear regression calculator finds the least-squares line of best fit, y = a + bx, for a set of (x, y) points, along with the correlation coefficient r, the coefficient of determination R² and predictions for new x values.

The slope is b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)² and the intercept is a = ȳ − b·x̄. Of all straight lines, this one makes the sum of squared vertical distances (residuals) from the points to the line as small as possible.

r measures how tightly the points follow a straight line, from −1 to 1; R² is the share of the variation in y explained by the line.

b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²a = ȳ − b·x̄r = Σ(x − x̄)(y − ȳ) ÷ √(Σ(x − x̄)² Σ(y − ȳ)²)R² = r²

Worked example: Points (1,2), (2,4), (3,5), (4,4), (5,5)

  1. x̄ = 3, ȳ = 4.
  2. Σ(x − x̄)(y − ȳ) = 6; Σ(x − x̄)² = 10.
  3. Slope b = 0.6; intercept a = 4 − 0.6 × 3 = 2.2.
  4. Line: y = 0.6x + 2.2, r = 0.775, R² = 0.60.

Common mistakes

  • Predicting far outside the range of the data.
  • Treating correlation as causation.
  • Ignoring outliers, which can pull the line strongly.
  • Fitting a straight line to a clearly curved pattern.

Frequently asked questions

How do you find the line of best fit?

Use least squares: slope = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², intercept = ȳ − slope × x̄.

What is a good R² value?

It depends on the field: physical measurements often exceed 0.9, while social data with R² of 0.3 can still be useful.

What is the difference between r and R²?

r is the correlation (−1 to 1, with sign); R² is r squared (0 to 1), the share of variation explained.

Does the regression line go through the mean?

Yes, the least-squares line always passes through (x̄, ȳ).

What is a residual?

The vertical distance from a point to the line: actual y minus predicted y.