Equation of the resulting line.
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
Results
Explained variation fraction; undefined for constant y.
Results show up to four significant digits for readability; internal calculations retain full precision.
See calculation details
01Mean x
x̄ = x₀ + ∑(xᵢ − x₀)/n1 + 3 / 302Mean y
ȳ = y₀ + ∑(yᵢ − y₀)/n2 + 6 / 303Sxx
Sxx = ∑(xᵢ − x̄)²(-1)² + 0² + 1²04Sxy
Sxy = ∑(xᵢ − x̄)(yᵢ − ȳ)(-1) × (-2) + 0 × 0 + 1 × 205Syy
Syy = ∑(yᵢ − ȳ)²(-2)² + 0² + 2²06Slope a
a = Sxy/Sxx4 / 207Intercept b
b = ȳ − ax̄4 − 2 × 208Equation
ŷ = ax + b2 × x + 009Correlation r
r = Sxy/√(Sxx·Syy)4 / √(2 × 8)10Coefficient of determination R²
R² = r²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 = Sxy/√(SxxSyy)
R² = r²
Sxx, Syy and Sxy: centered sums; r: correlation; R²: coefficient of determination.
| Symbol | Meaning |
|---|---|
| (xᵢ,yᵢ) | Pairs (x, y), one per line |
| a | Slope a |
| b | Intercept b |
| ŷ | Equation |
| R² | Coefficient of determination R² |
| r | Correlation r |
| n | Count n |
| x̄ | Mean x |
| ȳ | Mean y |
02Validation example
Reference numerical case
- (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.