Least Squares Regression Line Calculator
Enter paired x, y data to find the best-fit line, or predict y from a known regression equation.
📈 What is the Least Squares Regression Line?
The least squares regression line (also called the line of best fit) is the unique straight line ŷ = mx + b that minimises the sum of the squared vertical distances from each observed data point to the line. These vertical distances are called residuals, and squaring them ensures that positive and negative deviations both contribute positively to the total. By choosing the slope m and intercept b to make this sum as small as possible, least squares produces the line that best summarises the linear trend in the data.
The method was developed independently by Carl Friedrich Gauss and Adrien-Marie Legendre in the late 18th century. Gauss used it to predict the orbit of the dwarf planet Ceres from a limited number of astronomical observations, successfully locating it after it disappeared behind the sun. Since then, least squares regression has become one of the most widely used methods in data analysis, statistics, econometrics, engineering, and the natural sciences.
The regression line has two key properties. First, it always passes through the mean point (x̄, ȳ): substituting the mean x into the equation gives exactly the mean y. Second, the sum of all residuals is zero: the positive and negative deviations cancel out. These properties confirm that the line is centred on the data in a precise mathematical sense.
This calculator accepts x and y data in the Regression mode and computes the slope m, intercept b, Pearson correlation coefficient r, R² (coefficient of determination), mean values, and a full residuals table. Predict mode lets you enter a slope and intercept directly and forecast y for any x value.
📐 Formula
📖 How to Use This Calculator
Steps
💡 Example Calculations
Example 1: Classic textbook data set
x = 1,2,3,4,5 and y = 2,4,5,4,5
Example 2: Perfect linear relationship
x = 0,1,2,3 and y = 1,3,5,7
Example 3: Negative slope (inverse relationship)
x = 1,2,3,4,5 and y = 10,8,6,4,2
Example 4: Predict using a known line
Regression line ŷ = 0.6x + 2.2, predict at x = 10
❓ Frequently Asked Questions
🔗 Related Calculators
What is the least squares regression line?
The least squares regression line, also called the line of best fit, is the straight line ŷ = mx + b that minimises the sum of the squared vertical distances from each data point to the line. These vertical distances are called residuals. By minimising the sum of their squares, least squares produces the line that gives the best overall prediction for the data.
How do you calculate the slope of the regression line?
The slope formula is m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²), where n is the number of data points, Σxy is the sum of products of paired x and y values, Σx and Σy are the sums of x and y, and Σx² is the sum of squared x values. Once the slope is known, the intercept is b = ȳ − m·x̄.
What does R² mean in regression?
R² (the coefficient of determination) measures what proportion of the variation in y is explained by the linear relationship with x. An R² of 0.85 means 85% of the variability in y is accounted for by the regression line. R² ranges from 0 to 1 for standard regression, with values closer to 1 indicating a better linear fit.
What is the difference between r and R²?
The correlation coefficient r measures the strength and direction of the linear relationship between x and y, ranging from −1 to +1. A negative r means the slope is negative; a positive r means the slope is positive. R² equals r squared and measures how much variation in y the line explains (always between 0 and 1, ignoring direction). For simple linear regression, R² = r².
What is a residual in regression?
A residual is the difference between an observed y value and the value predicted by the regression line: residual = y − ŷ. Positive residuals mean the point is above the line; negative residuals mean it is below. The least squares method minimises the sum of squared residuals. A residual table (shown by this calculator) lists each data point's residual, helping identify outliers.
Does the regression line always pass through the mean?
Yes. The least squares regression line always passes through the point (x̄, ȳ), the means of x and y. This is a mathematical property of the formulas: substituting x = x̄ into ŷ = mx̄ + b = mx̄ + (ȳ − mx̄) = ȳ confirms it. If the predicted value at the mean x does not equal the mean y, something is wrong with the calculation.
How do you predict y from the regression line?
Substitute the desired x value into the regression equation: ŷ = mx + b. For example, if the regression line is ŷ = 2.5x + 1.3 and you want to predict at x = 4, then ŷ = 2.5(4) + 1.3 = 11.3. Use the Predict mode in this calculator to do this instantly once you know the slope and intercept.
What are the assumptions of least squares regression?
The main assumptions are: (1) linearity — the true relationship between x and y is linear; (2) independence — observations are not correlated with each other; (3) equal variance (homoscedasticity) — the spread of residuals is roughly constant across all x values; and (4) normality of residuals — the residuals follow a roughly normal distribution. Violations of these assumptions affect the reliability of predictions and inference.
Can regression be used for prediction outside the data range?
Extrapolation (predicting y for x values outside the observed range) is risky. The regression line is only guaranteed to describe the relationship within the range of the observed data. The relationship may not remain linear, may level off, or may reverse outside that range. Treat extrapolated predictions with caution and consider whether they make practical sense.
What does a negative slope mean?
A negative slope means y tends to decrease as x increases. For example, a negative slope in a regression of temperature (x) on ice cream sales (y) would be unusual; but a negative slope in altitude (x) versus air pressure (y) means higher altitude means lower pressure. The sign of the slope and correlation coefficient r always agree.
How many data points are needed for regression?
At least 2 data points are required to fit a line (two points determine a line exactly, with r = ±1 and R² = 1). However, meaningful regression for prediction requires more — typically 10 or more points — to get stable estimates of slope and intercept. With very few points the line can appear to fit well by chance. This calculator accepts as few as 2 points but shows residuals so you can judge the fit quality.
What is the line of best fit used for in real life?
The least squares regression line is used widely: in economics to forecast sales or GDP from indicator variables, in biology to study dose-response relationships, in physics to extract constants from experimental data (such as fitting Ohm's law to voltage-current measurements), in medicine to calibrate screening tests, and in machine learning as the foundation of linear regression models used for prediction.