@numero-uno-jpn/rstats 1.2.0 → 1.3.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 +81 -0
- package/README.ja.md +27 -10
- package/README.md +26 -9
- package/dist/chunks/{chunk-ORE5ZRCY.js → chunk-2VZW4XPK.js} +2 -2
- package/dist/chunks/chunk-4OOTEXZU.js +2 -0
- package/dist/chunks/{chunk-F3EQLL4F.js → chunk-6TZVS6WC.js} +2 -2
- package/dist/chunks/{chunk-L23RSC2N.js → chunk-6X2ZMPF5.js} +2 -2
- package/dist/chunks/{chunk-IAWXF73N.js → chunk-AJZV2SVY.js} +2 -2
- package/dist/chunks/{chunk-5SGTWSCB.js → chunk-BNTBZ5W3.js} +2 -2
- package/dist/chunks/{chunk-EEU7D4RU.js → chunk-EBRTF7RM.js} +1 -1
- package/dist/chunks/{chunk-IMPRIQE5.js → chunk-FFNHG5SB.js} +1 -1
- package/dist/chunks/{chunk-73LNRWU3.js → chunk-GUM57VVM.js} +1 -1
- package/dist/chunks/{chunk-RRO4LMCB.js → chunk-HBRSUHSU.js} +2 -2
- package/dist/chunks/{chunk-VERBXW6E.js → chunk-I5NWGQOQ.js} +2 -2
- package/dist/chunks/chunk-IJV43Y7Q.js +2 -0
- package/dist/chunks/{chunk-6FZHTD5G.js → chunk-IOGQEMDE.js} +2 -2
- package/dist/chunks/{chunk-JWBSEL3V.js → chunk-JJRZ2TTF.js} +2 -2
- package/dist/chunks/{chunk-2NNBUYHJ.js → chunk-LA4H6ZSC.js} +2 -2
- package/dist/chunks/{chunk-A66M3ZEA.js → chunk-LMNU4NHZ.js} +2 -2
- package/dist/chunks/{chunk-HBHMEPGY.js → chunk-LNUNKFIH.js} +3 -3
- package/dist/chunks/chunk-MJXETGT2.js +2 -0
- package/dist/chunks/{chunk-7WG7X2UI.js → chunk-P7ZHH2SF.js} +2 -2
- package/dist/chunks/{chunk-TNETWBF5.js → chunk-PDHBRDTF.js} +2 -2
- package/dist/chunks/{chunk-4AOTAT3O.js → chunk-RCGW2DCK.js} +2 -2
- package/dist/chunks/{chunk-NSU6U3ET.js → chunk-TZTVDVP4.js} +2 -2
- package/dist/chunks/{chunk-T5DRIITM.js → chunk-UJYRU37Z.js} +2 -2
- package/dist/chunks/{chunk-BI7NYJWP.js → chunk-UMDXXT3M.js} +1 -1
- package/dist/chunks/{chunk-7INBXTVV.js → chunk-UOAFEB64.js} +2 -2
- package/dist/chunks/{chunk-BKV3FINA.js → chunk-Z74ZOTUN.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +9 -0
- package/dist/entries/tests.js +9 -0
- package/dist/htest/agreement.d.ts +2 -2
- package/dist/htest/blandAltman.d.ts +100 -0
- package/dist/htest/blandAltman.js +159 -0
- package/dist/htest/diagnosticTests.d.ts +91 -0
- package/dist/htest/diagnosticTests.js +116 -0
- package/dist/htest/iccPsych.d.ts +106 -0
- package/dist/htest/iccPsych.js +332 -0
- package/dist/htest/ksTest.d.ts +1 -1
- package/dist/htest/mcreg.d.ts +209 -0
- package/dist/htest/mcreg.js +735 -0
- package/dist/htest/roc.d.ts +287 -0
- package/dist/htest/roc.js +635 -0
- package/dist/htest/rocTest.d.ts +139 -0
- package/dist/htest/rocTest.js +444 -0
- package/dist/htest/tTOST.d.ts +202 -0
- package/dist/htest/tTOST.js +558 -0
- package/dist/htest/trendTests.d.ts +1 -1
- package/dist/htest/trendTests.js +1 -1
- package/dist/internal/summary.js +5 -0
- package/dist/linalg/qr.d.ts +3 -0
- package/dist/linalg/qr.js +3 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/coeftest.d.ts +139 -0
- package/dist/models/coeftest.js +237 -0
- package/dist/models/coxph.d.ts +60 -9
- package/dist/models/coxph.js +367 -38
- package/dist/models/glm.d.ts +11 -1
- package/dist/models/glm.js +19 -2
- package/dist/models/inputRows.d.ts +10 -0
- package/dist/models/inputRows.js +23 -0
- package/dist/models/lm.d.ts +8 -0
- package/dist/models/lm.js +14 -2
- package/dist/models/sandwich.d.ts +177 -0
- package/dist/models/sandwich.js +923 -0
- package/dist/numerics.js +2 -2
- package/dist/random/rng.d.ts +8 -1
- package/dist/random/rng.js +14 -1
- package/dist/rstats.js +16 -16
- package/dist/special.js +2 -2
- package/dist/tests.js +13 -13
- package/llms.txt +17 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-47IIPUFN.js +0 -2
- package/dist/chunks/chunk-CQB4BJBR.js +0 -2
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { type DataFrame } from "../models/modelFrame.js";
|
|
2
|
+
import type { Sample } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Direction of a ROC curve: `"<"` when the cases have the higher values (an observation is classified as a case
|
|
5
|
+
* when its value is at least the threshold), `">"` when they have the lower values.
|
|
6
|
+
*
|
|
7
|
+
* @category Hypothesis tests
|
|
8
|
+
*/
|
|
9
|
+
export type RocDirection = "<" | ">";
|
|
10
|
+
/**
|
|
11
|
+
* A value of a response of {@link roc}: a number, a string or a boolean; `null` (or `NaN`) is a missing value.
|
|
12
|
+
*
|
|
13
|
+
* @category Hypothesis tests
|
|
14
|
+
*/
|
|
15
|
+
export type RocResponseValue = number | string | boolean | null;
|
|
16
|
+
/**
|
|
17
|
+
* Options for {@link roc}.
|
|
18
|
+
*
|
|
19
|
+
* @category Hypothesis tests
|
|
20
|
+
*/
|
|
21
|
+
export interface RocOptions {
|
|
22
|
+
/**
|
|
23
|
+
* The control and the case level of the response, in this order (numbers, strings or booleans, compared as R
|
|
24
|
+
* compares them as strings). Default: the first two of the response's sorted levels (numbers in numerical order,
|
|
25
|
+
* strings by code point, `false` before `true`). Not with the `{ controls, cases }` form.
|
|
26
|
+
*/
|
|
27
|
+
levels?: readonly RocResponseValue[];
|
|
28
|
+
/**
|
|
29
|
+
* `"auto"` (default): `"<"` when the median of the controls is at most the median of the cases, else `">"`
|
|
30
|
+
* (pROC's rule: the AUC can then be below 0.5). `"<"`: the cases have the higher values; `">"`: the lower values.
|
|
31
|
+
*/
|
|
32
|
+
direction?: "auto" | RocDirection;
|
|
33
|
+
/** Drop the observations with a missing response or predictor (default `true`); `false` makes them an error. */
|
|
34
|
+
naRm?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The two samples of {@link roc}'s `{ controls, cases }` form.
|
|
38
|
+
*
|
|
39
|
+
* @category Hypothesis tests
|
|
40
|
+
*/
|
|
41
|
+
export interface RocSamples {
|
|
42
|
+
/** The predictor values of the controls (`NaN` for a missing value). */
|
|
43
|
+
controls: Sample;
|
|
44
|
+
/** The predictor values of the cases (`NaN` for a missing value). */
|
|
45
|
+
cases: Sample;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A ROC curve: the result of {@link roc}, as pROC's `roc` object.
|
|
49
|
+
*
|
|
50
|
+
* @category Hypothesis tests
|
|
51
|
+
*/
|
|
52
|
+
export interface RocResult {
|
|
53
|
+
/**
|
|
54
|
+
* The thresholds, from `-Infinity` up to `Infinity` for direction `"<"` and down from `Infinity` for `">"`: the
|
|
55
|
+
* midpoints between consecutive distinct values of the predictor.
|
|
56
|
+
*/
|
|
57
|
+
thresholds: number[];
|
|
58
|
+
/** The sensitivity at each threshold (from 1 down to 0). */
|
|
59
|
+
sensitivities: number[];
|
|
60
|
+
/** The specificity at each threshold (from 0 up to 1). */
|
|
61
|
+
specificities: number[];
|
|
62
|
+
/** The area under the curve. */
|
|
63
|
+
auc: number;
|
|
64
|
+
/** The direction used (the one chosen by `"auto"`, or the one given). */
|
|
65
|
+
direction: RocDirection;
|
|
66
|
+
/** The control and the case level, as strings (`["0", "1"]` for the `{ controls, cases }` form). */
|
|
67
|
+
levels: [string, string];
|
|
68
|
+
/** The predictor values of the controls, in input order (missing values removed). */
|
|
69
|
+
controls: number[];
|
|
70
|
+
/** The predictor values of the cases, in input order (missing values removed). */
|
|
71
|
+
cases: number[];
|
|
72
|
+
/** R's warnings, each once, in R's order (an empty array when there are none). */
|
|
73
|
+
warnings: string[];
|
|
74
|
+
}
|
|
75
|
+
/** What a curve keeps of its data besides its fields: the observations, for pairing and the bootstrap */
|
|
76
|
+
export interface RocData {
|
|
77
|
+
/** The response as labels (null for a missing one), before any removal; null for the controls/cases form */
|
|
78
|
+
labels: (string | null)[] | null;
|
|
79
|
+
/** The predictor before any removal */
|
|
80
|
+
predictor: number[];
|
|
81
|
+
/** The values of the observations used, in input order (c(controls, cases) for the controls/cases form) */
|
|
82
|
+
values: number[];
|
|
83
|
+
/** Whether each of `values` is a case */
|
|
84
|
+
isCase: boolean[];
|
|
85
|
+
/** The indices of `values` in the original data (response form) */
|
|
86
|
+
kept: number[];
|
|
87
|
+
}
|
|
88
|
+
/** The data a curve was built from (undefined for an object not made by roc, such as a copy) */
|
|
89
|
+
export declare function rocData(r: RocResult): RocData | undefined;
|
|
90
|
+
/** The AUC of two samples in a direction: U/(mn) from integer counts, rounded once */
|
|
91
|
+
export declare function aucOf(controls: readonly number[], cases: readonly number[], direction: RocDirection): number;
|
|
92
|
+
/** Twice the Mann-Whitney U of two samples in a direction (an integer; the AUC is this over 2mn) */
|
|
93
|
+
export declare function twiceUOf(controls: readonly number[], cases: readonly number[], direction: RocDirection): number;
|
|
94
|
+
/**
|
|
95
|
+
* The rational num/den rounded once to the nearest double (den nonzero): the quotient to at least 55 bits, with a
|
|
96
|
+
* sticky bit for a remainder, converted by Number (which rounds to nearest, ties to even). A quotient in the
|
|
97
|
+
* subnormal range is rounded twice
|
|
98
|
+
*/
|
|
99
|
+
export declare function ratioToDouble(num: bigint, den: bigint): number;
|
|
100
|
+
/** Builds the curve from the classes (checked, without missing values), and records its data */
|
|
101
|
+
export declare function buildRoc(controls: number[], cases: number[], direction: "auto" | RocDirection, levels: [string, string], data: RocData, warnings: string[]): RocResult;
|
|
102
|
+
/**
|
|
103
|
+
* The curve of a response and a predictor: observations with a missing response or predictor dropped (or an
|
|
104
|
+
* error), the levels, and the classes
|
|
105
|
+
*/
|
|
106
|
+
export declare function rocFromResponse(response: readonly RocResponseValue[], predictor: readonly number[], options: {
|
|
107
|
+
levels?: readonly RocResponseValue[] | undefined;
|
|
108
|
+
direction?: string | undefined;
|
|
109
|
+
naRm?: boolean | undefined;
|
|
110
|
+
}): RocResult;
|
|
111
|
+
/**
|
|
112
|
+
* The ROC curve of a numeric predictor of a binary response. Equivalent to `pROC::roc()` in R.
|
|
113
|
+
*
|
|
114
|
+
* The first level of the response is the control, the second the case (with more than two levels the first two are
|
|
115
|
+
* used, with pROC's warning). The thresholds are the midpoints between consecutive distinct values, with `-Infinity`
|
|
116
|
+
* and `Infinity` at the ends, and the points are stored as pROC stores them, by increasing specificity. The AUC is
|
|
117
|
+
* computed from the counts as U/(mn) and rounded once. Infinite predictor values are an error (pROC returns NaN with
|
|
118
|
+
* a warning), as are an empty class and, with `naRm: false`, missing values (pROC returns NA).
|
|
119
|
+
*
|
|
120
|
+
* @param response The outcome: numbers, strings or booleans (`null` or `NaN` for a missing value).
|
|
121
|
+
* @param predictor The predictor, numeric, of the same length (`NaN` for a missing value).
|
|
122
|
+
* @param options See {@link RocOptions}.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const r = roc([0, 0, 0, 1, 1, 1], [1, 2, 4, 3, 5, 6]);
|
|
127
|
+
* r.auc; // 0.8888888888888888
|
|
128
|
+
* r.thresholds; // [-Infinity, 1.5, 2.5, 3.5, 4.5, 5.5, Infinity]
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* @category Hypothesis tests
|
|
132
|
+
*/
|
|
133
|
+
export declare function roc(response: readonly RocResponseValue[] | Float64Array, predictor: Sample, options?: RocOptions): RocResult;
|
|
134
|
+
/**
|
|
135
|
+
* The ROC curve from a formula `response ~ predictor` and a data frame; the rows where either is missing are dropped.
|
|
136
|
+
*
|
|
137
|
+
* @param formula `"response ~ predictor"`.
|
|
138
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
139
|
+
* @param options See {@link RocOptions}.
|
|
140
|
+
*/
|
|
141
|
+
export declare function roc(formula: string, data: DataFrame, options?: RocOptions): RocResult;
|
|
142
|
+
/**
|
|
143
|
+
* The ROC curve from the predictor values of the controls and of the cases (pROC's `roc(controls =, cases =)`).
|
|
144
|
+
*
|
|
145
|
+
* @param samples The two samples.
|
|
146
|
+
* @param options See {@link RocOptions} (without `levels`).
|
|
147
|
+
*/
|
|
148
|
+
export declare function roc(samples: RocSamples, options?: RocOptions): RocResult;
|
|
149
|
+
/**
|
|
150
|
+
* Options for {@link auc}.
|
|
151
|
+
*
|
|
152
|
+
* @category Hypothesis tests
|
|
153
|
+
*/
|
|
154
|
+
export interface AucOptions {
|
|
155
|
+
/** The limits `[a, b]` (in either order, within [0, 1]) of the specificity or sensitivity: the partial AUC. */
|
|
156
|
+
partialAuc?: readonly number[];
|
|
157
|
+
/** Which axis the limits are on: `"specificity"` (default) or `"sensitivity"`; abbreviations are accepted. */
|
|
158
|
+
focus?: "specificity" | "sensitivity" | (string & {});
|
|
159
|
+
/**
|
|
160
|
+
* Standardise the partial AUC as McClish (1989), so that 0.5 is the diagonal and 1 the perfect curve. Default
|
|
161
|
+
* `false`.
|
|
162
|
+
*/
|
|
163
|
+
correct?: boolean;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Result of {@link auc}.
|
|
167
|
+
*
|
|
168
|
+
* @category Hypothesis tests
|
|
169
|
+
*/
|
|
170
|
+
export interface AucResult {
|
|
171
|
+
/** The area (the full AUC, or the partial one, standardised with `correct`). */
|
|
172
|
+
auc: number;
|
|
173
|
+
/** The limits `[high, low]` of the partial area (pROC's order), or `null` for the full AUC. */
|
|
174
|
+
partialAuc: [number, number] | null;
|
|
175
|
+
/** The axis of the limits. */
|
|
176
|
+
focus: "specificity" | "sensitivity";
|
|
177
|
+
/** Whether the partial AUC is standardised. */
|
|
178
|
+
correct: boolean;
|
|
179
|
+
/** R's warnings, each once, in R's order (an empty array when there are none). */
|
|
180
|
+
warnings: string[];
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The area under a ROC curve, or the partial area between two specificities or sensitivities. Equivalent to
|
|
184
|
+
* `pROC::auc()` in R.
|
|
185
|
+
*
|
|
186
|
+
* The partial area sums the trapezoids of the points within the limits and interpolates the curve linearly at the
|
|
187
|
+
* limits. With `correct`, it is standardised as McClish (1989): (1 + (A − min)/(max − min))/2, with min the area under
|
|
188
|
+
* the diagonal and max that of the perfect curve between the limits, computed exactly from the counts of the points and
|
|
189
|
+
* rounded once (a curve on the diagonal gives 0.5); below the diagonal that is undefined, an error (pROC: NA with a
|
|
190
|
+
* warning). Limits outside [0, 1] are an error (pROC: NA).
|
|
191
|
+
*
|
|
192
|
+
* @param curve A curve from {@link roc}.
|
|
193
|
+
* @param options See {@link AucOptions}.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```ts
|
|
197
|
+
* const r = roc({ controls: [1, 2, 2, 3, 5, 7], cases: [2, 4, 5, 5, 8, 9, 10] });
|
|
198
|
+
* auc(r).auc; // 0.7857142857142857
|
|
199
|
+
* auc(r, { partialAuc: [1, 0.8] }).auc; // 0.08666666666666666
|
|
200
|
+
* auc(r, { partialAuc: [1, 0.8], correct: true }).auc; // 0.6851851851851852
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @category Hypothesis tests
|
|
204
|
+
*/
|
|
205
|
+
export declare function auc(curve: RocResult, options?: AucOptions): AucResult;
|
|
206
|
+
/**
|
|
207
|
+
* Options for {@link coords}.
|
|
208
|
+
*
|
|
209
|
+
* @category Hypothesis tests
|
|
210
|
+
*/
|
|
211
|
+
export interface CoordsOptions {
|
|
212
|
+
/**
|
|
213
|
+
* The criterion of `"best"`: `"youden"` (default; the maximum of se + r·sp) or `"closest.topleft"` (the minimum of
|
|
214
|
+
* (1 − se)² + r(1 − sp)²; `"topleft"` is the same); abbreviations are accepted.
|
|
215
|
+
*/
|
|
216
|
+
bestMethod?: "youden" | "closest.topleft" | "topleft" | (string & {});
|
|
217
|
+
/**
|
|
218
|
+
* `[cost, prevalence]`: the weight r = (1 − prevalence)/(cost · prevalence) of the specificity in the criteria.
|
|
219
|
+
* The cost must be positive and the prevalence in (0, 1). Default `[1, 0.5]` (r = 1).
|
|
220
|
+
*/
|
|
221
|
+
bestWeights?: readonly number[];
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Result of {@link coords}: one array per column, one value per point (pROC's data frame with `ret = "all"`).
|
|
225
|
+
*
|
|
226
|
+
* @category Hypothesis tests
|
|
227
|
+
*/
|
|
228
|
+
export interface CoordsResult {
|
|
229
|
+
/** The threshold of each point. */
|
|
230
|
+
threshold: number[];
|
|
231
|
+
/** Sensitivity tp/(tp + fn). */
|
|
232
|
+
sensitivity: number[];
|
|
233
|
+
/** Specificity tn/(tn + fp). */
|
|
234
|
+
specificity: number[];
|
|
235
|
+
/** Accuracy (tp + tn)/(number of observations). */
|
|
236
|
+
accuracy: number[];
|
|
237
|
+
/** True negatives. */
|
|
238
|
+
tn: number[];
|
|
239
|
+
/** True positives. */
|
|
240
|
+
tp: number[];
|
|
241
|
+
/** False negatives. */
|
|
242
|
+
fn: number[];
|
|
243
|
+
/** False positives. */
|
|
244
|
+
fp: number[];
|
|
245
|
+
/** Negative predictive value tn/(tn + fn) (`NaN` when no observation is negative). */
|
|
246
|
+
npv: number[];
|
|
247
|
+
/** Positive predictive value tp/(tp + fp) (`NaN` when no observation is positive). */
|
|
248
|
+
ppv: number[];
|
|
249
|
+
/** False positive rate fp/(fp + tn). */
|
|
250
|
+
fpr: number[];
|
|
251
|
+
/** Positive likelihood ratio se/(1 − sp). */
|
|
252
|
+
lrPos: number[];
|
|
253
|
+
/** Negative likelihood ratio (1 − se)/sp. */
|
|
254
|
+
lrNeg: number[];
|
|
255
|
+
/** pROC's Youden criterion se + r·sp (not se + sp − 1). */
|
|
256
|
+
youden: number[];
|
|
257
|
+
/** The squared distance to the top left corner (1 − se)² + r(1 − sp)². */
|
|
258
|
+
closestTopleft: number[];
|
|
259
|
+
/** R's warnings, each once, in R's order (an empty array when there are none). */
|
|
260
|
+
warnings: string[];
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Coordinates of a ROC curve: sensitivity, specificity, the counts, predictive values, likelihood ratios and the
|
|
264
|
+
* criteria of the best threshold, at every point of the curve, at the best threshold(s) or at given thresholds.
|
|
265
|
+
* Equivalent to `pROC::coords(roc, x, ret = "all")` in R.
|
|
266
|
+
*
|
|
267
|
+
* Every column is computed from the integer counts of each threshold and rounded once (pROC rebuilds the counts from
|
|
268
|
+
* the proportions, so that they are not always integers, and loses digits in 1 − specificity). The rows of `"best"`
|
|
269
|
+
* are all the points whose criterion equals the best one exactly (pROC compares rounded sums and can lose ties). At a
|
|
270
|
+
* given threshold, an observation equal to it counts as positive.
|
|
271
|
+
*
|
|
272
|
+
* @param curve A curve from {@link roc}.
|
|
273
|
+
* @param x `"all"` (default: every point of the curve), `"best"`, or thresholds (a number or an array).
|
|
274
|
+
* @param options See {@link CoordsOptions}.
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```ts
|
|
278
|
+
* const r = roc({ controls: [1, 2, 2, 3, 5, 7], cases: [2, 4, 5, 5, 8, 9, 10] });
|
|
279
|
+
* const best = coords(r, "best");
|
|
280
|
+
* best.threshold; // [3.5]
|
|
281
|
+
* best.sensitivity; // [0.8571428571428571]
|
|
282
|
+
* coords(r, [4, 6]).tp; // [6, 3]
|
|
283
|
+
* ```
|
|
284
|
+
*
|
|
285
|
+
* @category Hypothesis tests
|
|
286
|
+
*/
|
|
287
|
+
export declare function coords(curve: RocResult, x?: "all" | "best" | (string & {}) | number | readonly number[], options?: CoordsOptions): CoordsResult;
|