Least Squares Calculator

Enter your (X, Y) data pairs into the Least Squares Calculator to find the best-fit line through your data. Input your data points as comma-separated or newline-separated values, and get back the slope (m), Y-intercept (b), the full regression equation (ŷ = mx + b), and the R² (coefficient of determination) showing how well the line fits your data.

Enter X values separated by commas, spaces, or new lines.

Enter Y values separated by commas, spaces, or new lines. Must match count of X values.

Enter an X value to predict the corresponding Y using the regression line.

Forces the regression line to pass through the origin (0, 0).

Results

Regression Equation

--

Slope (m)

--

Y-Intercept (b)

--

R² (Coefficient of Determination)

--

Correlation Coefficient (r)

--

Number of Data Points (n)

--

Predicted Y (for given X)

--

Scatter Plot with Regression Line

Results Table

Frequently Asked Questions

What is the least squares method?

The least squares method is a mathematical technique for finding the best-fit line through a set of data points by minimizing the sum of the squared differences (residuals) between the observed Y values and the values predicted by the line. It produces the line ŷ = mx + b that is mathematically closest to all your data points simultaneously.

What is linear regression?

Linear regression is a statistical method used to model the relationship between two variables — a predictor (X) and a response (Y) — using a straight line. The resulting equation can be used to describe the relationship between variables and to predict Y values for new X inputs.

What is ordinary least squares (OLS)?

Ordinary Least Squares is the most common estimation method for linear regression. It finds the slope and intercept of the regression line by minimizing the total sum of squared residuals. When people refer to 'least squares regression,' they typically mean OLS.

How is the slope calculated in least squares regression?

The slope (m) is calculated as m = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²], where n is the number of data points. Once the slope is known, the Y-intercept is found using b = (Σy − m·Σx) / n. Our calculator computes these automatically from your data.

What does R² mean in linear regression?

R² (the coefficient of determination) measures how well the regression line fits the data. It ranges from 0 to 1: an R² of 1 means the line perfectly explains all variation in Y, while an R² of 0 means the line explains none of it. For example, an R² of 0.85 means 85% of the variance in Y is explained by X.

What is a residual in regression analysis?

A residual is the difference between an observed Y value and the predicted Y value (Ŷ) from the regression line: Residual = Y − Ŷ. Positive residuals mean the actual value is above the line; negative residuals mean it is below. The least squares method minimizes the sum of all squared residuals.

When should I force the regression line through zero?

You should force a zero Y-intercept only when there is a strong theoretical reason to believe that Y must equal 0 when X equals 0. For example, if you are modeling distance traveled when speed is 0. In most cases, you should allow the intercept to be estimated freely from the data.

How many data points do I need for least squares regression?

You need at least 3 data points to produce a meaningful regression line, though more data generally leads to more reliable results. With only 2 points, any line is a perfect fit. Most statisticians recommend at least 10 to 20 data points for reliable inference.

More Statistics Tools