@numero-uno-jpn/rstats 0.9.0 → 0.11.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 +87 -0
- package/README.ja.md +64 -12
- package/README.md +63 -11
- package/THIRD_PARTY_NOTICES.md +7 -2
- package/dist/chunks/chunk-5WDJPHYY.js +2 -0
- package/dist/chunks/chunk-7GQMKH3Z.js +2 -0
- package/dist/chunks/chunk-AHHS6AC4.js +3 -0
- package/dist/chunks/chunk-ALM3VHFP.js +2 -0
- package/dist/chunks/{chunk-TOVTN5Y2.js → chunk-C5XTWMGB.js} +1 -1
- package/dist/chunks/chunk-CGBNKHNZ.js +2 -0
- package/dist/chunks/{chunk-LWUSYIQ4.js → chunk-EZAFN5CD.js} +1 -1
- package/dist/chunks/{chunk-H2AP6TCR.js → chunk-EZWBRCJL.js} +1 -1
- package/dist/chunks/chunk-F5EDJFEO.js +2 -0
- package/dist/chunks/{chunk-FC2TLHZC.js → chunk-JWBO5WVN.js} +2 -2
- package/dist/chunks/{chunk-LE7TBHNI.js → chunk-TOGXTCZO.js} +2 -2
- package/dist/chunks/chunk-VJ4BDBMU.js +3 -0
- package/dist/chunks/chunk-Y5CNQ266.js +2 -0
- package/dist/chunks/{chunk-BGVMSDVJ.js → chunk-Y5PYRAYP.js} +2 -2
- package/dist/chunks/{chunk-5K4PS7IV.js → chunk-YSWGDOWK.js} +2 -2
- package/dist/data/grouping.d.ts +79 -0
- package/dist/data/grouping.js +262 -0
- package/dist/descriptive/dataFrames.d.ts +110 -0
- package/dist/descriptive/dataFrames.js +392 -0
- package/dist/descriptive/factanal.d.ts +56 -0
- package/dist/descriptive/factanal.js +186 -0
- package/dist/descriptive/missing.d.ts +35 -0
- package/dist/descriptive/missing.js +50 -0
- package/dist/descriptive/omega.d.ts +56 -0
- package/dist/descriptive/omega.js +323 -0
- package/dist/descriptive/pivot.d.ts +89 -0
- package/dist/descriptive/pivot.js +343 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions/bvnorm.d.ts +15 -0
- package/dist/distributions/bvnorm.js +131 -0
- package/dist/distributions/nbeta.js +34 -1
- package/dist/distributions/nchisq.d.ts +10 -0
- package/dist/distributions/nchisq.js +46 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +4 -0
- package/dist/entries/descriptive.js +4 -0
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +1 -0
- package/dist/entries/linalg.d.ts +2 -0
- package/dist/entries/linalg.js +2 -0
- package/dist/entries/numerics.d.ts +2 -0
- package/dist/entries/numerics.js +1 -0
- package/dist/entries/tests.d.ts +22 -1
- package/dist/entries/tests.js +19 -1
- package/dist/htest/agreement.d.ts +141 -0
- package/dist/htest/agreement.js +274 -0
- package/dist/htest/ansariTest.d.ts +12 -0
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/aov.d.ts +19 -0
- package/dist/htest/aov.js +22 -15
- package/dist/htest/cor.d.ts +12 -0
- package/dist/htest/cor.js +9 -27
- package/dist/htest/core/simulate.d.ts +2 -0
- package/dist/htest/core/simulate.js +1 -1
- package/dist/htest/dunnett.d.ts +5 -0
- package/dist/htest/dunnett.js +89 -0
- package/dist/htest/effectSize.d.ts +112 -0
- package/dist/htest/effectSize.js +385 -0
- package/dist/htest/ksTest.d.ts +13 -0
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/leveneTest.d.ts +30 -0
- package/dist/htest/leveneTest.js +41 -0
- package/dist/htest/multipleComparisons.d.ts +147 -0
- package/dist/htest/multipleComparisons.js +369 -0
- package/dist/htest/normalityTests.d.ts +44 -0
- package/dist/htest/normalityTests.js +164 -0
- package/dist/htest/onewayTest.d.ts +14 -0
- package/dist/htest/onewayTest.js +5 -7
- package/dist/htest/polycor.d.ts +117 -0
- package/dist/htest/polycor.js +468 -0
- package/dist/htest/power.d.ts +132 -0
- package/dist/htest/power.js +227 -0
- package/dist/htest/pwr.d.ts +263 -0
- package/dist/htest/pwr.js +440 -0
- package/dist/htest/rankTests.d.ts +64 -0
- package/dist/htest/rankTests.js +27 -29
- package/dist/htest/tTest.d.ts +20 -0
- package/dist/htest/tTest.js +19 -24
- package/dist/htest/testFormula.d.ts +57 -0
- package/dist/htest/testFormula.js +171 -0
- package/dist/htest/trendTests.d.ts +57 -0
- package/dist/htest/trendTests.js +167 -0
- package/dist/htest/varTest.d.ts +23 -0
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +14 -0
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/linalg/eigen.d.ts +8 -0
- package/dist/linalg/eigen.js +5 -4
- package/dist/linalg/expm.d.ts +48 -0
- package/dist/linalg/expm.js +472 -0
- package/dist/linalg/ops.d.ts +13 -2
- package/dist/linalg/ops.js +20 -5
- package/dist/linalg/schur.d.ts +48 -0
- package/dist/linalg/schur.js +225 -0
- package/dist/linalg.js +2 -2
- package/dist/models/anovaSequential.d.ts +58 -0
- package/dist/models/anovaSequential.js +138 -0
- package/dist/models/aovEz.d.ts +64 -0
- package/dist/models/aovEz.js +332 -0
- package/dist/models/aovStrata.d.ts +32 -0
- package/dist/models/aovStrata.js +104 -0
- package/dist/models/carAnova.d.ts +30 -0
- package/dist/models/carAnova.js +121 -0
- package/dist/models/coxph.d.ts +101 -0
- package/dist/models/coxph.js +502 -0
- package/dist/models/ddLeastSquares.d.ts +30 -0
- package/dist/models/ddLeastSquares.js +90 -0
- package/dist/models/ddMatrix.d.ts +10 -0
- package/dist/models/ddMatrix.js +91 -0
- package/dist/models/formula.d.ts +83 -0
- package/dist/models/formula.js +333 -0
- package/dist/models/lm.d.ts +84 -3
- package/dist/models/lm.js +141 -44
- package/dist/models/mauchly.d.ts +48 -0
- package/dist/models/mauchly.js +123 -0
- package/dist/models/modelFrame.d.ts +40 -0
- package/dist/models/modelFrame.js +131 -0
- package/dist/models/modelMatrix.d.ts +88 -0
- package/dist/models/modelMatrix.js +170 -0
- package/dist/models/survival.d.ts +87 -0
- package/dist/models/survival.js +238 -0
- package/dist/numerics.js +2 -2
- package/dist/optim/lbfgsb.d.ts +20 -0
- package/dist/optim/lbfgsb.js +1240 -0
- package/dist/optim/optim.d.ts +85 -0
- package/dist/optim/optim.js +610 -0
- package/dist/rstats.js +19 -4
- package/dist/special.js +2 -2
- package/dist/tests.js +17 -4
- package/llms.txt +114 -0
- package/package.json +3 -2
- package/dist/chunks/chunk-24U4DKRY.js +0 -2
- package/dist/chunks/chunk-IVOBWYLV.js +0 -2
- package/dist/chunks/chunk-JLSTEMSN.js +0 -2
- package/dist/chunks/chunk-MF4VBCHQ.js +0 -2
- package/dist/chunks/chunk-MSAUU6PY.js +0 -2
- package/dist/chunks/chunk-YC4JWUKG.js +0 -2
- package/dist/chunks/chunk-ZRO446BP.js +0 -2
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
// Power calculations of the pwr package (Cohen's effect sizes): pwr.t.test, pwr.t2n.test, pwr.2p.test,
|
|
2
|
+
// pwr.2p2n.test, pwr.p.test, pwr.norm.test, pwr.r.test, pwr.chisq.test, pwr.anova.test, pwr.f2.test, and the
|
|
3
|
+
// effect-size helpers ES.h, ES.w1, ES.w2 and cohen.ES.
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js from oracle/effectsize-behaviour.md (no ported code; pwr's source was not read). The quantity
|
|
6
|
+
// given as null is solved for with uniroot (src/numerics/integrate.ts) on pwr's intervals with uniroot's default
|
|
7
|
+
// tolerance and no extension of the interval, so that the tolerance-limited root is pwr's.
|
|
8
|
+
import { pchisq, qchisq } from "../distributions/chisq.js";
|
|
9
|
+
import { pf, qf } from "../distributions/f.js";
|
|
10
|
+
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
11
|
+
import { qt } from "../distributions/t.js";
|
|
12
|
+
import { checkExactlyOne, isUnknown, matchChoice, ptNcp, solveFor } from "./power.js";
|
|
13
|
+
const COHEN = {
|
|
14
|
+
p: [0.2, 0.5, 0.8],
|
|
15
|
+
t: [0.2, 0.5, 0.8],
|
|
16
|
+
r: [0.1, 0.3, 0.5],
|
|
17
|
+
anov: [0.1, 0.25, 0.4],
|
|
18
|
+
chisq: [0.1, 0.3, 0.5],
|
|
19
|
+
f2: [0.02, 0.15, 0.35],
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Cohen's conventional effect sizes. Equivalent to pwr's `cohen.ES(test, size)`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* cohenES("anov", "medium").effectSize; // 0.25
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function cohenES(test = "p", size = "small") {
|
|
30
|
+
const t = matchChoice(test, ["p", "t", "r", "anov", "chisq", "f2"]);
|
|
31
|
+
const s = matchChoice(size, ["small", "medium", "large"]);
|
|
32
|
+
return { test: t, size: s, effectSize: COHEN[t][["small", "medium", "large"].indexOf(s)], method: "Conventional effect size from Cohen (1982)" };
|
|
33
|
+
}
|
|
34
|
+
/** Effect size h of two proportions: `2 asin(sqrt(p1)) - 2 asin(sqrt(p2))`. Equivalent to pwr's `ES.h`. */
|
|
35
|
+
export function esH(p1, p2) {
|
|
36
|
+
return 2 * Math.asin(Math.sqrt(p1)) - 2 * Math.asin(Math.sqrt(p2));
|
|
37
|
+
}
|
|
38
|
+
/** Effect size w of a goodness-of-fit test, `sqrt(sum((p1 - p0)^2 / p0))`. Equivalent to pwr's `ES.w1`. */
|
|
39
|
+
export function esW1(p0, p1) {
|
|
40
|
+
if (p0.length !== p1.length)
|
|
41
|
+
throw new RangeError("'P0' and 'P1' must have the same length");
|
|
42
|
+
let s = 0;
|
|
43
|
+
for (let i = 0; i < p0.length; i++)
|
|
44
|
+
s += (p1[i] - p0[i]) ** 2 / p0[i];
|
|
45
|
+
return Math.sqrt(s);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Effect size w of a test of association, for a matrix of cell probabilities (an array of rows): with the row sums
|
|
49
|
+
* r and column sums c, `p0 = r c'` and `sqrt(sum((p - p0)^2 / p0))`. Equivalent to pwr's `ES.w2`.
|
|
50
|
+
*/
|
|
51
|
+
export function esW2(p) {
|
|
52
|
+
const rows = p.map((r) => r.reduce((a, b) => a + b, 0));
|
|
53
|
+
const nc = p[0]?.length ?? 0;
|
|
54
|
+
const cols = Array.from({ length: nc }, (_, j) => p.reduce((a, r) => a + r[j], 0));
|
|
55
|
+
let s = 0;
|
|
56
|
+
// column-major order, as R sums a matrix
|
|
57
|
+
for (let j = 0; j < nc; j++) {
|
|
58
|
+
for (let i = 0; i < p.length; i++) {
|
|
59
|
+
const p0 = rows[i] * cols[j];
|
|
60
|
+
s += (p[i][j] - p0) ** 2 / p0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return Math.sqrt(s);
|
|
64
|
+
}
|
|
65
|
+
const SIG_MESSAGE = "‘sig.level’ must be numeric in [0, 1]";
|
|
66
|
+
const POWER_MESSAGE = "‘power’ must be numeric in [0, 1]";
|
|
67
|
+
function checkProbabilities(sigLevel, power) {
|
|
68
|
+
const bad = (v) => !isUnknown(v) && !(typeof v === "number" && v >= 0 && v <= 1);
|
|
69
|
+
if (bad(sigLevel))
|
|
70
|
+
throw new RangeError(SIG_MESSAGE);
|
|
71
|
+
if (bad(power))
|
|
72
|
+
throw new RangeError(POWER_MESSAGE);
|
|
73
|
+
}
|
|
74
|
+
/** A string effect size replaced by Cohen's value for the test */
|
|
75
|
+
function effect(es, test) {
|
|
76
|
+
return typeof es === "string" ? cohenES(test, es).effectSize : es;
|
|
77
|
+
}
|
|
78
|
+
const ALTERNATIVES = ["two.sided", "less", "greater"];
|
|
79
|
+
/** The interval of an effect size: two-sided, less, greater */
|
|
80
|
+
function esInterval(alt, two) {
|
|
81
|
+
return alt === "two.sided" ? two : alt === "less" ? [-10, 5] : [-5, 10];
|
|
82
|
+
}
|
|
83
|
+
/** The power of a t test with nu degrees of freedom and noncentrality ncp, by pwr's rejection regions */
|
|
84
|
+
function tPower(nu, ncp, sig, alt) {
|
|
85
|
+
if (alt === "less")
|
|
86
|
+
return ptNcp(qt(sig, nu), nu, ncp, true);
|
|
87
|
+
if (alt === "greater")
|
|
88
|
+
return ptNcp(qt(sig, nu, { lowerTail: false }), nu, ncp, false);
|
|
89
|
+
const q = qt(sig / 2, nu, { lowerTail: false });
|
|
90
|
+
return ptNcp(q, nu, ncp, false) + ptNcp(-q, nu, ncp, true);
|
|
91
|
+
}
|
|
92
|
+
/** The power of a normal test with shift `es * scale`, by pwr's rejection regions */
|
|
93
|
+
function normalPower(shift, sig, alt) {
|
|
94
|
+
if (alt === "greater")
|
|
95
|
+
return pnorm(qnorm(1 - sig) - shift, { lowerTail: false });
|
|
96
|
+
if (alt === "less")
|
|
97
|
+
return pnorm(qnorm(sig) - shift);
|
|
98
|
+
return (pnorm(qnorm(1 - sig / 2) - shift, { lowerTail: false }) + pnorm(qnorm(sig / 2) - shift));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Power of a t test of means, or the sample size, effect size or significance level for a given power. Equivalent
|
|
102
|
+
* to pwr's `pwr.t.test()`.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* pwrTTest({ n: 20, d: 0.5 }).power; // 0.3379390289250411
|
|
107
|
+
* pwrTTest({ d: 0.5, power: 0.8 }).n; // 63.76561044415068
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function pwrTTest(options = {}) {
|
|
111
|
+
let { n, power } = options;
|
|
112
|
+
let d = effect(options.d, "t");
|
|
113
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
114
|
+
checkExactlyOne([n, d, power, sigLevel], "exactly one of n, d, power, and sig.level must be NULL");
|
|
115
|
+
checkProbabilities(sigLevel, power);
|
|
116
|
+
const type = matchChoice(options.type, ["two.sample", "one.sample", "paired"]);
|
|
117
|
+
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
118
|
+
const ts = type === "two.sample" ? 2 : 1;
|
|
119
|
+
if (alt === "two.sided" && !isUnknown(d))
|
|
120
|
+
d = Math.abs(d);
|
|
121
|
+
const powerOf = (n, d, sig) => tPower((n - 1) * ts, Math.sqrt(n / ts) * d, sig, alt);
|
|
122
|
+
if (isUnknown(power))
|
|
123
|
+
power = powerOf(n, d, sigLevel);
|
|
124
|
+
else {
|
|
125
|
+
const target = power;
|
|
126
|
+
if (isUnknown(n))
|
|
127
|
+
n = solveFor((x) => powerOf(x, d, sigLevel) - target, 2 + 1e-10, 1e9);
|
|
128
|
+
else if (isUnknown(d))
|
|
129
|
+
d = solveFor((x) => powerOf(n, x, sigLevel) - target, ...esInterval(alt, [1e-7, 10]));
|
|
130
|
+
else
|
|
131
|
+
sigLevel = solveFor((x) => powerOf(n, d, x) - target, 1e-10, 1 - 1e-10);
|
|
132
|
+
}
|
|
133
|
+
const note = type === "paired" ? "n is number of *pairs*" : type === "two.sample" ? "n is number in *each* group" : undefined;
|
|
134
|
+
const method = { "two.sample": "Two-sample", "one.sample": "One-sample", paired: "Paired" }[type] + " t test power calculation";
|
|
135
|
+
return { n: n, d: d, sigLevel: sigLevel, power, alternative: alt, ...(note === undefined ? {} : { note }), method };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Power of a two-sample t test with unequal sample sizes, or a sample size, the effect size or the significance
|
|
139
|
+
* level for a given power. Equivalent to pwr's `pwr.t2n.test()`.
|
|
140
|
+
*/
|
|
141
|
+
export function pwrT2nTest(options = {}) {
|
|
142
|
+
let { n1, n2, power } = options;
|
|
143
|
+
let d = effect(options.d, "t");
|
|
144
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
145
|
+
checkExactlyOne([n1, n2, d, power, sigLevel], "exactly one of n1, n2, d, power, and sig.level must be NULL");
|
|
146
|
+
checkProbabilities(sigLevel, power);
|
|
147
|
+
if (!isUnknown(n1) && n1 < 2)
|
|
148
|
+
throw new RangeError("number of observations in the first group must be at least 2");
|
|
149
|
+
if (!isUnknown(n2) && n2 < 2)
|
|
150
|
+
throw new RangeError("number of observations in the second group must be at least 2");
|
|
151
|
+
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
152
|
+
if (alt === "two.sided" && !isUnknown(d))
|
|
153
|
+
d = Math.abs(d);
|
|
154
|
+
const powerOf = (n1, n2, d, sig) => tPower(n1 + n2 - 2, d / Math.sqrt(1 / n1 + 1 / n2), sig, alt);
|
|
155
|
+
if (isUnknown(power))
|
|
156
|
+
power = powerOf(n1, n2, d, sigLevel);
|
|
157
|
+
else {
|
|
158
|
+
const target = power;
|
|
159
|
+
if (isUnknown(n1))
|
|
160
|
+
n1 = solveFor((x) => powerOf(x, n2, d, sigLevel) - target, 2 + 1e-10, 1e9);
|
|
161
|
+
else if (isUnknown(n2))
|
|
162
|
+
n2 = solveFor((x) => powerOf(n1, x, d, sigLevel) - target, 2 + 1e-10, 1e9);
|
|
163
|
+
else if (isUnknown(d))
|
|
164
|
+
d = solveFor((x) => powerOf(n1, n2, x, sigLevel) - target, ...esInterval(alt, [1e-7, 10]));
|
|
165
|
+
else
|
|
166
|
+
sigLevel = solveFor((x) => powerOf(n1, n2, d, x) - target, 1e-10, 1 - 1e-10);
|
|
167
|
+
}
|
|
168
|
+
return { n1: n1, n2: n2, d: d, sigLevel: sigLevel, power, alternative: alt, method: "t test power calculation" };
|
|
169
|
+
}
|
|
170
|
+
/** The normal-approximation tests: pwr.2p.test (scale sqrt(n/2)), pwr.p.test and pwr.norm.test (sqrt(n)) */
|
|
171
|
+
function normalTest(es0, options, name, scale, nLower, esLower) {
|
|
172
|
+
let { n, power } = options;
|
|
173
|
+
let es = effect(es0, name === "h" ? "p" : "t");
|
|
174
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
175
|
+
checkExactlyOne([es, n, power, sigLevel], `exactly one of ${name}, n, power, and sig.level must be NULL`);
|
|
176
|
+
checkProbabilities(sigLevel, power);
|
|
177
|
+
if (!isUnknown(n) && n < 1)
|
|
178
|
+
throw new RangeError("number of observations in each group must be at least 1");
|
|
179
|
+
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
180
|
+
if (alt === "two.sided" && !isUnknown(es))
|
|
181
|
+
es = Math.abs(es);
|
|
182
|
+
const powerOf = (es, n, sig) => normalPower(es * scale(n), sig, alt);
|
|
183
|
+
if (isUnknown(power))
|
|
184
|
+
power = powerOf(es, n, sigLevel);
|
|
185
|
+
else {
|
|
186
|
+
const target = power;
|
|
187
|
+
if (isUnknown(es))
|
|
188
|
+
es = solveFor((x) => powerOf(x, n, sigLevel) - target, ...esInterval(alt, [esLower, 10]));
|
|
189
|
+
else if (isUnknown(n))
|
|
190
|
+
n = solveFor((x) => powerOf(es, x, sigLevel) - target, nLower, 1e9);
|
|
191
|
+
else
|
|
192
|
+
sigLevel = solveFor((x) => powerOf(es, n, x) - target, 1e-10, 1 - 1e-10);
|
|
193
|
+
}
|
|
194
|
+
return { es: es, n: n, sigLevel: sigLevel, power, alternative: alt };
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Power of the test of two proportions with equal sample sizes (arcsine transformation), or the effect size, sample
|
|
198
|
+
* size or significance level for a given power. Equivalent to pwr's `pwr.2p.test()`.
|
|
199
|
+
*/
|
|
200
|
+
export function pwr2pTest(options = {}) {
|
|
201
|
+
const r = normalTest(options.h, options, "h", (n) => Math.sqrt(n / 2), 2 + 1e-10, 1e-10);
|
|
202
|
+
return {
|
|
203
|
+
h: r.es,
|
|
204
|
+
n: r.n,
|
|
205
|
+
sigLevel: r.sigLevel,
|
|
206
|
+
power: r.power,
|
|
207
|
+
alternative: r.alternative,
|
|
208
|
+
method: "Difference of proportion power calculation for binomial distribution (arcsine transformation)",
|
|
209
|
+
note: "same sample sizes",
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Power of the test of one proportion (arcsine transformation), or the effect size, sample size or significance
|
|
214
|
+
* level for a given power. Equivalent to pwr's `pwr.p.test()`.
|
|
215
|
+
*/
|
|
216
|
+
export function pwrPTest(options = {}) {
|
|
217
|
+
const r = normalTest(options.h, options, "h", (n) => Math.sqrt(n), 2 + 1e-10, 1e-10);
|
|
218
|
+
return {
|
|
219
|
+
h: r.es,
|
|
220
|
+
n: r.n,
|
|
221
|
+
sigLevel: r.sigLevel,
|
|
222
|
+
power: r.power,
|
|
223
|
+
alternative: r.alternative,
|
|
224
|
+
method: "proportion power calculation for binomial distribution (arcsine transformation)",
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Power of the test of a mean of a normal distribution with known variance, or the effect size, sample size or
|
|
229
|
+
* significance level for a given power. Equivalent to pwr's `pwr.norm.test()`.
|
|
230
|
+
*/
|
|
231
|
+
export function pwrNormTest(options = {}) {
|
|
232
|
+
const r = normalTest(options.d, options, "d", (n) => Math.sqrt(n), 1 + 1e-10, 1e-10);
|
|
233
|
+
return {
|
|
234
|
+
d: r.es,
|
|
235
|
+
n: r.n,
|
|
236
|
+
sigLevel: r.sigLevel,
|
|
237
|
+
power: r.power,
|
|
238
|
+
alternative: r.alternative,
|
|
239
|
+
method: "Mean power calculation for normal distribution with known variance",
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Power of the test of two proportions with unequal sample sizes (arcsine transformation), or the effect size, a
|
|
244
|
+
* sample size or the significance level for a given power. Equivalent to pwr's `pwr.2p2n.test()`.
|
|
245
|
+
*/
|
|
246
|
+
export function pwr2p2nTest(options = {}) {
|
|
247
|
+
let { n1, n2, power } = options;
|
|
248
|
+
let h = effect(options.h, "p");
|
|
249
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
250
|
+
checkExactlyOne([h, n1, n2, power, sigLevel], "exactly one of h, n1, n2, power, and sig.level must be NULL");
|
|
251
|
+
checkProbabilities(sigLevel, power);
|
|
252
|
+
if (!isUnknown(n1) && n1 < 2)
|
|
253
|
+
throw new RangeError("number of observations in the first group must be at least 2");
|
|
254
|
+
if (!isUnknown(n2) && n2 < 2)
|
|
255
|
+
throw new RangeError("number of observations in the second group must be at least 2");
|
|
256
|
+
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
257
|
+
if (alt === "two.sided" && !isUnknown(h))
|
|
258
|
+
h = Math.abs(h);
|
|
259
|
+
const powerOf = (h, n1, n2, sig) => normalPower(h * Math.sqrt((n1 * n2) / (n1 + n2)), sig, alt);
|
|
260
|
+
if (isUnknown(power))
|
|
261
|
+
power = powerOf(h, n1, n2, sigLevel);
|
|
262
|
+
else {
|
|
263
|
+
const target = power;
|
|
264
|
+
if (isUnknown(h))
|
|
265
|
+
h = solveFor((x) => powerOf(x, n1, n2, sigLevel) - target, ...esInterval(alt, [1e-10, 10]));
|
|
266
|
+
else if (isUnknown(n1))
|
|
267
|
+
n1 = solveFor((x) => powerOf(h, x, n2, sigLevel) - target, 2 + 1e-10, 1e9);
|
|
268
|
+
else if (isUnknown(n2))
|
|
269
|
+
n2 = solveFor((x) => powerOf(h, n1, x, sigLevel) - target, 2 + 1e-10, 1e9);
|
|
270
|
+
else
|
|
271
|
+
sigLevel = solveFor((x) => powerOf(h, n1, n2, x) - target, 1e-10, 1 - 1e-10);
|
|
272
|
+
}
|
|
273
|
+
return {
|
|
274
|
+
h: h,
|
|
275
|
+
n1: n1,
|
|
276
|
+
n2: n2,
|
|
277
|
+
sigLevel: sigLevel,
|
|
278
|
+
power,
|
|
279
|
+
alternative: alt,
|
|
280
|
+
method: "difference of proportion power calculation for binomial distribution (arcsine transformation)",
|
|
281
|
+
note: "different sample sizes",
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Power of the test of a correlation (Fisher's z transformation), or the sample size, correlation or significance
|
|
286
|
+
* level for a given power. Equivalent to pwr's `pwr.r.test()`. As in pwr, the power computed for `"less"` reports
|
|
287
|
+
* `r` negated.
|
|
288
|
+
*/
|
|
289
|
+
export function pwrRTest(options = {}) {
|
|
290
|
+
let { n, power } = options;
|
|
291
|
+
let r = effect(options.r, "r");
|
|
292
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
293
|
+
checkExactlyOne([n, r, power, sigLevel], "exactly one of n, r, power, and sig.level must be NULL");
|
|
294
|
+
checkProbabilities(sigLevel, power);
|
|
295
|
+
if (!isUnknown(n) && n < 4)
|
|
296
|
+
throw new RangeError("number of observations must be at least 4");
|
|
297
|
+
const alt = matchChoice(options.alternative, ALTERNATIVES);
|
|
298
|
+
if (alt === "two.sided" && !isUnknown(r))
|
|
299
|
+
r = Math.abs(r);
|
|
300
|
+
const upper = (n, r, sig, twoSided) => {
|
|
301
|
+
const t = qt(twoSided ? sig / 2 : sig, n - 2, { lowerTail: false });
|
|
302
|
+
const rc = Math.sqrt(t ** 2 / (t ** 2 + n - 2));
|
|
303
|
+
const zr = Math.atanh(r) + r / (2 * (n - 1));
|
|
304
|
+
const zrc = Math.atanh(rc);
|
|
305
|
+
const p = pnorm((zr - zrc) * Math.sqrt(n - 3));
|
|
306
|
+
return twoSided ? p + pnorm((-zr - zrc) * Math.sqrt(n - 3)) : p;
|
|
307
|
+
};
|
|
308
|
+
const powerOf = (n, r, sig) => alt === "two.sided" ? upper(n, r, sig, true) : upper(n, alt === "less" ? -r : r, sig, false);
|
|
309
|
+
if (isUnknown(power)) {
|
|
310
|
+
power = powerOf(n, r, sigLevel);
|
|
311
|
+
if (alt === "less")
|
|
312
|
+
r = -r;
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
const target = power;
|
|
316
|
+
if (isUnknown(n))
|
|
317
|
+
n = solveFor((x) => powerOf(x, r, sigLevel) - target, 4 + 1e-10, 1e9);
|
|
318
|
+
else if (isUnknown(r)) {
|
|
319
|
+
const [lo, hi] = alt === "two.sided" ? [1e-10, 1 - 1e-10] : [-1 + 1e-10, 1 - 1e-10];
|
|
320
|
+
r = solveFor((x) => powerOf(n, x, sigLevel) - target, lo, hi);
|
|
321
|
+
}
|
|
322
|
+
else
|
|
323
|
+
sigLevel = solveFor((x) => powerOf(n, r, x) - target, 1e-10, 1 - 1e-10);
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
n: n,
|
|
327
|
+
r: r,
|
|
328
|
+
sigLevel: sigLevel,
|
|
329
|
+
power,
|
|
330
|
+
alternative: alt,
|
|
331
|
+
method: "approximate correlation power calculation (arctangh transformation)",
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Power of a chi-squared test, or the effect size, number of observations or significance level for a given power.
|
|
336
|
+
* Equivalent to pwr's `pwr.chisq.test()`.
|
|
337
|
+
*/
|
|
338
|
+
export function pwrChisqTest(options) {
|
|
339
|
+
let { N, power } = options;
|
|
340
|
+
const { df } = options;
|
|
341
|
+
let w = effect(options.w, "chisq");
|
|
342
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
343
|
+
checkExactlyOne([w, N, power, sigLevel], "exactly one of w, N, power, and sig.level must be NULL");
|
|
344
|
+
if (typeof df !== "number")
|
|
345
|
+
throw new TypeError('argument "df" is missing, with no default');
|
|
346
|
+
checkProbabilities(sigLevel, power);
|
|
347
|
+
if (!isUnknown(w) && w < 0)
|
|
348
|
+
throw new RangeError("w must be positive");
|
|
349
|
+
if (!isUnknown(N) && N < 1)
|
|
350
|
+
throw new RangeError("number of observations must be at least 1");
|
|
351
|
+
const powerOf = (w, N, sig) => pchisq(qchisq(sig, df, { lowerTail: false }), df, { ncp: N * w ** 2, lowerTail: false });
|
|
352
|
+
if (isUnknown(power))
|
|
353
|
+
power = powerOf(w, N, sigLevel);
|
|
354
|
+
else {
|
|
355
|
+
const target = power;
|
|
356
|
+
if (isUnknown(w))
|
|
357
|
+
w = solveFor((x) => powerOf(x, N, sigLevel) - target, 1e-10, 1e9);
|
|
358
|
+
else if (isUnknown(N))
|
|
359
|
+
N = solveFor((x) => powerOf(w, x, sigLevel) - target, 1 + 1e-10, 1e9);
|
|
360
|
+
else
|
|
361
|
+
sigLevel = solveFor((x) => powerOf(w, N, x) - target, 1e-10, 1 - 1e-10);
|
|
362
|
+
}
|
|
363
|
+
return { w: w, N: N, df, sigLevel: sigLevel, power, method: "Chi squared power calculation", note: "N is the number of observations" };
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Power of the F test of a balanced one-way analysis of variance, or the number of groups, sample size, effect size
|
|
367
|
+
* or significance level for a given power. Equivalent to pwr's `pwr.anova.test()`.
|
|
368
|
+
*/
|
|
369
|
+
export function pwrAnovaTest(options = {}) {
|
|
370
|
+
let { k, n, power } = options;
|
|
371
|
+
let f = effect(options.f, "anov");
|
|
372
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
373
|
+
checkExactlyOne([k, n, f, power, sigLevel], "exactly one of k, n, f, power, and sig.level must be NULL");
|
|
374
|
+
checkProbabilities(sigLevel, power);
|
|
375
|
+
if (!isUnknown(f) && f < 0)
|
|
376
|
+
throw new RangeError("f must be positive");
|
|
377
|
+
if (!isUnknown(k) && k < 2)
|
|
378
|
+
throw new RangeError("number of groups must be at least 2");
|
|
379
|
+
if (!isUnknown(n) && n < 2)
|
|
380
|
+
throw new RangeError("number of observations in each group must be at least 2");
|
|
381
|
+
const powerOf = (k, n, f, sig) => {
|
|
382
|
+
const df1 = k - 1;
|
|
383
|
+
const df2 = (n - 1) * k;
|
|
384
|
+
return pf(qf(sig, df1, df2, { lowerTail: false }), df1, df2, { ncp: k * n * f ** 2, lowerTail: false });
|
|
385
|
+
};
|
|
386
|
+
if (isUnknown(power))
|
|
387
|
+
power = powerOf(k, n, f, sigLevel);
|
|
388
|
+
else {
|
|
389
|
+
const target = power;
|
|
390
|
+
if (isUnknown(k))
|
|
391
|
+
k = solveFor((x) => powerOf(x, n, f, sigLevel) - target, 2 + 1e-10, 100);
|
|
392
|
+
else if (isUnknown(n))
|
|
393
|
+
n = solveFor((x) => powerOf(k, x, f, sigLevel) - target, 2 + 1e-10, 1e9);
|
|
394
|
+
else if (isUnknown(f))
|
|
395
|
+
f = solveFor((x) => powerOf(k, n, x, sigLevel) - target, 1e-7, 1e7);
|
|
396
|
+
else
|
|
397
|
+
sigLevel = solveFor((x) => powerOf(k, n, f, x) - target, 1e-10, 1 - 1e-10);
|
|
398
|
+
}
|
|
399
|
+
return {
|
|
400
|
+
k: k,
|
|
401
|
+
n: n,
|
|
402
|
+
f: f,
|
|
403
|
+
sigLevel: sigLevel,
|
|
404
|
+
power,
|
|
405
|
+
note: "n is number in each group",
|
|
406
|
+
method: "Balanced one-way analysis of variance power calculation",
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Power of the F test of a general linear model, or the degrees of freedom, effect size or significance level for a
|
|
411
|
+
* given power. Equivalent to pwr's `pwr.f2.test()`.
|
|
412
|
+
*/
|
|
413
|
+
export function pwrF2Test(options = {}) {
|
|
414
|
+
let { u, v, power } = options;
|
|
415
|
+
let f2 = effect(options.f2, "f2");
|
|
416
|
+
let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
|
|
417
|
+
checkExactlyOne([u, v, f2, power, sigLevel], "exactly one of u, v, f2, power, and sig.level must be NULL");
|
|
418
|
+
checkProbabilities(sigLevel, power);
|
|
419
|
+
if (!isUnknown(f2) && f2 < 0)
|
|
420
|
+
throw new RangeError("f2 must be positive");
|
|
421
|
+
if (!isUnknown(u) && u < 1)
|
|
422
|
+
throw new RangeError("degree of freedom u for numerator must be at least 1");
|
|
423
|
+
if (!isUnknown(v) && v < 1)
|
|
424
|
+
throw new RangeError("degree of freedom v for denominator must be at least 1");
|
|
425
|
+
const powerOf = (u, v, f2, sig) => pf(qf(sig, u, v, { lowerTail: false }), u, v, { ncp: f2 * (u + v + 1), lowerTail: false });
|
|
426
|
+
if (isUnknown(power))
|
|
427
|
+
power = powerOf(u, v, f2, sigLevel);
|
|
428
|
+
else {
|
|
429
|
+
const target = power;
|
|
430
|
+
if (isUnknown(u))
|
|
431
|
+
u = solveFor((x) => powerOf(x, v, f2, sigLevel) - target, 1 + 1e-10, 100);
|
|
432
|
+
else if (isUnknown(v))
|
|
433
|
+
v = solveFor((x) => powerOf(u, x, f2, sigLevel) - target, 1 + 1e-10, 1e9);
|
|
434
|
+
else if (isUnknown(f2))
|
|
435
|
+
f2 = solveFor((x) => powerOf(u, v, x, sigLevel) - target, 1e-7, 1e7);
|
|
436
|
+
else
|
|
437
|
+
sigLevel = solveFor((x) => powerOf(u, v, f2, x) - target, 1e-10, 1 - 1e-10);
|
|
438
|
+
}
|
|
439
|
+
return { u: u, v: v, f2: f2, sigLevel: sigLevel, power, method: "Multiple regression power calculation" };
|
|
440
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type GroupLabel } from "./groups.js";
|
|
2
3
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
3
4
|
/** Result of a k-sample test with a chi-squared statistic. Has no `alternative`, as R returns none. */
|
|
@@ -11,15 +12,45 @@ export interface ChisqStatTestResult extends Omit<HTest, "alternative"> {
|
|
|
11
12
|
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
12
13
|
* labels `g`.
|
|
13
14
|
* @param g Group labels, parallel to `x`.
|
|
15
|
+
*
|
|
16
|
+
* There is also a formula form, `kruskalTest("y ~ g", data)` (next overload).
|
|
14
17
|
*/
|
|
15
18
|
export declare function kruskalTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[]): ChisqStatTestResult;
|
|
19
|
+
/**
|
|
20
|
+
* Kruskal-Wallis test from a formula and a data frame. Equivalent to R's `kruskal.test(formula, data)`: `"y ~ g"`,
|
|
21
|
+
* one group per level of `g`. Only the variables of the formula count: the rows where one of them is missing are
|
|
22
|
+
* dropped.
|
|
23
|
+
*
|
|
24
|
+
* @param formula `"y ~ g"` (one variable on each side).
|
|
25
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
26
|
+
*/
|
|
27
|
+
export declare function kruskalTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
16
28
|
/**
|
|
17
29
|
* Friedman rank sum test for an unreplicated complete block design. Equivalent to R's `friedman.test()` with a
|
|
18
30
|
* matrix.
|
|
19
31
|
*
|
|
20
32
|
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
33
|
+
*
|
|
34
|
+
* There is also a formula form, `friedmanTest("y ~ w | b", data)` (next overload).
|
|
21
35
|
*/
|
|
22
36
|
export declare function friedmanTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
|
|
37
|
+
/**
|
|
38
|
+
* Friedman test from a formula and a data frame. Equivalent to R's `friedman.test(formula, data)`: `"y ~ w | b"`
|
|
39
|
+
* with the groups `w` and the blocks `b`. The rows where `y`, `w` or `b` is missing are dropped first; then every
|
|
40
|
+
* combination of group and block must occur exactly once (else the error `not an unreplicated complete block
|
|
41
|
+
* design`, so that one missing value fails, unlike the matrix form, which drops its block).
|
|
42
|
+
*
|
|
43
|
+
* @param formula `"y ~ w | b"` (one variable on each side of `|`).
|
|
44
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
45
|
+
*/
|
|
46
|
+
export declare function friedmanTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
47
|
+
/**
|
|
48
|
+
* Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to DescTools'
|
|
49
|
+
* `CochranQTest()` with a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
|
|
50
|
+
*
|
|
51
|
+
* @param y Blocks as rows, treatments as columns, values 0 or 1. Rows with a missing value are dropped.
|
|
52
|
+
*/
|
|
53
|
+
export declare function cochranQTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
|
|
23
54
|
/** Result of {@link quadeTest}: an F statistic with two degrees of freedom. */
|
|
24
55
|
export interface QuadeTestResult extends Omit<HTest, "alternative" | "parameter" | "parameterName"> {
|
|
25
56
|
/** `[num df, denom df]`. */
|
|
@@ -30,16 +61,38 @@ export interface QuadeTestResult extends Omit<HTest, "alternative" | "parameter"
|
|
|
30
61
|
* Quade test for an unreplicated complete block design. Equivalent to R's `quade.test()` with a matrix.
|
|
31
62
|
*
|
|
32
63
|
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
64
|
+
*
|
|
65
|
+
* There is also a formula form, `quadeTest("y ~ w | b", data)` (next overload).
|
|
33
66
|
*/
|
|
34
67
|
export declare function quadeTest(y: readonly (readonly number[])[]): QuadeTestResult;
|
|
68
|
+
/**
|
|
69
|
+
* Quade test from a formula and a data frame. Equivalent to R's `quade.test(formula, data)`: `"y ~ w | b"` with the
|
|
70
|
+
* groups `w` and the blocks `b`. The rows where `y`, `w` or `b` is missing are dropped first; then every combination
|
|
71
|
+
* of group and block must occur exactly once (else the error `not an unreplicated complete block design`).
|
|
72
|
+
*
|
|
73
|
+
* @param formula `"y ~ w | b"` (one variable on each side of `|`).
|
|
74
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
75
|
+
*/
|
|
76
|
+
export declare function quadeTest(formula: string, data: DataFrame): QuadeTestResult;
|
|
35
77
|
/**
|
|
36
78
|
* Fligner-Killeen (median) test of homogeneity of variances. Equivalent to R's `fligner.test()`.
|
|
37
79
|
*
|
|
38
80
|
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
39
81
|
* labels `g`.
|
|
40
82
|
* @param g Group labels, parallel to `x`.
|
|
83
|
+
*
|
|
84
|
+
* There is also a formula form, `flignerTest("y ~ g", data)` (next overload).
|
|
41
85
|
*/
|
|
42
86
|
export declare function flignerTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[]): ChisqStatTestResult;
|
|
87
|
+
/**
|
|
88
|
+
* Fligner-Killeen test from a formula and a data frame. Equivalent to R's `fligner.test(formula, data)`: `"y ~ g"`,
|
|
89
|
+
* one group per level of `g`. Only the variables of the formula count: the rows where one of them is missing are
|
|
90
|
+
* dropped.
|
|
91
|
+
*
|
|
92
|
+
* @param formula `"y ~ g"` (one variable on each side).
|
|
93
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
94
|
+
*/
|
|
95
|
+
export declare function flignerTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
43
96
|
/** Options for {@link moodTest}. */
|
|
44
97
|
export interface MoodTestOptions {
|
|
45
98
|
/** Alternative hypothesis (unambiguous abbreviations accepted). Default `"two.sided"`. */
|
|
@@ -48,5 +101,16 @@ export interface MoodTestOptions {
|
|
|
48
101
|
/**
|
|
49
102
|
* Mood's two-sample test of scale. Equivalent to R's `mood.test()`. `NaN` values are dropped; ties get the average
|
|
50
103
|
* of the scores of their positions.
|
|
104
|
+
*
|
|
105
|
+
* There is also a formula form, `moodTest("y ~ g", data, options)` (next overload).
|
|
51
106
|
*/
|
|
52
107
|
export declare function moodTest(x: Sample, y: Sample, options?: MoodTestOptions): HTest;
|
|
108
|
+
/**
|
|
109
|
+
* Mood's test from a formula and a data frame. Equivalent to R's `mood.test(formula, data)`: `"y ~ g"` compares the
|
|
110
|
+
* two levels of `g` (the first level is `x`). Only the variables of the formula count: the rows where one of them is
|
|
111
|
+
* missing are dropped.
|
|
112
|
+
*
|
|
113
|
+
* @param formula `"y ~ g"`.
|
|
114
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
115
|
+
*/
|
|
116
|
+
export declare function moodTest(formula: string, data: DataFrame, options?: MoodTestOptions): HTest;
|
package/dist/htest/rankTests.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// Rank-based k-sample and two-sample tests: R's kruskal.test, friedman.test, quade.test, fligner.test and mood.test
|
|
1
|
+
// Rank-based k-sample and two-sample tests: R's kruskal.test, friedman.test, quade.test, fligner.test and mood.test,
|
|
2
|
+
// and DescTools' CochranQTest (the Friedman test of a 0/1 matrix, from oracle/rm-tests-behaviour.md).
|
|
2
3
|
//
|
|
3
4
|
// Written for rStats.js from the published statistics (no ported code), with ties handled by average ranks:
|
|
4
5
|
// - Kruskal and Wallis (JASA 47, 1952): H = (12 / (N (N + 1)) sum R_i^2 / n_i - 3 (N + 1)) / (1 - sum (t^3 - t) /
|
|
@@ -20,6 +21,7 @@ import { pgamma1 } from "../distributions/gamma.js";
|
|
|
20
21
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
21
22
|
import { rank, tieCounts, variance } from "../internal/summary.js";
|
|
22
23
|
import { completeRows, median, toGroups } from "./groups.js";
|
|
24
|
+
import { blockData, kSampleData, twoGroupData } from "./testFormula.js";
|
|
23
25
|
import { matchAlternative } from "./types.js";
|
|
24
26
|
function chisqResult(method, name, statistic, df) {
|
|
25
27
|
return {
|
|
@@ -37,14 +39,11 @@ function sumTies(x) {
|
|
|
37
39
|
s += t * t * t - t;
|
|
38
40
|
return s;
|
|
39
41
|
}
|
|
40
|
-
/**
|
|
41
|
-
* Kruskal-Wallis rank sum test. Equivalent to R's `kruskal.test()`.
|
|
42
|
-
*
|
|
43
|
-
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
44
|
-
* labels `g`.
|
|
45
|
-
* @param g Group labels, parallel to `x`.
|
|
46
|
-
*/
|
|
47
42
|
export function kruskalTest(x, g) {
|
|
43
|
+
if (typeof x === "string") {
|
|
44
|
+
const d = kSampleData(x, g);
|
|
45
|
+
return kruskalTest(d.values, d.group.codes);
|
|
46
|
+
}
|
|
48
47
|
const groups = toGroups(x, g, "error");
|
|
49
48
|
const k = groups.length;
|
|
50
49
|
if (k < 2)
|
|
@@ -64,13 +63,9 @@ export function kruskalTest(x, g) {
|
|
|
64
63
|
stat = ((12 * stat) / (n * (n + 1)) - 3 * (n + 1)) / (1 - sumTies(all) / (n * n * n - n));
|
|
65
64
|
return chisqResult("Kruskal-Wallis rank sum test", "Kruskal-Wallis chi-squared", stat, k - 1);
|
|
66
65
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
*
|
|
71
|
-
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
72
|
-
*/
|
|
73
|
-
export function friedmanTest(y) {
|
|
66
|
+
export function friedmanTest(y, data) {
|
|
67
|
+
if (typeof y === "string")
|
|
68
|
+
return friedmanTest(blockData(y, data));
|
|
74
69
|
const rows = completeRows(y);
|
|
75
70
|
const n = rows.length;
|
|
76
71
|
const k = y[0]?.length ?? 0;
|
|
@@ -92,11 +87,17 @@ export function friedmanTest(y) {
|
|
|
92
87
|
return chisqResult("Friedman rank sum test", "Friedman chi-squared", stat, k - 1);
|
|
93
88
|
}
|
|
94
89
|
/**
|
|
95
|
-
*
|
|
90
|
+
* Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to DescTools'
|
|
91
|
+
* `CochranQTest()` with a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
|
|
96
92
|
*
|
|
97
|
-
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
93
|
+
* @param y Blocks as rows, treatments as columns, values 0 or 1. Rows with a missing value are dropped.
|
|
98
94
|
*/
|
|
99
|
-
export function
|
|
95
|
+
export function cochranQTest(y) {
|
|
96
|
+
return { ...friedmanTest(y), method: "Cochran's Q test", statisticName: "Q" };
|
|
97
|
+
}
|
|
98
|
+
export function quadeTest(y, data) {
|
|
99
|
+
if (typeof y === "string")
|
|
100
|
+
return quadeTest(blockData(y, data));
|
|
100
101
|
const rows = completeRows(y);
|
|
101
102
|
// as in R, b counts all blocks, including those dropped for a missing value
|
|
102
103
|
const b = y.length;
|
|
@@ -145,14 +146,11 @@ export function quadeTest(y) {
|
|
|
145
146
|
pValue,
|
|
146
147
|
};
|
|
147
148
|
}
|
|
148
|
-
/**
|
|
149
|
-
* Fligner-Killeen (median) test of homogeneity of variances. Equivalent to R's `fligner.test()`.
|
|
150
|
-
*
|
|
151
|
-
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
152
|
-
* labels `g`.
|
|
153
|
-
* @param g Group labels, parallel to `x`.
|
|
154
|
-
*/
|
|
155
149
|
export function flignerTest(x, g) {
|
|
150
|
+
if (typeof x === "string") {
|
|
151
|
+
const d = kSampleData(x, g);
|
|
152
|
+
return flignerTest(d.values, d.group.codes);
|
|
153
|
+
}
|
|
156
154
|
const groups = toGroups(x, g, "error");
|
|
157
155
|
const k = groups.length;
|
|
158
156
|
if (k < 2)
|
|
@@ -181,11 +179,11 @@ export function flignerTest(x, g) {
|
|
|
181
179
|
stat /= variance(a);
|
|
182
180
|
return chisqResult("Fligner-Killeen test of homogeneity of variances", "Fligner-Killeen:med chi-squared", stat, k - 1);
|
|
183
181
|
}
|
|
184
|
-
/**
|
|
185
|
-
* Mood's two-sample test of scale. Equivalent to R's `mood.test()`. `NaN` values are dropped; ties get the average
|
|
186
|
-
* of the scores of their positions.
|
|
187
|
-
*/
|
|
188
182
|
export function moodTest(x, y, options = {}) {
|
|
183
|
+
if (typeof x === "string") {
|
|
184
|
+
const d = twoGroupData(x, y);
|
|
185
|
+
return moodTest(d.x, d.y, options);
|
|
186
|
+
}
|
|
189
187
|
// as in R, infinite values are dropped along with NaN
|
|
190
188
|
const xs = Array.from(x).filter(Number.isFinite);
|
|
191
189
|
const ys = Array.from(y).filter(Number.isFinite);
|