About Linear Regression — OLS Least Squares
This simulation fits a straight line to a scatter of data points using ordinary least squares (OLS). You click on the canvas to add points, and the line updates instantly to minimise the sum of squared vertical residuals. The slope is computed as m = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)², and the intercept as b = ȳ − m·x̄, giving the unique best-fit line for any set of two or more points.
The mode buttons let you add, drag, or delete points, while Show Residuals toggles the dashed vertical lines OLS is minimising. Preset datasets demonstrate perfect linear fits, the effect of a single outlier, no correlation, and the limits of fitting a straight line to quadratic data. Live statistics report the slope, intercept, R², Pearson r, point count and SSE. Regression like this underpins forecasting, trend analysis and machine learning.
Frequently Asked Questions
What is linear regression?
Linear regression finds the straight line y = m·x + b that best describes the relationship between two variables. This page uses ordinary least squares, which chooses the slope m and intercept b that minimise the total squared vertical distance between each data point and the line.
How does ordinary least squares work?
OLS measures the vertical residual for every point, squares it, and sums the squares. It then selects the single line that makes that sum (the SSE) as small as possible. The minimum has a closed-form solution, so the line is computed directly rather than by trial and error.
What do the controls on this page do?
Add Points, Drag and Delete change how clicks affect the scatter; Show Residuals toggles the dashed vertical error lines; Undo reverts the last change and Clear All empties the canvas. The preset buttons load example datasets, and the stat cards update live with slope, intercept, R², Pearson r, n and SSE.
What is the equation used to compute the line?
The slope is m = Σ(xᵢ−x̄)(yᵢ−ȳ) / Σ(xᵢ−x̄)², the covariance of x and y divided by the variance of x. The intercept then follows from b = ȳ − m·x̄, which forces the line to pass through the mean point (x̄, ȳ).
What does R² mean?
R² is the coefficient of determination, the fraction of the total variance in y explained by the line: R² = 1 − SSE/SST. A value of 1 means a perfect fit with no residual error, while 0 means the line explains nothing beyond the mean of y.
How is Pearson r related to R²?
Pearson r measures the strength and direction of the linear relationship, ranging from −1 to +1. For simple linear regression it equals the signed square root of R², so the simulation takes the sign from the slope: r = sign(m)·√R².
Why does a single outlier change the line so much?
Because OLS squares each residual, points far from the line contribute disproportionately to the total error. A single distant outlier can drag the slope and intercept noticeably, as the Outlier Effect preset demonstrates. This sensitivity is why least squares is not robust to extreme values.
What happens with the quadratic preset?
The Quadratic preset generates curved data that no straight line can match well. OLS still returns its best linear fit, but residuals stay large and R² drops, illustrating that linear regression only captures linear trends and will systematically miss curvature in the data.
Is the simulation mathematically accurate?
Yes. It uses the exact closed-form OLS formulas for slope and intercept and computes SSE, SST, R² and r directly from the points. The only practical limits are floating-point rounding and the fact that values are clamped to the visible 0–10 plotting range.
Where is linear regression used in the real world?
It is one of the most widely used statistical tools, appearing in economics, biology, engineering and finance for trend estimation and forecasting. It also serves as a foundational model in machine learning, where the same least-squares idea generalises to many predictor variables.