@numero-uno-jpn/rstats 0.15.3 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +24 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/htest/types.js
CHANGED
|
@@ -18,6 +18,13 @@ export function checkConfLevel(level) {
|
|
|
18
18
|
throw new RangeError("confLevel must be a number between 0 and 1");
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether v is a nonnegative count. The slack is absolute: a count is an integer whatever its size, so a relative
|
|
23
|
+
* slack would accept 1e8 + 0.25 and silently round it
|
|
24
|
+
*/
|
|
25
|
+
export function isCount(v) {
|
|
26
|
+
return Number.isFinite(v) && v >= 0 && Math.abs(v - Math.round(v)) <= 1e-7;
|
|
27
|
+
}
|
|
21
28
|
/** Validate the confidence level strictly inside (0, 1), as most of R's tests require */
|
|
22
29
|
export function checkConfLevelOpen(level) {
|
|
23
30
|
if (!(Number.isFinite(level) && level > 0 && level < 1)) {
|
package/dist/htest/varTest.js
CHANGED
|
@@ -7,10 +7,16 @@
|
|
|
7
7
|
// n_i = size - 1, N = sum n_i and s_p^2 the pooled variance; chi-squared with k - 1 degrees of freedom.
|
|
8
8
|
import { pf1, qf1 } from "../distributions/f.js";
|
|
9
9
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
10
|
-
import {
|
|
10
|
+
import { groupMoments, ldexpWide } from "../internal/summary.js";
|
|
11
11
|
import { kSampleData, samplesByLevel, twoGroupData } from "./testFormula.js";
|
|
12
12
|
import { checkConfLevelOpen, matchAlternative } from "./types.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 VAR_TEST_KEYS = ["ratio", "alternative", "confLevel"];
|
|
16
|
+
// Generated by `node scripts/option-keys.mjs --write` (end)
|
|
17
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
13
18
|
export function varTest(x, y, options = {}) {
|
|
19
|
+
checkOptions(options, VAR_TEST_KEYS, "varTest"); // generated: option-keys.mjs
|
|
14
20
|
if (typeof x === "string") {
|
|
15
21
|
const d = twoGroupData(x, y);
|
|
16
22
|
return varTest(d.x, d.y, options);
|
|
@@ -30,7 +36,22 @@ export function varTest(x, y, options = {}) {
|
|
|
30
36
|
throw new Error("not enough 'x' observations");
|
|
31
37
|
if (dfy < 1)
|
|
32
38
|
throw new Error("not enough 'y' observations");
|
|
33
|
-
|
|
39
|
+
// scaled by a power of 2 (exact) so that the variances stay in range; the statistic does not depend on the units
|
|
40
|
+
let [vx, vy] = groupMoments([xs, ys]).variance;
|
|
41
|
+
let exponent = 0;
|
|
42
|
+
if (!(vx >= MIN_NORMAL && vy >= MIN_NORMAL)) {
|
|
43
|
+
// a variance below the normal range on the common scale (#393; as #270): each sample on its own scale 2^-e,
|
|
44
|
+
// the ratio times 2^(2 (e_x - e_y)). varTest([1e308, 1e308], [0, 1e-100]) had both variances 0
|
|
45
|
+
const mx = groupMoments([xs]);
|
|
46
|
+
const my = groupMoments([ys]);
|
|
47
|
+
vx = mx.variance[0];
|
|
48
|
+
vy = my.variance[0];
|
|
49
|
+
exponent = 2 * (mx.k - my.k);
|
|
50
|
+
}
|
|
51
|
+
// both variances 0: the ratio is 0 / 0 (R returns NaN); one of them 0 gives the limit 0 or Infinity as before
|
|
52
|
+
if (vx === 0 && vy === 0)
|
|
53
|
+
throw new RangeError("both samples have zero variance");
|
|
54
|
+
const estimate = exponent === 0 ? vx / vy : ldexpWide(vx / vy, exponent);
|
|
34
55
|
const statistic = estimate / ratio;
|
|
35
56
|
const lower = pf1(statistic, dfx, dfy, true, false);
|
|
36
57
|
const upper = pf1(statistic, dfx, dfy, false, false);
|
|
@@ -47,7 +68,9 @@ export function varTest(x, y, options = {}) {
|
|
|
47
68
|
else {
|
|
48
69
|
pValue = Math.min(1, 2 * Math.min(lower, upper));
|
|
49
70
|
const beta = (1 - confLevel) / 2;
|
|
50
|
-
|
|
71
|
+
// (for alpha below 1e-8 the upper probability itself: 1 - alpha loses its digits and rounds to 1 below 1.1e-16; review 58)
|
|
72
|
+
const fHigh = beta < 1e-8 ? qf1(beta, dfx, dfy, false, false) : qf1(1 - beta, dfx, dfy, true, false);
|
|
73
|
+
confInt = [estimate / fHigh, estimate / qf1(beta, dfx, dfy, true, false)];
|
|
51
74
|
}
|
|
52
75
|
return {
|
|
53
76
|
method: "F test to compare two variances",
|
|
@@ -63,6 +86,7 @@ export function varTest(x, y, options = {}) {
|
|
|
63
86
|
estimateNames: "ratio of variances",
|
|
64
87
|
nullValue: ratio,
|
|
65
88
|
nullValueName: "ratio of variances",
|
|
89
|
+
warnings: [],
|
|
66
90
|
};
|
|
67
91
|
}
|
|
68
92
|
export function bartlettTest(x, data) {
|
|
@@ -74,16 +98,13 @@ export function bartlettTest(x, data) {
|
|
|
74
98
|
const k = groups.length;
|
|
75
99
|
if (k < 2)
|
|
76
100
|
throw new RangeError("'x' must be a list with at least 2 elements");
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
n.push(s.length - 1);
|
|
85
|
-
v.push(variance(s));
|
|
86
|
-
}
|
|
101
|
+
// as in R, infinite values are dropped along with NaN
|
|
102
|
+
const samples = groups.map((g) => Array.from(g).filter(Number.isFinite));
|
|
103
|
+
if (samples.some((s) => s.length < 2))
|
|
104
|
+
throw new RangeError("there must be at least 2 observations in each group");
|
|
105
|
+
const n = samples.map((s) => s.length - 1);
|
|
106
|
+
// scaled by a power of 2 (exact) so that the variances stay in range; the statistic does not depend on the units
|
|
107
|
+
const v = groupMoments(samples).variance;
|
|
87
108
|
let N = 0;
|
|
88
109
|
let pooled = 0;
|
|
89
110
|
let sumLog = 0;
|
|
@@ -95,7 +116,23 @@ export function bartlettTest(x, data) {
|
|
|
95
116
|
sumInv += 1 / n[i];
|
|
96
117
|
}
|
|
97
118
|
pooled /= N;
|
|
98
|
-
|
|
119
|
+
let logRatio = N * Math.log(pooled) - sumLog;
|
|
120
|
+
if (v.some((vi) => vi < MIN_NORMAL)) {
|
|
121
|
+
// A variance underflows (or is subnormal) on the common scale although the log ratio is finite: each group on
|
|
122
|
+
// its own scale 2^-e_i, log v_i = log w_i + 2 (e_i - E) log 2 relative to the largest exponent E (the E terms
|
|
123
|
+
// cancel, as sum n_i = N), and the log of the pooled variance by log-sum-exp
|
|
124
|
+
const own = samples.map((g) => {
|
|
125
|
+
const m = groupMoments([g]);
|
|
126
|
+
return { w: m.variance[0], e: m.k };
|
|
127
|
+
});
|
|
128
|
+
const top = Math.max(...own.map((o) => o.e));
|
|
129
|
+
const logs = own.map((o) => Math.log(o.w) + 2 * (o.e - top) * Math.LN2);
|
|
130
|
+
const terms = logs.map((l, i) => Math.log(n[i]) + l);
|
|
131
|
+
const peak = Math.max(...terms);
|
|
132
|
+
const logPooled = peak + Math.log(terms.reduce((a, t) => a + Math.exp(t - peak), 0)) - Math.log(N);
|
|
133
|
+
logRatio = N * logPooled - logs.reduce((a, l, i) => a + n[i] * l, 0);
|
|
134
|
+
}
|
|
135
|
+
const statistic = logRatio / (1 + (sumInv - 1 / N) / (3 * (k - 1)));
|
|
99
136
|
const df = k - 1;
|
|
100
137
|
return {
|
|
101
138
|
method: "Bartlett test of homogeneity of variances",
|
|
@@ -104,5 +141,6 @@ export function bartlettTest(x, data) {
|
|
|
104
141
|
parameter: df,
|
|
105
142
|
parameterName: "df",
|
|
106
143
|
pValue: pgamma1(statistic, df / 2, 2, false, false),
|
|
144
|
+
warnings: [],
|
|
107
145
|
};
|
|
108
146
|
}
|
package/dist/htest/wilcoxTest.js
CHANGED
|
@@ -23,9 +23,20 @@ import { pwilcox1, qwilcox1 } from "../distributions/wilcox.js";
|
|
|
23
23
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
24
24
|
import { completePairs, dropNaN, mean, rank, tieCounts, } from "../internal/summary.js";
|
|
25
25
|
import { uniroot } from "../internal/uniroot.js";
|
|
26
|
+
import { reported, Warnings } from "../internal/warnings.js";
|
|
26
27
|
import { twoSampleData } from "./testFormula.js";
|
|
27
28
|
import { matchAlternative } from "./types.js";
|
|
29
|
+
import { maxOf, midpoint, minOf } from "../internal/extremes.js";
|
|
30
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
31
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
32
|
+
import { isOptionsObject } from "../internal/optionalArgs.js";
|
|
33
|
+
const WILCOX_TEST_KEYS = [
|
|
34
|
+
"alternative", "mu", "paired", "exact", "correct", "confInt", "confLevel", "tolRoot",
|
|
35
|
+
];
|
|
28
36
|
export function wilcoxTest(x, y, options = {}) {
|
|
37
|
+
if (typeof x !== "string" && isOptionsObject(y))
|
|
38
|
+
checkOptions(y, WILCOX_TEST_KEYS, "wilcoxTest"); // generated: option-keys.mjs
|
|
39
|
+
checkOptions(options, WILCOX_TEST_KEYS, "wilcoxTest"); // generated: option-keys.mjs
|
|
29
40
|
if (typeof x === "string") {
|
|
30
41
|
if (options.paired !== undefined)
|
|
31
42
|
throw new Error("cannot use 'paired' in formula method");
|
|
@@ -78,10 +89,9 @@ export function wilcoxTest(x, y, options = {}) {
|
|
|
78
89
|
confInt,
|
|
79
90
|
confLevel,
|
|
80
91
|
tolRoot,
|
|
92
|
+
warnings: new Warnings(),
|
|
81
93
|
};
|
|
82
|
-
return yv === undefined
|
|
83
|
-
? signedRank(xs, exact, paired, ctx)
|
|
84
|
-
: rankSum(xs, yv, exact, ctx);
|
|
94
|
+
return reported("wilcoxTest", yv === undefined ? signedRank(xs, exact, paired, ctx) : rankSum(xs, yv, exact, ctx));
|
|
85
95
|
}
|
|
86
96
|
/** Continuity correction amount (two-sided: 0.5 in the direction of the statistic's sign) */
|
|
87
97
|
function correction(z, alternative, correct) {
|
|
@@ -138,10 +148,19 @@ function signedRankStat(x) {
|
|
|
138
148
|
function signedRank(x0, exactOpt, paired, ctx) {
|
|
139
149
|
const { alternative, mu, correct } = ctx;
|
|
140
150
|
let x = x0.map((v) => v - mu);
|
|
151
|
+
// A finite observation whose difference from mu overflows: the differences in half units (x / 2 - mu / 2). Then
|
|
152
|
+
// abs(mu) >= 2^970, so each half difference is exactly half the rounded difference (a subnormal x / 2 rounds far
|
|
153
|
+
// below the spacing of mu / 2) and the ranks, signs and zeros are those of the differences
|
|
154
|
+
const halved = x.some((v, i) => !Number.isFinite(v) && Number.isFinite(x0[i]));
|
|
155
|
+
if (halved)
|
|
156
|
+
x = x0.map((v) => v / 2 - mu / 2);
|
|
141
157
|
const zeroes = x.some((v) => v === 0);
|
|
142
158
|
if (zeroes)
|
|
143
159
|
x = x.filter((v) => v !== 0);
|
|
144
160
|
const n = x.length;
|
|
161
|
+
// only zero differences: nothing to rank (R returns a p-value of NaN)
|
|
162
|
+
if (n === 0)
|
|
163
|
+
throw new RangeError("no non-zero differences for the signed rank test");
|
|
145
164
|
const exact = exactOpt ?? n < 50;
|
|
146
165
|
const r = rank(x.map(Math.abs));
|
|
147
166
|
const ties = tieCounts(r).length > 0;
|
|
@@ -175,14 +194,44 @@ function signedRank(x0, exactOpt, paired, ctx) {
|
|
|
175
194
|
pValue,
|
|
176
195
|
nullValue: mu,
|
|
177
196
|
nullValueName: paired ? "location shift" : "location",
|
|
197
|
+
warnings: ctx.warnings.list,
|
|
178
198
|
};
|
|
179
|
-
|
|
199
|
+
// R's warnings of the exact method it could not use (after those of the interval)
|
|
200
|
+
const exactWarnings = () => {
|
|
201
|
+
if (exact && ties) {
|
|
202
|
+
ctx.warnings.add("cannot compute exact p-value with ties");
|
|
203
|
+
if (ctx.confInt)
|
|
204
|
+
ctx.warnings.add("cannot compute exact confidence interval with ties");
|
|
205
|
+
}
|
|
206
|
+
if (exact && zeroes) {
|
|
207
|
+
ctx.warnings.add("cannot compute exact p-value with zeroes");
|
|
208
|
+
if (ctx.confInt)
|
|
209
|
+
ctx.warnings.add("cannot compute exact confidence interval with zeroes");
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
if (!ctx.confInt) {
|
|
213
|
+
exactWarnings();
|
|
180
214
|
return result;
|
|
215
|
+
}
|
|
181
216
|
// The interval is on the original scale (add mu back to the data with zero differences removed)
|
|
182
|
-
|
|
217
|
+
// (with half units, the observations other than mu themselves)
|
|
218
|
+
// R also adds mu back. Where mu dwarfs the data, (x - mu) + mu loses the observations (rnorm(n, 0, 1e-200) with
|
|
219
|
+
// mu = 0.5 gave R's interval NaN NaN and estimate 0): then the observations themselves, which are the same values
|
|
220
|
+
// (x - mu = 0 exactly where x = mu). The bound 2^-26 of the largest observation leaves the rounding of ordinary
|
|
221
|
+
// data, an ulp or two, to R's values (#502)
|
|
222
|
+
let xo = halved ? x0.filter((v) => v !== mu) : x.map((v) => v + mu);
|
|
223
|
+
if (!halved) {
|
|
224
|
+
const direct = x0.filter((v) => v !== mu);
|
|
225
|
+
let big = 0;
|
|
226
|
+
for (const v of direct)
|
|
227
|
+
big = Math.max(big, Math.abs(v));
|
|
228
|
+
if (xo.some((v, i) => Math.abs(v - direct[i]) > 2 ** -26 * big))
|
|
229
|
+
xo = direct;
|
|
230
|
+
}
|
|
183
231
|
const ci = useExact
|
|
184
232
|
? signedRankExactCI(xo, ctx)
|
|
185
233
|
: signedRankNormalCI(xo, ctx);
|
|
234
|
+
exactWarnings();
|
|
186
235
|
return { ...result, ...ci };
|
|
187
236
|
}
|
|
188
237
|
/** Interval with the given finite ends, the other end(s) open according to the alternative */
|
|
@@ -206,8 +255,8 @@ function exactRank(quantile, alternative, a) {
|
|
|
206
255
|
return q === 0 ? 1 : q;
|
|
207
256
|
}
|
|
208
257
|
function checkTolRoot(tol) {
|
|
209
|
-
if (!(tol > 0))
|
|
210
|
-
throw new RangeError("tolRoot must be positive");
|
|
258
|
+
if (!(tol > 0 && tol < Infinity))
|
|
259
|
+
throw new RangeError("tolRoot must be a positive finite number");
|
|
211
260
|
}
|
|
212
261
|
/**
|
|
213
262
|
* Exact interval from the order statistics of the Walsh averages (Bauer 1972) and the Hodges-Lehmann estimate, their
|
|
@@ -219,16 +268,19 @@ function signedRankExactCI(x, ctx) {
|
|
|
219
268
|
const walsh = [];
|
|
220
269
|
for (let i = 0; i < n; i++)
|
|
221
270
|
for (let j = i; j < n; j++)
|
|
222
|
-
walsh.push((x[i]
|
|
271
|
+
walsh.push(midpoint(x[i], x[j]));
|
|
223
272
|
walsh.sort(ascending);
|
|
224
273
|
const N = walsh.length;
|
|
225
274
|
const a = 1 - confLevel;
|
|
226
275
|
const q = exactRank((p) => qsignrank1(p, n, true, false), alternative, a);
|
|
227
276
|
const tail = psignrank1(q - 1, n, true, false);
|
|
228
277
|
const achieved = alternative === "two.sided" ? 2 * tail : tail;
|
|
278
|
+
const notAchievable = achieved - a > a / 2;
|
|
279
|
+
if (notAchievable)
|
|
280
|
+
ctx.warnings.add("requested conf.level not achievable");
|
|
229
281
|
return {
|
|
230
282
|
confInt: shape(alternative, walsh[q - 1], walsh[N - q]),
|
|
231
|
-
confLevel:
|
|
283
|
+
confLevel: notAchievable ? 1 - signif2(achieved) : confLevel,
|
|
232
284
|
estimate: sortedMedian(walsh),
|
|
233
285
|
estimateNames: "(pseudo)median",
|
|
234
286
|
};
|
|
@@ -242,10 +294,13 @@ function signedRankNormalCI(x, ctx) {
|
|
|
242
294
|
const n = x.length;
|
|
243
295
|
if (n === 0)
|
|
244
296
|
return noInterval(alternative, NaN);
|
|
245
|
-
const lo =
|
|
246
|
-
const hi =
|
|
297
|
+
const lo = minOf(x);
|
|
298
|
+
const hi = maxOf(x);
|
|
247
299
|
if (!Number.isFinite(lo) || !Number.isFinite(hi) || n === 1 || lo === hi) {
|
|
248
|
-
|
|
300
|
+
// R's statistic of x - lo has no non-zero values when all observations are equal (n = 1 included)
|
|
301
|
+
if (Number.isFinite(lo) && lo === hi)
|
|
302
|
+
ctx.warnings.add("cannot compute confidence interval when all observations are zero or tied");
|
|
303
|
+
return noInterval(alternative, midpoint(lo, hi), "midrange");
|
|
249
304
|
}
|
|
250
305
|
const stat = (d, corr) => {
|
|
251
306
|
const [, centre, sigma] = signedRankStat(x.map((v) => v - d).filter((v) => v !== 0));
|
|
@@ -267,6 +322,8 @@ function signedRankNormalCI(x, ctx) {
|
|
|
267
322
|
a *= 2;
|
|
268
323
|
enlarged = true;
|
|
269
324
|
}
|
|
325
|
+
if (enlarged)
|
|
326
|
+
ctx.warnings.add("requested conf.level not achievable");
|
|
270
327
|
checkTolRoot(tolRoot);
|
|
271
328
|
const crossing = (z) => uniroot((d) => stat(d, correct) - z, lo, hi, { tol: tolRoot, fLower: sLo - z, fUpper: sHi - z });
|
|
272
329
|
let confInt;
|
|
@@ -309,33 +366,89 @@ function rankSumStat(x, y) {
|
|
|
309
366
|
const sigma = Math.sqrt(((nx * ny) / 12) * (N + 1 - ties / (N * (N - 1))));
|
|
310
367
|
return [W, W - (nx * ny) / 2, sigma];
|
|
311
368
|
}
|
|
369
|
+
/**
|
|
370
|
+
* rankSumStat for x_i - d given in half units h_i = x_i / 2 - d / 2 (used where a finite x_i - d overflows, so
|
|
371
|
+
* abs(d) >= 2^970), with whether there are ties. y stays in its own units: h_i is compared with y_j / 2, which is exact
|
|
372
|
+
* except for a subnormal y_j, and there h_i is 0 (compared with y_j itself) or of magnitude above 2^900, so the order
|
|
373
|
+
* is that of the differences. Halving y would merge neighbouring subnormals
|
|
374
|
+
*/
|
|
375
|
+
function rankSumHalf(h, y) {
|
|
376
|
+
const nx = h.length;
|
|
377
|
+
const ny = y.length;
|
|
378
|
+
const N = nx + ny;
|
|
379
|
+
// sign of 2 h - v (h in half units, v in units)
|
|
380
|
+
const cmpHalf = (hv, v) => {
|
|
381
|
+
const w = hv === 0 || !Number.isFinite(hv) || !Number.isFinite(v) ? v : v / 2;
|
|
382
|
+
return hv < w ? -1 : hv > w ? 1 : 0;
|
|
383
|
+
};
|
|
384
|
+
const cmp = (a, b) => {
|
|
385
|
+
if (a < nx && b < nx)
|
|
386
|
+
return h[a] < h[b] ? -1 : h[a] > h[b] ? 1 : 0;
|
|
387
|
+
if (a >= nx && b >= nx)
|
|
388
|
+
return y[a - nx] < y[b - nx] ? -1 : y[a - nx] > y[b - nx] ? 1 : 0;
|
|
389
|
+
return a < nx ? cmpHalf(h[a], y[b - nx]) : -cmpHalf(h[b], y[a - nx]);
|
|
390
|
+
};
|
|
391
|
+
const idx = Array.from({ length: N }, (_, i) => i);
|
|
392
|
+
idx.sort((a, b) => cmp(a, b) || a - b);
|
|
393
|
+
let s = 0;
|
|
394
|
+
let ties = 0;
|
|
395
|
+
let i = 0;
|
|
396
|
+
while (i < N) {
|
|
397
|
+
let j = i;
|
|
398
|
+
while (j + 1 < N && cmp(idx[j + 1], idx[i]) === 0)
|
|
399
|
+
j++;
|
|
400
|
+
const avg = (i + j + 2) / 2;
|
|
401
|
+
for (let k = i; k <= j; k++)
|
|
402
|
+
if (idx[k] < nx)
|
|
403
|
+
s += avg;
|
|
404
|
+
const t = j - i + 1;
|
|
405
|
+
ties += t * t * t - t;
|
|
406
|
+
i = j + 1;
|
|
407
|
+
}
|
|
408
|
+
const W = s - (nx * (nx + 1)) / 2;
|
|
409
|
+
const sigma = Math.sqrt(((nx * ny) / 12) * (N + 1 - ties / (N * (N - 1))));
|
|
410
|
+
return [W, W - (nx * ny) / 2, sigma, ties > 0];
|
|
411
|
+
}
|
|
312
412
|
function rankSum(x, y, exactOpt, ctx) {
|
|
313
413
|
const { alternative, mu, correct } = ctx;
|
|
314
414
|
const nx = x.length;
|
|
315
415
|
const ny = y.length;
|
|
316
|
-
|
|
416
|
+
let xs = x.map((v) => v - mu);
|
|
317
417
|
const exact = exactOpt ?? (nx < 50 && ny < 50);
|
|
318
|
-
|
|
319
|
-
const
|
|
418
|
+
// A finite x_i - mu that overflows: ranked from x / 2 - mu / 2 against y (see rankSumHalf)
|
|
419
|
+
const halved = xs.some((v, i) => !Number.isFinite(v) && Number.isFinite(x[i]));
|
|
420
|
+
let ties;
|
|
421
|
+
let W, dz, sigma;
|
|
422
|
+
if (halved) {
|
|
423
|
+
xs = x.map((v) => v / 2 - mu / 2);
|
|
424
|
+
[W, dz, sigma, ties] = rankSumHalf(xs, y);
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
ties = tieCounts([...xs, ...y]).length > 0;
|
|
428
|
+
[W, dz, sigma] = rankSumStat(xs, y);
|
|
429
|
+
}
|
|
320
430
|
let pValue;
|
|
321
431
|
let method;
|
|
322
432
|
const useExact = exact && !ties;
|
|
323
433
|
if (useExact) {
|
|
324
434
|
method = "Wilcoxon rank sum exact test";
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
435
|
+
pValue = rankSumExact(() => {
|
|
436
|
+
if (alternative === "less")
|
|
437
|
+
return pwilcox1(W, nx, ny, true, false);
|
|
438
|
+
if (alternative === "greater")
|
|
439
|
+
return pwilcox1(W - 1, nx, ny, false, false);
|
|
330
440
|
const p = W > (nx * ny) / 2
|
|
331
441
|
? pwilcox1(W - 1, nx, ny, false, false)
|
|
332
442
|
: pwilcox1(W, nx, ny, true, false);
|
|
333
|
-
|
|
334
|
-
}
|
|
443
|
+
return Math.min(1, 2 * p);
|
|
444
|
+
});
|
|
335
445
|
}
|
|
336
446
|
else {
|
|
337
447
|
method = `Wilcoxon rank sum test${correct ? " with continuity correction" : ""}`;
|
|
338
|
-
|
|
448
|
+
// all values tied: W always equals its expectation, so p = 1 (R 4.5.2: 0 / 0 = NaN two-sided; R 4.6 returns 1)
|
|
449
|
+
// (never with half units: an overflowing difference equals no y)
|
|
450
|
+
const allTied = !halved && xs.every((v) => v === y[0]) && y.every((v) => v === y[0]);
|
|
451
|
+
pValue = allTied ? 1 : normalP((dz - correction(dz, alternative, correct)) / sigma, alternative);
|
|
339
452
|
}
|
|
340
453
|
const result = {
|
|
341
454
|
method,
|
|
@@ -345,12 +458,31 @@ function rankSum(x, y, exactOpt, ctx) {
|
|
|
345
458
|
pValue,
|
|
346
459
|
nullValue: mu,
|
|
347
460
|
nullValueName: "location shift",
|
|
461
|
+
warnings: ctx.warnings.list,
|
|
348
462
|
};
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
463
|
+
const ci = !ctx.confInt ? {} : useExact ? rankSumExact(() => rankSumExactCI(x, y, ctx)) : rankSumNormalCI(x, y, ctx);
|
|
464
|
+
// R's warnings of the exact method it could not use (after those of the interval)
|
|
465
|
+
if (exact && ties) {
|
|
466
|
+
ctx.warnings.add("cannot compute exact p-value with ties");
|
|
467
|
+
if (ctx.confInt)
|
|
468
|
+
ctx.warnings.add("cannot compute exact confidence intervals with ties");
|
|
469
|
+
}
|
|
352
470
|
return { ...result, ...ci };
|
|
353
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* f, with the RangeErrors of the exact distribution (a table too large or too long to build, or inaccurate near the
|
|
474
|
+
* center of large samples) pointing to exact: false
|
|
475
|
+
*/
|
|
476
|
+
function rankSumExact(f) {
|
|
477
|
+
try {
|
|
478
|
+
return f();
|
|
479
|
+
}
|
|
480
|
+
catch (e) {
|
|
481
|
+
if (e instanceof RangeError && e.message.startsWith("wilcox:"))
|
|
482
|
+
throw new RangeError(`${e.message}: use exact: false`);
|
|
483
|
+
throw e;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
354
486
|
/** Exact interval from the order statistics of the differences x_i - y_j and their median as the estimate */
|
|
355
487
|
function rankSumExactCI(x, y, ctx) {
|
|
356
488
|
const { alternative, confLevel } = ctx;
|
|
@@ -366,9 +498,12 @@ function rankSumExactCI(x, y, ctx) {
|
|
|
366
498
|
const q = exactRank((p) => qwilcox1(p, nx, ny, true, false), alternative, a);
|
|
367
499
|
const tail = pwilcox1(q - 1, nx, ny, true, false);
|
|
368
500
|
const achieved = alternative === "two.sided" ? 2 * tail : tail;
|
|
501
|
+
const notAchievable = achieved - a > a / 2;
|
|
502
|
+
if (notAchievable)
|
|
503
|
+
ctx.warnings.add("Requested conf.level not achievable");
|
|
369
504
|
return {
|
|
370
505
|
confInt: shape(alternative, diffs[q - 1], diffs[M - q]),
|
|
371
|
-
confLevel:
|
|
506
|
+
confLevel: notAchievable ? 1 - achieved : confLevel,
|
|
372
507
|
estimate: sortedMedian(diffs),
|
|
373
508
|
estimateNames: "difference in location",
|
|
374
509
|
};
|
|
@@ -379,8 +514,8 @@ function rankSumExactCI(x, y, ctx) {
|
|
|
379
514
|
*/
|
|
380
515
|
function rankSumNormalCI(x, y, ctx) {
|
|
381
516
|
const { alternative, correct, confLevel, tolRoot } = ctx;
|
|
382
|
-
const lo =
|
|
383
|
-
const hi =
|
|
517
|
+
const lo = minOf(x) - maxOf(y);
|
|
518
|
+
const hi = maxOf(x) - minOf(y);
|
|
384
519
|
if (!Number.isFinite(lo) || !Number.isFinite(hi)) {
|
|
385
520
|
throw new Error("cannot compute the confidence interval because the differences are not finite");
|
|
386
521
|
}
|
|
@@ -275,11 +275,31 @@ function dqpsrt(limit, last, maxerr, elist, iord, nrmax) {
|
|
|
275
275
|
* transformed) range [a0, b0]; `neval` gives the number of evaluations from the number of subintervals
|
|
276
276
|
*/
|
|
277
277
|
function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
278
|
+
// The work arrays (length limit + 1 in QUADPACK) start small and are doubled as subintervals are added, up to
|
|
279
|
+
// limit + 1: iteration L reads and writes indices up to L + 1 at most (the final sum reads rlist[L]), and the unused
|
|
280
|
+
// entries are 0 as in a full-length array, so the result is the same; a large subdivisions no longer allocates
|
|
281
|
+
// 36 (limit + 1) bytes up front (#369)
|
|
282
|
+
let cap = Math.min(limit + 1, 128);
|
|
283
|
+
let alist = new Float64Array(cap);
|
|
284
|
+
let blist = new Float64Array(cap);
|
|
285
|
+
let rlist = new Float64Array(cap);
|
|
286
|
+
let elist = new Float64Array(cap);
|
|
287
|
+
let iord = new Int32Array(cap);
|
|
288
|
+
const grow = (need) => {
|
|
289
|
+
cap = Math.min(limit + 1, Math.max(need, 2 * cap));
|
|
290
|
+
const f64 = (old) => {
|
|
291
|
+
const a = new Float64Array(cap);
|
|
292
|
+
a.set(old);
|
|
293
|
+
return a;
|
|
294
|
+
};
|
|
295
|
+
alist = f64(alist);
|
|
296
|
+
blist = f64(blist);
|
|
297
|
+
rlist = f64(rlist);
|
|
298
|
+
elist = f64(elist);
|
|
299
|
+
const io = new Int32Array(cap);
|
|
300
|
+
io.set(iord);
|
|
301
|
+
iord = io;
|
|
302
|
+
};
|
|
283
303
|
let ier = 0;
|
|
284
304
|
let last = 0;
|
|
285
305
|
let result = 0;
|
|
@@ -332,6 +352,8 @@ function adaptive(rule, a0, b0, epsabs, epsrel, limit, smallStart, neval) {
|
|
|
332
352
|
let L = 1;
|
|
333
353
|
for (L = 1; L < limit; L++) {
|
|
334
354
|
last = L + 1;
|
|
355
|
+
if (cap < L + 2)
|
|
356
|
+
grow(L + 2);
|
|
335
357
|
// bisect the subinterval with the nrmax-th largest error estimate
|
|
336
358
|
const a1 = alist[maxerr];
|
|
337
359
|
const b1 = 0.5 * (alist[maxerr] + blist[maxerr]);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws a RangeError if `options` has an enumerable key that is not in `keys`. `undefined` and `null` options, and
|
|
3
|
+
* values that are not objects, are left to the function. A key whose value is `undefined` is still checked. `path` is
|
|
4
|
+
* the key of a nested options object (such as `"control"`), written before the unknown key in the message.
|
|
5
|
+
*/
|
|
6
|
+
export declare function checkOptions(options: unknown, keys: ReadonlySet<string> | readonly string[], fnName: string, path?: string): void;
|
|
7
|
+
/**
|
|
8
|
+
* The nested options object at `path` (keys joined by dots, as `"family.link"`) of `options`, or `undefined` where a
|
|
9
|
+
* step is not an object (a family given by name): what checkOptions is called with for a nested object.
|
|
10
|
+
*/
|
|
11
|
+
export declare function nestedOptions(options: unknown, path: string): unknown;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// The check that an options object has no key the function does not know: a misspelt key ({ lowertail: false })
|
|
2
|
+
// would otherwise be ignored and give a wrong result without a sign. The key lists are generated from the option
|
|
3
|
+
// types by scripts/option-keys.mjs.
|
|
4
|
+
/**
|
|
5
|
+
* Throws a RangeError if `options` has an enumerable key that is not in `keys`. `undefined` and `null` options, and
|
|
6
|
+
* values that are not objects, are left to the function. A key whose value is `undefined` is still checked. `path` is
|
|
7
|
+
* the key of a nested options object (such as `"control"`), written before the unknown key in the message.
|
|
8
|
+
*/
|
|
9
|
+
export function checkOptions(options, keys, fnName, path) {
|
|
10
|
+
if (typeof options !== "object" || options === null)
|
|
11
|
+
return;
|
|
12
|
+
for (const key in options) {
|
|
13
|
+
if (Array.isArray(keys) ? keys.includes(key) : keys.has(key))
|
|
14
|
+
continue;
|
|
15
|
+
const name = path === undefined ? key : `${path}.${key}`;
|
|
16
|
+
throw new RangeError(`unknown option '${name}' for ${fnName} (known: ${[...keys].join(", ")})`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The nested options object at `path` (keys joined by dots, as `"family.link"`) of `options`, or `undefined` where a
|
|
21
|
+
* step is not an object (a family given by name): what checkOptions is called with for a nested object.
|
|
22
|
+
*/
|
|
23
|
+
export function nestedOptions(options, path) {
|
|
24
|
+
let v = options;
|
|
25
|
+
for (const key of path.split(".")) {
|
|
26
|
+
if (typeof v !== "object" || v === null)
|
|
27
|
+
return undefined;
|
|
28
|
+
v = v[key];
|
|
29
|
+
}
|
|
30
|
+
return v;
|
|
31
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Throws unless the matrix is square, finite, symmetric (as R's isSymmetric: relative difference from its transpose at
|
|
3
|
+
* most 100 ε) and has a positive diagonal. R's factanal takes any matrix: an asymmetric one gave a result
|
|
4
|
+
*/
|
|
5
|
+
export declare function checkCovmat(S: readonly (readonly number[])[]): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// The check of a covariance (or correlation) matrix given as `covmat` (factanal, omega).
|
|
2
|
+
import { isSymmetric } from "../linalg/eigen.js";
|
|
3
|
+
import { toMat } from "../linalg/matrix.js";
|
|
4
|
+
/**
|
|
5
|
+
* Throws unless the matrix is square, finite, symmetric (as R's isSymmetric: relative difference from its transpose at
|
|
6
|
+
* most 100 ε) and has a positive diagonal. R's factanal takes any matrix: an asymmetric one gave a result
|
|
7
|
+
*/
|
|
8
|
+
export function checkCovmat(S) {
|
|
9
|
+
const p = S.length;
|
|
10
|
+
if (p === 0 || S.some((r) => r.length !== p))
|
|
11
|
+
throw new RangeError("'covmat' must be a square matrix");
|
|
12
|
+
if (S.some((r) => r.some((v) => !Number.isFinite(v))))
|
|
13
|
+
throw new RangeError("'covmat' must be finite");
|
|
14
|
+
if (S.some((r, i) => !(r[i] > 0)))
|
|
15
|
+
throw new RangeError("the diagonal of 'covmat' must be positive");
|
|
16
|
+
// in the matrix's own units, as R's isSymmetric (isSymmetric handles sums that overflow; the matrix scaled to its
|
|
17
|
+
// largest entry first took [[1e308, 1e-10], [0, 1]] for a symmetric one, issue #99)
|
|
18
|
+
if (!isSymmetric(toMat(S)))
|
|
19
|
+
throw new RangeError("'covmat' must be symmetric");
|
|
20
|
+
}
|
package/dist/internal/ddexp.d.ts
CHANGED
|
@@ -12,7 +12,10 @@ export declare function standardizeDD(x: number, location: number, scale: number
|
|
|
12
12
|
* subnormal, 0 or overflows (the log of a subnormal ratio has lost them in the division)
|
|
13
13
|
*/
|
|
14
14
|
export declare function logRatioDD(a: number, b: number): DD;
|
|
15
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* a b as double-double: twoProd, which keeps the error term of a finite product also where Veltkamp splitting would
|
|
17
|
+
* overflow (it splits the operands scaled by powers of 2); only the double product when that is not finite
|
|
18
|
+
*/
|
|
16
19
|
export declare function mulDD(a: number, b: number): DD;
|
|
17
20
|
/** exp(-t) (t is double-double). The low-order part of t enters to first order */
|
|
18
21
|
export declare function expNeg(t: DD): number;
|
|
@@ -26,7 +29,18 @@ export declare function expNeg(t: DD): number;
|
|
|
26
29
|
export declare function expNegOver(t: DD, s: number): number;
|
|
27
30
|
/** log(1 - exp(x)) (x <= 0). Switches method by the size of x to avoid cancellation */
|
|
28
31
|
export declare function log1mExp(x: number): number;
|
|
32
|
+
/** log(e^a + e^b) */
|
|
33
|
+
export declare function logAddExp(a: number, b: number): number;
|
|
34
|
+
/** log(e^a - e^b) for a >= b */
|
|
35
|
+
export declare function logSubExp(a: number, b: number): number;
|
|
29
36
|
/** Log of the lower probability 1 - exp(-t) (t double-double, t > 0). For large t, exp(-t) includes the low-order part of t */
|
|
30
37
|
export declare function logOneMinusExpNeg(t: DD): number;
|
|
31
38
|
/** location + scale t with a single rounding (the rounding of scale t would show up in the subtraction with location) */
|
|
32
39
|
export declare function locationScale(location: number, scale: number, t: number): number;
|
|
40
|
+
/**
|
|
41
|
+
* (x - location) / scale in double, as the plain formula gives it, but from the halves of x and location when their
|
|
42
|
+
* difference overflows although both are finite (z itself may be in range)
|
|
43
|
+
*/
|
|
44
|
+
export declare function standardizeValue(x: number, location: number, scale: number): number;
|
|
45
|
+
/** location + scale t in double, as the plain formula gives it, unless scale t overflows (then as locationScale) */
|
|
46
|
+
export declare function affine(location: number, scale: number, t: number): number;
|