fit

A least-squares fit through the observations of the scatter named in of:, and no others — a graph may carry two fits over two scatters. model chooses the family and degree the polynomial degree; giving degree a parameter rather than a number puts the degree under the reader's control through that parameter's slider. Publishes derived slope, intercept and r2.

fitNode
Only withxyplane
Seven grey readings that rise, dip and rise again, with a teal cubic fitted through them.Seven grey readings that rise, dip and rise again, with a teal cubic fitted through them.

A fit is the least-squares line or polynomial through one scatter's observations, named in of: — its head slot, so fit: #observations is the same as fit(of: #observations). Give degree a parameter instead of a number and the reader drives it — Interaction shows that pairing. residuals draws the differences from a fit to the points it was found from.

Four grey points rising from (1, 2.0) to (4, 4.8), with a teal fitted line through them.Four grey points rising from (1, 2.0) to (4, 4.8), with a teal fitted line through them.
canvas.graph#fit-graph{
    scatter#observations(x: 1, 2, 3, 4; y: 2.0, 2.4, 3.1, 4.8; colour: grey)
    fit(of: #observations; model: polynomial; degree: 1; colour: teal)
}(
    x-axis-label: x
    y-axis-label: y
)

A fit fits the mark it names and nothing else, so a graph can carry two of them: draw the sample twice, once whole and once without the point in question, and the two lines stand beside each other with their own slope and r2. To fit part of a sample, draw that part as its own scatter and fit that.

Five grey points, the last an outlier at (5, 14.0), with a grey line fitted to all five and a blue line fitted to the first four.Five grey points, the last an outlier at (5, 14.0), with a grey line fitted to all five and a blue line fitted to the first four.
canvas.graph{
    scatter#all(x: 1, 2, 3, 4, 5; y: 2.1, 3.9, 6.2, 7.8, 14.0; colour: grey)
    scatter#typical(x: 1, 2, 3, 4; y: 2.1, 3.9, 6.2, 7.8; colour: blue)

    fit#with-outlier(of: #all; degree: 1; colour: grey)
    fit#without(of: #typical; degree: 1; colour: blue)
}
A fit exposes derived slope, intercept and r2. The slope is the fitted polynomial's derivative at zero — exact, and live under whatever degree is in force — so prose citing {{#f.slope}} cannot disagree with the graph.

#Attributes

ofRequiredImplicit
The scatter to fit, by its id — fit(of: #readings). The fit is taken through that mark's observations alone, so two scatters in one graph carry two independent fits. To fit part of a sample, draw that part as its own scatter and name it here.
Also written: through, dataCan be written as shorthand: fit:value
model
Model family: polynomial or linear.
polynomiallinear
Defaultpolynomial
degreeAnimatable
Polynomial degree — a number, or an expression of parameters (degree: d) when the reader should drive it through a parameter slider.
Default1
colourAnimatable
Curve colour.
bluelight-bluedark-blueredlight-reddark-redorangelight-orangedark-orangeyellowlight-yellowdark-yellowteallight-tealdark-tealgreenlight-greendark-greenpinklight-pinkdark-pinkpurplelight-purpledark-purplegreylight-greydark-greyneutralhex(…)rgb(…)
Also written: color
Defaultteal
widthAnimatable
Stroke width.
Default2
hiddenAnimatable
Declared but not drawn. Animatable — hidden: true !cue.to{false}(at: 2) reveals it — and unlike a cue, a hidden element still frames the plot, so the axes do not jump when it appears.
Defaultfalse
lock
Keeps the reader from editing this element on an editable: graph. Nothing at all on a graph that is not editable.
Also written: locked
Defaultfalse

#Derived attributes

Computed while the element renders — read one with #id.name in attribute position or {{#id.name}} in prose, never set.

slopeDerived
The x¹ coefficient of the current fit — live under the reader-draggable degree.
interceptDerived
The constant term of the current fit.
r2Derived
The coefficient of determination of the current fit against the observations it was taken through.

#Allowed in

#Examples

Five readings with a teal polynomial fitted through them, and a fit degree menu beneath the plot set to 1.Five readings with a teal polynomial fitted through them, and a fit degree menu beneath the plot set to 1.
canvas.graph{
    scatter#readings(x: 1, 2, 3, 4, 5; y: 2.0, 2.4, 3.1, 4.8, 7.4)
    selector:d(name: fit degree; options: [1, 2, 3, 4])
    fit(of: #readings; model: polynomial; degree: d; colour: teal)
}
Five readings, the last an outlier, with a grey line fitted to all five and a blue line fitted to the first four.Five readings, the last an outlier, with a grey line fitted to all five and a blue line fitted to the first four.
canvas.graph{
    scatter#all(x: #trial.dose; y: #trial.response)
    scatter#typical(x: #clean.dose; y: #clean.response)

    fit#with-outlier(of: #all; degree: 1; colour: grey)
    fit#without(of: #typical; degree: 1; colour: blue)
}

Last updated