@numero-uno-jpn/rstats 1.19.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 (135) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.ja.md +6 -6
  3. package/README.md +6 -6
  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-FAKOND5W.js → chunk-4EQN74JN.js} +2 -2
  8. package/dist/chunks/chunk-5RRFO4NU.js +2 -0
  9. package/dist/chunks/{chunk-ILQ4VWSD.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-OTETSONJ.js → chunk-6LAKI2QG.js} +2 -2
  13. package/dist/chunks/{chunk-SJJJ7Y4X.js → chunk-6QCH4LAL.js} +2 -2
  14. package/dist/chunks/{chunk-FCGTZ6BD.js → chunk-6RA73LXX.js} +2 -2
  15. package/dist/chunks/{chunk-MPBFXHXT.js → chunk-7C2ECGT6.js} +1 -1
  16. package/dist/chunks/{chunk-QH4GK3JN.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-WWRKCRTZ.js → chunk-B5GSH3AK.js} +1 -1
  20. package/dist/chunks/{chunk-XHHDBEHB.js → chunk-BLDLJHUG.js} +2 -2
  21. package/dist/chunks/{chunk-ANFN3X2M.js → chunk-BU4I544S.js} +2 -2
  22. package/dist/chunks/{chunk-LWFKGF43.js → chunk-CAUNJ7S6.js} +2 -2
  23. package/dist/chunks/chunk-DFGW4QWU.js +2 -0
  24. package/dist/chunks/{chunk-MOYWWJP4.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-VNLSP22M.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-7D7FBLWD.js → chunk-LZAB4M7P.js} +2 -2
  33. package/dist/chunks/chunk-LZTDYQRS.js +3 -0
  34. package/dist/chunks/{chunk-AS2XTRHH.js → chunk-MTSU2QHB.js} +2 -2
  35. package/dist/chunks/{chunk-5NW4WT4F.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-SAFH76DR.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-DXSVV47D.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-U6RXYALO.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.js +2 -2
  56. package/dist/entries/tests.d.ts +5 -0
  57. package/dist/entries/tests.js +5 -0
  58. package/dist/entries/timeseries.d.ts +11 -0
  59. package/dist/entries/timeseries.js +11 -0
  60. package/dist/linalg.js +2 -2
  61. package/dist/meta/metabin.d.ts +32 -0
  62. package/dist/meta/metabin.js +137 -0
  63. package/dist/meta/metaextra.d.ts +153 -0
  64. package/dist/meta/metaextra.js +290 -0
  65. package/dist/meta/metagen.d.ts +124 -0
  66. package/dist/meta/metagen.js +263 -0
  67. package/dist/meta/metaother.d.ts +87 -0
  68. package/dist/meta/metaother.js +342 -0
  69. package/dist/meta/netmeta.d.ts +256 -0
  70. package/dist/meta/netmeta.js +547 -0
  71. package/dist/mixed.js +3 -4
  72. package/dist/models/glmFamily.js +4 -1
  73. package/dist/models/metaAnalysis.d.ts +4 -0
  74. package/dist/models/metaAnalysis.js +100 -18
  75. package/dist/multivariate.js +2 -2
  76. package/dist/numerics.js +2 -2
  77. package/dist/regression.js +2 -2
  78. package/dist/rstats.js +20 -18
  79. package/dist/special.js +2 -2
  80. package/dist/tests.js +15 -15
  81. package/dist/timeseries/acf.js +5 -2
  82. package/dist/timeseries/arima.d.ts +154 -0
  83. package/dist/timeseries/arima.js +1007 -0
  84. package/dist/timeseries/autoArima.d.ts +70 -0
  85. package/dist/timeseries/autoArima.js +191 -0
  86. package/dist/timeseries/ets.d.ts +158 -0
  87. package/dist/timeseries/ets.js +883 -0
  88. package/dist/timeseries/forecastArima.d.ts +136 -0
  89. package/dist/timeseries/forecastArima.js +432 -0
  90. package/dist/timeseries/forecastDispatch.d.ts +83 -0
  91. package/dist/timeseries/forecastDispatch.js +130 -0
  92. package/dist/timeseries/forecastSimple.d.ts +177 -0
  93. package/dist/timeseries/forecastSimple.js +371 -0
  94. package/dist/timeseries/forecastUtils.d.ts +260 -0
  95. package/dist/timeseries/forecastUtils.js +619 -0
  96. package/dist/timeseries/holtWinters.d.ts +65 -0
  97. package/dist/timeseries/holtWinters.js +193 -0
  98. package/dist/timeseries/input.d.ts +8 -0
  99. package/dist/timeseries/input.js +27 -0
  100. package/dist/timeseries/ocsbCritical.d.ts +2 -0
  101. package/dist/timeseries/ocsbCritical.js +6 -0
  102. package/dist/timeseries/scale.d.ts +16 -0
  103. package/dist/timeseries/scale.js +48 -0
  104. package/dist/timeseries/statsTs.d.ts +128 -0
  105. package/dist/timeseries/statsTs.js +456 -0
  106. package/dist/timeseries/stl.d.ts +131 -0
  107. package/dist/timeseries/stl.js +489 -0
  108. package/dist/timeseries/tseries.d.ts +110 -0
  109. package/dist/timeseries/tseries.js +369 -0
  110. package/dist/timeseries.js +2 -2
  111. package/package.json +1 -1
  112. package/dist/chunks/chunk-2S45NNTL.js +0 -2
  113. package/dist/chunks/chunk-4JFGT4PN.js +0 -2
  114. package/dist/chunks/chunk-4JRJU5F5.js +0 -2
  115. package/dist/chunks/chunk-6ZGEYAFC.js +0 -2
  116. package/dist/chunks/chunk-BJ5WAL2C.js +0 -2
  117. package/dist/chunks/chunk-CEOSH3H2.js +0 -2
  118. package/dist/chunks/chunk-CMNKXRZF.js +0 -3
  119. package/dist/chunks/chunk-CNJLPH6W.js +0 -2
  120. package/dist/chunks/chunk-FELX2TZZ.js +0 -2
  121. package/dist/chunks/chunk-G4B2SQR6.js +0 -4
  122. package/dist/chunks/chunk-HPIZAWMT.js +0 -2
  123. package/dist/chunks/chunk-I5XUWUEV.js +0 -2
  124. package/dist/chunks/chunk-JLFRYDYO.js +0 -2
  125. package/dist/chunks/chunk-JXKHFSGK.js +0 -2
  126. package/dist/chunks/chunk-LBU73UMS.js +0 -2
  127. package/dist/chunks/chunk-LSZTDKIB.js +0 -2
  128. package/dist/chunks/chunk-M5J3DHXB.js +0 -2
  129. package/dist/chunks/chunk-NWNJEAKI.js +0 -2
  130. package/dist/chunks/chunk-QVE7V5UG.js +0 -2
  131. package/dist/chunks/chunk-RMGEIU4Z.js +0 -2
  132. package/dist/chunks/chunk-TVU4N6BA.js +0 -2
  133. package/dist/chunks/chunk-W4EITKLJ.js +0 -2
  134. package/dist/chunks/chunk-XJMBX6A2.js +0 -2
  135. package/dist/chunks/chunk-YZ6SBFFG.js +0 -2
@@ -0,0 +1,290 @@
1
+ // meta's subgroup analysis, metabias (Egger, Begg, Thompson, Harbord, Peters), trimfill, metareg, metainf, metacum
2
+ // and baujat. Written for rStats.js from the behaviour specification oracle/tsmeta-behaviour.md ("Subgroups, bias,
3
+ // trim-and-fill, regression, influence") with the details found in experiments/tsmeta120-log.md (items 68-70, 100).
4
+ // No R or package source was read.
5
+ import { pchisq } from "../distributions/chisq.js";
6
+ import { pnorm } from "../distributions/normal.js";
7
+ import { pt1 } from "../distributions/t.js";
8
+ import { lm } from "../models/lm.js";
9
+ import { extremeEffectScale, rescaleRma, rma } from "../models/metaAnalysis.js";
10
+ import { metaCore } from "./metagen.js";
11
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
12
+ import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
13
+ const META_SUBGROUP_KEYS = [
14
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "tauCommon",
15
+ ];
16
+ const METABIAS_KEYS = ["method", "counts"];
17
+ const METABIAS_COUNTS_KEYS = ["e1", "n1", "e2", "n2"];
18
+ const METAINF_KEYS = [
19
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "pooled",
20
+ ];
21
+ const METACUM_KEYS = [
22
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "pooled",
23
+ ];
24
+ const TRIMFILL_KEYS = [
25
+ "methodTau", "methodRandomCi", "methodTauCi", "level", "levelMa", "levelPredict", "side", "maxiter",
26
+ ];
27
+ // Generated by `node scripts/option-keys.mjs --write` (end)
28
+ const sum = (v) => v.reduce((a, b) => a + b, 0);
29
+ const finiteIdx = (m) => m.TE.map((t, i) => (Number.isFinite(t) && Number.isFinite(m.seTE[i]) && m.seTE[i] > 0 ? i : -1)).filter((i) => i >= 0);
30
+ /** Q of estimates about their inverse-variance mean. */
31
+ const qOf = (te, se) => {
32
+ const w = se.map((s) => 1 / (s * s));
33
+ const mu = sum(w.map((wi, i) => wi * te[i])) / sum(w);
34
+ return sum(w.map((wi, i) => wi * (te[i] - mu) ** 2));
35
+ };
36
+ /**
37
+ * Subgroup analysis (meta's `subgroup` argument): `refit(rows)` fits the same meta-analysis to the given study rows
38
+ * (for example `(r) => metabin(pick(e1, r), ...)`). Within each subgroup the common and random effects; Q.b = Q of the
39
+ * subgroup estimates with weights 1/se^2 (common and random), Q.w = the within-subgroup Q. With `tauCommon`, one tau2
40
+ * for all subgroups: that of the meta-regression on the subgroup (rma with the subgroup as a factor); the overall
41
+ * estimates stay those of `overall`.
42
+ * Not an R function. R: the `subgroup` argument of meta's functions.
43
+ *
44
+ * @category Meta-analysis
45
+ */
46
+ export function metaSubgroup(overall, subgroup, refit, options = {}) {
47
+ checkOptions(options, META_SUBGROUP_KEYS, "metaSubgroup"); // generated: option-keys.mjs
48
+ if (subgroup.length !== overall.TE.length)
49
+ throw new RangeError("metaSubgroup: one subgroup per study");
50
+ const ok = new Set(finiteIdx(overall));
51
+ const levels = [];
52
+ for (const [i, g] of subgroup.entries())
53
+ if (ok.has(i) && !levels.includes(String(g)))
54
+ levels.push(String(g));
55
+ let tau2Common;
56
+ if (options.tauCommon) {
57
+ const idx = [...ok].sort((a, b) => a - b);
58
+ const mods = idx.map((i) => levels.slice(1).map((l) => (String(subgroup[i]) === l ? 1 : 0)));
59
+ // TE and seTE divided by a power of two beyond 2^+-200 before squaring (log 110)
60
+ const c = extremeEffectScale(idx.map((i) => overall.TE[i]), idx.map((i) => overall.seTE[i]));
61
+ tau2Common = rma(idx.map((i) => overall.TE[i] / c), idx.map((i) => (overall.seTE[i] / c) ** 2), { method: (options.methodTau ?? "REML"), mods }).tau2 * c * c;
62
+ }
63
+ const fits = levels.map((l) => {
64
+ const f = refit(subgroup.map((g, i) => (String(g) === l && ok.has(i) ? i : -1)).filter((i) => i >= 0));
65
+ if (tau2Common === undefined)
66
+ return f;
67
+ // the random part with the common tau2
68
+ const j = finiteIdx(f);
69
+ const r = metaCore(j.map((i) => f.TE[i]), j.map((i) => f.seTE[i]), { ...options, fixedTau2: tau2Common });
70
+ return { ...f, "TE.random": r["TE.random"], "seTE.random": r["seTE.random"], tau2: tau2Common };
71
+ });
72
+ const teC = fits.map((f) => f["TE.common"]), seC = fits.map((f) => f["seTE.common"]), teR = fits.map((f) => f["TE.random"]), seR = fits.map((f) => f["seTE.random"]);
73
+ const qbC = qOf(teC, seC), qbR = qOf(teR, seR);
74
+ const df = levels.length - 1;
75
+ // the overall estimates are not affected by tauCommon (as meta)
76
+ const overallR = { tau2: overall.tau2, te: overall["TE.random"], se: overall["seTE.random"] };
77
+ return {
78
+ levels, "TE.common.w": teC, "seTE.common.w": seC, "TE.random.w": teR, "seTE.random.w": seR, "tau2.w": fits.map((f) => f.tau2), "Q.w": fits.map((f) => f.Q), "k.w": fits.map((f) => f.k),
79
+ "Q.w.common": sum(fits.map((f) => f.Q)), "Q.b.common": qbC, "Q.b.random": qbR, "df.Q.b": df, "df.Q.w": sum(fits.map((f) => f.k - 1)),
80
+ "pval.Q.b.common": pchisq(qbC, df, { lowerTail: false }), "pval.Q.b.random": pchisq(qbR, df, { lowerTail: false }),
81
+ tau2: overallR.tau2, "TE.random": overallR.te, "seTE.random": overallR.se,
82
+ ...(tau2Common !== undefined ? { "Q.w.random": sum(fits.map((f) => f.Q)) } : {}),
83
+ };
84
+ }
85
+ /** The t test of coefficient j of a weighted least squares fit. */
86
+ function lmT(X, y, j, weights) {
87
+ const f = lm(X, y, weights ? { weights } : {});
88
+ return { est: f.coefficients[j], se: f.coefTable.stdError[j], t: f.coefTable.tValue[j], df: f.dfResidual };
89
+ }
90
+ /**
91
+ * Tests for funnel plot asymmetry (meta's `metabias()`): "Egger" (default for continuous outcomes: the intercept of
92
+ * TE/seTE on 1/seTE, t on k - 2), "Begg" (Kendall's S of the standardised deviations against the variances, no
93
+ * continuity correction), "Thompson" (Thompson-Sharp: the REML meta-regression on seTE, its z with t(k - 2) p),
94
+ * "Harbord" (binary: lm(Z/sqrt(V) ~ sqrt(V)) with Z = O - E and V the hypergeometric variance) and "Peters" (binary:
95
+ * lm(TE ~ 1/N) with weights 1/(1/(a + c) + 1/(b + d))). Harbord and Peters need the 2 x 2 counts (`counts`).
96
+ * Equivalent to `meta::metabias()` in R.
97
+ *
98
+ * @category Meta-analysis
99
+ */
100
+ export function metabias(m, options = {}) {
101
+ checkOptions(options, METABIAS_KEYS, "metabias"); // generated: option-keys.mjs
102
+ checkOptions(nestedOptions(options, "counts"), METABIAS_COUNTS_KEYS, "metabias", "counts"); // generated: option-keys.mjs
103
+ const method = options.method ?? "Egger";
104
+ const idx = finiteIdx(m);
105
+ const k = idx.length;
106
+ if (k < 3)
107
+ throw new RangeError("metabias: at least three studies");
108
+ // TE and seTE divided by a power of two beyond 2^+-200 (the statistics do not depend on it; log 110)
109
+ const cs = extremeEffectScale(idx.map((i) => m.TE[i]), idx.map((i) => m.seTE[i]));
110
+ const te = idx.map((i) => m.TE[i] / cs), se = idx.map((i) => m.seTE[i] / cs);
111
+ if (method === "Egger") {
112
+ const r = lmT(se.map((s) => [1 / s]), te.map((t, j) => t / se[j]), 0);
113
+ return { statistic: r.t, df: r.df, pValue: 2 * pt1(-Math.abs(r.t), r.df, true, false), estimate: [r.est, r.se], method: "Linear regression test of funnel plot asymmetry" };
114
+ }
115
+ if (method === "Begg") {
116
+ const wC = se.map((s) => 1 / (s * s));
117
+ const teC = sum(wC.map((w, j) => w * te[j])) / sum(wC);
118
+ const vC = 1 / sum(wC);
119
+ const z = te.map((t, j) => (t - teC) / Math.sqrt(se[j] ** 2 - vC));
120
+ const v = se.map((s) => s * s);
121
+ let S = 0;
122
+ for (let i = 0; i < k; i++)
123
+ for (let j = i + 1; j < k; j++)
124
+ S += Math.sign(z[i] - z[j]) * Math.sign(v[i] - v[j]);
125
+ const sd = Math.sqrt((k * (k - 1) * (2 * k + 5)) / 18);
126
+ const st = S / sd;
127
+ return { statistic: st, df: null, pValue: 2 * pnorm(-Math.abs(st)), estimate: [S, sd], method: "Rank correlation test of funnel plot asymmetry" };
128
+ }
129
+ if (method === "Thompson") {
130
+ const f = rma(te, se.map((s) => s * s), { method: "REML", mods: se.map((s) => [s]) });
131
+ const b = f.beta.rows[1][0];
132
+ const sb = f.se[1];
133
+ const zv = b / sb;
134
+ return { statistic: zv, df: k - 2, pValue: 2 * pt1(-Math.abs(zv), k - 2, true, false), estimate: [b, sb], method: "Linear regression test of funnel plot asymmetry (methods of moment)" };
135
+ }
136
+ const c = options.counts;
137
+ if (!c)
138
+ throw new RangeError(`metabias: method '${method}' needs the 2 x 2 counts`);
139
+ const a = idx.map((i) => c.e1[i]), b = idx.map((i) => c.n1[i] - c.e1[i]), cc = idx.map((i) => c.e2[i]), d = idx.map((i) => c.n2[i] - c.e2[i]);
140
+ if (method === "Harbord") {
141
+ const N = a.map((x, j) => x + b[j] + cc[j] + d[j]);
142
+ const n1 = a.map((x, j) => x + b[j]), m1 = a.map((x, j) => x + cc[j]);
143
+ const Z = a.map((x, j) => x - (n1[j] * m1[j]) / N[j]);
144
+ const V = N.map((n, j) => (n1[j] * (n - n1[j]) * m1[j] * (n - m1[j])) / (n * n * (n - 1)));
145
+ const r = lmT(V.map((v) => [Math.sqrt(v)]), Z.map((z, j) => z / Math.sqrt(V[j])), 0);
146
+ return { statistic: r.t, df: r.df, pValue: 2 * pt1(-Math.abs(r.t), r.df, true, false), estimate: [r.est, r.se], method: "Linear regression test of funnel plot asymmetry (efficient score)" };
147
+ }
148
+ const N = a.map((x, j) => x + b[j] + cc[j] + d[j]);
149
+ const w = a.map((x, j) => 1 / (1 / (x + cc[j]) + 1 / (b[j] + d[j])));
150
+ const r = lmT(N.map((n) => [1 / n]), te, 1, w);
151
+ return { statistic: r.t, df: r.df, pValue: 2 * pt1(-Math.abs(r.t), r.df, true, false), estimate: [r.est, r.se], method: "Linear regression test of funnel plot asymmetry (based on sample size)" };
152
+ }
153
+ /**
154
+ * Meta-regression (meta's `metareg()`): rma(TE, seTE^2, mods, method = the meta-analysis's tau2 method) on the studies
155
+ * with finite values. `mods` has one row per study (all studies, as given to the meta function).
156
+ * Equivalent to `meta::metareg()` in R.
157
+ *
158
+ * @category Meta-analysis
159
+ */
160
+ export function metareg(m, mods) {
161
+ const idx = finiteIdx(m);
162
+ // TE and seTE divided by a power of two beyond 2^+-200 before squaring, the fit mapped back (log 110)
163
+ const te = idx.map((i) => m.TE[i]), se = idx.map((i) => m.seTE[i]);
164
+ const c = extremeEffectScale(te, se);
165
+ const r = rma(te.map((t) => t / c), se.map((s) => (s / c) ** 2), { method: m.methodTau, mods: idx.map((i) => [...mods[i]]), ...(m.methodRandomCi === "HK" ? { test: "knha" } : {}) });
166
+ return rescaleRma(r, c, te, se.map((s) => s * s));
167
+ }
168
+ function sequenceOf(m, subsets, pooled, options) {
169
+ const out = { TE: [], seTE: [], lower: [], upper: [], pval: [], tau2: [], I2: [] };
170
+ const opt = { methodTau: m.methodTau, methodRandomCi: m.methodRandomCi, ...options };
171
+ for (const rows of subsets) {
172
+ const f = metaCore(rows.map((i) => m.TE[i]), rows.map((i) => m.seTE[i]), opt);
173
+ const p = pooled === "common" ? "common" : "random";
174
+ out.TE.push(f[`TE.${p}`]);
175
+ out.seTE.push(f[`seTE.${p}`]);
176
+ out.lower.push(f[`lower.${p}`]);
177
+ out.upper.push(f[`upper.${p}`]);
178
+ out.pval.push(f[`pval.${p}`]);
179
+ out.tau2.push(rows.length > 1 ? f.tau2 : NaN);
180
+ out.I2.push(f.I2);
181
+ }
182
+ return out;
183
+ }
184
+ /**
185
+ * Leave-one-out meta-analysis (meta's `metainf()`): the pooled estimate without each study in turn; `pooled` "common"
186
+ * (default) or "random".
187
+ * Equivalent to `meta::metainf()` in R.
188
+ *
189
+ * @category Meta-analysis
190
+ */
191
+ export function metainf(m, options = {}) {
192
+ checkOptions(options, METAINF_KEYS, "metainf"); // generated: option-keys.mjs
193
+ const idx = finiteIdx(m);
194
+ return sequenceOf(m, idx.map((i) => idx.filter((j) => j !== i)), options.pooled ?? "common", options);
195
+ }
196
+ /**
197
+ * Cumulative meta-analysis (meta's `metacum()`): the pooled estimate of the first 1, 2, ... studies; `pooled` "common"
198
+ * (default) or "random".
199
+ * Equivalent to `meta::metacum()` in R.
200
+ *
201
+ * @category Meta-analysis
202
+ */
203
+ export function metacum(m, options = {}) {
204
+ checkOptions(options, METACUM_KEYS, "metacum"); // generated: option-keys.mjs
205
+ const idx = finiteIdx(m);
206
+ return sequenceOf(m, idx.map((_, j) => idx.slice(0, j + 1)), options.pooled ?? "common", options);
207
+ }
208
+ /**
209
+ * The coordinates of Baujat's plot (meta's `baujat()`): x = each study's contribution to Q, w (TE - TE.common)^2;
210
+ * y = its influence on the common estimate, (TE.common - TE.common without it)^2 / seTE.common without it^2.
211
+ * Equivalent to `meta::baujat()` in R.
212
+ *
213
+ * @category Meta-analysis
214
+ */
215
+ export function baujat(m) {
216
+ // TE and seTE divided by a power of two beyond 2^+-200 (x and y do not depend on it; log 110)
217
+ const idx = finiteIdx(m);
218
+ const x = [], y = [];
219
+ const c = extremeEffectScale(idx.map((i) => m.TE[i]), idx.map((i) => m.seTE[i]));
220
+ const T = m.TE.map((t) => t / c), S = m.seTE.map((s) => s / c), TC = m["TE.common"] / c;
221
+ for (const i of idx) {
222
+ x.push((T[i] - TC) ** 2 / S[i] ** 2);
223
+ const rest = idx.filter((j) => j !== i);
224
+ const w = rest.map((j) => 1 / S[j] ** 2);
225
+ const te = sum(w.map((wj, t) => wj * T[rest[t]])) / sum(w);
226
+ y.push((TC - te) ** 2 * sum(w));
227
+ }
228
+ return { x, y };
229
+ }
230
+ /**
231
+ * Duval and Tweedie's trim-and-fill (meta's `trimfill()`): the L0 estimator on the common-effect model, iterated
232
+ * (trim the k0 most extreme studies on one side, re-estimate, re-rank), the side opposite to the asymmetry (the sign of
233
+ * the Egger-type regression of TE on seTE); the k0 mirrored studies 2 theta - TE added, then the meta-analysis of the
234
+ * augmented data (random effect with the given tau2 method, default REML).
235
+ * Equivalent to `meta::trimfill()` in R.
236
+ *
237
+ * @category Meta-analysis
238
+ */
239
+ export function trimfill(m, options = {}) {
240
+ checkOptions(options, TRIMFILL_KEYS, "trimfill"); // generated: option-keys.mjs
241
+ // TE and seTE divided by a power of two beyond 2^+-200 for the trimming (k0 does not depend on it), the filled
242
+ // studies mapped back (log 110)
243
+ const idx = finiteIdx(m);
244
+ const cs = extremeEffectScale(idx.map((i) => m.TE[i]), idx.map((i) => m.seTE[i]));
245
+ const te0 = idx.map((i) => m.TE[i] / cs), se0 = idx.map((i) => m.seTE[i] / cs);
246
+ const k = idx.length;
247
+ let side = options.side;
248
+ if (!side) {
249
+ const f = rma(te0, se0.map((s) => s * s), { method: "FE", mods: se0.map((s) => [s]) });
250
+ // the missing studies are on the side opposite to the asymmetry (log 100)
251
+ side = f.beta.rows[1][0] < 0 ? "left" : "right";
252
+ }
253
+ // work on the side to trim as the right side: mirror the values for "left"
254
+ const sgn = side === "right" ? 1 : -1;
255
+ const y = te0.map((t) => sgn * t);
256
+ const order = y.map((_, i) => i).sort((a, b) => y[a] - y[b]);
257
+ const ys = order.map((i) => y[i]), ss = order.map((i) => se0[i]);
258
+ const fe = (n) => { const w = ss.slice(0, n).map((s) => 1 / (s * s)); return sum(w.map((wi, j) => wi * ys[j])) / sum(w); };
259
+ let k0 = 0, prev = -1, theta = fe(k);
260
+ const maxiter = options.maxiter ?? 100;
261
+ for (let it = 0; it < maxiter && k0 !== prev; it++) {
262
+ prev = k0;
263
+ theta = fe(k - k0);
264
+ const dev = ys.map((v) => v - theta);
265
+ const absRank = rankAvg(dev.map(Math.abs));
266
+ const Tn = sum(dev.map((d, j) => (d > 0 ? absRank[j] : 0)));
267
+ const L0 = (4 * Tn - k * (k + 1)) / (2 * k - 1);
268
+ k0 = Math.max(0, Math.round(L0));
269
+ }
270
+ theta = fe(k - k0);
271
+ const filledY = ys.slice(k - k0).map((v) => 2 * theta - v);
272
+ const filledS = ss.slice(k - k0);
273
+ const TE = [...te0, ...filledY.map((v) => sgn * v)].map((v) => v * cs), seTE = [...se0, ...filledS].map((v) => v * cs);
274
+ const meta = metaCore(TE, seTE, { methodTau: options.methodTau ?? m.methodTau, ...options });
275
+ return { k0, side, TE, seTE, filled: TE.map((_, i) => i >= k), meta };
276
+ }
277
+ /** Ranks with ties averaged. */
278
+ function rankAvg(v) {
279
+ const o = v.map((_, i) => i).sort((a, b) => v[a] - v[b]);
280
+ const r = new Array(v.length);
281
+ for (let i = 0; i < o.length;) {
282
+ let j = i;
283
+ while (j + 1 < o.length && v[o[j + 1]] === v[o[i]])
284
+ j++;
285
+ for (let t = i; t <= j; t++)
286
+ r[o[t]] = (i + j) / 2 + 1;
287
+ i = j + 1;
288
+ }
289
+ return r;
290
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * The between-study variance estimators of meta (`method.tau`).
3
+ *
4
+ * @category Meta-analysis
5
+ */
6
+ export type MetaTauMethod = "REML" | "DL" | "PM" | "ML" | "SJ" | "HE" | "EB" | "HS";
7
+ /**
8
+ * Options shared by the meta functions.
9
+ *
10
+ * @category Meta-analysis
11
+ */
12
+ export interface MetaCommonOptions {
13
+ /** The between-study variance estimator (default "REML"). */
14
+ methodTau?: MetaTauMethod;
15
+ /** "classic" (normal, default) or "HK" (Hartung-Knapp, t on k - 1). */
16
+ methodRandomCi?: "classic" | "HK";
17
+ /** The tau2 interval: "QP" (Q-profile), "J" (Jackson) or "BJ" (Biggerstaff-Jackson); default "J" for DL, else "QP". */
18
+ methodTauCi?: "QP" | "J" | "BJ";
19
+ /** Confidence level of the study and pooled intervals (default 0.95). */
20
+ level?: number;
21
+ /** Confidence level of the pooled intervals (default `level`). */
22
+ levelMa?: number;
23
+ /** Level of the prediction interval (default `level`). */
24
+ levelPredict?: number;
25
+ }
26
+ /**
27
+ * The result of {@link metagen} (R's element names).
28
+ *
29
+ * @category Meta-analysis
30
+ */
31
+ export interface MetaResult {
32
+ TE: number[];
33
+ seTE: number[];
34
+ k: number;
35
+ "TE.common": number;
36
+ "seTE.common": number;
37
+ "lower.common": number;
38
+ "upper.common": number;
39
+ "statistic.common": number;
40
+ "pval.common": number;
41
+ "TE.random": number;
42
+ "seTE.random": number;
43
+ "lower.random": number;
44
+ "upper.random": number;
45
+ "statistic.random": number;
46
+ "pval.random": number;
47
+ /** Degrees of freedom of the random interval (k - 1 with Hartung-Knapp, else null). */
48
+ "df.random": number | null;
49
+ "seTE.predict": number;
50
+ "lower.predict": number;
51
+ "upper.predict": number;
52
+ "df.predict": number;
53
+ Q: number;
54
+ "df.Q": number;
55
+ "pval.Q": number;
56
+ tau2: number;
57
+ "lower.tau2": number;
58
+ "upper.tau2": number;
59
+ tau: number;
60
+ "lower.tau": number;
61
+ "upper.tau": number;
62
+ H: number;
63
+ "lower.H": number;
64
+ "upper.H": number;
65
+ I2: number;
66
+ "lower.I2": number;
67
+ "upper.I2": number;
68
+ "w.common": number[];
69
+ "w.random": number[];
70
+ methodTau: MetaTauMethod;
71
+ methodRandomCi: "classic" | "HK";
72
+ methodTauCi: "QP" | "J" | "BJ";
73
+ }
74
+ /**
75
+ * P(sum_j lambda_j chi2_1 <= q) for lambda_j >= 0: Ruben's (1962) series sum_k c_k P(chi2_{r + 2k} <= q / beta) with
76
+ * beta = 2 lmin lmax / (lmin + lmax), or Imhof's (1961) integral when the series would be long.
77
+ * Not an R function.
78
+ *
79
+ * @category Meta-analysis
80
+ */
81
+ export declare function pWeightedChisq(q0: number, lambda0: readonly number[]): number;
82
+ /**
83
+ * The generalised Q interval for tau2 (Jackson 2013) with study weights a: Q_a = sum a (y - mu_a)^2 follows
84
+ * sum lambda chi2_1 with lambda the eigenvalues of S^(1/2) (A - a a' / sum a) S^(1/2), S = diag(v + tau2); the bounds
85
+ * solve P(Q_a <= q_obs) = 1 - alpha/2 and alpha/2 (0 when not reached).
86
+ * Not an R function.
87
+ *
88
+ * @category Meta-analysis
89
+ */
90
+ export declare function genqInterval(y: readonly number[], v: readonly number[], a: readonly number[], level: number): [number, number];
91
+ /**
92
+ * H and I2 with Higgins and Thompson's (2002) intervals (H at least 1, as meta). Not an R function.
93
+ *
94
+ * @category Meta-analysis
95
+ */
96
+ export declare function heterogeneityIntervals(Q: number, k: number, level: number): {
97
+ H: number;
98
+ lH: number;
99
+ uH: number;
100
+ I2: number;
101
+ lI2: number;
102
+ uI2: number;
103
+ };
104
+ /** The pooled estimates and heterogeneity of study estimates TE with standard errors seTE (inverse variance). */
105
+ /**
106
+ * Internal: a tau2 and a Q fixed by the caller (metabin's Q about the MH estimate with DL, log 64).
107
+ *
108
+ * @category Meta-analysis
109
+ */
110
+ export interface MetaCoreInternal {
111
+ fixedTau2?: number;
112
+ fixedQ?: number;
113
+ }
114
+ export declare function metaCore(TE: readonly number[], seTE: readonly number[], options?: MetaCommonOptions & MetaCoreInternal): MetaResult;
115
+ /**
116
+ * Generic inverse-variance meta-analysis (meta's `metagen()`): study estimates TE with standard errors seTE; the
117
+ * common effect, the random effect with tau2 by `methodTau` (default REML) and a classic or Hartung-Knapp interval,
118
+ * the prediction interval (t on k - 1), Q, tau2's interval (QP, or J for DL), H and I2 with Higgins and Thompson's
119
+ * intervals.
120
+ * Equivalent to `meta::metagen()` in R.
121
+ *
122
+ * @category Meta-analysis
123
+ */
124
+ export declare function metagen(TE: readonly number[], seTE: readonly number[], options?: MetaCommonOptions): MetaResult;