General Math
Linear Regression Calculator: Least Squares Method Explained
Understand linear regression, the least squares method, R² interpretation, and how to find the best-fit line. Complete guide with examples.
Try it now
Linear Regression Calculator
Calculate line of best fit, R², slope, and intercept from data points.
Linear regression is the workhorse of statistical modeling — the single most widely used technique for understanding relationships between variables. From predicting housing prices and forecasting sales to analyzing scientific experiments and identifying market trends, linear regression provides a mathematically rigorous framework for fitting a straight line through scattered data points and making quantitative predictions.
Our linear regression calculator computes the best-fit line instantly from your data, providing the slope, intercept, R² value, correlation coefficient, and predicted values. This guide explains the mathematical foundation so you can interpret results with confidence and apply regression appropriately.
What Is Linear Regression?
Linear regression assumes that the relationship between an independent variable x and a dependent variable y can be approximated by a straight line: y = mx + b. The goal is to find the specific values of slope m and intercept b that best represent the underlying trend in your data, even though individual data points rarely fall exactly on any line.
The “best” line is defined mathematically as the one that minimizes the sum of squared vertical distances (residuals) between the observed data points and the corresponding predicted points on the line. This minimization criterion — the method of least squares — gives linear regression its alternative name: Ordinary Least Squares (OLS) regression.
The Least Squares Method
The Mathematical Objective
Given n data points (x₁, y₁), (x₂, y₂), …, (xₙ, yₙ), we want to find m and b that minimize:
S = Σᵢ₌₁ⁿ (yᵢ − (mxᵢ + b))²
Each term (yᵢ − ŷᵢ)² represents the squared residual for point i — how far the actual y-value is from the predicted value on our line. Squaring ensures that points above and below the line don’t cancel each other, and it penalizes large deviations more heavily than small ones.
Deriving the Normal Equations
To minimize S, we take partial derivatives with respect to m and b, set them equal to zero, and solve the resulting system of two linear equations (called the “normal equations”):
∂S/∂b = 0 → Σ(yᵢ − mxᵢ − b) = 0 ∂S/∂m = 0 → Σxᵢ(yᵢ − mxᵢ − b) = 0
The Solution Formulas
Solving the normal equations yields:
Slope: m = [n·Σ(xᵢyᵢ) − Σxᵢ·Σyᵢ] / [n·Σ(xᵢ²) − (Σxᵢ)²]
Intercept: b = ȳ − m·x̄
Where x̄ and ȳ are the means of the x and y values respectively. The intercept formula tells us something elegant: the regression line always passes through the point (x̄, ȳ) — the centroid of the data.
Worked Example
Consider the data points: (1, 2), (2, 4), (3, 5), (4, 4), (5, 5).
Step 1: Compute sums
- n = 5
- Σx = 1+2+3+4+5 = 15
- Σy = 2+4+5+4+5 = 20
- Σxy = 1(2)+2(4)+3(5)+4(4)+5(5) = 2+8+15+16+25 = 66
- Σx² = 1+4+9+16+25 = 55
Step 2: Calculate slope m = [5(66) − 15(20)] / [5(55) − 15²] = [330 − 300] / [275 − 225] = 30/50 = 0.6
Step 3: Calculate intercept x̄ = 15/5 = 3, ȳ = 20/5 = 4 b = 4 − 0.6(3) = 4 − 1.8 = 2.2
Best-fit line: y = 0.6x + 2.2
You can verify this by entering these five points into our linear regression calculator and comparing the output with the hand calculation above.
Understanding R² (Coefficient of Determination)
R² is perhaps the most important single number in regression analysis. It measures what proportion of the total variation in y is explained by the linear relationship with x.
The Formula
R² = 1 − (SS_res / SS_tot)
Where:
- SS_res = Σ(yᵢ − ŷᵢ)² (sum of squared residuals — unexplained variation)
- SS_tot = Σ(yᵢ − ȳ)² (total sum of squares — total variation in y)
Equivalently, for simple linear regression:
R² = [n·Σ(xy) − Σx·Σy]² / {[n·Σ(x²) − (Σx)²] · [n·Σ(y²) − (Σy)²]}
Interpreting R²
- R² = 1.0: Perfect fit. All points lie exactly on the line. 100% of variation is explained.
- R² = 0.9: Strong linear relationship. 90% of y-variation is explained by x.
- R² = 0.5: Moderate relationship. Half the variation is explained, half is not.
- R² = 0.0: No linear relationship exists. The line explains nothing.
R² Doesn’t Tell the Whole Story
A high R² doesn’t guarantee:
- That the relationship is causal (correlation ≠ causation)
- That a linear model is appropriate (a curved pattern can have moderate R² with a linear fit)
- That predictions outside the data range (extrapolation) are reliable
- That the model is well-specified (omitted variables may matter)
Always examine residual plots alongside R² to check model adequacy.
Pearson Correlation Coefficient (r)
The correlation coefficient r measures the strength and direction of the linear relationship between x and y. For simple linear regression, R² = r². The sign of r indicates direction:
- r > 0: Positive correlation (y increases as x increases)
- r < 0: Negative correlation (y decreases as x increases)
- r = 0: No linear correlation
The correlation coefficient always satisfies −1 ≤ r ≤ 1. Values close to ±1 indicate strong linear relationships; values near 0 indicate weak or no linear relationship.
Assumptions of Linear Regression
For the results of OLS regression to be statistically valid (for hypothesis testing and confidence intervals), several assumptions should hold:
- Linearity: The true relationship between x and y is approximately linear.
- Independence: Observations are independent of each other.
- Homoscedasticity: The variance of residuals is constant across all levels of x.
- Normality: Residuals are approximately normally distributed (important for small samples).
- No perfect multicollinearity (for multiple regression): Predictors are not perfectly correlated.
Violating these assumptions doesn’t invalidate the line of best fit as a descriptive tool, but it affects the reliability of statistical inferences drawn from the model.
Making Predictions
Once you have the regression equation y = mx + b, you can predict y for any given x:
ŷ = m × x_new + b
Interpolation (predicting within the range of your data) is generally reliable. Extrapolation (predicting outside the data range) is risky because the linear relationship may not hold beyond the observed region.
Our linear regression calculator shows predicted values alongside actual values and residuals for each data point, helping you assess how well the model fits individual observations.
Applications of Linear Regression
Economics and Business
- Forecasting sales based on advertising spend
- Estimating salary based on years of experience
- Modeling demand as a function of price
Natural Sciences
- Calibration curves in analytical chemistry
- Hooke’s law: force vs displacement
- Beer-Lambert law: absorbance vs concentration
Social Sciences
- Predicting test scores from study hours
- Analyzing crime rates vs socioeconomic factors
- Modeling voter turnout from demographic variables
Engineering
- Material stress-strain relationships in the elastic region
- Sensor calibration (voltage to physical measurement)
- Process control and quality monitoring
Limitations and When Not to Use Linear Regression
Linear regression is inappropriate when:
- The relationship is clearly non-linear (consider polynomial, logarithmic, or exponential models)
- The data has significant outliers that dominate the fit
- There is heteroscedasticity (fan-shaped residual patterns)
- The observations are not independent (e.g., time series with autocorrelation)
- You have very few data points (minimum 5–10 recommended)
In these cases, consider robust regression, weighted least squares, polynomial regression, or non-parametric methods.
Beyond Simple Linear Regression
Simple linear regression (one predictor) naturally extends to multiple linear regression (many predictors): y = b₀ + b₁x₁ + b₂x₂ + … + bₖxₖ. The mathematical framework is identical but expressed in matrix notation. The normal equations become (XᵀX)β = Xᵀy, solved as β = (XᵀX)⁻¹Xᵀy.
This connection to matrix algebra underscores how deeply interconnected mathematical tools are. The same matrix operations used in our matrix calculator find direct application in computing regression coefficients for complex multi-variable models.
OurDailyCalc Team
OurDailyCalc — beautiful tools for everyday calculations.