Regression Calculator

Fit a simple linear regression line to paired X and Y data. Enter your values below, and see the formula, a worked example, and ready-to-use Python code underneath.

Enter your data

Comma, space, or newline separated.

Must have the same number of values as X, in matching order.

Please enter two equal-length lists of at least 2 paired values.

Regression equation
Slope (b₁)
Intercept (b₀)

What linear regression tells you

Simple linear regression fits a straight line through paired X and Y data, describing how Y tends to change as X changes. The line is chosen to minimize the total squared vertical distance between the observed points and the line — a method called ordinary least squares.

The fitted line gives you two things: an equation you can use to predict Y for a given X, and an R² value that tells you how much of the variation in Y the line actually explains. Regression is closely tied to correlation — R² is just the square of the Pearson correlation coefficient.

Regression formula

ŷ = b₀ + b₁x
b₁ = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)²
b₀ = ȳ − b₁x̄
ŷ is the predicted Y value, b₁ is the slope, b₀ is the intercept, and x̄ / ȳ are the means of X and Y.

Python code

You can fit the line manually, or with NumPy or SciPy:

# Method 1 — manual calculation
X = [2, 4, 6, 8, 10]
Y = [65, 70, 75, 85, 95]
n = len(X)
mean_x, mean_y = sum(X) / n, sum(Y) / n
b1 = sum((x - mean_x) * (y - mean_y) for x, y in zip(X, Y)) / \
     sum((x - mean_x) ** 2 for x in X)
b0 = mean_y - b1 * mean_x
print(f"y = {b0} + {b1}x")  # y = 55.5 + 3.75x

# Method 2 — NumPy
import numpy as np
b1, b0 = np.polyfit(X, Y, 1)

# Method 3 — SciPy (also returns r-value, p-value, std errors)
from scipy.stats import linregress
result = linregress(X, Y)
result.slope, result.intercept, result.rvalue ** 2

Worked example

Using the same hours-studied (X) and exam-score (Y) pairs as the correlation calculator example:

Hours studied (X)246810
Exam score (Y)6570758595
  1. 1
    Find the means.
    x̄ = 6, ȳ = 78
  2. 2
    Compute the slope.
    b₁ = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)² = 150 / 40 = 3.75
  3. 3
    Compute the intercept.
    b₀ = 78 − (3.75 × 6) = 55.5
  4. 4
    Write the equation and check R².
    ŷ = 55.5 + 3.75x, with R² = 0.9698

Each additional hour studied predicts about 3.75 more points on the exam. Plugging in x = 7: ŷ = 55.5 + 3.75 × 7 = 81.75.

Frequently asked questions

What do the slope and intercept mean in a regression equation?

The slope is the predicted change in Y for every one-unit increase in X. The intercept is the predicted value of Y when X equals zero, which is only meaningful if X = 0 makes sense for your data.

What does R² tell you about a regression?

R² (the coefficient of determination) is the proportion of the variation in Y that's explained by the linear relationship with X, on a scale from 0 to 1. An R² of 0.90 means 90% of the variation in Y is explained by X; the rest is unexplained by the linear model.

Is it safe to use the regression line to predict values outside my data's range?

Generally not. Extrapolating beyond the range of X values you actually observed assumes the same linear relationship continues to hold, which often isn't true. Predictions are most reliable within, or close to, the range of X values used to fit the line.

How is linear regression related to correlation?

They're closely linked: the R² of a simple linear regression equals the square of the Pearson correlation coefficient between X and Y. Correlation measures the strength of the linear relationship; regression fits a specific line to describe it and make predictions.

What method does this calculator use to fit the line?

Ordinary least squares (OLS) — the line is chosen to minimize the sum of the squared vertical distances between the observed Y values and the line's predicted values.

Advertisement