Parameters
While paused, moving a slider updates the results immediately.
\(a = \bar{y} - b\bar{x}\)
\(R^2 = 1 - \frac{SS_E}{SS_T} = \frac{SS_R}{SS_T}\)
Generate random data by changing the noise, true slope and number of points, and watch the line approach the least-squares solution. See the coefficient of determination R², residuals and correlation coefficient in real time to get a feel for how least squares works.
While paused, moving a slider updates the results immediately.
The model of linear regression assumes a straight line \(y = a + b x\) (a: intercept, b: slope) for the observed data points \((x_i, y_i)\). The least-squares method finds the parameters \(a, b\) that minimise the sum of squares \(S = \sum_{i=1}^{n} \varepsilon_i^2\) of the vertical errors (residuals) \(\varepsilon_i = y_i - (a + b x_i)\). Solving \(\frac{\partial S}{\partial a} = 0\) and \(\frac{\partial S}{\partial b} = 0\) gives the analytical result \(b = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2}\), \(a = \bar{y} - b \bar{x}\), where \(\bar{x}, \bar{y}\) are the means. The coefficient of determination \(R^2 = 1 - \frac{\sum \varepsilon_i^2}{\sum (y_i - \bar{y})^2}\) expresses the goodness of fit as a value from 0 to 1 and, for simple regression, equals the square of the correlation coefficient r. In this simulator the data are regenerated every time you change the noise, true slope or number of points, and you can watch in real time as the line approaches the least-squares solution and the residuals and R² change. This gives an intuitive feel for how least squares captures the scatter in the data and determines the line.
Industrial use
In manufacturing, linear regression is widely used to analyse component durability test data. For example, the relationship between temperature and fatigue life is modelled by least squares and the fit is assessed with R², helping to plan prototypes and additional tests. In semiconductor manufacturing, regression is also used to capture the relationship between equipment parameters such as gas flow rate and results such as film thickness, and to tune process conditions.
Research and education
In university physics labs, students plot spring extension against the mass of the weights and verify Hooke's law by linear regression. Turning on the residual lines in this tool shows visually what least squares is minimising. In environmental science, it is also used as teaching material for analysing long-term changes in temperature or carbon dioxide concentration and discussing the validity of a model from R².
Relation to CAE analysis and practical use
When validating CAE simulation results, linear regression is used to assess the correlation between experimental data and analysis values. For example, you regress the stresses from an FEM analysis against measured values and check whether the slope is close to 1, the intercept close to 0 and R² high enough (even with a high R², a slope that departs from 1 indicates a systematic error). This tool is a learning aid for checking that idea quickly.
It is tempting to think that "the more data points, the higher R² must be", but forcing a straight line through data that contain outliers or a non-linear relationship can lower R². R² shows the goodness of fit, but it depends heavily on the quality and distribution of the data, so simply adding samples does not necessarily improve it.
A common misunderstanding is that "all data points should fall inside the 95% confidence interval". In fact the confidence interval expresses the uncertainty in the average position of the regression line, which is different from the prediction interval that describes the scatter of individual points. Many points lying outside the confidence interval is therefore not a statistical problem; take care not to confuse the two.
It is tempting to think that "least squares is a universal method that works for any data", but it is very sensitive to outliers: a single extreme point can distort the regression line badly. When the data are not linear or the errors are not normally distributed, other regression methods or data preprocessing should be considered.
The points in this tool are generated around the true line y = x + 1.5 (true slope 1, intercept 1.5), with x drawn uniformly from 0.4 to 9.6. The variance of x is 9.2²/12 ≈ 7.05, so a rule of thumb for the coefficient of determination at noise σ is R² ≈ 7.05/(7.05 + σ²): R² ≈ 0.97 for σ = 0.5, R² ≈ 0.78 for the initial σ = 1.4 and R² ≈ 0.44 for σ = 3. Because the points are regenerated randomly each time, the displayed value scatters around this estimate (about 0.70–0.85 measured with n = 24 and σ = 1.4). The scatter (standard error) of the slope b is about σ/√(n × 7.05), roughly 0.11 for σ = 1.4 and n = 24.