Linear Regression Calculator

Enter your x and y data to find the least-squares regression line, its slope, intercept and R².

Linear regression fits the straight line y = mx + b that best matches your data by least squares. The slope is the covariance of x and y divided by the variance of x; the intercept places the line through the means. R² measures how much of the variation the line explains, from 0 to 1.

Advertisement

How to Use This Calculator

Enter your x values and your y values as two comma-separated lists, in the same order and with the same count so each x pairs with its y. The calculator returns the best-fit line in the form y = mx + b, along with R², a measure of fit.

Linear regression is the workhorse of data analysis, used to summarise a trend and to predict y from x within the range of your data.

How the Calculation Works

Least squares finds the line that minimises the total squared vertical distance from the points to the line. The slope works out to the covariance of x and y divided by the variance of x, and the intercept is set so the line passes through the point of the two means.

R², the coefficient of determination, is the fraction of the variation in y that the line explains. An R² of 1 is a perfect fit; 0 means the line explains none of the variation.

m = Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)²; b = ȳ − m·x̄

Worked Example

For x = 1, 2, 3, 4 and y = 2, 4, 5, 4: the means are x̄ = 2.5 and ȳ = 3.75. The slope is Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)² = 3.5 ÷ 5 = 0.7, and the intercept is 3.75 − 0.7 × 2.5 = 2.0.

So the best-fit line is y = 0.7x + 2.0. Its R² comes out to about 0.49, meaning it explains roughly half the variation.

Assumptions and Limitations

  • Fits a straight line by ordinary least squares
  • x and y lists must have the same number of values
  • Best for linear trends; curved data needs a different model
  • Predict only within the range of your data, not far beyond it

Who Uses a Linear Regression Calculator

Students, researchers, and analysts use this to fit a least-squares line and get the slope, intercept, and R² from paired data. Run it to spot a trend, make a simple prediction, or check a statistics assignment. Enter your x and y values to get the equation and how well it fits.

Frequently Asked Questions

How do you calculate a linear regression?

Find the slope as the covariance of x and y divided by the variance of x, then set the intercept so the line passes through the means. The result is the least-squares line y = mx + b.

What does R² mean?

R², the coefficient of determination, is the share of the variation in y explained by the line, from 0 to 1. Higher means a better fit; 1 is perfect and 0 explains nothing.

What is the least-squares method?

It chooses the line that minimises the sum of the squared vertical distances between the data points and the line, giving the single best-fitting straight line for the data.

Can I use regression to predict?

Yes, within the range of your data. Plug an x into y = mx + b to predict y. Extrapolating far beyond your data is unreliable because the trend may not continue.

What if my data isn't linear?

Linear regression will still fit a line, but it may fit poorly (low R²). Curved relationships need polynomial or nonlinear models, or a transformation of the variables.

Looking for a different calculator?

CalculatorPlus has free tools across finance, construction, math, health and more — each one showing the formula and a worked example.