@numero-uno-jpn/rstats 1.18.0 → 1.20.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 (139) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.ja.md +7 -7
  3. package/README.md +7 -7
  4. package/dist/causal.js +2 -2
  5. package/dist/chunks/chunk-33ODAPMR.js +2 -0
  6. package/dist/chunks/chunk-3ST6RZXG.js +2 -0
  7. package/dist/chunks/{chunk-JF4PXFJE.js → chunk-4EQN74JN.js} +2 -2
  8. package/dist/chunks/chunk-5RRFO4NU.js +2 -0
  9. package/dist/chunks/{chunk-3JFRDXN7.js → chunk-64DDDGJU.js} +2 -2
  10. package/dist/chunks/chunk-66TEOHPF.js +2 -0
  11. package/dist/chunks/chunk-6ISDROPG.js +2 -0
  12. package/dist/chunks/{chunk-EC77KAAM.js → chunk-6LAKI2QG.js} +2 -2
  13. package/dist/chunks/{chunk-5CTWYFMB.js → chunk-6QCH4LAL.js} +2 -2
  14. package/dist/chunks/{chunk-H6Z7JVSD.js → chunk-6RA73LXX.js} +2 -2
  15. package/dist/chunks/{chunk-KJMLQHCY.js → chunk-7C2ECGT6.js} +1 -1
  16. package/dist/chunks/{chunk-SCFFHEDZ.js → chunk-7KPFNJBE.js} +2 -2
  17. package/dist/chunks/chunk-ARPXG2PG.js +2 -0
  18. package/dist/chunks/chunk-AWXW4WSX.js +2 -0
  19. package/dist/chunks/{chunk-XQCHC3SH.js → chunk-B5GSH3AK.js} +1 -1
  20. package/dist/chunks/{chunk-2JB7F646.js → chunk-BLDLJHUG.js} +2 -2
  21. package/dist/chunks/{chunk-IKMW63BE.js → chunk-BU4I544S.js} +2 -2
  22. package/dist/chunks/{chunk-LCIX76FR.js → chunk-CAUNJ7S6.js} +2 -2
  23. package/dist/chunks/chunk-DFGW4QWU.js +2 -0
  24. package/dist/chunks/{chunk-WYVUCQO3.js → chunk-DPCCSFCG.js} +1 -1
  25. package/dist/chunks/chunk-EXS5QD7V.js +2 -0
  26. package/dist/chunks/chunk-FOCHNNP3.js +2 -0
  27. package/dist/chunks/{chunk-MXZ64QME.js → chunk-HEPGGURG.js} +2 -2
  28. package/dist/chunks/chunk-INNFAVT3.js +2 -0
  29. package/dist/chunks/chunk-JEELIABM.js +2 -0
  30. package/dist/chunks/chunk-KNF2GPJV.js +2 -0
  31. package/dist/chunks/chunk-L4ZS7MAX.js +2 -0
  32. package/dist/chunks/{chunk-RXYZQ3UX.js → chunk-LZAB4M7P.js} +2 -2
  33. package/dist/chunks/chunk-LZTDYQRS.js +3 -0
  34. package/dist/chunks/{chunk-N7BUE54L.js → chunk-MTSU2QHB.js} +2 -2
  35. package/dist/chunks/{chunk-GIK34P23.js → chunk-MXRN2G7O.js} +2 -2
  36. package/dist/chunks/chunk-OFDD4GTZ.js +2 -0
  37. package/dist/chunks/chunk-OOHFFLMY.js +2 -0
  38. package/dist/chunks/chunk-OOO5AAM7.js +2 -0
  39. package/dist/chunks/chunk-PJJJR3H6.js +2 -0
  40. package/dist/chunks/chunk-PVRNAOO7.js +2 -0
  41. package/dist/chunks/chunk-Q2JLU5YD.js +2 -0
  42. package/dist/chunks/{chunk-Z33Q545U.js → chunk-QCQBK5AC.js} +2 -2
  43. package/dist/chunks/chunk-RXK4KSKH.js +2 -0
  44. package/dist/chunks/chunk-UC3YYJHM.js +4 -0
  45. package/dist/chunks/chunk-UPACHZ4F.js +2 -0
  46. package/dist/chunks/{chunk-VATAUY7N.js → chunk-VCXPGCA4.js} +2 -2
  47. package/dist/chunks/chunk-VFL4GGYT.js +2 -0
  48. package/dist/chunks/chunk-VL72MQOQ.js +2 -0
  49. package/dist/chunks/chunk-VSV5QGXX.js +2 -0
  50. package/dist/chunks/{chunk-CTZUYKVL.js → chunk-WMEF5XWO.js} +2 -2
  51. package/dist/chunks/chunk-X6JS6ZZB.js +2 -0
  52. package/dist/chunks/chunk-YEDVOX5V.js +2 -0
  53. package/dist/chunks/chunk-ZCTB4N5J.js +3 -0
  54. package/dist/descriptive.js +2 -2
  55. package/dist/distributions/normal.d.ts +4 -0
  56. package/dist/distributions/normal.js +4 -2
  57. package/dist/distributions.js +2 -2
  58. package/dist/entries/tests.d.ts +6 -0
  59. package/dist/entries/tests.js +6 -0
  60. package/dist/entries/timeseries.d.ts +11 -0
  61. package/dist/entries/timeseries.js +11 -0
  62. package/dist/htest/timeROC.d.ts +564 -0
  63. package/dist/htest/timeROC.js +1106 -0
  64. package/dist/linalg.js +2 -2
  65. package/dist/meta/metabin.d.ts +32 -0
  66. package/dist/meta/metabin.js +137 -0
  67. package/dist/meta/metaextra.d.ts +153 -0
  68. package/dist/meta/metaextra.js +290 -0
  69. package/dist/meta/metagen.d.ts +124 -0
  70. package/dist/meta/metagen.js +263 -0
  71. package/dist/meta/metaother.d.ts +87 -0
  72. package/dist/meta/metaother.js +342 -0
  73. package/dist/meta/netmeta.d.ts +256 -0
  74. package/dist/meta/netmeta.js +547 -0
  75. package/dist/mixed.js +3 -4
  76. package/dist/models/glmFamily.js +4 -1
  77. package/dist/models/metaAnalysis.d.ts +4 -0
  78. package/dist/models/metaAnalysis.js +100 -18
  79. package/dist/multivariate.js +2 -2
  80. package/dist/numerics.js +2 -2
  81. package/dist/regression.js +2 -2
  82. package/dist/rstats.js +20 -18
  83. package/dist/special.js +2 -2
  84. package/dist/tests.js +15 -15
  85. package/dist/timeseries/acf.js +5 -2
  86. package/dist/timeseries/arima.d.ts +154 -0
  87. package/dist/timeseries/arima.js +1007 -0
  88. package/dist/timeseries/autoArima.d.ts +70 -0
  89. package/dist/timeseries/autoArima.js +191 -0
  90. package/dist/timeseries/ets.d.ts +158 -0
  91. package/dist/timeseries/ets.js +883 -0
  92. package/dist/timeseries/forecastArima.d.ts +136 -0
  93. package/dist/timeseries/forecastArima.js +432 -0
  94. package/dist/timeseries/forecastDispatch.d.ts +83 -0
  95. package/dist/timeseries/forecastDispatch.js +130 -0
  96. package/dist/timeseries/forecastSimple.d.ts +177 -0
  97. package/dist/timeseries/forecastSimple.js +371 -0
  98. package/dist/timeseries/forecastUtils.d.ts +260 -0
  99. package/dist/timeseries/forecastUtils.js +619 -0
  100. package/dist/timeseries/holtWinters.d.ts +65 -0
  101. package/dist/timeseries/holtWinters.js +193 -0
  102. package/dist/timeseries/input.d.ts +8 -0
  103. package/dist/timeseries/input.js +27 -0
  104. package/dist/timeseries/ocsbCritical.d.ts +2 -0
  105. package/dist/timeseries/ocsbCritical.js +6 -0
  106. package/dist/timeseries/scale.d.ts +16 -0
  107. package/dist/timeseries/scale.js +48 -0
  108. package/dist/timeseries/statsTs.d.ts +128 -0
  109. package/dist/timeseries/statsTs.js +456 -0
  110. package/dist/timeseries/stl.d.ts +131 -0
  111. package/dist/timeseries/stl.js +489 -0
  112. package/dist/timeseries/tseries.d.ts +110 -0
  113. package/dist/timeseries/tseries.js +369 -0
  114. package/dist/timeseries.js +2 -2
  115. package/package.json +1 -1
  116. package/dist/chunks/chunk-53WQDB3C.js +0 -2
  117. package/dist/chunks/chunk-6I23C2YJ.js +0 -2
  118. package/dist/chunks/chunk-FLFNMSHI.js +0 -3
  119. package/dist/chunks/chunk-GEHMND3C.js +0 -2
  120. package/dist/chunks/chunk-HG4LJBNV.js +0 -2
  121. package/dist/chunks/chunk-HGAEVPVS.js +0 -2
  122. package/dist/chunks/chunk-HMLYEFQN.js +0 -2
  123. package/dist/chunks/chunk-HRRCC2BD.js +0 -2
  124. package/dist/chunks/chunk-IBHMM2SL.js +0 -2
  125. package/dist/chunks/chunk-ICDW3I2F.js +0 -2
  126. package/dist/chunks/chunk-IOJVFYHW.js +0 -2
  127. package/dist/chunks/chunk-IW4S634O.js +0 -2
  128. package/dist/chunks/chunk-JPWYGTUN.js +0 -2
  129. package/dist/chunks/chunk-KVJBCT3D.js +0 -2
  130. package/dist/chunks/chunk-LY5XGFPU.js +0 -2
  131. package/dist/chunks/chunk-MKZH65DK.js +0 -2
  132. package/dist/chunks/chunk-MVPHISOV.js +0 -2
  133. package/dist/chunks/chunk-NDZVVNBB.js +0 -2
  134. package/dist/chunks/chunk-NOTJ45G3.js +0 -4
  135. package/dist/chunks/chunk-QUD3374N.js +0 -2
  136. package/dist/chunks/chunk-V6FMGGUV.js +0 -2
  137. package/dist/chunks/chunk-W5M3R4NH.js +0 -2
  138. package/dist/chunks/chunk-XUMVTGMM.js +0 -2
  139. package/dist/chunks/chunk-YXMHWS5R.js +0 -2
@@ -0,0 +1,342 @@
1
+ // meta's metacont, metainc, metacor, metamean, metarate and metaprop (inverse variance). Written for rStats.js from the
2
+ // behaviour specification oracle/tsmeta-behaviour.md with the details found in experiments/tsmeta120-log.md (items
3
+ // 66-67, 69, 98). No R or package source was read.
4
+ import { pchisq } from "../distributions/chisq.js";
5
+ import { pnorm, qnorm } from "../distributions/normal.js";
6
+ import { qt1 } from "../distributions/t.js";
7
+ import { glm, glmSummary } from "../models/glm.js";
8
+ import { glmer } from "../models/glmer.js";
9
+ import { lgamma } from "../special/gamma.js";
10
+ import { heterogeneityIntervals, metaCore } from "./metagen.js";
11
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
12
+ import { checkOptions } from "../internal/checkOptions.js";
13
+ const METACONT_KEYS = ["methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "sm"];
14
+ const METAINC_KEYS = /* @__PURE__ */ new Set([
15
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "sm", "method", "incr",
16
+ ]);
17
+ const METACOR_KEYS = ["methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "sm"];
18
+ const METAMEAN_KEYS = ["methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "sm"];
19
+ const METARATE_KEYS = [
20
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "sm", "incr",
21
+ ];
22
+ const METAPROP_KEYS = [
23
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "sm", "incr",
24
+ ];
25
+ const METAPROP_GLMM_KEYS = ["methodRandomCi", "level", "levelMa", "levelPredict"];
26
+ // Generated by `node scripts/option-keys.mjs --write` (end)
27
+ const sum = (v) => v.reduce((a, b) => a + b, 0);
28
+ const sameLength = (what, ...v) => {
29
+ const k = v[0].length;
30
+ if (v.some((x) => x.length !== k))
31
+ throw new RangeError(`${what}: the inputs must have the same length`);
32
+ if (k === 0)
33
+ throw new RangeError(`${what}: no studies`);
34
+ return k;
35
+ };
36
+ /** Input checks with clear messages (1.20.0 edge-case review). */
37
+ const check = (ok, msg) => { if (!ok)
38
+ throw new RangeError(msg); };
39
+ const finitePos = (v, what, name) => check(v.every((x) => Number.isFinite(x) && x > 0), `${what}: ${name} must be finite and positive`);
40
+ const counts = (x, n, what) => {
41
+ finitePos(n, what, "n");
42
+ check(x.every((e, i) => Number.isFinite(e) && e >= 0 && e <= n[i]), `${what}: events must lie in 0..n`);
43
+ };
44
+ /** The pooled result of study values (NaN values left out of the pooling, kept in TE / seTE). */
45
+ function pool(TE, seTE, options) {
46
+ const idx = TE.map((t, i) => (Number.isFinite(t) && Number.isFinite(seTE[i]) && seTE[i] > 0 ? i : -1)).filter((i) => i >= 0);
47
+ if (idx.length === 0)
48
+ throw new RangeError("meta: no study with a finite estimate and standard error");
49
+ const core = metaCore(idx.map((i) => TE[i]), idx.map((i) => seTE[i]), options);
50
+ const spread = (v) => { const o = new Array(TE.length).fill(0); idx.forEach((i, j) => { o[i] = v[j]; }); return o; };
51
+ return { ...core, TE, seTE, k: idx.length, "w.common": spread(core["w.common"]), "w.random": spread(core["w.random"]) };
52
+ }
53
+ /**
54
+ * Meta-analysis of continuous outcomes (meta's `metacont()`): sm "MD" (default), "SMD" (Hedges' g with the exact J;
55
+ * variance 1/n1 + 1/n2 + (1 - (N - 4) / ((N - 2) J^2)) g^2, log 69) or "ROM" (log ratio of means).
56
+ * Equivalent to `meta::metacont()` in R.
57
+ *
58
+ * @category Meta-analysis
59
+ */
60
+ export function metacont(n1, m1, sd1, n2, m2, sd2, options = {}) {
61
+ checkOptions(options, METACONT_KEYS, "metacont"); // generated: option-keys.mjs
62
+ const k = sameLength("metacont", n1, m1, sd1, n2, m2, sd2);
63
+ const sm = options.sm ?? "MD";
64
+ finitePos(n1, "metacont", "n1");
65
+ finitePos(n2, "metacont", "n2");
66
+ check([...sd1, ...sd2].every((x) => Number.isFinite(x) && x >= 0), "metacont: sd must be finite and non-negative");
67
+ check([...m1, ...m2].every(Number.isFinite), "metacont: means must be finite");
68
+ const TE = [], seTE = [];
69
+ for (let i = 0; i < k; i++) {
70
+ const a = n1[i], b = n2[i], s1 = sd1[i], s2 = sd2[i];
71
+ // the square roots of sums of squares by hypot: no overflow of the squares at means and sds near 1e+-300 (log 110)
72
+ if (sm === "MD") {
73
+ TE.push(m1[i] - m2[i]);
74
+ seTE.push(Math.hypot(s1 / Math.sqrt(a), s2 / Math.sqrt(b)));
75
+ }
76
+ else if (sm === "ROM") {
77
+ TE.push(Math.log(m1[i] / m2[i]));
78
+ seTE.push(Math.hypot(s1 / (Math.sqrt(a) * m1[i]), s2 / (Math.sqrt(b) * m2[i])));
79
+ }
80
+ else {
81
+ const N = a + b;
82
+ const sp = Math.hypot(s1 * Math.sqrt(a - 1), s2 * Math.sqrt(b - 1)) / Math.sqrt(N - 2);
83
+ const df = N - 2;
84
+ const J = Math.exp(lgamma(df / 2) - 0.5 * Math.log(df / 2) - lgamma((df - 1) / 2));
85
+ const g = (J * (m1[i] - m2[i])) / sp;
86
+ TE.push(g);
87
+ seTE.push(Math.sqrt(1 / a + 1 / b + (1 - (N - 4) / ((N - 2) * J * J)) * g * g));
88
+ }
89
+ }
90
+ return pool(TE, seTE, options);
91
+ }
92
+ /**
93
+ * Meta-analysis of incidence rates (meta's `metainc()`): events e1 / e2 in person-times t1 / t2; sm "IRR" (default) or
94
+ * "IRD"; method "MH" (default; Greenland and Robins 1985, no increments) or "Inverse". Studies with a zero get 0.5 added
95
+ * to the events for the study values (log 67).
96
+ * Equivalent to `meta::metainc()` in R.
97
+ *
98
+ * @category Meta-analysis
99
+ */
100
+ export function metainc(e1, t1, e2, t2, options = {}) {
101
+ checkOptions(options, METAINC_KEYS, "metainc"); // generated: option-keys.mjs
102
+ const k = sameLength("metainc", e1, t1, e2, t2);
103
+ const sm = options.sm ?? "IRR";
104
+ const method = options.method ?? "MH";
105
+ const incr = options.incr ?? 0.5;
106
+ finitePos([...t1, ...t2], "metainc", "time");
107
+ check([...e1, ...e2].every((x) => Number.isFinite(x) && x >= 0), "metainc: events must be finite and non-negative");
108
+ const TE = [], seTE = [];
109
+ for (let i = 0; i < k; i++) {
110
+ const z = e1[i] === 0 || e2[i] === 0 ? incr : 0;
111
+ const x1 = e1[i] + z, x2 = e2[i] + z;
112
+ if (sm === "IRR") {
113
+ TE.push(Math.log(x1 / t1[i] / (x2 / t2[i])));
114
+ seTE.push(Math.sqrt(1 / x1 + 1 / x2));
115
+ }
116
+ else {
117
+ TE.push(e1[i] / t1[i] - e2[i] / t2[i]);
118
+ seTE.push(Math.hypot(Math.sqrt(x1) / t1[i], Math.sqrt(x2) / t2[i]));
119
+ }
120
+ }
121
+ const res = pool(TE, seTE, options);
122
+ if (method === "MH") {
123
+ const T = t1.map((x, i) => x + t2[i]);
124
+ let te, se, w;
125
+ if (sm === "IRR") {
126
+ // the person-time as fractions of T (no products of times: times near 1e+-300 work; log 110)
127
+ const R = e1.map((x, i) => x * (t2[i] / T[i])), S = e2.map((x, i) => x * (t1[i] / T[i]));
128
+ te = Math.log(sum(R) / sum(S));
129
+ se = Math.sqrt(sum(T.map((tt, i) => (t1[i] / tt) * (t2[i] / tt) * (e1[i] + e2[i]))) / (sum(R) * sum(S)));
130
+ w = S;
131
+ }
132
+ else {
133
+ const W = t1.map((x, i) => x * (t2[i] / T[i]));
134
+ te = sum(e1.map((x, i) => x * (t2[i] / T[i]) - e2[i] * (t1[i] / T[i]))) / sum(W);
135
+ // the weights relative to the largest (the ratio does not depend on their scale)
136
+ // e / t^2 = (e / t) / (t / tm) / tm (tm the largest time: no underflow at times near 1e300)
137
+ const wm = Math.max(...W);
138
+ const Wn = W.map((wi) => wi / wm);
139
+ const tm = Math.max(...t1, ...t2);
140
+ se = Math.sqrt(sum(Wn.map((wi, i) => wi * wi * ((e1[i] / t1[i]) / (t1[i] / tm) + (e2[i] / t2[i]) / (t2[i] / tm))))) / Math.sqrt(tm) / sum(Wn);
141
+ w = W;
142
+ }
143
+ const level = options.levelMa ?? options.level ?? 0.95;
144
+ const zq = qnorm(0.5 + level / 2);
145
+ const st = te / se;
146
+ Object.assign(res, { "TE.common": te, "seTE.common": se, "lower.common": te - zq * se, "upper.common": te + zq * se, "statistic.common": st, "pval.common": 2 * pnorm(-Math.abs(st)), "w.common": w });
147
+ }
148
+ return res;
149
+ }
150
+ /**
151
+ * Meta-analysis of correlations (meta's `metacor()`): sm "ZCOR" (Fisher's z, variance 1/(n - 3), default) or "COR"
152
+ * (variance (1 - r^2)^2 / (n - 1)).
153
+ * Equivalent to `meta::metacor()` in R.
154
+ *
155
+ * @category Meta-analysis
156
+ */
157
+ export function metacor(cor, n, options = {}) {
158
+ checkOptions(options, METACOR_KEYS, "metacor"); // generated: option-keys.mjs
159
+ sameLength("metacor", cor, n);
160
+ const sm = options.sm ?? "ZCOR";
161
+ check(cor.every((r) => Number.isFinite(r) && r >= -1 && r <= 1), "metacor: cor must lie in [-1, 1]");
162
+ check(n.every((x) => Number.isFinite(x) && x > (sm === "ZCOR" ? 3 : 1)), `metacor: n must exceed ${sm === "ZCOR" ? 3 : 1}`);
163
+ return pool(cor.map((r) => (sm === "ZCOR" ? Math.atanh(r) : r)), cor.map((r, i) => (sm === "ZCOR" ? 1 / Math.sqrt(n[i] - 3) : (1 - r * r) / Math.sqrt(n[i] - 1))), options);
164
+ }
165
+ /**
166
+ * Meta-analysis of single means (meta's `metamean()`): sm "MRAW" (default; variance sd^2/n) or "MLN" (log mean,
167
+ * variance sd^2 / (n mean^2)).
168
+ * Equivalent to `meta::metamean()` in R.
169
+ *
170
+ * @category Meta-analysis
171
+ */
172
+ export function metamean(n, mean, sd, options = {}) {
173
+ checkOptions(options, METAMEAN_KEYS, "metamean"); // generated: option-keys.mjs
174
+ sameLength("metamean", n, mean, sd);
175
+ const sm = options.sm ?? "MRAW";
176
+ finitePos(n, "metamean", "n");
177
+ check(sd.every((x) => Number.isFinite(x) && x >= 0), "metamean: sd must be finite and non-negative");
178
+ return pool(mean.map((m) => (sm === "MRAW" ? m : Math.log(m))), sd.map((s, i) => (sm === "MRAW" ? s / Math.sqrt(n[i]) : s / (Math.sqrt(n[i]) * mean[i]))), options);
179
+ }
180
+ /**
181
+ * Meta-analysis of single incidence rates (meta's `metarate()`): sm "IRLN" (log rate, default), "IR", "IRS" (square
182
+ * root) or "IRFT" (Freeman-Tukey); 0.5 added to zero counts for IR and IRLN.
183
+ * Equivalent to `meta::metarate()` in R.
184
+ *
185
+ * @category Meta-analysis
186
+ */
187
+ export function metarate(event, time, options = {}) {
188
+ checkOptions(options, METARATE_KEYS, "metarate"); // generated: option-keys.mjs
189
+ sameLength("metarate", event, time);
190
+ const sm = options.sm ?? "IRLN";
191
+ const incr = options.incr ?? 0.5;
192
+ finitePos(time, "metarate", "time");
193
+ check(event.every((x) => Number.isFinite(x) && x >= 0), "metarate: events must be finite and non-negative");
194
+ const TE = [], seTE = [];
195
+ event.forEach((e0, i) => {
196
+ const t = time[i];
197
+ const e = (sm === "IR" || sm === "IRLN") && e0 === 0 ? e0 + incr : e0;
198
+ if (sm === "IRLN") {
199
+ TE.push(Math.log(e / t));
200
+ seTE.push(Math.sqrt(1 / e));
201
+ }
202
+ else if (sm === "IR") {
203
+ TE.push(e / t);
204
+ seTE.push(Math.sqrt(e) / t);
205
+ }
206
+ else if (sm === "IRS") {
207
+ TE.push(Math.sqrt(e / t));
208
+ seTE.push(Math.sqrt(1 / (4 * t)));
209
+ }
210
+ else {
211
+ TE.push(0.5 * (Math.sqrt(e / t) + Math.sqrt((e + 1) / t)));
212
+ seTE.push(Math.sqrt(1 / (4 * t)));
213
+ }
214
+ });
215
+ return pool(TE, seTE, options);
216
+ }
217
+ /**
218
+ * Meta-analysis of single proportions by the inverse variance (meta's `metaprop(method = "Inverse")`): sm "PLOGIT"
219
+ * (default; (x + 0.5)/(n + 1) for 0 or n events), "PFT" (Freeman-Tukey), "PAS" (arcsine), "PRAW" or "PLN"
220
+ * (log((x + 0.5)/(n + 0.5)) for 0; for n events the variance 1/x - 1/(n + 0.5)). The default method of meta, the GLMM, is {@link metapropGlmm}.
221
+ * Equivalent to `meta::metaprop()` in R.
222
+ *
223
+ * @category Meta-analysis
224
+ */
225
+ export function metaprop(event, n, options = {}) {
226
+ checkOptions(options, METAPROP_KEYS, "metaprop"); // generated: option-keys.mjs
227
+ sameLength("metaprop", event, n);
228
+ const sm = options.sm ?? "PLOGIT";
229
+ const incr = options.incr ?? 0.5;
230
+ counts(event, n, "metaprop");
231
+ const TE = [], seTE = [];
232
+ event.forEach((x, i) => {
233
+ const N = n[i];
234
+ const edge = x === 0 || x === N;
235
+ if (sm === "PLOGIT") {
236
+ const a = edge ? x + incr : x, b = edge ? N - x + incr : N - x;
237
+ TE.push(Math.log(a / b));
238
+ seTE.push(Math.sqrt(1 / a + 1 / b));
239
+ }
240
+ else if (sm === "PFT") {
241
+ TE.push(0.5 * (Math.asin(Math.sqrt(x / (N + 1))) + Math.asin(Math.sqrt((x + 1) / (N + 1)))));
242
+ seTE.push(Math.sqrt(1 / (4 * N + 2)));
243
+ }
244
+ else if (sm === "PAS") {
245
+ TE.push(Math.asin(Math.sqrt(x / N)));
246
+ seTE.push(Math.sqrt(1 / (4 * N)));
247
+ }
248
+ else if (sm === "PRAW") {
249
+ const a = edge ? x + incr : x, nn = edge ? N + 2 * incr : N;
250
+ const p = a / nn;
251
+ TE.push(x / N);
252
+ seTE.push(Math.sqrt((p * (1 - p)) / nn));
253
+ }
254
+ else {
255
+ // PLN: for 0 events log((x + 0.5)/(n + 0.5)); for n events the raw log(x/n) = 0 with the variance 1/x - 1/(n + 0.5)
256
+ // (meta's values, log 98)
257
+ const a = x === 0 ? x + incr : x, nn = edge ? N + incr : N;
258
+ TE.push(Math.log(a / (x === 0 ? nn : N)));
259
+ seTE.push(Math.sqrt(1 / a - 1 / nn));
260
+ }
261
+ });
262
+ return pool(TE, seTE, options);
263
+ }
264
+ /**
265
+ * Meta-analysis of single proportions by a random intercept logistic model (meta's `metaprop()` default, method
266
+ * "GLMM"; log 75-76): glmer(x/n ~ 1 + (1 | study), binomial, weights n, nAGQ = 7) for the random effect and tau2,
267
+ * glm(x/n ~ 1) for the common effect; Q = [Wald, LRT]: the Wald test of equal study effects in glm(x/n ~ study) and the
268
+ * common glm's deviance; H and I2 from the Wald Q. Study values are the PLOGIT ones.
269
+ * Equivalent to `meta::metaprop()` in R.
270
+ *
271
+ * @category Meta-analysis
272
+ */
273
+ export function metapropGlmm(event, n, options = {}) {
274
+ checkOptions(options, METAPROP_GLMM_KEYS, "metapropGlmm"); // generated: option-keys.mjs
275
+ const k = sameLength("metapropGlmm", event, n);
276
+ counts(event, n, "metapropGlmm");
277
+ if (k < 2)
278
+ throw new RangeError("metapropGlmm: at least two studies");
279
+ const study = event.map((_, i) => `s${String(i + 1).padStart(String(k).length, "0")}`);
280
+ const data = { p: event.map((x, i) => x / n[i]), w: n.slice(), study };
281
+ const fitR = glmer("p ~ 1 + (1 | study)", data, { family: "binomial", weights: "w", nAGQ: 7 });
282
+ const tau2 = fitR.theta[0] ** 2;
283
+ const teR = fitR.fixef[0];
284
+ const seR = Math.sqrt(fitR.vcov[0][0]);
285
+ const fitC = glm("p ~ 1", data, { family: "binomial", weights: "w" });
286
+ const teC = fitC.coefficients[0];
287
+ const seC = Math.sqrt(glmSummary(fitC).covUnscaled[0][0]);
288
+ const fitS = glm("p ~ study", data, { family: "binomial", weights: "w" });
289
+ const V = glmSummary(fitS).covUnscaled;
290
+ const d = fitS.coefficients.slice(1);
291
+ const Vd = V.slice(1).map((r) => r.slice(1));
292
+ const qWald = dot(d, solveSym(Vd, d));
293
+ const qLrt = fitC.deviance;
294
+ const df = k - 1;
295
+ const level = options.levelMa ?? options.level ?? 0.95;
296
+ const z = qnorm(0.5 + level / 2);
297
+ const levelPredict = options.levelPredict ?? options.level ?? 0.95;
298
+ const sePred = Math.sqrt(tau2 + seR * seR);
299
+ const tp = qt1(0.5 + levelPredict / 2, df, true, false);
300
+ const het = heterogeneityIntervals(qWald, k, options.level ?? 0.95);
301
+ const plogit = metaprop(event, n, { sm: "PLOGIT" });
302
+ const stC = teC / seC;
303
+ return {
304
+ TE: plogit.TE, seTE: plogit.seTE, k,
305
+ "TE.common": teC, "seTE.common": seC, "lower.common": teC - z * seC, "upper.common": teC + z * seC, "statistic.common": stC, "pval.common": 2 * pnorm(-Math.abs(stC)),
306
+ "TE.random": teR, "seTE.random": seR, "lower.random": teR - z * seR, "upper.random": teR + z * seR, "statistic.random": teR / seR, "pval.random": 2 * pnorm(-Math.abs(teR / seR)), "df.random": null,
307
+ "seTE.predict": sePred, "lower.predict": teR - tp * sePred, "upper.predict": teR + tp * sePred, "df.predict": df,
308
+ Q: [qWald, qLrt], "df.Q": [df, df], "pval.Q": [pchisq(qWald, df, { lowerTail: false }), pchisq(qLrt, df, { lowerTail: false })],
309
+ tau2, "lower.tau2": NaN, "upper.tau2": NaN, tau: Math.sqrt(tau2), "lower.tau": NaN, "upper.tau": NaN,
310
+ H: het.H, "lower.H": het.lH, "upper.H": het.uH, I2: het.I2, "lower.I2": het.lI2, "upper.I2": het.uI2,
311
+ "w.common": new Array(k).fill(NaN), "w.random": new Array(k).fill(NaN),
312
+ methodTau: "ML", methodRandomCi: options.methodRandomCi ?? "classic", methodTauCi: "QP",
313
+ };
314
+ }
315
+ const dot = (a, b) => a.reduce((s, x, i) => s + x * b[i], 0);
316
+ /** Solves A x = b for a symmetric positive definite A (Cholesky). */
317
+ function solveSym(A, b) {
318
+ const n = b.length;
319
+ const L = Array.from({ length: n }, () => new Array(n).fill(0));
320
+ for (let i = 0; i < n; i++)
321
+ for (let j = 0; j <= i; j++) {
322
+ let s = A[i][j];
323
+ for (let k = 0; k < j; k++)
324
+ s -= L[i][k] * L[j][k];
325
+ L[i][j] = i === j ? Math.sqrt(s) : s / L[j][j];
326
+ }
327
+ const y = new Array(n);
328
+ for (let i = 0; i < n; i++) {
329
+ let s = b[i];
330
+ for (let k = 0; k < i; k++)
331
+ s -= L[i][k] * y[k];
332
+ y[i] = s / L[i][i];
333
+ }
334
+ const x = new Array(n);
335
+ for (let i = n - 1; i >= 0; i--) {
336
+ let s = y[i];
337
+ for (let k = i + 1; k < n; k++)
338
+ s -= L[k][i] * x[k];
339
+ x[i] = s / L[i][i];
340
+ }
341
+ return x;
342
+ }
@@ -0,0 +1,256 @@
1
+ /**
2
+ * A matrix with treatment names.
3
+ *
4
+ * @category Meta-analysis
5
+ */
6
+ export interface TreatmentMatrix {
7
+ rows: string[];
8
+ cols: string[];
9
+ values: number[][];
10
+ }
11
+ /**
12
+ * The result of {@link netmeta} (R's element names).
13
+ *
14
+ * @category Meta-analysis
15
+ */
16
+ export interface NetmetaResult {
17
+ trts: string[];
18
+ studlab: string[];
19
+ treat1: string[];
20
+ treat2: string[];
21
+ TE: number[];
22
+ seTE: number[];
23
+ k: number;
24
+ n: number;
25
+ m: number;
26
+ "TE.common": TreatmentMatrix;
27
+ "seTE.common": TreatmentMatrix;
28
+ "lower.common": TreatmentMatrix;
29
+ "upper.common": TreatmentMatrix;
30
+ "TE.random": TreatmentMatrix;
31
+ "seTE.random": TreatmentMatrix;
32
+ "lower.random": TreatmentMatrix;
33
+ "upper.random": TreatmentMatrix;
34
+ "lower.predict": TreatmentMatrix;
35
+ "upper.predict": TreatmentMatrix;
36
+ "TE.direct.common": TreatmentMatrix;
37
+ "seTE.direct.common": TreatmentMatrix;
38
+ "TE.direct.random": TreatmentMatrix;
39
+ "seTE.direct.random": TreatmentMatrix;
40
+ Q: number;
41
+ "df.Q": number;
42
+ "pval.Q": number;
43
+ tau2: number;
44
+ I2: number;
45
+ "lower.I2": number;
46
+ "upper.I2": number;
47
+ "Q.heterogeneity": number;
48
+ "df.Q.heterogeneity": number;
49
+ "Q.inconsistency": number;
50
+ "df.Q.inconsistency": number;
51
+ "w.common": number[];
52
+ "w.random": number[];
53
+ referenceGroup: string | null;
54
+ level: number;
55
+ }
56
+ /**
57
+ * Frequentist network meta-analysis (netmeta's `netmeta()`): contrasts TE (treat1 versus treat2) with standard errors
58
+ * seTE from studies studlab; the common effects by weighted least squares on the network graph (Ruecker 2012; multi-arm
59
+ * studies by the effective resistances of their comparisons), Q on sum(arms - 1) - (n - 1) df, tau2 by the DL
60
+ * generalisation of Jackson, White and Riley (2012), the random effects with seTE^2 + tau2, the direct estimates, the
61
+ * prediction interval (t on df.Q), and Q split into heterogeneity (within designs) and inconsistency (between).
62
+ * Equivalent to `netmeta::netmeta()` in R.
63
+ *
64
+ * @category Meta-analysis
65
+ */
66
+ export declare function netmeta(TE: readonly number[], seTE: readonly number[], treat1: readonly string[], treat2: readonly string[], studlab: readonly string[], options?: {
67
+ referenceGroup?: string;
68
+ level?: number;
69
+ levelPredict?: number;
70
+ tau2?: number;
71
+ }): NetmetaResult;
72
+ /**
73
+ * P-scores (netmeta's `netrank()`, Ruecker and Schwarzer 2015): for each treatment the mean over the others of the
74
+ * probability that it is better, pnorm((TE_j - TE_i) / se_ij) when small values are desirable (default) or
75
+ * pnorm((TE_i - TE_j) / se_ij) when undesirable.
76
+ * Equivalent to `netmeta::netrank()` in R.
77
+ *
78
+ * @category Meta-analysis
79
+ */
80
+ export declare function netrank(fit: NetmetaResult, options?: {
81
+ random?: boolean;
82
+ smallValues?: "desirable" | "undesirable";
83
+ }): Record<string, number>;
84
+ /**
85
+ * One column of {@link netsplit}.
86
+ *
87
+ * @category Meta-analysis
88
+ */
89
+ export interface NetsplitPart {
90
+ comparison: string[];
91
+ TE: number[];
92
+ seTE: number[];
93
+ lower: number[];
94
+ upper: number[];
95
+ z: number[];
96
+ p: number[];
97
+ }
98
+ /**
99
+ * Direct and indirect evidence for each comparison (netmeta's `netsplit()`, back-calculation): the direct estimate (the
100
+ * inverse-variance pooling of the comparison's contrasts), the indirect one from the network estimate and the direct
101
+ * one, their difference, and the direct evidence proportion v_network / v_direct.
102
+ * Equivalent to `netmeta::netsplit()` in R.
103
+ *
104
+ * @category Meta-analysis
105
+ */
106
+ export declare function netsplit(fit: NetmetaResult): Record<"direct.random" | "indirect.random" | "compare.random" | "direct.common" | "indirect.common" | "compare.common", NetsplitPart> & {
107
+ "prop.random": number[];
108
+ "prop.common": number[];
109
+ };
110
+ /**
111
+ * The connected components of a network (netmeta's `netconnection()`): the number of subnets, the numbers of
112
+ * studies k, treatments n and comparisons, and the distance matrix (shortest path lengths; Infinity between subnets).
113
+ * Equivalent to `netmeta::netconnection()` in R.
114
+ *
115
+ * @category Meta-analysis
116
+ */
117
+ export declare function netconnection(treat1: readonly string[], treat2: readonly string[], studlab?: readonly string[]): {
118
+ "n.subnets": number;
119
+ k: number;
120
+ n: number;
121
+ m: number;
122
+ D: TreatmentMatrix;
123
+ subnet: Record<string, number>;
124
+ };
125
+ /**
126
+ * The result of {@link decompDesign}.
127
+ *
128
+ * @category Meta-analysis
129
+ */
130
+ export interface DecompDesignResult {
131
+ /** Total, within designs, between designs. */
132
+ "Q.decomp": {
133
+ Q: number[];
134
+ df: number[];
135
+ pval: number[];
136
+ };
137
+ "Q.het.design": {
138
+ design: string[];
139
+ Q: number[];
140
+ df: number[];
141
+ pval: number[];
142
+ };
143
+ }
144
+ /**
145
+ * Q decomposed by design (netmeta's `decomp.design()`): the Q of each design = that of the network fitted to its
146
+ * studies alone, df (studies - 1)(arms - 1); within = their sum, between = total - within. Designs are listed by their
147
+ * number of arms, then alphabetically.
148
+ * Equivalent to `netmeta::decomp.design()` in R.
149
+ *
150
+ * @category Meta-analysis
151
+ */
152
+ export declare function decompDesign(fit: NetmetaResult): DecompDesignResult;
153
+ /**
154
+ * Network measures (netmeta's `netmeasures()`): the direct evidence proportion of each comparison, v_network / v_direct
155
+ * (0 without direct evidence), for the random-effects model (default, as netmeta when the fit has one) or the
156
+ * common-effect model. netmeta's mean path length and minimal parallelism are not provided (log 101: not reproduced).
157
+ * Equivalent to `netmeta::netmeasures()` in R.
158
+ *
159
+ * @category Meta-analysis
160
+ */
161
+ export declare function netmeasures(fit: NetmetaResult, options?: {
162
+ random?: boolean;
163
+ }): {
164
+ proportion: Record<string, number>;
165
+ };
166
+ /**
167
+ * Contrasts from arm-level data (netmeta's `pairwise()`): every pair of arms of each study (treat1 the earlier arm).
168
+ * Binary outcomes (`event`, `n`; sm "OR", "RR" or "RD"): all arms of a study get 0.5 when any arm has 0 or n events
169
+ * (log 74); continuous (`mean`, `sd`, `n`; sm "MD").
170
+ * Equivalent to `netmeta::pairwise()` in R.
171
+ *
172
+ * @category Meta-analysis
173
+ */
174
+ export declare function pairwise(data: {
175
+ treat: readonly string[];
176
+ studlab: readonly string[];
177
+ n: readonly number[];
178
+ event?: readonly number[];
179
+ mean?: readonly number[];
180
+ sd?: readonly number[];
181
+ }, options?: {
182
+ sm?: "OR" | "RR" | "RD" | "MD";
183
+ incr?: number;
184
+ }): {
185
+ TE: number[];
186
+ seTE: number[];
187
+ treat1: string[];
188
+ treat2: string[];
189
+ studlab: string[];
190
+ };
191
+ /**
192
+ * The result of {@link netcomb} / {@link discomb}.
193
+ *
194
+ * @category Meta-analysis
195
+ */
196
+ export interface NetcombResult {
197
+ comps: string[];
198
+ "Comp.common": number[];
199
+ "seComp.common": number[];
200
+ "Comp.random": number[];
201
+ "seComp.random": number[];
202
+ trts: string[];
203
+ "Comb.common": number[];
204
+ "seComb.common": number[];
205
+ "Comb.random": number[];
206
+ "seComb.random": number[];
207
+ "Q.additive": number;
208
+ "df.Q.additive": number;
209
+ tau2: number;
210
+ }
211
+ /**
212
+ * The additive component network meta-analysis (netmeta's `netcomb()` / `discomb()`, Ruecker et al. 2020; the network
213
+ * may be disconnected): treatments are "+"-joined components; the components' effects by weighted least squares on
214
+ * X = B C (C treatment by component, the `inactive` treatment left out), Q.additive the weighted residual sum of squares
215
+ * on comparisons - rank(X) df, tau2 by the DL generalisation with the additive hat matrix (two-arm studies; log 77).
216
+ * Equivalent to `netmeta::discomb()` in R.
217
+ *
218
+ * @category Meta-analysis
219
+ */
220
+ export declare function discomb(TE: readonly number[], seTE: readonly number[], treat1: readonly string[], treat2: readonly string[], studlab: readonly string[], options?: {
221
+ inactive?: string;
222
+ sep?: string;
223
+ }): NetcombResult;
224
+ /**
225
+ * {@link discomb} for a fitted network (netmeta's `netcomb()`). Equivalent to `netmeta::netcomb()` in R.
226
+ *
227
+ * @category Meta-analysis
228
+ */
229
+ export declare function netcomb(fit: NetmetaResult, options?: {
230
+ inactive?: string;
231
+ sep?: string;
232
+ }): NetcombResult;
233
+ /**
234
+ * The league table (netmeta's `netleague()`): the diagonal the treatments, the lower triangle [i][j] the network
235
+ * estimate of the column treatment against the row treatment (TE[j][i]) with its interval, the upper triangle the direct
236
+ * estimates (TE[i][j]; "." where there is none); "est (lower to upper)" with `digits` decimals, the bounds of a table
237
+ * padded to a common width.
238
+ * Equivalent to `netmeta::netleague()` in R.
239
+ *
240
+ * @category Meta-analysis
241
+ */
242
+ export declare function netleague(fit: NetmetaResult, options?: {
243
+ random?: boolean;
244
+ digits?: number;
245
+ bracket?: "(" | "[";
246
+ separator?: string;
247
+ }): string[][];
248
+ /**
249
+ * The partial order of treatments over several outcomes (netmeta's `netposet()`): P the treatments by outcomes matrix of
250
+ * ranking scores; M0 the covering relation (Hasse diagram) of "i at least as good as j in every outcome and better in
251
+ * one" (M0[i][j] = 1: i directly above j).
252
+ * Equivalent to `netmeta::netposet()` in R.
253
+ *
254
+ * @category Meta-analysis
255
+ */
256
+ export declare function netposet(P: readonly (readonly number[])[]): number[][];