@numero-uno-jpn/rstats 1.13.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.ja.md +7 -7
- package/README.md +7 -7
- package/dist/causal.js +2 -2
- package/dist/chunks/{chunk-LS55YUPZ.js → chunk-3K47QLCY.js} +2 -2
- package/dist/chunks/{chunk-XRB552MP.js → chunk-4EH3O466.js} +2 -2
- package/dist/chunks/{chunk-OWD2GUXJ.js → chunk-5BCXHJPF.js} +2 -2
- package/dist/chunks/{chunk-CKQOWTX7.js → chunk-5JKKIVYL.js} +2 -2
- package/dist/chunks/{chunk-JTARV4D3.js → chunk-5Q5YVM7S.js} +2 -2
- package/dist/chunks/{chunk-CCYGRSQW.js → chunk-6SZLUVSL.js} +2 -2
- package/dist/chunks/{chunk-C7NEFIAH.js → chunk-AGA7JCRF.js} +2 -2
- package/dist/chunks/{chunk-2SOBG73L.js → chunk-AILN2NGE.js} +2 -2
- package/dist/chunks/{chunk-CBAEKGP7.js → chunk-APOF7BLH.js} +2 -2
- package/dist/chunks/chunk-CIBAGTO2.js +4 -0
- package/dist/chunks/{chunk-UMLJFZJ5.js → chunk-FD375XBW.js} +2 -2
- package/dist/chunks/{chunk-CSZCDPF3.js → chunk-FV4ZQPRI.js} +2 -2
- package/dist/chunks/{chunk-5Y425VAJ.js → chunk-GBNGGBU3.js} +2 -2
- package/dist/chunks/{chunk-AH6IVJRF.js → chunk-HF4TT233.js} +1 -1
- package/dist/chunks/{chunk-7V6FRF32.js → chunk-HSVVCFEN.js} +2 -2
- package/dist/chunks/{chunk-RGEW3YLB.js → chunk-IMSXMJ43.js} +2 -2
- package/dist/chunks/{chunk-NRBTDLVW.js → chunk-JAXYZJV7.js} +2 -2
- package/dist/chunks/{chunk-3T4TVNXS.js → chunk-JGZSGGMD.js} +2 -2
- package/dist/chunks/{chunk-IIKFLH2E.js → chunk-KQGH6SEW.js} +2 -2
- package/dist/chunks/chunk-L7HJGM52.js +2 -0
- package/dist/chunks/{chunk-XRBNTYQW.js → chunk-M3HFLMXL.js} +2 -2
- package/dist/chunks/{chunk-DQ2MIFRQ.js → chunk-MKCPS5QQ.js} +2 -2
- package/dist/chunks/{chunk-P2UT6OCU.js → chunk-NULJXDAR.js} +2 -2
- package/dist/chunks/{chunk-2N7PIFPJ.js → chunk-OBTCIG5D.js} +2 -2
- package/dist/chunks/{chunk-CAOEREY3.js → chunk-OO22LZ7Q.js} +2 -2
- package/dist/chunks/chunk-P4TKSL4W.js +2 -0
- package/dist/chunks/{chunk-P6HSXCGZ.js → chunk-PZRABNMZ.js} +2 -2
- package/dist/chunks/{chunk-JJGRQ22M.js → chunk-R74QJUYU.js} +2 -2
- package/dist/chunks/{chunk-EWJS4VYF.js → chunk-RHNGF7R7.js} +2 -2
- package/dist/chunks/{chunk-7ZKPFHAO.js → chunk-RV4JZHTE.js} +2 -2
- package/dist/chunks/{chunk-GRXOLZ6J.js → chunk-SMA62635.js} +2 -2
- package/dist/chunks/{chunk-24UIPUSE.js → chunk-SNQABDAT.js} +1 -1
- package/dist/chunks/{chunk-OVJU5WCL.js → chunk-U5UOVXKD.js} +2 -2
- package/dist/chunks/{chunk-2BXB6DRK.js → chunk-UFJFVNC6.js} +1 -1
- package/dist/chunks/{chunk-Y4UWFH4Q.js → chunk-VHBIWPB4.js} +2 -2
- package/dist/chunks/{chunk-NWWY2WMF.js → chunk-VHMFOILD.js} +2 -2
- package/dist/chunks/{chunk-ST6I33C6.js → chunk-VO3E4Q6H.js} +1 -1
- package/dist/chunks/{chunk-KOF2HBUV.js → chunk-WL3JGBLZ.js} +2 -2
- package/dist/chunks/{chunk-TQ4P6W3P.js → chunk-XVRRYHLM.js} +2 -2
- package/dist/chunks/{chunk-CEY7FHMZ.js → chunk-YYLBBEWZ.js} +2 -2
- package/dist/chunks/{chunk-M3LK23YR.js → chunk-ZDUVFNFH.js} +1 -1
- package/dist/chunks/{chunk-TTBKW2KL.js → chunk-ZUTVNRML.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/regression.d.ts +1 -0
- package/dist/entries/regression.js +2 -0
- package/dist/entries/tests.d.ts +2 -1
- package/dist/entries/tests.js +1 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -5
- package/dist/models/fitOptions.d.ts +4 -0
- package/dist/models/fitOptions.js +14 -0
- package/dist/models/glm.d.ts +67 -5
- package/dist/models/glm.js +105 -27
- package/dist/models/glmerStructure.d.ts +5 -0
- package/dist/models/glmerStructure.js +2 -2
- package/dist/models/lm.js +3 -1
- package/dist/models/multinom.d.ts +250 -0
- package/dist/models/multinom.js +520 -0
- package/dist/models/step.d.ts +250 -0
- package/dist/models/step.js +592 -0
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +3 -3
- package/dist/rstats.js +17 -17
- package/dist/special.js +2 -2
- package/dist/tests.js +14 -14
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-DZLOU6HK.js +0 -2
- package/dist/chunks/chunk-GQ4HYF4L.js +0 -3
- package/dist/chunks/chunk-QWXSJEDI.js +0 -2
- package/dist/chunks/chunk-VO65CBCV.js +0 -2
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { type LmFormulaResult } from "./lm.js";
|
|
2
|
+
import { type GlmFormulaResult } from "./glm.js";
|
|
3
|
+
import { type DataFrame } from "./modelFrame.js";
|
|
4
|
+
/**
|
|
5
|
+
* A formula fit that the stepwise functions take: the result of {@link lm} or {@link glm} with a formula.
|
|
6
|
+
*
|
|
7
|
+
* @category Model selection
|
|
8
|
+
*/
|
|
9
|
+
export type StepModel = LmFormulaResult | GlmFormulaResult;
|
|
10
|
+
/**
|
|
11
|
+
* Options of {@link extractAIC}.
|
|
12
|
+
*
|
|
13
|
+
* @category Model selection
|
|
14
|
+
*/
|
|
15
|
+
export interface ExtractAicOptions {
|
|
16
|
+
/** The weight of the equivalent degrees of freedom: 2 (default) for AIC, `Math.log(n)` for BIC. */
|
|
17
|
+
k?: number;
|
|
18
|
+
/** lm only: a known residual variance; when positive the criterion is Mallows' Cp. Default 0. glm ignores it. */
|
|
19
|
+
scale?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Options of {@link drop1} and {@link add1}.
|
|
23
|
+
*
|
|
24
|
+
* @category Model selection
|
|
25
|
+
*/
|
|
26
|
+
export interface AddDropOptions extends ExtractAicOptions {
|
|
27
|
+
/**
|
|
28
|
+
* The terms to consider, as a one-sided formula such as `"~ x1 + x2 + f"`. drop1: default the terms that can be
|
|
29
|
+
* dropped respecting marginality; add1: required, the largest model (the terms that can be added respecting
|
|
30
|
+
* marginality are taken from it).
|
|
31
|
+
*/
|
|
32
|
+
scope?: string;
|
|
33
|
+
/**
|
|
34
|
+
* A test column: `"F"` (lm; glm with an estimated dispersion), `"Chisq"` or `"LRT"` (the likelihood-ratio test of
|
|
35
|
+
* glm; for lm the change of RSS scaled by the residual variance), or `"none"` (default).
|
|
36
|
+
*/
|
|
37
|
+
test?: "none" | "F" | "Chisq" | "LRT";
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Result of {@link drop1} and {@link add1}: R's table, one row for the current model (`"<none>"`) and one per term.
|
|
41
|
+
*
|
|
42
|
+
* @category Model selection
|
|
43
|
+
*/
|
|
44
|
+
export interface AddDropTable {
|
|
45
|
+
/** `"<none>"` and the terms. */
|
|
46
|
+
term: string[];
|
|
47
|
+
/** Degrees of freedom of each term (the change of rank; NaN for `<none>`). */
|
|
48
|
+
df: number[];
|
|
49
|
+
/** lm: the change of the residual sum of squares (R's `Sum of Sq`; NaN for `<none>`). Absent for glm. */
|
|
50
|
+
sumOfSq?: number[];
|
|
51
|
+
/** lm: the residual sum of squares of each model (R's `RSS`); glm: its deviance (R's `Deviance`). */
|
|
52
|
+
deviance: number[];
|
|
53
|
+
/** The criterion of each model (AIC, or Cp with `scale > 0`). */
|
|
54
|
+
aic: number[];
|
|
55
|
+
/** `"AIC"` or `"Cp"`: the name of the criterion's column in R. */
|
|
56
|
+
criterion: "AIC" | "Cp";
|
|
57
|
+
/** The statistic of the test (R's `F value`, `LRT` or `scaled dev.`; NaN for `<none>`), when a test was asked for. */
|
|
58
|
+
statistic?: number[];
|
|
59
|
+
/** The p-values of the test (R's `Pr(>F)` or `Pr(>Chi)`; NaN for `<none>`). */
|
|
60
|
+
pValue?: number[];
|
|
61
|
+
/** The test, when one was asked for: `"F"` or `"Chisq"`. */
|
|
62
|
+
test?: "F" | "Chisq";
|
|
63
|
+
/** R's name of the statistic's column: `"F value"`, `"LRT"` (binomial, poisson) or `"scaled dev."` (the others). */
|
|
64
|
+
statisticName?: string;
|
|
65
|
+
/** R's warnings, each once. */
|
|
66
|
+
warnings: string[];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Options of {@link step} and {@link stepAIC}.
|
|
70
|
+
*
|
|
71
|
+
* @category Model selection
|
|
72
|
+
*/
|
|
73
|
+
export interface StepOptions {
|
|
74
|
+
/**
|
|
75
|
+
* The range of models: a one-sided formula for the largest (`"~ x1 + x2 + f"`), or `{ lower, upper }` (either may be
|
|
76
|
+
* left out: lower default no terms, upper default the start model). Without a scope only terms of the start
|
|
77
|
+
* model can be dropped.
|
|
78
|
+
*/
|
|
79
|
+
scope?: string | {
|
|
80
|
+
/** The smallest model, a one-sided formula (default no terms): its terms are never dropped. */
|
|
81
|
+
lower?: string;
|
|
82
|
+
/** The largest model, a one-sided formula (default the start model): terms are added from it. */
|
|
83
|
+
upper?: string;
|
|
84
|
+
};
|
|
85
|
+
/** `"both"` (default), `"backward"` or `"forward"`. */
|
|
86
|
+
direction?: "both" | "backward" | "forward";
|
|
87
|
+
/** The weight of the equivalent degrees of freedom: 2 (default) for AIC, `Math.log(n)` for BIC. */
|
|
88
|
+
k?: number;
|
|
89
|
+
/** lm only: a known residual variance; when positive the criterion is Mallows' Cp. Default 0. */
|
|
90
|
+
scale?: number;
|
|
91
|
+
/** The largest number of moves. Default 1000. */
|
|
92
|
+
steps?: number;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* One model of the search: its formula and criterion, and the table of moves that was examined from it (sorted by the
|
|
96
|
+
* criterion, `"<none>"` the current model, `"- term"` a drop, `"+ term"` an add), as R prints them.
|
|
97
|
+
*
|
|
98
|
+
* @category Model selection
|
|
99
|
+
*/
|
|
100
|
+
export interface StepVisit {
|
|
101
|
+
/** The model's formula. */
|
|
102
|
+
formula: string;
|
|
103
|
+
/** The labels of its terms. */
|
|
104
|
+
termLabels: string[];
|
|
105
|
+
/** Its criterion (R's `extractAIC`). */
|
|
106
|
+
aic: number;
|
|
107
|
+
/** The table examined from it; absent when an aliased term was removed without a table, or the steps ran out. */
|
|
108
|
+
table?: {
|
|
109
|
+
/** `"<none>"`, `"- term"` and `"+ term"`, sorted by the criterion. */
|
|
110
|
+
move: string[];
|
|
111
|
+
/** Degrees of freedom of each move (NaN for `<none>`). */
|
|
112
|
+
df: number[];
|
|
113
|
+
/** lm: the change of the residual sum of squares (NaN for `<none>`). Absent for glm. */
|
|
114
|
+
sumOfSq?: number[];
|
|
115
|
+
/** lm: residual sum of squares; glm: deviance (of the model after the move). */
|
|
116
|
+
deviance: number[];
|
|
117
|
+
/** The criterion after the move. */
|
|
118
|
+
aic: number[];
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Result of {@link step} and {@link stepAIC}.
|
|
123
|
+
*
|
|
124
|
+
* @category Model selection
|
|
125
|
+
*/
|
|
126
|
+
export interface StepResult<F extends StepModel = StepModel> {
|
|
127
|
+
/** The selected model: the result of `lm` or `glm` with its formula. */
|
|
128
|
+
fit: F;
|
|
129
|
+
/**
|
|
130
|
+
* The path (R's `$anova`): one row for the start (step `""`, df and deviance NaN) and one per move. `df` is the change
|
|
131
|
+
* of the residual degrees of freedom (step: −1 for an added term) or its size (stepAIC: positive); `deviance` the
|
|
132
|
+
* change of the residual deviance (positive); `residDf` = nobs − edf; `aic` the criterion.
|
|
133
|
+
*/
|
|
134
|
+
anova: {
|
|
135
|
+
/** `""` for the start, `"- term"` or `"+ term"` for each move. */
|
|
136
|
+
step: string[];
|
|
137
|
+
/** The change of the residual degrees of freedom (step) or its size (stepAIC); NaN for the start. */
|
|
138
|
+
df: number[];
|
|
139
|
+
/** The change of the residual deviance (positive); NaN for the start. */
|
|
140
|
+
deviance: number[];
|
|
141
|
+
/** nobs − edf of each model. */
|
|
142
|
+
residDf: number[];
|
|
143
|
+
/** The residual deviance of each model (lm: the RSS). */
|
|
144
|
+
residDeviance: number[];
|
|
145
|
+
/** The criterion of each model (R's `AIC` or `Cp` column). */
|
|
146
|
+
aic: number[];
|
|
147
|
+
};
|
|
148
|
+
/** `"AIC"` or `"Cp"`: the name of the criterion's column in R. */
|
|
149
|
+
criterion: "AIC" | "Cp";
|
|
150
|
+
/** Each model visited, the start first, with the table examined from it (R's printed trace). */
|
|
151
|
+
visits: StepVisit[];
|
|
152
|
+
/** R's warnings, each once (those of the refits, of add1's combined fit, of a perfect fit). */
|
|
153
|
+
warnings: string[];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The criterion of a fit for model selection: `[edf, criterion]`. Equivalent to R's `extractAIC()`. lm: n log(RSS / n) + k edf, or with `scale > 0`
|
|
157
|
+
* Mallows' Cp = RSS / scale − n + k edf; glm: AIC + (k − 2) edf. n counts the rows used (weight-0 rows included) and
|
|
158
|
+
* edf = n − the residual degrees of freedom, so that with weight-0 rows edf exceeds the rank by their number, as in R.
|
|
159
|
+
*
|
|
160
|
+
* @param fit A fit of {@link lm} or {@link glm} with a formula.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* const data = { y: [1.2, 2.3, 2.9, 4.1, 5.2, 5.8], x: [1, 2, 3, 4, 5, 6] };
|
|
165
|
+
* extractAIC(lm("y ~ x", data)); // [2, -19.211]
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* @category Model selection
|
|
169
|
+
*/
|
|
170
|
+
export declare function extractAIC(fit: StepModel, options?: ExtractAicOptions): [number, number];
|
|
171
|
+
/**
|
|
172
|
+
* The models with one term dropped, for lm and glm. Equivalent to R's `drop1()`. With the change of the residual sum of squares or
|
|
173
|
+
* the deviance, the criterion of {@link extractAIC} (edf the rank) and optionally a test. As in R, each model is the
|
|
174
|
+
* fit's model matrix without the columns of the term, on the rows of `fit` (not a refit of a formula). By default
|
|
175
|
+
* only the terms not contained in another term of the model are dropped (marginality).
|
|
176
|
+
*
|
|
177
|
+
* @param fit A fit of {@link lm} or {@link glm} with a formula.
|
|
178
|
+
* @param data The data frame the fit was made from.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* const data = { y: [1.2, 2.3, 2.9, 4.1, 5.2, 5.8, 7.1, 7.9], x: [1, 2, 3, 4, 5, 6, 7, 8], z: [5, 3, 8, 1, 9, 2, 7, 4] };
|
|
183
|
+
* drop1(lm("y ~ x + z", data), data, { test: "F" }).term; // ["<none>", "x", "z"]
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* @category Model selection
|
|
187
|
+
*/
|
|
188
|
+
export declare function drop1(fit: StepModel, data: DataFrame, options?: AddDropOptions): AddDropTable;
|
|
189
|
+
/**
|
|
190
|
+
* The models with one term added, for lm and glm. Equivalent to R's `add1()`. With the change of the residual sum of squares or the
|
|
191
|
+
* deviance, the criterion of {@link extractAIC} (edf the rank) and optionally a test. The terms are those of `scope`
|
|
192
|
+
* that can be added respecting marginality. Every model is refitted on the rows complete in the variables of the
|
|
193
|
+
* model and of all the terms; when these are fewer than the fit's rows the current model is refitted on them too,
|
|
194
|
+
* with R's warning "using the m/n rows from a combined fit".
|
|
195
|
+
*
|
|
196
|
+
* @param fit A fit of {@link lm} or {@link glm} with a formula.
|
|
197
|
+
* @param data The data frame the fit was made from.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const data = { y: [1.2, 2.3, 2.9, 4.1, 5.2, 5.8, 7.1, 7.9], x: [1, 2, 3, 4, 5, 6, 7, 8], z: [5, 3, 8, 1, 9, 2, 7, 4] };
|
|
202
|
+
* add1(lm("y ~ 1", data), data, { scope: "~ x + z" }).term; // ["<none>", "x", "z"]
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @category Model selection
|
|
206
|
+
*/
|
|
207
|
+
export declare function add1(fit: StepModel, data: DataFrame, options?: AddDropOptions): AddDropTable;
|
|
208
|
+
/**
|
|
209
|
+
* Stepwise selection of the terms of a linear or generalized linear model by AIC. Equivalent to R's `step()`. From the start model,
|
|
210
|
+
* each step examines the models with one term dropped (respecting marginality, keeping the lower scope) and, with an
|
|
211
|
+
* upper scope, one term added, and moves to the one with the smallest criterion of {@link extractAIC} when it is
|
|
212
|
+
* strictly below the current model's; it stops when no move improves it, or after `steps` moves. `k = Math.log(n)`
|
|
213
|
+
* gives BIC. Terms whose columns are all aliased are removed first. The result holds the selected fit, the path
|
|
214
|
+
* (R's `$anova`) and every table examined (R's printed trace).
|
|
215
|
+
*
|
|
216
|
+
* As in R, the models are refitted on their own rows: when a move changes the number of rows used (a variable with
|
|
217
|
+
* missing values dropped or added) the search stops with an error, and the table of adds is computed on the rows
|
|
218
|
+
* complete in all the candidates' variables (with a warning when those are fewer).
|
|
219
|
+
*
|
|
220
|
+
* @param fit The start model: a fit of {@link lm} or {@link glm} with a formula.
|
|
221
|
+
* @param data The data frame the fit was made from.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* const data = { y: [1.2, 2.3, 2.9, 4.1, 5.2, 5.8, 7.1, 7.9], x: [1, 2, 3, 4, 5, 6, 7, 8], z: [5, 3, 8, 1, 9, 2, 7, 4] };
|
|
226
|
+
* const s = step(lm("y ~ x + z", data), data);
|
|
227
|
+
* s.fit.formula; // "y ~ x"
|
|
228
|
+
* s.anova.step; // ["", "- z"]
|
|
229
|
+
* ```
|
|
230
|
+
*
|
|
231
|
+
* @category Model selection
|
|
232
|
+
*/
|
|
233
|
+
export declare function step<F extends StepModel>(fit: F, data: DataFrame, options?: StepOptions): StepResult<F>;
|
|
234
|
+
/**
|
|
235
|
+
* Stepwise selection by AIC. Equivalent to `MASS::stepAIC()` in R. The same search as {@link step}; the only difference R shows is the
|
|
236
|
+
* path's `df`, which stepAIC gives as the size of the term moved (positive) where step gives the change of the
|
|
237
|
+
* residual degrees of freedom.
|
|
238
|
+
*
|
|
239
|
+
* @param fit The start model: a fit of {@link lm} or {@link glm} with a formula.
|
|
240
|
+
* @param data The data frame the fit was made from.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* const data = { y: [1.2, 2.3, 2.9, 4.1, 5.2, 5.8, 7.1, 7.9], x: [1, 2, 3, 4, 5, 6, 7, 8], z: [5, 3, 8, 1, 9, 2, 7, 4] };
|
|
245
|
+
* stepAIC(lm("y ~ 1", data), data, { scope: "~ x + z" }).anova.df; // [NaN, 1]
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @category Model selection
|
|
249
|
+
*/
|
|
250
|
+
export declare function stepAIC<F extends StepModel>(fit: F, data: DataFrame, options?: StepOptions): StepResult<F>;
|