Calculators.Life Calculator
Statistics

Least Squares Regression Line Calculator

Input Parameters

Scenarios
Enter at least 2 pairs of numbers. E.g. '1, 2' or '1 2' on each line.
Enter an X value to compute the predicted y-hat using the regression equation.

Computed Results

Regression Equation (ŷ = a + bx)
ŷ = 1.3 + 0.9x
Slope (b)0.9
Y-Intercept (a)1.3
Correlation Coefficient (r)0.9
Coefficient of Determination (R²)0.81
Sample Size (n)5
Predicted Y (ŷ at input X)6.7

The least squares regression line calculator determines the best-fitting straight line through a set of data points. It calculates the regression equation, slope, y-intercept, correlation coefficient, and coefficient of determination to help you analyze linear trends and predict future values based on your data.

How the Least Squares Regression Formula Works

The least squares method finds the line that minimizes the sum of the squared differences (residuals) between the observed values and the values predicted by the line. The resulting regression equation is written as:

ŷ = a + bx

In this equation:

The calculator determines the slope (b) and intercept (a) using the sums of the x and y coordinates, their products, and their squares. It also calculates the correlation coefficient (r) to measure the strength and direction of the linear relationship, and the coefficient of determination (R²) to show the proportion of the variance in the dependent variable that is predictable from the independent variable.

Verified Calculation Examples

Here are several scenarios demonstrating how the calculator processes different datasets.

Example 1: Standard Positive Trend

If you input the data pairs (1, 2), (2, 3), (3, 5), (4, 4), and (5, 6) and want to predict Y for an X value of 6, the calculator determines a sample size of 5. The resulting regression equation is ŷ = 1.3 + 0.9x. The correlation coefficient (r) is 0.9, and R² is 0.81. The predicted Y value for X = 6 is 6.7.

Example 2: Perfect Positive Correlation

For a dataset with a perfect linear fit, such as (1, 10), (2, 20), (3, 30), (4, 40), and (5, 50), the slope is exactly 10 and the y-intercept is 0. The equation is ŷ = 0 + 10x. Both r and R² are exactly 1. Predicting Y for X = 10 yields exactly 100.

Example 3: Negative Correlation

If the data trends downward, such as (10, 50), (20, 40), (30, 30), and (40, 20), the calculator identifies a negative slope. The equation is ŷ = 60 - 1x. The correlation coefficient is -1, indicating a perfect negative linear relationship. Predicting Y for X = 25 results in 35.

Example 4: Vertical Line Edge Case

If all X values are identical, such as (5, 10), (5, 20), and (5, 30), a standard slope cannot be calculated because the line is vertical. The calculator identifies this boundary condition and outputs Vertical line (x = 5), with the slope, intercept, r, and R² all returning 0.

Practical Use Cases

Linear regression is widely used in statistics and data analysis to understand relationships between variables. Common applications include:

Frequently Asked Questions

What is the difference between r and R²?

The correlation coefficient (r) measures the strength and direction of the linear relationship between two variables, ranging from -1 to 1. The coefficient of determination (R²) is the square of r, ranging from 0 to 1, and represents the percentage of the variance in the dependent variable that can be explained by the independent variable.

How many data points are required to calculate a regression line?

A minimum of two data points is required to calculate a straight line. However, more data points generally provide a more reliable and meaningful regression model.

What happens if my data points form a vertical line?

If all the X values in your dataset are the same, the slope is mathematically undefined (division by zero). The calculator detects this and outputs a vertical line equation (e.g., x = 5) rather than a standard y-intercept equation.

Author portrait
About the Author
Arnab Biswas

Calculator developer and SEO strategist who builds practical online calculators and tests calculation logic, input handling, and expected results using real-world scenarios.