Mathematics · Statistics

Linear regression calculator — slope, intercept and R²

Fit y = ax + b by ordinary least squares and plot the data with the fitted line, correlation and R².

Created v1.0

By Thibaut Grzelak, Mechanical Analysis Engineer

Statistics
01

Calculation inputs

Use spaces, semicolons or newlines between values. A point or comma is a decimal separator, never a list separator. Scientific notation is accepted. One row per line; spaces or semicolons separate columns.

03

Results

Slope a
2
Intercept b
0
Correlation r
1
Count n
3
Mean x
2
Mean y
4

Results show up to four significant digits for readability; internal calculations retain full precision.

See calculation details

01Mean x

Formulax̄ = x₀ + ∑(xᵢ − x₀)/n
Substitution1 + 3 / 3
Result
2

02Mean y

Formulaȳ = y₀ + ∑(yᵢ − y₀)/n
Substitution2 + 6 / 3
Result
4

03Sxx

FormulaSxx = ∑(xᵢ − x̄)²
Substitution(-1)² + 0² + 1²
Result
2

04Sxy

FormulaSxy = ∑(xᵢ − x̄)(yᵢ − ȳ)
Substitution(-1) × (-2) + 0 × 0 + 1 × 2
Result
4

05Syy

FormulaSyy = ∑(yᵢ − ȳ)²
Substitution(-2)² + 0² + 2²
Result
8

06Slope a

Formulaa = Sxy/Sxx
Substitution4 / 2
Result
2

07Intercept b

Formulab = ȳ − ax̄
Substitution4 − 2 × 2
Result
0

08Equation

Formulaŷ = ax + b
Substitution2 × x + 0
Result
ŷ = 2x

09Correlation r

Formular = Sxy/√(Sxx·Syy)
Substitution4 / √(2 × 8)
Result
1

10Coefficient of determination R²

FormulaR² = r²
Substitution1²
Result
1
01Formulas and symbols

Formulas used

ŷ

a = (∑(xᵢ−x̄)(yᵢ−ȳ))/(∑(xᵢ−x̄)²)

b = ȳ−ax̄

ŷ = ax+b

a: slope; b: intercept; ŷ: fitted value; x̄ and ȳ: means.

r

r = Sxy/√(SxxSyy)

R² = r²

Sxx, Syy and Sxy: centered sums; r: correlation; R²: coefficient of determination.

SymbolMeaning
(xᵢ,yᵢ)Pairs (x, y), one per line
aSlope a
bIntercept b
ŷEquation
R²Coefficient of determination R²
rCorrelation r
nCount n
x̄Mean x
ȳMean y
02Validation example

Reference numerical case

  1. (1, 2), (2, 4), (3, 6) give slope 2, intercept 0, R² = 1 and r = 1. For (1, 1), (2, 2), (3, 2), slope = 0.5 and R² = 0.75.
03How it works

The slope divides centered cross-products by centered x-square deviations. The intercept places the line through (x̄, ȳ). R² equals r² for this ordinary least-squares fit with an intercept and nonconstant y.

Sxx and Syy sum squared centered deviations; Sxy sums centered cross-products. Correlation r measures direction and linear association. R² is the explained-variance fraction for this fit when y varies.

04Limits and conventions

Two to 5000 finite (x, y) pairs, with at least two distinct x values. When every y is identical the fitted line is constant, but correlation and R² are undefined. Large offsets are centered before accumulation. The plot shows at most 500 sampled points; the fit uses all points.

05Common mistakes

A high R² does not prove causality or justify extrapolation. Repeated points count as repeated observations; outliers can substantially change the fitted line.

06Practical questions

What does R² actually tell you?

R² is the proportion of variation in the response explained by the fitted linear relationship in this model. A value near 1 indicates a close fit to the observed data, not necessarily a good causal or out-of-sample predictive model.

Does a high R² prove that one variable causes the other?

No. Regression measures association in the supplied data. Confounding variables, reverse causation or coincidence can produce a strong relationship without one variable causing the other.

07Technical references