@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,237 @@
|
|
|
1
|
+
// Tests and confidence intervals of the coefficients of lm and glm fits with a given covariance matrix (such as one of
|
|
2
|
+
// vcovHC or vcovCL): the lmtest package's coeftest and coefci.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js (no ported code) from oracle/robust-behaviour.md (a description of the behaviour of lmtest
|
|
5
|
+
// 0.9-40): Wald statistics estimate / standard error with the standard errors from the diagonal of the covariance, t
|
|
6
|
+
// tests on the residual degrees of freedom of an lm and z tests for a glm (Zeileis and Hothorn 2002, "Diagnostic
|
|
7
|
+
// checking in regression relationships", R News 2(3), 7-10).
|
|
8
|
+
import { pt1, qt1 } from "../distributions/t.js";
|
|
9
|
+
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
10
|
+
import { reported, unreported } from "../internal/warnings.js";
|
|
11
|
+
import { checkFit, coefficientNames, isGlm, modelCovariance, scaledStandardError } from "./sandwich.js";
|
|
12
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
13
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
14
|
+
const COEFTEST_KEYS = ["vcov", "df"];
|
|
15
|
+
const COEFCI_KEYS = ["parm", "level", "vcov", "df"];
|
|
16
|
+
function isVcovResult(v) {
|
|
17
|
+
return typeof v === "object" && v !== null && !Array.isArray(v) && Array.isArray(v.vcov) && Array.isArray(v.names);
|
|
18
|
+
}
|
|
19
|
+
function checkMatrix(m) {
|
|
20
|
+
if (!Array.isArray(m) || m.some((r) => !Array.isArray(r) && !ArrayBuffer.isView(r)))
|
|
21
|
+
throw new TypeError("'vcov' must be a matrix (an array of rows)");
|
|
22
|
+
const rows = m.map((r) => Array.from(r, Number));
|
|
23
|
+
if (rows.some((r) => r.length !== rows.length))
|
|
24
|
+
throw new RangeError("'vcov' must be a square matrix");
|
|
25
|
+
return rows;
|
|
26
|
+
}
|
|
27
|
+
const unique = (v) => new Set(v).size === v.length;
|
|
28
|
+
/** The estimates and standard errors of the coefficients kept, with their names and the warnings of the covariance */
|
|
29
|
+
function coefficientTable(fit, vcov) {
|
|
30
|
+
checkFit(fit);
|
|
31
|
+
const estAll = fit.coefficients;
|
|
32
|
+
const namesAll = coefficientNames(fit);
|
|
33
|
+
const p = estAll.length;
|
|
34
|
+
const aliased = estAll.map((b) => Number.isNaN(b));
|
|
35
|
+
let V;
|
|
36
|
+
let vNames;
|
|
37
|
+
let warnings = [];
|
|
38
|
+
let se;
|
|
39
|
+
if (vcov === undefined) {
|
|
40
|
+
// the fit's own covariance: the aliased coefficients stay, with NaN (R's vcov has NA rows for them); the standard
|
|
41
|
+
// errors from its scaled form (representable where the covariance is not, responses near 1e±300)
|
|
42
|
+
const m = modelCovariance(fit);
|
|
43
|
+
const pos = new Map(m.names.map((nm, i) => [nm, i]));
|
|
44
|
+
const kept = namesAll.map((nm, j) => (aliased[j] ? undefined : pos.get(nm)));
|
|
45
|
+
V = kept.map((a) => kept.map((b) => (a === undefined || b === undefined ? NaN : m.vcov[a][b])));
|
|
46
|
+
vNames = namesAll;
|
|
47
|
+
se = kept.map((a) => (a === undefined ? NaN : (scaledStandardError(m.vcov, a, m.vcov[a][a]) ?? Math.sqrt(m.vcov[a][a]))));
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const v = typeof vcov === "function" ? unreported(() => vcov(fit)) : vcov;
|
|
51
|
+
const given = isVcovResult(v) ? v.vcov : v;
|
|
52
|
+
if (isVcovResult(v)) {
|
|
53
|
+
V = checkMatrix(v.vcov);
|
|
54
|
+
vNames = v.names.slice();
|
|
55
|
+
if (vNames.length !== V.length)
|
|
56
|
+
throw new RangeError("'vcov' must have one name per row");
|
|
57
|
+
warnings = [...(v.warnings ?? [])];
|
|
58
|
+
}
|
|
59
|
+
else
|
|
60
|
+
V = checkMatrix(v);
|
|
61
|
+
// the standard errors: of a result of vcovHC or vcovCL from its scaled form (representable where the variance is
|
|
62
|
+
// Infinity or 0), else the square root of the diagonal, which must be a finite non-negative number (R gives NaN,
|
|
63
|
+
// with "NaNs produced" for a negative variance; errors here). Only the diagonal is used: the matrix is not checked
|
|
64
|
+
// for symmetry
|
|
65
|
+
se = V.map((r, j) => {
|
|
66
|
+
const d = r[j];
|
|
67
|
+
const s = typeof given === "object" && given !== null ? scaledStandardError(given, j, d) : undefined;
|
|
68
|
+
if (s !== undefined && !Number.isNaN(s))
|
|
69
|
+
return s;
|
|
70
|
+
if (!Number.isFinite(d))
|
|
71
|
+
throw new RangeError(`the variance of coefficient ${j + 1} in 'vcov' is not a finite number`);
|
|
72
|
+
if (d < 0)
|
|
73
|
+
throw new RangeError(`the variance of coefficient ${j + 1} in 'vcov' is negative`);
|
|
74
|
+
return Math.sqrt(d);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
if (vNames !== undefined && unique(vNames) && unique(namesAll)) {
|
|
78
|
+
// by name: the coefficients whose names the covariance has, in the fit's order
|
|
79
|
+
const pos = new Map(vNames.map((nm, i) => [nm, i]));
|
|
80
|
+
const js = namesAll.flatMap((nm, j) => (pos.has(nm) ? [j] : []));
|
|
81
|
+
return { est: js.map((j) => estAll[j]), se: js.map((j) => se[pos.get(namesAll[j])]), names: js.map((j) => namesAll[j]), warnings };
|
|
82
|
+
}
|
|
83
|
+
// by position: one row per coefficient, or per coefficient that is not aliased
|
|
84
|
+
let js;
|
|
85
|
+
if (V.length === p)
|
|
86
|
+
js = estAll.map((_, j) => j);
|
|
87
|
+
else if (V.length === fit.rank)
|
|
88
|
+
js = estAll.flatMap((_, j) => (aliased[j] ? [] : [j]));
|
|
89
|
+
else {
|
|
90
|
+
const sizes = fit.rank === p ? `${p} x ${p}` : `${p} x ${p} or ${fit.rank} x ${fit.rank}`;
|
|
91
|
+
throw new RangeError(`'vcov' must be ${sizes}, one row per coefficient`);
|
|
92
|
+
}
|
|
93
|
+
return { est: js.map((j) => estAll[j]), se, names: js.map((j) => namesAll[j]), warnings };
|
|
94
|
+
}
|
|
95
|
+
/** The degrees of freedom (lmtest's default: the residual degrees of freedom for an lm, Infinity for a glm) */
|
|
96
|
+
function dfOf(fit, df) {
|
|
97
|
+
if (df === undefined)
|
|
98
|
+
return isGlm(fit) ? Infinity : fit.dfResidual;
|
|
99
|
+
if (typeof df !== "number")
|
|
100
|
+
throw new TypeError("'df' must be a number");
|
|
101
|
+
if (Number.isNaN(df))
|
|
102
|
+
throw new RangeError("'df' must not be NaN");
|
|
103
|
+
return df;
|
|
104
|
+
}
|
|
105
|
+
/** Whether t tests are used: a positive finite df (else z, as lmtest) */
|
|
106
|
+
const useT = (df) => Number.isFinite(df) && df > 0;
|
|
107
|
+
function nobsOf(fit) {
|
|
108
|
+
return isGlm(fit) ? fit.priorWeights.filter((w) => w !== 0).length : fit.rank + fit.dfResidual;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Tests of the coefficients of an `lm` or `glm` fit with a given covariance matrix. Equivalent to R's
|
|
112
|
+
* `lmtest::coeftest()`.
|
|
113
|
+
*
|
|
114
|
+
* Each coefficient's estimate divided by its standard error (the square root of the covariance's diagonal), with a t
|
|
115
|
+
* test on `df` degrees of freedom or a z test. The default `df` is the residual degrees of freedom of an lm and
|
|
116
|
+
* `Infinity` (z tests) for a glm of every family, as lmtest (unlike `glmSummary`, which uses t tests where the
|
|
117
|
+
* dispersion is estimated). A covariance whose diagonal has a negative, NaN or infinite variance is an error (R gives
|
|
118
|
+
* NaN); only the diagonal is used (the matrix is not checked for symmetry, as R's). For a result of {@link vcovHC} or
|
|
119
|
+
* {@link vcovCL} (or the fit's own covariance) the standard errors come from the covariance in units of powers of 2,
|
|
120
|
+
* so they are right where the matrix holds Infinity or 0 (a response around 1e±300). That record belongs to the matrix
|
|
121
|
+
* object returned: a copy of it (`vcov.map((r) => r.slice())`, `structuredClone`, JSON) loses it and gives the values
|
|
122
|
+
* the copy holds (se 0, z Infinity and p 0 where it holds 0); pass the result or its original matrix instead.
|
|
123
|
+
*
|
|
124
|
+
* @param fit The result of {@link lm} or {@link glm}.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const fit = lm([1, 2, 3, 4, 5, 6], [1.1, 2.3, 2.8, 4.5, 4.9, 6.4]);
|
|
129
|
+
* const t = coeftest(fit, { vcov: (f) => vcovHC(f, { type: "HC1" }) });
|
|
130
|
+
* t.stdError; // [0.17608, 0.049777]
|
|
131
|
+
* t.pValue; // [0.72421, 0.0000324]
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @category Linear models and ANOVA
|
|
135
|
+
*/
|
|
136
|
+
export function coeftest(fit, options = {}) {
|
|
137
|
+
checkOptions(options, COEFTEST_KEYS, "coeftest"); // generated: option-keys.mjs
|
|
138
|
+
const { est, se, names, warnings } = coefficientTable(fit, options.vcov);
|
|
139
|
+
const df = dfOf(fit, options.df);
|
|
140
|
+
const t = useT(df);
|
|
141
|
+
const statistic = est.map((b, j) => b / se[j]);
|
|
142
|
+
const pValue = statistic.map((s) => (Number.isNaN(s) ? NaN : t ? 2 * pt1(-Math.abs(s), df, true, false) : 2 * pnorm(-Math.abs(s))));
|
|
143
|
+
return reported("coeftest", {
|
|
144
|
+
estimate: est,
|
|
145
|
+
stdError: se,
|
|
146
|
+
statistic,
|
|
147
|
+
pValue,
|
|
148
|
+
names,
|
|
149
|
+
statisticName: t ? "t value" : "z value",
|
|
150
|
+
df,
|
|
151
|
+
method: t ? "t test of coefficients" : "z test of coefficients",
|
|
152
|
+
nobs: nobsOf(fit),
|
|
153
|
+
warnings,
|
|
154
|
+
}, fit, typeof options.vcov === "function" ? undefined : options.vcov);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* R's column names of an interval: format(100 * c(a, 1 - a), digits = 3) followed by " %", both numbers with the
|
|
158
|
+
* decimals the one that needs more of them takes to show 3 significant digits (trailing zeros dropped)
|
|
159
|
+
*/
|
|
160
|
+
function percentNames(a) {
|
|
161
|
+
const v = [100 * a, 100 * (1 - a)];
|
|
162
|
+
const decimals = v.map((x) => {
|
|
163
|
+
if (x === 0 || !Number.isFinite(x))
|
|
164
|
+
return 0;
|
|
165
|
+
const e = Math.floor(Math.log10(Math.abs(x)));
|
|
166
|
+
const s = x.toFixed(Math.max(0, 2 - e));
|
|
167
|
+
const frac = s.includes(".") ? s.replace(/0+$/, "").split(".")[1] : "";
|
|
168
|
+
return frac.length;
|
|
169
|
+
});
|
|
170
|
+
const d = Math.max(...decimals);
|
|
171
|
+
return [`${v[0].toFixed(d)} %`, `${v[1].toFixed(d)} %`];
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Confidence intervals of the coefficients of an `lm` or `glm` fit with a given covariance matrix. Equivalent to R's
|
|
175
|
+
* `lmtest::coefci()`.
|
|
176
|
+
*
|
|
177
|
+
* The limits estimate ± q se with the t quantile on `df` degrees of freedom, or the normal quantile (by default for a
|
|
178
|
+
* glm of every family, as lmtest). `parm` takes 0-based indices (R: 1-based) or names. The standard errors as in
|
|
179
|
+
* {@link coeftest}: from the covariance in units of powers of 2 for a result of {@link vcovHC} or {@link vcovCL} or
|
|
180
|
+
* its original matrix, not for a copy of the matrix (which gives intervals of width 0 where it holds 0).
|
|
181
|
+
*
|
|
182
|
+
* @param fit The result of {@link lm} or {@link glm}.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* const fit = lm([1, 2, 3, 4, 5, 6], [1.1, 2.3, 2.8, 4.5, 4.9, 6.4]);
|
|
187
|
+
* const ci = coefci(fit, { vcov: (f) => vcovHC(f) });
|
|
188
|
+
* ci.colNames; // ["2.5 %", "97.5 %"]
|
|
189
|
+
* ci.lower; // [-0.51418, 0.84206]
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @category Linear models and ANOVA
|
|
193
|
+
*/
|
|
194
|
+
export function coefci(fit, options = {}) {
|
|
195
|
+
checkOptions(options, COEFCI_KEYS, "coefci"); // generated: option-keys.mjs
|
|
196
|
+
const level = options.level ?? 0.95;
|
|
197
|
+
if (typeof level !== "number")
|
|
198
|
+
throw new TypeError("'level' must be a number");
|
|
199
|
+
// R computes NaN limits outside [0, 1] (with "NaNs produced"): an error
|
|
200
|
+
if (!(level >= 0 && level <= 1))
|
|
201
|
+
throw new RangeError("'level' must be between 0 and 1");
|
|
202
|
+
const tab = coefficientTable(fit, options.vcov);
|
|
203
|
+
const df = dfOf(fit, options.df);
|
|
204
|
+
let rows = tab.est.map((_, j) => j);
|
|
205
|
+
const parm = options.parm;
|
|
206
|
+
if (parm !== undefined) {
|
|
207
|
+
const list = (Array.isArray(parm) ? parm : [parm]);
|
|
208
|
+
if (list.every((v) => typeof v === "string")) {
|
|
209
|
+
const wanted = new Set(list);
|
|
210
|
+
rows = rows.filter((j) => wanted.has(tab.names[j]));
|
|
211
|
+
}
|
|
212
|
+
else if (list.every((v) => typeof v === "number")) {
|
|
213
|
+
rows = list.map((j) => {
|
|
214
|
+
if (!Number.isInteger(j) || j < 0)
|
|
215
|
+
throw new RangeError("'parm' must hold 0-based indices or names");
|
|
216
|
+
if (j >= tab.est.length)
|
|
217
|
+
throw new RangeError("subscript out of bounds");
|
|
218
|
+
return j;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
else
|
|
222
|
+
throw new TypeError("'parm' must hold 0-based indices or names");
|
|
223
|
+
}
|
|
224
|
+
const a = (1 - level) / 2;
|
|
225
|
+
// the lower quantile, and the upper from the upper tail where 1 - a has lost its digits
|
|
226
|
+
const [lo, hi] = useT(df)
|
|
227
|
+
? [qt1(a, df, true, false), a < 1e-8 ? qt1(a, df, false, false) : qt1(1 - a, df, true, false)]
|
|
228
|
+
: [qnorm(a), a < 1e-8 ? qnorm(a, { lowerTail: false }) : qnorm(1 - a)];
|
|
229
|
+
const limit = (b, s, q) => b + q * s;
|
|
230
|
+
return reported("coefci", {
|
|
231
|
+
lower: rows.map((j) => limit(tab.est[j], tab.se[j], lo)),
|
|
232
|
+
upper: rows.map((j) => limit(tab.est[j], tab.se[j], hi)),
|
|
233
|
+
names: rows.map((j) => tab.names[j]),
|
|
234
|
+
colNames: percentNames(a),
|
|
235
|
+
warnings: tab.warnings,
|
|
236
|
+
}, fit, typeof options.vcov === "function" ? undefined : options.vcov);
|
|
237
|
+
}
|
package/dist/models/coxph.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export interface CoxphControl {
|
|
|
22
22
|
export interface CoxphOptions {
|
|
23
23
|
/** Handling of tied event times. Default `"efron"`. */
|
|
24
24
|
ties?: "efron" | "breslow";
|
|
25
|
-
/** Case weights (positive
|
|
25
|
+
/** Case weights (positive). A weight that is not an integer switches the robust variance on (see `robust`). */
|
|
26
26
|
weights?: readonly number[];
|
|
27
27
|
/** Stratum labels, one per observation: a separate baseline hazard per stratum. */
|
|
28
28
|
strata?: readonly (GroupLabel | null)[];
|
|
@@ -34,6 +34,17 @@ export interface CoxphOptions {
|
|
|
34
34
|
nocenter?: readonly number[];
|
|
35
35
|
/** Confidence level of the intervals of exp(coef). Default 0.95. */
|
|
36
36
|
confLevel?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Whether to use the robust (Lin-Wei sandwich) variance. Default as R: `true` when `cluster` is given or some case
|
|
39
|
+
* weight is not an integer, else `false`. With `false` a `cluster` is ignored (with R's warning).
|
|
40
|
+
*/
|
|
41
|
+
robust?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Cluster labels, one per observation, for the robust variance (R's `cluster`): the observations of a cluster are
|
|
44
|
+
* treated as correlated. Rows with a missing label (`null` or NaN) are dropped, like rows with a missing stratum.
|
|
45
|
+
* Without it, each observation is its own cluster. At least two clusters are needed.
|
|
46
|
+
*/
|
|
47
|
+
cluster?: readonly (GroupLabel | null)[];
|
|
37
48
|
}
|
|
38
49
|
/**
|
|
39
50
|
* A test statistic with its degrees of freedom and upper chi-squared tail.
|
|
@@ -56,7 +67,10 @@ export interface CoxphTest {
|
|
|
56
67
|
export interface CoxphResult {
|
|
57
68
|
/** Coefficients (NaN for R's NA: an aliased column or one whose information fell below the tolerance) */
|
|
58
69
|
coefficients: number[];
|
|
59
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* Variance matrix of the coefficients (array of rows): the robust variance when `usedRobust` (R's `var`), else the
|
|
72
|
+
* model-based one
|
|
73
|
+
*/
|
|
60
74
|
var: number[][];
|
|
61
75
|
/** Log partial likelihood at the starting values and at the end */
|
|
62
76
|
loglik: [number, number];
|
|
@@ -74,9 +88,12 @@ export interface CoxphResult {
|
|
|
74
88
|
nevent: number;
|
|
75
89
|
/** Linear predictors (centred by the means), one per observation used, in input order */
|
|
76
90
|
linearPredictors: number[];
|
|
77
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* Model-based (naive) standard errors of the coefficients, also when `usedRobust` (R's column `se(coef)`; the robust
|
|
93
|
+
* ones are `robustSe`)
|
|
94
|
+
*/
|
|
78
95
|
se: number[];
|
|
79
|
-
/** Wald z statistics */
|
|
96
|
+
/** Wald z statistics (coefficient / `robustSe` when `usedRobust`, else / `se`) */
|
|
80
97
|
z: number[];
|
|
81
98
|
/** Two-sided p-values `Pr(>|z|)` */
|
|
82
99
|
p: number[];
|
|
@@ -86,15 +103,15 @@ export interface CoxphResult {
|
|
|
86
103
|
zValue: number[];
|
|
87
104
|
/** Alias of `p`: the same array. */
|
|
88
105
|
pValue: number[];
|
|
89
|
-
/** Lower confidence limits of exp(coef) */
|
|
106
|
+
/** Lower confidence limits of exp(coef) (from `robustSe` when `usedRobust`) */
|
|
90
107
|
lower: number[];
|
|
91
|
-
/** Upper confidence limits of exp(coef) */
|
|
108
|
+
/** Upper confidence limits of exp(coef) (from `robustSe` when `usedRobust`) */
|
|
92
109
|
upper: number[];
|
|
93
110
|
/** Likelihood ratio test */
|
|
94
111
|
logtest: CoxphTest;
|
|
95
112
|
/** Score (log-rank) test */
|
|
96
113
|
sctest: CoxphTest;
|
|
97
|
-
/** Wald test */
|
|
114
|
+
/** Wald test (with the robust variance when `usedRobust`) */
|
|
98
115
|
waldtest: CoxphTest;
|
|
99
116
|
/** R^2 (1 - exp(-logtest / n)) and its maximum (1 - exp(2 loglik[0] / n)) */
|
|
100
117
|
rsq: {
|
|
@@ -103,13 +120,33 @@ export interface CoxphResult {
|
|
|
103
120
|
/** Its maximum, 1 - exp(2 loglik[0] / n) (R's `maxrsq`). */
|
|
104
121
|
maxrsq: number;
|
|
105
122
|
};
|
|
123
|
+
/**
|
|
124
|
+
* Whether the robust variance was used (R's `used.robust`). False also when it was asked for but there is nothing to
|
|
125
|
+
* compute it from (no events, or every coefficient NaN), as R.
|
|
126
|
+
*/
|
|
127
|
+
usedRobust: boolean;
|
|
128
|
+
/** The model-based variance (R's `naive.var`); only when `usedRobust`. */
|
|
129
|
+
naiveVar?: number[][];
|
|
130
|
+
/** Robust standard errors, the square roots of the diagonal of `var` (R's `robust se`); only when `usedRobust`. */
|
|
131
|
+
robustSe?: number[];
|
|
132
|
+
/** The robust score test statistic at `init` (R's `rscore`); only when `usedRobust`. */
|
|
133
|
+
rscore?: number;
|
|
134
|
+
/** The robust score test (R's `robscore`: `rscore`, the number of non-NaN coefficients); only when `usedRobust`. */
|
|
135
|
+
robscore?: CoxphTest;
|
|
106
136
|
/** R's warnings, each once, in order of first appearance. */
|
|
107
137
|
warnings: string[];
|
|
108
138
|
}
|
|
109
139
|
/**
|
|
110
140
|
* Cox proportional hazards regression. Equivalent to R's `survival::coxph()`. As
|
|
111
|
-
* `coxph(Surv(time, status) ~ x [+ strata(strata)], weights, ties, init, control)` for right-censored
|
|
112
|
-
* values of `summary(fit)`.
|
|
141
|
+
* `coxph(Surv(time, status) ~ x [+ strata(strata)], weights, ties, init, robust, cluster, control)` for right-censored
|
|
142
|
+
* data, with the values of `summary(fit)`.
|
|
143
|
+
*
|
|
144
|
+
* With `robust` (by default when `cluster` is given or a case weight is not an integer) the variance is the robust
|
|
145
|
+
* (Lin-Wei sandwich) one, summed over the clusters (each observation its own cluster without `cluster`), as R:
|
|
146
|
+
* `var` is the robust variance and `naiveVar` the model-based one; `se` (and `stdError`) stay the model-based standard
|
|
147
|
+
* errors (R's `se(coef)`), while `z`, `p`, the intervals `lower`/`upper`, `wald` and `waldtest` use the robust ones
|
|
148
|
+
* (`robustSe`); `robscore` is the robust score test at `init`. The likelihood ratio and score tests and the R^2 ignore
|
|
149
|
+
* the clustering.
|
|
113
150
|
*
|
|
114
151
|
* Rows with a missing time, status, covariate, weight or stratum are dropped. Times within sqrt(eps) of each other are
|
|
115
152
|
* treated as tied. Covariates whose values all lie in `nocenter` are not centred; the others are centred by their
|
|
@@ -120,6 +157,8 @@ export interface CoxphResult {
|
|
|
120
157
|
* @param status 1 for an event, 0 for a censored time.
|
|
121
158
|
* @param x The covariate matrix, one row per observation (at least one column).
|
|
122
159
|
* @param options See {@link CoxphOptions}.
|
|
160
|
+
* @throws TypeError if `robust` is not a boolean. RangeError for invalid arguments, among them an infinite weight
|
|
161
|
+
* and a robust variance with fewer than two clusters.
|
|
123
162
|
*
|
|
124
163
|
* @example
|
|
125
164
|
* ```ts
|
|
@@ -131,3 +170,15 @@ export interface CoxphResult {
|
|
|
131
170
|
* @category Survival analysis
|
|
132
171
|
*/
|
|
133
172
|
export declare function coxph(time: readonly number[], status: readonly number[], x: readonly (readonly number[])[], options?: CoxphOptions): CoxphResult;
|
|
173
|
+
/**
|
|
174
|
+
* The fit of {@link coxph}, and with the robust variance also the residuals it was made from (for the tests; not
|
|
175
|
+
* exported from the package): R's `residuals(fit, type = "score")` (unweighted) and `residuals(fit, type =
|
|
176
|
+
* "dfbeta")` (weighted), one row per observation used, in input order.
|
|
177
|
+
*
|
|
178
|
+
* @internal
|
|
179
|
+
*/
|
|
180
|
+
export declare function fitCoxph(time: readonly number[], status: readonly number[], x: readonly (readonly number[])[], options?: CoxphOptions): {
|
|
181
|
+
result: CoxphResult;
|
|
182
|
+
scoreResiduals?: number[][];
|
|
183
|
+
dfbeta?: number[][];
|
|
184
|
+
};
|