@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.
- package/CHANGELOG.md +38 -0
- package/README.ja.md +6 -6
- package/README.md +6 -6
- package/dist/causal.js +2 -2
- package/dist/chunks/chunk-33ODAPMR.js +2 -0
- package/dist/chunks/chunk-3ST6RZXG.js +2 -0
- package/dist/chunks/{chunk-FAKOND5W.js → chunk-4EQN74JN.js} +2 -2
- package/dist/chunks/chunk-5RRFO4NU.js +2 -0
- package/dist/chunks/{chunk-ILQ4VWSD.js → chunk-64DDDGJU.js} +2 -2
- package/dist/chunks/chunk-66TEOHPF.js +2 -0
- package/dist/chunks/chunk-6ISDROPG.js +2 -0
- package/dist/chunks/{chunk-OTETSONJ.js → chunk-6LAKI2QG.js} +2 -2
- package/dist/chunks/{chunk-SJJJ7Y4X.js → chunk-6QCH4LAL.js} +2 -2
- package/dist/chunks/{chunk-FCGTZ6BD.js → chunk-6RA73LXX.js} +2 -2
- package/dist/chunks/{chunk-MPBFXHXT.js → chunk-7C2ECGT6.js} +1 -1
- package/dist/chunks/{chunk-QH4GK3JN.js → chunk-7KPFNJBE.js} +2 -2
- package/dist/chunks/chunk-ARPXG2PG.js +2 -0
- package/dist/chunks/chunk-AWXW4WSX.js +2 -0
- package/dist/chunks/{chunk-WWRKCRTZ.js → chunk-B5GSH3AK.js} +1 -1
- package/dist/chunks/{chunk-XHHDBEHB.js → chunk-BLDLJHUG.js} +2 -2
- package/dist/chunks/{chunk-ANFN3X2M.js → chunk-BU4I544S.js} +2 -2
- package/dist/chunks/{chunk-LWFKGF43.js → chunk-CAUNJ7S6.js} +2 -2
- package/dist/chunks/chunk-DFGW4QWU.js +2 -0
- package/dist/chunks/{chunk-MOYWWJP4.js → chunk-DPCCSFCG.js} +1 -1
- package/dist/chunks/chunk-EXS5QD7V.js +2 -0
- package/dist/chunks/chunk-FOCHNNP3.js +2 -0
- package/dist/chunks/{chunk-VNLSP22M.js → chunk-HEPGGURG.js} +2 -2
- package/dist/chunks/chunk-INNFAVT3.js +2 -0
- package/dist/chunks/chunk-JEELIABM.js +2 -0
- package/dist/chunks/chunk-KNF2GPJV.js +2 -0
- package/dist/chunks/chunk-L4ZS7MAX.js +2 -0
- package/dist/chunks/{chunk-7D7FBLWD.js → chunk-LZAB4M7P.js} +2 -2
- package/dist/chunks/chunk-LZTDYQRS.js +3 -0
- package/dist/chunks/{chunk-AS2XTRHH.js → chunk-MTSU2QHB.js} +2 -2
- package/dist/chunks/{chunk-5NW4WT4F.js → chunk-MXRN2G7O.js} +2 -2
- package/dist/chunks/chunk-OFDD4GTZ.js +2 -0
- package/dist/chunks/chunk-OOHFFLMY.js +2 -0
- package/dist/chunks/chunk-OOO5AAM7.js +2 -0
- package/dist/chunks/chunk-PJJJR3H6.js +2 -0
- package/dist/chunks/chunk-PVRNAOO7.js +2 -0
- package/dist/chunks/chunk-Q2JLU5YD.js +2 -0
- package/dist/chunks/{chunk-SAFH76DR.js → chunk-QCQBK5AC.js} +2 -2
- package/dist/chunks/chunk-RXK4KSKH.js +2 -0
- package/dist/chunks/chunk-UC3YYJHM.js +4 -0
- package/dist/chunks/chunk-UPACHZ4F.js +2 -0
- package/dist/chunks/{chunk-DXSVV47D.js → chunk-VCXPGCA4.js} +2 -2
- package/dist/chunks/chunk-VFL4GGYT.js +2 -0
- package/dist/chunks/chunk-VL72MQOQ.js +2 -0
- package/dist/chunks/chunk-VSV5QGXX.js +2 -0
- package/dist/chunks/{chunk-U6RXYALO.js → chunk-WMEF5XWO.js} +2 -2
- package/dist/chunks/chunk-X6JS6ZZB.js +2 -0
- package/dist/chunks/chunk-YEDVOX5V.js +2 -0
- package/dist/chunks/chunk-ZCTB4N5J.js +3 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +5 -0
- package/dist/entries/tests.js +5 -0
- package/dist/entries/timeseries.d.ts +11 -0
- package/dist/entries/timeseries.js +11 -0
- package/dist/linalg.js +2 -2
- package/dist/meta/metabin.d.ts +32 -0
- package/dist/meta/metabin.js +137 -0
- package/dist/meta/metaextra.d.ts +153 -0
- package/dist/meta/metaextra.js +290 -0
- package/dist/meta/metagen.d.ts +124 -0
- package/dist/meta/metagen.js +263 -0
- package/dist/meta/metaother.d.ts +87 -0
- package/dist/meta/metaother.js +342 -0
- package/dist/meta/netmeta.d.ts +256 -0
- package/dist/meta/netmeta.js +547 -0
- package/dist/mixed.js +3 -4
- package/dist/models/glmFamily.js +4 -1
- package/dist/models/metaAnalysis.d.ts +4 -0
- package/dist/models/metaAnalysis.js +100 -18
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +20 -18
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries/acf.js +5 -2
- package/dist/timeseries/arima.d.ts +154 -0
- package/dist/timeseries/arima.js +1007 -0
- package/dist/timeseries/autoArima.d.ts +70 -0
- package/dist/timeseries/autoArima.js +191 -0
- package/dist/timeseries/ets.d.ts +158 -0
- package/dist/timeseries/ets.js +883 -0
- package/dist/timeseries/forecastArima.d.ts +136 -0
- package/dist/timeseries/forecastArima.js +432 -0
- package/dist/timeseries/forecastDispatch.d.ts +83 -0
- package/dist/timeseries/forecastDispatch.js +130 -0
- package/dist/timeseries/forecastSimple.d.ts +177 -0
- package/dist/timeseries/forecastSimple.js +371 -0
- package/dist/timeseries/forecastUtils.d.ts +260 -0
- package/dist/timeseries/forecastUtils.js +619 -0
- package/dist/timeseries/holtWinters.d.ts +65 -0
- package/dist/timeseries/holtWinters.js +193 -0
- package/dist/timeseries/input.d.ts +8 -0
- package/dist/timeseries/input.js +27 -0
- package/dist/timeseries/ocsbCritical.d.ts +2 -0
- package/dist/timeseries/ocsbCritical.js +6 -0
- package/dist/timeseries/scale.d.ts +16 -0
- package/dist/timeseries/scale.js +48 -0
- package/dist/timeseries/statsTs.d.ts +128 -0
- package/dist/timeseries/statsTs.js +456 -0
- package/dist/timeseries/stl.d.ts +131 -0
- package/dist/timeseries/stl.js +489 -0
- package/dist/timeseries/tseries.d.ts +110 -0
- package/dist/timeseries/tseries.js +369 -0
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-2S45NNTL.js +0 -2
- package/dist/chunks/chunk-4JFGT4PN.js +0 -2
- package/dist/chunks/chunk-4JRJU5F5.js +0 -2
- package/dist/chunks/chunk-6ZGEYAFC.js +0 -2
- package/dist/chunks/chunk-BJ5WAL2C.js +0 -2
- package/dist/chunks/chunk-CEOSH3H2.js +0 -2
- package/dist/chunks/chunk-CMNKXRZF.js +0 -3
- package/dist/chunks/chunk-CNJLPH6W.js +0 -2
- package/dist/chunks/chunk-FELX2TZZ.js +0 -2
- package/dist/chunks/chunk-G4B2SQR6.js +0 -4
- package/dist/chunks/chunk-HPIZAWMT.js +0 -2
- package/dist/chunks/chunk-I5XUWUEV.js +0 -2
- package/dist/chunks/chunk-JLFRYDYO.js +0 -2
- package/dist/chunks/chunk-JXKHFSGK.js +0 -2
- package/dist/chunks/chunk-LBU73UMS.js +0 -2
- package/dist/chunks/chunk-LSZTDKIB.js +0 -2
- package/dist/chunks/chunk-M5J3DHXB.js +0 -2
- package/dist/chunks/chunk-NWNJEAKI.js +0 -2
- package/dist/chunks/chunk-QVE7V5UG.js +0 -2
- package/dist/chunks/chunk-RMGEIU4Z.js +0 -2
- package/dist/chunks/chunk-TVU4N6BA.js +0 -2
- package/dist/chunks/chunk-W4EITKLJ.js +0 -2
- package/dist/chunks/chunk-XJMBX6A2.js +0 -2
- package/dist/chunks/chunk-YZ6SBFFG.js +0 -2
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
// netmeta's frequentist network meta-analysis: netmeta (common and random effects by graph theory), netrank
|
|
2
|
+
// (P-scores), netsplit, decomp.design, netmeasures' proportion, netconnection, pairwise, netcomb / discomb (additive
|
|
3
|
+
// component models), netleague and netposet. Written for rStats.js from the behaviour specification
|
|
4
|
+
// oracle/tsmeta-behaviour.md ("netmeta") with the details found in experiments/tsmeta120-log.md (items 71-74, 77, 101).
|
|
5
|
+
// The methods are those of Ruecker (2012), Jackson, White and Riley (2012), Ruecker and Schwarzer (2015) and Ruecker et
|
|
6
|
+
// al. (2020). No R or package source was read.
|
|
7
|
+
import { extremeEffectScale } from "../models/metaAnalysis.js";
|
|
8
|
+
import { pchisq } from "../distributions/chisq.js";
|
|
9
|
+
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
10
|
+
import { qt1 } from "../distributions/t.js";
|
|
11
|
+
import { symmetricEigen } from "../random/mvrnorm.js";
|
|
12
|
+
import { heterogeneityIntervals } from "./metagen.js";
|
|
13
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
14
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
15
|
+
const NETMETA_KEYS = ["referenceGroup", "level", "levelPredict", "tau2"];
|
|
16
|
+
const NETRANK_KEYS = ["random", "smallValues"];
|
|
17
|
+
const NETMEASURES_KEYS = ["random"];
|
|
18
|
+
const PAIRWISE_KEYS_0 = ["treat", "studlab", "n", "event", "mean", "sd"];
|
|
19
|
+
const PAIRWISE_KEYS_1 = ["sm", "incr"];
|
|
20
|
+
const DISCOMB_KEYS = ["inactive", "sep"];
|
|
21
|
+
const NETCOMB_KEYS = ["inactive", "sep"];
|
|
22
|
+
const NETLEAGUE_KEYS = ["random", "digits", "bracket", "separator"];
|
|
23
|
+
const zeros = (r, c) => Array.from({ length: r }, () => new Array(c).fill(0));
|
|
24
|
+
const mul = (A, B) => A.map((row) => B[0].map((_, j) => row.reduce((s, a, k) => s + a * B[k][j], 0)));
|
|
25
|
+
const tr = (A) => A[0].map((_, j) => A.map((r) => r[j]));
|
|
26
|
+
const sum = (v) => v.reduce((a, b) => a + b, 0);
|
|
27
|
+
/** The Moore-Penrose pseudoinverse of a symmetric matrix (eigenvalues below 1e-10 of the largest dropped). */
|
|
28
|
+
function pinvSym(A) {
|
|
29
|
+
const n = A.length;
|
|
30
|
+
const S = A.map((r, i) => r.map((v, j) => (v + A[j][i]) / 2));
|
|
31
|
+
const { values, vectors } = symmetricEigen(S);
|
|
32
|
+
const mx = Math.max(...values.map(Math.abs));
|
|
33
|
+
const out = zeros(n, n);
|
|
34
|
+
values.forEach((l, k) => { if (Math.abs(l) > 1e-10 * mx)
|
|
35
|
+
for (let i = 0; i < n; i++)
|
|
36
|
+
for (let j = 0; j < n; j++)
|
|
37
|
+
out[i][j] += (vectors[i][k] * vectors[j][k]) / l; });
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
/** The weights of the contrasts: 1/v for two-arm studies, -L_s[i, j] with L_s = pinv(-P V P / 2) for multi-arm ones. */
|
|
41
|
+
function contrastWeights(v, t1, t2, stud) {
|
|
42
|
+
const w = new Array(v.length).fill(0);
|
|
43
|
+
for (const s of [...new Set(stud)]) {
|
|
44
|
+
const idx = stud.map((x, i) => (x === s ? i : -1)).filter((i) => i >= 0);
|
|
45
|
+
if (idx.length === 1) {
|
|
46
|
+
w[idx[0]] = 1 / v[idx[0]];
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const arms = [...new Set(idx.flatMap((i) => [t1[i], t2[i]]))];
|
|
50
|
+
const k = arms.length;
|
|
51
|
+
const V = zeros(k, k);
|
|
52
|
+
for (const i of idx) {
|
|
53
|
+
const a = arms.indexOf(t1[i]), b = arms.indexOf(t2[i]);
|
|
54
|
+
V[a][b] = v[i];
|
|
55
|
+
V[b][a] = v[i];
|
|
56
|
+
}
|
|
57
|
+
const P = Array.from({ length: k }, (_, i) => Array.from({ length: k }, (_, j) => (i === j ? 1 : 0) - 1 / k));
|
|
58
|
+
const Ls = pinvSym(mul(mul(P, V), P).map((r) => r.map((x) => -x / 2)));
|
|
59
|
+
for (const i of idx)
|
|
60
|
+
w[i] = -Ls[arms.indexOf(t1[i])][arms.indexOf(t2[i])];
|
|
61
|
+
}
|
|
62
|
+
return w;
|
|
63
|
+
}
|
|
64
|
+
function fitNetwork(TE, v, t1, t2, stud, trts) {
|
|
65
|
+
const m = TE.length, n = trts.length;
|
|
66
|
+
const B = zeros(m, n);
|
|
67
|
+
for (let i = 0; i < m; i++) {
|
|
68
|
+
B[i][trts.indexOf(t1[i])] = 1;
|
|
69
|
+
B[i][trts.indexOf(t2[i])] = -1;
|
|
70
|
+
}
|
|
71
|
+
const w = contrastWeights(v, t1, t2, stud);
|
|
72
|
+
const L = zeros(n, n);
|
|
73
|
+
for (let i = 0; i < m; i++)
|
|
74
|
+
for (let a = 0; a < n; a++)
|
|
75
|
+
for (let b = 0; b < n; b++)
|
|
76
|
+
L[a][b] += B[i][a] * w[i] * B[i][b];
|
|
77
|
+
const Lp = pinvSym(L);
|
|
78
|
+
const BtWy = Array.from({ length: n }, (_, a) => sum(B.map((r, i) => r[a] * w[i] * TE[i])));
|
|
79
|
+
const theta = Lp.map((r) => sum(r.map((x, b) => x * BtWy[b])));
|
|
80
|
+
const Q = sum(TE.map((y, i) => w[i] * (y - sum(B[i].map((x, a) => x * theta[a]))) ** 2));
|
|
81
|
+
return { theta, Lp, Q, w, B };
|
|
82
|
+
}
|
|
83
|
+
const matOf = (trts, f) => ({ rows: [...trts], cols: [...trts], values: trts.map((_, i) => trts.map((__, j) => f(i, j))) });
|
|
84
|
+
/**
|
|
85
|
+
* Frequentist network meta-analysis (netmeta's `netmeta()`): contrasts TE (treat1 versus treat2) with standard errors
|
|
86
|
+
* seTE from studies studlab; the common effects by weighted least squares on the network graph (Ruecker 2012; multi-arm
|
|
87
|
+
* studies by the effective resistances of their comparisons), Q on sum(arms - 1) - (n - 1) df, tau2 by the DL
|
|
88
|
+
* generalisation of Jackson, White and Riley (2012), the random effects with seTE^2 + tau2, the direct estimates, the
|
|
89
|
+
* prediction interval (t on df.Q), and Q split into heterogeneity (within designs) and inconsistency (between).
|
|
90
|
+
* Equivalent to `netmeta::netmeta()` in R.
|
|
91
|
+
*
|
|
92
|
+
* @category Meta-analysis
|
|
93
|
+
*/
|
|
94
|
+
export function netmeta(TE, seTE, treat1, treat2, studlab, options = {}) {
|
|
95
|
+
checkOptions(options, NETMETA_KEYS, "netmeta"); // generated: option-keys.mjs
|
|
96
|
+
const m = TE.length;
|
|
97
|
+
if (![seTE, treat1, treat2, studlab].every((x) => x.length === m))
|
|
98
|
+
throw new RangeError("netmeta: the inputs must have the same length");
|
|
99
|
+
for (let i = 0; i < m; i++) {
|
|
100
|
+
if (!(Number.isFinite(TE[i]) && seTE[i] > 0))
|
|
101
|
+
throw new RangeError("netmeta: TE must be finite and seTE positive");
|
|
102
|
+
if (treat1[i] === treat2[i])
|
|
103
|
+
throw new RangeError("netmeta: treat1 and treat2 must differ");
|
|
104
|
+
}
|
|
105
|
+
const trts = [...new Set([...treat1, ...treat2])].sort();
|
|
106
|
+
const n = trts.length;
|
|
107
|
+
if (netconnection(treat1, treat2, studlab)["n.subnets"] > 1)
|
|
108
|
+
throw new RangeError("netmeta: the network is not connected (see netconnection)");
|
|
109
|
+
// TE and seTE beyond 2^+-200: the fit of TE / c, seTE / c (c a power of two; exact), mapped back (log 110: the
|
|
110
|
+
// variances seTE^2 left the range at 1e+-300)
|
|
111
|
+
const cs = extremeEffectScale(TE, seTE);
|
|
112
|
+
if (cs !== 1) {
|
|
113
|
+
const r = netmeta(TE.map((t) => t / cs), seTE.map((x) => x / cs), treat1, treat2, studlab, { ...options, ...(options.tau2 !== undefined ? { tau2: options.tau2 / cs / cs } : {}) });
|
|
114
|
+
const M = (m) => ({ ...m, values: m.values.map((row) => row.map((v) => v * cs)) });
|
|
115
|
+
return { ...r, TE: [...TE], seTE: [...seTE], "TE.common": M(r["TE.common"]), "seTE.common": M(r["seTE.common"]), "lower.common": M(r["lower.common"]), "upper.common": M(r["upper.common"]),
|
|
116
|
+
"TE.random": M(r["TE.random"]), "seTE.random": M(r["seTE.random"]), "lower.random": M(r["lower.random"]), "upper.random": M(r["upper.random"]),
|
|
117
|
+
"lower.predict": M(r["lower.predict"]), "upper.predict": M(r["upper.predict"]), "TE.direct.common": M(r["TE.direct.common"]), "seTE.direct.common": M(r["seTE.direct.common"]),
|
|
118
|
+
"TE.direct.random": M(r["TE.direct.random"]), "seTE.direct.random": M(r["seTE.direct.random"]), tau2: r.tau2 * cs * cs,
|
|
119
|
+
"w.common": r["w.common"].map((w) => w / cs / cs), "w.random": r["w.random"].map((w) => w / cs / cs) };
|
|
120
|
+
}
|
|
121
|
+
const v = seTE.map((s) => s * s);
|
|
122
|
+
const studies = [...new Set(studlab)];
|
|
123
|
+
const k = studies.length;
|
|
124
|
+
const fitC = fitNetwork(TE, v, treat1, treat2, studlab, trts);
|
|
125
|
+
const arms = studies.map((s) => new Set(studlab.flatMap((x, i) => (x === s ? [treat1[i], treat2[i]] : []))).size);
|
|
126
|
+
const dfQ = sum(arms.map((a) => a - 1)) - (n - 1);
|
|
127
|
+
// tau2: (Q - df) / tr(W (I - H) S (I - H)'), H = B L+ B' W, S = (1/2) C C' per study
|
|
128
|
+
let tau2 = options.tau2 ?? 0;
|
|
129
|
+
if (options.tau2 === undefined && dfQ > 0) {
|
|
130
|
+
const B = fitC.B;
|
|
131
|
+
const W = fitC.w;
|
|
132
|
+
const BL = mul(B, fitC.Lp);
|
|
133
|
+
const H = mul(BL, tr(B)).map((r) => r.map((x, j) => x * W[j]));
|
|
134
|
+
const IH = H.map((r, i) => r.map((x, j) => (i === j ? 1 : 0) - x));
|
|
135
|
+
const S = zeros(m, m);
|
|
136
|
+
for (const s of studies) {
|
|
137
|
+
const idx = studlab.map((x, i) => (x === s ? i : -1)).filter((i) => i >= 0);
|
|
138
|
+
for (const a of idx)
|
|
139
|
+
for (const b of idx)
|
|
140
|
+
S[a][b] = 0.5 * sum(B[a].map((x, t) => x * B[b][t]));
|
|
141
|
+
}
|
|
142
|
+
const M = mul(mul(IH, S), tr(IH));
|
|
143
|
+
const trace = sum(M.map((r, i) => W[i] * r[i]));
|
|
144
|
+
tau2 = Math.max(0, (fitC.Q - dfQ) / trace);
|
|
145
|
+
}
|
|
146
|
+
const fitR = fitNetwork(TE, v.map((x) => x + tau2), treat1, treat2, studlab, trts);
|
|
147
|
+
const level = options.level ?? 0.95;
|
|
148
|
+
const z = qnorm(0.5 + level / 2);
|
|
149
|
+
const levelPredict = options.levelPredict ?? level;
|
|
150
|
+
const te = (f) => (i, j) => f.theta[i] - f.theta[j];
|
|
151
|
+
const se = (f) => (i, j) => (i === j ? 0 : Math.sqrt(Math.max(0, f.Lp[i][i] + f.Lp[j][j] - 2 * f.Lp[i][j])));
|
|
152
|
+
const tp = dfQ > 0 ? qt1(0.5 + levelPredict / 2, dfQ, true, false) : NaN;
|
|
153
|
+
const direct = (add) => {
|
|
154
|
+
const T = matOf(trts, () => NaN), S = matOf(trts, () => NaN);
|
|
155
|
+
for (let i = 0; i < n; i++)
|
|
156
|
+
for (let j = 0; j < n; j++) {
|
|
157
|
+
if (i === j)
|
|
158
|
+
continue;
|
|
159
|
+
const rows = TE.map((_, r) => (treat1[r] === trts[i] && treat2[r] === trts[j] ? 1 : treat1[r] === trts[j] && treat2[r] === trts[i] ? -1 : 0));
|
|
160
|
+
const w = rows.map((s, r) => (s === 0 ? 0 : 1 / (v[r] + add)));
|
|
161
|
+
const sw = sum(w);
|
|
162
|
+
if (sw > 0) {
|
|
163
|
+
T.values[i][j] = sum(w.map((wr, r) => wr * rows[r] * TE[r])) / sw;
|
|
164
|
+
S.values[i][j] = Math.sqrt(1 / sw);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { te: T, se: S };
|
|
168
|
+
};
|
|
169
|
+
const dC = direct(0), dR = direct(tau2);
|
|
170
|
+
// heterogeneity within designs and inconsistency between them
|
|
171
|
+
const design = (s) => [...new Set(studlab.flatMap((x, i) => (x === s ? [treat1[i], treat2[i]] : [])))].sort().join(":");
|
|
172
|
+
let qHet = 0, dfHet = 0;
|
|
173
|
+
for (const g of [...new Set(studies.map(design))]) {
|
|
174
|
+
const rows = studlab.map((x, i) => (design(x) === g ? i : -1)).filter((i) => i >= 0);
|
|
175
|
+
const ts = [...new Set(rows.flatMap((i) => [treat1[i], treat2[i]]))].sort();
|
|
176
|
+
const f = fitNetwork(rows.map((i) => TE[i]), rows.map((i) => v[i]), rows.map((i) => treat1[i]), rows.map((i) => treat2[i]), rows.map((i) => studlab[i]), ts);
|
|
177
|
+
const ks = new Set(rows.map((i) => studlab[i])).size;
|
|
178
|
+
qHet += f.Q;
|
|
179
|
+
dfHet += (ks - 1) * (ts.length - 1);
|
|
180
|
+
}
|
|
181
|
+
const het = heterogeneityIntervals(fitC.Q, dfQ + 1, level);
|
|
182
|
+
const tC = te(fitC), sC = se(fitC), tR = te(fitR), sR = se(fitR);
|
|
183
|
+
return {
|
|
184
|
+
trts, studlab: [...studlab], treat1: [...treat1], treat2: [...treat2], TE: [...TE], seTE: [...seTE], k, n, m,
|
|
185
|
+
"TE.common": matOf(trts, tC), "seTE.common": matOf(trts, sC), "lower.common": matOf(trts, (i, j) => tC(i, j) - z * sC(i, j)), "upper.common": matOf(trts, (i, j) => tC(i, j) + z * sC(i, j)),
|
|
186
|
+
"TE.random": matOf(trts, tR), "seTE.random": matOf(trts, sR), "lower.random": matOf(trts, (i, j) => tR(i, j) - z * sR(i, j)), "upper.random": matOf(trts, (i, j) => tR(i, j) + z * sR(i, j)),
|
|
187
|
+
"lower.predict": matOf(trts, (i, j) => (i === j ? 0 : tR(i, j) - tp * Math.sqrt(sR(i, j) ** 2 + tau2))), "upper.predict": matOf(trts, (i, j) => (i === j ? 0 : tR(i, j) + tp * Math.sqrt(sR(i, j) ** 2 + tau2))),
|
|
188
|
+
"TE.direct.common": dC.te, "seTE.direct.common": dC.se, "TE.direct.random": dR.te, "seTE.direct.random": dR.se,
|
|
189
|
+
// without heterogeneity degrees of freedom (a star of single studies) tau2 is not estimable: NaN as R's NA, the
|
|
190
|
+
// random-effects results those of tau2 = 0 (log 109)
|
|
191
|
+
Q: fitC.Q, "df.Q": dfQ, "pval.Q": dfQ > 0 ? pchisq(fitC.Q, dfQ, { lowerTail: false }) : NaN,
|
|
192
|
+
tau2: options.tau2 === undefined && dfQ <= 0 ? NaN : tau2,
|
|
193
|
+
I2: het.I2, "lower.I2": het.lI2, "upper.I2": het.uI2,
|
|
194
|
+
"Q.heterogeneity": qHet, "df.Q.heterogeneity": dfHet, "Q.inconsistency": fitC.Q - qHet, "df.Q.inconsistency": dfQ - dfHet,
|
|
195
|
+
"w.common": fitC.w, "w.random": fitR.w, referenceGroup: options.referenceGroup ?? null, level,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* P-scores (netmeta's `netrank()`, Ruecker and Schwarzer 2015): for each treatment the mean over the others of the
|
|
200
|
+
* probability that it is better, pnorm((TE_j - TE_i) / se_ij) when small values are desirable (default) or
|
|
201
|
+
* pnorm((TE_i - TE_j) / se_ij) when undesirable.
|
|
202
|
+
* Equivalent to `netmeta::netrank()` in R.
|
|
203
|
+
*
|
|
204
|
+
* @category Meta-analysis
|
|
205
|
+
*/
|
|
206
|
+
export function netrank(fit, options = {}) {
|
|
207
|
+
checkOptions(options, NETRANK_KEYS, "netrank"); // generated: option-keys.mjs
|
|
208
|
+
const T = options.random === false ? fit["TE.common"] : fit["TE.random"];
|
|
209
|
+
const S = options.random === false ? fit["seTE.common"] : fit["seTE.random"];
|
|
210
|
+
const sign = (options.smallValues ?? "desirable") === "desirable" ? 1 : -1;
|
|
211
|
+
const n = fit.trts.length;
|
|
212
|
+
const out = {};
|
|
213
|
+
fit.trts.forEach((t, i) => {
|
|
214
|
+
// P(i better than j) = pnorm((TE_j - TE_i) / se) with small values desirable, TE[j][i] = theta_j - theta_i
|
|
215
|
+
let s = 0;
|
|
216
|
+
for (let j = 0; j < n; j++)
|
|
217
|
+
if (j !== i)
|
|
218
|
+
s += pnorm((sign * T.values[j][i]) / S.values[i][j]);
|
|
219
|
+
out[t] = s / (n - 1);
|
|
220
|
+
});
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Direct and indirect evidence for each comparison (netmeta's `netsplit()`, back-calculation): the direct estimate (the
|
|
225
|
+
* inverse-variance pooling of the comparison's contrasts), the indirect one from the network estimate and the direct
|
|
226
|
+
* one, their difference, and the direct evidence proportion v_network / v_direct.
|
|
227
|
+
* Equivalent to `netmeta::netsplit()` in R.
|
|
228
|
+
*
|
|
229
|
+
* @category Meta-analysis
|
|
230
|
+
*/
|
|
231
|
+
export function netsplit(fit) {
|
|
232
|
+
const n = fit.trts.length;
|
|
233
|
+
const z = qnorm(0.5 + fit.level / 2);
|
|
234
|
+
const comps = [];
|
|
235
|
+
// pairs in treatment order; a pair with the reference group is written "other:reference" (as netmeta)
|
|
236
|
+
const ref = fit.referenceGroup === null ? -1 : fit.trts.indexOf(fit.referenceGroup);
|
|
237
|
+
for (let i = 0; i < n; i++)
|
|
238
|
+
for (let j = i + 1; j < n; j++)
|
|
239
|
+
comps.push(i === ref ? [j, i] : [i, j]);
|
|
240
|
+
comps.sort((a, b) => (a[1] === ref ? 0 : 1) - (b[1] === ref ? 0 : 1));
|
|
241
|
+
const part = (te, se, label) => ({
|
|
242
|
+
comparison: label, TE: te, seTE: se, lower: te.map((t, i) => t - z * se[i]), upper: te.map((t, i) => t + z * se[i]),
|
|
243
|
+
z: te.map((t, i) => t / se[i]), p: te.map((t, i) => 2 * pnorm(-Math.abs(t / se[i]))),
|
|
244
|
+
});
|
|
245
|
+
const label = comps.map(([i, j]) => `${fit.trts[i]}:${fit.trts[j]}`);
|
|
246
|
+
// the effects divided by a power of two beyond 2^+-200 before squaring, mapped back (log 110)
|
|
247
|
+
const cs = extremeEffectScale(fit.TE, fit.seTE);
|
|
248
|
+
const dv = (m) => (cs === 1 ? m : m.map((r) => r.map((v) => v / cs)));
|
|
249
|
+
const back = (q) => (cs === 1 ? q : { ...q, TE: q.TE.map((v) => v * cs), seTE: q.seTE.map((v) => v * cs), lower: q.lower.map((v) => v * cs), upper: q.upper.map((v) => v * cs) });
|
|
250
|
+
const build = (kind) => {
|
|
251
|
+
const N = dv(fit[`TE.${kind}`].values), NS = dv(fit[`seTE.${kind}`].values), D = dv(fit[`TE.direct.${kind}`].values), DS = dv(fit[`seTE.direct.${kind}`].values);
|
|
252
|
+
const dte = [], dse = [], ite = [], ise = [], cte = [], cse = [], prop = [];
|
|
253
|
+
for (const [i, j] of comps) {
|
|
254
|
+
const vn = NS[i][j] ** 2, vd = DS[i][j] ** 2;
|
|
255
|
+
dte.push(D[i][j]);
|
|
256
|
+
dse.push(DS[i][j]);
|
|
257
|
+
if (!Number.isFinite(vd)) {
|
|
258
|
+
ite.push(N[i][j]);
|
|
259
|
+
ise.push(NS[i][j]);
|
|
260
|
+
cte.push(NaN);
|
|
261
|
+
cse.push(NaN);
|
|
262
|
+
prop.push(0);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
const p = vn / vd;
|
|
266
|
+
prop.push(p);
|
|
267
|
+
if (p >= 1 - 1e-12) {
|
|
268
|
+
ite.push(NaN);
|
|
269
|
+
ise.push(NaN);
|
|
270
|
+
cte.push(NaN);
|
|
271
|
+
cse.push(NaN);
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
const vi = 1 / (1 / vn - 1 / vd);
|
|
275
|
+
const ti = vi * (N[i][j] / vn - D[i][j] / vd);
|
|
276
|
+
ite.push(ti);
|
|
277
|
+
ise.push(Math.sqrt(vi));
|
|
278
|
+
cte.push(D[i][j] - ti);
|
|
279
|
+
cse.push(Math.sqrt(vd + vi));
|
|
280
|
+
}
|
|
281
|
+
return { direct: back(part(dte, dse, label)), indirect: back(part(ite, ise, label)), compare: back(part(cte, cse, label)), prop };
|
|
282
|
+
};
|
|
283
|
+
const r = build("random"), c = build("common");
|
|
284
|
+
return { "direct.random": r.direct, "indirect.random": r.indirect, "compare.random": r.compare, "direct.common": c.direct, "indirect.common": c.indirect, "compare.common": c.compare, "prop.random": r.prop, "prop.common": c.prop };
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The connected components of a network (netmeta's `netconnection()`): the number of subnets, the numbers of
|
|
288
|
+
* studies k, treatments n and comparisons, and the distance matrix (shortest path lengths; Infinity between subnets).
|
|
289
|
+
* Equivalent to `netmeta::netconnection()` in R.
|
|
290
|
+
*
|
|
291
|
+
* @category Meta-analysis
|
|
292
|
+
*/
|
|
293
|
+
export function netconnection(treat1, treat2, studlab) {
|
|
294
|
+
const trts = [...new Set([...treat1, ...treat2])].sort();
|
|
295
|
+
const n = trts.length;
|
|
296
|
+
const D = Array.from({ length: n }, (_, i) => Array.from({ length: n }, (__, j) => (i === j ? 0 : Infinity)));
|
|
297
|
+
treat1.forEach((a, i) => { const x = trts.indexOf(a), y = trts.indexOf(treat2[i]); D[x][y] = 1; D[y][x] = 1; });
|
|
298
|
+
for (let k = 0; k < n; k++)
|
|
299
|
+
for (let i = 0; i < n; i++)
|
|
300
|
+
for (let j = 0; j < n; j++)
|
|
301
|
+
if (D[i][k] + D[k][j] < D[i][j])
|
|
302
|
+
D[i][j] = D[i][k] + D[k][j];
|
|
303
|
+
const subnet = {};
|
|
304
|
+
let c = 0;
|
|
305
|
+
for (let i = 0; i < n; i++) {
|
|
306
|
+
if (subnet[trts[i]] !== undefined)
|
|
307
|
+
continue;
|
|
308
|
+
c++;
|
|
309
|
+
for (let j = 0; j < n; j++)
|
|
310
|
+
if (Number.isFinite(D[i][j]))
|
|
311
|
+
subnet[trts[j]] = c;
|
|
312
|
+
}
|
|
313
|
+
return { "n.subnets": c, k: new Set(studlab ?? treat1.map((_, i) => String(i))).size, n, m: treat1.length, D: { rows: trts, cols: trts, values: D }, subnet };
|
|
314
|
+
}
|
|
315
|
+
/** The design of each study: its treatments sorted and joined by ":". */
|
|
316
|
+
const designsOf = (fit) => {
|
|
317
|
+
const m = new Map();
|
|
318
|
+
for (const s of new Set(fit.studlab))
|
|
319
|
+
m.set(s, [...new Set(fit.studlab.flatMap((x, i) => (x === s ? [fit.treat1[i], fit.treat2[i]] : [])))].sort().join(":"));
|
|
320
|
+
return m;
|
|
321
|
+
};
|
|
322
|
+
/**
|
|
323
|
+
* Q decomposed by design (netmeta's `decomp.design()`): the Q of each design = that of the network fitted to its
|
|
324
|
+
* studies alone, df (studies - 1)(arms - 1); within = their sum, between = total - within. Designs are listed by their
|
|
325
|
+
* number of arms, then alphabetically.
|
|
326
|
+
* Equivalent to `netmeta::decomp.design()` in R.
|
|
327
|
+
*
|
|
328
|
+
* @category Meta-analysis
|
|
329
|
+
*/
|
|
330
|
+
export function decompDesign(fit) {
|
|
331
|
+
// TE and seTE divided by a power of two beyond 2^+-200 (the Q statistics do not depend on it; log 110)
|
|
332
|
+
const cs = extremeEffectScale(fit.TE, fit.seTE);
|
|
333
|
+
const TEs = fit.TE.map((t) => t / cs);
|
|
334
|
+
const des = designsOf(fit);
|
|
335
|
+
const v = fit.seTE.map((s) => (s / cs) * (s / cs));
|
|
336
|
+
const names = [...new Set(des.values())].sort((a, b) => a.split(":").length - b.split(":").length || (a < b ? -1 : a > b ? 1 : 0));
|
|
337
|
+
const Q = [], df = [];
|
|
338
|
+
for (const g of names) {
|
|
339
|
+
const rows = fit.studlab.map((x, i) => (des.get(x) === g ? i : -1)).filter((i) => i >= 0);
|
|
340
|
+
const ts = g.split(":");
|
|
341
|
+
const f = fitNetwork(rows.map((i) => TEs[i]), rows.map((i) => v[i]), rows.map((i) => fit.treat1[i]), rows.map((i) => fit.treat2[i]), rows.map((i) => fit.studlab[i]), ts);
|
|
342
|
+
Q.push(f.Q);
|
|
343
|
+
df.push((new Set(rows.map((i) => fit.studlab[i])).size - 1) * (ts.length - 1));
|
|
344
|
+
}
|
|
345
|
+
const p = (q, d) => (d > 0 ? pchisq(q, d, { lowerTail: false }) : NaN);
|
|
346
|
+
const qw = sum(Q), dw = sum(df);
|
|
347
|
+
return {
|
|
348
|
+
"Q.decomp": { Q: [fit.Q, qw, fit.Q - qw], df: [fit["df.Q"], dw, fit["df.Q"] - dw], pval: [p(fit.Q, fit["df.Q"]), p(qw, dw), p(fit.Q - qw, fit["df.Q"] - dw)] },
|
|
349
|
+
"Q.het.design": { design: names, Q, df, pval: Q.map((q, i) => p(q, df[i])) },
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Network measures (netmeta's `netmeasures()`): the direct evidence proportion of each comparison, v_network / v_direct
|
|
354
|
+
* (0 without direct evidence), for the random-effects model (default, as netmeta when the fit has one) or the
|
|
355
|
+
* common-effect model. netmeta's mean path length and minimal parallelism are not provided (log 101: not reproduced).
|
|
356
|
+
* Equivalent to `netmeta::netmeasures()` in R.
|
|
357
|
+
*
|
|
358
|
+
* @category Meta-analysis
|
|
359
|
+
*/
|
|
360
|
+
export function netmeasures(fit, options = {}) {
|
|
361
|
+
checkOptions(options, NETMEASURES_KEYS, "netmeasures"); // generated: option-keys.mjs
|
|
362
|
+
const kind = options.random === false ? "common" : "random";
|
|
363
|
+
const n = fit.trts.length;
|
|
364
|
+
const DS = fit[`seTE.direct.${kind}`].values;
|
|
365
|
+
const proportion = {};
|
|
366
|
+
const cs = extremeEffectScale(fit.TE, fit.seTE);
|
|
367
|
+
for (let i = 0; i < n; i++)
|
|
368
|
+
for (let j = i + 1; j < n; j++) {
|
|
369
|
+
// a ratio of variances: the standard errors divided by a power of two first (log 110)
|
|
370
|
+
const vn = (fit[`seTE.${kind}`].values[i][j] / cs) ** 2;
|
|
371
|
+
const vd = (DS[i][j] / cs) ** 2;
|
|
372
|
+
proportion[`${fit.trts[i]}:${fit.trts[j]}`] = Number.isFinite(vd) ? vn / vd : 0;
|
|
373
|
+
}
|
|
374
|
+
return { proportion };
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Contrasts from arm-level data (netmeta's `pairwise()`): every pair of arms of each study (treat1 the earlier arm).
|
|
378
|
+
* 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
|
|
379
|
+
* (log 74); continuous (`mean`, `sd`, `n`; sm "MD").
|
|
380
|
+
* Equivalent to `netmeta::pairwise()` in R.
|
|
381
|
+
*
|
|
382
|
+
* @category Meta-analysis
|
|
383
|
+
*/
|
|
384
|
+
export function pairwise(data, options = {}) {
|
|
385
|
+
checkOptions(data, PAIRWISE_KEYS_0, "pairwise"); // generated: option-keys.mjs
|
|
386
|
+
checkOptions(options, PAIRWISE_KEYS_1, "pairwise"); // generated: option-keys.mjs
|
|
387
|
+
const sm = options.sm ?? (data.event ? "OR" : "MD");
|
|
388
|
+
const incr = options.incr ?? 0.5;
|
|
389
|
+
const out = { TE: [], seTE: [], treat1: [], treat2: [], studlab: [] };
|
|
390
|
+
for (const s of [...new Set(data.studlab)]) {
|
|
391
|
+
const idx = data.studlab.map((x, i) => (x === s ? i : -1)).filter((i) => i >= 0);
|
|
392
|
+
const anyEdge = data.event ? idx.some((i) => data.event[i] === 0 || data.event[i] === data.n[i]) : false;
|
|
393
|
+
for (let a = 0; a < idx.length; a++)
|
|
394
|
+
for (let b = a + 1; b < idx.length; b++) {
|
|
395
|
+
const i = idx[a], j = idx[b];
|
|
396
|
+
let te, se;
|
|
397
|
+
if (sm === "MD") {
|
|
398
|
+
te = data.mean[i] - data.mean[j];
|
|
399
|
+
se = Math.sqrt(data.sd[i] ** 2 / data.n[i] + data.sd[j] ** 2 / data.n[j]);
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
const t = anyEdge ? incr : 0;
|
|
403
|
+
const x1 = data.event[i] + t, x2 = data.event[j] + t;
|
|
404
|
+
if (sm === "OR") {
|
|
405
|
+
const m1 = data.n[i] + 2 * t, m2 = data.n[j] + 2 * t;
|
|
406
|
+
te = Math.log(x1 / (m1 - x1)) - Math.log(x2 / (m2 - x2));
|
|
407
|
+
se = Math.sqrt(1 / x1 + 1 / (m1 - x1) + 1 / x2 + 1 / (m2 - x2));
|
|
408
|
+
}
|
|
409
|
+
else if (sm === "RR") {
|
|
410
|
+
const m1 = data.n[i] + t, m2 = data.n[j] + t;
|
|
411
|
+
te = Math.log(x1 / m1) - Math.log(x2 / m2);
|
|
412
|
+
se = Math.sqrt(1 / x1 - 1 / m1 + 1 / x2 - 1 / m2);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
const m1 = data.n[i] + 2 * t, m2 = data.n[j] + 2 * t;
|
|
416
|
+
te = data.event[i] / data.n[i] - data.event[j] / data.n[j];
|
|
417
|
+
se = Math.sqrt((x1 * (m1 - x1)) / m1 ** 3 + (x2 * (m2 - x2)) / m2 ** 3);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
out.TE.push(te);
|
|
421
|
+
out.seTE.push(se);
|
|
422
|
+
out.treat1.push(data.treat[i]);
|
|
423
|
+
out.treat2.push(data.treat[j]);
|
|
424
|
+
out.studlab.push(s);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return out;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* The additive component network meta-analysis (netmeta's `netcomb()` / `discomb()`, Ruecker et al. 2020; the network
|
|
431
|
+
* may be disconnected): treatments are "+"-joined components; the components' effects by weighted least squares on
|
|
432
|
+
* X = B C (C treatment by component, the `inactive` treatment left out), Q.additive the weighted residual sum of squares
|
|
433
|
+
* on comparisons - rank(X) df, tau2 by the DL generalisation with the additive hat matrix (two-arm studies; log 77).
|
|
434
|
+
* Equivalent to `netmeta::discomb()` in R.
|
|
435
|
+
*
|
|
436
|
+
* @category Meta-analysis
|
|
437
|
+
*/
|
|
438
|
+
export function discomb(TE, seTE, treat1, treat2, studlab, options = {}) {
|
|
439
|
+
checkOptions(options, DISCOMB_KEYS, "discomb"); // generated: option-keys.mjs
|
|
440
|
+
// TE and seTE beyond 2^+-200: the fit of TE / c, seTE / c, mapped back (log 110)
|
|
441
|
+
const cs = extremeEffectScale(TE, seTE);
|
|
442
|
+
if (cs !== 1) {
|
|
443
|
+
const r = discomb(TE.map((t) => t / cs), seTE.map((x) => x / cs), treat1, treat2, studlab, options);
|
|
444
|
+
const k = (a) => a.map((v) => v * cs);
|
|
445
|
+
return { ...r, "Comp.common": k(r["Comp.common"]), "seComp.common": k(r["seComp.common"]), "Comp.random": k(r["Comp.random"]), "seComp.random": k(r["seComp.random"]),
|
|
446
|
+
"Comb.common": k(r["Comb.common"]), "seComb.common": k(r["seComb.common"]), "Comb.random": k(r["Comb.random"]), "seComb.random": k(r["seComb.random"]), tau2: r.tau2 * cs * cs };
|
|
447
|
+
}
|
|
448
|
+
const sep = options.sep ?? "+";
|
|
449
|
+
const trts = [...new Set([...treat1, ...treat2])].sort();
|
|
450
|
+
const comps = [...new Set(trts.flatMap((t) => t.split(sep).map((c) => c.trim())))].filter((c) => c !== options.inactive).sort();
|
|
451
|
+
const C = trts.map((t) => { const parts = t.split(sep).map((c) => c.trim()); return comps.map((c) => (parts.includes(c) ? 1 : 0)); });
|
|
452
|
+
const m = TE.length;
|
|
453
|
+
const X = TE.map((_, i) => comps.map((_, c) => C[trts.indexOf(treat1[i])][c] - C[trts.indexOf(treat2[i])][c]));
|
|
454
|
+
const v = seTE.map((s) => s * s);
|
|
455
|
+
const wls = (add) => {
|
|
456
|
+
const w = contrastWeights(v.map((x) => x + add), treat1, treat2, studlab);
|
|
457
|
+
const XtWX = comps.map((_, a) => comps.map((__, b) => sum(X.map((r, i) => r[a] * w[i] * r[b]))));
|
|
458
|
+
const A = pinvSym(XtWX);
|
|
459
|
+
const XtWy = comps.map((_, a) => sum(X.map((r, i) => r[a] * w[i] * TE[i])));
|
|
460
|
+
const beta = A.map((r) => sum(r.map((x, b) => x * XtWy[b])));
|
|
461
|
+
const Q = sum(TE.map((y, i) => w[i] * (y - sum(X[i].map((x, a) => x * beta[a]))) ** 2));
|
|
462
|
+
return { beta, se: A.map((r, a) => Math.sqrt(r[a])), Q, A, w };
|
|
463
|
+
};
|
|
464
|
+
const c0 = wls(0);
|
|
465
|
+
const rank = matrixRank(X);
|
|
466
|
+
const df = m - rank;
|
|
467
|
+
let tau2 = 0;
|
|
468
|
+
if (df > 0) {
|
|
469
|
+
const H = X.map((r) => X.map((r2, j) => sum(r.map((x, a) => x * sum(c0.A[a].map((y, b) => y * r2[b])))) * c0.w[j]));
|
|
470
|
+
const IH = H.map((r, i) => r.map((x, j) => (i === j ? 1 : 0) - x));
|
|
471
|
+
const tr_ = sum(IH.map((r, i) => c0.w[i] * sum(r.map((x) => x * x))));
|
|
472
|
+
tau2 = Math.max(0, (c0.Q - df) / tr_);
|
|
473
|
+
}
|
|
474
|
+
const cr = wls(tau2);
|
|
475
|
+
const comb = (f) => ({
|
|
476
|
+
e: C.map((r) => sum(r.map((x, a) => x * f.beta[a]))), s: C.map((r) => Math.sqrt(Math.max(0, sum(r.map((x, a) => x * sum(f.A[a].map((y, b) => y * r[b]))))))),
|
|
477
|
+
});
|
|
478
|
+
const kc = comb(c0), kr = comb(cr);
|
|
479
|
+
return { comps, "Comp.common": c0.beta, "seComp.common": c0.se, "Comp.random": cr.beta, "seComp.random": cr.se, trts, "Comb.common": kc.e, "seComb.common": kc.s, "Comb.random": kr.e, "seComb.random": kr.s, "Q.additive": c0.Q, "df.Q.additive": df, tau2 };
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* {@link discomb} for a fitted network (netmeta's `netcomb()`). Equivalent to `netmeta::netcomb()` in R.
|
|
483
|
+
*
|
|
484
|
+
* @category Meta-analysis
|
|
485
|
+
*/
|
|
486
|
+
export function netcomb(fit, options = {}) {
|
|
487
|
+
checkOptions(options, NETCOMB_KEYS, "netcomb"); // generated: option-keys.mjs
|
|
488
|
+
return discomb(fit.TE, fit.seTE, fit.treat1, fit.treat2, fit.studlab, options);
|
|
489
|
+
}
|
|
490
|
+
function matrixRank(X) {
|
|
491
|
+
const p = X[0].length;
|
|
492
|
+
const G = Array.from({ length: p }, (_, a) => Array.from({ length: p }, (__, b) => sum(X.map((r) => r[a] * r[b]))));
|
|
493
|
+
const { values } = symmetricEigen(G);
|
|
494
|
+
const mx = Math.max(...values.map(Math.abs));
|
|
495
|
+
return values.filter((l) => Math.abs(l) > 1e-10 * mx).length;
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* The league table (netmeta's `netleague()`): the diagonal the treatments, the lower triangle [i][j] the network
|
|
499
|
+
* estimate of the column treatment against the row treatment (TE[j][i]) with its interval, the upper triangle the direct
|
|
500
|
+
* estimates (TE[i][j]; "." where there is none); "est (lower to upper)" with `digits` decimals, the bounds of a table
|
|
501
|
+
* padded to a common width.
|
|
502
|
+
* Equivalent to `netmeta::netleague()` in R.
|
|
503
|
+
*
|
|
504
|
+
* @category Meta-analysis
|
|
505
|
+
*/
|
|
506
|
+
export function netleague(fit, options = {}) {
|
|
507
|
+
checkOptions(options, NETLEAGUE_KEYS, "netleague"); // generated: option-keys.mjs
|
|
508
|
+
const kind = options.random === false ? "common" : "random";
|
|
509
|
+
const digits = options.digits ?? 2;
|
|
510
|
+
const sepr = options.separator ?? " to ";
|
|
511
|
+
const [ob, cb] = (options.bracket ?? "(") === "(" ? ["(", ")"] : ["[", "]"];
|
|
512
|
+
const z = qnorm(0.5 + fit.level / 2);
|
|
513
|
+
const n = fit.trts.length;
|
|
514
|
+
const T = fit[`TE.${kind}`].values, D = fit[`TE.direct.${kind}`].values, DS = fit[`seTE.direct.${kind}`].values, Lo = fit[`lower.${kind}`].values, Up = fit[`upper.${kind}`].values;
|
|
515
|
+
const cells = fit.trts.map((_, i) => fit.trts.map((__, j) => {
|
|
516
|
+
if (i === j)
|
|
517
|
+
return { est: NaN, lo: NaN, up: NaN };
|
|
518
|
+
if (i > j)
|
|
519
|
+
return { est: T[j][i], lo: Lo[j][i], up: Up[j][i] };
|
|
520
|
+
return { est: D[i][j], lo: D[i][j] - z * DS[i][j], up: D[i][j] + z * DS[i][j] };
|
|
521
|
+
}));
|
|
522
|
+
const fmt = (x) => x.toFixed(digits);
|
|
523
|
+
const bounds = cells.flat().flatMap((c) => (Number.isFinite(c.est) ? [fmt(c.lo), fmt(c.up)] : []));
|
|
524
|
+
const width = Math.max(0, ...bounds.map((b) => b.length));
|
|
525
|
+
const pad = (s) => s.padStart(width, " ");
|
|
526
|
+
return cells.map((row, i) => row.map((c, j) => (i === j ? fit.trts[i] : Number.isFinite(c.est) ? `${fmt(c.est)} ${ob}${pad(fmt(c.lo))}${sepr}${pad(fmt(c.up))}${cb}` : ".")));
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* The partial order of treatments over several outcomes (netmeta's `netposet()`): P the treatments by outcomes matrix of
|
|
530
|
+
* ranking scores; M0 the covering relation (Hasse diagram) of "i at least as good as j in every outcome and better in
|
|
531
|
+
* one" (M0[i][j] = 1: i directly above j).
|
|
532
|
+
* Equivalent to `netmeta::netposet()` in R.
|
|
533
|
+
*
|
|
534
|
+
* @category Meta-analysis
|
|
535
|
+
*/
|
|
536
|
+
export function netposet(P) {
|
|
537
|
+
const n = P.length;
|
|
538
|
+
const dom = (i, j) => i !== j && P[i].every((x, o) => x >= P[j][o]) && P[i].some((x, o) => x > P[j][o]);
|
|
539
|
+
return Array.from({ length: n }, (_, i) => Array.from({ length: n }, (__, j) => {
|
|
540
|
+
if (!dom(i, j))
|
|
541
|
+
return 0;
|
|
542
|
+
for (let k = 0; k < n; k++)
|
|
543
|
+
if (k !== i && k !== j && dom(i, k) && dom(k, j))
|
|
544
|
+
return 0;
|
|
545
|
+
return 1;
|
|
546
|
+
}));
|
|
547
|
+
}
|