Linear Regression Calculator

Find the best-fit line (y = mx + b) for two data sets. Get slope, intercept, r, r squared, and predictions using least squares.

Data Input

Separate numbers with spaces or new lines. Use a dot for decimals (1.5). X and Y must have the same count.

Results

Enter two equal-length lists of numbers and click Calculate.

How Does the Formula Work?

Linear regression fits a straight line through paired data so you can describe a relationship and make predictions. Enter your X values (the independent variable, such as hours studied) and Y values (the dependent variable, such as exam score), and the calculator returns the best-fit equation y = mx + b, the slope and intercept, the correlation r, the coefficient of determination r², and an optional prediction. The r it reports is identical to the value from our correlation calculator for the same data.

m = Σ((xi - x̄)(yi - ȳ)) / Σ(xi - x̄)²
b = ȳ - m·x̄
r = Σ((xi - x̄)(yi - ȳ)) / √(Σ(xi - x̄)² × Σ(yi - ȳ)²)
ŷ = m·x + b
Example: X = [1, 2, 3, 4, 5], Y = [2, 4, 6, 8, 10] → y = 2x + 0, r = 1

Reading the Slope and Intercept

The slope m is the heart of the model: it says how much Y changes, on average, for each one-unit increase in X. A slope of 2 means Y climbs by 2 for every step in X; a negative slope means Y falls as X rises. The intercept b is where the line crosses the Y axis, the predicted value of Y when X equals 0. The intercept is only meaningful when X = 0 is realistic for your data. For example, a line relating adult height to weight has an intercept for zero height that carries no physical meaning, even though it is needed to position the line.

What r² Tells You

The coefficient of determination r² is the fraction of the variation in Y that the line explains, from 0 to 1. An r² of 0.85 means the model accounts for 85% of the ups and downs in Y, leaving 15% to other factors and noise. A high r² signals a tight linear fit; a low r² warns that a straight line is a poor summary of the data, often because the true relationship is curved. Because r² is r squared, it discards the sign, so it measures explanatory power without telling you the direction. For direction, read the slope or r.

The Least Squares Method

This tool uses ordinary least squares (OLS), the standard regression method. Among all possible straight lines, OLS chooses the one that minimizes the sum of the squared vertical distances between each data point and the line. Squaring those distances penalizes large misses more heavily and yields a single, unique best-fit line with a clean formula. OLS is the foundation of most statistical modeling, from simple trend lines in a spreadsheet to the building blocks of machine learning.

Where Regression Is Used

Regression is used wherever people want to quantify a trend or forecast an outcome: sales teams project revenue from advertising spend, scientists calibrate instruments by fitting measured readings to known standards, economists model demand against price, and engineers relate load to deflection. The equation makes the relationship explicit and lets you estimate Y for values of X you have not measured, provided you stay within a sensible range.

Methodology

This calculator uses the ordinary least squares method with the mean-centered formula, which stays numerically accurate even for large values where a raw-sum formula would overflow. The correlation r is computed exactly as in our correlation calculator so the two tools always agree. A regression line is reported as undefined when every X value is identical (a vertical line has no defined slope), when the lists have different lengths, or when fewer than two pairs are provided. Each release is tested against known reference values.

Tips & Recommendations

Not Causation

A fitted line and a high r² never prove that X causes Y. The relationship may run the other way or be driven by a hidden third factor.

Do Not Extrapolate

Predictions are reliable only inside the range of your data. Projecting far beyond it assumes the trend continues, which is often wrong.

Watch Outliers and Curves

A single outlier can swing the line sharply. A low r² often means the data is curved, not linear, so a straight line does not fit.

Use Enough Data

A line through very few points is unstable. Aim for 10 or more pairs before trusting the slope, intercept, or any prediction.

Frequently Asked Questions

What is linear regression?

Linear regression finds the straight line that best fits a set of paired data points. It produces an equation y = mx + b, where m is the slope and b is the intercept, so you can describe the relationship and predict Y from a new X. This tool uses the ordinary least squares method, which minimizes the sum of squared vertical distances from the points to the line.

What does the slope (m) mean?

The slope tells you how much Y changes for each 1-unit increase in X. A slope of 2 means Y rises by 2 units for every 1-unit rise in X; a slope of -0.5 means Y falls by half a unit. The intercept b is the predicted value of Y when X is 0.

What is r squared and what is a good value?

r squared (the coefficient of determination) is the share of variation in Y explained by the model, from 0 to 1. An r squared of 0.85 means the line explains 85% of the variation. What counts as good depends on the field: physical measurements may need above 0.95, while social science often treats 0.3 to 0.5 as meaningful. A low r squared may mean the relationship is not linear.

What is the difference between regression and correlation?

Correlation measures the strength and direction of a linear relationship with a single number, r. Regression goes further and gives the equation of the line, so it can make predictions. This calculator reports the same r as our correlation calculator, plus the slope, intercept, and predictions.

How reliable is a prediction from this model?

Predictions are most trustworthy inside the range of your data. Predicting far outside that range (extrapolation) is risky because the linear pattern may not continue. Reliability also depends on a high r squared, enough data points (ideally 10 or more), and the absence of strong outliers, which can distort the line.

Recent Calculations

No calculations yet

Last updated: July 10, 2026 Reviewed by: MathGyro Editorial Team