linearConfidence
Builds the confidence band for the mean response of a simple linear regression of points: ŷ ± t·s·√(1/n + (x − x̄)²/Sxx). Returns a function mapping an x value to its [lower, upper] bounds, or null when there are fewer than three usable points or the x values do not vary.
Basic example
Section titled “Basic example”Fits a straight line to the points and returns a function giving the [lower, upper] bounds of the 95% confidence band for the line at any x. The band is narrowest at the mean x and widens toward the ends, where the fit is least certain. Plot charts draw this behind a linear trend line.
import {linearConfidence} from "@d3plus/math";
const points = [
[1, 3.8],
[2, 3.8],
[3, 8.8],
[4, 8.4],
[5, 12],
[6, 11.4],
[7, 15.4],
[8, 18.4],
[9, 18.2],
[10, 21.2],
];
const band = linearConfidence(points);
[1, 5.5, 10].map(band);
Confidence level
Section titled “Confidence level”The second argument sets the confidence level: 80% gives a tighter band than the default, 99% a wider one. The result is null when fewer than three points are usable or the x values do not vary.
import {linearConfidence} from "@d3plus/math";
const points = [
[1, 3.8],
[2, 3.8],
[3, 8.8],
[4, 8.4],
[5, 12],
[6, 11.4],
[7, 15.4],
[8, 18.4],
[9, 18.2],
[10, 21.2],
];
const band = linearConfidence(points, 0.99);
[1, 5.5, 10].map(band);
Parameters
import {linearConfidence} from "@d3plus/math";