@@ -2,23 +2,110 @@ import type {ChannelValue, ChannelValueDenseBinSpec, ChannelValueSpec} from "../
2
2
import type { Data , MarkOptions , RenderableMark } from "../mark.js" ;
3
3
import type { BinOptions , BinReducer } from "../transforms/bin.js" ;
4
4
5
+ /** Options for the linearRegressionX and linearRegressionY marks. */
5
6
interface LinearRegressionOptions extends MarkOptions , BinOptions {
6
- x ?: ChannelValueSpec ;
7
- y ?: ChannelValueSpec ;
8
- z ?: ChannelValue ;
7
+ /** The confidence interval in (0, 1), or 0 to hide bands; defaults to 0.95. */
9
8
ci ?: number ;
9
+ /** The distance in pixels between samples of the confidence band; defaults to 4. */
10
10
precision ?: number ;
11
+
12
+ /**
13
+ * An optional ordinal channel for grouping data into (possibly stacked)
14
+ * series, producing an independent regression for each group. If not
15
+ * specified, it defaults to **fill** if a channel, or **stroke** if a
16
+ * channel.
17
+ */
18
+ z ?: ChannelValue ;
19
+
20
+ /**
21
+ * How to reduce the dependent variable (**y** for linearRegressionY, or **x**
22
+ * for linearRegressionX) when the independent variable (**x** for
23
+ * linearRegressionY, or **y** for linearRegressionX) is binned with an
24
+ * **interval**; defaults to *first*. For example, for a trend line on the
25
+ * maximum value of the Apple’s stack price by month:
26
+ *
27
+ * ```js
28
+ * Plot.linearRegressionY(aapl, {x: "Date", y: "Close", interval: "month", reduce: "max"})
29
+ * ```
30
+ *
31
+ * To default to zero instead of showing gaps in data, as when the observed
32
+ * value represents a quantity, use the *sum* reducer.
33
+ */
11
34
reduce ?: BinReducer ;
12
35
}
13
36
37
+ /** Options for the linearRegressionX mark. */
14
38
export interface LinearRegressionXOptions extends LinearRegressionOptions {
39
+ /**
40
+ * The independent variable vertical position channel, typically bound to the
41
+ * *y* scale; defaults to the zero-based index of the data [0, 1, 2, …].
42
+ *
43
+ * If an **interval** is specified, **y** values are binned accordingly,
44
+ * allowing zeroes for empty bins instead of interpolating across gaps. This
45
+ * is recommended to “regularize” sampled data; for example, if your data
46
+ * represents timestamped observations and you expect one observation per day,
47
+ * use *day* as the **interval**.
48
+ */
15
49
y ?: ChannelValueDenseBinSpec ;
50
+
51
+ /**
52
+ * The dependent variable horizontal position channel, typically bound to the
53
+ * *x* scale; defaults to identity, assuming that *data* = [*x₀*, *x₁*, *x₂*,
54
+ * …].
55
+ */
56
+ x ?: ChannelValueSpec ;
16
57
}
17
58
59
+ /** Options for the linearRegressionY mark. */
18
60
export interface LinearRegressionYOptions extends LinearRegressionOptions {
61
+ /**
62
+ * The independent variable horizontal position channel, typically bound to
63
+ * the *x* scale; defaults to the zero-based index of the data [0, 1, 2, …].
64
+ *
65
+ * If an **interval** is specified, **x** values are binned accordingly,
66
+ * allowing zeroes for empty bins instead of interpolating across gaps. This
67
+ * is recommended to “regularize” sampled data; for example, if your data
68
+ * represents timestamped observations and you expect one observation per day,
69
+ * use *day* as the **interval**.
70
+ */
19
71
x ?: ChannelValueDenseBinSpec ;
72
+
73
+ /**
74
+ * The dependent variable vertical position channel, typically bound to the
75
+ * *y* scale; defaults to identity, assuming that *data* = [*y₀*, *y₁*, *y₂*,
76
+ * …].
77
+ */
78
+ y ?: ChannelValueSpec ;
20
79
}
21
80
81
+ /**
82
+ * Like linearRegressionY, but where *x* is the dependent variable and *y* is
83
+ * the independent variable. This orientation is infrequently used, but suitable
84
+ * for example when visualizing a time-series where time goes up↑; use
85
+ * linearRegressionY instead if time goes right→.
86
+ */
22
87
export function linearRegressionX ( data ?: Data , options ?: LinearRegressionXOptions ) : RenderableMark ;
23
88
89
+ /**
90
+ * Returns a mark that draws [linear regression][1] lines with confidence bands,
91
+ * representing the estimated relation of a dependent variable (*y*) on an
92
+ * independent variable (*x*). For example to estimate the linear dependence of
93
+ * horsepower (*hp*) on weight (*wt*):
94
+ *
95
+ * ```js
96
+ * Plot.linearRegressionY(mtcars, {x: "wt", y: "hp"})
97
+ * ```
98
+ *
99
+ * The linear regression line is fit using the [least squares][2] approach. See
100
+ * Torben Jansen’s [“Linear regression with confidence bands”][3] and [this
101
+ * StatExchange question][4] for details on the confidence interval calculation.
102
+ *
103
+ * Multiple regressions can be produced by specifying a **z**, **fill**, or
104
+ * **stroke** channel.
105
+ *
106
+ * [1]: https://en.wikipedia.org/wiki/Linear_regression
107
+ * [2]: https://en.wikipedia.org/wiki/Least_squares
108
+ * [3]: https://observablehq.com/@toja/linear-regression-with-confidence-bands
109
+ * [4]: https://stats.stackexchange.com/questions/101318/understanding-shape-and-calculation-of-confidence-bands-in-linear-regression
110
+ */
24
111
export function linearRegressionY ( data ?: Data , options ?: LinearRegressionYOptions ) : RenderableMark ;
0 commit comments