@numero-uno-jpn/rstats 1.2.0 → 1.4.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 (89) hide show
  1. package/CHANGELOG.md +128 -0
  2. package/README.ja.md +28 -11
  3. package/README.md +27 -10
  4. package/dist/chunks/{chunk-RRO4LMCB.js → chunk-27VFB3SA.js} +2 -2
  5. package/dist/chunks/{chunk-BKV3FINA.js → chunk-56GFP4UU.js} +2 -2
  6. package/dist/chunks/{chunk-JWBSEL3V.js → chunk-5SW3L6J3.js} +2 -2
  7. package/dist/chunks/{chunk-NSU6U3ET.js → chunk-6FZAJQ4H.js} +2 -2
  8. package/dist/chunks/{chunk-VERBXW6E.js → chunk-BQJFFGYP.js} +2 -2
  9. package/dist/chunks/{chunk-ORE5ZRCY.js → chunk-F6Z3GLWA.js} +2 -2
  10. package/dist/chunks/{chunk-2NNBUYHJ.js → chunk-GBSMRVUY.js} +2 -2
  11. package/dist/chunks/{chunk-7WG7X2UI.js → chunk-GEF2YRBV.js} +2 -2
  12. package/dist/chunks/{chunk-L23RSC2N.js → chunk-GIER5WMB.js} +2 -2
  13. package/dist/chunks/{chunk-IAWXF73N.js → chunk-GLGOV3A5.js} +2 -2
  14. package/dist/chunks/{chunk-F3EQLL4F.js → chunk-H72MW7YP.js} +2 -2
  15. package/dist/chunks/{chunk-T5DRIITM.js → chunk-HWB7XRX2.js} +2 -2
  16. package/dist/chunks/chunk-J72MLFUG.js +2 -0
  17. package/dist/chunks/{chunk-TNETWBF5.js → chunk-PZ66PUWC.js} +2 -2
  18. package/dist/chunks/{chunk-IMPRIQE5.js → chunk-RUZATICF.js} +1 -1
  19. package/dist/chunks/{chunk-BI7NYJWP.js → chunk-RY7CCWVU.js} +1 -1
  20. package/dist/chunks/{chunk-7INBXTVV.js → chunk-S2YCFUCA.js} +2 -2
  21. package/dist/chunks/chunk-SVS54D6Z.js +2 -0
  22. package/dist/chunks/{chunk-HBHMEPGY.js → chunk-TEHMTQNB.js} +3 -3
  23. package/dist/chunks/chunk-UMMCVF53.js +2 -0
  24. package/dist/chunks/{chunk-5SGTWSCB.js → chunk-UNP4HBPP.js} +2 -2
  25. package/dist/chunks/{chunk-A66M3ZEA.js → chunk-V2KVTAVV.js} +2 -2
  26. package/dist/chunks/{chunk-EEU7D4RU.js → chunk-WWJGCBWH.js} +1 -1
  27. package/dist/chunks/chunk-X7K6EF7S.js +2 -0
  28. package/dist/chunks/{chunk-73LNRWU3.js → chunk-YBOSCQF2.js} +1 -1
  29. package/dist/chunks/{chunk-6FZHTD5G.js → chunk-ZQSOMEIM.js} +2 -2
  30. package/dist/chunks/{chunk-4AOTAT3O.js → chunk-ZRFZ5UIB.js} +2 -2
  31. package/dist/descriptive.js +2 -2
  32. package/dist/distributions.js +2 -2
  33. package/dist/entries/tests.d.ts +13 -0
  34. package/dist/entries/tests.js +13 -0
  35. package/dist/htest/agreement.d.ts +2 -2
  36. package/dist/htest/blandAltman.d.ts +100 -0
  37. package/dist/htest/blandAltman.js +159 -0
  38. package/dist/htest/diagnosticTests.d.ts +91 -0
  39. package/dist/htest/diagnosticTests.js +116 -0
  40. package/dist/htest/iccPsych.d.ts +106 -0
  41. package/dist/htest/iccPsych.js +332 -0
  42. package/dist/htest/ksTest.d.ts +1 -1
  43. package/dist/htest/mcreg.d.ts +209 -0
  44. package/dist/htest/mcreg.js +735 -0
  45. package/dist/htest/roc.d.ts +287 -0
  46. package/dist/htest/roc.js +635 -0
  47. package/dist/htest/rocTest.d.ts +139 -0
  48. package/dist/htest/rocTest.js +444 -0
  49. package/dist/htest/tTOST.d.ts +202 -0
  50. package/dist/htest/tTOST.js +558 -0
  51. package/dist/htest/trendTests.d.ts +1 -1
  52. package/dist/htest/trendTests.js +1 -1
  53. package/dist/internal/summary.js +5 -0
  54. package/dist/linalg/qr.d.ts +3 -0
  55. package/dist/linalg/qr.js +3 -0
  56. package/dist/linalg.js +2 -2
  57. package/dist/mixed.js +2 -2
  58. package/dist/models/coeftest.d.ts +139 -0
  59. package/dist/models/coeftest.js +237 -0
  60. package/dist/models/competingRisks.d.ts +188 -0
  61. package/dist/models/competingRisks.js +673 -0
  62. package/dist/models/coxZph.d.ts +67 -0
  63. package/dist/models/coxZph.js +479 -0
  64. package/dist/models/coxph.d.ts +107 -16
  65. package/dist/models/coxph.js +641 -72
  66. package/dist/models/glm.d.ts +11 -1
  67. package/dist/models/glm.js +19 -2
  68. package/dist/models/inputRows.d.ts +10 -0
  69. package/dist/models/inputRows.js +23 -0
  70. package/dist/models/lm.d.ts +8 -0
  71. package/dist/models/lm.js +14 -2
  72. package/dist/models/rmst2.d.ts +126 -0
  73. package/dist/models/rmst2.js +365 -0
  74. package/dist/models/sandwich.d.ts +177 -0
  75. package/dist/models/sandwich.js +923 -0
  76. package/dist/models/survival.d.ts +65 -24
  77. package/dist/models/survival.js +407 -72
  78. package/dist/models/survreg.d.ts +160 -0
  79. package/dist/models/survreg.js +809 -0
  80. package/dist/numerics.js +2 -2
  81. package/dist/random/rng.d.ts +8 -1
  82. package/dist/random/rng.js +14 -1
  83. package/dist/rstats.js +16 -16
  84. package/dist/special.js +2 -2
  85. package/dist/tests.js +13 -13
  86. package/llms.txt +25 -0
  87. package/package.json +1 -1
  88. package/dist/chunks/chunk-47IIPUFN.js +0 -2
  89. package/dist/chunks/chunk-CQB4BJBR.js +0 -2
@@ -13,6 +13,8 @@ export interface CoxphControl {
13
13
  tolerChol?: number;
14
14
  /** Tolerance of the check for infinite coefficients. Default sqrt(`eps`). */
15
15
  tolerInf?: number;
16
+ /** Whether to merge event times (and start/stop times for counting-process data) within the tolerance of aeqSurv. Default true. */
17
+ timefix?: boolean;
16
18
  }
17
19
  /**
18
20
  * Options for {@link coxph}.
@@ -20,9 +22,13 @@ export interface CoxphControl {
20
22
  * @category Survival analysis
21
23
  */
22
24
  export interface CoxphOptions {
25
+ /** Start times for counting-process data; when present, time is the stop time. */
26
+ start?: readonly number[];
27
+ /** Subject identifiers for counting-process data; missing identifiers drop their rows. */
28
+ id?: readonly (GroupLabel | null)[];
23
29
  /** Handling of tied event times. Default `"efron"`. */
24
30
  ties?: "efron" | "breslow";
25
- /** Case weights (positive integers; other weights make R use a robust variance, which is not supported yet). */
31
+ /** Case weights (positive). A weight that is not an integer switches the robust variance on (see `robust`). */
26
32
  weights?: readonly number[];
27
33
  /** Stratum labels, one per observation: a separate baseline hazard per stratum. */
28
34
  strata?: readonly (GroupLabel | null)[];
@@ -34,6 +40,19 @@ export interface CoxphOptions {
34
40
  nocenter?: readonly number[];
35
41
  /** Confidence level of the intervals of exp(coef). Default 0.95. */
36
42
  confLevel?: number;
43
+ /**
44
+ * Whether to use the robust (Lin-Wei sandwich) variance. Default as R: `true` when `cluster` is given, a counting
45
+ * process `id` has repeated event rows, or some case weight is not an integer, else `false`. With `false` a
46
+ * `cluster` is ignored (with R's warning).
47
+ */
48
+ robust?: boolean;
49
+ /**
50
+ * Cluster labels, one per observation, for the robust variance (R's `cluster`): the observations of a cluster are
51
+ * treated as correlated. Rows with a missing label (`null` or NaN) are dropped, like rows with a missing stratum.
52
+ * For counting-process data, `id` supplies the subject clusters when `cluster` is absent. Without either, each row
53
+ * is its own cluster for the default robust variance of non-integer weights. At least two clusters are needed.
54
+ */
55
+ cluster?: readonly (GroupLabel | null)[];
37
56
  }
38
57
  /**
39
58
  * A test statistic with its degrees of freedom and upper chi-squared tail.
@@ -48,15 +67,46 @@ export interface CoxphTest {
48
67
  /** Upper chi-squared tail (R's `pvalue`). */
49
68
  pValue: number;
50
69
  }
70
+ /**
71
+ * Processed coxph data retained privately for coxZph; all row arrays are in the fit's used-input order unless named sorted.
72
+ *
73
+ * @internal
74
+ * @category Survival analysis
75
+ */
76
+ export interface CoxphZphData {
77
+ time: number[];
78
+ start?: number[];
79
+ status: boolean[];
80
+ weights: number[];
81
+ strata?: GroupLabel[];
82
+ x: number[][];
83
+ sortedToInput: number[];
84
+ sortedTime: number[];
85
+ sortedStart?: number[];
86
+ sortedEvent: boolean[];
87
+ sortedWeight: number[];
88
+ sortedX: number[][];
89
+ stratumIndex: number[];
90
+ stratumEnd: boolean[];
91
+ betaScaled: number[];
92
+ inverseCovariateScale: number[];
93
+ weightExp: number;
94
+ ties: "efron" | "breslow";
95
+ }
51
96
  /**
52
97
  * Result of {@link coxph}.
53
98
  *
54
99
  * @category Survival analysis
55
100
  */
56
101
  export interface CoxphResult {
102
+ /** @internal Non-enumerable fit data used by coxZph; not part of the serialized result. */
103
+ coxZphData?: CoxphZphData;
57
104
  /** Coefficients (NaN for R's NA: an aliased column or one whose information fell below the tolerance) */
58
105
  coefficients: number[];
59
- /** Variance matrix of the coefficients (array of rows) */
106
+ /**
107
+ * Variance matrix of the coefficients (array of rows): the robust variance when `usedRobust` (R's `var`), else the
108
+ * model-based one
109
+ */
60
110
  var: number[][];
61
111
  /** Log partial likelihood at the starting values and at the end */
62
112
  loglik: [number, number];
@@ -64,7 +114,7 @@ export interface CoxphResult {
64
114
  score: number;
65
115
  /** The Wald statistic of the non-NA coefficients (against `init` when given) */
66
116
  wald: number;
67
- /** Number of iterations (`iterMax + 1` when they ran out) */
117
+ /** Number of iterations (`iterMax + 1` for right-censored fits, `iterMax` for counting-process fits when they ran out). */
68
118
  iter: number;
69
119
  /** The means used to centre the covariates (0 for the columns not centred) */
70
120
  means: number[];
@@ -72,11 +122,16 @@ export interface CoxphResult {
72
122
  n: number;
73
123
  /** Number of events */
74
124
  nevent: number;
125
+ /** Number of distinct subjects, only when id was supplied. */
126
+ nId?: number;
75
127
  /** Linear predictors (centred by the means), one per observation used, in input order */
76
128
  linearPredictors: number[];
77
- /** Standard errors of the coefficients */
129
+ /**
130
+ * Model-based (naive) standard errors of the coefficients, also when `usedRobust` (R's column `se(coef)`; the robust
131
+ * ones are `robustSe`)
132
+ */
78
133
  se: number[];
79
- /** Wald z statistics */
134
+ /** Wald z statistics (coefficient / `robustSe` when `usedRobust`, else / `se`) */
80
135
  z: number[];
81
136
  /** Two-sided p-values `Pr(>|z|)` */
82
137
  p: number[];
@@ -86,15 +141,15 @@ export interface CoxphResult {
86
141
  zValue: number[];
87
142
  /** Alias of `p`: the same array. */
88
143
  pValue: number[];
89
- /** Lower confidence limits of exp(coef) */
144
+ /** Lower confidence limits of exp(coef) (from `robustSe` when `usedRobust`) */
90
145
  lower: number[];
91
- /** Upper confidence limits of exp(coef) */
146
+ /** Upper confidence limits of exp(coef) (from `robustSe` when `usedRobust`) */
92
147
  upper: number[];
93
148
  /** Likelihood ratio test */
94
149
  logtest: CoxphTest;
95
150
  /** Score (log-rank) test */
96
151
  sctest: CoxphTest;
97
- /** Wald test */
152
+ /** Wald test (with the robust variance when `usedRobust`) */
98
153
  waldtest: CoxphTest;
99
154
  /** R^2 (1 - exp(-logtest / n)) and its maximum (1 - exp(2 loglik[0] / n)) */
100
155
  rsq: {
@@ -103,23 +158,45 @@ export interface CoxphResult {
103
158
  /** Its maximum, 1 - exp(2 loglik[0] / n) (R's `maxrsq`). */
104
159
  maxrsq: number;
105
160
  };
161
+ /**
162
+ * Whether the robust variance was used (R's `used.robust`). False also when it was asked for but there is nothing to
163
+ * compute it from (no events, or every coefficient NaN), as R.
164
+ */
165
+ usedRobust: boolean;
166
+ /** The model-based variance (R's `naive.var`); only when `usedRobust`. */
167
+ naiveVar?: number[][];
168
+ /** Robust standard errors, the square roots of the diagonal of `var` (R's `robust se`); only when `usedRobust`. */
169
+ robustSe?: number[];
170
+ /** The robust score test statistic at `init` (R's `rscore`); only when `usedRobust`. */
171
+ rscore?: number;
172
+ /** The robust score test (R's `robscore`: `rscore`, the number of non-NaN coefficients); only when `usedRobust`. */
173
+ robscore?: CoxphTest;
106
174
  /** R's warnings, each once, in order of first appearance. */
107
175
  warnings: string[];
108
176
  }
109
177
  /**
110
178
  * 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 data, with the
112
- * values of `summary(fit)`.
179
+ * `coxph(Surv(time, status) ~ x ...)` for right-censored data and `coxph(Surv(start, stop, status) ~ x ...)` for
180
+ * counting-process data (with the option `start`), with the values of `summary(fit)`.
181
+ * With `robust` (by default when `cluster` is given, a counting-process `id` has repeated event rows, or a case
182
+ * weight is not an integer) the variance is the robust (Lin-Wei sandwich) one, summed over the clusters (each
183
+ * observation its own cluster without `cluster` or `id`), as R:
184
+ * `var` is the robust variance and `naiveVar` the model-based one; `se` (and `stdError`) stay the model-based standard
185
+ * errors (R's `se(coef)`), while `z`, `p`, the intervals `lower`/`upper`, `wald` and `waldtest` use the robust ones
186
+ * (`robustSe`); `robscore` is the robust score test at `init`. The likelihood ratio and score tests and the R^2 ignore
187
+ * the clustering.
113
188
  *
114
- * Rows with a missing time, status, covariate, weight or stratum are dropped. Times within sqrt(eps) of each other are
115
- * treated as tied. Covariates whose values all lie in `nocenter` are not centred; the others are centred by their
116
- * weighted means, which only shifts the linear predictors. R's warnings (non-convergence, possibly infinite
117
- * coefficients) are returned in `warnings`.
189
+ * Rows with a missing time, status, covariate, weight, stratum, cluster or id are dropped. With `start`, the time is
190
+ * the stop time and the risk set is `start < t <= stop`; start and stop times are merged together unless
191
+ * `control.timefix` is false. Counting-process means are unweighted; right-censored means are weighted. With `id`,
192
+ * `nId` reports distinct subjects; `rsq` still uses the number of rows. R's warnings are returned in `warnings`.
118
193
  *
119
- * @param time Follow-up times.
120
- * @param status 1 for an event, 0 for a censored time.
194
+ * @param time Follow-up/stop times; with `start`, these are the stop times.
195
+ * @param status 1 for an event, 0 for a censored time (other counting-process values are dropped with a warning).
121
196
  * @param x The covariate matrix, one row per observation (at least one column).
122
197
  * @param options See {@link CoxphOptions}.
198
+ * @throws TypeError if `robust` or `control.timefix` is not a boolean. RangeError for invalid arguments, among them
199
+ * an infinite weight, an interval with `start >= stop`, and a robust variance with fewer than two clusters.
123
200
  *
124
201
  * @example
125
202
  * ```ts
@@ -131,3 +208,17 @@ export interface CoxphResult {
131
208
  * @category Survival analysis
132
209
  */
133
210
  export declare function coxph(time: readonly number[], status: readonly number[], x: readonly (readonly number[])[], options?: CoxphOptions): CoxphResult;
211
+ /**
212
+ * The fit of {@link coxph}, plus the score and dfbeta residuals used by the tests (not exported from the package):
213
+ * R's `residuals(fit, type = "score")` (unweighted) and `residuals(fit, type = "dfbeta")` (weighted), one row per
214
+ * observation used, in input order. Counting-process fixtures also compare the final score vector and convergence flag.
215
+ *
216
+ * @internal
217
+ */
218
+ export declare function fitCoxph(time: readonly number[], status: readonly number[], x: readonly (readonly number[])[], options?: CoxphOptions, collectCountingResiduals?: boolean): {
219
+ result: CoxphResult;
220
+ scoreResiduals?: number[][];
221
+ dfbeta?: number[][];
222
+ first?: number[];
223
+ converged?: boolean;
224
+ };