Loess
# loess function
packages/stats/src/smoothing/loess.ts:135loess(arg1: Partial<LoessOptions>): (args_0: DataPoint[]) => SmoothResult
loess(suppliedOptions: Partial<LoessOptions>, data: DataPoint[]): SmoothResultPerforms LOESS (locally estimated scatterplot smoothing) with local
linear fits and tricube weights.
Unlike every method in regression/, this fits no global model. Each output
point comes from its own weighted line through nearby observations, which
lets the curve follow structure no single equation could — several peaks, a
plateau, a reversal partway through.
The trade is that there is no equation, no slope, no predict: a value at an
unobserved x would require refitting against the whole dataset, so the curve
cannot be projected past the data. What it offers instead is shape —
where the curve turns, and which way it runs overall.
Parameters
| Name | Type | Description |
|---|---|---|
| suppliedOptions | Partial<LoessOptions> | { bandwidth?, precision?, turningPointThreshold? }. |
| data | DataPoint[] | Array of [x, y] tuples. Requires at least 3 valid points. |
Returns
SmoothResultDiscriminant union:
ok: true— includespoints,turningPoints,netDirection,r2,rmse,n.ok: false— includeserrorTypeandmessage.
insight
- Shape —
turningPointsnames each peak and trough with its position, supporting statements like “rises to a peak at week 12, then declines”. - Overall movement —
netDirectioncompares the first smoothed value with the last, independent of what happens between them. - Smoothing strength —
bandwidthrecords how much local averaging was applied, which is what turning-point counts are sensitive to. - Goodness of fit —
r2andrmseagainst the observed values.
Examples
const data: DataPoint[] = [[1, 2], [2, 5], [3, 9], [4, 12], [5, 10], [6, 6], [7, 3]];
const result = loess({ bandwidth: 0.5 }, data);
if (result.ok) {
console.log(result.turningPoints); // [{ x: 4, y: ~12, kind: "peak" }]
console.log(result.netDirection); // "falling"
}// Curried / partial application (data-last)
const smooth = loess({ bandwidth: 0.4 });
const result = smooth(data);