@numero-uno-jpn/rstats 0.15.2 → 0.16.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 +280 -0
- package/README.ja.md +40 -33
- package/README.md +36 -28
- package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
- package/dist/chunks/chunk-5UQCRBAG.js +2 -0
- package/dist/chunks/chunk-6CSTWA5E.js +2 -0
- package/dist/chunks/chunk-72Y3HZID.js +2 -0
- package/dist/chunks/chunk-BG2KLEHM.js +2 -0
- package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
- package/dist/chunks/chunk-FLJNSXSW.js +2 -0
- package/dist/chunks/chunk-GM53LWSR.js +2 -0
- package/dist/chunks/chunk-JBCA56ZS.js +3 -0
- package/dist/chunks/chunk-K3RJAD2T.js +2 -0
- package/dist/chunks/chunk-KAZRWIB5.js +2 -0
- package/dist/chunks/chunk-KBVVIMXX.js +3 -0
- package/dist/chunks/chunk-N3SIFCZX.js +2 -0
- package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
- package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
- package/dist/chunks/chunk-SYWOBOTR.js +2 -0
- package/dist/chunks/chunk-URIO5LY5.js +2 -0
- package/dist/chunks/chunk-VTUGG2SV.js +2 -0
- package/dist/chunks/chunk-W26LRYOJ.js +2 -0
- package/dist/chunks/chunk-XHETEYVC.js +2 -0
- package/dist/chunks/{chunk-PWVQVJTP.js → chunk-YRZWONZB.js} +1 -1
- package/dist/chunks/chunk-ZGABZMWX.js +2 -0
- package/dist/data/grouping.js +20 -5
- package/dist/descriptive/basic.js +90 -8
- package/dist/descriptive/cov.js +12 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +56 -18
- package/dist/descriptive/factanal.js +16 -7
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +71 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +72 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +68 -9
- package/dist/descriptive/pivot.d.ts +21 -9
- package/dist/descriptive/pivot.js +161 -35
- package/dist/descriptive/quantile.js +2 -1
- package/dist/descriptive/rank.js +4 -1
- package/dist/descriptive/table.d.ts +3 -2
- package/dist/descriptive/table.js +24 -9
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +19 -2
- package/dist/distributions/beta.js +35 -5
- package/dist/distributions/binomial.js +37 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +87 -8
- package/dist/distributions/chisq.js +7 -3
- package/dist/distributions/exponential.js +11 -1
- package/dist/distributions/f.js +73 -22
- package/dist/distributions/gamma.js +61 -6
- package/dist/distributions/geometric.js +53 -14
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +93 -6
- package/dist/distributions/invgamma.js +6 -1
- package/dist/distributions/kumaraswamy.js +22 -4
- package/dist/distributions/laplace.js +31 -7
- package/dist/distributions/logistic.js +11 -2
- package/dist/distributions/lognormal.js +20 -3
- package/dist/distributions/multinomial.js +54 -11
- package/dist/distributions/nbeta.js +128 -35
- package/dist/distributions/nchisq.js +85 -14
- package/dist/distributions/negbinomial.js +100 -18
- package/dist/distributions/normal.js +35 -5
- package/dist/distributions/nt.d.ts +28 -3
- package/dist/distributions/nt.js +536 -35
- package/dist/distributions/pareto.js +9 -1
- package/dist/distributions/poisson.js +28 -3
- package/dist/distributions/signrank.js +7 -4
- package/dist/distributions/summary.js +450 -60
- package/dist/distributions/t.js +55 -3
- package/dist/distributions/triangular.js +45 -13
- package/dist/distributions/tukey.js +21 -2
- package/dist/distributions/uniform.js +2 -0
- package/dist/distributions/weibull.js +33 -3
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +155 -26
- package/dist/distributions.js +2 -2
- package/dist/htest/agreement.d.ts +3 -2
- package/dist/htest/agreement.js +76 -14
- package/dist/htest/ansariTest.d.ts +6 -1
- package/dist/htest/ansariTest.js +75 -8
- package/dist/htest/aov.d.ts +1 -1
- package/dist/htest/aov.js +48 -34
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +41 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +7 -5
- package/dist/htest/chisqTest.d.ts +2 -1
- package/dist/htest/chisqTest.js +130 -43
- package/dist/htest/cor.d.ts +2 -1
- package/dist/htest/cor.js +17 -19
- 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/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +2 -0
- package/dist/htest/effectSize.js +91 -29
- package/dist/htest/fisherTest.d.ts +2 -2
- package/dist/htest/fisherTest.js +36 -18
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +3 -1
- package/dist/htest/ksTest.js +23 -2
- package/dist/htest/leveneTest.js +19 -3
- package/dist/htest/mantelhaenTest.js +125 -25
- package/dist/htest/mcnemarTest.js +32 -11
- package/dist/htest/multipleComparisons.d.ts +8 -1
- package/dist/htest/multipleComparisons.js +44 -21
- package/dist/htest/normalityTests.js +44 -10
- package/dist/htest/onewayTest.js +96 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +38 -6
- package/dist/htest/pairwise.d.ts +1 -1
- package/dist/htest/pairwise.js +17 -15
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +111 -59
- package/dist/htest/polycor.js +27 -7
- package/dist/htest/power.d.ts +11 -1
- package/dist/htest/power.js +112 -8
- package/dist/htest/propTest.js +21 -4
- package/dist/htest/pwr.js +121 -24
- package/dist/htest/rankTests.js +29 -5
- package/dist/htest/tTest.js +44 -21
- package/dist/htest/trendTests.d.ts +7 -3
- package/dist/htest/trendTests.js +114 -20
- package/dist/htest/types.d.ts +5 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +45 -14
- package/dist/htest/wilcoxTest.js +114 -21
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +23 -1
- package/dist/internal/ddexp.js +86 -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.d.ts +5 -0
- package/dist/internal/invert.js +25 -7
- package/dist/internal/noncentral.d.ts +7 -0
- package/dist/internal/noncentral.js +9 -0
- package/dist/internal/poissonMixture.js +29 -6
- 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/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +51 -7
- package/dist/linalg/chol.js +4 -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 +71 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +109 -15
- package/dist/linalg/kappa.js +58 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +35 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/ops.js +60 -13
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +91 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +147 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +29 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +124 -25
- package/dist/models/aovEz.js +140 -30
- package/dist/models/aovStrata.js +55 -22
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +83 -32
- package/dist/models/coxph.js +148 -66
- 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 +35 -5
- package/dist/models/glm.js +398 -55
- package/dist/models/glmAnova.js +84 -13
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +115 -27
- package/dist/models/glmFamily.d.ts +10 -0
- package/dist/models/glmFamily.js +244 -40
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +235 -36
- package/dist/models/lmer.d.ts +20 -1
- package/dist/models/lmer.js +109 -27
- package/dist/models/lmerAnova.js +37 -4
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +9 -2
- package/dist/models/lmerTest.js +64 -19
- package/dist/models/mauchly.d.ts +4 -1
- package/dist/models/mauchly.js +34 -13
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +8 -2
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +40 -15
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +6 -1
- package/dist/models/survival.js +67 -27
- 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 +64 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +103 -16
- package/dist/numerics/interp.d.ts +8 -2
- package/dist/numerics/interp.js +352 -50
- package/dist/numerics/ode.js +55 -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 +80 -8
- package/dist/random/beta.js +53 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +33 -5
- package/dist/random/extra.js +42 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +36 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +26 -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/sample.d.ts +12 -2
- package/dist/random/sample.js +57 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +168 -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 +11 -1
- package/dist/special/choose.js +17 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +15 -2
- package/dist/special/igam.js +33 -6
- package/dist/special/incbet.js +100 -10
- package/dist/special/incbetLargeA.js +7 -2
- package/dist/special/incbetPrefactor.d.ts +5 -0
- package/dist/special/incbetPrefactor.js +14 -3
- 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 +179 -33
- package/dist/special/log1pmx.js +4 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -11
- package/llms.txt +12 -6
- package/package.json +1 -1
- package/dist/chunks/chunk-2CUKVVRO.js +0 -2
- package/dist/chunks/chunk-3WQCKGKP.js +0 -2
- package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
- package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
- package/dist/chunks/chunk-D4F7GQAY.js +0 -2
- package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
- package/dist/chunks/chunk-HBMHR5SS.js +0 -2
- package/dist/chunks/chunk-IBPWRQIF.js +0 -2
- package/dist/chunks/chunk-JLAH3X77.js +0 -2
- package/dist/chunks/chunk-LG74QY5U.js +0 -2
- package/dist/chunks/chunk-PEYGUK32.js +0 -2
- package/dist/chunks/chunk-Q4PGYESP.js +0 -2
- package/dist/chunks/chunk-RCAL4VHM.js +0 -3
- package/dist/chunks/chunk-SREPSVTT.js +0 -2
- package/dist/chunks/chunk-UFT662QZ.js +0 -2
- package/dist/chunks/chunk-VWJ74UHC.js +0 -2
- package/dist/chunks/chunk-XNPU54NH.js +0 -2
- package/dist/chunks/chunk-XPLDEK6D.js +0 -2
- package/dist/chunks/chunk-YGT4P7EU.js +0 -2
- package/dist/chunks/chunk-YVREYVI2.js +0 -2
package/dist/htest/power.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { type UnirootOptions } from "../numerics/integrate.js";
|
|
|
3
3
|
export declare function matchChoice<T extends string>(value: string | undefined, choices: readonly T[]): T;
|
|
4
4
|
/** Whether a quantity is to be solved for: null, or omitted when R gives it no default */
|
|
5
5
|
export declare const isUnknown: (v: unknown) => v is null | undefined;
|
|
6
|
-
/** The error when not exactly one of the quantities is unknown */
|
|
7
6
|
export declare function checkExactlyOne(values: readonly unknown[], message: string): void;
|
|
8
7
|
/** The root of f(x) = 0 by uniroot, as the power functions call it */
|
|
9
8
|
export declare function solveFor(f: (x: number) => number, lower: number, upper: number, options?: UnirootOptions): number;
|
|
@@ -172,6 +171,17 @@ export interface PowerAnovaTestResult {
|
|
|
172
171
|
/** Description of the calculation. */
|
|
173
172
|
method: string;
|
|
174
173
|
}
|
|
174
|
+
/** Whether an F test's power is taken from the normal approximation (df1 finite, both above 1e16) */
|
|
175
|
+
export declare const hugeDf: (df1: number, df2: number) => boolean;
|
|
176
|
+
/**
|
|
177
|
+
* The power P[F > qf(sig)] of an F test with noncentrality ncp where both degrees of freedom exceed 1e16 (df2 may be
|
|
178
|
+
* Infinity). There the critical value 1 + O(df^-1/2) keeps only a few significant digits beyond 1 in doubles (and
|
|
179
|
+
* rounds to 1 for the largest), so that pf(qf(...)) loses the test; F = (χ²(df1, λ)/df1) / (χ²_df2/df2) is normal
|
|
180
|
+
* instead, with mean 1 + μ, μ = λ/df1, and variance 2 (1 + 2μ)/df1 + 2 (1 + μ)²/df2 (2/df1 + 2/df2 under the null),
|
|
181
|
+
* to within O(df1^-1/2) in the power (about 1e-8 at 1e16, 1e-10 at 1e20).
|
|
182
|
+
* The standard deviation is formed as a hypot so that (1 + μ)² does not overflow.
|
|
183
|
+
*/
|
|
184
|
+
export declare function fPowerHugeDf(df1: number, df2: number, ncp: number, sig: number): number;
|
|
175
185
|
/**
|
|
176
186
|
* Power of the F test of a balanced one-way analysis of variance, or one of the other quantities for a given power.
|
|
177
187
|
* Equivalent to R's `power.anova.test()`.
|
package/dist/htest/power.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { pf, qf } from "../distributions/f.js";
|
|
8
8
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
9
9
|
import { pt, qt } from "../distributions/t.js";
|
|
10
|
+
import { ldexp } from "../internal/summary.js";
|
|
10
11
|
import { uniroot } from "../numerics/integrate.js";
|
|
11
12
|
const TOL = Number.EPSILON ** 0.25;
|
|
12
13
|
/** R's match.arg: the value in full, also from an unambiguous abbreviation */
|
|
@@ -25,6 +26,13 @@ export function matchChoice(value, choices) {
|
|
|
25
26
|
/** Whether a quantity is to be solved for: null, or omitted when R gives it no default */
|
|
26
27
|
export const isUnknown = (v) => v === null || v === undefined;
|
|
27
28
|
/** The error when not exactly one of the quantities is unknown */
|
|
29
|
+
/** Throws unless a known (non-null) value satisfies ok */
|
|
30
|
+
function checkKnown(v, ok, message) {
|
|
31
|
+
if (!isUnknown(v) && !ok(v))
|
|
32
|
+
throw new RangeError(message);
|
|
33
|
+
}
|
|
34
|
+
const isProb = (v) => v >= 0 && v <= 1;
|
|
35
|
+
const isPositiveFinite = (v) => v > 0 && v < Infinity;
|
|
28
36
|
export function checkExactlyOne(values, message) {
|
|
29
37
|
if (values.filter(isUnknown).length !== 1)
|
|
30
38
|
throw new RangeError(message);
|
|
@@ -59,6 +67,11 @@ export function powerTTest(options = {}) {
|
|
|
59
67
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
60
68
|
checkExactlyOne([n, delta, sd, power, sigLevel], "exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL");
|
|
61
69
|
checkSigLevel(sigLevel, SIG_MESSAGE);
|
|
70
|
+
checkKnown(power, isProb, "'power' must be a probability");
|
|
71
|
+
checkKnown(sd, isPositiveFinite, "'sd' must be a positive finite number");
|
|
72
|
+
checkKnown(n, (v) => v >= 2, "number of observations in each group must be at least 2");
|
|
73
|
+
checkKnown(delta, (v) => !Number.isNaN(v), "'delta' must not be NaN");
|
|
74
|
+
checkKnown(options.tol, isPositiveFinite, "'tol' must be a positive finite number");
|
|
62
75
|
const type = matchChoice(options.type, ["two.sample", "one.sample", "paired"]);
|
|
63
76
|
const alternative = matchChoice(options.alternative, ["two.sided", "one.sided"]);
|
|
64
77
|
const strict = options.strict ?? false;
|
|
@@ -70,7 +83,12 @@ export function powerTTest(options = {}) {
|
|
|
70
83
|
const powerOf = (n, delta, sd, sig) => {
|
|
71
84
|
const nu = Math.max(1e-7, n - 1) * ts;
|
|
72
85
|
const q = qt(sig / sides, nu, { lowerTail: false });
|
|
73
|
-
|
|
86
|
+
let ncp = (Math.sqrt(n / ts) * delta) / sd;
|
|
87
|
+
// sqrt(n / ts) * delta can overflow before the division by a large sd (#197); with the ratio delta / sd first the
|
|
88
|
+
// noncentrality is Infinity only where it is beyond the doubles itself (sqrt(n / ts) >= 1). Only there: elsewhere
|
|
89
|
+
// R's order, with its bits
|
|
90
|
+
if (!Number.isFinite(ncp))
|
|
91
|
+
ncp = Math.sqrt(n / ts) * (delta / sd);
|
|
74
92
|
const p = ptNcp(q, nu, ncp, false);
|
|
75
93
|
return strict && sides === 2 ? p + ptNcp(-q, nu, ncp, true) : p;
|
|
76
94
|
};
|
|
@@ -78,10 +96,19 @@ export function powerTTest(options = {}) {
|
|
|
78
96
|
power = powerOf(n, delta, sd, sigLevel);
|
|
79
97
|
else {
|
|
80
98
|
const target = power;
|
|
81
|
-
if (isUnknown(n))
|
|
99
|
+
if (isUnknown(n)) {
|
|
100
|
+
// with delta = 0 the power does not depend on n (R and before #311: the search failed with "no sign change")
|
|
101
|
+
if (delta === 0)
|
|
102
|
+
throw new RangeError("'n' cannot be determined when 'delta' is 0: the power does not depend on it");
|
|
82
103
|
n = solveFor((x) => powerOf(x, delta, sd, sigLevel) - target, 2, 1e7, { tol, extendInt: "upX" });
|
|
104
|
+
}
|
|
83
105
|
else if (isUnknown(sd)) {
|
|
84
|
-
|
|
106
|
+
// with delta = 0 the power does not depend on sd (R: "lower < upper is not fulfilled"); with a negative delta (one
|
|
107
|
+
// sided) the power rises towards sigLevel as sd grows, so the search runs over |delta| the other way
|
|
108
|
+
if (delta === 0)
|
|
109
|
+
throw new RangeError("'sd' cannot be determined when 'delta' is 0: the power does not depend on it");
|
|
110
|
+
const ad = Math.abs(delta);
|
|
111
|
+
sd = solveFor((x) => powerOf(n, delta, x, sigLevel) - target, ad * 1e-7, ad * 1e7, { tol, extendInt: delta > 0 ? "downX" : "upX" });
|
|
85
112
|
}
|
|
86
113
|
else if (isUnknown(delta)) {
|
|
87
114
|
delta = solveFor((x) => powerOf(n, x, sd, sigLevel) - target, sd * 1e-7, sd * 1e7, { tol, extendInt: "upX" });
|
|
@@ -123,6 +150,12 @@ export function powerPropTest(options = {}) {
|
|
|
123
150
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
124
151
|
checkExactlyOne([n, p1, p2, power, sigLevel], "exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL");
|
|
125
152
|
checkSigLevel(sigLevel, SIG_MESSAGE);
|
|
153
|
+
checkKnown(power, isProb, "'power' must be a probability");
|
|
154
|
+
checkKnown(p1, isProb, "'p1' must be a probability");
|
|
155
|
+
checkKnown(p2, isProb, "'p2' must be a probability");
|
|
156
|
+
// (R gives a power for n = 0 and NaN for a negative n)
|
|
157
|
+
checkKnown(n, (v) => v > 0, "number of observations in each group must be positive");
|
|
158
|
+
checkKnown(options.tol, isPositiveFinite, "'tol' must be a positive finite number");
|
|
126
159
|
const alternative = matchChoice(options.alternative, ["two.sided", "one.sided"]);
|
|
127
160
|
const strict = options.strict ?? false;
|
|
128
161
|
const tol = options.tol ?? TOL;
|
|
@@ -146,14 +179,24 @@ export function powerPropTest(options = {}) {
|
|
|
146
179
|
power = powerOf(n, p1, p2, sigLevel);
|
|
147
180
|
else {
|
|
148
181
|
const target = power;
|
|
149
|
-
if (isUnknown(n))
|
|
182
|
+
if (isUnknown(n)) {
|
|
183
|
+
// with p1 = p2 the power does not depend on n (R and before #311: the search failed with "no sign change")
|
|
184
|
+
if (p1 === p2)
|
|
185
|
+
throw new RangeError("'n' cannot be determined when 'p1' equals 'p2': the power does not depend on it");
|
|
150
186
|
n = solveFor((x) => powerOf(x, p1, p2, sigLevel) - target, 1, 1e7, { tol, extendInt: "upX" });
|
|
187
|
+
}
|
|
151
188
|
else if (isUnknown(p1)) {
|
|
189
|
+
// p1 is searched in [0, p2]: with p2 = 0 that is a point (R and before #312: "lower < upper is not fulfilled")
|
|
190
|
+
if (p2 === 0)
|
|
191
|
+
throw new RangeError("'p1' cannot be determined when 'p2' is 0: no p1 in [0, p2] differs from p2");
|
|
152
192
|
p1 = solveFor((x) => powerOf(n, x, p2, sigLevel) - target, 0, p2, { tol, extendInt: "yes" });
|
|
153
193
|
if (p1 < 0)
|
|
154
194
|
warnings.push("No p1 in [0, p2] can be found to achieve the desired power");
|
|
155
195
|
}
|
|
156
196
|
else if (isUnknown(p2)) {
|
|
197
|
+
// p2 is searched in [p1, 1]: with p1 = 1 that is a point (as for p1 above)
|
|
198
|
+
if (p1 === 1)
|
|
199
|
+
throw new RangeError("'p2' cannot be determined when 'p1' is 1: no p2 in [p1, 1] differs from p1");
|
|
157
200
|
p2 = solveFor((x) => powerOf(n, p1, x, sigLevel) - target, p1, 1, { tol, extendInt: "yes" });
|
|
158
201
|
if (p2 > 1)
|
|
159
202
|
warnings.push("No p2 in [p1, 1] can be found to achieve the desired power");
|
|
@@ -176,6 +219,41 @@ export function powerPropTest(options = {}) {
|
|
|
176
219
|
warnings,
|
|
177
220
|
};
|
|
178
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Above this, both degrees of freedom of an F test are huge enough for {@link fPowerHugeDf} (pwrF2Test, pwrAnovaTest,
|
|
224
|
+
* powerAnovaTest). (1e16: the exact path pf(qf(...)) already loses about 1e-8 of the power there, from the critical
|
|
225
|
+
* value's few significant digits beyond 1, and up to 1e-5 just below 1e20; the approximation's error O(df^-1/2) is
|
|
226
|
+
* below that.)
|
|
227
|
+
*/
|
|
228
|
+
const HUGE_DF = 1e16;
|
|
229
|
+
/** Whether an F test's power is taken from the normal approximation (df1 finite, both above 1e16) */
|
|
230
|
+
export const hugeDf = (df1, df2) => df1 > HUGE_DF && df1 < Infinity && df2 > HUGE_DF;
|
|
231
|
+
/**
|
|
232
|
+
* The power P[F > qf(sig)] of an F test with noncentrality ncp where both degrees of freedom exceed 1e16 (df2 may be
|
|
233
|
+
* Infinity). There the critical value 1 + O(df^-1/2) keeps only a few significant digits beyond 1 in doubles (and
|
|
234
|
+
* rounds to 1 for the largest), so that pf(qf(...)) loses the test; F = (χ²(df1, λ)/df1) / (χ²_df2/df2) is normal
|
|
235
|
+
* instead, with mean 1 + μ, μ = λ/df1, and variance 2 (1 + 2μ)/df1 + 2 (1 + μ)²/df2 (2/df1 + 2/df2 under the null),
|
|
236
|
+
* to within O(df1^-1/2) in the power (about 1e-8 at 1e16, 1e-10 at 1e20).
|
|
237
|
+
* The standard deviation is formed as a hypot so that (1 + μ)² does not overflow.
|
|
238
|
+
*/
|
|
239
|
+
export function fPowerHugeDf(df1, df2, ncp, sig) {
|
|
240
|
+
const z = qnorm(sig, { lowerTail: false });
|
|
241
|
+
const mu = ncp / df1;
|
|
242
|
+
const sd0 = Math.sqrt(2 / df1 + 2 / df2);
|
|
243
|
+
const sd1 = Math.hypot(Math.sqrt((2 * (1 + 2 * mu)) / df1), Math.sqrt(2 / df2) * (1 + mu));
|
|
244
|
+
return pnorm((z * sd0 - mu) / sd1, { lowerTail: false });
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* a b c / d for finite a, b, d > 0 and c >= 0, each factor scaled to [1, 2) by a power of 2 and the exponents added
|
|
248
|
+
* back at the end, so that no partial product overflows or underflows unless the result does
|
|
249
|
+
*/
|
|
250
|
+
function scaledProductOver(a, b, c, d) {
|
|
251
|
+
if (c === 0)
|
|
252
|
+
return 0;
|
|
253
|
+
const k = (v) => Math.floor(Math.log2(v));
|
|
254
|
+
const [ka, kb, kc, kd] = [k(a), k(b), k(c), k(d)];
|
|
255
|
+
return ldexp((ldexp(a, -ka) * ldexp(b, -kb) * ldexp(c, -kc)) / ldexp(d, -kd), ka + kb + kc - kd);
|
|
256
|
+
}
|
|
179
257
|
/**
|
|
180
258
|
* Power of the F test of a balanced one-way analysis of variance, or one of the other quantities for a given power.
|
|
181
259
|
* Equivalent to R's `power.anova.test()`.
|
|
@@ -191,15 +269,33 @@ export function powerAnovaTest(options = {}) {
|
|
|
191
269
|
let { groups, n, betweenVar, withinVar, power } = options;
|
|
192
270
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
193
271
|
checkExactlyOne([groups, n, betweenVar, withinVar, power, sigLevel], "exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL");
|
|
194
|
-
if (!isUnknown(groups) && groups
|
|
272
|
+
if (!isUnknown(groups) && !(groups >= 2))
|
|
195
273
|
throw new RangeError("number of groups must be at least 2");
|
|
196
|
-
if (!isUnknown(n) && n
|
|
274
|
+
if (!isUnknown(n) && !(n >= 2))
|
|
197
275
|
throw new RangeError("number of observations in each group must be at least 2");
|
|
198
276
|
checkSigLevel(sigLevel, SIG_MESSAGE);
|
|
277
|
+
checkKnown(power, isProb, "'power' must be a probability");
|
|
278
|
+
checkKnown(betweenVar, (v) => v >= 0 && v < Infinity, "'betweenVar' must be a nonnegative finite number");
|
|
279
|
+
checkKnown(withinVar, isPositiveFinite, "'withinVar' must be a positive finite number");
|
|
199
280
|
const powerOf = (groups, n, between, within, sig) => {
|
|
200
|
-
|
|
281
|
+
// (the ratio first where the product with between overflows but the noncentrality is in range; where
|
|
282
|
+
// (groups - 1) n itself overflows, from the four factors scaled by powers of 2: groups 3, n 1e308, between 1,
|
|
283
|
+
// within 1e308 gave the limit 1 for lambda = 2, and between 0 NaN for 0)
|
|
284
|
+
let lambda = ((groups - 1) * n * between) / within;
|
|
285
|
+
if (!(lambda < Infinity))
|
|
286
|
+
lambda = (groups - 1) * n * (between / within);
|
|
287
|
+
if (!(lambda < Infinity) && Number.isFinite(groups) && Number.isFinite(n)) {
|
|
288
|
+
lambda = scaledProductOver(groups - 1, n, between, within);
|
|
289
|
+
}
|
|
201
290
|
const df1 = groups - 1;
|
|
202
291
|
const df2 = (n - 1) * groups;
|
|
292
|
+
// (a noncentrality beyond the doubles: the limit 1)
|
|
293
|
+
if (lambda === Infinity)
|
|
294
|
+
return 1;
|
|
295
|
+
// (both degrees of freedom above 1e16: the normal approximation, as in pwrAnovaTest; the critical value is 1 to a
|
|
296
|
+
// few digits, and groups 1e200, n 1e200, between 1e8, within 1e308 gave 0.760 for 0.174. R gives NaN there)
|
|
297
|
+
if (hugeDf(df1, df2))
|
|
298
|
+
return fPowerHugeDf(df1, df2, lambda, sig);
|
|
203
299
|
return pf(qf(sig, df1, df2, { lowerTail: false }), df1, df2, { ncp: lambda, lowerTail: false });
|
|
204
300
|
};
|
|
205
301
|
if (isUnknown(power))
|
|
@@ -208,9 +304,17 @@ export function powerAnovaTest(options = {}) {
|
|
|
208
304
|
const target = power;
|
|
209
305
|
if (isUnknown(groups))
|
|
210
306
|
groups = solveFor((x) => powerOf(x, n, betweenVar, withinVar, sigLevel) - target, 2, 100);
|
|
211
|
-
else if (isUnknown(n))
|
|
307
|
+
else if (isUnknown(n)) {
|
|
308
|
+
// (with betweenVar = 0 the power does not depend on n; R and before #311: "f() values at end points not of
|
|
309
|
+
// opposite sign")
|
|
310
|
+
if (betweenVar === 0)
|
|
311
|
+
throw new RangeError("'n' cannot be determined when 'betweenVar' is 0: the power does not depend on it");
|
|
212
312
|
n = solveFor((x) => powerOf(groups, x, betweenVar, withinVar, sigLevel) - target, 2, 1e5);
|
|
313
|
+
}
|
|
213
314
|
else if (isUnknown(withinVar)) {
|
|
315
|
+
// (with betweenVar = 0 the power does not depend on withinVar; R: "lower < upper is not fulfilled")
|
|
316
|
+
if (betweenVar === 0)
|
|
317
|
+
throw new RangeError("'withinVar' cannot be determined when 'betweenVar' is 0: the power does not depend on it");
|
|
214
318
|
withinVar = solveFor((x) => powerOf(groups, n, betweenVar, x, sigLevel) - target, betweenVar * 1e-7, betweenVar * 1e7);
|
|
215
319
|
}
|
|
216
320
|
else if (isUnknown(betweenVar)) {
|
package/dist/htest/propTest.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// difference of two.
|
|
12
12
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
13
13
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
14
|
+
import { ldexp, squareOver } from "../internal/summary.js";
|
|
14
15
|
import { checkConfLevelOpen, matchAlternative } from "./types.js";
|
|
15
16
|
/**
|
|
16
17
|
* Test of equal or given proportions. Equivalent to R's `prop.test()`.
|
|
@@ -61,6 +62,8 @@ export function propTest(x, n, options = {}) {
|
|
|
61
62
|
ns = kept.map((i) => ns[i]);
|
|
62
63
|
if (ns.some((v) => !(v > 0)))
|
|
63
64
|
throw new RangeError("elements of 'n' must be positive");
|
|
65
|
+
if (ns.some((v) => v === Infinity))
|
|
66
|
+
throw new RangeError("elements of 'n' must be finite");
|
|
64
67
|
if (xs.some((v) => !(v >= 0)))
|
|
65
68
|
throw new RangeError("elements of 'x' must be nonnegative");
|
|
66
69
|
if (xs.some((v, i) => v > ns[i]))
|
|
@@ -99,15 +102,15 @@ export function propTest(x, n, options = {}) {
|
|
|
99
102
|
[yates, capped] = [gap, true];
|
|
100
103
|
}
|
|
101
104
|
// ---- Pearson's chi-squared on the k x 2 table of successes and failures ----
|
|
102
|
-
const pooled = xs
|
|
105
|
+
const pooled = pooledProportion(xs, ns);
|
|
103
106
|
let statistic = 0;
|
|
104
107
|
if (!capped) {
|
|
105
108
|
for (let i = 0; i < k; i++) {
|
|
106
109
|
const pi = p?.[i] ?? pooled;
|
|
107
110
|
const e1 = ns[i] * pi;
|
|
108
111
|
const e2 = ns[i] * (1 - pi);
|
|
109
|
-
statistic += (Math.abs(xs[i] - e1) - yates
|
|
110
|
-
statistic += (Math.abs(ns[i] - xs[i] - e2) - yates
|
|
112
|
+
statistic += squareOver(Math.abs(xs[i] - e1) - yates, e1);
|
|
113
|
+
statistic += squareOver(Math.abs(ns[i] - xs[i] - e2) - yates, e2);
|
|
111
114
|
}
|
|
112
115
|
}
|
|
113
116
|
const df = p !== undefined ? k : k - 1;
|
|
@@ -121,7 +124,9 @@ export function propTest(x, n, options = {}) {
|
|
|
121
124
|
// ---- confidence interval ----
|
|
122
125
|
let confInt;
|
|
123
126
|
if (k === 1 || (k === 2 && !pGiven)) {
|
|
124
|
-
|
|
127
|
+
// (for alpha below 1e-8 the upper probability itself: 1 - alpha loses its digits and rounds to 1 below 1.1e-16; review 58)
|
|
128
|
+
const a2 = (1 - confLevel) / 2;
|
|
129
|
+
const z = alternative === "two.sided" && a2 < 1e-8 ? qnorm(a2, { lowerTail: false }) : qnorm(alternative === "two.sided" ? (1 + confLevel) / 2 : confLevel);
|
|
125
130
|
if (k === 1) {
|
|
126
131
|
const nn = ns[0];
|
|
127
132
|
const z2 = z * z;
|
|
@@ -164,3 +169,15 @@ export function propTest(x, n, options = {}) {
|
|
|
164
169
|
}
|
|
165
170
|
return result;
|
|
166
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* sum(x) / sum(n). Where the total of the trials overflows (each n is finite), both totals in units of 2^s with
|
|
174
|
+
* 2^s >= the number of groups, so that neither can overflow; the common power of 2 cancels in the ratio. Elsewhere the
|
|
175
|
+
* plain ratio, unchanged
|
|
176
|
+
*/
|
|
177
|
+
function pooledProportion(xs, ns) {
|
|
178
|
+
const total = ns.reduce((a, b) => a + b, 0);
|
|
179
|
+
if (total < Infinity)
|
|
180
|
+
return xs.reduce((a, b) => a + b, 0) / total;
|
|
181
|
+
const s = Math.ceil(Math.log2(ns.length));
|
|
182
|
+
return xs.reduce((a, b) => a + ldexp(b, -s), 0) / ns.reduce((a, b) => a + ldexp(b, -s), 0);
|
|
183
|
+
}
|
package/dist/htest/pwr.js
CHANGED
|
@@ -9,7 +9,8 @@ import { pchisq, qchisq } from "../distributions/chisq.js";
|
|
|
9
9
|
import { pf, qf } from "../distributions/f.js";
|
|
10
10
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
11
11
|
import { qt } from "../distributions/t.js";
|
|
12
|
-
import {
|
|
12
|
+
import { ldexp, squareOver } from "../internal/summary.js";
|
|
13
|
+
import { checkExactlyOne, fPowerHugeDf, hugeDf, isUnknown, matchChoice, ptNcp, solveFor } from "./power.js";
|
|
13
14
|
const COHEN = {
|
|
14
15
|
p: [0.2, 0.5, 0.8],
|
|
15
16
|
t: [0.2, 0.5, 0.8],
|
|
@@ -44,6 +45,9 @@ export function cohenES(test = "p", size = "small") {
|
|
|
44
45
|
* @category Effect sizes and power
|
|
45
46
|
*/
|
|
46
47
|
export function esH(p1, p2) {
|
|
48
|
+
// (a probability outside [0, 1] gave NaN through asin)
|
|
49
|
+
if (!isProbability(p1) || !isProbability(p2))
|
|
50
|
+
throw new RangeError("p1 and p2 must be probabilities in [0, 1]");
|
|
47
51
|
return 2 * Math.asin(Math.sqrt(p1)) - 2 * Math.asin(Math.sqrt(p2));
|
|
48
52
|
}
|
|
49
53
|
/**
|
|
@@ -59,9 +63,14 @@ export function esH(p1, p2) {
|
|
|
59
63
|
export function esW1(p0, p1) {
|
|
60
64
|
if (p0.length !== p1.length)
|
|
61
65
|
throw new RangeError("'P0' and 'P1' must have the same length");
|
|
66
|
+
if (![...p0, ...p1].every(isProbability))
|
|
67
|
+
throw new RangeError("'P0' and 'P1' must be probabilities in [0, 1]");
|
|
62
68
|
let s = 0;
|
|
69
|
+
// a cell with p0 = p1 = 0 adds nothing (0 / 0 was NaN); p0 = 0 < p1 adds Inf, as pwr. squareOver forms
|
|
70
|
+
// (d / sqrt(p0))^2 where d^2 underflows (esW1([1e-320, 1], [1e-200, 1]) gave 0 for 1e-40)
|
|
63
71
|
for (let i = 0; i < p0.length; i++)
|
|
64
|
-
|
|
72
|
+
if (p0[i] !== 0 || p1[i] !== 0)
|
|
73
|
+
s += squareOver(p1[i] - p0[i], p0[i]);
|
|
65
74
|
return Math.sqrt(s);
|
|
66
75
|
}
|
|
67
76
|
/**
|
|
@@ -76,6 +85,12 @@ export function esW1(p0, p1) {
|
|
|
76
85
|
* @category Effect sizes and power
|
|
77
86
|
*/
|
|
78
87
|
export function esW2(p) {
|
|
88
|
+
// (an empty matrix gave 0, a ragged one NaN)
|
|
89
|
+
if (p.length === 0 || p[0].length === 0 || p.some((r) => r.length !== p[0].length)) {
|
|
90
|
+
throw new RangeError("'P' must be a non-empty matrix (rows of the same length)");
|
|
91
|
+
}
|
|
92
|
+
if (!p.every((r) => r.every(isProbability)))
|
|
93
|
+
throw new RangeError("'P' must contain probabilities in [0, 1]");
|
|
79
94
|
const rows = p.map((r) => r.reduce((a, b) => a + b, 0));
|
|
80
95
|
const nc = p[0]?.length ?? 0;
|
|
81
96
|
const cols = Array.from({ length: nc }, (_, j) => p.reduce((a, r) => a + r[j], 0));
|
|
@@ -84,11 +99,25 @@ export function esW2(p) {
|
|
|
84
99
|
for (let j = 0; j < nc; j++) {
|
|
85
100
|
for (let i = 0; i < p.length; i++) {
|
|
86
101
|
const p0 = rows[i] * cols[j];
|
|
87
|
-
|
|
102
|
+
// (a cell of an empty row or column, p0 = p = 0, adds nothing: 0 / 0 was NaN)
|
|
103
|
+
if (p0 === 0 && p[i][j] === 0)
|
|
104
|
+
continue;
|
|
105
|
+
if (p0 >= MIN_NORMAL)
|
|
106
|
+
s += squareOver(p[i][j] - p0, p0);
|
|
107
|
+
else {
|
|
108
|
+
// (p0 below the normal range, with a row and column sum not 0: (p / sqrt(r c) - sqrt(r c))^2 from the square
|
|
109
|
+
// roots of the sums, as r c may round or underflow to 0; esW2([[1e-200, 0], [0, 1]]) gave NaN for 1)
|
|
110
|
+
const sr = Math.sqrt(rows[i]);
|
|
111
|
+
const sc = Math.sqrt(cols[j]);
|
|
112
|
+
const z = p[i][j] / sr / sc - sr * sc;
|
|
113
|
+
s += z * z;
|
|
114
|
+
}
|
|
88
115
|
}
|
|
89
116
|
}
|
|
90
117
|
return Math.sqrt(s);
|
|
91
118
|
}
|
|
119
|
+
const MIN_NORMAL = 2 ** -1022;
|
|
120
|
+
const isProbability = (v) => v >= 0 && v <= 1;
|
|
92
121
|
const SIG_MESSAGE = "‘sig.level’ must be numeric in [0, 1]";
|
|
93
122
|
const POWER_MESSAGE = "‘power’ must be numeric in [0, 1]";
|
|
94
123
|
function checkProbabilities(sigLevel, power) {
|
|
@@ -100,6 +129,9 @@ function checkProbabilities(sigLevel, power) {
|
|
|
100
129
|
}
|
|
101
130
|
/** A string effect size replaced by Cohen's value for the test */
|
|
102
131
|
function effect(es, test) {
|
|
132
|
+
// (NaN gave a NaN power silently; pwr stops on it in most functions)
|
|
133
|
+
if (typeof es === "number" && Number.isNaN(es))
|
|
134
|
+
throw new RangeError("the effect size must not be NaN");
|
|
103
135
|
return typeof es === "string" ? cohenES(test, es).effectSize : es;
|
|
104
136
|
}
|
|
105
137
|
const ALTERNATIVES = ["two.sided", "less", "greater"];
|
|
@@ -116,13 +148,31 @@ function tPower(nu, ncp, sig, alt) {
|
|
|
116
148
|
const q = qt(sig / 2, nu, { lowerTail: false });
|
|
117
149
|
return ptNcp(q, nu, ncp, false) + ptNcp(-q, nu, ncp, true);
|
|
118
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* The upper critical value qnorm(1 - a): for a < 1e-8 the quantile of the upper tail a (1 - a loses digits, and rounds to
|
|
153
|
+
* 1 below 1.1e-16: sigLevel 1e-20 gave a power of 1e-30 for 2.7e-13), as for the confidence levels close to 1 elsewhere
|
|
154
|
+
*/
|
|
155
|
+
const upperCritical = (a) => (a < 1e-8 ? qnorm(a, { lowerTail: false }) : qnorm(1 - a));
|
|
119
156
|
/** The power of a normal test with shift `es * scale`, by pwr's rejection regions */
|
|
120
157
|
function normalPower(shift, sig, alt) {
|
|
121
158
|
if (alt === "greater")
|
|
122
|
-
return pnorm(
|
|
159
|
+
return pnorm(upperCritical(sig) - shift, { lowerTail: false });
|
|
123
160
|
if (alt === "less")
|
|
124
161
|
return pnorm(qnorm(sig) - shift);
|
|
125
|
-
return
|
|
162
|
+
return pnorm(upperCritical(sig / 2) - shift, { lowerTail: false }) + pnorm(qnorm(sig / 2) - shift);
|
|
163
|
+
}
|
|
164
|
+
/** The product of finite factors, each scaled to [1, 2) by a power of 2 and the exponents added back at the end */
|
|
165
|
+
function scaledProduct(xs) {
|
|
166
|
+
let v = 1;
|
|
167
|
+
let e = 0;
|
|
168
|
+
for (const x of xs) {
|
|
169
|
+
if (x === 0)
|
|
170
|
+
return 0;
|
|
171
|
+
const k = Math.floor(Math.log2(Math.abs(x)));
|
|
172
|
+
v *= ldexp(x, -k);
|
|
173
|
+
e += k;
|
|
174
|
+
}
|
|
175
|
+
return ldexp(v, e);
|
|
126
176
|
}
|
|
127
177
|
/**
|
|
128
178
|
* Power of a t test of means, or the sample size, effect size or significance level for a given power.
|
|
@@ -142,6 +192,8 @@ export function pwrTTest(options = {}) {
|
|
|
142
192
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
143
193
|
checkExactlyOne([n, d, power, sigLevel], "exactly one of n, d, power, and sig.level must be NULL");
|
|
144
194
|
checkProbabilities(sigLevel, power);
|
|
195
|
+
if (!isUnknown(n) && !(n >= 2))
|
|
196
|
+
throw new RangeError("number of observations in each group must be at least 2");
|
|
145
197
|
const type = matchChoice(options.type, ["two.sample", "one.sample", "paired"]);
|
|
146
198
|
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
147
199
|
const ts = type === "two.sample" ? 2 : 1;
|
|
@@ -181,9 +233,9 @@ export function pwrT2nTest(options = {}) {
|
|
|
181
233
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
182
234
|
checkExactlyOne([n1, n2, d, power, sigLevel], "exactly one of n1, n2, d, power, and sig.level must be NULL");
|
|
183
235
|
checkProbabilities(sigLevel, power);
|
|
184
|
-
if (!isUnknown(n1) && n1
|
|
236
|
+
if (!isUnknown(n1) && !(n1 >= 2))
|
|
185
237
|
throw new RangeError("number of observations in the first group must be at least 2");
|
|
186
|
-
if (!isUnknown(n2) && n2
|
|
238
|
+
if (!isUnknown(n2) && !(n2 >= 2))
|
|
187
239
|
throw new RangeError("number of observations in the second group must be at least 2");
|
|
188
240
|
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
189
241
|
if (alt === "two.sided" && !isUnknown(d))
|
|
@@ -211,7 +263,7 @@ function normalTest(es0, options, name, scale, nLower, esLower) {
|
|
|
211
263
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
212
264
|
checkExactlyOne([es, n, power, sigLevel], `exactly one of ${name}, n, power, and sig.level must be NULL`);
|
|
213
265
|
checkProbabilities(sigLevel, power);
|
|
214
|
-
if (!isUnknown(n) && n
|
|
266
|
+
if (!isUnknown(n) && !(n >= 1))
|
|
215
267
|
throw new RangeError("number of observations in each group must be at least 1");
|
|
216
268
|
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
217
269
|
if (alt === "two.sided" && !isUnknown(es))
|
|
@@ -318,14 +370,23 @@ export function pwr2p2nTest(options = {}) {
|
|
|
318
370
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
319
371
|
checkExactlyOne([h, n1, n2, power, sigLevel], "exactly one of h, n1, n2, power, and sig.level must be NULL");
|
|
320
372
|
checkProbabilities(sigLevel, power);
|
|
321
|
-
if (!isUnknown(n1) && n1
|
|
373
|
+
if (!isUnknown(n1) && !(n1 >= 2))
|
|
322
374
|
throw new RangeError("number of observations in the first group must be at least 2");
|
|
323
|
-
if (!isUnknown(n2) && n2
|
|
375
|
+
if (!isUnknown(n2) && !(n2 >= 2))
|
|
324
376
|
throw new RangeError("number of observations in the second group must be at least 2");
|
|
325
377
|
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
326
378
|
if (alt === "two.sided" && !isUnknown(h))
|
|
327
379
|
h = Math.abs(h);
|
|
328
|
-
|
|
380
|
+
// the effective size n1 n2 / (n1 + n2); where the product overflows (finite sizes above about 1e154) as
|
|
381
|
+
// a / (1 + a/b) with a <= b (the same bits elsewhere; pwr gives power 1 there)
|
|
382
|
+
const effectiveN = (n1, n2) => {
|
|
383
|
+
const prod = n1 * n2;
|
|
384
|
+
if (Number.isFinite(prod) || !Number.isFinite(n1) || !Number.isFinite(n2))
|
|
385
|
+
return prod / (n1 + n2);
|
|
386
|
+
const a = Math.min(n1, n2);
|
|
387
|
+
return a / (1 + a / Math.max(n1, n2));
|
|
388
|
+
};
|
|
389
|
+
const powerOf = (h, n1, n2, sig) => normalPower(h * Math.sqrt(effectiveN(n1, n2)), sig, alt);
|
|
329
390
|
if (isUnknown(power))
|
|
330
391
|
power = powerOf(h, n1, n2, sigLevel);
|
|
331
392
|
else {
|
|
@@ -369,14 +430,20 @@ export function pwrRTest(options = {}) {
|
|
|
369
430
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
370
431
|
checkExactlyOne([n, r, power, sigLevel], "exactly one of n, r, power, and sig.level must be NULL");
|
|
371
432
|
checkProbabilities(sigLevel, power);
|
|
372
|
-
if (!isUnknown(n) && n
|
|
433
|
+
if (!isUnknown(n) && !(n >= 4))
|
|
373
434
|
throw new RangeError("number of observations must be at least 4");
|
|
435
|
+
// (a correlation outside [-1, 1] gave NaN through atanh)
|
|
436
|
+
if (!isUnknown(r) && !(Math.abs(r) <= 1))
|
|
437
|
+
throw new RangeError("'r' must be in [-1, 1]");
|
|
374
438
|
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
375
439
|
if (alt === "two.sided" && !isUnknown(r))
|
|
376
440
|
r = Math.abs(r);
|
|
377
441
|
const upper = (n, r, sig, twoSided) => {
|
|
378
442
|
const t = qt(twoSided ? sig / 2 : sig, n - 2, { lowerTail: false });
|
|
379
|
-
const
|
|
443
|
+
const den = t ** 2 + n - 2;
|
|
444
|
+
// (where t^2 + n - 2 overflows for a finite t, 1 / sqrt(1 + ((n - 2) / t) / t): Inf / Inf gave NaN, as
|
|
445
|
+
// pwrRTest({ n: 4, r: 0.5, sigLevel: 1e-320 }) with t = 7e159)
|
|
446
|
+
const rc = den === Infinity && Number.isFinite(t) ? 1 / Math.sqrt(1 + (n - 2) / t / t) : Math.sqrt(t ** 2 / den);
|
|
380
447
|
const zr = Math.atanh(r) + r / (2 * (n - 1));
|
|
381
448
|
const zrc = Math.atanh(rc);
|
|
382
449
|
const p = pnorm((zr - zrc) * Math.sqrt(n - 3));
|
|
@@ -428,12 +495,18 @@ export function pwrChisqTest(options) {
|
|
|
428
495
|
checkExactlyOne([w, N, power, sigLevel], "exactly one of w, N, power, and sig.level must be NULL");
|
|
429
496
|
if (typeof df !== "number")
|
|
430
497
|
throw new TypeError('argument "df" is missing, with no default');
|
|
498
|
+
if (!(df > 0 && df < Infinity))
|
|
499
|
+
throw new RangeError("'df' must be a positive finite number");
|
|
431
500
|
checkProbabilities(sigLevel, power);
|
|
432
|
-
if (!isUnknown(w) && w
|
|
501
|
+
if (!isUnknown(w) && !(w >= 0))
|
|
433
502
|
throw new RangeError("w must be positive");
|
|
434
|
-
if (!isUnknown(N) && N
|
|
503
|
+
if (!isUnknown(N) && !(N >= 1))
|
|
435
504
|
throw new RangeError("number of observations must be at least 1");
|
|
436
|
-
|
|
505
|
+
// a noncentrality beyond the doubles: the power is its limit 1 (NaN from the distribution with ncp = Inf)
|
|
506
|
+
const powerOf = (w, N, sig) => {
|
|
507
|
+
const ncp = N * w ** 2;
|
|
508
|
+
return ncp === Infinity ? 1 : pchisq(qchisq(sig, df, { lowerTail: false }), df, { ncp, lowerTail: false });
|
|
509
|
+
};
|
|
437
510
|
if (isUnknown(power))
|
|
438
511
|
power = powerOf(w, N, sigLevel);
|
|
439
512
|
else {
|
|
@@ -465,16 +538,27 @@ export function pwrAnovaTest(options = {}) {
|
|
|
465
538
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
466
539
|
checkExactlyOne([k, n, f, power, sigLevel], "exactly one of k, n, f, power, and sig.level must be NULL");
|
|
467
540
|
checkProbabilities(sigLevel, power);
|
|
468
|
-
if (!isUnknown(f) && f
|
|
541
|
+
if (!isUnknown(f) && !(f >= 0))
|
|
469
542
|
throw new RangeError("f must be positive");
|
|
470
|
-
if (!isUnknown(k) && k
|
|
543
|
+
if (!isUnknown(k) && !(k >= 2))
|
|
471
544
|
throw new RangeError("number of groups must be at least 2");
|
|
472
|
-
if (!isUnknown(n) && n
|
|
545
|
+
if (!isUnknown(n) && !(n >= 2))
|
|
473
546
|
throw new RangeError("number of observations in each group must be at least 2");
|
|
474
547
|
const powerOf = (k, n, f, sig) => {
|
|
475
548
|
const df1 = k - 1;
|
|
476
549
|
const df2 = (n - 1) * k;
|
|
477
|
-
|
|
550
|
+
let ncp = k * n * f ** 2;
|
|
551
|
+
// (where k n overflows or f^2 underflows for finite factors, from the factors scaled by powers of 2: k = n = 1e160,
|
|
552
|
+
// f = 1e-160 gave the limit 1 for the noncentrality 1)
|
|
553
|
+
if (!Number.isFinite(ncp) && Number.isFinite(k) && Number.isFinite(n) && Number.isFinite(f))
|
|
554
|
+
ncp = scaledProduct([k, n, f, f]);
|
|
555
|
+
// (a noncentrality beyond the doubles: the limit 1)
|
|
556
|
+
if (ncp === Infinity)
|
|
557
|
+
return 1;
|
|
558
|
+
// (both degrees of freedom above 1e16: the normal approximation, the critical value being 1 to a few digits; R NaN)
|
|
559
|
+
if (hugeDf(df1, df2))
|
|
560
|
+
return fPowerHugeDf(df1, df2, ncp, sig);
|
|
561
|
+
return pf(qf(sig, df1, df2, { lowerTail: false }), df1, df2, { ncp, lowerTail: false });
|
|
478
562
|
};
|
|
479
563
|
if (isUnknown(power))
|
|
480
564
|
power = powerOf(k, n, f, sigLevel);
|
|
@@ -517,13 +601,26 @@ export function pwrF2Test(options = {}) {
|
|
|
517
601
|
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
518
602
|
checkExactlyOne([u, v, f2, power, sigLevel], "exactly one of u, v, f2, power, and sig.level must be NULL");
|
|
519
603
|
checkProbabilities(sigLevel, power);
|
|
520
|
-
if (!isUnknown(f2) && f2
|
|
604
|
+
if (!isUnknown(f2) && !(f2 >= 0))
|
|
521
605
|
throw new RangeError("f2 must be positive");
|
|
522
|
-
if (!isUnknown(u) && u
|
|
606
|
+
if (!isUnknown(u) && !(u >= 1))
|
|
523
607
|
throw new RangeError("degree of freedom u for numerator must be at least 1");
|
|
524
|
-
if (!isUnknown(v) && v
|
|
608
|
+
if (!isUnknown(v) && !(v >= 1))
|
|
525
609
|
throw new RangeError("degree of freedom v for denominator must be at least 1");
|
|
526
|
-
|
|
610
|
+
// a noncentrality beyond the doubles: the power is its limit 1
|
|
611
|
+
const powerOf = (u, v, f2, sig) => {
|
|
612
|
+
let ncp = f2 * (u + v + 1);
|
|
613
|
+
// (where u + v overflows for finite degrees of freedom, from the halves: u = v = 1e308, f2 = 1e-308 gave the limit 1
|
|
614
|
+
// for the noncentrality 2)
|
|
615
|
+
if (!Number.isFinite(ncp) && Number.isFinite(u) && Number.isFinite(v) && Number.isFinite(f2))
|
|
616
|
+
ncp = 2 * (f2 * (u / 2 + v / 2 + 0.5));
|
|
617
|
+
if (ncp === Infinity)
|
|
618
|
+
return 1;
|
|
619
|
+
// (both degrees of freedom above 1e16: the normal approximation, the critical value being 1 to a few digits; R NaN)
|
|
620
|
+
if (hugeDf(u, v))
|
|
621
|
+
return fPowerHugeDf(u, v, ncp, sig);
|
|
622
|
+
return pf(qf(sig, u, v, { lowerTail: false }), u, v, { ncp, lowerTail: false });
|
|
623
|
+
};
|
|
527
624
|
if (isUnknown(power))
|
|
528
625
|
power = powerOf(u, v, f2, sigLevel);
|
|
529
626
|
else {
|