Calculate linear regression

Fit an ordinary least-squares straight line to paired x and y observations.

freeworks offlinenothing uploaded
ToolLinear Regression Calculator
Input
Output
Put this on your own site

The frame below runs the same code as this page, in the reader's own browser. Nothing is sent to us, and nothing is sent to you.

Pick a dark background and the text and panels follow it, so the frame stays readable on a dark page.

Preview

How it works

The slope is Σ(x−x̄)(y−ȳ) ÷ Σ(x−x̄)² and the intercept is ȳ minus slope × x̄. Predictions lie on that line, residuals are observed minus predicted, and R² is the squared Pearson correlation for this intercept model.

  • At least two paired observations are required.
  • No weights, robust loss, polynomial terms, uncertainty interval, or causal claim are added.

Worked example

Five paired points
The same points used in the correlation calculator
Input
											X values: [1,2,3,4,5]
Y values: [2,4,5,4,5]
										
Output
												Slope: 0.6
Intercept: 2.2
R squared: 0.6
Equation: y = 0.6x + 2.2
											

When to use this

Calibration exercises fit response against concentration, trend charts overlay a straight line, and classroom datasets inspect residuals.

Edge cases

  • Identical x values make the slope denominator zero and are rejected.
  • A constant y series receives the implementation’s zero-variance R² convention, so that number is not evidence of predictive information.
  • One extreme point can pull an unweighted least-squares line sharply toward itself.

References