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