guide

How to Calculate a Linear Regression (2026)

By Rui Barreira · Last updated: 18 June 2026

Linear regression finds the straight line that best fits a set of data points, letting you quantify the relationship between two variables and make predictions. Given an input variable (x) and an output variable (y), the result is the equation y = mx + b — where m is the slope and b is the y-intercept. Use the Linear Regression Calculator to compute this instantly from any dataset.

The Formulas

For a dataset of n points, the slope and intercept are calculated as:

  • Slope (m) = [n × Σ(xy) − Σx × Σy] / [n × Σ(x²) − (Σx)²]
  • Intercept (b) = [Σy − m × Σx] / n

The R² value (coefficient of determination) measures how well the line fits the data. R² = 1 means a perfect fit; R² = 0 means the line explains none of the variation in y.

Worked Example

Suppose you want to predict monthly sales (y) from advertising spend in thousands (x). Here is a small dataset:

Ad Spend (x, $k)Sales (y, $k)xy
114114
217434
322966
42516100
53025150
Σ = 15Σ = 108Σ = 55Σ = 364

With n = 5: slope m = (5 × 364 − 15 × 108) / (5 × 55 − 15²) = (1820 − 1620) / (275 − 225) = 200 / 50 = 4.0. Intercept b = (108 − 4.0 × 15) / 5 = (108 − 60) / 5 = 9.6. The regression line is y = 4x + 9.6. For $6k in ad spend, the model predicts $33.6k in sales.

When to Trust the Result

A high R² alone does not validate a regression. Check that the relationship is plausible — correlation is not causation. Watch for outliers that pull the slope disproportionately; a single extreme point can shift m significantly. The model is also only reliable within the range of x values in your data — extrapolating far outside that range produces increasingly unreliable predictions. For small datasets (fewer than 10 points), treat the output as directional rather than precise.

Use the Linear Regression Calculator to paste your own data and get the slope, intercept, R², and predicted values without any manual arithmetic.

Frequently Asked Questions

Is this tool free?
Yes — completely free, no signup required. All processing happens in your browser.
Does the tool work offline?
Once loaded, most features work without an internet connection since everything runs client-side.
More free toolsSee all 469
Merge PDFsCompress ImageJSON FormatterPassword GeneratorVAT CalculatorQR Code Generator
How to Calculate a Linear Regression (2026) | brevio