Documentation

Index

@statili/compat-regression ·v0.0.1-beta.0 ·7 exports

signature
_round(number: number, precision: number): number
Rounds to precision decimal places.
Exported because regression exports it as _round, and some callers reach for it to format their own values consistently with the fit.

Parameters

NameTypeDescription
number number
precision number

Returns

number
signature
exponential(data: DataPoint[], options?: Partial<Options>): Result
Fits y = a·e^(bx). Returns coefficients as [a, b].
Uses the same y-weighted least squares as regression, which tracks the data in its original units more closely than plain regression on ln(y).

Parameters

NameTypeDescription
data DataPoint[] [x, y] pairs. Requires y > 0.
optionsoptional Partial<Options> | undefined

Returns

Result
signature
linear(data: DataPoint[], options?: Partial<Options>): Result
Fits a straight line, y = mx + c. Returns coefficients as [m, c].

Parameters

NameTypeDescription
data DataPoint[] [x, y] pairs. Rows with a null y are skipped.
optionsoptional Partial<Options> | undefined { precision }.

Returns

Result

Example

const result = linear([[0, 1], [32, 67], [12, 79]]);
result.equation;  // [gradient, intercept]
result.predict(5);
signature
logarithmic(data: DataPoint[], options?: Partial<Options>): Result
Fits y = a + b·ln(x). Returns coefficients as [a, b].

Parameters

NameTypeDescription
data DataPoint[] [x, y] pairs. Requires x > 0.
optionsoptional Partial<Options> | undefined

Returns

Result
signature
polynomial(data: DataPoint[], options?: Partial<Options>): Result
Fits a polynomial of the given order.
Returns coefficients highest power first[aₙ, …, a₁, a₀] — matching regression. Note this is the reverse of @statili/stats, which orders them ascending.

Parameters

NameTypeDescription
data DataPoint[] [x, y] pairs.
optionsoptional Partial<Options> | undefined { order, precision }.

Returns

Result
signature
power(data: DataPoint[], options?: Partial<Options>): Result
Fits y = a·x^b. Returns coefficients as [a, b].

Parameters

NameTypeDescription
data DataPoint[] [x, y] pairs. Requires x > 0 and y > 0.
optionsoptional Partial<Options> | undefined

Returns

Result
signature
regression = { linear, exponential, logarithmic, power, polynomial, _round }
The five fitting methods, matching the default export of regression.

Examples

Before
import regression from "regression";
// After — the only line that changes
import regression from "@statili/compat-regression";

const result = regression.linear([[0, 1], [32, 67], [12, 79]]);
What the shim adds: a way to know the fit failed
const result = regression.linear([[1, 1], [1, 2], [1, 3]]);
if (!result.ok) console.warn(result.error.message);
// `regression` answers this with a confident horizontal line and r2 = 0.