Index
# _round function
packages/compat-regression/src/index.ts:22_round(number: number, precision: number): numberRounds 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
| Name | Type | Description |
|---|---|---|
| number | number | — |
| precision | number | — |
Returns
number
# exponential function
packages/compat-regression/src/index.ts:122exponential(data: DataPoint[], options?: Partial<Options>): ResultFits 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
| Name | Type | Description |
|---|---|---|
| data | DataPoint[] | [x, y] pairs. Requires y > 0. |
| optionsoptional | Partial<Options> | undefined | — |
Returns
Result
# linear function
packages/compat-regression/src/index.ts:98linear(data: DataPoint[], options?: Partial<Options>): ResultFits a straight line, y = mx + c. Returns coefficients as
[m, c].Parameters
| Name | Type | Description |
|---|---|---|
| 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);# logarithmic function
packages/compat-regression/src/index.ts:137logarithmic(data: DataPoint[], options?: Partial<Options>): ResultFits y = a + b·ln(x). Returns coefficients as
[a, b].Parameters
| Name | Type | Description |
|---|---|---|
| data | DataPoint[] | [x, y] pairs. Requires x > 0. |
| optionsoptional | Partial<Options> | undefined | — |
Returns
Result
# polynomial function
packages/compat-regression/src/index.ts:172polynomial(data: DataPoint[], options?: Partial<Options>): ResultFits 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
| Name | Type | Description |
|---|---|---|
| data | DataPoint[] | [x, y] pairs. |
| optionsoptional | Partial<Options> | undefined | { order, precision }. |
Returns
Result
# power function
packages/compat-regression/src/index.ts:152power(data: DataPoint[], options?: Partial<Options>): ResultFits y = a·x^b. Returns coefficients as
[a, b].Parameters
| Name | Type | Description |
|---|---|---|
| data | DataPoint[] | [x, y] pairs. Requires x > 0 and y > 0. |
| optionsoptional | Partial<Options> | undefined | — |
Returns
Result
# default const
packages/compat-regression/src/index.ts:209regression = { linear, exponential, logarithmic, power, polynomial, _round }The five fitting methods, matching the default export of
regression.Examples
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]]);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.