Documentation

Types

@statili/stats ·v0.0.2-beta.0 ·5 exports

signature
interface LoessOptions {
  /**
   * Fraction of the sample in each local neighbourhood, in `(0, 1]`.
   *
   * Smaller follows the data more closely and picks up more turning points;
   * larger produces a smoother curve. `highcharts-regression` exposes the same
   * control as `loessSmooth`, expressed as a percentage.
   *
   * @default 0.25
   */
  bandwidth: number;
  /** Decimal places for the smoothed values. @default 2 */
  precision: number;
  /**
   * Minimum change in Y for a direction reversal to count as a turning point,
   * as a fraction of the smoothed Y range. Suppresses noise-level wobble.
   *
   * @default 0.01
   */
  turningPointThreshold: number;
}
signature
interface SmoothError {
  ok: false;
  method?: "loess";
  errorType: RegressionErrorType;
  message: string;
}
signature
interface SmoothSuccess {
  ok: true;
  method: "loess";
  /** Smoothed `[x, y]` at each observed x, in ascending x order. */
  points: PredictedPoint[];
  /** Fraction of the sample used in each local neighbourhood. */
  bandwidth: number;
  /** Proportion of variance the smoothed curve accounts for. */
  r2: number;
  rmse: number;
  n: number;
  /** Direction changes in the smoothed curve, in ascending x order. */
  turningPoints: TurningPoint[];
  /** Overall direction from the first smoothed value to the last. */
  netDirection: "rising" | "falling" | "flat";
}
The result of a scatterplot smoother.

Deliberately not a RegressionSuccess. A smoother fits no global model, so there is no slope, intercept or equation to report, and no predict — a LOESS value at an unobserved x requires refitting against the whole dataset, so the curve cannot be projected beyond the data it was built from.

What it can state, and a parametric fit cannot, is shape: where the curve turns, and which way it runs overall.

signature
interface TurningPoint {
  x: number;
  y: number;
  kind: "peak" | "trough";
}
A point where the smoothed curve changes direction.
signature
type SmoothResult = SmoothSuccess | SmoothError;