Linear Regression Calculator (Least Squares)
Calculate the linear regression line (line of best fit) for a data set using the least squares method.
Find slope, y-intercept, and R² correlation.
What Is Linear Regression?
Linear regression finds the straight line that best fits a set of data points — minimizing the total squared distance from each point to the line. This “line of best fit” can then be used to predict values and understand the relationship between two variables.
The Least Squares Formulas
Given n pairs of (x, y) data:
Slope: m = [n·Σxy - Σx·Σy] / [n·Σx² - (Σx)²]
Intercept: b = (Σy - m·Σx) / n
Regression line: y = mx + b
The slope m tells you how much y changes for each unit increase in x. The intercept b is the value of y when x = 0.
Pearson Correlation Coefficient (R)
R = [n·Σxy - Σx·Σy] / √{ [n·Σx² - (Σx)²] × [n·Σy² - (Σy)²] }
R ranges from -1 to +1:
- R = +1: Perfect positive linear relationship (as x increases, y increases proportionally)
- R = -1: Perfect negative linear relationship
- R = 0: No linear relationship
Strength interpretation by |R|: 0.0–0.3 = weak, 0.3–0.7 = moderate, 0.7–1.0 = strong.
R² — Coefficient of Determination
R² is R squared, ranging from 0 to 1. It represents the proportion of variance in y explained by x. An R² of 0.85 means 85% of the variation in y is explained by the linear relationship with x — and 15% is due to other factors.
Real-World Uses
- Sales forecasting: Predict next month sales based on advertising spend
- Biology: Height vs. weight relationships in a population
- Physics: Voltage vs. current (Ohm’s Law verification)
- Economics: GDP vs. unemployment rate
- Environmental science: Temperature trends over years
Important Caution
Correlation does not imply causation. A strong R² means x and y move together — not that x causes y. Always consider whether the relationship makes physical or logical sense before drawing conclusions.
Worked Example
Data: (1,2), (2,4), (3,5), (4,4), (5,5). n=5, Σx=15, Σy=20, Σxy=66, Σx²=55. m = (5×66 - 15×20) / (5×55 - 225) = (330-300)/(275-225) = 30/50 = 0.6. b = (20 - 0.6×15)/5 = 11/5 = 2.2. Line: y = 0.6x + 2.2.