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.
Your calculations will appear here
How to Use the Linear Regression Calculator
- Select a mode: Linear Regression, Correlation Analysis, Prediction, or Residual Analysis.
- Enter your data points in the textarea, one x,y pair per line (e.g. 1, 2). The calculator detects valid points automatically.
- In Linear Regression mode, the calculator finds the best-fit line y = mx + b using the least squares method.
- In Correlation Analysis mode, the Pearson r and R-squared values are calculated with an interpretation of the strength and direction.
- In Prediction mode, enter an x value to predict the corresponding y using the regression equation.
- In Residual Analysis mode, the calculator shows the difference between observed and predicted values for each data point.
- 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?
When should I not use linear regression?
What does a negative slope mean?
How many data points do I need?
Related Statistics Calculators
Probability Calculator
Calculate probability of events, complements, unions, and intersections. Free online probability calculator for statistics.
Standard Deviation Calculator
Calculate standard deviation, variance, mean, and more for any dataset. Supports population and sample statistics. Free online tool.
Z-Score Calculator
Calculate z-scores, find values from z-scores, and determine probabilities. Free online z-score calculator with bell curve visualisation.
Binomial Calculator
Calculate binomial probabilities, cumulative distributions, and expected values. Free online binomial distribution calculator with bar chart.
Normal Distribution Calculator
Calculate probabilities for normal distributions. Find areas under the bell curve, percentiles, and inverse values. Free online tool.
Correlation Calculator
Calculate Pearson correlation coefficient, R-squared, and regression line. Free online correlation calculator with scatter plot.