Skip to content
K Knidox Search…
Statistics · Regression

Linear Regression

Fit the least-squares line and get the slope, intercept, equation, r, and r².

Two columns of numbers — x and y separated by a comma or space.
Sample data — tap to load
Regression line
ŷ = 1.8 + 0.8xn = 5

slope b₁ = 0.8 · intercept b₀ = 1.8 · r = 0.8528 · r² = 0.7273

Slope (b₁)
0.8
Intercept (b₀)
1.8
Correlation (r)
0.8528
Variance explained (r²)
0.7273
Data points with the fitted regression line
Scatter plot of the 5 data points with the fitted line ŷ = 1.8 + 0.8x6215

This tool fits the least-squares regression line ŷ = b₀ + b₁x to your paired data, returning the slope, intercept, correlation r, and r². For x = 1–5 and y = 2, 4, 5, 4, 6, it gives ŷ = 1.8 + 0.8x with r² = 0.7273 — meaning about 73% of the variation in y is explained by x.

What the least-squares line is

Linear regression draws the single straight line that best summarises how y changes with x. “Best” means least squares: the line that makes the sum of the squared vertical distances between each point and the line as small as possible. The slope b₁ is how much ŷ moves per one-unit rise in x; the intercept b₀ is the predicted y when x = 0.

The coefficient of determination, r², is the share of the variance in y that the line explains — from 0 (the line is no better than the mean of y) to 1 (every point lies exactly on the line). It is simply the square of Pearson’s correlation r.

b₁ = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)², b₀ = ȳ − b₁·x̄

slope is covariation over the spread of x; the intercept anchors the line through (x̄, ȳ)

Worked example

Five pairs — x: 1, 2, 3, 4, 5 and y: 2, 4, 5, 4, 6. Here x̄ = 3 and ȳ = 4.2.

  1. 1
    Find the means. x̄ = (1+2+3+4+5) ÷ 5 = 3 and ȳ = (2+4+5+4+6) ÷ 5 = 4.2.
  2. 2
    Build the sums of squares. Σ(x−x̄)(y−ȳ) = 4.4 + 0.2 + 0 − 0.2 + 3.6 = 8 and Σ(x−x̄)² = 4 + 1 + 0 + 1 + 4 = 10.
  3. 3
    Compute the slope. b₁ = 8 ÷ 10 = 0.8.
  4. 4
    Compute the intercept. b₀ = ȳ − b₁·x̄ = 4.2 − 0.8 × 3 = 1.8, so ŷ = 1.8 + 0.8x.
  5. 5
    Assess the fit. r = 8 ÷ √(10 × 8.8) ≈ 0.8528, so r² ≈ 0.7273 — about 73% of the variance in y is explained.

Interpreting r² (goodness of fit)

Rough bands for the share of variance explained; the right cutoffs depend on your field.

Variance explainedFit
0.00–0.250–25%weak / little linear fit
0.25–0.5025–50%modest
0.50–0.7550–75%moderate
0.75–0.9075–90%strong
0.90–1.0090–100%very strong

Reading the result responsibly

Correlation is not causation. A good fit means x predicts y in this sample — not that changing x causes y to change. A lurking third variable can drive both.

Don’t extrapolate beyond the data. The line is only supported over the range of x you measured. Predictions far outside that range assume the linear pattern continues, which it often does not.

r versus r². r (−1 to +1) carries the direction of the relationship; r² (0 to 1) is the share of variance explained and is always positive. An r of −0.85 and +0.85 give the same r² of about 0.72.

What does “least squares” actually minimise?
The sum of the squared vertical distances (residuals) between each observed y and the value the line predicts. Squaring keeps the errors positive and penalises large misses more heavily, which fixes one unique best-fit line.
What is the difference between r and r²?
r is Pearson’s correlation, from −1 to +1, and shows both strength and direction. r² is its square, from 0 to 1, and gives the proportion of variance in y explained by x. r = 0.8528 corresponds to r² ≈ 0.7273.
What does the slope mean in context?
The slope b₁ is the predicted change in y for each one-unit increase in x. In the example, b₁ = 0.8 means y is predicted to rise by 0.8 for every 1-unit rise in x.
What does the intercept mean?
The intercept b₀ is the predicted y when x = 0. It is only meaningful if x = 0 is within or near your data’s range; otherwise treat it as a mathematical anchor rather than a real prediction.
Why shouldn’t I extrapolate beyond the data?
The fit is only justified over the range of x you observed. Outside that range you have no evidence the straight-line relationship still holds, so predictions can be wildly wrong.
What are the assumptions of linear regression?
A genuinely linear relationship, residuals that are independent with roughly constant spread (homoscedastic) and approximately normal, and no extreme outliers. Always plot the points and the residuals before trusting the line.
How many points do I need?
At least two distinct x-values are required to define a line, but two points fit perfectly and tell you nothing about fit. More points give a more reliable slope, intercept, and r².