Linear Regression Calculator

Simple linear regression fits the straight line y = a + bx that best predicts Y from X. For X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5 the line is y = 2.2 + 0.6x, with r = 0.7746 and R² = 0.6, predicting y = 5.8 at x = 6.

Last updated · By · Runs in your browser, nothing is uploaded

Advanced options
Result

Step-by-Step Solution

Enter your values above to see the full working.

What Is Linear Regression?

Linear regression finds the straight line that best describes how a response variable Y changes with a predictor variable X. “Best” has a precise meaning: the least-squares line is the one that makes the sum of the squared vertical distances between the points and the line as small as possible.

The result is a simple equation, y = a + bx, that you can use to understand the relationship (each extra unit of x changes y by b on average) and to predict y for new values of x.

The Parts of the Regression Line

TermSymbolMeaning
SlopebAverage change in y for a one-unit increase in x
InterceptaPredicted y when x = 0
CorrelationrStrength and direction of the linear relationship
Coefficient of determinationR²Share of the variation in y explained by x

Least-Squares Regression Formulas

b = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ Σ(xᵢ − x̄)² a = ȳ − b × x̄ ŷ = a + b × x
  • b is the slope: the covariance of x and y divided by the variance of x.
  • a is the intercept, chosen so that the line passes through the point (x̄, ȳ).
  • ŷ (“y-hat”) is the predicted value of y.
  • R² = 1 − SSE ÷ SST, where SSE is the sum of squared residuals and SST is the total sum of squares of y.

Assumptions Behind the Line

  • Linearity: the relationship between x and y is approximately a straight line.
  • Independence: each observation is independent of the others.
  • Constant spread: the residuals have similar variability at every x.
  • Roughly normal residuals: needed for confidence intervals and tests on the slope.

How Far Off Are the Predictions?

The standard error of the estimate summarises typical prediction error: √(SSE ÷ (n − 2)). Here that is √(2.4 ÷ 3) = 0.894, so predictions are typically off by about 0.9 units of y.

Worked Example: Fitting a Line

X = 1, 2, 3, 4, 5 and Y = 2, 4, 5, 4, 5.

  1. Means: x̄ = 3 and ȳ = 4.
  2. Σ(x − x̄)(y − ȳ) = 6 and Σ(x − x̄)² = 10.
  3. Slope: b = 6 ÷ 10 = 0.6.
  4. Intercept: a = 4 − 0.6 × 3 = 2.2.
  5. Line: y = 2.2 + 0.6x.
  6. Prediction at x = 6: 2.2 + 0.6 × 6 = 5.8.

Checking the Fit with Residuals

xActual yPredicted ŷResidual (y − ŷ)Residual²
122.8−0.80.64
243.40.60.36
354.01.01.00
444.6−0.60.36
555.2−0.20.04
Total02.40

SSE = 2.4 and SST = 6, so R² = 1 − 2.4 ÷ 6 = 0.6. The line explains 60% of the variation in y. The residuals add up to zero, a built-in check for the least-squares line.

How to Use This Linear Regression Calculator

  1. Enter your X values (the independent variable) and Y values (the dependent variable), in matching order.
  2. Enter an x value in “Predict y at x =” if you want a forecast.
  3. Read the regression equation, slope, intercept, correlation r, R² and the prediction.
  4. Open the steps to see every sum used.

To test whether the relationship is statistically significant, use the correlation coefficient calculator, which also returns a p-value.

Real-World Uses of Linear Regression

  • Forecasting: predicting sales from advertising spend or energy use from temperature.
  • Science: calibrating instruments, where a reading is regressed on a known standard.
  • Economics: estimating how wages change with years of education.
  • Real estate: relating price to floor area.
  • Machine learning: linear regression is the starting point and baseline for more complex models.

What Are the Most Common Mistakes With the Linear Regression Calculator?

  • Extrapolating too far. The line is only trustworthy within the range of your x values. Predicting at x = 6 here is already beyond the data.
  • Assuming causation. A good fit shows association, not that x causes y.
  • Not looking at a scatter plot. A curved pattern can give a decent R² yet be poorly described by a line.
  • Outliers and leverage points. A single extreme point can tilt the entire line.
  • Swapping X and Y. Regressing y on x gives a different line from regressing x on y.
  • Over-trusting small samples. With five points, the slope has a wide margin of uncertainty.

Where Can You Verify These Formulas?

The definitions and formulas on this page follow standard references. Check them against these sources:

Built and maintained by S. Dilshan. Found a mistake? Email [email protected] with the page address.

Frequently Asked Questions

What is the least squares regression line?

It is the straight line that minimises the sum of the squared vertical distances between the data points and the line. Its slope is b = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)².

How do I interpret the slope and intercept?

The slope is the average change in y for each one-unit increase in x. The intercept is the predicted y when x is zero, which is only meaningful if x = 0 makes sense for your data.

What does R² tell me?

R² is the proportion of the variation in y that is explained by x through the line. An R² of 0.6 means 60% of the variation is explained and 40% is not.

What is the difference between correlation and regression?

Correlation measures how strongly two variables are linearly related, with no distinction between them. Regression produces an equation to predict one variable from the other, so which variable is X and which is Y matters.

Can I use regression to predict outside my data range?

You can compute a prediction, but extrapolation is risky because the relationship may change beyond the data you have observed.

How many data points do I need for linear regression?

You need at least two points to draw a line, but meaningful regression needs many more. More data gives more reliable slope and intercept estimates.