@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
|
@@ -11,8 +11,13 @@
|
|
|
11
11
|
// - One-sided statistic D_n^+ = sup (F_n(x) - F(x)) (Birnbaum and Tingey, Annals of Mathematical Statistics 22, 1951):
|
|
12
12
|
// P(D_n^+ >= d) = d sum_(j=0)^(floor(n (1 - d))) C(n, j) (1 - d - j/n)^(n-j) (d + j/n)^(j-1),
|
|
13
13
|
// whose terms are b(j; n, a) / a with a = d + j/n: binomial probabilities from Loader's dbinomRawDD.
|
|
14
|
+
// - Upper tail P(D_n >= d) as a sum of positive terms (#483): the counting process of the sample is a Markov chain
|
|
15
|
+
// with binomial steps between the breakpoints i/n - d and j/n + d, and the probability leaving it across either
|
|
16
|
+
// boundary is added up (the first-passage form of the statistic, e.g. Durbin, "Distribution theory for tests based
|
|
17
|
+
// on the sample distribution function", SIAM 1973).
|
|
14
18
|
import { ddAdd, ddDiv, twoProd, twoSum } from "../../special/dd.js";
|
|
15
19
|
import { dbinomRawDD } from "../../special/loader.js";
|
|
20
|
+
import { dbinom1 } from "../../distributions/binomial.js";
|
|
16
21
|
/** pi^2 / 8 as a double-double */
|
|
17
22
|
const PI2_8 = [1.2337005501361697, 7.831619385924639e-17];
|
|
18
23
|
/** H^n with the binary exponent kept apart: returns [matrix, e2] with the true power = matrix * 2^e2 */
|
|
@@ -65,6 +70,27 @@ function matPow(H, m, n) {
|
|
|
65
70
|
}
|
|
66
71
|
return [result, re];
|
|
67
72
|
}
|
|
73
|
+
/** Largest work m^3 (number of matrix products) of pkolmogorovExact that ksTest accepts: about 4 s (#307) */
|
|
74
|
+
const MAX_EXACT_WORK = 2 ** 33;
|
|
75
|
+
/**
|
|
76
|
+
* Throws a RangeError when pkolmogorovExact(n, d) would need more than MAX_EXACT_WORK: the m x m matrix
|
|
77
|
+
* (m = 2 floor(n d) + 1) is raised to the n-th power with floor(log2 n) squarings and popcount(n) - 1 products, each
|
|
78
|
+
* m^3 multiply-adds (n 1e4 with d 0.4999 asked for an 800 MB matrix and hours of work). Checked before any allocation.
|
|
79
|
+
*
|
|
80
|
+
* @param n - Sample size.
|
|
81
|
+
* @param d - Value of the statistic.
|
|
82
|
+
*/
|
|
83
|
+
export function checkKolmogorovExactSize(n, d) {
|
|
84
|
+
// the branches of pkolmogorovExact that use no matrix
|
|
85
|
+
if (Number.isNaN(d) || !(n >= 1) || n !== Math.floor(n) || d <= 1 / (2 * n) || d >= 1 || n * d < 1)
|
|
86
|
+
return;
|
|
87
|
+
const m = 2 * Math.floor(n * d) + 1;
|
|
88
|
+
const bits = n.toString(2);
|
|
89
|
+
const products = Math.max(1, bits.length - 1 + (bits.split("1").length - 2));
|
|
90
|
+
if (m ** 3 * products > MAX_EXACT_WORK) {
|
|
91
|
+
throw new RangeError(`the exact distribution of D for n = ${n} needs powers of a ${m} x ${m} matrix, too much work: use exact: false`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
68
94
|
/**
|
|
69
95
|
* P(D_n < d), the exact distribution of the two-sided one-sample Kolmogorov-Smirnov statistic for a continuous
|
|
70
96
|
* distribution (Marsaglia, Tsang and Wang 2003). The matrix has size 2 floor(n d) + 1, so the cost grows like
|
|
@@ -174,6 +200,9 @@ export function pkolmogorovLimit(x, lowerTail = true) {
|
|
|
174
200
|
if (t <= 1e-17 * (s + first))
|
|
175
201
|
break;
|
|
176
202
|
}
|
|
203
|
+
// the series underflows below x = 0.0407; there sqrt(2 pi) / x overflows for subnormal x (Inf * 0)
|
|
204
|
+
if (first + s === 0)
|
|
205
|
+
return lowerTail ? 0 : 1;
|
|
177
206
|
const k = (Math.sqrt(2 * Math.PI) / x) * (first + s);
|
|
178
207
|
return lowerTail ? k : 1 - k;
|
|
179
208
|
}
|
|
@@ -219,3 +248,97 @@ export function pkolmogorovOneSidedUpper(n, d) {
|
|
|
219
248
|
}
|
|
220
249
|
return Math.min(d * s, 1);
|
|
221
250
|
}
|
|
251
|
+
/** Largest number of binomial terms of pkolmogorovExactUpper: about 4 s on the development machine (#483) */
|
|
252
|
+
const MAX_CHAIN_WORK = 2 ** 28;
|
|
253
|
+
/**
|
|
254
|
+
* P(D_n >= d), the upper tail of the two-sided one-sample statistic, as a sum of positive terms (#483), or NaN where
|
|
255
|
+
* that needs more than MAX_CHAIN_WORK terms. 1 - P(D_n < d) from pkolmogorovExact has absolute accuracy only (1e-22
|
|
256
|
+
* came out as 3e-15 at n = 99).
|
|
257
|
+
*
|
|
258
|
+
* N(t), the number of points at or below t, is a Markov chain: given N(t_k) = s, the count in (t_k, t_(k+1)] is
|
|
259
|
+
* Binomial(n - s, (t_(k+1) - t_k) / (1 - t_k)). D+ >= d when N(i/n - d) >= i for some i, D- >= d when
|
|
260
|
+
* N(j/n + d) <= j for some j (N(t-) = N(t) almost surely). Between the at most 2n breakpoints i/n - d and j/n + d the
|
|
261
|
+
* thresholds are constant, so the chain is stepped from one breakpoint to the next and the probability that leaves it
|
|
262
|
+
* across a threshold is added up. Each binomial row starts from dbinom1 at its mode and follows the ratio recurrence
|
|
263
|
+
* both ways until it underflows.
|
|
264
|
+
*
|
|
265
|
+
* @param n - Sample size, a positive integer.
|
|
266
|
+
* @param d - Value of the statistic, in (0, 1).
|
|
267
|
+
*/
|
|
268
|
+
export function pkolmogorovExactUpper(n, d) {
|
|
269
|
+
const [hi, lo] = twoProd(n, d);
|
|
270
|
+
// floor(n d) of the exact n d = hi + lo
|
|
271
|
+
let nd = Math.floor(hi);
|
|
272
|
+
if (nd === hi && lo < 0)
|
|
273
|
+
nd -= 1;
|
|
274
|
+
const pts = [];
|
|
275
|
+
for (let i = 0; i <= n; i++) {
|
|
276
|
+
const u = (i - hi - lo) / n;
|
|
277
|
+
if (u > 0 && u < 1)
|
|
278
|
+
pts.push({ t: u, up: true, i });
|
|
279
|
+
const l = (i + hi + lo) / n;
|
|
280
|
+
if (l > 0 && l < 1)
|
|
281
|
+
pts.push({ t: l, up: false, i });
|
|
282
|
+
}
|
|
283
|
+
// at most 2 nd + 1 live states per step, rows of about 100 terms before they underflow
|
|
284
|
+
if (pts.length * (2 * nd + 1) * Math.min(n + 1, 100) > MAX_CHAIN_WORK)
|
|
285
|
+
return NaN;
|
|
286
|
+
pts.sort((a, b) => a.t - b.t);
|
|
287
|
+
let upper = nd + 1; // N >= upper has crossed the upper threshold
|
|
288
|
+
let alive = new Float64Array(n + 1);
|
|
289
|
+
let next = new Float64Array(n + 1);
|
|
290
|
+
alive[0] = 1;
|
|
291
|
+
let sLo = 0; // the live states lie in [sLo, upper - 1]
|
|
292
|
+
let t = 0;
|
|
293
|
+
let p = 0;
|
|
294
|
+
let work = 0;
|
|
295
|
+
for (const pt of pts) {
|
|
296
|
+
const q = (pt.t - t) / (1 - t);
|
|
297
|
+
if (q > 0) {
|
|
298
|
+
const odds = q / (1 - q);
|
|
299
|
+
next.fill(0);
|
|
300
|
+
for (let s = sLo; s < upper; s++) {
|
|
301
|
+
const a = alive[s];
|
|
302
|
+
if (a === 0)
|
|
303
|
+
continue;
|
|
304
|
+
const m = n - s;
|
|
305
|
+
const c0 = Math.min(m, Math.floor((m + 1) * q));
|
|
306
|
+
const cu = upper - s; // c >= cu crosses
|
|
307
|
+
let v = dbinom1(c0, m, q, false);
|
|
308
|
+
for (let c = c0; c <= m && v !== 0; c++) {
|
|
309
|
+
if (c > c0)
|
|
310
|
+
v = (v * (m - c + 1) * odds) / c;
|
|
311
|
+
if (c >= cu)
|
|
312
|
+
p += a * v;
|
|
313
|
+
else
|
|
314
|
+
next[s + c] += a * v;
|
|
315
|
+
work++;
|
|
316
|
+
}
|
|
317
|
+
v = c0 > 0 ? dbinom1(c0 - 1, m, q, false) : 0;
|
|
318
|
+
for (let c = c0 - 1; c >= 0 && v !== 0; c--) {
|
|
319
|
+
if (c < c0 - 1)
|
|
320
|
+
v = (v * (c + 1)) / ((m - c) * odds);
|
|
321
|
+
if (c >= cu)
|
|
322
|
+
p += a * v;
|
|
323
|
+
else
|
|
324
|
+
next[s + c] += a * v;
|
|
325
|
+
work++;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
if (work > 2 * MAX_CHAIN_WORK)
|
|
329
|
+
return NaN;
|
|
330
|
+
[alive, next] = [next, alive];
|
|
331
|
+
}
|
|
332
|
+
t = pt.t;
|
|
333
|
+
if (pt.up)
|
|
334
|
+
upper = pt.i + 1;
|
|
335
|
+
else {
|
|
336
|
+
for (let s = sLo; s <= pt.i; s++) {
|
|
337
|
+
p += alive[s];
|
|
338
|
+
alive[s] = 0;
|
|
339
|
+
}
|
|
340
|
+
sLo = Math.max(sLo, pt.i + 1);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return Math.min(p, 1);
|
|
344
|
+
}
|
|
@@ -6,10 +6,11 @@
|
|
|
6
6
|
// odds ratio (Mantel and Haenszel, JNCI 22, 1959; Birch, JRSS B 26, 1964), so their sum S has the density
|
|
7
7
|
// d0(s) psi^s / sum_t d0(t) psi^t, where d0 is the convolution of the K central hypergeometric densities. The
|
|
8
8
|
// conditional MLE of psi is the noncentrality whose mean equals the observed S, and the confidence limits are the
|
|
9
|
-
// noncentralities at which a one-sided probability equals alpha; the roots are found
|
|
10
|
-
//
|
|
9
|
+
// noncentralities at which a one-sided probability equals alpha; the roots are found on the log scale of psi to the
|
|
10
|
+
// precision of the doubles, as for fisherTest's 2 x 2 tables (src/htest/fisherTest.ts; R's uniroot conventions stop
|
|
11
|
+
// 1e-6 to 1e-2 from the roots).
|
|
11
12
|
import { dhyper1 } from "../../distributions/hypergeometric.js";
|
|
12
|
-
import {
|
|
13
|
+
import { logScaleRoot } from "../../internal/uniroot.js";
|
|
13
14
|
import { ddAdd } from "../../special/dd.js";
|
|
14
15
|
/** Compensated (double-double) sum of v[from..to) */
|
|
15
16
|
function sumRange(v, from, to) {
|
|
@@ -112,7 +113,8 @@ export class ConvolvedNoncentralHyper {
|
|
|
112
113
|
* t = 1/psi in [eps, 1]
|
|
113
114
|
*/
|
|
114
115
|
function oddsRoot(f, belowOne) {
|
|
115
|
-
|
|
116
|
+
// on the log scale to the precision of the doubles (R's uniroot conventions stopped 1e-6 to 1e-2 off; review 58)
|
|
117
|
+
return logScaleRoot(f, belowOne);
|
|
116
118
|
}
|
|
117
119
|
/** The conditional maximum likelihood estimate of the common odds ratio given the observed sum s */
|
|
118
120
|
export function conditionalMLE(h, s) {
|
|
@@ -8,7 +8,7 @@ export declare function swilkCoefficients(n: number): Float64Array;
|
|
|
8
8
|
* r^2 = sum a_i^2 sum (x_i - mean)^2, so that it keeps its relative accuracy when W is close to 1 (the p-value
|
|
9
9
|
* depends on log(1 - W)). The data are scaled by their range first.
|
|
10
10
|
*/
|
|
11
|
-
export declare function swilkOneMinusW(
|
|
11
|
+
export declare function swilkOneMinusW(data: readonly number[]): number;
|
|
12
12
|
/**
|
|
13
13
|
* The W statistic of a sample sorted in increasing order:
|
|
14
14
|
* W = (sum a_i x_(i))^2 / sum (x_i - mean)^2.
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// Beyond the range of Royston's approximation for 4 <= n <= 11 the p-value is 1e-99: 1e-99 is the value R reports
|
|
12
12
|
// here (observed behaviour).
|
|
13
13
|
import { pnorm, qnorm } from "../../distributions/normal.js";
|
|
14
|
+
import { scaleToUnit } from "../../internal/summary.js";
|
|
14
15
|
/** Polynomial c[0] + c[1] x + c[2] x^2 + ... */
|
|
15
16
|
function poly(c, x) {
|
|
16
17
|
let s = 0;
|
|
@@ -79,7 +80,10 @@ export function swilkCoefficients(n) {
|
|
|
79
80
|
* r^2 = sum a_i^2 sum (x_i - mean)^2, so that it keeps its relative accuracy when W is close to 1 (the p-value
|
|
80
81
|
* depends on log(1 - W)). The data are scaled by their range first.
|
|
81
82
|
*/
|
|
82
|
-
export function swilkOneMinusW(
|
|
83
|
+
export function swilkOneMinusW(data) {
|
|
84
|
+
// divided by a power of 2 first (exact, so the ratios to the range are unchanged): the range of finite data can
|
|
85
|
+
// overflow (shapiroTest([-1e308, 0, 1e308]) was NaN)
|
|
86
|
+
const sorted = scaleToUnit([data]).groups[0];
|
|
83
87
|
const n = sorted.length;
|
|
84
88
|
const a = swilkCoefficients(n);
|
|
85
89
|
const range = sorted[n - 1] - sorted[0];
|
|
@@ -2,3 +2,9 @@
|
|
|
2
2
|
export declare function simulatedMethod(B: number): string;
|
|
3
3
|
/** A number as R's as.character prints it (15 significant digits, fixed or scientific, whichever is narrower) */
|
|
4
4
|
export declare function formatR(v: number): string;
|
|
5
|
+
/**
|
|
6
|
+
* A number as R's format() prints it with `digits` significant digits (default 7, R's option): the fewest significant
|
|
7
|
+
* digits, at most `digits`, that show the number rounded to `digits`, in fixed notation unless the scientific one is
|
|
8
|
+
* narrower (R's scipen = 0)
|
|
9
|
+
*/
|
|
10
|
+
export declare function formatDigits(v: number, digits?: number): string;
|
|
@@ -14,3 +14,23 @@ export function formatR(v) {
|
|
|
14
14
|
const fixed = String(Number(v.toPrecision(15)));
|
|
15
15
|
return fixed.length <= sciStr.length ? fixed : sciStr;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* A number as R's format() prints it with `digits` significant digits (default 7, R's option): the fewest significant
|
|
19
|
+
* digits, at most `digits`, that show the number rounded to `digits`, in fixed notation unless the scientific one is
|
|
20
|
+
* narrower (R's scipen = 0)
|
|
21
|
+
*/
|
|
22
|
+
export function formatDigits(v, digits = 7) {
|
|
23
|
+
if (!Number.isFinite(v))
|
|
24
|
+
return Number.isNaN(v) ? "NaN" : v > 0 ? "Inf" : "-Inf";
|
|
25
|
+
if (v === 0)
|
|
26
|
+
return "0";
|
|
27
|
+
const [mant, exp] = v.toExponential(digits - 1).split("e");
|
|
28
|
+
const m = mant.includes(".") ? mant.replace(/0+$/, "").replace(/\.$/, "") : mant;
|
|
29
|
+
const e = Number(exp);
|
|
30
|
+
const sig = m.replace(/^-/, "").replace(".", "").length;
|
|
31
|
+
const sciStr = `${m}e${e < 0 ? "-" : "+"}${String(Math.abs(e)).padStart(2, "0")}`;
|
|
32
|
+
// digits right of the point in fixed notation
|
|
33
|
+
const right = Math.max(0, sig - 1 - e);
|
|
34
|
+
const fixedWidth = (v < 0 ? 1 : 0) + (e >= 0 ? e + 1 : 1) + (right > 0 ? right + 1 : 0);
|
|
35
|
+
return fixedWidth > sciStr.length ? sciStr : v.toFixed(right);
|
|
36
|
+
}
|
package/dist/htest/dunnett.js
CHANGED
|
@@ -27,10 +27,17 @@ function phi(z) {
|
|
|
27
27
|
function logPhi(x) {
|
|
28
28
|
return pnorm(x, { logP: true });
|
|
29
29
|
}
|
|
30
|
-
/**
|
|
31
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Integral over the whole line with QUADPACK's dqagi at full precision. With `checked` (the outer integral, the
|
|
32
|
+
* p-value itself), NaN when QUADPACK reports no convergence (ier 1: subdivisions used up, 3: bad integrand, 5:
|
|
33
|
+
* divergence) and its error estimate exceeds 1e-6 of it: no p-value rather than a wrong one. ier 2 and 4 (roundoff
|
|
34
|
+
* detected at this tight tolerance) leave the result usable. The inner integrals are not checked: conditional
|
|
35
|
+
* probabilities far below the p-value may carry a large relative error that does not reach it
|
|
36
|
+
*/
|
|
37
|
+
function wholeLine(f, lower = -Infinity, checked = false) {
|
|
32
38
|
const r = lower === -Infinity ? dqagie(f, 0, 2, 0, REL_TOL, LIMIT) : dqagie(f, lower, 1, 0, REL_TOL, LIMIT);
|
|
33
|
-
|
|
39
|
+
const failed = r.ier === 1 || r.ier === 3 || r.ier === 5;
|
|
40
|
+
return checked && failed && !(r.abserr <= 1e-6 * Math.abs(r.result)) ? NaN : r.result;
|
|
34
41
|
}
|
|
35
42
|
/**
|
|
36
43
|
* Inner probability given w = c u: P(max T_i >= c | U = u) (twoSided false) or P(max |T_i| >= c | U = u) (twoSided
|
|
@@ -53,8 +60,10 @@ function conditionalUpper(w, lambda, twoSided) {
|
|
|
53
60
|
const q = pnorm(b) + pnorm(a, { lowerTail: false });
|
|
54
61
|
if (q < 0.5)
|
|
55
62
|
logIn += Math.log1p(-q);
|
|
63
|
+
// (b, a) is narrower than the rounding of the tail probabilities when w is tiny: their difference may come out
|
|
64
|
+
// below 0 (it gave NaN, and a NaN p-value: glhtDunnett two-sided at t = 1.55); it is at least 0
|
|
56
65
|
else
|
|
57
|
-
logIn += Math.log(a + b < 0 ? pnorm(a) - pnorm(b) : pnorm(b, { lowerTail: false }) - pnorm(a, { lowerTail: false }));
|
|
66
|
+
logIn += Math.log(Math.max(0, a + b < 0 ? pnorm(a) - pnorm(b) : pnorm(b, { lowerTail: false }) - pnorm(a, { lowerTail: false })));
|
|
58
67
|
}
|
|
59
68
|
return phi(z) * -Math.expm1(logIn);
|
|
60
69
|
};
|
|
@@ -85,5 +94,5 @@ export function pDunnettUpper(c, lambda, df, twoSided) {
|
|
|
85
94
|
return 0;
|
|
86
95
|
return Math.exp(ld) * conditionalUpper(c * Math.exp(v), lambda, twoSided);
|
|
87
96
|
};
|
|
88
|
-
return Math.min(1, wholeLine(g));
|
|
97
|
+
return Math.min(1, wholeLine(g, -Infinity, true));
|
|
89
98
|
}
|
|
@@ -41,11 +41,16 @@ export interface CohensDResult {
|
|
|
41
41
|
/** effectsize's warnings. */
|
|
42
42
|
warnings: string[];
|
|
43
43
|
}
|
|
44
|
+
/** Hedges' correction J(df) = Γ(df/2) / (sqrt(df/2) Γ((df − 1)/2)) (exported for the tests) */
|
|
45
|
+
export declare function hedgesJ(df: number): number;
|
|
44
46
|
/**
|
|
45
47
|
* Cohen's d: the standardized difference of means of two samples (pooled or averaged standard deviation), of paired
|
|
46
48
|
* samples, or of one sample against `mu`, with a confidence interval from the noncentral t distribution.
|
|
47
49
|
* Equivalent to `effectsize::cohens_d()` in R.
|
|
48
50
|
*
|
|
51
|
+
* As effectsize, `warnings` has "'y' is numeric but has only 2 unique values. ..." where `y` has exactly 2 distinct
|
|
52
|
+
* values (a grouping variable is given as two samples here) and "Missing values detected. NAs dropped." for `NaN`.
|
|
53
|
+
*
|
|
49
54
|
* @example
|
|
50
55
|
* ```ts
|
|
51
56
|
* cohensD([5.1, 4.9, 6.2, 5.8], [4.1, 4.5, 3.9, 5.0]).value; // 2.049693834034246
|
package/dist/htest/effectSize.js
CHANGED
|
@@ -8,47 +8,83 @@
|
|
|
8
8
|
// the exact roots (Brent's method run to the resolution of doubles).
|
|
9
9
|
import { pf, qf } from "../distributions/f.js";
|
|
10
10
|
import { pt } from "../distributions/t.js";
|
|
11
|
-
import { completePairs, dropNaN, mean, variance } from "../internal/summary.js";
|
|
11
|
+
import { completePairs, dropNaN, groupMoments, ldexp, mean, pairedDifferences, satterthwaiteDfSe, scaleToUnit, variance } from "../internal/summary.js";
|
|
12
12
|
import { anova, aovSummary } from "../models/anovaSequential.js";
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
13
|
+
import { scaledTables } from "../models/scaledTables.js";
|
|
14
|
+
import { unirootCore as uniroot } from "../numerics/integrate.js";
|
|
15
|
+
import { lgammaDiff } from "../special/lgammaDiff.js";
|
|
15
16
|
import { matchChoice } from "./power.js";
|
|
17
|
+
import { reported, unreported } from "../internal/warnings.js";
|
|
18
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
19
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
20
|
+
const COHENS_D_KEYS = ["pooledSd", "mu", "paired", "ci", "alternative", "adjust"];
|
|
21
|
+
const HEDGES_G_KEYS = ["pooledSd", "mu", "paired", "ci", "alternative"];
|
|
22
|
+
const ETA_SQUARED_KEYS = ["generalized", "partial", "ci", "alternative"];
|
|
23
|
+
const OMEGA_SQUARED_KEYS = ["partial", "ci", "alternative"];
|
|
24
|
+
const EPSILON_SQUARED_KEYS = ["partial", "ci", "alternative"];
|
|
25
|
+
/** The confidence level of an interval, null for none: as effectsize, strictly between 0 and 1 */
|
|
26
|
+
function checkCi(ci) {
|
|
27
|
+
if (ci !== null && !(ci > 0 && ci < 1))
|
|
28
|
+
throw new RangeError("ci must be a single numeric value between (0, 1)");
|
|
29
|
+
}
|
|
16
30
|
const NA_WARNING = "Missing values detected. NAs dropped.";
|
|
31
|
+
/** effectsize's warning where y has exactly 2 distinct values (not counting NA), as a grouping variable would */
|
|
32
|
+
const TWO_VALUES_WARNING = "'y' is numeric but has only 2 unique values.\n If this is a grouping variable, convert it to a factor.";
|
|
17
33
|
/** Brent's method run until the bracket is as narrow as doubles allow */
|
|
18
34
|
const EXACT = { tol: 0, maxiter: 5000 };
|
|
19
|
-
/** Hedges' correction J(df) = Γ(df/2) / (sqrt(df/2) Γ((df − 1)/2)) */
|
|
20
|
-
function hedgesJ(df) {
|
|
21
|
-
|
|
35
|
+
/** Hedges' correction J(df) = Γ(df/2) / (sqrt(df/2) Γ((df − 1)/2)) (exported for the tests) */
|
|
36
|
+
export function hedgesJ(df) {
|
|
37
|
+
if (df === Infinity)
|
|
38
|
+
return 1;
|
|
39
|
+
// log J = lgamma((df - 1)/2 + 1/2) - lgamma((df - 1)/2) - log(df/2)/2, the difference of the lgammas formed directly
|
|
40
|
+
// (two lgammas near df log df cancel: J was 0.99997 at df = 1e10, 1.28 at 1e14, as effectsize's, where it is
|
|
41
|
+
// 1 - 3/(4 df - 1) to within 1e-20)
|
|
42
|
+
return Math.exp(lgammaDiff(0.5, (df - 1) / 2) - 0.5 * Math.log(df / 2));
|
|
22
43
|
}
|
|
23
44
|
/** The noncentrality parameter with pt(t, df, ncp) = p (pt decreases in ncp) */
|
|
24
45
|
function ncpOfT(t, df, p) {
|
|
25
|
-
|
|
46
|
+
// (where t ± 1 both round to t, a step of 1 to 2 ulp of t: the empty interval [t, t] was an error; issue #143)
|
|
47
|
+
const h = t - 1 < t + 1 ? 1 : Math.abs(t) * Number.EPSILON;
|
|
48
|
+
return uniroot((ncp) => pt(t, df, { ncp }) - p, t - h, t + h, { ...EXACT, extendInt: "downX" }).root;
|
|
26
49
|
}
|
|
27
50
|
function standardizedDifference(x, y, options, adjust) {
|
|
28
51
|
const pooled = options.pooledSd ?? true;
|
|
29
52
|
const mu = options.mu ?? 0;
|
|
30
53
|
const paired = options.paired ?? false;
|
|
31
54
|
const ci = options.ci === undefined ? 0.95 : options.ci;
|
|
55
|
+
checkCi(ci);
|
|
32
56
|
const alt = matchChoice(options.alternative, ["two.sided", "less", "greater"]);
|
|
33
57
|
const xs = Array.from(x);
|
|
34
58
|
const ys = y === undefined ? undefined : Array.from(y);
|
|
35
59
|
const warnings = [];
|
|
60
|
+
if (ys !== undefined && new Set(dropNaN(ys)).size === 2)
|
|
61
|
+
warnings.push(TWO_VALUES_WARNING);
|
|
36
62
|
if (xs.some(Number.isNaN) || (ys?.some(Number.isNaN) ?? false))
|
|
37
63
|
warnings.push(NA_WARNING);
|
|
64
|
+
// (an infinite value, or too few observations, ended in the root search of the interval: "lower < upper is not
|
|
65
|
+
// fulfilled"; effectsize gives NA)
|
|
66
|
+
const infinite = (v) => v === Infinity || v === -Infinity;
|
|
67
|
+
if (xs.some(infinite) || (ys?.some(infinite) ?? false))
|
|
68
|
+
throw new RangeError("the observations must be finite (NaN is dropped as missing)");
|
|
69
|
+
// Everything is computed on the data scaled by one power of 2, 2^-k (exact; paired values before their differences
|
|
70
|
+
// are taken), with mu scaled alike: the effect size and its interval have no units, so nothing is scaled back
|
|
38
71
|
let d;
|
|
39
72
|
let s;
|
|
40
73
|
let se;
|
|
41
74
|
let h;
|
|
42
75
|
let df;
|
|
76
|
+
let k;
|
|
43
77
|
if (ys === undefined || paired) {
|
|
44
78
|
let v;
|
|
45
79
|
if (ys === undefined)
|
|
46
|
-
v = dropNaN(xs);
|
|
80
|
+
({ groups: [v], k } = scaleToUnit([dropNaN(xs)]));
|
|
47
81
|
else {
|
|
48
82
|
const [a, b] = completePairs(xs, ys);
|
|
49
|
-
|
|
83
|
+
({ d: v, k } = pairedDifferences(a, b));
|
|
50
84
|
}
|
|
51
85
|
const n = v.length;
|
|
86
|
+
if (n < 2)
|
|
87
|
+
throw new RangeError(ys === undefined ? "not enough observations: at least 2 are needed" : "not enough complete pairs: at least 2 are needed");
|
|
52
88
|
d = mean(v);
|
|
53
89
|
s = Math.sqrt(variance(v));
|
|
54
90
|
se = s / Math.sqrt(n);
|
|
@@ -56,13 +92,14 @@ function standardizedDifference(x, y, options, adjust) {
|
|
|
56
92
|
df = n - 1;
|
|
57
93
|
}
|
|
58
94
|
else {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
const n1 =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
95
|
+
const gm = groupMoments([dropNaN(xs), dropNaN(ys)]);
|
|
96
|
+
k = gm.k;
|
|
97
|
+
const [n1, n2] = gm.n;
|
|
98
|
+
if (n1 < 2 || n2 < 2)
|
|
99
|
+
throw new RangeError("not enough observations: at least 2 are needed in each group");
|
|
100
|
+
d = gm.mean[0] - gm.mean[1];
|
|
101
|
+
const s1 = Math.sqrt(gm.variance[0]);
|
|
102
|
+
const s2 = Math.sqrt(gm.variance[1]);
|
|
66
103
|
if (pooled) {
|
|
67
104
|
s = Math.sqrt(((n1 - 1) * s1 ** 2 + (n2 - 1) * s2 ** 2) / (n1 + n2 - 2));
|
|
68
105
|
se = s * Math.sqrt(1 / n1 + 1 / n2);
|
|
@@ -72,17 +109,19 @@ function standardizedDifference(x, y, options, adjust) {
|
|
|
72
109
|
else {
|
|
73
110
|
s = Math.sqrt((s1 ** 2 + s2 ** 2) / 2);
|
|
74
111
|
h = (2 * (n2 * s1 ** 2 + n1 * s2 ** 2)) / (n1 * n2 * (s1 ** 2 + s2 ** 2));
|
|
75
|
-
se =
|
|
76
|
-
|
|
112
|
+
// effectsize's se = sqrt(se1^2 + se2^2) with se_i = sqrt(s_i^2 / n_i), and its df from them
|
|
113
|
+
se = Math.sqrt(Math.sqrt(s1 ** 2 / n1) ** 2 + Math.sqrt(s2 ** 2 / n2) ** 2);
|
|
114
|
+
df = satterthwaiteDfSe([s1 ** 2 / n1, s2 ** 2 / n2], [n1 - 1, n2 - 1]);
|
|
77
115
|
}
|
|
78
116
|
}
|
|
79
117
|
const j = adjust ? hedgesJ(df) : 1;
|
|
80
|
-
const
|
|
118
|
+
const muS = ldexp(mu, -k);
|
|
119
|
+
const value = ((d - muS) / s) * j;
|
|
81
120
|
const name = adjust ? "Hedges_g" : "Cohens_d";
|
|
82
121
|
if (ci === null)
|
|
83
122
|
return { name, value, warnings };
|
|
84
123
|
const level = alt === "two.sided" ? ci : 2 * ci - 1;
|
|
85
|
-
const t = (d -
|
|
124
|
+
const t = (d - muS) / se;
|
|
86
125
|
let ciLow = ncpOfT(t, df, (1 + level) / 2) * Math.sqrt(h) * j;
|
|
87
126
|
let ciHigh = ncpOfT(t, df, (1 - level) / 2) * Math.sqrt(h) * j;
|
|
88
127
|
if (alt === "less")
|
|
@@ -96,6 +135,9 @@ function standardizedDifference(x, y, options, adjust) {
|
|
|
96
135
|
* samples, or of one sample against `mu`, with a confidence interval from the noncentral t distribution.
|
|
97
136
|
* Equivalent to `effectsize::cohens_d()` in R.
|
|
98
137
|
*
|
|
138
|
+
* As effectsize, `warnings` has "'y' is numeric but has only 2 unique values. ..." where `y` has exactly 2 distinct
|
|
139
|
+
* values (a grouping variable is given as two samples here) and "Missing values detected. NAs dropped." for `NaN`.
|
|
140
|
+
*
|
|
99
141
|
* @example
|
|
100
142
|
* ```ts
|
|
101
143
|
* cohensD([5.1, 4.9, 6.2, 5.8], [4.1, 4.5, 3.9, 5.0]).value; // 2.049693834034246
|
|
@@ -104,7 +146,8 @@ function standardizedDifference(x, y, options, adjust) {
|
|
|
104
146
|
* @category Effect sizes and power
|
|
105
147
|
*/
|
|
106
148
|
export function cohensD(x, y, options = {}) {
|
|
107
|
-
|
|
149
|
+
checkOptions(options, COHENS_D_KEYS, "cohensD"); // generated: option-keys.mjs
|
|
150
|
+
return reported("cohensD", standardizedDifference(x, y, options, options.adjust ?? false));
|
|
108
151
|
}
|
|
109
152
|
/**
|
|
110
153
|
* Hedges' g: Cohen's d and its interval multiplied by the small-sample correction J(df). Equivalent to
|
|
@@ -118,7 +161,8 @@ export function cohensD(x, y, options = {}) {
|
|
|
118
161
|
* @category Effect sizes and power
|
|
119
162
|
*/
|
|
120
163
|
export function hedgesG(x, y, options = {}) {
|
|
121
|
-
|
|
164
|
+
checkOptions(options, HEDGES_G_KEYS, "hedgesG"); // generated: option-keys.mjs
|
|
165
|
+
return reported("hedgesG", standardizedDifference(x, y, options, true));
|
|
122
166
|
}
|
|
123
167
|
const isFit = (m) => Array.isArray(m.effects);
|
|
124
168
|
const isStrataFit = (m) => m.multistratum === true;
|
|
@@ -127,9 +171,10 @@ const isEz = (m) => {
|
|
|
127
171
|
const e = m;
|
|
128
172
|
return typeof e.univariate === "object" && typeof e.anova === "object";
|
|
129
173
|
};
|
|
130
|
-
const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
131
174
|
/** A name as a whole word */
|
|
132
|
-
|
|
175
|
+
// Whether a variable is one of the components of a term name such as "a:b" (compared as names, so that a name that is
|
|
176
|
+
// not a run of word characters, such as ".grp", is found too)
|
|
177
|
+
const word = (name) => ({ test: (term) => term.split(":").includes(name) });
|
|
133
178
|
/** The strata of aov with Error() from its summary (intercept stratum left out), and the subject stratum */
|
|
134
179
|
function summaryStrata(s) {
|
|
135
180
|
const strata = s.strata
|
|
@@ -215,14 +260,18 @@ function observedTerms(gen, terms) {
|
|
|
215
260
|
const observed = Array.isArray(gen) ? gen : [];
|
|
216
261
|
const missing = observed.filter((o) => !terms.some((t) => word(o).test(t.name)));
|
|
217
262
|
if (missing.length > 0)
|
|
218
|
-
throw new
|
|
263
|
+
throw new RangeError(`Observed variable not in data: ${missing.join(", ")}`);
|
|
219
264
|
const isObserved = (name) => observed.some((o) => word(o).test(name));
|
|
220
265
|
return { isObserved, ssObserved: terms.reduce((a, t) => (isObserved(t.name) ? a + t.ss : a), 0) };
|
|
221
266
|
}
|
|
222
267
|
/** The noncentrality limits of an F value: ncp ≥ 0 with pf(F, df1, df2, ncp) = p (pf decreases in ncp), 0 if none */
|
|
223
268
|
function ncpOfF(f, df1, df2, p) {
|
|
224
269
|
const g = (ncp) => pf(f, df1, df2, { ncp }) - p;
|
|
225
|
-
|
|
270
|
+
// (NaN is no root of 0: it was taken as one)
|
|
271
|
+
const g0 = g(0);
|
|
272
|
+
if (Number.isNaN(g0))
|
|
273
|
+
return NaN;
|
|
274
|
+
if (!(g0 > 0))
|
|
226
275
|
return 0;
|
|
227
276
|
if (f === Infinity)
|
|
228
277
|
return Infinity;
|
|
@@ -247,25 +296,40 @@ function withInterval(es, df, dfe, ci, alt) {
|
|
|
247
296
|
ciHigh = 1;
|
|
248
297
|
return [es, ci, ciLow, ciHigh];
|
|
249
298
|
}
|
|
299
|
+
/** The sums of squares of rows divided by one power of 2, the largest in [1, 2) (exact; the effect sizes are ratios) */
|
|
300
|
+
function normalizeSS(rows) {
|
|
301
|
+
const k = scaleToUnit([rows.map((r) => r.ss)]).k;
|
|
302
|
+
if (k !== 0)
|
|
303
|
+
for (const r of rows)
|
|
304
|
+
r.ss = ldexp(r.ss, -k);
|
|
305
|
+
}
|
|
250
306
|
const NEEDS = "needs a table of anova() or Anova(), a fit from lm() or aov() with a formula, its aovSummary(), or a result of aovEz()";
|
|
251
307
|
function anovaEffectSize(model, kind, options) {
|
|
252
308
|
if (typeof model !== "object" || model === null)
|
|
253
309
|
throw new TypeError(NEEDS);
|
|
254
310
|
if (isStrataFit(model) || isStrataSummary(model) || isEz(model))
|
|
255
311
|
return strataEffectSize(model, kind, options);
|
|
256
|
-
const
|
|
257
|
-
if (!Array.isArray(
|
|
312
|
+
const given = isFit(model) ? unreported(() => anova(model)) : model;
|
|
313
|
+
if (!Array.isArray(given.colNames) || !Array.isArray(given.table))
|
|
258
314
|
throw new TypeError(NEEDS);
|
|
315
|
+
// the sums of squares as fitted where the table has them (those returned can overflow: scaledTables.ts)
|
|
316
|
+
const scaled = scaledTables.get(given);
|
|
317
|
+
const tab = scaled !== undefined && !Array.isArray(scaled) ? scaled : given;
|
|
259
318
|
const ci = options.ci === undefined ? 0.95 : options.ci;
|
|
319
|
+
checkCi(ci);
|
|
260
320
|
const alt = matchChoice(options.alternative ?? "greater", ["two.sided", "less", "greater"]);
|
|
261
321
|
const iDf = tab.colNames.indexOf("Df");
|
|
262
322
|
const iSS = tab.colNames.indexOf("Sum Sq");
|
|
263
323
|
if (iDf < 0 || iSS < 0)
|
|
264
324
|
throw new TypeError("the table needs the columns 'Df' and 'Sum Sq'");
|
|
265
325
|
const rows = tab.rowNames.map((name, i) => ({ name, df: tab.table[i][iDf], ss: tab.table[i][iSS] })).filter((r) => r.name !== "(Intercept)");
|
|
326
|
+
normalizeSS(rows);
|
|
266
327
|
const res = rows.find((r) => r.name === "Residuals");
|
|
267
328
|
if (res === undefined)
|
|
268
329
|
throw new TypeError("the table has no row 'Residuals'");
|
|
330
|
+
// (a saturated model: the effect sizes were 0 / 0 rounding noise, as effectsize's "No residuals data found")
|
|
331
|
+
if (!(res.df > 0))
|
|
332
|
+
throw new Error("the model has no residual degrees of freedom: the effect sizes are not defined");
|
|
269
333
|
const terms = rows.filter((r) => r !== res);
|
|
270
334
|
const dfe = res.df;
|
|
271
335
|
const sse = res.ss;
|
|
@@ -298,7 +362,8 @@ function anovaEffectSize(model, kind, options) {
|
|
|
298
362
|
colNames: ci === null ? [column] : [column, "CI", "CI_low", "CI_high"],
|
|
299
363
|
rowNames: terms.map((t) => t.name),
|
|
300
364
|
table: terms.map((t) => (ci === null ? [value(t)] : withInterval(value(t), t.df, dfe, ci, alt))),
|
|
301
|
-
warnings
|
|
365
|
+
// the warnings of the table (and so of the fit it came from), as for models with error strata
|
|
366
|
+
warnings: Array.isArray(given.warnings) ? [...given.warnings] : [],
|
|
302
367
|
};
|
|
303
368
|
}
|
|
304
369
|
/**
|
|
@@ -307,20 +372,31 @@ function anovaEffectSize(model, kind, options) {
|
|
|
307
372
|
*/
|
|
308
373
|
function strataEffectSize(model, kind, options) {
|
|
309
374
|
const ci = options.ci === undefined ? 0.95 : options.ci;
|
|
375
|
+
checkCi(ci);
|
|
310
376
|
const alt = matchChoice(options.alternative ?? "greater", ["two.sided", "less", "greater"]);
|
|
311
377
|
const ez = isEz(model);
|
|
312
378
|
let strata;
|
|
313
379
|
let subject;
|
|
314
380
|
let rows;
|
|
315
381
|
if (ez) {
|
|
316
|
-
const
|
|
382
|
+
const sc = scaledTables.get(model);
|
|
383
|
+
const s = ezStrata(sc !== undefined && !Array.isArray(sc) ? { ...model, univariate: sc } : model);
|
|
317
384
|
({ strata, subject } = s);
|
|
318
385
|
rows = s.order.map((term) => ({ term, stratum: s.strata.find((st) => st.terms.includes(term)) }));
|
|
319
386
|
}
|
|
320
387
|
else {
|
|
321
|
-
|
|
388
|
+
const summary = isStrataFit(model) ? unreported(() => aovSummary(model)) : model;
|
|
389
|
+
const sc = scaledTables.get(summary);
|
|
390
|
+
({ strata, subject } = summaryStrata(Array.isArray(sc) ? { ...summary, strata: sc.map((t, i) => ({ ...summary.strata[i], ...t })) } : summary));
|
|
322
391
|
rows = strata.flatMap((stratum) => stratum.terms.map((term) => ({ term, stratum })));
|
|
323
392
|
}
|
|
393
|
+
// one power of 2 for all sums of squares (their sums can overflow; the effect sizes are ratios of them)
|
|
394
|
+
const k = scaleToUnit([[...strata.flatMap((st) => st.terms.map((t) => t.ss)), ...strata.map((st) => st.sse)]]).k;
|
|
395
|
+
for (const st of strata) {
|
|
396
|
+
st.sse = ldexp(st.sse, -k);
|
|
397
|
+
for (const t of st.terms)
|
|
398
|
+
t.ss = ldexp(t.ss, -k);
|
|
399
|
+
}
|
|
324
400
|
const allTerms = strata.flatMap((st) => st.terms);
|
|
325
401
|
const sseAll = strata.reduce((a, st) => a + st.sse, 0);
|
|
326
402
|
const ssTot = allTerms.reduce((a, t) => a + t.ss, 0) + sseAll;
|
|
@@ -356,8 +432,11 @@ function strataEffectSize(model, kind, options) {
|
|
|
356
432
|
}),
|
|
357
433
|
warnings: [],
|
|
358
434
|
};
|
|
359
|
-
if (!ez)
|
|
435
|
+
if (!ez) {
|
|
360
436
|
out.groups = rows.map((r) => r.stratum.name);
|
|
437
|
+
// the warnings of the fit (or of its summary), such as "Error() model is singular"
|
|
438
|
+
out.warnings = [...(isStrataFit(model) || isStrataSummary(model) ? model.warnings : [])];
|
|
439
|
+
}
|
|
361
440
|
return out;
|
|
362
441
|
}
|
|
363
442
|
/**
|
|
@@ -384,7 +463,8 @@ function strataEffectSize(model, kind, options) {
|
|
|
384
463
|
* @category Effect sizes and power
|
|
385
464
|
*/
|
|
386
465
|
export function etaSquared(model, options = {}) {
|
|
387
|
-
|
|
466
|
+
checkOptions(options, ETA_SQUARED_KEYS, "etaSquared"); // generated: option-keys.mjs
|
|
467
|
+
return reported("etaSquared", anovaEffectSize(model, "Eta2", options), model);
|
|
388
468
|
}
|
|
389
469
|
/**
|
|
390
470
|
* Omega squared of the terms of an analysis-of-variance table, also of repeated-measures designs (see
|
|
@@ -399,7 +479,8 @@ export function etaSquared(model, options = {}) {
|
|
|
399
479
|
* @category Effect sizes and power
|
|
400
480
|
*/
|
|
401
481
|
export function omegaSquared(model, options = {}) {
|
|
402
|
-
|
|
482
|
+
checkOptions(options, OMEGA_SQUARED_KEYS, "omegaSquared"); // generated: option-keys.mjs
|
|
483
|
+
return reported("omegaSquared", anovaEffectSize(model, "Omega2", options), model);
|
|
403
484
|
}
|
|
404
485
|
/**
|
|
405
486
|
* Epsilon squared of the terms of an analysis-of-variance table, also of repeated-measures designs (see
|
|
@@ -414,5 +495,6 @@ export function omegaSquared(model, options = {}) {
|
|
|
414
495
|
* @category Effect sizes and power
|
|
415
496
|
*/
|
|
416
497
|
export function epsilonSquared(model, options = {}) {
|
|
417
|
-
|
|
498
|
+
checkOptions(options, EPSILON_SQUARED_KEYS, "epsilonSquared"); // generated: option-keys.mjs
|
|
499
|
+
return reported("epsilonSquared", anovaEffectSize(model, "Epsilon2", options), model);
|
|
418
500
|
}
|
|
@@ -43,7 +43,8 @@ export interface FisherTestResult extends Omit<HTest, "statistic" | "statisticNa
|
|
|
43
43
|
* reproducing R's numbers (including R's root-finding tolerance). The two-sided p-value
|
|
44
44
|
* sums the probabilities of tables no more likely than the observed one (relative
|
|
45
45
|
* tolerance `1e-7`, as in R). Non-integer counts are rounded to the nearest integer
|
|
46
|
-
* (ties to even),
|
|
46
|
+
* (ties to even), with R's warning ("'x' has been rounded to integer: Mean relative difference: 0.0625", the text of
|
|
47
|
+
* R's `all.equal` of the counts and the rounded counts; none where they differ by less than about 1.5e-8).
|
|
47
48
|
*
|
|
48
49
|
* With `simulatePValue: true`, the p-value of a table larger than 2 x 2 is computed from `B` random tables with the
|
|
49
50
|
* observed margins, drawn from the random stream of {@link setSeed} exactly as in R.
|
|
@@ -58,8 +59,8 @@ export interface FisherTestResult extends Omit<HTest, "statistic" | "statisticNa
|
|
|
58
59
|
* ```ts
|
|
59
60
|
* const r = fisherTest([[3, 1], [1, 3]]);
|
|
60
61
|
* r.pValue; // 0.4857142857142857
|
|
61
|
-
* r.estimate; // 6.
|
|
62
|
-
* r.confInt; // [0.
|
|
62
|
+
* r.estimate; // 6.408319658199675 (conditional MLE of the odds ratio; R: 6.408309)
|
|
63
|
+
* r.confInt; // [0.2117355954465785, 626.2435305888127] (R: 0.2117329, 621.93)
|
|
63
64
|
* ```
|
|
64
65
|
*
|
|
65
66
|
* @category Hypothesis tests
|