|
| 1 | +import {create, extent, range, sum, area as shapeArea, namespaces} from "d3"; |
| 2 | +import {identity, indexOf, isNone, isNoneish, maybeZ} from "../options.js"; |
| 3 | +import {Mark} from "../plot.js"; |
| 4 | +import {qt} from "../stats.js"; |
| 5 | +import {applyDirectStyles, applyGroupedChannelStyles, applyIndirectStyles, applyTransform, groupZ, offset} from "../style.js"; |
| 6 | +import {maybeDenseIntervalX, maybeDenseIntervalY} from "../transforms/bin.js"; |
| 7 | + |
| 8 | +const defaults = { |
| 9 | + ariaLabel: "linear-regression", |
| 10 | + fill: "currentColor", |
| 11 | + fillOpacity: 0.1, |
| 12 | + stroke: "currentColor", |
| 13 | + strokeWidth: 1.5, |
| 14 | + strokeLinecap: "round", |
| 15 | + strokeLinejoin: "round", |
| 16 | + strokeMiterlimit: 1 |
| 17 | +}; |
| 18 | + |
| 19 | +class LinearRegression extends Mark { |
| 20 | + constructor(data, options = {}) { |
| 21 | + const {x, y, z, ci = 0.95, precision = 4} = options; |
| 22 | + super( |
| 23 | + data, |
| 24 | + [ |
| 25 | + {name: "x", value: x, scale: "x"}, |
| 26 | + {name: "y", value: y, scale: "y"}, |
| 27 | + {name: "z", value: maybeZ(options), optional: true} |
| 28 | + ], |
| 29 | + options, |
| 30 | + defaults |
| 31 | + ); |
| 32 | + this.z = z; |
| 33 | + this.ci = +ci; |
| 34 | + this.precision = +precision; |
| 35 | + if (!(0 <= this.ci && this.ci < 1)) throw new Error(`invalid ci; not in [0, 1): ${ci}`); |
| 36 | + if (!(this.precision > 0)) throw new Error(`invalid precision: ${precision}`); |
| 37 | + } |
| 38 | + render(I, {x, y}, channels, dimensions) { |
| 39 | + const {x: X, y: Y, z: Z} = channels; |
| 40 | + const {dx, dy, ci} = this; |
| 41 | + return create("svg:g") |
| 42 | + .call(applyIndirectStyles, this, dimensions) |
| 43 | + .call(applyTransform, x, y, offset + dx, offset + dy) |
| 44 | + .call(g => g.selectAll() |
| 45 | + .data(Z ? groupZ(I, Z, this.z) : [I]) |
| 46 | + .enter() |
| 47 | + .call(enter => enter.append("path") |
| 48 | + .attr("fill", "none") |
| 49 | + .call(applyDirectStyles, this) |
| 50 | + .call(applyGroupedChannelStyles, this, {...channels, fill: null, fillOpacity: null}) |
| 51 | + .attr("d", I => this._renderLine(I, X, Y)) |
| 52 | + .call(ci && !isNone(this.fill) ? path => path.select(pathBefore) |
| 53 | + .attr("stroke", "none") |
| 54 | + .call(applyDirectStyles, this) |
| 55 | + .call(applyGroupedChannelStyles, this, {...channels, stroke: null, strokeOpacity: null, strokeWidth: null}) |
| 56 | + .attr("d", I => this._renderBand(I, X, Y)) : () => {}))) |
| 57 | + .node(); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function pathBefore() { |
| 62 | + return this.parentNode.insertBefore(this.ownerDocument.createElementNS(namespaces.svg, "path"), this); |
| 63 | +} |
| 64 | + |
| 65 | +class LinearRegressionX extends LinearRegression { |
| 66 | + constructor(data, options) { |
| 67 | + super(data, options); |
| 68 | + } |
| 69 | + _renderBand(I, X, Y) { |
| 70 | + const {ci, precision} = this; |
| 71 | + const [y1, y2] = extent(I, i => Y[i]); |
| 72 | + const f = linearRegressionF(I, Y, X); |
| 73 | + const g = confidenceIntervalF(I, Y, X, (1 - ci) / 2, f); |
| 74 | + return shapeArea() |
| 75 | + .y(y => y) |
| 76 | + .x0(y => g(y, -1)) |
| 77 | + .x1(y => g(y, +1)) |
| 78 | + (range(y1, y2 - precision / 2, precision).concat(y2)); |
| 79 | + } |
| 80 | + _renderLine(I, X, Y) { |
| 81 | + const [y1, y2] = extent(I, i => Y[i]); |
| 82 | + const f = linearRegressionF(I, Y, X); |
| 83 | + return `M${f(y1)},${y1}L${f(y2)},${y2}`; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +class LinearRegressionY extends LinearRegression { |
| 88 | + constructor(data, options) { |
| 89 | + super(data, options); |
| 90 | + } |
| 91 | + _renderBand(I, X, Y) { |
| 92 | + const {ci, precision} = this; |
| 93 | + const [x1, x2] = extent(I, i => X[i]); |
| 94 | + const f = linearRegressionF(I, X, Y); |
| 95 | + const g = confidenceIntervalF(I, X, Y, (1 - ci) / 2, f); |
| 96 | + return shapeArea() |
| 97 | + .x(x => x) |
| 98 | + .y0(x => g(x, -1)) |
| 99 | + .y1(x => g(x, +1)) |
| 100 | + (range(x1, x2 - precision / 2, precision).concat(x2)); |
| 101 | + } |
| 102 | + _renderLine(I, X, Y) { |
| 103 | + const [x1, x2] = extent(I, i => X[i]); |
| 104 | + const f = linearRegressionF(I, X, Y); |
| 105 | + return `M${x1},${f(x1)}L${x2},${f(x2)}`; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +export function linearRegressionX(data, {y = indexOf, x = identity, stroke, fill = isNoneish(stroke) ? "currentColor" : stroke, ...options} = {}) { |
| 110 | + return new LinearRegressionX(data, maybeDenseIntervalY({...options, x, y, fill, stroke})); |
| 111 | +} |
| 112 | + |
| 113 | +export function linearRegressionY(data, {x = indexOf, y = identity, stroke, fill = isNoneish(stroke) ? "currentColor" : stroke, ...options} = {}) { |
| 114 | + return new LinearRegressionY(data, maybeDenseIntervalX({...options, x, y, fill, stroke})); |
| 115 | +} |
| 116 | + |
| 117 | +function linearRegressionF(I, X, Y) { |
| 118 | + let sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0; |
| 119 | + for (const i of I) { |
| 120 | + const xi = X[i]; |
| 121 | + const yi = Y[i]; |
| 122 | + sumX += xi; |
| 123 | + sumY += yi; |
| 124 | + sumXY += xi * yi; |
| 125 | + sumX2 += xi * xi; |
| 126 | + } |
| 127 | + const n = I.length; |
| 128 | + const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX); |
| 129 | + const intercept = (sumY - slope * sumX) / n; |
| 130 | + return x => slope * x + intercept; |
| 131 | +} |
| 132 | + |
| 133 | +function confidenceIntervalF(I, X, Y, p, f) { |
| 134 | + const mean = sum(I, i => X[i]) / I.length; |
| 135 | + let a = 0, b = 0; |
| 136 | + for (const i of I) { |
| 137 | + a += (X[i] - mean) ** 2; |
| 138 | + b += (Y[i] - f(X[i])) ** 2; |
| 139 | + } |
| 140 | + const sy = Math.sqrt(b / (I.length - 2)); |
| 141 | + const t = qt(p, I.length - 2); |
| 142 | + return (x, k) => { |
| 143 | + const Y = f(x); |
| 144 | + const se = sy * Math.sqrt(1 / I.length + (x - mean) ** 2 / a); |
| 145 | + return Y + k * t * se; |
| 146 | + }; |
| 147 | +} |
0 commit comments