@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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Multiple comparisons after a one-way layout: PMCMRplus's dscfAllPairsTest, kwAllPairsDunnTest, kwManyOneDunnTest,
|
|
2
|
-
// dunnettTest, gamesHowellTest, tukeyTest
|
|
2
|
+
// dunnettTest, gamesHowellTest, tukeyTest and manyOneUTest, dunn.test's dunn.test and multcomp's Dunnett contrasts
|
|
3
3
|
// (summary(glht(aov(y ~ g), mcp(g = "Dunnett")))).
|
|
4
4
|
//
|
|
5
5
|
// Written for rStats.js from the published statistics and the behaviour described in oracle/mcomp-behaviour.md (no
|
|
@@ -16,12 +16,21 @@
|
|
|
16
16
|
// - Tukey (1953), Tukey-Kramer: the pooled standard error, studentized range on N - k degrees of freedom.
|
|
17
17
|
import { pnorm } from "../distributions/normal.js";
|
|
18
18
|
import { ptukey } from "../distributions/tukey.js";
|
|
19
|
-
import {
|
|
19
|
+
import { groupMoments, ldexp, rank, satterthwaiteDfPM, tieCounts } from "../internal/summary.js";
|
|
20
20
|
import { pDunnettUpper } from "./dunnett.js";
|
|
21
21
|
import { labelledGroups } from "./groups.js";
|
|
22
22
|
import { pAdjust } from "./pAdjust.js";
|
|
23
23
|
import { kruskalTest } from "./rankTests.js";
|
|
24
24
|
import { matchAlternative } from "./types.js";
|
|
25
|
+
import { reported } from "../internal/warnings.js";
|
|
26
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
27
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
28
|
+
const KW_ALL_PAIRS_DUNN_TEST_KEYS = ["pAdjustMethod"];
|
|
29
|
+
const KW_MANY_ONE_DUNN_TEST_KEYS = ["pAdjustMethod", "alternative"];
|
|
30
|
+
const DUNNETT_TEST_KEYS = ["alternative"];
|
|
31
|
+
const MANY_ONE_UTEST_KEYS = ["pAdjustMethod", "alternative"];
|
|
32
|
+
const DUNN_TEST_KEYS = ["method", "altp"];
|
|
33
|
+
const GLHT_DUNNETT_KEYS = ["alternative"];
|
|
25
34
|
const TIES_WARNING = "Ties are present. z-quantiles were corrected for ties.";
|
|
26
35
|
function layout(x, g) {
|
|
27
36
|
const { labels, groups } = labelledGroups(x, g);
|
|
@@ -96,17 +105,29 @@ function manyOne(d, method, statistic, pValue, adjust, warnings = []) {
|
|
|
96
105
|
}
|
|
97
106
|
const upperNormal = (z) => pnorm(z, { lowerTail: false });
|
|
98
107
|
const upperRange = (q, k, df) => ptukey(Math.abs(q), k, df, { lowerTail: false });
|
|
99
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Group means, and the pooled variance sum (n_i - 1) s_i^2 / sum (n_i - 1) with its degrees of freedom, of the data
|
|
110
|
+
* scaled by 2^-k (exact; the variances then stay in range). Means and standard errors are multiplied by 2^k for output
|
|
111
|
+
*/
|
|
100
112
|
function pooled(d) {
|
|
101
|
-
|
|
113
|
+
const gm = groupMoments(d.groups);
|
|
102
114
|
let df = 0;
|
|
103
|
-
for (const
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
115
|
+
for (const n of gm.n)
|
|
116
|
+
if (n > 1)
|
|
117
|
+
df += n - 1;
|
|
118
|
+
checkPooledVariance(df, gm.ssWithin);
|
|
119
|
+
return { m: gm.mean, s2: gm.ssWithin / df, df: d.N - d.k, k: gm.k };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* No inference without a residual variance (the statistics were +-Infinity, or NaN for equal means): the pooled
|
|
123
|
+
* comparisons here, pairwiseTTest with a pooled SD and onewayTest with equal variances. The sum of squares
|
|
124
|
+
* (groupMoments) is double-double, so it is exactly 0 only when every group is constant
|
|
125
|
+
*/
|
|
126
|
+
export function checkPooledVariance(df, ssWithin) {
|
|
127
|
+
if (df === 0)
|
|
128
|
+
throw new RangeError("no residual degrees of freedom");
|
|
129
|
+
if (ssWithin === 0)
|
|
130
|
+
throw new RangeError("the pooled within-group variance is 0");
|
|
110
131
|
}
|
|
111
132
|
/**
|
|
112
133
|
* Dunn's all-pairs test of mean ranks with adjusted p-values. Equivalent to `PMCMRplus::kwAllPairsDunnTest()` in R.
|
|
@@ -124,13 +145,14 @@ function pooled(d) {
|
|
|
124
145
|
* @category Multiple comparisons
|
|
125
146
|
*/
|
|
126
147
|
export function kwAllPairsDunnTest(x, g, options = {}) {
|
|
148
|
+
checkOptions(options, KW_ALL_PAIRS_DUNN_TEST_KEYS, "kwAllPairsDunnTest"); // generated: option-keys.mjs
|
|
127
149
|
const adjust = options.pAdjustMethod ?? "holm";
|
|
128
150
|
const d = layout(x, g);
|
|
129
151
|
const { R, C } = meanRanks(d);
|
|
130
152
|
const v = (d.N * (d.N + 1)) / 12 - C;
|
|
131
153
|
const z = lowerTable(d.k, (i, j) => Math.abs(R[i] - R[j]) / Math.sqrt(v * (1 / d.n[i] + 1 / d.n[j])));
|
|
132
154
|
const p = adjustLower(z.map((row) => row.map((s) => 2 * upperNormal(s))), adjust);
|
|
133
|
-
return allPairs(d, "Dunn's all-pairs test", z, p, adjust, C !== 0 ? [TIES_WARNING] : []);
|
|
155
|
+
return reported("kwAllPairsDunnTest", allPairs(d, "Dunn's all-pairs test", z, p, adjust, C !== 0 ? [TIES_WARNING] : []));
|
|
134
156
|
}
|
|
135
157
|
/**
|
|
136
158
|
* Dunn's many-to-one test of mean ranks against the control (the first group). Equivalent to
|
|
@@ -147,6 +169,7 @@ export function kwAllPairsDunnTest(x, g, options = {}) {
|
|
|
147
169
|
* @category Multiple comparisons
|
|
148
170
|
*/
|
|
149
171
|
export function kwManyOneDunnTest(x, g, options = {}) {
|
|
172
|
+
checkOptions(options, KW_MANY_ONE_DUNN_TEST_KEYS, "kwManyOneDunnTest"); // generated: option-keys.mjs
|
|
150
173
|
const alternative = matchAlternative(options.alternative);
|
|
151
174
|
const adjust = options.pAdjustMethod ?? "single-step";
|
|
152
175
|
const d = layout(x, g);
|
|
@@ -156,7 +179,7 @@ export function kwManyOneDunnTest(x, g, options = {}) {
|
|
|
156
179
|
const z = idx.map((j) => (R[j] - R[0]) / Math.sqrt(v * (1 / d.n[0] + 1 / d.n[j])));
|
|
157
180
|
const lambda = idx.map((j) => d.n[j] / (d.n[j] + d.n[0]));
|
|
158
181
|
const p = manyOneNormalP(z, lambda, alternative, adjust);
|
|
159
|
-
return manyOne(d, "Dunn's many-to-one test", z, p, adjust, C !== 0 ? [TIES_WARNING] : []);
|
|
182
|
+
return reported("kwManyOneDunnTest", manyOne(d, "Dunn's many-to-one test", z, p, adjust, C !== 0 ? [TIES_WARNING] : []));
|
|
160
183
|
}
|
|
161
184
|
/**
|
|
162
185
|
* Many-to-one p-values of normal statistics z (one per group against the control): `"single-step"`, the exact
|
|
@@ -186,6 +209,7 @@ function manyOneNormalP(z, lambda, alternative, adjust) {
|
|
|
186
209
|
* @category Multiple comparisons
|
|
187
210
|
*/
|
|
188
211
|
export function dunnettTest(x, g, options = {}) {
|
|
212
|
+
checkOptions(options, DUNNETT_TEST_KEYS, "dunnettTest"); // generated: option-keys.mjs
|
|
189
213
|
const alternative = matchAlternative(options.alternative);
|
|
190
214
|
const d = layout(x, g);
|
|
191
215
|
const { m, s2, df } = pooled(d);
|
|
@@ -196,7 +220,7 @@ export function dunnettTest(x, g, options = {}) {
|
|
|
196
220
|
const p = t.map((v) => alternative === "two.sided"
|
|
197
221
|
? Math.min(1, 2 * pDunnettUpper(Math.abs(v), lambda, df, false))
|
|
198
222
|
: pDunnettUpper(alternative === "greater" ? v : -v, lambda, df, false));
|
|
199
|
-
return manyOne(d, "Dunnett's-test for multiple\t\n comparisons with one control", t, p, "single-step");
|
|
223
|
+
return reported("dunnettTest", manyOne(d, "Dunnett's-test for multiple\t\n comparisons with one control", t, p, "single-step"));
|
|
200
224
|
}
|
|
201
225
|
/**
|
|
202
226
|
* Steel's many-to-one rank test in its normal approximation: the Mann-Whitney statistic of each group against the
|
|
@@ -218,6 +242,7 @@ export function dunnettTest(x, g, options = {}) {
|
|
|
218
242
|
* @category Multiple comparisons
|
|
219
243
|
*/
|
|
220
244
|
export function manyOneUTest(x, g, options = {}) {
|
|
245
|
+
checkOptions(options, MANY_ONE_UTEST_KEYS, "manyOneUTest"); // generated: option-keys.mjs
|
|
221
246
|
const alternative = matchAlternative(options.alternative);
|
|
222
247
|
const adjust = options.pAdjustMethod ?? "single-step";
|
|
223
248
|
const d = layout(x, g);
|
|
@@ -246,7 +271,7 @@ export function manyOneUTest(x, g, options = {}) {
|
|
|
246
271
|
});
|
|
247
272
|
const lambda = idx.map((j) => d.n[j] / (d.n[j] + n1));
|
|
248
273
|
const p = manyOneNormalP(z, lambda, alternative, adjust);
|
|
249
|
-
return manyOne(d, "Wilcoxon, Mann, Whittney U-test\n\tfor multiple comparisons with one control", z, p, adjust);
|
|
274
|
+
return reported("manyOneUTest", manyOne(d, "Wilcoxon, Mann, Whittney U-test\n\tfor multiple comparisons with one control", z, p, adjust));
|
|
250
275
|
}
|
|
251
276
|
/**
|
|
252
277
|
* Dwass-Steel-Critchlow-Fligner all-pairs test of pairwise Mann-Whitney statistics. Equivalent to
|
|
@@ -282,7 +307,7 @@ export function dscfAllPairsTest(x, g) {
|
|
|
282
307
|
return (Math.SQRT2 * (u - (ni * nj) / 2)) / Math.sqrt(v);
|
|
283
308
|
});
|
|
284
309
|
const p = stat.map((row) => row.map((q) => (Number.isNaN(q) ? NaN : upperRange(q, d.k, Infinity))));
|
|
285
|
-
return allPairs(d, "Dwass-Steele-Critchlow-Fligner all-pairs test", stat, p, "single-step");
|
|
310
|
+
return reported("dscfAllPairsTest", allPairs(d, "Dwass-Steele-Critchlow-Fligner all-pairs test", stat, p, "single-step"));
|
|
286
311
|
}
|
|
287
312
|
/**
|
|
288
313
|
* Games-Howell all-pairs test of means with Welch's standard errors and degrees of freedom. Equivalent to
|
|
@@ -299,14 +324,16 @@ export function dscfAllPairsTest(x, g) {
|
|
|
299
324
|
*/
|
|
300
325
|
export function gamesHowellTest(x, g) {
|
|
301
326
|
const d = layout(x, g);
|
|
302
|
-
|
|
303
|
-
const
|
|
327
|
+
// moments of the data scaled by a power of 2 (exact), so that the variances stay in range; q and df have no units
|
|
328
|
+
const gm = groupMoments(d.groups);
|
|
329
|
+
const m = gm.mean;
|
|
330
|
+
const w = gm.variance.map((v, i) => v / d.n[i]);
|
|
304
331
|
const q = lowerTable(d.k, (i, j) => ((m[i] - m[j]) / Math.sqrt(w[i] + w[j])) * Math.SQRT2);
|
|
305
332
|
const p = lowerTable(d.k, (i, j) => {
|
|
306
|
-
const df = (
|
|
333
|
+
const df = satterthwaiteDfPM([gm.variance[i], gm.variance[j]], [d.n[i], d.n[j]]);
|
|
307
334
|
return upperRange(q[i - 1][j], d.k, df);
|
|
308
335
|
});
|
|
309
|
-
return allPairs(d, "Games-Howell test", q, p, "none");
|
|
336
|
+
return reported("gamesHowellTest", allPairs(d, "Games-Howell test", q, p, "none"));
|
|
310
337
|
}
|
|
311
338
|
/**
|
|
312
339
|
* Tukey's all-pairs test of means (Tukey-Kramer for unequal sizes). Equivalent to `PMCMRplus::tukeyTest()` in R.
|
|
@@ -323,47 +350,18 @@ export function gamesHowellTest(x, g) {
|
|
|
323
350
|
export function tukeyTest(x, g) {
|
|
324
351
|
const d = layout(x, g);
|
|
325
352
|
const { m, s2, df } = pooled(d);
|
|
353
|
+
// ptukey is defined from 2 df: with 1 the p-values were NaN (as TukeyHSD, #315)
|
|
354
|
+
if (df < 2)
|
|
355
|
+
throw new RangeError("the studentized range needs at least 2 residual degrees of freedom");
|
|
326
356
|
const q = lowerTable(d.k, (i, j) => (m[i] - m[j]) / Math.sqrt((s2 * (1 / d.n[i] + 1 / d.n[j])) / 2));
|
|
327
357
|
const p = q.map((row) => row.map((v) => (Number.isNaN(v) ? NaN : upperRange(v, d.k, df))));
|
|
328
|
-
return allPairs(d, "Tukey's test", q, p, "single-step");
|
|
329
|
-
}
|
|
330
|
-
/**
|
|
331
|
-
* Steel's many-to-one rank test. Equivalent to `PMCMRplus::steelTest()` in R. It behaves as `steelTest()` of
|
|
332
|
-
* PMCMRplus 1.9.12 does: that fails on every input, so this reproduces its errors. A two-sided
|
|
333
|
-
* alternative is rejected (`'arg' should be one of “greater”, “less”`), unequal group sizes give `Steel's test is
|
|
334
|
-
* only valid for balanced designs.`, and balanced data the error PMCMRplus raises internally.
|
|
335
|
-
*
|
|
336
|
-
* @throws Always.
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
339
|
-
* ```ts
|
|
340
|
-
* const x = [1.2, 2.3, 1.8, 2.9, 3.1, 3.6, 4.0, 4.4, 5.2];
|
|
341
|
-
* const g = ["a", "a", "a", "b", "b", "b", "c", "c", "c"];
|
|
342
|
-
* const message = (() => {
|
|
343
|
-
* try {
|
|
344
|
-
* steelTest(x, g, { alternative: "greater" });
|
|
345
|
-
* } catch (e) {
|
|
346
|
-
* return (e as Error).message;
|
|
347
|
-
* }
|
|
348
|
-
* })();
|
|
349
|
-
* message; // "'data' must be of a vector type, was 'NULL'"
|
|
350
|
-
* ```
|
|
351
|
-
*
|
|
352
|
-
* @category Multiple comparisons
|
|
353
|
-
*/
|
|
354
|
-
export function steelTest(x, g, options = {}) {
|
|
355
|
-
const alternative = options.alternative === undefined ? "greater" : matchAlternative(options.alternative);
|
|
356
|
-
if (alternative === "two.sided")
|
|
357
|
-
throw new RangeError("'arg' should be one of “greater”, “less”");
|
|
358
|
-
const d = layout(x, g);
|
|
359
|
-
if (d.n.some((v) => v !== d.n[0]))
|
|
360
|
-
throw new RangeError("Steel's test is only valid for balanced designs.");
|
|
361
|
-
throw new Error("'data' must be of a vector type, was 'NULL'");
|
|
358
|
+
return reported("tukeyTest", allPairs(d, "Tukey's test", q, p, "single-step"));
|
|
362
359
|
}
|
|
363
360
|
/**
|
|
364
361
|
* Dunn's test of all pairs of mean ranks. Equivalent to `dunn.test::dunn.test(x, g, method, altp)` in R. The step-wise
|
|
365
362
|
* adjustments multiply (or transform) each p-value by the factor of its rank and cap it at 1, without enforcing
|
|
366
|
-
* monotonicity, as dunn.test does (unlike `pAdjust`).
|
|
363
|
+
* monotonicity, as dunn.test does (unlike `pAdjust`). Equal p-values share the rank of the first of them in the
|
|
364
|
+
* ordering, so they get the same adjusted value (dunn.test ranks ties in the order of the comparisons).
|
|
367
365
|
*
|
|
368
366
|
* @example
|
|
369
367
|
* ```ts
|
|
@@ -377,6 +375,7 @@ export function steelTest(x, g, options = {}) {
|
|
|
377
375
|
* @category Multiple comparisons
|
|
378
376
|
*/
|
|
379
377
|
export function dunnTest(x, g, options = {}) {
|
|
378
|
+
checkOptions(options, DUNN_TEST_KEYS, "dunnTest"); // generated: option-keys.mjs
|
|
380
379
|
const method = options.method ?? "none";
|
|
381
380
|
const altp = options.altp ?? false;
|
|
382
381
|
const d = layout(x, g);
|
|
@@ -392,17 +391,21 @@ export function dunnTest(x, g, options = {}) {
|
|
|
392
391
|
}
|
|
393
392
|
}
|
|
394
393
|
const P = Z.map((z) => (altp ? 2 : 1) * upperNormal(Math.abs(z)));
|
|
395
|
-
|
|
394
|
+
const PAdjusted = dunnAdjust(P, method);
|
|
395
|
+
return { chi2, Z, P, PAdjusted, zValue: Z, pValue: P, pAdjusted: PAdjusted, comparisons, warnings: [] };
|
|
396
396
|
}
|
|
397
|
-
/** dunn.test's adjustments: each value by the factor of its rank, capped at 1 */
|
|
397
|
+
/** dunn.test's adjustments: each value by the factor of its rank (tied values: the same rank), capped at 1 */
|
|
398
398
|
function dunnAdjust(P, method) {
|
|
399
399
|
const m = P.length;
|
|
400
400
|
const sidakLike = (p, e) => Math.min(1, -Math.expm1(e * Math.log1p(-p)));
|
|
401
|
-
// i = 1..m: rank among the increasing (or decreasing) values
|
|
401
|
+
// i = 1..m: rank among the increasing (or decreasing) values. Tied values share the first rank of their run
|
|
402
|
+
// (dunn.test ranks them in the order of the comparisons, so equal p-values got different adjusted values that
|
|
403
|
+
// depended on the order of the groups). The first of a run is the one the stopping rule decides on, step-down
|
|
404
|
+
// (increasing) or step-up (decreasing), so the decisions are those of dunn.test
|
|
402
405
|
const ranks = (decreasing) => {
|
|
403
406
|
const order = P.map((_, k) => k).sort((a, b) => (decreasing ? P[b] - P[a] : P[a] - P[b]) || a - b);
|
|
404
407
|
const r = new Array(m);
|
|
405
|
-
order.forEach((k, pos) => (r[k] = pos + 1));
|
|
408
|
+
order.forEach((k, pos) => (r[k] = pos > 0 && P[k] === P[order[pos - 1]] ? r[order[pos - 1]] : pos + 1));
|
|
406
409
|
return r;
|
|
407
410
|
};
|
|
408
411
|
switch (method) {
|
|
@@ -457,17 +460,20 @@ function dunnAdjust(P, method) {
|
|
|
457
460
|
* @category Multiple comparisons
|
|
458
461
|
*/
|
|
459
462
|
export function glhtDunnett(x, g, options = {}) {
|
|
463
|
+
checkOptions(options, GLHT_DUNNETT_KEYS, "glhtDunnett"); // generated: option-keys.mjs
|
|
460
464
|
const alternative = matchAlternative(options.alternative);
|
|
461
465
|
const d = layout(x, g);
|
|
462
|
-
const { m, s2, df } = pooled(d);
|
|
466
|
+
const { m, s2, df, k } = pooled(d);
|
|
463
467
|
const s = Math.sqrt(s2);
|
|
464
468
|
const idx = d.n.slice(1).map((_, i) => i + 1);
|
|
465
|
-
const
|
|
466
|
-
const
|
|
467
|
-
const t =
|
|
469
|
+
const est = idx.map((j) => m[j] - m[0]);
|
|
470
|
+
const seS = idx.map((j) => s * Math.sqrt(1 / d.n[j] + 1 / d.n[0]));
|
|
471
|
+
const t = est.map((e, i) => e / seS[i]);
|
|
472
|
+
const estimate = est.map((v) => ldexp(v, k));
|
|
473
|
+
const se = seS.map((v) => ldexp(v, k));
|
|
468
474
|
const lambda = idx.map((j) => d.n[j] / (d.n[j] + d.n[0]));
|
|
469
475
|
const p = t.map((v) => alternative === "two.sided"
|
|
470
476
|
? pDunnettUpper(v, lambda, df, true)
|
|
471
477
|
: pDunnettUpper(alternative === "greater" ? v : -v, lambda, df, false));
|
|
472
|
-
return { names: idx.map((j) => `${d.labels[j]} - ${d.labels[0]}`), estimate, se, t, p, df };
|
|
478
|
+
return { names: idx.map((j) => `${d.labels[j]} - ${d.labels[0]}`), estimate, se, t, p, stdError: se, tValue: t, pValue: p, df, warnings: [] };
|
|
473
479
|
}
|
|
@@ -8,16 +8,30 @@
|
|
|
8
8
|
// the Shapiro-Francia W), and the chi-square test on classes of equal probability.
|
|
9
9
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
10
10
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
11
|
-
import { covariance, dropNaN,
|
|
11
|
+
import { covariance, dropNaN, scaleToUnit, standardize, variance } from "../internal/summary.js";
|
|
12
|
+
import { reported } from "../internal/warnings.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 PEARSON_TEST_KEYS = ["nClasses", "adjust"];
|
|
12
16
|
/** The sorted values without NaN */
|
|
13
17
|
function sorted(x) {
|
|
14
18
|
return dropNaN(Array.from(x)).sort((a, b) => a - b);
|
|
15
19
|
}
|
|
20
|
+
/** Rejects infinite values (the sorted values without NaN: only the ends can be infinite) */
|
|
21
|
+
function requireFinite(xs) {
|
|
22
|
+
if (xs.length > 0 && !(Number.isFinite(xs[0]) && Number.isFinite(xs[xs.length - 1])))
|
|
23
|
+
throw new RangeError("'x' must be finite");
|
|
24
|
+
}
|
|
16
25
|
/** The sorted values standardised by their mean and standard deviation */
|
|
17
26
|
function standardized(xs) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
// from the deviations of the values scaled by a power of 2 first (src/internal/summary.ts): no overflow or
|
|
28
|
+
// underflow at any scale. An infinite value is an error, as in shapiroTest: its standardised values are NaN and
|
|
29
|
+
// the piecewise p-values fell through to a fixed tail value (#304)
|
|
30
|
+
requireFinite(xs);
|
|
31
|
+
const z = standardize(xs);
|
|
32
|
+
if (z.some(Number.isNaN))
|
|
33
|
+
throw new RangeError("all 'x' values are identical");
|
|
34
|
+
return z;
|
|
21
35
|
}
|
|
22
36
|
/**
|
|
23
37
|
* Anderson-Darling test for normality. Equivalent to `nortest::ad.test()` in R. `NaN` values are dropped; at least 8
|
|
@@ -53,7 +67,7 @@ export function adTest(x) {
|
|
|
53
67
|
pValue = Math.exp(1.2937 - 5.709 * aa + 0.0186 * aa ** 2);
|
|
54
68
|
else
|
|
55
69
|
pValue = 3.7e-24;
|
|
56
|
-
return { method: "Anderson-Darling normality test", statistic: a, statisticName: "A", pValue };
|
|
70
|
+
return { method: "Anderson-Darling normality test", statistic: a, statisticName: "A", pValue, warnings: [] };
|
|
57
71
|
}
|
|
58
72
|
/**
|
|
59
73
|
* Cramer-von Mises test for normality. Equivalent to `nortest::cvm.test()` in R. `NaN` values are dropped; at least 8
|
|
@@ -92,7 +106,7 @@ export function cvmTest(x) {
|
|
|
92
106
|
pValue = 7.37e-10;
|
|
93
107
|
warnings.push("p-value is smaller than 7.37e-10, cannot be computed more accurately");
|
|
94
108
|
}
|
|
95
|
-
return { method: "Cramer-von Mises normality test", statistic: w, statisticName: "W", pValue, warnings };
|
|
109
|
+
return reported("cvmTest", { method: "Cramer-von Mises normality test", statistic: w, statisticName: "W", pValue, warnings });
|
|
96
110
|
}
|
|
97
111
|
/**
|
|
98
112
|
* Lilliefors (Kolmogorov-Smirnov) test for normality. Equivalent to `nortest::lillie.test()` in R. `NaN` values are
|
|
@@ -135,7 +149,7 @@ export function lillieTest(x) {
|
|
|
135
149
|
else
|
|
136
150
|
pValue = 0;
|
|
137
151
|
}
|
|
138
|
-
return { method: "Lilliefors (Kolmogorov-Smirnov) normality test", statistic: d, statisticName: "D", pValue };
|
|
152
|
+
return { method: "Lilliefors (Kolmogorov-Smirnov) normality test", statistic: d, statisticName: "D", pValue, warnings: [] };
|
|
139
153
|
}
|
|
140
154
|
/**
|
|
141
155
|
* Shapiro-Francia test for normality. Equivalent to `nortest::sf.test()` in R. `NaN` values are dropped; the sample
|
|
@@ -154,14 +168,21 @@ export function sfTest(x) {
|
|
|
154
168
|
const n = xs.length;
|
|
155
169
|
if (n < 5 || n > 5000)
|
|
156
170
|
throw new RangeError("sample size must be between 5 and 5000");
|
|
171
|
+
requireFinite(xs);
|
|
172
|
+
if (xs[0] === xs[n - 1])
|
|
173
|
+
throw new RangeError("all 'x' values are identical");
|
|
157
174
|
const q = xs.map((_, k) => qnorm((k + 1 - 3 / 8) / (n + 1 / 4)));
|
|
158
|
-
|
|
175
|
+
// the values scaled by a power of 2 (exact) first, so that the squared covariance stays in range (W has no units)
|
|
176
|
+
const xu = scaleToUnit([xs]).groups[0];
|
|
177
|
+
// a squared correlation: the three moments are rounded separately, so the quotient can exceed 1 by an ulp for data
|
|
178
|
+
// almost affine in the scores, where log(1 - W) was NaN (#384); at most 1, as R's cor()
|
|
179
|
+
const w = Math.min(covariance(xu, q) ** 2 / (variance(xu) * variance(q)), 1);
|
|
159
180
|
const u = Math.log(n);
|
|
160
181
|
const v = Math.log(u);
|
|
161
182
|
const mu = -1.2725 + 1.0521 * (v - u);
|
|
162
183
|
const sig = 1.0308 - 0.26758 * (v + 2 / u);
|
|
163
184
|
const pValue = pnorm((Math.log(1 - w) - mu) / sig, { lowerTail: false });
|
|
164
|
-
return { method: "Shapiro-Francia normality test", statistic: w, statisticName: "W", pValue };
|
|
185
|
+
return { method: "Shapiro-Francia normality test", statistic: w, statisticName: "W", pValue, warnings: [] };
|
|
165
186
|
}
|
|
166
187
|
/**
|
|
167
188
|
* Pearson chi-square test for normality, with classes of equal probability under the fitted normal distribution.
|
|
@@ -176,23 +197,42 @@ export function sfTest(x) {
|
|
|
176
197
|
* @category Hypothesis tests
|
|
177
198
|
*/
|
|
178
199
|
export function pearsonTest(x, options = {}) {
|
|
200
|
+
checkOptions(options, PEARSON_TEST_KEYS, "pearsonTest"); // generated: option-keys.mjs
|
|
179
201
|
const xs = sorted(x);
|
|
180
202
|
const n = xs.length;
|
|
181
203
|
const k = options.nClasses ?? Math.ceil(2 * n ** (2 / 5));
|
|
182
204
|
const adjust = options.adjust ?? true;
|
|
183
|
-
|
|
205
|
+
if (!(Number.isInteger(k) && k - 1 - (adjust ? 2 : 0) > 0))
|
|
206
|
+
throw new RangeError("'nClasses' must be an integer leaving positive degrees of freedom");
|
|
207
|
+
// R's tabulate(num, n.classes): beyond the integer range "attempt to make a table with >= 2^31 elements" (#305)
|
|
208
|
+
if (k > 2147483647)
|
|
209
|
+
throw new RangeError("attempt to make a table with >= 2^31 elements");
|
|
210
|
+
const classes = [];
|
|
184
211
|
for (const z of standardized(xs)) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
212
|
+
// a value whose pnorm rounds to 1 belongs to the last class (nortest drops it: tabulate ignores the class k + 1)
|
|
213
|
+
const c = Math.min(k, Math.floor(1 + k * pnorm(z)));
|
|
214
|
+
if (c >= 1)
|
|
215
|
+
classes.push(c);
|
|
188
216
|
}
|
|
217
|
+
classes.sort((a, b) => a - b);
|
|
218
|
+
// the sum over the classes 1..k in order, with the counts taken from the sorted class numbers: the same terms in
|
|
219
|
+
// the same order as summing a k-entry count array, without allocating it (#305)
|
|
189
220
|
const e = n / k;
|
|
190
|
-
|
|
221
|
+
let statistic = 0;
|
|
222
|
+
for (let c = 1, i = 0; c <= k; c++) {
|
|
223
|
+
let count = 0;
|
|
224
|
+
while (i < classes.length && classes[i] === c) {
|
|
225
|
+
count++;
|
|
226
|
+
i++;
|
|
227
|
+
}
|
|
228
|
+
statistic = statistic + (count - e) ** 2 / e;
|
|
229
|
+
}
|
|
191
230
|
const df = k - 1 - (adjust ? 2 : 0);
|
|
192
231
|
return {
|
|
193
232
|
method: "Pearson chi-square normality test",
|
|
194
233
|
statistic,
|
|
195
234
|
statisticName: "P",
|
|
196
235
|
pValue: pgamma1(statistic, df / 2, 2, false, false),
|
|
236
|
+
warnings: [],
|
|
197
237
|
};
|
|
198
238
|
}
|
package/dist/htest/onewayTest.js
CHANGED
|
@@ -6,11 +6,21 @@
|
|
|
6
6
|
// c = sum (1 - w_i / sum w)^2 / (n_i - 1) / (k^2 - 1), on (k - 1, 1 / (3 c)) degrees of freedom.
|
|
7
7
|
// shapiro.test: W and its p-value from src/htest/core/shapiro.ts (Royston's AS R94).
|
|
8
8
|
import { pf1 } from "../distributions/f.js";
|
|
9
|
-
import {
|
|
9
|
+
import { groupMoments, ldexpWide } from "../internal/summary.js";
|
|
10
10
|
import { swilkOneMinusW, swilkPValueFromOneMinusW } from "./core/shapiro.js";
|
|
11
11
|
import { toGroups } from "./groups.js";
|
|
12
|
+
import { checkPooledVariance } from "./multipleComparisons.js";
|
|
12
13
|
import { onewayData } from "./testFormula.js";
|
|
14
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
15
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
16
|
+
import { isOptionsObject } from "../internal/optionalArgs.js";
|
|
17
|
+
const ONEWAY_TEST_KEYS = ["varEqual"];
|
|
18
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
19
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
13
20
|
export function onewayTest(x, g, options = {}) {
|
|
21
|
+
if (typeof x !== "string" && isOptionsObject(g))
|
|
22
|
+
checkOptions(g, ONEWAY_TEST_KEYS, "onewayTest"); // generated: option-keys.mjs
|
|
23
|
+
checkOptions(options, ONEWAY_TEST_KEYS, "onewayTest"); // generated: option-keys.mjs
|
|
14
24
|
if (typeof x === "string") {
|
|
15
25
|
const d = onewayData(x, g);
|
|
16
26
|
return onewayTest(d.values, d.groups, options);
|
|
@@ -20,7 +30,11 @@ export function onewayTest(x, g, options = {}) {
|
|
|
20
30
|
options = g;
|
|
21
31
|
else
|
|
22
32
|
labels = g;
|
|
23
|
-
|
|
33
|
+
// moments of the data scaled by a power of 2 (exact), so that the variances stay in range; the statistic has no
|
|
34
|
+
// units
|
|
35
|
+
const raw = toGroups(x, labels);
|
|
36
|
+
const gm = groupMoments(raw);
|
|
37
|
+
const groups = gm.groups;
|
|
24
38
|
const varEqual = options.varEqual ?? false;
|
|
25
39
|
const k = groups.length;
|
|
26
40
|
if (k < 2)
|
|
@@ -28,13 +42,14 @@ export function onewayTest(x, g, options = {}) {
|
|
|
28
42
|
const ni = groups.map((s) => s.length);
|
|
29
43
|
if (ni.some((n) => n < 2))
|
|
30
44
|
throw new RangeError("not enough observations");
|
|
31
|
-
const mi =
|
|
32
|
-
const vi =
|
|
45
|
+
const mi = gm.mean;
|
|
46
|
+
const vi = gm.variance;
|
|
33
47
|
const N = ni.reduce((a, b) => a + b, 0);
|
|
34
48
|
let statistic;
|
|
35
49
|
let df2;
|
|
50
|
+
let own;
|
|
36
51
|
if (varEqual) {
|
|
37
|
-
const m =
|
|
52
|
+
const m = gm.grandMean;
|
|
38
53
|
let between = 0;
|
|
39
54
|
let within = 0;
|
|
40
55
|
for (let i = 0; i < k; i++) {
|
|
@@ -42,8 +57,63 @@ export function onewayTest(x, g, options = {}) {
|
|
|
42
57
|
within += (ni[i] - 1) * vi[i];
|
|
43
58
|
}
|
|
44
59
|
df2 = N - k;
|
|
60
|
+
// every group constant: an error, as tukeyTest (R's F Inf and p 0, or NaN for equal means)
|
|
61
|
+
checkPooledVariance(df2, gm.ssWithin);
|
|
45
62
|
statistic = between / (k - 1) / (within / df2);
|
|
46
63
|
}
|
|
64
|
+
else if (vi.some((v) => !(v >= MIN_NORMAL)) && (own = ownScaleMoments(raw)) !== undefined) {
|
|
65
|
+
// A variance underflows (or is subnormal) on the common scale although no group is constant (#393; as #270 for
|
|
66
|
+
// bartlettTest): each group on its own scale 2^-k_i, the weights and the terms as a mantissa and a power of 2,
|
|
67
|
+
// and F in the pairwise form sum_{i<j} w_i w_j (m_i - m_j)^2 / sum w, with 1 - w_i / sum w = (the sum of the
|
|
68
|
+
// other weights) / sum w
|
|
69
|
+
const w = own.map((o, i) => splitPow2(ni[i] / o.variance, -2 * o.k));
|
|
70
|
+
const top = Math.max(...w.map((x) => x.e));
|
|
71
|
+
const parts = w.map((x) => ldexpWide(x.m, x.e - top));
|
|
72
|
+
const sw = parts.reduce((s, v) => s + v, 0);
|
|
73
|
+
let c = 0;
|
|
74
|
+
for (let i = 0; i < k; i++) {
|
|
75
|
+
const others = parts.reduce((s, v, j) => (j === i ? s : s + v), 0);
|
|
76
|
+
c += (others / sw) ** 2 / (ni[i] - 1);
|
|
77
|
+
}
|
|
78
|
+
c /= k * k - 1;
|
|
79
|
+
const terms = [];
|
|
80
|
+
for (let i = 0; i < k; i++) {
|
|
81
|
+
for (let j = i + 1; j < k; j++) {
|
|
82
|
+
// m_i - m_j in units of 2^max(k_i, k_j)
|
|
83
|
+
const [p, q] = own[i].k >= own[j].k ? [own[i], own[j]] : [own[j], own[i]];
|
|
84
|
+
const d = p.mean - ldexpWide(q.mean, q.k - p.k);
|
|
85
|
+
if (d === 0)
|
|
86
|
+
continue;
|
|
87
|
+
const dd = splitPow2(d, p.k);
|
|
88
|
+
terms.push({ m: (w[i].m * w[j].m * dd.m * dd.m) / sw, e: w[i].e + w[j].e + 2 * dd.e - top });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const peak = Math.max(...terms.map((t) => t.e));
|
|
92
|
+
const sum = terms.reduce((s, t) => s + ldexpWide(t.m, t.e - peak), 0);
|
|
93
|
+
statistic = (terms.length === 0 ? 0 : ldexpWide(sum, peak)) / ((k - 1) * (1 + 2 * (k - 2) * c));
|
|
94
|
+
df2 = 1 / (3 * c);
|
|
95
|
+
}
|
|
96
|
+
else if (vi.some((v) => v === 0)) {
|
|
97
|
+
// a constant group z (#313): as its variance tends to 0, w_z -> Infinity, the weighted mean -> m_z, and
|
|
98
|
+
// F -> sum_{i != z} w_i (m_i - m_z)^2 / ((k - 1)(1 + 2 (k - 2) c)), c = sum_{i != z} 1 / (n_i - 1) / (k^2 - 1)
|
|
99
|
+
// (for k = 2 the square of Welch's t). With two or more constant groups the limit depends on how the variances
|
|
100
|
+
// tend to 0 (df2, and F for equal means), so the test is not defined
|
|
101
|
+
const zero = vi.flatMap((v, i) => (v === 0 ? [i] : []));
|
|
102
|
+
if (zero.length > 1)
|
|
103
|
+
throw new RangeError("Welch's test is not defined with two or more constant groups");
|
|
104
|
+
const z = zero[0];
|
|
105
|
+
let c = 0;
|
|
106
|
+
let num = 0;
|
|
107
|
+
for (let i = 0; i < k; i++) {
|
|
108
|
+
if (i === z)
|
|
109
|
+
continue;
|
|
110
|
+
c += 1 / (ni[i] - 1);
|
|
111
|
+
num += (ni[i] / vi[i]) * (mi[i] - mi[z]) ** 2;
|
|
112
|
+
}
|
|
113
|
+
c /= k * k - 1;
|
|
114
|
+
statistic = num / ((k - 1) * (1 + 2 * (k - 2) * c));
|
|
115
|
+
df2 = 1 / (3 * c);
|
|
116
|
+
}
|
|
47
117
|
else {
|
|
48
118
|
const wi = ni.map((n, i) => n / vi[i]);
|
|
49
119
|
const sw = wi.reduce((a, b) => a + b, 0);
|
|
@@ -68,6 +138,7 @@ export function onewayTest(x, g, options = {}) {
|
|
|
68
138
|
parameter: [k - 1, df2],
|
|
69
139
|
parameterName: ["num df", "denom df"],
|
|
70
140
|
pValue: pf1(statistic, k - 1, df2, false, false),
|
|
141
|
+
warnings: [],
|
|
71
142
|
};
|
|
72
143
|
}
|
|
73
144
|
/**
|
|
@@ -101,5 +172,35 @@ export function shapiroTest(x) {
|
|
|
101
172
|
statistic: 1 - w1,
|
|
102
173
|
statisticName: "W",
|
|
103
174
|
pValue: swilkPValueFromOneMinusW(w1, n),
|
|
175
|
+
warnings: [],
|
|
104
176
|
};
|
|
105
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Each group's mean and variance on its own scale 2^-k (#393); undefined when a group is constant (its weight is
|
|
180
|
+
* infinite: #313)
|
|
181
|
+
*/
|
|
182
|
+
function ownScaleMoments(groups) {
|
|
183
|
+
const out = [];
|
|
184
|
+
for (const g of groups) {
|
|
185
|
+
const m = groupMoments([g]);
|
|
186
|
+
const variance = m.variance[0];
|
|
187
|
+
if (!(variance > 0))
|
|
188
|
+
return undefined;
|
|
189
|
+
out.push({ mean: m.mean[0], variance, k: m.k });
|
|
190
|
+
}
|
|
191
|
+
return out;
|
|
192
|
+
}
|
|
193
|
+
/** v 2^e (v finite and nonzero) as m 2^e' with abs(m) in [1, 2) */
|
|
194
|
+
function splitPow2(v, e) {
|
|
195
|
+
let f = Math.floor(Math.log2(Math.abs(v)));
|
|
196
|
+
let m = ldexpWide(v, -f);
|
|
197
|
+
if (Math.abs(m) >= 2) {
|
|
198
|
+
m /= 2;
|
|
199
|
+
f++;
|
|
200
|
+
}
|
|
201
|
+
else if (Math.abs(m) < 1) {
|
|
202
|
+
m *= 2;
|
|
203
|
+
f--;
|
|
204
|
+
}
|
|
205
|
+
return { m, e: e + f };
|
|
206
|
+
}
|
package/dist/htest/pAdjust.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export interface PAdjustOptions {
|
|
|
14
14
|
method?: PAdjustMethod | (string & {});
|
|
15
15
|
/**
|
|
16
16
|
* Number of comparisons. Default: the number of non-`NaN` p-values. Must not be smaller
|
|
17
|
-
* than that; must be an integer for `"hommel"`.
|
|
17
|
+
* than that; must be an integer and at most 65536 for `"hommel"`.
|
|
18
18
|
*/
|
|
19
19
|
n?: number;
|
|
20
20
|
}
|
|
@@ -23,7 +23,8 @@ export interface PAdjustOptions {
|
|
|
23
23
|
*
|
|
24
24
|
* `NaN` values are excluded from the adjustment and returned as `NaN` in place. If `n` is
|
|
25
25
|
* 1 or less, the p-values are returned unchanged. Differences from R: with `"hommel"` a
|
|
26
|
-
* non-integer `n` is an error, and an empty `p` always
|
|
26
|
+
* 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
|
|
27
|
+
* gives an empty result.
|
|
27
28
|
*
|
|
28
29
|
* @param p p-values.
|
|
29
30
|
* @param options See {@link PAdjustOptions}, or just the method name as a string.
|