Types
# FitError interface
packages/compat-regression/src/types.ts:15interface FitError {
/**
* Machine-readable cause.
*
* - `InsufficientData` — too few usable points for this model.
* - `DegenerateInput` — the x-values carry no variation to fit against.
* - `InvalidInput` — a non-finite value, or an out-of-domain point.
* - `MathError` — the fit produced non-finite coefficients.
* - `NumericalStability` — the normal equations were singular or ill-conditioned.
*/
type:
| "InsufficientData"
| "DegenerateInput"
| "InvalidInput"
| "MathError"
| "NumericalStability";
/** What went wrong, and what to do about it. */
message: string;
}Why a fit could not be produced.
# Options interface
packages/compat-regression/src/types.ts:5interface Options {
/** Polynomial degree. @default 2 */
order: number;
/** Decimal places for rounding. @default 2 */
precision: number;
/** Accepted and ignored, as in `regression`. */
period: number | null;
}The five configuration keys
regression accepts.# Result interface
packages/compat-regression/src/types.ts:47interface Result {
/**
* Fitted coefficients.
*
* - `linear` — `[gradient, intercept]`
* - `exponential` — `[a, b]` for y = ae^(bx)
* - `logarithmic` — `[a, b]` for y = a + b·ln(x)
* - `power` — `[a, b]` for y = ax^b
* - `polynomial` — `[aₙ, …, a₁, a₀]`, highest power first
*
* Filled with `NaN` when `ok` is `false`.
*/
equation: number[];
/** Human-readable equation. Empty string when `ok` is `false`. */
string: string;
/**
* Predicted `[x, y]` at each input x.
*
* Empty when `ok` is `false`. The original returns points full of `NaN`
* instead, which charting libraries either throw on or render as garbage.
*/
points: [number, number][];
/** Coefficient of determination. `NaN` when `ok` is `false`. */
r2: number;
/** Predicted `[x, y]` at any x. Returns `[x, NaN]` when `ok` is `false`. */
predict: (x: number) => [number, number];
/** `false` when no fit could be produced. Not present in `regression`. */
ok: boolean;
/** Why the fit failed. Absent when `ok` is `true`. Not present in `regression`. */
error?: FitError;
}The result shape returned by
regression, plus two additive fields.equation, string, points, r2 and predict are byte-for-byte what the
original returns on data it handles correctly. ok and error are new.
They have to be additive because the original API has no error channel —
there is no way to say “I could not fit this” through its shape. That is
precisely why it answers a vertical line with a confident horizontal one.
Existing code ignores unknown properties and keeps working; code that wants
to know can check ok.
# DataPoint type
packages/compat-regression/src/types.ts:2type DataPoint = [number, number | null];A
[x, y] pair. null y marks a gap and is skipped, as in regression.