This simulation shows the effect of noise and outliers in linear regression. The line \( y = x + 1 \) is shown in the top plot (dashed black line). Experimental data are generated from this equation by adding white Gaussian noise and are shown on the same plot (blue circles). These data are fit with a linear model (solid green line). The residuals of that fitting are shown in the lower plot. Change the amount of noise in the data using the slider bar and observe the change in the equation of best fit, the \( r^{2} \) value, and the residuals plot. To add an outlier, click any location on the top plot to add the point. The point will replace the middle point of the original data set. The fit to the original data and the data with the outlier will both be shown in the upper plot. The residuals of the original and new fits are shown in the lower plot. Observe the change in the equation of best fit, the \( r^{2} \) value, and the residuals plot. Moving the noise slider generates a new data set and removes the outlier.
Experimental data \( (x_{i},\, y_{i}) \) are generated from the line \( y = x + 1 \) by adding zero-mean white Gaussian noise. The noise slider sets the signal-to-noise ratio (SNR, in dB), so the standard deviation of the added noise is
\[ \sigma = \sqrt{10^{-\mathrm{SNR}/10}}. \]The data are fit with the linear model
\[ \hat{y} = \hat{\beta}_{0} + \hat{\beta}_{1}\,x, \]where the least-squares estimates of the slope \( \hat{\beta}_{1} \) and intercept \( \hat{\beta}_{0} \) are
\[ \hat{\beta}_{1} = \frac{\displaystyle\sum_{i=1}^{n}(x_{i}-\bar{x})(y_{i}-\bar{y})} {\displaystyle\sum_{i=1}^{n}(x_{i}-\bar{x})^{2}}, \qquad \hat{\beta}_{0} = \bar{y} - \hat{\beta}_{1}\,\bar{x}, \]and \( \bar{x} \) and \( \bar{y} \) are the means of the \( x_{i} \) and \( y_{i} \) data and \( n \) is the number of data points. The correlation coefficient \( r \) is
\[ r = \frac{\displaystyle\sum_{i=1}^{n}(x_{i}-\bar{x})(y_{i}-\bar{y})} {\sqrt{\displaystyle\sum_{i=1}^{n}(x_{i}-\bar{x})^{2}}\; \sqrt{\displaystyle\sum_{i=1}^{n}(y_{i}-\bar{y})^{2}}}, \]and \( r^{2} \) is the coefficient of determination. The residual for each data point is the difference between the measured value and the value predicted by the fit,
\[ e_{i} = y_{i} - \hat{y}_{i}, \qquad \hat{y}_{i} = \hat{\beta}_{0} + \hat{\beta}_{1}\,x_{i}, \]and the lower plot shows the residuals \( y_{i} - \hat{y}_{i} \) versus the predicted values \( \hat{y}_{i} \). For a good fit, the residuals scatter randomly about zero with no trend; an outlier produces a large residual and can significantly change the best-fit line and lower \( r^{2} \).
This simulation was created in the Department of Chemical and Biological Engineering at University of Colorado Boulder for LearnChemE.com by John L. Falconer using Claude AI. It is a JavaScript/HTML5 implementation of a MATLAB simulation by Keith Regner. It was prepared with financial support from the National Science Foundation (DUE 2336987 and 2336988). Address any questions or comments to LearnChemE@gmail.com.