@numero-uno-jpn/rstats 1.4.0 → 1.6.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.ja.md +13 -6
  3. package/README.md +13 -6
  4. package/dist/chunks/{chunk-GEF2YRBV.js → chunk-2I7Z7L5J.js} +2 -2
  5. package/dist/chunks/{chunk-F6Z3GLWA.js → chunk-2TVHT4PF.js} +2 -2
  6. package/dist/chunks/{chunk-GBSMRVUY.js → chunk-3PCHPKBD.js} +2 -2
  7. package/dist/chunks/{chunk-TEHMTQNB.js → chunk-55XSRONZ.js} +2 -2
  8. package/dist/chunks/{chunk-5SW3L6J3.js → chunk-5AITXV3T.js} +2 -2
  9. package/dist/chunks/{chunk-GLGOV3A5.js → chunk-7ZNFRZO4.js} +2 -2
  10. package/dist/chunks/{chunk-56GFP4UU.js → chunk-ABSUQPOG.js} +2 -2
  11. package/dist/chunks/{chunk-RUZATICF.js → chunk-AHFMBHZW.js} +1 -1
  12. package/dist/chunks/chunk-BJTGQ52J.js +2 -0
  13. package/dist/chunks/{chunk-SVS54D6Z.js → chunk-BMEO5X5F.js} +2 -2
  14. package/dist/chunks/chunk-F7G74UNI.js +3 -0
  15. package/dist/chunks/{chunk-HWB7XRX2.js → chunk-FLITCXLY.js} +2 -2
  16. package/dist/chunks/{chunk-PZ66PUWC.js → chunk-KAAUNR3G.js} +1 -1
  17. package/dist/chunks/chunk-KND2BFD5.js +2 -0
  18. package/dist/chunks/{chunk-ZRFZ5UIB.js → chunk-N2PNW4HR.js} +2 -2
  19. package/dist/chunks/{chunk-V2KVTAVV.js → chunk-NVIJXWS6.js} +2 -2
  20. package/dist/chunks/{chunk-J72MLFUG.js → chunk-O5CMNUJL.js} +2 -2
  21. package/dist/chunks/{chunk-WWJGCBWH.js → chunk-OZLDZOOA.js} +1 -1
  22. package/dist/chunks/chunk-RSTU6CX7.js +2 -0
  23. package/dist/chunks/{chunk-GIER5WMB.js → chunk-SFZF44G6.js} +2 -2
  24. package/dist/chunks/{chunk-ZQSOMEIM.js → chunk-TPBVVP32.js} +2 -2
  25. package/dist/chunks/{chunk-S2YCFUCA.js → chunk-WAMHZ47G.js} +2 -2
  26. package/dist/chunks/chunk-XNWSQVXS.js +2 -0
  27. package/dist/chunks/{chunk-X7K6EF7S.js → chunk-Z2XN4NN7.js} +2 -2
  28. package/dist/chunks/{chunk-RY7CCWVU.js → chunk-ZL7EAPO5.js} +1 -1
  29. package/dist/descriptive.js +2 -2
  30. package/dist/distributions.js +2 -2
  31. package/dist/entries/mixed.d.ts +2 -0
  32. package/dist/entries/mixed.js +2 -0
  33. package/dist/entries/tests.d.ts +2 -0
  34. package/dist/entries/tests.js +2 -0
  35. package/dist/htest/sampleSize.d.ts +290 -0
  36. package/dist/htest/sampleSize.js +625 -0
  37. package/dist/linalg.js +2 -2
  38. package/dist/mixed.js +5 -5
  39. package/dist/models/competingRisks.d.ts +1 -1
  40. package/dist/models/competingRisks.js +42 -30
  41. package/dist/models/metaAnalysis.d.ts +207 -0
  42. package/dist/models/metaAnalysis.js +696 -0
  43. package/dist/models/mixedConfint.d.ts +42 -0
  44. package/dist/models/mixedConfint.js +573 -0
  45. package/dist/models/mixedSimulate.d.ts +36 -0
  46. package/dist/models/mixedSimulate.js +232 -0
  47. package/dist/numerics.js +2 -2
  48. package/dist/rstats.js +19 -16
  49. package/dist/special.js +2 -2
  50. package/dist/tests.js +16 -13
  51. package/llms.txt +8 -3
  52. package/package.json +1 -1
  53. package/dist/chunks/chunk-27VFB3SA.js +0 -2
  54. package/dist/chunks/chunk-6FZAJQ4H.js +0 -2
  55. package/dist/chunks/chunk-BQJFFGYP.js +0 -2
  56. package/dist/chunks/chunk-H72MW7YP.js +0 -2
  57. package/dist/chunks/chunk-UMMCVF53.js +0 -2
  58. package/dist/chunks/chunk-UNP4HBPP.js +0 -3
  59. package/dist/chunks/chunk-YBOSCQF2.js +0 -2
@@ -38,7 +38,7 @@ function validateVectorLengths(time, status, options) {
38
38
  /**
39
39
  * Cumulative incidence functions and Gray's tests for competing risks. Equivalent to `cmprsk::cuminc()` in R.
40
40
  * Uses the Aalen-Johansen cumulative-incidence estimator and Gray's weighted test; tied-time
41
- * variance uses the documented hypergeometric correction.
41
+ * variance applies the target-cause and competing-cause finite-sample corrections.
42
42
  *
43
43
  * @param time Follow-up times (finite and non-negative).
44
44
  * @param status Event codes; every code other than `cencode` is a competing event cause.
@@ -87,11 +87,11 @@ export function cuminc(time, status, options = {}) {
87
87
  const causes = cs.labels.filter((x) => x !== cencodeValue);
88
88
  if (causes.length === 0)
89
89
  throw new Error("there are no events of any cause");
90
- const rows = keep.map((i) => ({
90
+ const rows = keep.map((i, j) => ({
91
91
  time: time[i],
92
- status: statusValues[keep.indexOf(i)],
93
- group: group === undefined ? 0 : gs.index.get(groupValues[keep.indexOf(i)]),
94
- strata: strata === undefined ? 0 : ss.index.get(strataValues[keep.indexOf(i)]),
92
+ status: statusValues[j],
93
+ group: group === undefined ? 0 : gs.index.get(groupValues[j]),
94
+ strata: strata === undefined ? 0 : ss.index.get(strataValues[j]),
95
95
  source: i,
96
96
  })).sort((a, b) => a.time - b.time);
97
97
  const curves = [];
@@ -160,7 +160,7 @@ export function cuminc(time, status, options = {}) {
160
160
  const tests = gs.labels.length >= 2 ? grayTests(rows, gs.labels, causes, ss.labels.length, rho, cencodeValue) : undefined;
161
161
  return reported("cuminc", { curves, ...(tests === undefined ? {} : { tests }), warnings: warnings.list });
162
162
  }
163
- /** Gray's score and covariance, summed over strata; the tied-time own-score term gets the chosen hypergeometric factor. */
163
+ /** Gray's score and covariance, summed over strata, with finite-sample corrections for tied event times. */
164
164
  function grayTests(rows, groups, causes, nStrata, rho, cencode) {
165
165
  const out = [];
166
166
  for (const cause of causes) {
@@ -232,39 +232,51 @@ function grayTests(rows, groups, causes, nStrata, rho, cencode) {
232
232
  restK += d1 * dGamma;
233
233
  restP += d2 * dGamma;
234
234
  }
235
- const qv = e.Safter[g] === 0 ? 0 : e.Gafter / e.Safter[g];
236
- const ak = dkg + (1 - qv) * restK;
237
- const ap = dpg + (1 - qv) * restP;
238
- const bk = -qv * restK;
239
- const bp = -qv * restP;
235
+ // qAfter is G(t)/S_r(t), used in a_kr and b_kr (post-event values).
236
+ const qAfter = e.Safter[g] === 0 ? 0 : e.Gafter / e.Safter[g];
237
+ const ak = dkg + (1 - qAfter) * restK;
238
+ const ap = dpg + (1 - qAfter) * restP;
239
+ const bk = -qAfter * restK;
240
+ const bp = -qAfter * restP;
240
241
  const dF2 = e.Y[g] === 0 ? 0 : e.Sbefore[g] * e.d2[g] / e.Y[g];
241
- let aa = ak * ap;
242
+ const base = (ak * ap * e.dF0 + bk * bp * dF2) / e.h[g];
242
243
  const d1tot = e.d1.reduce((a, b) => a + b, 0);
243
- const Ytot = e.Y.reduce((a, b) => a + b, 0);
244
- if (d1tot > 1 && Ytot > 1)
245
- aa += ((Ytot - d1tot) / (Ytot - 1) - 1) * dkg * dpg;
246
- covariance += (aa * e.dF0 + bk * bp * dF2) / e.h[g];
244
+ let targetCauseTieCorrection = 0;
245
+ if (d1tot > 1 && e.Y[g] > 0 && e.Sbefore[g] > 0) {
246
+ // Empirically determined from public cmprsk::cuminc() output (Issue #525; oracle/competing-behaviour.md §1.6).
247
+ // This finite-sample tie correction supplements the covariance contribution in Gray (1988), Eq. (2.10).
248
+ // qTieBefore is 1/S_r(t-), used only by Delta^(1); it is not qAfter above.
249
+ const qTieBefore = 1 / e.Sbefore[g];
250
+ if (htot > qTieBefore) {
251
+ targetCauseTieCorrection = (d1tot * (d1tot - 1) / htot) *
252
+ (e.Y[g] * qTieBefore * qTieBefore / (htot - qTieBefore)) *
253
+ (ak * ap / (e.h[g] * e.h[g]));
254
+ }
255
+ }
256
+ let competingCauseTieCorrection = 0;
257
+ if (e.d2[g] > 1 && e.Y[g] > 1) {
258
+ // Empirically determined from public cmprsk::cuminc() output (Issue #525; oracle/competing-behaviour.md §1.6).
259
+ // This within-group finite-sample correction supplements the covariance contribution in Gray (1988), Eq. (2.10).
260
+ competingCauseTieCorrection = ((e.d2[g] - 1) / (e.Y[g] - 1)) *
261
+ (dF2 / e.h[g]) * bk * bp;
262
+ }
263
+ covariance += base - targetCauseTieCorrection - competingCauseTieCorrection;
247
264
  }
248
265
  V[k][kp] += covariance;
249
266
  }
250
267
  }
251
268
  const df = K - 1;
252
269
  let statistic = NaN, pValue = NaN;
253
- if (z.every((x) => x === 0)) {
254
- statistic = 0;
255
- pValue = 1;
270
+ const sub = V.slice(0, df).map((row) => row.slice(0, df));
271
+ try {
272
+ // Check covariance invertibility even when the score is zero; a singular quadratic form is undefined.
273
+ const b = solve(sub, z.slice(0, df), { tol: 1e-12 });
274
+ statistic = z.slice(0, df).reduce((s, x, i) => s + x * b[i], 0);
275
+ pValue = pchisq(statistic, df, { lowerTail: false });
256
276
  }
257
- else {
258
- const sub = V.slice(0, df).map((row) => row.slice(0, df));
259
- try {
260
- const b = solve(sub, z.slice(0, df), { tol: 1e-12 });
261
- statistic = z.slice(0, df).reduce((s, x, i) => s + x * b[i], 0);
262
- pValue = pchisq(statistic, df, { lowerTail: false });
263
- }
264
- catch {
265
- statistic = NaN;
266
- pValue = NaN;
267
- }
277
+ catch {
278
+ statistic = NaN;
279
+ pValue = NaN;
268
280
  }
269
281
  out.push({ cause: levelText(cause), stat: statistic, df, pValue });
270
282
  }
@@ -0,0 +1,207 @@
1
+ import type { MatrixInput } from "../linalg/matrix.js";
2
+ import type { DataFrame } from "./modelFrame.js";
3
+ /** Estimator for the between-study variance.
4
+ * @category Meta-analysis
5
+ */
6
+ export type MetaMethod = "EE" | "FE" | "DL" | "HE" | "HS" | "HSk" | "SJ" | "ML" | "REML" | "EB" | "PM";
7
+ /** Inference method for model coefficients.
8
+ * @category Meta-analysis
9
+ */
10
+ export type MetaTest = "z" | "t" | "knha";
11
+ /** Effect-size measure supported by {@link escalc}.
12
+ * @category Meta-analysis
13
+ */
14
+ export type EscalcMeasure = "RR" | "OR" | "RD" | "MD" | "SMD" | "ZCOR";
15
+ /** Options for {@link escalc}.
16
+ * @category Meta-analysis
17
+ */
18
+ export interface EscalcOptions {
19
+ ai?: readonly number[];
20
+ bi?: readonly number[];
21
+ ci?: readonly number[];
22
+ di?: readonly number[];
23
+ m1i?: readonly number[];
24
+ sd1i?: readonly number[];
25
+ n1i?: readonly number[];
26
+ m2i?: readonly number[];
27
+ sd2i?: readonly number[];
28
+ n2i?: readonly number[];
29
+ ri?: readonly number[];
30
+ ni?: readonly number[];
31
+ add?: number;
32
+ to?: "only0" | "all" | "if0all" | "none";
33
+ drop00?: boolean;
34
+ }
35
+ /** Study-level estimates returned by {@link escalc}.
36
+ * @category Meta-analysis
37
+ */
38
+ export interface EscalcResult {
39
+ yi: number[];
40
+ vi: number[];
41
+ measure: EscalcMeasure;
42
+ warnings: string[];
43
+ }
44
+ /** Iteration controls for {@link rma}.
45
+ * @category Meta-analysis
46
+ */
47
+ export interface RmaControl {
48
+ tau2Init?: number;
49
+ threshold?: number;
50
+ maxiter?: number;
51
+ stepadj?: number;
52
+ tol?: number;
53
+ tau2Max?: number;
54
+ }
55
+ /** A moderator matrix with optional column and row names.
56
+ * @category Meta-analysis
57
+ */
58
+ export interface ModeratorMatrix extends Record<string, unknown> {
59
+ rows?: readonly (readonly number[])[];
60
+ columns?: readonly (readonly number[])[];
61
+ dim?: readonly number[];
62
+ rowNames?: readonly string[] | string | null;
63
+ colNames?: readonly string[] | string | null;
64
+ }
65
+ /** Options for {@link rma}.
66
+ * @category Meta-analysis
67
+ */
68
+ export interface RmaOptions {
69
+ method?: MetaMethod;
70
+ mods?: MatrixInput | readonly number[] | string | ModeratorMatrix;
71
+ data?: DataFrame;
72
+ intercept?: boolean;
73
+ test?: MetaTest;
74
+ level?: number;
75
+ control?: RmaControl;
76
+ }
77
+ /** A numeric matrix with its dimensions and dimnames.
78
+ * @category Meta-analysis
79
+ */
80
+ export interface NumericMatrix extends Record<string, unknown> {
81
+ dim: number[];
82
+ rowNames: string | string[] | null;
83
+ colNames: string | string[] | null;
84
+ rows: number[][];
85
+ }
86
+ /** Fitted meta-analysis model returned by {@link rma}.
87
+ * @category Meta-analysis
88
+ */
89
+ export interface RmaResult {
90
+ beta: NumericMatrix;
91
+ se: number | number[];
92
+ zval: number | number[];
93
+ pval: number | number[];
94
+ "ci.lb": number | number[];
95
+ "ci.ub": number | number[];
96
+ tau2: number;
97
+ "se.tau2": number;
98
+ QE: number;
99
+ QEp: number;
100
+ QM: number;
101
+ QMp: number;
102
+ R2: number | null;
103
+ p: number;
104
+ m: number;
105
+ I2: number;
106
+ H2: number;
107
+ k: number;
108
+ yi: number[];
109
+ vi: number[];
110
+ mods: number[][];
111
+ intercept: boolean;
112
+ method: MetaMethod;
113
+ test: MetaTest;
114
+ level: number;
115
+ warnings: string[];
116
+ covariance: number[][];
117
+ tau2Null: number;
118
+ }
119
+ /** Options for {@link confintRma}.
120
+ * @category Meta-analysis
121
+ */
122
+ export interface ConfintRmaOptions {
123
+ type?: "QP" | "PL";
124
+ level?: number;
125
+ }
126
+ /** Options for {@link predictRma}.
127
+ * @category Meta-analysis
128
+ */
129
+ export interface PredictRmaOptions {
130
+ newmods?: MatrixInput | readonly number[];
131
+ predtype?: "default" | "riley";
132
+ level?: number;
133
+ }
134
+ /** Options for {@link regtest}.
135
+ * @category Meta-analysis
136
+ */
137
+ export interface RegtestOptions {
138
+ model?: "rma" | "lm";
139
+ predictor?: "sei" | "vi" | "ninv";
140
+ retFit?: boolean;
141
+ level?: number;
142
+ }
143
+ /**
144
+ * Computes study-level effect sizes and sampling variances. Equivalent to `metafor::escalc()` in R.
145
+ *
146
+ * @param measure One of RR, OR, RD, MD, SMD or ZCOR.
147
+ * @param options Study-level counts, means, standard deviations, sample sizes or correlations.
148
+ * @returns The estimates yi and sampling variances vi.
149
+ * @category Meta-analysis
150
+ */
151
+ export declare function escalc(measure: EscalcMeasure, options: EscalcOptions): EscalcResult;
152
+ /**
153
+ * Fits an equal-effects or random-effects meta-analysis, including meta-regression. Equivalent to `metafor::rma.uni()` in R.
154
+ *
155
+ * @param yi Study effect estimates, or the result of {@link escalc}.
156
+ * @param vi Sampling variances (omitted when yi is an escalc result).
157
+ * @param options Estimator, moderators, inference and iteration controls.
158
+ * @returns Coefficients, heterogeneity statistics, tests and model metadata.
159
+ * @category Meta-analysis
160
+ */
161
+ export declare function rma(yi: readonly number[] | EscalcResult, viOrOptions?: readonly number[] | RmaOptions, maybeOptions?: RmaOptions): RmaResult;
162
+ /** Computes heterogeneity confidence intervals. Equivalent to `metafor::confint.rma()` in R.
163
+ * @category Meta-analysis
164
+ */
165
+ export declare function confintRma(object: RmaResult, options?: ConfintRmaOptions): {
166
+ random: {
167
+ dim: number[];
168
+ rowNames: string[];
169
+ colNames: string[];
170
+ rows: {
171
+ estimate: number;
172
+ "ci.lb": number;
173
+ "ci.ub": number;
174
+ }[];
175
+ };
176
+ digits: Record<string, string>;
177
+ "ci.null": boolean;
178
+ "lb.sign": string;
179
+ "ub.sign": string;
180
+ "tau2.min": string;
181
+ };
182
+ /** Predicts the mean effect and its prediction interval at moderator values. Equivalent to `metafor::predict.rma()` in R.
183
+ * @category Meta-analysis
184
+ */
185
+ export declare function predictRma(object: RmaResult, options?: PredictRmaOptions): {
186
+ pred: number | number[];
187
+ se: number | number[];
188
+ "ci.lb": number | number[];
189
+ "ci.ub": number | number[];
190
+ "pi.lb": number | number[];
191
+ "pi.ub": number | number[];
192
+ };
193
+ /** Tests for funnel-plot asymmetry using a regression model. Equivalent to `metafor::regtest()` in R.
194
+ * @category Meta-analysis
195
+ */
196
+ export declare function regtest(x: readonly number[] | RmaResult, viOrOptions?: readonly number[] | RegtestOptions, maybeOptions?: RegtestOptions): {
197
+ model: string;
198
+ predictor: string;
199
+ zval: number;
200
+ pval: number;
201
+ dfs: number;
202
+ est: number;
203
+ "ci.lb": number;
204
+ "ci.ub": number;
205
+ warnings: string[];
206
+ fit?: RmaResult;
207
+ };