Documentation

Loess

@statili/stats ·v0.0.2-beta.0 ·1 export

signatures
loess(arg1: Partial<LoessOptions>): (args_0: DataPoint[]) => SmoothResult
loess(suppliedOptions: Partial<LoessOptions>, data: DataPoint[]): SmoothResult
Performs 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

NameTypeDescription
suppliedOptions Partial<LoessOptions> { bandwidth?, precision?, turningPointThreshold? }.
data DataPoint[] Array of [x, y] tuples. Requires at least 3 valid points.

Returns

SmoothResult

Discriminant union:

  • ok: true — includes points, turningPoints, netDirection, r2, rmse, n.
  • ok: false — includes errorType and message.
insight
  • ShapeturningPoints names each peak and trough with its position, supporting statements like “rises to a peak at week 12, then declines”.
  • Overall movementnetDirection compares the first smoothed value with the last, independent of what happens between them.
  • Smoothing strengthbandwidth records how much local averaging was applied, which is what turning-point counts are sensitive to.
  • Goodness of fitr2 and rmse against the observed values.

Examples

A curve that rises then falls — no single equation captures this well
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);