Skip to content

Linear Regression Calculator — Step-by-Step Solutions

The Linear Regression Calculator fits a straight line to your data using the least squares method. Enter pairs of (x, y) values to find the regression equation y = mx + b, correlation coefficient (Pearson r), and coefficient of determination (R-squared).

The calculator offers four modes: simple linear regression with slope and intercept, correlation analysis with strength interpretation, prediction mode to estimate y for any x value, and residual analysis showing how well the model fits each data point. An SVG scatter plot with the regression line is displayed for all modes.

0 valid points detected

Your calculations will appear here

How to Use the Linear Regression Calculator

  1. Select a mode: Linear Regression, Correlation Analysis, Prediction, or Residual Analysis.
  2. Enter your data points in the textarea, one x,y pair per line (e.g. 1, 2). The calculator detects valid points automatically.
  3. In Linear Regression mode, the calculator finds the best-fit line y = mx + b using the least squares method.
  4. In Correlation Analysis mode, the Pearson r and R-squared values are calculated with an interpretation of the strength and direction.
  5. In Prediction mode, enter an x value to predict the corresponding y using the regression equation.
  6. In Residual Analysis mode, the calculator shows the difference between observed and predicted values for each data point.
  7. All modes display an SVG scatter plot with the regression line. Residual mode also shows vertical lines from each point to the fitted line.

Formula and Theory

Linear regression finds the straight line that best fits a set of data points by minimising the sum of squared vertical distances (residuals) from the points to the line. This is known as the method of least squares.

The slope m represents the change in y for each unit increase in x. The intercept b is the value of y when x equals zero. Together they form the equation y = mx + b.

The Pearson correlation coefficient r measures the strength and direction of the linear relationship between x and y. It ranges from -1 (perfect negative) to +1 (perfect positive), with 0 indicating no linear relationship.

The coefficient of determination R-squared is the square of r. It represents the proportion of variance in y that is explained by the linear relationship with x. An R-squared of 0.85 means 85% of the variability in y is accounted for by x.

Residuals (e = y - y-hat) should ideally be randomly scattered around zero. Patterns in the residuals suggest the linear model may not be appropriate for the data.

Worked Examples

Example 1: Simple linear regression

Problem: Find the regression line for the points (1,2), (2,4), (3,5), (4,4), (5,5).

Solution: Mean x = 3, mean y = 4. Sum of (xi - xbar)(yi - ybar) = 4. Sum of (xi - xbar) squared = 10. Slope = 4/10 = 0.4. Intercept = 4 - 0.4 x 3 = 2.8.

Answer: y = 0.4x + 2.8

Example 2: Prediction from regression

Problem: Using y = 0.4x + 2.8, predict y when x = 10.

Solution: Substitute x = 10: y = 0.4(10) + 2.8 = 4 + 2.8 = 6.8.

Answer: y = 6.8

Example 3: Correlation coefficient

Problem: For data (1,1), (2,2), (3,3), (4,4), find the Pearson r.

Solution: All points lie exactly on the line y = x. The correlation is perfect positive.

Answer: r = 1.0, R-squared = 1.0

Frequently Asked Questions

What is the difference between correlation and regression?
Correlation measures the strength and direction of a linear relationship between two variables. Regression goes further by finding the equation of the best-fit line, allowing you to predict one variable from the other.
When should I not use linear regression?
Linear regression is not appropriate when the relationship between variables is non-linear, when there are significant outliers distorting the fit, or when the residuals show clear patterns rather than random scatter.
What does a negative slope mean?
A negative slope means that as x increases, y tends to decrease. The regression line slopes downward from left to right.
How many data points do I need?
A minimum of 2 points is required to fit a line, but meaningful statistical analysis typically requires at least 10 to 20 observations to give reliable estimates of slope, intercept, and correlation.

Related Statistics Calculators

Related Articles