@numero-uno-jpn/rstats 1.11.0 → 1.13.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 +20 -0
- package/README.ja.md +7 -6
- package/README.md +7 -6
- package/dist/causal/balance.d.ts +118 -0
- package/dist/causal/balance.js +264 -0
- package/dist/causal/match.d.ts +86 -0
- package/dist/causal/match.js +155 -0
- package/dist/causal/scale.d.ts +8 -0
- package/dist/causal/scale.js +26 -0
- package/dist/causal/weightit.d.ts +159 -0
- package/dist/causal/weightit.js +491 -0
- package/dist/causal.js +2 -0
- package/dist/chunks/{chunk-ZW5OCUIY.js → chunk-24UIPUSE.js} +1 -1
- package/dist/chunks/{chunk-JD6JHIVV.js → chunk-2BXB6DRK.js} +1 -1
- package/dist/chunks/chunk-2N7PIFPJ.js +2 -0
- package/dist/chunks/{chunk-IRO6MXW2.js → chunk-2SOBG73L.js} +2 -2
- package/dist/chunks/{chunk-PF6LEBKO.js → chunk-3T4TVNXS.js} +2 -2
- package/dist/chunks/chunk-5Y425VAJ.js +2 -0
- package/dist/chunks/{chunk-3K5KP3M2.js → chunk-7V6FRF32.js} +2 -2
- package/dist/chunks/chunk-7ZKPFHAO.js +2 -0
- package/dist/chunks/{chunk-MJLK2RIQ.js → chunk-AH6IVJRF.js} +1 -1
- package/dist/chunks/{chunk-W27XXELU.js → chunk-C7NEFIAH.js} +2 -2
- package/dist/chunks/{chunk-OG5FTIWF.js → chunk-CAOEREY3.js} +2 -2
- package/dist/chunks/{chunk-HIPKYBZ3.js → chunk-CBAEKGP7.js} +2 -2
- package/dist/chunks/{chunk-6VIOWV53.js → chunk-CCYGRSQW.js} +2 -2
- package/dist/chunks/{chunk-46DELNLN.js → chunk-CEY7FHMZ.js} +2 -2
- package/dist/chunks/{chunk-Z6UI6ON2.js → chunk-CKQOWTX7.js} +2 -2
- package/dist/chunks/chunk-CSZCDPF3.js +2 -0
- package/dist/chunks/chunk-DQ2MIFRQ.js +2 -0
- package/dist/chunks/{chunk-XLBSUUWY.js → chunk-DZLOU6HK.js} +2 -2
- package/dist/chunks/{chunk-S5JZMHAW.js → chunk-EWJS4VYF.js} +2 -2
- package/dist/chunks/{chunk-KICTA3JY.js → chunk-GQ4HYF4L.js} +2 -2
- package/dist/chunks/chunk-GRXOLZ6J.js +2 -0
- package/dist/chunks/{chunk-BA32XHDA.js → chunk-IIKFLH2E.js} +2 -2
- package/dist/chunks/{chunk-M4SLTLOX.js → chunk-JJGRQ22M.js} +2 -2
- package/dist/chunks/chunk-JTARV4D3.js +2 -0
- package/dist/chunks/{chunk-OWOBNS75.js → chunk-KOF2HBUV.js} +2 -2
- package/dist/chunks/{chunk-BKH5GQNA.js → chunk-LS55YUPZ.js} +2 -2
- package/dist/chunks/chunk-M3LK23YR.js +2 -0
- package/dist/chunks/{chunk-BNHMQU7V.js → chunk-NRBTDLVW.js} +2 -2
- package/dist/chunks/{chunk-HCHZ6M34.js → chunk-NWWY2WMF.js} +2 -2
- package/dist/chunks/{chunk-TNU4SMV3.js → chunk-OVJU5WCL.js} +2 -2
- package/dist/chunks/{chunk-WHKUEZQI.js → chunk-OWD2GUXJ.js} +2 -2
- package/dist/chunks/{chunk-BZBPPRSH.js → chunk-P2UT6OCU.js} +2 -2
- package/dist/chunks/{chunk-XKQSKVCL.js → chunk-P6HSXCGZ.js} +2 -2
- package/dist/chunks/{chunk-YPDFCKL4.js → chunk-QWXSJEDI.js} +2 -2
- package/dist/chunks/{chunk-QRUQEJJ5.js → chunk-RGEW3YLB.js} +2 -2
- package/dist/chunks/{chunk-BGY7Y3P4.js → chunk-ST6I33C6.js} +1 -1
- package/dist/chunks/{chunk-W2D6IDBY.js → chunk-TQ4P6W3P.js} +2 -2
- package/dist/chunks/chunk-TTBKW2KL.js +2 -0
- package/dist/chunks/{chunk-EV7ZQC6D.js → chunk-UMLJFZJ5.js} +2 -2
- package/dist/chunks/{chunk-NO675KX4.js → chunk-VO65CBCV.js} +2 -2
- package/dist/chunks/{chunk-DMT423BG.js → chunk-XRB552MP.js} +2 -2
- package/dist/chunks/{chunk-JFZOKMZA.js → chunk-XRBNTYQW.js} +2 -2
- package/dist/chunks/{chunk-Y2SS3GX2.js → chunk-Y4UWFH4Q.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/causal.d.ts +3 -0
- package/dist/entries/causal.js +4 -0
- package/dist/entries/tests.d.ts +3 -0
- package/dist/entries/tests.js +3 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/clogit.d.ts +80 -0
- package/dist/models/clogit.js +211 -0
- package/dist/models/coxph.d.ts +9 -0
- package/dist/models/coxph.js +3 -3
- package/dist/models/survfitCoxph.d.ts +95 -0
- package/dist/models/survfitCoxph.js +223 -0
- package/dist/models/survival.d.ts +9 -2
- package/dist/models/survival.js +15 -11
- package/dist/models/svyglm.d.ts +77 -0
- package/dist/models/svyglm.js +102 -0
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +18 -18
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries.js +2 -2
- package/package.json +6 -1
- package/dist/chunks/chunk-HPWRADJM.js +0 -2
- package/dist/chunks/chunk-JWMVP7XO.js +0 -2
- package/dist/chunks/chunk-P23KCD67.js +0 -2
- package/dist/chunks/chunk-QY4OBLXK.js +0 -2
- package/dist/chunks/chunk-SV6SLUVB.js +0 -2
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Conditional logistic regression, after survival's clogit(case ~ x + strata(set), method): "exact" maximises the
|
|
2
|
+
// exact conditional likelihood (Gail, Lubin & Rubinstein (1981), "Likelihood calculations for matched case-control
|
|
3
|
+
// studies and survival studies with tied death times", Biometrika 68, 703-707), "approximate" (= "breslow") is the
|
|
4
|
+
// Cox partial likelihood with Breslow's ties on equal times (coxph). Newton-Raphson as coxph (Therneau & Grambsch
|
|
5
|
+
// 2000, chapter 3). Written for rStats.js from the documentation and the published output (no ported code).
|
|
6
|
+
import { pchisq } from "../distributions/chisq.js";
|
|
7
|
+
import { qnorm } from "../distributions/normal.js";
|
|
8
|
+
import { cholFactor, cholInverse, cholSolve, coxph } from "./coxph.js";
|
|
9
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
10
|
+
import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
|
|
11
|
+
const CLOGIT_KEYS = ["method", "confLevel", "control"];
|
|
12
|
+
const CLOGIT_CONTROL_KEYS = ["eps", "iterMax"];
|
|
13
|
+
/**
|
|
14
|
+
* Log likelihood, score and information of the exact conditional likelihood at beta (covariates centred). For each set,
|
|
15
|
+
* B_k (the sum over the subsets of size k of the products of r = exp(eta - max eta)) and its first and second
|
|
16
|
+
* derivatives D_k, H_k are built one member at a time (B_k += r B_{k-1}, from the largest k down). B_k grows like the
|
|
17
|
+
* binomial coefficient, past the doubles for large sets (a set of 2000 with 1000 cases): each k keeps its own power of 2.
|
|
18
|
+
*/
|
|
19
|
+
function evaluateExact(sets, beta) {
|
|
20
|
+
const p = beta.length;
|
|
21
|
+
let loglik = 0;
|
|
22
|
+
const score = new Array(p).fill(0);
|
|
23
|
+
const info = Array.from({ length: p }, () => new Array(p).fill(0));
|
|
24
|
+
for (const { x, cases: d } of sets) {
|
|
25
|
+
const m = x.length;
|
|
26
|
+
if (d === 0 || d === m)
|
|
27
|
+
continue; // a set of cases only or controls only carries no information
|
|
28
|
+
const eta = x.map((row) => row.reduce((acc, v, j) => acc + v * beta[j], 0));
|
|
29
|
+
const top = Math.max(...eta);
|
|
30
|
+
const B = new Array(d + 1).fill(0);
|
|
31
|
+
const E = new Array(d + 1).fill(0); // B_k, D_k, H_k are stored divided by 2^E[k]
|
|
32
|
+
const D = Array.from({ length: d + 1 }, () => new Array(p).fill(0));
|
|
33
|
+
const H = Array.from({ length: d + 1 }, () => Array.from({ length: p }, () => new Array(p).fill(0)));
|
|
34
|
+
B[0] = 1;
|
|
35
|
+
for (let i = 0; i < m; i++) {
|
|
36
|
+
const xi = x[i];
|
|
37
|
+
const r = Math.exp(eta[i] - top);
|
|
38
|
+
for (let k = Math.min(d, i + 1); k >= 1; k--) {
|
|
39
|
+
// new_k = old_k + r old_{k-1}, the two in the common unit 2^e
|
|
40
|
+
const e = B[k] === 0 ? E[k - 1] : Math.max(E[k], E[k - 1]);
|
|
41
|
+
const fa = B[k] === 0 ? 0 : 2 ** (E[k] - e);
|
|
42
|
+
const fb = r * 2 ** (E[k - 1] - e);
|
|
43
|
+
const bPrev = B[k - 1], dPrev = D[k - 1], hPrev = H[k - 1], dk = D[k], hk = H[k];
|
|
44
|
+
B[k] = B[k] * fa + fb * bPrev;
|
|
45
|
+
for (let a = 0; a < p; a++) {
|
|
46
|
+
const da = dk[a];
|
|
47
|
+
dk[a] = da * fa + fb * (xi[a] * bPrev + dPrev[a]);
|
|
48
|
+
for (let b = 0; b < p; b++) {
|
|
49
|
+
hk[a][b] = hk[a][b] * fa + fb * (xi[a] * xi[b] * bPrev + xi[a] * dPrev[b] + xi[b] * dPrev[a] + hPrev[a][b]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
E[k] = e;
|
|
53
|
+
// keep the stored B_k near 1 (D_k and H_k are of the same order times the covariates)
|
|
54
|
+
if (B[k] > 2 ** 256 || (B[k] > 0 && B[k] < 2 ** -256)) {
|
|
55
|
+
const shift = Math.floor(Math.log2(B[k]));
|
|
56
|
+
const f = 2 ** -shift;
|
|
57
|
+
B[k] = B[k] * f;
|
|
58
|
+
for (let a = 0; a < p; a++) {
|
|
59
|
+
dk[a] = dk[a] * f;
|
|
60
|
+
for (let b = 0; b < p; b++)
|
|
61
|
+
hk[a][b] = hk[a][b] * f;
|
|
62
|
+
}
|
|
63
|
+
E[k] = e + shift;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
let num = 0;
|
|
68
|
+
for (let i = 0; i < d; i++)
|
|
69
|
+
num += eta[i] - top;
|
|
70
|
+
loglik += num - (Math.log(B[d]) + E[d] * Math.LN2);
|
|
71
|
+
for (let a = 0; a < p; a++) {
|
|
72
|
+
let sx = 0;
|
|
73
|
+
for (let i = 0; i < d; i++)
|
|
74
|
+
sx += x[i][a];
|
|
75
|
+
const ma = D[d][a] / B[d];
|
|
76
|
+
score[a] += sx - ma;
|
|
77
|
+
for (let b = 0; b < p; b++)
|
|
78
|
+
info[a][b] += H[d][a][b] / B[d] - ma * (D[d][b] / B[d]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { loglik, score, info };
|
|
82
|
+
}
|
|
83
|
+
function test(statistic, df) {
|
|
84
|
+
return { statistic, df, pValue: pchisq(statistic, df, { lowerTail: false }) };
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Conditional logistic regression for matched sets (1:1, 1:M or several cases per set). Equivalent to
|
|
88
|
+
* `survival::clogit()` in R. The call followed is `clogit(y ~ x1 + ... + strata(set), method)`.
|
|
89
|
+
*
|
|
90
|
+
* `y` is 1 for a case and 0 for a control, `x` one row of covariates per observation, `strata` the matched set of
|
|
91
|
+
* each observation (for matched pairs from {@link match}, the pair number). Sets with only cases or only controls
|
|
92
|
+
* carry no information. `"exact"` (R's default) uses the exact conditional likelihood, summing over the subsets of each
|
|
93
|
+
* set with as many members as it has cases; `"approximate"` is Breslow's approximation, the same as {@link coxph}
|
|
94
|
+
* with equal times, `ties: "breslow"` and these strata.
|
|
95
|
+
* @category Survival analysis
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* const y = [1, 0, 1, 0, 1, 0, 0, 1];
|
|
99
|
+
* const x = [[1], [0], [1], [0], [0], [0], [1], [0]];
|
|
100
|
+
* const fit = clogit(y, x, [1, 1, 2, 2, 3, 3, 4, 4]);
|
|
101
|
+
* fit.coefficients; // [0.6931471805599454] (log 2: two discordant pairs for x, one against)
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
export function clogit(y, x, strata, options = {}) {
|
|
105
|
+
checkOptions(options, CLOGIT_KEYS, "clogit"); // generated: option-keys.mjs
|
|
106
|
+
checkOptions(nestedOptions(options, "control"), CLOGIT_CONTROL_KEYS, "clogit", "control"); // generated: option-keys.mjs
|
|
107
|
+
const { method = "exact", confLevel = 0.95, control = {} } = options;
|
|
108
|
+
const { eps = 1e-9, iterMax = 20 } = control;
|
|
109
|
+
const n = y.length;
|
|
110
|
+
if (x.length !== n || strata.length !== n)
|
|
111
|
+
throw new RangeError("'y', 'x' and 'strata' must have the same length");
|
|
112
|
+
if (method !== "exact" && method !== "approximate" && method !== "breslow")
|
|
113
|
+
throw new RangeError(`invalid 'method': ${String(method)}`);
|
|
114
|
+
y.forEach((v, i) => { if (v !== 0 && v !== 1)
|
|
115
|
+
throw new RangeError(`'y' must be 0 or 1 (element ${i + 1})`); });
|
|
116
|
+
const p = x[0]?.length ?? 0;
|
|
117
|
+
if (p === 0 || x.some((row) => row.length !== p || row.some((v) => !Number.isFinite(v)))) {
|
|
118
|
+
throw new RangeError("'x' must have the same positive number of finite covariates in every row");
|
|
119
|
+
}
|
|
120
|
+
const zq = qnorm((1 + confLevel) / 2);
|
|
121
|
+
const finish = (coefficients, variance, loglik, score, wald, iter, m, warnings) => {
|
|
122
|
+
const se = variance.map((row, i) => Math.sqrt(row[i]));
|
|
123
|
+
const z = coefficients.map((c, i) => c / se[i]);
|
|
124
|
+
return {
|
|
125
|
+
coefficients, var: variance, loglik, score, wald, iter, n, nevent: y.reduce((s, v) => s + v, 0), method: m, se, z,
|
|
126
|
+
p: z.map((v) => pchisq(v * v, 1, { lowerTail: false })),
|
|
127
|
+
lower: coefficients.map((c, i) => Math.exp(c - zq * se[i])), upper: coefficients.map((c, i) => Math.exp(c + zq * se[i])),
|
|
128
|
+
logtest: test(2 * (loglik[1] - loglik[0]), p), sctest: test(score, p), waldtest: test(wald, p), warnings,
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
if (method !== "exact") {
|
|
132
|
+
const fit = coxph(new Array(n).fill(1), y, x, { ties: "breslow", strata, confLevel, control: { eps, iterMax } });
|
|
133
|
+
return finish(fit.coefficients, fit.var, fit.loglik, fit.score, fit.wald, fit.iter, "breslow", fit.warnings);
|
|
134
|
+
}
|
|
135
|
+
// matched sets with the cases first; covariates centred on the overall means (the likelihood does not change)
|
|
136
|
+
const means = Array.from({ length: p }, (_, j) => x.reduce((s, row) => s + row[j], 0) / n);
|
|
137
|
+
const groups = new Map();
|
|
138
|
+
for (let i = 0; i < n; i++) {
|
|
139
|
+
let g = groups.get(strata[i]);
|
|
140
|
+
if (g === undefined) {
|
|
141
|
+
g = { cases: [], controls: [] };
|
|
142
|
+
groups.set(strata[i], g);
|
|
143
|
+
}
|
|
144
|
+
(y[i] === 1 ? g.cases : g.controls).push(x[i].map((v, j) => v - means[j]));
|
|
145
|
+
}
|
|
146
|
+
const sets = [...groups.values()].map((g) => ({ x: [...g.cases, ...g.controls], cases: g.cases.length }));
|
|
147
|
+
// Newton-Raphson with step halving, as coxph; the pivots of the information below 2^-39 of the largest are dropped
|
|
148
|
+
// (an aliased covariate gets NaN, as R's NA)
|
|
149
|
+
const tolerChol = 2 ** -39;
|
|
150
|
+
const factorOf = (info) => {
|
|
151
|
+
const f = info.map((row) => row.slice());
|
|
152
|
+
return { f, rank: cholFactor(f, tolerChol) };
|
|
153
|
+
};
|
|
154
|
+
let beta = new Array(p).fill(0);
|
|
155
|
+
let ev = evaluateExact(sets, beta);
|
|
156
|
+
const loglik = [ev.loglik, ev.loglik];
|
|
157
|
+
let { f: factor } = factorOf(ev.info);
|
|
158
|
+
let step = cholSolve(factor, ev.score);
|
|
159
|
+
const score = ev.score.reduce((acc, u, i) => acc + u * step[i], 0);
|
|
160
|
+
const warnings = [];
|
|
161
|
+
// the coefficients accepted at each iteration (0: the start), for the iterations-ran-out case below
|
|
162
|
+
const accepted = [beta];
|
|
163
|
+
let best = ev.loglik;
|
|
164
|
+
let next = beta.map((b, i) => b + step[i]);
|
|
165
|
+
let halvings = 0;
|
|
166
|
+
let converged = false;
|
|
167
|
+
let iter = 0;
|
|
168
|
+
for (iter = 1; iter <= iterMax; iter++) {
|
|
169
|
+
ev = evaluateExact(sets, next);
|
|
170
|
+
const finite = Number.isFinite(ev.loglik) && ev.score.every(Number.isFinite);
|
|
171
|
+
// (no shortcut for an unchanged log likelihood: R's exact fit with only uninformative sets, 0 / 0, runs out)
|
|
172
|
+
if (finite && Math.abs(1 - best / ev.loglik) <= eps) {
|
|
173
|
+
converged = true;
|
|
174
|
+
beta = next;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
if (!finite || ev.loglik < best) {
|
|
178
|
+
halvings++;
|
|
179
|
+
next = next.map((b, i) => (b + halvings * beta[i]) / (halvings + 1));
|
|
180
|
+
accepted.push(beta);
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
halvings = 0;
|
|
184
|
+
best = ev.loglik;
|
|
185
|
+
({ f: factor } = factorOf(ev.info));
|
|
186
|
+
step = cholSolve(factor, ev.score);
|
|
187
|
+
beta = next;
|
|
188
|
+
accepted.push(beta);
|
|
189
|
+
next = next.map((b, i) => b + step[i]);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (!converged) {
|
|
193
|
+
// R's exact fit reports iter.max iterations with the coefficients accepted at iteration max(1, iter.max - 1)
|
|
194
|
+
// (experiments/psm112-edge-iter.txt: iter.max = 1, 2, 3, 5 give the approximate fit's coefficients after 1, 1, 2, 4)
|
|
195
|
+
iter = iterMax;
|
|
196
|
+
beta = accepted[Math.max(1, iterMax - 1)] ?? beta;
|
|
197
|
+
warnings.push("Ran out of iterations and did not converge");
|
|
198
|
+
}
|
|
199
|
+
ev = evaluateExact(sets, beta);
|
|
200
|
+
loglik[1] = ev.loglik;
|
|
201
|
+
const fin = factorOf(ev.info);
|
|
202
|
+
const inverse = cholInverse(fin.f);
|
|
203
|
+
const coefficients = beta.map((b, j) => (fin.rank < p && inverse[j][j] === 0 ? NaN : b));
|
|
204
|
+
// Wald statistic beta' var^-1 beta over the coefficients kept
|
|
205
|
+
const kept = coefficients.map((c, j) => (Number.isNaN(c) ? -1 : j)).filter((j) => j >= 0);
|
|
206
|
+
let wald = 0;
|
|
207
|
+
for (const a of kept)
|
|
208
|
+
for (const b of kept)
|
|
209
|
+
wald += beta[a] * ev.info[a][b] * beta[b];
|
|
210
|
+
return finish(coefficients, inverse, loglik, score, wald, iter, "exact", warnings);
|
|
211
|
+
}
|
package/dist/models/coxph.d.ts
CHANGED
|
@@ -174,6 +174,15 @@ export interface CoxphResult {
|
|
|
174
174
|
/** R's warnings, each once, in order of first appearance. */
|
|
175
175
|
warnings: string[];
|
|
176
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Generalised Cholesky factorisation M = L D L' in place (D on the diagonal, L below it), pivots below the tolerance
|
|
179
|
+
* set to 0. Returns the rank (negative when M is not non-negative definite).
|
|
180
|
+
*/
|
|
181
|
+
export declare function cholFactor(m: number[][], tol: number): number;
|
|
182
|
+
/** Solves M y = b with the factor of {@link cholFactor} (0 for the dropped pivots) */
|
|
183
|
+
export declare function cholSolve(m: readonly number[][], b: readonly number[]): number[];
|
|
184
|
+
/** The (generalised) inverse from the factor of {@link cholFactor}, in place; returns the full symmetric matrix */
|
|
185
|
+
export declare function cholInverse(m: number[][]): number[][];
|
|
177
186
|
/**
|
|
178
187
|
* Cox proportional hazards regression. Equivalent to R's `survival::coxph()`. As
|
|
179
188
|
* `coxph(Surv(time, status) ~ x ...)` for right-censored data and `coxph(Surv(start, stop, status) ~ x ...)` for
|
package/dist/models/coxph.js
CHANGED
|
@@ -441,7 +441,7 @@ function scoreResidualsCounting(data, beta, efron) {
|
|
|
441
441
|
* Generalised Cholesky factorisation M = L D L' in place (D on the diagonal, L below it), pivots below the tolerance
|
|
442
442
|
* set to 0. Returns the rank (negative when M is not non-negative definite).
|
|
443
443
|
*/
|
|
444
|
-
function cholFactor(m, tol) {
|
|
444
|
+
export function cholFactor(m, tol) {
|
|
445
445
|
const p = m.length;
|
|
446
446
|
let scale = 0;
|
|
447
447
|
let anyPositive = false;
|
|
@@ -481,7 +481,7 @@ function cholFactor(m, tol) {
|
|
|
481
481
|
return nonNegative ? rank : -rank;
|
|
482
482
|
}
|
|
483
483
|
/** Solves M y = b with the factor of {@link cholFactor} (0 for the dropped pivots) */
|
|
484
|
-
function cholSolve(m, b) {
|
|
484
|
+
export function cholSolve(m, b) {
|
|
485
485
|
const p = m.length;
|
|
486
486
|
const y = b.slice();
|
|
487
487
|
for (let i = 0; i < p; i++) {
|
|
@@ -503,7 +503,7 @@ function cholSolve(m, b) {
|
|
|
503
503
|
return y;
|
|
504
504
|
}
|
|
505
505
|
/** The (generalised) inverse from the factor of {@link cholFactor}, in place; returns the full symmetric matrix */
|
|
506
|
-
function cholInverse(m) {
|
|
506
|
+
export function cholInverse(m) {
|
|
507
507
|
const p = m.length;
|
|
508
508
|
for (let i = 0; i < p; i++) {
|
|
509
509
|
if (!(m[i][i] > 0))
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { type CoxphOptions, type CoxphResult } from "./coxph.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link survfitCoxph}: those of {@link coxph} for the fit (right-censored data without strata), and of the
|
|
4
|
+
* curves.
|
|
5
|
+
*
|
|
6
|
+
* @category Survival analysis
|
|
7
|
+
*/
|
|
8
|
+
export interface SurvfitCoxphOptions extends Omit<CoxphOptions, "start" | "id" | "strata" | "cluster"> {
|
|
9
|
+
/** Covariate profiles (rows of the columns of `x`), one curve each. Default: one curve at the means of the fit. */
|
|
10
|
+
newdata?: readonly (readonly number[])[];
|
|
11
|
+
/** Transformation of the pointwise confidence intervals: `"log"` (default), `"log-log"`, `"plain"` or `"none"`. */
|
|
12
|
+
confType?: "log" | "log-log" | "plain" | "none";
|
|
13
|
+
/** Confidence level of the curves. Default 0.95. */
|
|
14
|
+
confInt?: number;
|
|
15
|
+
/** Times at which to read the curves, as R's `summary(fit, times)`. */
|
|
16
|
+
times?: readonly number[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Result of {@link survfitCoxph}: the rows of R's `survfit(fit, newdata)` (one column of `surv` per profile).
|
|
20
|
+
*
|
|
21
|
+
* @category Survival analysis
|
|
22
|
+
*/
|
|
23
|
+
export interface SurvfitCoxphResult {
|
|
24
|
+
/** The Cox fit. */
|
|
25
|
+
fit: CoxphResult;
|
|
26
|
+
/** Distinct observed times, events and censorings. */
|
|
27
|
+
time: number[];
|
|
28
|
+
/** Weighted number at risk just before each time. */
|
|
29
|
+
nRisk: number[];
|
|
30
|
+
/** Weighted number of events at each time. */
|
|
31
|
+
nEvent: number[];
|
|
32
|
+
/** Weighted number censored at each time. */
|
|
33
|
+
nCensor: number[];
|
|
34
|
+
/** Survival per curve and time: `surv[curve][row]` (exp(−cumhaz), R's for a Cox fit). */
|
|
35
|
+
surv: number[][];
|
|
36
|
+
/** Standard error of the cumulative hazard (of −log S), per curve and time (with the fit's variance of the
|
|
37
|
+
* coefficients: the robust one when the fit uses it). */
|
|
38
|
+
stdErr: number[][];
|
|
39
|
+
/** Lower pointwise limits, per curve and time. */
|
|
40
|
+
lower: number[][];
|
|
41
|
+
/** Upper pointwise limits, per curve and time. */
|
|
42
|
+
upper: number[][];
|
|
43
|
+
/** Cumulative hazard, per curve and time. */
|
|
44
|
+
cumhaz: number[][];
|
|
45
|
+
/** Per curve: the median and its lower and upper limits (NaN when not reached), as R's `summary()$table`. */
|
|
46
|
+
median: number[][];
|
|
47
|
+
/** Number of records. */
|
|
48
|
+
records: number;
|
|
49
|
+
/** The weighted number at the start (R's n.max and n.start). */
|
|
50
|
+
nStart: number;
|
|
51
|
+
/** The weighted number of events. */
|
|
52
|
+
events: number;
|
|
53
|
+
/** With `times`: the curves read at those times (R's `summary(fit, times)`). */
|
|
54
|
+
at?: SurvfitCoxphAt;
|
|
55
|
+
/** R's warnings, each once. */
|
|
56
|
+
warnings: string[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The curves of {@link survfitCoxph} read at given times (R's `summary(fit, times)`).
|
|
60
|
+
*
|
|
61
|
+
* @category Survival analysis
|
|
62
|
+
*/
|
|
63
|
+
export interface SurvfitCoxphAt {
|
|
64
|
+
/** The times (those not after the last observed time). */
|
|
65
|
+
time: number[];
|
|
66
|
+
/** Survival per curve and time. */
|
|
67
|
+
surv: number[][];
|
|
68
|
+
/** Standard error of the cumulative hazard per curve and time. */
|
|
69
|
+
stdErr: number[][];
|
|
70
|
+
/** Lower pointwise limits per curve and time. */
|
|
71
|
+
lower: number[][];
|
|
72
|
+
/** Upper pointwise limits per curve and time. */
|
|
73
|
+
upper: number[][];
|
|
74
|
+
/** Weighted number at risk at each time. */
|
|
75
|
+
nRisk: number[];
|
|
76
|
+
/** Weighted number of events since the previous time. */
|
|
77
|
+
nEvent: number[];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Predicted survival curves of a Cox model for covariate profiles (adjusted survival curves). Equivalent to R's
|
|
81
|
+
* `survival::survfit.coxph()`. The call followed is
|
|
82
|
+
* `survfit(coxph(Surv(time, status) ~ ..., weights, robust = TRUE), newdata)`.
|
|
83
|
+
*
|
|
84
|
+
* The baseline cumulative hazard is Breslow's (with `ties: "breslow"`) or its Efron form for tied events (the default
|
|
85
|
+
* `"efron"`), with case weights; the curve of a profile z is exp(−exp((z − means) b) Λ0(t)). The standard error is
|
|
86
|
+
* that of the cumulative hazard (Tsiatis), with the fit's variance of the coefficients (the robust one when the fit
|
|
87
|
+
* uses it, as R; with non-integer weights coxph uses it by default). Strata and counting-process data are not supported.
|
|
88
|
+
* @category Survival analysis
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const s = survfitCoxph([5, 3, 8, 1, 9, 4], [1, 1, 0, 1, 0, 1], [[0], [1], [0], [1], [0], [1]], { newdata: [[0], [1]] });
|
|
92
|
+
* s.surv[1]; // [0.7165313106444244, 0.43459820864631604, 0.15987974627276413, 0, 0, 0] (the profile x = 1)
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function survfitCoxph(time: readonly number[], status: readonly number[], x: readonly (readonly number[])[], options?: SurvfitCoxphOptions): SurvfitCoxphResult;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// survival's survfit for a coxph fit with newdata: the predicted (adjusted) survival curves of covariate profiles, with
|
|
2
|
+
// case weights. The behaviour is in oracle/weightit-behaviour.md, from survival's documentation and published output;
|
|
3
|
+
// Kalbfleisch & Prentice (2002), "The Statistical Analysis of Failure Time Data", section 4.3, and Therneau & Grambsch
|
|
4
|
+
// (2000), "Modeling Survival Data", section 10.2, for the baseline hazard (Breslow, and the Efron form for tied events)
|
|
5
|
+
// and the variance of the predicted cumulative hazard (Tsiatis 1981). Written for rStats.js (no ported code).
|
|
6
|
+
import { coxph } from "./coxph.js";
|
|
7
|
+
import { curveMedian } from "./survival.js";
|
|
8
|
+
import { qnorm } from "../distributions/normal.js";
|
|
9
|
+
import { mergeTiedTimes } from "./tiedTimes.js";
|
|
10
|
+
import { ldexp } from "../internal/summary.js";
|
|
11
|
+
import { reported, unreported } from "../internal/warnings.js";
|
|
12
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
13
|
+
import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
|
|
14
|
+
const SURVFIT_COXPH_KEYS = /* @__PURE__ */ new Set([
|
|
15
|
+
"newdata", "confType", "confInt", "times", "ties", "weights", "init", "control", "nocenter", "confLevel", "robust",
|
|
16
|
+
]);
|
|
17
|
+
const SURVFIT_COXPH_CONTROL_KEYS = ["eps", "iterMax", "tolerChol", "tolerInf", "timefix"];
|
|
18
|
+
/**
|
|
19
|
+
* Predicted survival curves of a Cox model for covariate profiles (adjusted survival curves). Equivalent to R's
|
|
20
|
+
* `survival::survfit.coxph()`. The call followed is
|
|
21
|
+
* `survfit(coxph(Surv(time, status) ~ ..., weights, robust = TRUE), newdata)`.
|
|
22
|
+
*
|
|
23
|
+
* The baseline cumulative hazard is Breslow's (with `ties: "breslow"`) or its Efron form for tied events (the default
|
|
24
|
+
* `"efron"`), with case weights; the curve of a profile z is exp(−exp((z − means) b) Λ0(t)). The standard error is
|
|
25
|
+
* that of the cumulative hazard (Tsiatis), with the fit's variance of the coefficients (the robust one when the fit
|
|
26
|
+
* uses it, as R; with non-integer weights coxph uses it by default). Strata and counting-process data are not supported.
|
|
27
|
+
* @category Survival analysis
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const s = survfitCoxph([5, 3, 8, 1, 9, 4], [1, 1, 0, 1, 0, 1], [[0], [1], [0], [1], [0], [1]], { newdata: [[0], [1]] });
|
|
31
|
+
* s.surv[1]; // [0.7165313106444244, 0.43459820864631604, 0.15987974627276413, 0, 0, 0] (the profile x = 1)
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export function survfitCoxph(time, status, x, options = {}) {
|
|
35
|
+
checkOptions(options, SURVFIT_COXPH_KEYS, "survfitCoxph"); // generated: option-keys.mjs
|
|
36
|
+
checkOptions(nestedOptions(options, "control"), SURVFIT_COXPH_CONTROL_KEYS, "survfitCoxph", "control"); // generated: option-keys.mjs
|
|
37
|
+
const { newdata, confType = "log", confInt = 0.95, times, ...coxOptions } = options;
|
|
38
|
+
if (!["log", "log-log", "plain", "none"].includes(confType))
|
|
39
|
+
throw new RangeError(`invalid 'confType': ${String(confType)}`);
|
|
40
|
+
if (!(confInt > 0 && confInt < 1))
|
|
41
|
+
throw new RangeError("'confInt' must be between 0 and 1");
|
|
42
|
+
const fit = unreported(() => coxph(time, status, x, coxOptions));
|
|
43
|
+
const { ties = "efron", weights } = coxOptions;
|
|
44
|
+
// the curves from a fit on the columns rescaled exactly (a power of 2, less a first value), the same model: the
|
|
45
|
+
// variance of a coefficient of a column near 1e300 (about 1e-600) would underflow, and a column 1e15 + small
|
|
46
|
+
// differences lose its digits in the centring; newdata is mapped the same way
|
|
47
|
+
const maps = (x[0] ?? []).map((_, j) => columnMap(x.map((r) => r[j])));
|
|
48
|
+
const scaledX = x.map((r) => r.map((v, j) => maps[j](v)));
|
|
49
|
+
const work = unreported(() => coxph(time, status, scaledX, coxOptions));
|
|
50
|
+
const p = work.coefficients.length;
|
|
51
|
+
const beta = work.coefficients.map((b) => (Number.isFinite(b) ? b : 0));
|
|
52
|
+
// the variance of the fit as R keeps it in `var`: the robust one when the fit uses it
|
|
53
|
+
const V = work.var.map((r) => r.map((v) => (Number.isFinite(v) ? v : 0)));
|
|
54
|
+
// the rows the fit used (no missing value), in time order
|
|
55
|
+
const rows = [];
|
|
56
|
+
for (let i = 0; i < time.length; i++) {
|
|
57
|
+
if (Number.isNaN(time[i]) || Number.isNaN(status[i]) || x[i].some((v) => Number.isNaN(v)))
|
|
58
|
+
continue;
|
|
59
|
+
if (weights !== undefined && Number.isNaN(weights[i]))
|
|
60
|
+
continue;
|
|
61
|
+
rows.push(i);
|
|
62
|
+
}
|
|
63
|
+
// times within the tolerance of coxph's timefix (R's aeqSurv) are one time, as in the fit
|
|
64
|
+
const merged = coxOptions.control?.timefix === false ? time.slice() : mergeTiedTimes(time);
|
|
65
|
+
rows.sort((a, b) => merged[a] - merged[b]);
|
|
66
|
+
const w = (i) => (weights === undefined ? 1 : weights[i]);
|
|
67
|
+
const centred = (v) => v.map((e, j) => e - work.means[j]);
|
|
68
|
+
const risk = new Map();
|
|
69
|
+
for (const i of rows)
|
|
70
|
+
risk.set(i, Math.exp(centred(scaledX[i]).reduce((a, e, j) => a + e * beta[j], 0)));
|
|
71
|
+
// sums over the risk set, from the last time backwards
|
|
72
|
+
const distinct = [];
|
|
73
|
+
for (const i of rows)
|
|
74
|
+
if (distinct.length === 0 || distinct[distinct.length - 1] !== merged[i])
|
|
75
|
+
distinct.push(merged[i]);
|
|
76
|
+
const m = distinct.length;
|
|
77
|
+
const hazard = new Array(m).fill(0), varTerm = new Array(m).fill(0);
|
|
78
|
+
const xbarHaz = Array.from({ length: m }, () => new Array(p).fill(0));
|
|
79
|
+
const nRisk = new Array(m).fill(0), nEvent = new Array(m).fill(0), nCensor = new Array(m).fill(0);
|
|
80
|
+
let S0 = 0, Y = 0;
|
|
81
|
+
const S1 = new Array(p).fill(0);
|
|
82
|
+
let k = rows.length - 1;
|
|
83
|
+
for (let j = m - 1; j >= 0; j--) {
|
|
84
|
+
const t = distinct[j];
|
|
85
|
+
let d = 0, dw = 0, E0 = 0;
|
|
86
|
+
const E1 = new Array(p).fill(0);
|
|
87
|
+
for (; k >= 0 && merged[rows[k]] === t; k--) {
|
|
88
|
+
const i = rows[k], wi = w(i), ri = risk.get(i), xi = centred(scaledX[i]);
|
|
89
|
+
S0 += wi * ri;
|
|
90
|
+
Y += wi;
|
|
91
|
+
for (let a = 0; a < p; a++)
|
|
92
|
+
S1[a] += wi * ri * xi[a];
|
|
93
|
+
if (status[i] === 1) {
|
|
94
|
+
d++;
|
|
95
|
+
dw += wi;
|
|
96
|
+
E0 += wi * ri;
|
|
97
|
+
for (let a = 0; a < p; a++)
|
|
98
|
+
E1[a] += wi * ri * xi[a];
|
|
99
|
+
}
|
|
100
|
+
else
|
|
101
|
+
nCensor[j] += wi;
|
|
102
|
+
}
|
|
103
|
+
nRisk[j] = Y;
|
|
104
|
+
nEvent[j] = dw;
|
|
105
|
+
if (d === 0)
|
|
106
|
+
continue;
|
|
107
|
+
// Efron: the d tied events leave the risk set in d equal steps; Breslow: all at once
|
|
108
|
+
const steps = ties === "efron" ? d : 1;
|
|
109
|
+
for (let s = 0; s < steps; s++) {
|
|
110
|
+
const f = ties === "efron" ? s / d : 0;
|
|
111
|
+
const den = S0 - f * E0, share = dw / steps;
|
|
112
|
+
// divided one factor at a time: den * den under- or overflows for weights near 1e-200 or 1e200
|
|
113
|
+
const h = share / den;
|
|
114
|
+
hazard[j] += h;
|
|
115
|
+
varTerm[j] += h / den;
|
|
116
|
+
for (let a = 0; a < p; a++)
|
|
117
|
+
xbarHaz[j][a] += h * ((S1[a] - f * E1[a]) / den);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (newdata?.some((z) => z.length !== p))
|
|
121
|
+
throw new RangeError("each row of 'newdata' must have one value per column of 'x'");
|
|
122
|
+
// the default profile is the means of the fit as R keeps them (0 for the columns coxph does not centre)
|
|
123
|
+
const profiles = (newdata ?? [fit.means]).map((z) => z.map((v, j) => maps[j](v)));
|
|
124
|
+
const zq = confInt < 1 - 1e-8 ? qnorm(1 - (1 - confInt) / 2) : qnorm((1 - confInt) / 2, { lowerTail: false });
|
|
125
|
+
const surv = [], stdErr = [], lower = [], upper = [], cumhaz = [];
|
|
126
|
+
for (const z of profiles) {
|
|
127
|
+
const zc = centred(z);
|
|
128
|
+
const rz = Math.exp(zc.reduce((a, e, j) => a + e * beta[j], 0));
|
|
129
|
+
let H = 0, v1 = 0;
|
|
130
|
+
const q = new Array(p).fill(0);
|
|
131
|
+
const s = [], se = [], lo = [], up = [], ch = [];
|
|
132
|
+
for (let j = 0; j < m; j++) {
|
|
133
|
+
H += hazard[j];
|
|
134
|
+
v1 += varTerm[j];
|
|
135
|
+
for (let a = 0; a < p; a++)
|
|
136
|
+
q[a] += zc[a] * hazard[j] - xbarHaz[j][a];
|
|
137
|
+
let quad = 0;
|
|
138
|
+
for (let a = 0; a < p; a++)
|
|
139
|
+
for (let b = 0; b < p; b++)
|
|
140
|
+
quad += q[a] * V[a][b] * q[b];
|
|
141
|
+
const cum = rz * H, sv = Math.exp(-cum), e = rz * Math.sqrt(v1 + quad);
|
|
142
|
+
ch.push(cum);
|
|
143
|
+
s.push(sv);
|
|
144
|
+
se.push(e);
|
|
145
|
+
const [l, u] = limits(sv, e, zq, confType);
|
|
146
|
+
lo.push(l);
|
|
147
|
+
up.push(u);
|
|
148
|
+
}
|
|
149
|
+
surv.push(s);
|
|
150
|
+
stdErr.push(se);
|
|
151
|
+
lower.push(lo);
|
|
152
|
+
upper.push(up);
|
|
153
|
+
cumhaz.push(ch);
|
|
154
|
+
}
|
|
155
|
+
const median = surv.map((s, c) => [curveMedian(distinct, s), curveMedian(distinct, lower[c]), curveMedian(distinct, upper[c])]);
|
|
156
|
+
const result = {
|
|
157
|
+
fit, time: distinct, nRisk, nEvent, nCensor, surv, stdErr, lower, upper, cumhaz, median,
|
|
158
|
+
records: rows.length, nStart: nRisk[0] ?? 0, events: nEvent.reduce((a, b) => a + b, 0), warnings: fit.warnings.slice(),
|
|
159
|
+
};
|
|
160
|
+
if (times !== undefined)
|
|
161
|
+
result.at = readAt(result, times);
|
|
162
|
+
return reported("survfitCoxph", result);
|
|
163
|
+
}
|
|
164
|
+
/** The exact map of a column to units near 1: v 2^-e1 less the first such value, times 2^-e2 */
|
|
165
|
+
function columnMap(c) {
|
|
166
|
+
const exponent = (vals) => {
|
|
167
|
+
let top = 0;
|
|
168
|
+
for (const v of vals)
|
|
169
|
+
if (Number.isFinite(v) && Math.abs(v) > top)
|
|
170
|
+
top = Math.abs(v);
|
|
171
|
+
return top > 0 ? Math.floor(Math.log2(top)) : 0;
|
|
172
|
+
};
|
|
173
|
+
const e1 = exponent(c);
|
|
174
|
+
const first = ldexp(c.find((v) => Number.isFinite(v)) ?? 0, -e1);
|
|
175
|
+
const e2 = exponent(c.map((v) => ldexp(v, -e1) - first));
|
|
176
|
+
return (v) => ldexp(ldexp(v, -e1) - first, -e2);
|
|
177
|
+
}
|
|
178
|
+
/** Pointwise limits of a survival value from the standard error of its cumulative hazard */
|
|
179
|
+
function limits(s, se, z, type) {
|
|
180
|
+
if (type === "none" || !Number.isFinite(se))
|
|
181
|
+
return [NaN, NaN];
|
|
182
|
+
if (type === "plain")
|
|
183
|
+
return [Math.max(0, s - z * se * s), Math.min(1, s + z * se * s)];
|
|
184
|
+
if (s === 0)
|
|
185
|
+
return [NaN, NaN];
|
|
186
|
+
if (type === "log")
|
|
187
|
+
return [Math.exp(Math.log(s) - z * se), Math.min(1, Math.exp(Math.log(s) + z * se))];
|
|
188
|
+
// log-log: on the scale of log(-log S), whose standard error is se / |log S|
|
|
189
|
+
if (s === 1)
|
|
190
|
+
return [NaN, NaN];
|
|
191
|
+
const ll = Math.log(-Math.log(s)), e = se / Math.log(s);
|
|
192
|
+
return [Math.exp(-Math.exp(ll - z * e)), Math.exp(-Math.exp(ll + z * e))];
|
|
193
|
+
}
|
|
194
|
+
/** The curves read at given times: the last row at or before each time; before the first, the start of the curve */
|
|
195
|
+
function readAt(r, times) {
|
|
196
|
+
const sorted = [...times].sort((a, b) => a - b);
|
|
197
|
+
const out = { time: [], surv: r.surv.map(() => []), stdErr: r.surv.map(() => []),
|
|
198
|
+
lower: r.surv.map(() => []), upper: r.surv.map(() => []), nRisk: [], nEvent: [] };
|
|
199
|
+
let from = 0;
|
|
200
|
+
for (const t of sorted) {
|
|
201
|
+
let j = -1;
|
|
202
|
+
while (j + 1 < r.time.length && r.time[j + 1] <= t)
|
|
203
|
+
j++;
|
|
204
|
+
if (t > r.time[r.time.length - 1])
|
|
205
|
+
break;
|
|
206
|
+
out.time.push(t);
|
|
207
|
+
// the number at risk at t: that of the first row at or after t
|
|
208
|
+
const next = r.time.findIndex((u) => u >= t);
|
|
209
|
+
out.nRisk.push(next < 0 ? 0 : r.nRisk[next]);
|
|
210
|
+
let ev = 0;
|
|
211
|
+
for (let k = from; k <= j; k++)
|
|
212
|
+
ev += r.nEvent[k];
|
|
213
|
+
out.nEvent.push(ev);
|
|
214
|
+
from = j + 1;
|
|
215
|
+
r.surv.forEach((_, c) => {
|
|
216
|
+
out.surv[c].push(j < 0 ? 1 : r.surv[c][j]);
|
|
217
|
+
out.stdErr[c].push(j < 0 ? 0 : r.stdErr[c][j]);
|
|
218
|
+
out.lower[c].push(j < 0 ? 1 : r.lower[c][j]);
|
|
219
|
+
out.upper[c].push(j < 0 ? 1 : r.upper[c][j]);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
@@ -56,13 +56,14 @@ export interface SurvfitOptions {
|
|
|
56
56
|
start?: readonly number[];
|
|
57
57
|
/** Subject identifiers for counting-process data; requires `start`; adjacent split rows are not censorings. */
|
|
58
58
|
id?: readonly (GroupLabel | null)[];
|
|
59
|
-
/** Case weights
|
|
59
|
+
/** Case weights, non-negative (inverse probability weights need not be integers; not with `stype: 2` and the robust
|
|
60
|
+
* variance). */
|
|
60
61
|
weights?: readonly number[];
|
|
61
62
|
/** Cumulative hazard estimator: 1 Nelson-Aalen, 2 Fleming-Harrington. Default 1. */
|
|
62
63
|
ctype?: 1 | 2;
|
|
63
64
|
/** Survival estimator: 1 Kaplan-Meier, 2 exponential of cumulative hazard. Default 1. */
|
|
64
65
|
stype?: 1 | 2;
|
|
65
|
-
/** Use the infinitesimal-jackknife variance. Default
|
|
66
|
+
/** Use the infinitesimal-jackknife variance. Default as R: true when some weight is not an integer, else false. */
|
|
66
67
|
robust?: boolean;
|
|
67
68
|
/** Restricted-mean upper limit: "common" (default), "individual", "none", or a finite number. */
|
|
68
69
|
rmean?: "none" | "common" | "individual" | number;
|
|
@@ -150,6 +151,12 @@ export interface SurvfitResult {
|
|
|
150
151
|
* @category Survival analysis
|
|
151
152
|
*/
|
|
152
153
|
export declare function survdiff(time: readonly number[], status: readonly number[], group: readonly (GroupLabel | null)[], options?: SurvdiffOptions): SurvdiffResult;
|
|
154
|
+
/**
|
|
155
|
+
* The median of a curve over the rows of a stratum, as survival's survmean (minmin): of the points that are not NaN
|
|
156
|
+
* and lie below 0.5 + sqrt(eps), the first time, or, if the curve equals 0.5 (within sqrt(eps)) there and a later such
|
|
157
|
+
* point is lower, the midpoint between that time and the time of the first lower point. NaN if there is no such point.
|
|
158
|
+
*/
|
|
159
|
+
export declare function curveMedian(time: readonly number[], curve: readonly number[]): number;
|
|
153
160
|
/**
|
|
154
161
|
* Kaplan-Meier or exponential cumulative-hazard estimate with pointwise confidence intervals, medians and restricted means.
|
|
155
162
|
* Equivalent to R's `survival::survfit()`. Supports right-censored and counting-process data, Nelson-Aalen and Fleming-Harrington cumulative hazards,
|