@numero-uno-jpn/rstats 0.15.3 → 0.17.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 +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +24 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/htest/pAdjust.js
CHANGED
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
// - p outside [0, 1] is computed as is, like R (capped at 1 above, not bounded below)
|
|
13
13
|
// Differences from R: with Hommel, a non-integer n is an error (R silently returns something). For a length-0 p
|
|
14
14
|
// with Hommel and n > 0, R returns a length-1 value; here a length-0 result is returned
|
|
15
|
-
import {
|
|
15
|
+
import { wideSum, wideSumAdd, wideSumValue } from "../internal/summary.js";
|
|
16
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
17
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
18
|
+
const P_ADJUST_KEYS = ["method", "n"];
|
|
16
19
|
const METHODS = ["holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"];
|
|
17
20
|
function matchMethod(m) {
|
|
18
21
|
if (METHODS.includes(m))
|
|
@@ -27,7 +30,8 @@ function matchMethod(m) {
|
|
|
27
30
|
*
|
|
28
31
|
* `NaN` values are excluded from the adjustment and returned as `NaN` in place. If `n` is
|
|
29
32
|
* 1 or less, the p-values are returned unchanged. Differences from R: with `"hommel"` a
|
|
30
|
-
* non-integer `n` is an error, and an empty `p` always
|
|
33
|
+
* non-integer `n` or an `n` above 65536 (O(n^2) work, about 6 s at the limit) is an error, and an empty `p` always
|
|
34
|
+
* gives an empty result.
|
|
31
35
|
*
|
|
32
36
|
* @param p p-values.
|
|
33
37
|
* @param options See {@link PAdjustOptions}, or just the method name as a string.
|
|
@@ -44,6 +48,7 @@ function matchMethod(m) {
|
|
|
44
48
|
* @category Multiple comparisons
|
|
45
49
|
*/
|
|
46
50
|
export function pAdjust(p, options = {}) {
|
|
51
|
+
checkOptions(options, P_ADJUST_KEYS, "pAdjust"); // generated: option-keys.mjs
|
|
47
52
|
const opts = typeof options === "string" ? { method: options } : options;
|
|
48
53
|
const method = matchMethod(opts.method ?? "holm");
|
|
49
54
|
const all = Array.from(p);
|
|
@@ -55,7 +60,11 @@ export function pAdjust(p, options = {}) {
|
|
|
55
60
|
const n = opts.n ?? lp;
|
|
56
61
|
if (!(n >= lp))
|
|
57
62
|
throw new RangeError("n must be at least the number of non-NaN p-values");
|
|
58
|
-
|
|
63
|
+
// (an infinite number of comparisons: BY's harmonic sum never ended, and holm gave 1 for every p-value; R stops)
|
|
64
|
+
if (n === Infinity)
|
|
65
|
+
throw new RangeError("n must be finite");
|
|
66
|
+
// no p-values to adjust (empty or all NaN): returned before hommel builds n - lp padding values (#306)
|
|
67
|
+
if (n <= 1 || lp === 0)
|
|
59
68
|
return all;
|
|
60
69
|
const pv = idx.map((i) => all[i]);
|
|
61
70
|
const adj = adjust(pv, method, n);
|
|
@@ -104,20 +113,44 @@ function adjust(p, method, n) {
|
|
|
104
113
|
// Cumulative minimum of (n / i) p_(i) in decreasing order. BY additionally multiplies by sum_{k=1}^{n} 1/k
|
|
105
114
|
// (evaluated as q * n / i * p, which rounds the same way as R)
|
|
106
115
|
// (R's sum accumulates in extended precision, so the sum is taken in double-double)
|
|
107
|
-
|
|
116
|
+
// sum_{k=1}^{n} 1/k accumulated term by term (no array of n terms; the same double-double sum as sum())
|
|
117
|
+
let q = 1;
|
|
118
|
+
if (method === "BY") {
|
|
119
|
+
const acc = wideSum();
|
|
120
|
+
const m = Math.floor(n);
|
|
121
|
+
if (m <= 1e6) {
|
|
122
|
+
for (let k = 1; k <= m; k++)
|
|
123
|
+
wideSumAdd(acc, 1 / k, m);
|
|
124
|
+
q = wideSumValue(acc);
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
// the asymptotic expansion of the harmonic number (error below 1/(252 m^6)): the sum took m steps
|
|
128
|
+
const m2 = m * m;
|
|
129
|
+
q = Math.log(m) + 0.5772156649015329 + 1 / (2 * m) - 1 / (12 * m2) + 1 / (120 * m2 * m2);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// q n overflows for n above about 2.5e305 (BY): then the same products in units of 2^10 (q < 2^10), which
|
|
133
|
+
// round the same way (the values are far above the subnormal range: n p >= 1e305 Number.MIN_VALUE)
|
|
134
|
+
const qn = q * n;
|
|
135
|
+
const unit = Number.isFinite(qn) ? 1 : 1024;
|
|
136
|
+
const qnu = Number.isFinite(qn) ? qn : q * (n / 1024);
|
|
108
137
|
const o = order(p, true);
|
|
109
138
|
let m = Infinity;
|
|
110
139
|
o.forEach((j, k) => {
|
|
111
140
|
const i = lp - k;
|
|
112
|
-
m = Math.min(m, (
|
|
141
|
+
m = Math.min(m, (qnu / i) * p[j] * unit);
|
|
113
142
|
out[j] = Math.min(1, m);
|
|
114
143
|
});
|
|
115
144
|
return out;
|
|
116
145
|
}
|
|
117
146
|
case "hommel":
|
|
118
|
-
|
|
147
|
+
// as R, Hochberg's method for n = 2 (the same values for p in [0, 1]; p outside it are taken as they are, where
|
|
148
|
+
// the Hommel loop gave [-2, 0.3] for [-2, 0.3], R [-4, 0.3]; #471)
|
|
149
|
+
return n === 2 ? adjust(p, "hochberg", n) : hommel(p, n);
|
|
119
150
|
}
|
|
120
151
|
}
|
|
152
|
+
/** Largest n of the Hommel method (#391) */
|
|
153
|
+
const MAX_HOMMEL_N = 2 ** 16;
|
|
121
154
|
/**
|
|
122
155
|
* Hommel adjusted p-values (the hommel procedure of statsmodels multipletests).
|
|
123
156
|
* The n - lp unobserved p are added as 1. O(n^2)
|
|
@@ -125,6 +158,9 @@ function adjust(p, method, n) {
|
|
|
125
158
|
function hommel(p, n) {
|
|
126
159
|
if (!Number.isInteger(n))
|
|
127
160
|
throw new RangeError("n must be an integer for the hommel method");
|
|
161
|
+
// O(n^2) steps: n = 65536 takes about 6 s, 1e5 about 12 s (#391); checked before the padding is built
|
|
162
|
+
if (n > MAX_HOMMEL_N)
|
|
163
|
+
throw new RangeError(`n must be at most ${MAX_HOMMEL_N} for the hommel method (O(n^2) work)`);
|
|
128
164
|
const lp = p.length;
|
|
129
165
|
const full = [...p, ...new Array(n - lp).fill(1)];
|
|
130
166
|
const o = order(full, false);
|
package/dist/htest/pairwise.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type GroupLabel } from "./groups.js";
|
|
2
2
|
import { type PAdjustMethod } from "./pAdjust.js";
|
|
3
3
|
import { type Alternative, type Sample } from "./types.js";
|
|
4
4
|
import { type WilcoxTestOptions } from "./wilcoxTest.js";
|
|
@@ -21,6 +21,8 @@ export interface PairwiseResult {
|
|
|
21
21
|
colNames: string[];
|
|
22
22
|
/** Adjustment method. */
|
|
23
23
|
pAdjustMethod: PAdjustMethod | string;
|
|
24
|
+
/** R's warnings, each once, in R's order (an empty array when there are none). */
|
|
25
|
+
warnings: string[];
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
26
28
|
* Common options of the pairwise comparisons.
|
package/dist/htest/pairwise.js
CHANGED
|
@@ -6,21 +6,31 @@
|
|
|
6
6
|
// the standard deviation pooled over all groups, s^2 = sum (n_i - 1) s_i^2 / sum (n_i - 1), with its degrees of
|
|
7
7
|
// freedom; the other comparisons are the two-sample tests themselves.
|
|
8
8
|
import { pt1 } from "../distributions/t.js";
|
|
9
|
-
import { levelText
|
|
10
|
-
import {
|
|
9
|
+
import { levelText } from "../internal/levels.js";
|
|
10
|
+
import { groupMoments } from "../internal/summary.js";
|
|
11
|
+
import { compareLabels, factorLabels } from "./groups.js";
|
|
12
|
+
import { checkPooledVariance } from "./multipleComparisons.js";
|
|
11
13
|
import { pAdjust } from "./pAdjust.js";
|
|
12
14
|
import { propTest } from "./propTest.js";
|
|
13
15
|
import { tTest } from "./tTest.js";
|
|
14
16
|
import { matchAlternative } from "./types.js";
|
|
15
17
|
import { wilcoxTest } from "./wilcoxTest.js";
|
|
16
|
-
|
|
18
|
+
import { reported, unreported, Warnings } from "../internal/warnings.js";
|
|
19
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
20
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
21
|
+
const PAIRWISE_TTEST_KEYS = ["poolSd", "paired", "pAdjustMethod", "alternative"];
|
|
22
|
+
const PAIRWISE_WILCOX_TEST_KEYS = [
|
|
23
|
+
"mu", "paired", "exact", "correct", "confLevel", "tolRoot", "pAdjustMethod", "alternative",
|
|
24
|
+
];
|
|
25
|
+
const PAIRWISE_PROP_TEST_KEYS = ["pAdjustMethod", "correct"];
|
|
26
|
+
/** Group a sample by labels: levels sorted (numbers numerically, strings by code point), values in input order */
|
|
17
27
|
function byLevel(x, g, dropNaN) {
|
|
18
28
|
const xs = Array.from(x);
|
|
19
29
|
if (xs.length !== g.length)
|
|
20
30
|
throw new RangeError("'x' and 'g' must have the same length");
|
|
21
31
|
const map = new Map();
|
|
22
|
-
// numeric labels with the same 15-digit text are one level, as in R's factor()
|
|
23
|
-
const gs =
|
|
32
|
+
// numeric labels with the same 15-digit text are one level, and mixed labels are text, as in R's factor()
|
|
33
|
+
const gs = factorLabels(g);
|
|
24
34
|
for (let i = 0; i < xs.length; i++) {
|
|
25
35
|
const l = gs[i];
|
|
26
36
|
if (typeof l === "number" && Number.isNaN(l))
|
|
@@ -32,7 +42,7 @@ function byLevel(x, g, dropNaN) {
|
|
|
32
42
|
map.set(l, (arr = []));
|
|
33
43
|
arr.push(xs[i]);
|
|
34
44
|
}
|
|
35
|
-
const levels = Array.from(map.keys()).sort(
|
|
45
|
+
const levels = Array.from(map.keys()).sort(compareLabels);
|
|
36
46
|
return { levels, groups: levels.map((l) => map.get(l)) };
|
|
37
47
|
}
|
|
38
48
|
/** Lower-triangular table of compare(i, j) for i > j, adjusted together */
|
|
@@ -50,13 +60,14 @@ function table(k, compare, method) {
|
|
|
50
60
|
pos.forEach(([r, c], idx) => (out[r][c] = adj[idx]));
|
|
51
61
|
return out;
|
|
52
62
|
}
|
|
53
|
-
function result(method, levels, pValue, adjust) {
|
|
63
|
+
function result(method, levels, pValue, adjust, warnings = []) {
|
|
54
64
|
return {
|
|
55
65
|
method,
|
|
56
66
|
pValue,
|
|
57
67
|
rowNames: levels.slice(1).map(levelText),
|
|
58
68
|
colNames: levels.slice(0, -1).map(levelText),
|
|
59
69
|
pAdjustMethod: adjust,
|
|
70
|
+
warnings,
|
|
60
71
|
};
|
|
61
72
|
}
|
|
62
73
|
/**
|
|
@@ -75,6 +86,7 @@ function result(method, levels, pValue, adjust) {
|
|
|
75
86
|
* @category Multiple comparisons
|
|
76
87
|
*/
|
|
77
88
|
export function pairwiseTTest(x, g, options = {}) {
|
|
89
|
+
checkOptions(options, PAIRWISE_TTEST_KEYS, "pairwiseTTest"); // generated: option-keys.mjs
|
|
78
90
|
const paired = options.paired ?? false;
|
|
79
91
|
const poolSd = options.poolSd ?? !paired;
|
|
80
92
|
if (paired && poolSd)
|
|
@@ -90,16 +102,16 @@ export function pairwiseTTest(x, g, options = {}) {
|
|
|
90
102
|
if (poolSd) {
|
|
91
103
|
method = "t tests with pooled SD";
|
|
92
104
|
const n = groups.map((s) => s.length);
|
|
93
|
-
|
|
94
|
-
|
|
105
|
+
// moments of the data scaled by a power of 2 (exact), so that the variances stay in range; t has no units
|
|
106
|
+
const gm = groupMoments(groups);
|
|
107
|
+
const means = gm.mean;
|
|
95
108
|
let df = 0;
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
const sd = Math.sqrt(ss / df);
|
|
109
|
+
for (const m of n)
|
|
110
|
+
if (m > 1)
|
|
111
|
+
df += m - 1;
|
|
112
|
+
// every group constant: an error, as tukeyTest (R's p-values 0, or NaN for equal means)
|
|
113
|
+
checkPooledVariance(df, gm.ssWithin);
|
|
114
|
+
const sd = Math.sqrt(gm.ssWithin / df);
|
|
103
115
|
compare = (i, j) => {
|
|
104
116
|
const t = (means[i] - means[j]) / (sd * Math.sqrt(1 / n[i] + 1 / n[j]));
|
|
105
117
|
if (alternative === "less")
|
|
@@ -111,7 +123,7 @@ export function pairwiseTTest(x, g, options = {}) {
|
|
|
111
123
|
}
|
|
112
124
|
else {
|
|
113
125
|
method = paired ? "paired t tests" : "t tests with non-pooled SD";
|
|
114
|
-
compare = (i, j) => tTest(groups[i], groups[j], { paired, alternative }).pValue;
|
|
126
|
+
compare = (i, j) => unreported(() => tTest(groups[i], groups[j], { paired, alternative })).pValue;
|
|
115
127
|
}
|
|
116
128
|
return result(method, levels, table(k, compare, adjust), adjust);
|
|
117
129
|
}
|
|
@@ -129,6 +141,7 @@ export function pairwiseTTest(x, g, options = {}) {
|
|
|
129
141
|
* @category Multiple comparisons
|
|
130
142
|
*/
|
|
131
143
|
export function pairwiseWilcoxTest(x, g, options = {}) {
|
|
144
|
+
checkOptions(options, PAIRWISE_WILCOX_TEST_KEYS, "pairwiseWilcoxTest"); // generated: option-keys.mjs
|
|
132
145
|
const { pAdjustMethod, alternative, ...rest } = options;
|
|
133
146
|
const adjust = pAdjustMethod ?? "holm";
|
|
134
147
|
const { levels, groups } = byLevel(x, g, !(rest.paired ?? false));
|
|
@@ -136,12 +149,16 @@ export function pairwiseWilcoxTest(x, g, options = {}) {
|
|
|
136
149
|
if (k < 2)
|
|
137
150
|
throw new RangeError("there must be at least two groups");
|
|
138
151
|
let method = "";
|
|
152
|
+
// the comparisons' warnings, each once, in the order of the comparisons (R repeats them for each)
|
|
153
|
+
const warnings = new Warnings();
|
|
139
154
|
const pv = table(k, (i, j) => {
|
|
140
|
-
const r = wilcoxTest(groups[i], groups[j], { ...rest, ...(alternative === undefined ? {} : { alternative }) });
|
|
155
|
+
const r = unreported(() => wilcoxTest(groups[i], groups[j], { ...rest, ...(alternative === undefined ? {} : { alternative }) }));
|
|
141
156
|
method = r.method;
|
|
157
|
+
for (const w of r.warnings)
|
|
158
|
+
warnings.add(w);
|
|
142
159
|
return r.pValue;
|
|
143
160
|
}, adjust);
|
|
144
|
-
return result(method, levels, pv, adjust);
|
|
161
|
+
return reported("pairwiseWilcoxTest", result(method, levels, pv, adjust, warnings.list));
|
|
145
162
|
}
|
|
146
163
|
/**
|
|
147
164
|
* Pairwise two-group tests of equal proportions, with adjusted p-values. Equivalent to R's `pairwise.prop.test()`.
|
|
@@ -158,6 +175,7 @@ export function pairwiseWilcoxTest(x, g, options = {}) {
|
|
|
158
175
|
* @category Multiple comparisons
|
|
159
176
|
*/
|
|
160
177
|
export function pairwisePropTest(x, n, options = {}) {
|
|
178
|
+
checkOptions(options, PAIRWISE_PROP_TEST_KEYS, "pairwisePropTest"); // generated: option-keys.mjs
|
|
161
179
|
if (x.length !== n.length)
|
|
162
180
|
throw new RangeError("'x' and 'n' must have the same length");
|
|
163
181
|
// as in R, groups with a missing count are dropped and the rest numbered 1, 2, ... in their order
|
|
@@ -174,7 +192,14 @@ export function pairwisePropTest(x, n, options = {}) {
|
|
|
174
192
|
throw new RangeError("too few groups");
|
|
175
193
|
const adjust = options.pAdjustMethod ?? "holm";
|
|
176
194
|
const correct = options.correct ?? true;
|
|
177
|
-
|
|
195
|
+
// the comparisons' warnings ("Chi-squared approximation may be incorrect"), each once (R repeats them for each)
|
|
196
|
+
const warnings = new Warnings();
|
|
197
|
+
const pv = table(k, (i, j) => {
|
|
198
|
+
const r = unreported(() => propTest([xs[i], xs[j]], [ns[i], ns[j]], { correct }));
|
|
199
|
+
for (const w of r.warnings)
|
|
200
|
+
warnings.add(w);
|
|
201
|
+
return r.pValue;
|
|
202
|
+
}, adjust);
|
|
178
203
|
const levels = Array.from({ length: k }, (_, i) => i + 1);
|
|
179
|
-
return result("Pairwise comparison of proportions", levels, pv, adjust);
|
|
204
|
+
return reported("pairwisePropTest", result("Pairwise comparison of proportions", levels, pv, adjust, warnings.list));
|
|
180
205
|
}
|
|
@@ -5,7 +5,7 @@ import { type Alternative, type HTest } from "./types.js";
|
|
|
5
5
|
* @category Hypothesis tests
|
|
6
6
|
*/
|
|
7
7
|
export interface PoissonTestOptions {
|
|
8
|
-
/** Time base(s): one per count,
|
|
8
|
+
/** Time base(s): one per count, positive and finite (R accepts 0 and returns NaN or Inf; an error here). Default `1` (or `[1, 1]`). */
|
|
9
9
|
T?: number | readonly number[];
|
|
10
10
|
/** Hypothesized rate, or rate ratio for two samples (nonnegative). Default `1`. */
|
|
11
11
|
r?: number;
|
|
@@ -8,12 +8,14 @@
|
|
|
8
8
|
// p / (1 - p) T2 / T1.
|
|
9
9
|
import { dpois1, ppois1 } from "../distributions/poisson.js";
|
|
10
10
|
import { qgamma1 } from "../distributions/gamma.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { firstTrue, lastTrue } from "../internal/discrete.js";
|
|
12
|
+
import { ldexp } from "../internal/summary.js";
|
|
13
|
+
import { binomTestCounts } from "./binomTest.js";
|
|
14
|
+
import { checkConfLevel, checkConfLevelOpen, isCount, matchAlternative } from "./types.js";
|
|
15
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
16
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
17
|
+
const POISSON_TEST_KEYS = ["T", "r", "alternative", "confLevel"];
|
|
13
18
|
const REL_ERR = 1 + 1e-7;
|
|
14
|
-
function isCount(v) {
|
|
15
|
-
return Number.isFinite(v) && v >= 0 && Math.abs(v - Math.round(v)) <= 1e-7 * Math.max(1, Math.abs(v));
|
|
16
|
-
}
|
|
17
19
|
/**
|
|
18
20
|
* Two-sided p-value of the count x under Poisson(m): the total probability of the counts whose probability is at most
|
|
19
21
|
* (1 + 1e-7) times that of x. The density is unimodal (increasing up to floor(m), decreasing after), so the observed
|
|
@@ -29,48 +31,73 @@ function twoSidedP(x, m) {
|
|
|
29
31
|
const qualifies = (i) => dpois1(i, m, false) <= cut;
|
|
30
32
|
const mode = Math.floor(m);
|
|
31
33
|
if (x < m) {
|
|
32
|
-
// smallest j >=
|
|
33
|
-
// and m are equally likely, so starting at x + 1 picks up m when x = m - 1)
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
if (!qualifies(start)) {
|
|
37
|
-
let fails = start; // largest count known not to qualify
|
|
38
|
-
let step = 1;
|
|
39
|
-
j = start + 1;
|
|
40
|
-
while (!qualifies(j)) {
|
|
41
|
-
fails = j;
|
|
42
|
-
step *= 2;
|
|
43
|
-
j = start + step;
|
|
44
|
-
}
|
|
45
|
-
while (j - fails > 1) {
|
|
46
|
-
const mid = fails + Math.floor((j - fails) / 2);
|
|
47
|
-
if (qualifies(mid))
|
|
48
|
-
j = mid;
|
|
49
|
-
else
|
|
50
|
-
fails = mid;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return Math.min(1, ppois1(x, m, true, false) + ppois1(j - 1, m, false, false));
|
|
54
|
-
}
|
|
55
|
-
// x > m: largest j <= end with dpois(j) <= cut; densities increase from 0 up to the mode (j = -1 when none does)
|
|
56
|
-
const end = Math.min(x - 1, mode);
|
|
57
|
-
let j;
|
|
58
|
-
if (!qualifies(0))
|
|
59
|
-
j = -1;
|
|
60
|
-
else if (qualifies(end))
|
|
61
|
-
j = end;
|
|
62
|
-
else {
|
|
63
|
-
j = 0;
|
|
64
|
-
let fails = end; // smallest count known not to qualify
|
|
65
|
-
while (fails - j > 1) {
|
|
66
|
-
const mid = j + Math.floor((fails - j) / 2);
|
|
67
|
-
if (qualifies(mid))
|
|
68
|
-
j = mid;
|
|
69
|
-
else
|
|
70
|
-
fails = mid;
|
|
71
|
-
}
|
|
34
|
+
// smallest j >= max(x + 1, mode) with dpois(j) <= cut; densities decrease from the mode onwards (with m an
|
|
35
|
+
// integer, m - 1 and m are equally likely, so starting at x + 1 picks up m when x = m - 1)
|
|
36
|
+
const j = firstTrue(qualifies, Math.max(x + 1, mode), Infinity);
|
|
37
|
+
return Math.min(1, ppois1(x, m, true, false) + atLeast(j, m));
|
|
72
38
|
}
|
|
73
|
-
|
|
39
|
+
// x > m: largest j <= min(x - 1, mode) with dpois(j) <= cut; densities increase from 0 up to the mode (j = -1
|
|
40
|
+
// when none does)
|
|
41
|
+
const j = lastTrue(qualifies, 0, Math.min(x - 1, mode));
|
|
42
|
+
return Math.min(1, ppois1(j, m, true, false) + atLeast(x, m));
|
|
43
|
+
}
|
|
44
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
45
|
+
/**
|
|
46
|
+
* P[X >= x] under Poisson(m). Where x - 1 is not exact (x beyond 2^53), P[X > x] + dpois(x): ppois(x - 1, upper)
|
|
47
|
+
* rounded x - 1 and dropped or added a point mass (x = 2^53 + 2, m = x gave 0.5000000056047, true 0.5000000014012;
|
|
48
|
+
* issue #323)
|
|
49
|
+
*/
|
|
50
|
+
function atLeast(x, m) {
|
|
51
|
+
return x - (x - 1) === 1 ? ppois1(x - 1, m, false, false) : ppois1(x, m, false, false) + dpois1(x, m, false);
|
|
52
|
+
}
|
|
53
|
+
/** [f, e] with v = f 2^e, f in [1, 2) up to the rounding of log2 (v > 0 finite, subnormal v included; exact) */
|
|
54
|
+
function frexp(v) {
|
|
55
|
+
const s = v < 2 ** -1000 ? 600 : 0;
|
|
56
|
+
const e = Math.floor(Math.log2(v * 2 ** s));
|
|
57
|
+
return [v * 2 ** s * 2 ** -e, e - s];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* n r T1 / (T1 + r T2), with the ratio T2 / T1 where the products overflow, fall below the normal range or underflow
|
|
61
|
+
* to 0. Where that
|
|
62
|
+
* overflows too (n r, or r T2 / T1; #223: n = 1e308, r = 2, T = (1, 1e308) gave Inf / Inf), from the mantissas of n, r,
|
|
63
|
+
* T1 and T2 with their powers of 2 applied last
|
|
64
|
+
*/
|
|
65
|
+
function expectedCount1(n, r, T1, T2) {
|
|
66
|
+
const den = T1 + r * T2;
|
|
67
|
+
const v = (n * r * T1) / den;
|
|
68
|
+
// (a 0 from positive n and r is n r T1 underflowing, not the value: n = T2 = MAX_VALUE, r = T1 = MIN_VALUE gave 0
|
|
69
|
+
// for MIN_VALUE; issue #481)
|
|
70
|
+
if (Number.isFinite(v) && Number.isFinite(den) && den >= MIN_NORMAL && (v !== 0 || n === 0 || r === 0))
|
|
71
|
+
return v;
|
|
72
|
+
const nr = n * r;
|
|
73
|
+
const den1 = 1 + r * (T2 / T1);
|
|
74
|
+
// (n or r of 0: 0, as R's n r T1 / (T1 + r T2); 0 r (T2 / T1) here was 0 Inf = NaN)
|
|
75
|
+
if (n === 0 || r === 0)
|
|
76
|
+
return 0;
|
|
77
|
+
if (Number.isFinite(nr) && Number.isFinite(den1))
|
|
78
|
+
return nr / den1;
|
|
79
|
+
const [fn, en] = frexp(n);
|
|
80
|
+
const [fr, er] = frexp(r);
|
|
81
|
+
const [f1, e1] = frexp(T1);
|
|
82
|
+
const [f2, e2] = frexp(T2);
|
|
83
|
+
// T1 / (T1 + r T2) = f1 / (f1 + f 2^-d) = 2^d f1 / (2^d f1 + f), with f = fr f2 in [1, 4) and d = e1 - er - e2; the
|
|
84
|
+
// power of 2 goes on the term it makes smaller (2^-d for d >= 0, 2^d otherwise), so g is in (0, 2) and fn fr g < 8
|
|
85
|
+
const f = fr * f2;
|
|
86
|
+
const d = e1 - er - e2;
|
|
87
|
+
const g = d >= 0 ? f1 / (f1 + ldexp(f, -d)) : f1 / (ldexp(f1, d) + f);
|
|
88
|
+
return ldexp(fn * fr * g, en + er + Math.min(d, 0));
|
|
89
|
+
}
|
|
90
|
+
/** (x1 / T1) / (x2 / T2), as (x1 / x2) (T2 / T1) where the rates overflow or fall below the normal range */
|
|
91
|
+
function rateRatio(x1, x2, T1, T2) {
|
|
92
|
+
const r1 = x1 / T1;
|
|
93
|
+
const r2 = x2 / T2;
|
|
94
|
+
const v = r1 / r2;
|
|
95
|
+
if (x2 === 0 || (Number.isFinite(r1) && Number.isFinite(r2) && r2 >= MIN_NORMAL && (r1 === 0 || r1 >= MIN_NORMAL)))
|
|
96
|
+
return v;
|
|
97
|
+
// (x1 of 0: 0; 0 (T2 / T1) was 0 Inf = NaN for T = (5e-324, 1e308); issue #325)
|
|
98
|
+
if (x1 === 0)
|
|
99
|
+
return 0;
|
|
100
|
+
return (x1 / x2) * (T2 / T1);
|
|
74
101
|
}
|
|
75
102
|
/**
|
|
76
103
|
* Exact test of a Poisson rate, or comparison of two Poisson rates. Equivalent to R's `poisson.test()`.
|
|
@@ -87,6 +114,7 @@ function twoSidedP(x, m) {
|
|
|
87
114
|
* @category Hypothesis tests
|
|
88
115
|
*/
|
|
89
116
|
export function poissonTest(x, options = {}) {
|
|
117
|
+
checkOptions(options, POISSON_TEST_KEYS, "poissonTest"); // generated: option-keys.mjs
|
|
90
118
|
const xs = typeof x === "number" ? [x] : Array.from(x);
|
|
91
119
|
if (xs.length < 1 || xs.length > 2)
|
|
92
120
|
throw new RangeError("incorrect length of 'x'");
|
|
@@ -98,39 +126,67 @@ export function poissonTest(x, options = {}) {
|
|
|
98
126
|
const T = typeof Traw === "number" ? new Array(k).fill(Traw) : Array.from(Traw);
|
|
99
127
|
if (T.length !== k)
|
|
100
128
|
throw new RangeError("'x' and 'T' have incompatible length");
|
|
101
|
-
//
|
|
102
|
-
|
|
103
|
-
|
|
129
|
+
// a time base of 0 defines no rate (R accepts it and returns NaN or Infinity for the estimate and the interval); a
|
|
130
|
+
// rate r of 0 is accepted, as in R (the counts then have mean 0 under the null hypothesis)
|
|
131
|
+
if (!T.every((t) => Number.isFinite(t) && t > 0))
|
|
132
|
+
throw new RangeError("'T' must be positive");
|
|
104
133
|
const r = options.r ?? 1;
|
|
105
134
|
if (!(Number.isFinite(r) && r >= 0))
|
|
106
135
|
throw new RangeError("'r' must be a single positive number");
|
|
107
136
|
const alternative = matchAlternative(options.alternative);
|
|
108
137
|
const confLevel = options.confLevel ?? 0.95;
|
|
109
|
-
|
|
138
|
+
// as in R, the one-sample interval takes the closed range (at 1 it is [0, Inf)); the two-sample one goes through
|
|
139
|
+
// binomTest, which needs 0 < confLevel < 1
|
|
140
|
+
if (k === 2)
|
|
141
|
+
checkConfLevelOpen(confLevel);
|
|
142
|
+
else
|
|
143
|
+
checkConfLevel(confLevel);
|
|
110
144
|
if (k === 2) {
|
|
111
145
|
const [x1, x2] = counts;
|
|
112
146
|
const [T1, T2] = T;
|
|
113
147
|
const n = x1 + x2;
|
|
114
|
-
//
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
148
|
+
// (two counts whose sum overflows: R stops; binomTest would have n = Infinity)
|
|
149
|
+
if (n === Infinity)
|
|
150
|
+
throw new RangeError("'x' must be finite, nonnegative, and integer");
|
|
151
|
+
// Two counts of 0 give p-value 1 and the interval [0, 1] as in R, where binomTest([0, 0]) and binomTest(0, 0) are
|
|
152
|
+
// errors. Where r T1 + T2 overflows or is not a normal number, the ratio of the time bases instead
|
|
153
|
+
// (T = c(1e308, 1e308) gave p0 = 0 and a p-value of 0; R too)
|
|
154
|
+
const a = r * T1;
|
|
155
|
+
const den = a + T2;
|
|
156
|
+
const p0 = Number.isFinite(den) && den >= MIN_NORMAL && (a === 0 || a >= MIN_NORMAL) ? a / den : 1 / (1 + T2 / T1 / r);
|
|
157
|
+
const b = binomTestCounts(Math.round(x1), Math.round(x1) + Math.round(x2), { p: p0, alternative, confLevel });
|
|
158
|
+
// (a limit p of 0 is the ratio 0, also where T2 / T1 overflows: 0 Inf was NaN; issue #325)
|
|
159
|
+
// Where T2 / T1 overflows, the odds p / (1 - p) and T2 / T1 from their mantissas with the powers of 2 applied last
|
|
160
|
+
// (a subnormal limit p for a count of about MAX_VALUE: T = (1e-12, MAX_VALUE) gave [Inf, Inf] for [2.5e10, 5.6e12];
|
|
161
|
+
// issue #482)
|
|
162
|
+
const t21 = T2 / T1;
|
|
163
|
+
const toRatio = (p) => {
|
|
164
|
+
if (p === 0)
|
|
165
|
+
return 0;
|
|
166
|
+
const odds = p / (1 - p);
|
|
167
|
+
if (Number.isFinite(t21) || odds === Infinity)
|
|
168
|
+
return odds * t21;
|
|
169
|
+
const [fo, eo] = frexp(odds);
|
|
170
|
+
const [f1, e1] = frexp(T1);
|
|
171
|
+
const [f2, e2] = frexp(T2);
|
|
172
|
+
return ldexp((fo * f2) / f1, eo + e2 - e1);
|
|
173
|
+
};
|
|
119
174
|
return {
|
|
120
175
|
method: "Comparison of Poisson rates",
|
|
121
176
|
alternative,
|
|
122
177
|
statistic: x1,
|
|
123
178
|
statisticName: "count1",
|
|
124
179
|
// R reports sum(x) r T1 / (T1 + r T2) here (r on the other time base than in the binomial probability)
|
|
125
|
-
parameter: (n
|
|
180
|
+
parameter: expectedCount1(n, r, T1, T2),
|
|
126
181
|
parameterName: "expected count1",
|
|
127
182
|
pValue: b.pValue,
|
|
128
183
|
confInt: [toRatio(b.confInt[0]), toRatio(b.confInt[1])],
|
|
129
184
|
confLevel,
|
|
130
|
-
estimate: x1
|
|
185
|
+
estimate: rateRatio(x1, x2, T1, T2),
|
|
131
186
|
estimateNames: "rate ratio",
|
|
132
187
|
nullValue: r,
|
|
133
188
|
nullValueName: "rate ratio",
|
|
189
|
+
warnings: [],
|
|
134
190
|
};
|
|
135
191
|
}
|
|
136
192
|
const xv = counts[0];
|
|
@@ -140,11 +196,12 @@ export function poissonTest(x, options = {}) {
|
|
|
140
196
|
if (alternative === "less")
|
|
141
197
|
pValue = ppois1(xv, m, true, false);
|
|
142
198
|
else if (alternative === "greater")
|
|
143
|
-
pValue =
|
|
199
|
+
pValue = atLeast(xv, m);
|
|
144
200
|
else
|
|
145
201
|
pValue = twoSidedP(xv, m);
|
|
146
202
|
const lower = (alpha) => (xv === 0 ? 0 : qgamma1(alpha, xv, 1, true, false));
|
|
147
|
-
|
|
203
|
+
// (for alpha below 1e-8 the upper probability itself, as in binomTest: 1 - alpha rounds to 1 below 1.1e-16)
|
|
204
|
+
const upper = (alpha) => alpha < 1e-8 ? qgamma1(alpha, xv + 1, 1, false, false) : qgamma1(1 - alpha, xv + 1, 1, true, false);
|
|
148
205
|
let ci;
|
|
149
206
|
if (alternative === "less")
|
|
150
207
|
ci = [0, upper(1 - confLevel)];
|
|
@@ -168,5 +225,6 @@ export function poissonTest(x, options = {}) {
|
|
|
168
225
|
estimateNames: "event rate",
|
|
169
226
|
nullValue: r,
|
|
170
227
|
nullValueName: "event rate",
|
|
228
|
+
warnings: [],
|
|
171
229
|
};
|
|
172
230
|
}
|