regression
Fits a regression model to a set of [x, y] points. Points with non-finite values, or that fall outside a model’s domain (y ≤ 0 for exponential, x ≤ 0 for logarithmic, either for power), are ignored. Returns null when there are too few usable points or the x values do not vary.
Exponential
Section titled “Exponential”"exponential" fits y = a·e^(b·x); points with y ≤ 0 cannot be log-transformed and are left out of the fit.
import {regression} from "@d3plus/math";
const points = [
[1, 3.1],
[2, 3.044],
[3, 5.819],
[4, 6.34],
[5, 9.463],
[6, 11.299],
[7, 16.532],
[8, 22.746],
[9, 29.359],
[10, 40.271],
];
regression(points, "exponential");
Linear
Section titled “Linear”Least-squares fit of y = c0 + c1·x. The result holds the coefficients, a predict(x) function for drawing the line, the coefficient of determination r2, and the number of points n used. Plot charts expose this as a trend line.
import {regression} from "@d3plus/math";
const points = [
[1, 3.4],
[2, 4.4],
[3, 7.9],
[4, 8.7],
[5, 11.5],
[6, 12.2],
[7, 15.2],
[8, 17.7],
[9, 18.6],
[10, 21.1],
];
regression(points, "linear");
Logarithmic
Section titled “Logarithmic”"logarithmic" fits y = a + b·ln(x), so points with x ≤ 0 are ignored. A "power" type (y = a·x^b) is also available with the same rules on both axes.
import {regression} from "@d3plus/math";
const points = [
[1, 5.4],
[2, 6.479],
[3, 9.196],
[4, 8.859],
[5, 10.328],
[6, 9.575],
[7, 11.038],
[8, 11.938],
[9, 11.192],
[10, 12.008],
];
regression(points, "logarithmic");
Polynomial
Section titled “Polynomial”"polynomial" fits y = c0 + c1·x + c2·x² … up to the order option (default 2). Coefficients are reported in original units, lowest power first.
import {regression} from "@d3plus/math";
const points = [
[1, 7.9],
[2, 5.4],
[3, 6.4],
[4, 5.7],
[5, 8],
[6, 9.2],
[7, 13.7],
[8, 18.7],
[9, 23.1],
[10, 30.1],
];
regression(points, "polynomial", {order: 2});
Parameters
import {regression} from "@d3plus/math";