Least-Squares Fit

Scatter plot with a least-squares line the learner manipulates: drag data points or a candidate line and watch residuals, RSS, R^2 and the ANOVA split respond; drag x0 to read confidence and prediction bands; drag one point to see leverage, standardized residual and Cook's distance with and without it; recentre a polynomial or interaction model and watch only the lower-order coefficients change.

Used in: Introduction to Statistics

Props

PropTypeDefaultDescription
modestringfitView: 'fit' (drag data, line refits), 'line' (drag a candidate line vs the least-squares minimum), 'anova' (SST = SSR + SSE for a highlighted point), 'bands' (CI and prediction bands at a draggable x0), 'influence' (fit with and without one point; leverage, standardized residual, Cook's D), 'polynomial' (polynomial fit with a draggable centring origin), 'interaction' (conditional slopes of y = b0 + b1 x1 + b2 x2 + b3 x1 x2).
points[number, number][][[1,2],[2,3],[3,5],[4,4],[5,6]]Data as [x, y] pairs. Defaults per mode: influence adds the point (10, 6); polynomial uses [[1,3],[2,5],[3,6],[4,6],[5,5],[6,3]]. Ignored in 'interaction' mode.
presets(string | {label: string, points: [number, number][]})[][]Extra data sets offered in a 'Data set' dropdown (after `points`, if given). Built-in names: study-hours, linear, parabola, random-cloud (uses `seed`), high-leverage-off-trend, high-leverage-on-trend, outlier-at-centre.
titlestringper modeTitle bar text.
xLabelstring$x$x-axis title ($...$ TeX allowed); '$x_1$' in interaction mode.
yLabelstring$y$y-axis title ($...$ TeX allowed).
xRange[number, number]data extent paddedVisible x window (also the drag bounds). In 'bands' mode the default widens to include x0.
yRange[number, number]data extent paddedVisible y window (also the drag bounds); in 'bands' mode the default includes the bands.
editablebooleantrueWhether data points can be dragged (and, in fit/anova modes, added by clicking the plot).
readoutsstring[]per modeRows of the readout panel, in order. fit/anova: n, slope, intercept, r, r2, sse, rss, sst, ssr, s, F, t, t2, p, df, residualSum, xbar, ybar. line: candidate, intercept, slope, rss, minRss, excess, residualSum, lsSlope, lsIntercept, s. bands: x0, yhat, ci, ciHalfWidth, pi, piHalfWidth, ratio, seMean, sePred, v0, tStar, floor, s, n, xbar, coverage. influence: point, leverage, leverageCutoff, residual, residualSdFactor, residualSd, standardized, cooks, slopeWith, slopeWithout, interceptWith, interceptWithout, s, sWithout, r2, n. polynomial: sse, r2, origin, xbar, fittedAtOrigin, slopeAtOrigin, corrRaw, corrCentered, seTop. interaction: conditionalSlope, b1, b3, a0, a1, a2, a3.
showResidualsbooleantruefit and line modes: start with the vertical residual segments shown (learner can toggle).
showSquaresbooleanfalsefit and line modes: start with each squared residual drawn as a square (learner can toggle).
showTablebooleanfalseShow the mode's table: per-point fitted values and residuals (fit), the ANOVA table (anova), or per-point h, e, r, D (influence).
showMeanbooleanfalsefit mode: mark the centroid (x-bar, y-bar), which the least-squares line always passes through.
line[number, number] | {intercept, slope}[y-bar, 0]line mode: the learner's starting candidate line as [intercept, slope].
showLeastSquaresbooleanfalseline mode: start with the least-squares line revealed (learner can toggle).
showRssSurfacebooleanfalseline mode: add a contour plot of RSS over (intercept, slope) with the candidate as a draggable point.
levelnumber0.95bands mode: starting confidence level (0.5 to 0.999); the learner can slide it.
x0numbermiddle of the databands mode: starting position of the draggable target x0.
bandsstring[]["mean", "prediction"]bands mode: which bands to draw, any of 'mean' (confidence band for the mean response) and 'prediction' (prediction band for a new response).
shadeOutsideDatabooleanfalsebands mode: grey out the x range outside the data and label it extrapolation.
replicateMaxnumber0bands mode: if > 1, a 'copies of each x' slider from 1 to this value replicates the design with s held fixed (n grows, the CI shrinks, the PI does not).
simulatebooleanfalsebands mode: add a 'Draw new sample' button that regenerates y from the initial fitted line plus normal noise (sd = initial s), refits, and counts how often the CI covers the true mean and the PI covers a new response at x0.
seednumber | string1Seed for the 'random-cloud' preset and for simulate draws.
dragIndexnumberlast pointinfluence mode: 0-based index of the initially selected point (the fit without it is drawn dashed).
cutoffsnumber[][2, 3]influence mode: |standardized residual| cutoffs for 'large' and 'extreme' flags.
degreenumber2polynomial mode: starting degree 1, 2 or 3 (learner can switch).
originnumber | 'mean''mean' (polynomial), 0 (interaction)polynomial mode: starting centring origin c for the predictor; interaction mode: starting centring constant m for x2.
coefficients[number, number, number, number][10, 2, 1, 0.5]interaction mode: fitted coefficients [b0, b1, b2, b3] of y = b0 + b1 x1 + b2 x2 + b3 x1 x2.
x2number0interaction mode: starting value of x2 at which the conditional slope of x1 is read.
x2Valuesnumber[][0, 2, 4]interaction mode: x2 values drawn as reference lines.
x1Range[number, number][0, 10]interaction mode: x1 window.
controlsstring[]all controls shownRegistry ids of the readouts and panels this lecture shows (see `controls` in this file); unlisted ones stay hidden, e.g. the correlation and R^2 rows before those are taught, or the prediction band before regression prediction intervals. Computed by component_scopes.py fit-nodes; omit to show everything.