@numero-uno-jpn/rstats 0.8.0 → 0.10.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 +88 -0
- package/README.ja.md +89 -13
- package/README.md +90 -13
- package/THIRD_PARTY_NOTICES.md +12 -2
- package/dist/chunks/{chunk-OHPHNE3O.js → chunk-2XKG3DX4.js} +1 -1
- package/dist/chunks/{chunk-3I6EKGB6.js → chunk-4NM37MGA.js} +2 -2
- package/dist/chunks/chunk-7HPVUVZB.js +2 -0
- package/dist/chunks/{chunk-I3LAZE6C.js → chunk-AECGWZHD.js} +2 -2
- package/dist/chunks/chunk-ENL43FNO.js +2 -0
- package/dist/chunks/chunk-EWC6I75B.js +2 -0
- package/dist/chunks/chunk-IT354SHV.js +3 -0
- package/dist/chunks/{chunk-VIPSPG3Y.js → chunk-LIMZKONM.js} +2 -2
- package/dist/chunks/chunk-O6PT7IVU.js +2 -0
- package/dist/chunks/{chunk-YGWPU67I.js → chunk-QFULZNHL.js} +1 -1
- package/dist/chunks/{chunk-XU4BGFZ6.js → chunk-QJJWGNJP.js} +2 -2
- package/dist/chunks/{chunk-32PRRONF.js → chunk-QULPY6YM.js} +1 -1
- package/dist/chunks/chunk-XWTZ4RTR.js +2 -0
- package/dist/chunks/chunk-ZRIM4R4A.js +2 -0
- package/dist/descriptive/factanal.d.ts +56 -0
- package/dist/descriptive/factanal.js +186 -0
- package/dist/descriptive/omega.d.ts +56 -0
- package/dist/descriptive/omega.js +323 -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/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 +25 -1
- package/dist/entries/tests.js +22 -1
- package/dist/htest/agreement.d.ts +141 -0
- package/dist/htest/agreement.js +274 -0
- package/dist/htest/aov.d.ts +19 -0
- package/dist/htest/aov.js +22 -15
- 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/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/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 +7 -0
- package/dist/htest/rankTests.js +11 -1
- package/dist/htest/trendTests.d.ts +57 -0
- package/dist/htest/trendTests.js +167 -0
- 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/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/glm.d.ts +226 -0
- package/dist/models/glm.js +546 -0
- package/dist/models/glmAnova.d.ts +31 -0
- package/dist/models/glmAnova.js +146 -0
- package/dist/models/glmConfint.d.ts +25 -0
- package/dist/models/glmConfint.js +139 -0
- package/dist/models/glmFamily.d.ts +66 -0
- package/dist/models/glmFamily.js +359 -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 -3
- package/dist/special.js +2 -2
- package/dist/tests.js +18 -3
- package/llms.txt +102 -0
- package/package.json +3 -2
- package/dist/chunks/chunk-EJRCDJ76.js +0 -2
- package/dist/chunks/chunk-JERGQKZM.js +0 -2
- package/dist/chunks/chunk-JVYV4633.js +0 -2
- package/dist/chunks/chunk-LXMMKO26.js +0 -2
- package/dist/chunks/chunk-MJQLKEO5.js +0 -2
- package/dist/chunks/chunk-RZ7SBJEG.js +0 -2
- package/dist/chunks/chunk-SPQC445Q.js +0 -2
- package/dist/chunks/chunk-U633VFH3.js +0 -2
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// Multiple comparisons after a one-way layout: PMCMRplus's dscfAllPairsTest, kwAllPairsDunnTest, kwManyOneDunnTest,
|
|
2
|
+
// dunnettTest, gamesHowellTest, tukeyTest, steelTest and manyOneUTest, dunn.test's dunn.test and multcomp's Dunnett contrasts
|
|
3
|
+
// (summary(glht(aov(y ~ g), mcp(g = "Dunnett")))).
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js from the published statistics and the behaviour described in oracle/mcomp-behaviour.md (no
|
|
6
|
+
// ported code):
|
|
7
|
+
// - Dunn (Technometrics 6, 1964): z = (mean rank difference) / sqrt((N (N + 1) / 12 - C) (1/n_i + 1/n_j)) with the
|
|
8
|
+
// tie term C = sum (t^3 - t) / (12 (N - 1)).
|
|
9
|
+
// - Dunnett (JASA 50, 1955): t = (m_j - m_1) / (s sqrt(1/n_j + 1/n_1)) with the pooled s; the simultaneous
|
|
10
|
+
// probabilities are computed exactly by src/htest/dunnett.ts (R computes them by randomised integration).
|
|
11
|
+
// - Dwass (1960), Steel (1960), Critchlow and Fligner (1991): pairwise Mann-Whitney statistics standardised and
|
|
12
|
+
// compared with the studentized range for k means and infinite degrees of freedom.
|
|
13
|
+
// - Steel (Biometrics 15, 1959): the Mann-Whitney statistic of each group against the control, standardised, with
|
|
14
|
+
// simultaneous normal probabilities for the correlations of Dunnett's many-to-one comparisons.
|
|
15
|
+
// - Games and Howell (1976): Welch's standard error and degrees of freedom per pair, studentized range.
|
|
16
|
+
// - Tukey (1953), Tukey-Kramer: the pooled standard error, studentized range on N - k degrees of freedom.
|
|
17
|
+
import { pnorm } from "../distributions/normal.js";
|
|
18
|
+
import { ptukey } from "../distributions/tukey.js";
|
|
19
|
+
import { mean, rank, tieCounts, variance } from "../internal/summary.js";
|
|
20
|
+
import { pDunnettUpper } from "./dunnett.js";
|
|
21
|
+
import { labelledGroups } from "./groups.js";
|
|
22
|
+
import { pAdjust } from "./pAdjust.js";
|
|
23
|
+
import { kruskalTest } from "./rankTests.js";
|
|
24
|
+
import { matchAlternative } from "./types.js";
|
|
25
|
+
const TIES_WARNING = "Ties are present. z-quantiles were corrected for ties.";
|
|
26
|
+
function layout(x, g) {
|
|
27
|
+
const { labels, groups } = labelledGroups(x, g);
|
|
28
|
+
const k = groups.length;
|
|
29
|
+
if (k < 2)
|
|
30
|
+
throw new RangeError("there must be at least two groups");
|
|
31
|
+
const n = groups.map((s) => s.length);
|
|
32
|
+
return { labels, groups, n, N: n.reduce((a, b) => a + b, 0), k };
|
|
33
|
+
}
|
|
34
|
+
/** Mean ranks of the groups in the ranking of all observations, and the tie term C */
|
|
35
|
+
function meanRanks(d) {
|
|
36
|
+
const all = d.groups.flat();
|
|
37
|
+
const r = rank(all);
|
|
38
|
+
const R = [];
|
|
39
|
+
let pos = 0;
|
|
40
|
+
for (const ni of d.n) {
|
|
41
|
+
let s = 0;
|
|
42
|
+
for (let i = 0; i < ni; i++)
|
|
43
|
+
s += r[pos + i];
|
|
44
|
+
pos += ni;
|
|
45
|
+
R.push(s / ni);
|
|
46
|
+
}
|
|
47
|
+
let t3 = 0;
|
|
48
|
+
for (const t of tieCounts(all))
|
|
49
|
+
t3 += t * t * t - t;
|
|
50
|
+
return { R, C: t3 / (12 * (d.N - 1)) };
|
|
51
|
+
}
|
|
52
|
+
/** Lower-triangular table of f(i, j), i > j (row i - 1, column j), NaN above the diagonal */
|
|
53
|
+
function lowerTable(k, f) {
|
|
54
|
+
const out = Array.from({ length: k - 1 }, () => new Array(k - 1).fill(NaN));
|
|
55
|
+
for (let j = 0; j < k - 1; j++)
|
|
56
|
+
for (let i = j + 1; i < k; i++)
|
|
57
|
+
out[i - 1][j] = f(i, j);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
/** p.adjust over the lower-triangle cells, column by column (as R's pairwise.table) */
|
|
61
|
+
function adjustLower(p, method) {
|
|
62
|
+
const k = p.length + 1;
|
|
63
|
+
const raw = [];
|
|
64
|
+
for (let j = 0; j < k - 1; j++)
|
|
65
|
+
for (let i = j + 1; i < k; i++)
|
|
66
|
+
raw.push(p[i - 1][j]);
|
|
67
|
+
const adj = pAdjust(raw, method);
|
|
68
|
+
let idx = 0;
|
|
69
|
+
const out = p.map((row) => row.slice());
|
|
70
|
+
for (let j = 0; j < k - 1; j++)
|
|
71
|
+
for (let i = j + 1; i < k; i++)
|
|
72
|
+
out[i - 1][j] = adj[idx++];
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
function allPairs(d, method, statistic, pValue, adjust, warnings = []) {
|
|
76
|
+
return {
|
|
77
|
+
method,
|
|
78
|
+
statistic,
|
|
79
|
+
pValue,
|
|
80
|
+
rowNames: d.labels.slice(1),
|
|
81
|
+
colNames: d.labels.slice(0, -1),
|
|
82
|
+
pAdjustMethod: adjust,
|
|
83
|
+
warnings,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function manyOne(d, method, statistic, pValue, adjust, warnings = []) {
|
|
87
|
+
return {
|
|
88
|
+
method,
|
|
89
|
+
statistic: statistic.map((v) => [v]),
|
|
90
|
+
pValue: pValue.map((v) => [v]),
|
|
91
|
+
rowNames: d.labels.slice(1),
|
|
92
|
+
colNames: d.labels.slice(0, 1),
|
|
93
|
+
pAdjustMethod: adjust,
|
|
94
|
+
warnings,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const upperNormal = (z) => pnorm(z, { lowerTail: false });
|
|
98
|
+
const upperRange = (q, k, df) => ptukey(Math.abs(q), k, df, { lowerTail: false });
|
|
99
|
+
/** Group means, and the pooled variance sum (n_i - 1) s_i^2 / sum (n_i - 1) with its degrees of freedom */
|
|
100
|
+
function pooled(d) {
|
|
101
|
+
let ss = 0;
|
|
102
|
+
let df = 0;
|
|
103
|
+
for (const grp of d.groups) {
|
|
104
|
+
if (grp.length < 2)
|
|
105
|
+
continue;
|
|
106
|
+
ss += (grp.length - 1) * variance(grp);
|
|
107
|
+
df += grp.length - 1;
|
|
108
|
+
}
|
|
109
|
+
return { m: d.groups.map(mean), s2: ss / df, df: d.N - d.k };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Dunn's all-pairs test of mean ranks with adjusted p-values. Equivalent to PMCMRplus's `kwAllPairsDunnTest()`.
|
|
113
|
+
*
|
|
114
|
+
* @param x Response values.
|
|
115
|
+
* @param g Group labels, parallel to `x` (groups in the order of the sorted labels).
|
|
116
|
+
*/
|
|
117
|
+
export function kwAllPairsDunnTest(x, g, options = {}) {
|
|
118
|
+
const adjust = options.pAdjustMethod ?? "holm";
|
|
119
|
+
const d = layout(x, g);
|
|
120
|
+
const { R, C } = meanRanks(d);
|
|
121
|
+
const v = (d.N * (d.N + 1)) / 12 - C;
|
|
122
|
+
const z = lowerTable(d.k, (i, j) => Math.abs(R[i] - R[j]) / Math.sqrt(v * (1 / d.n[i] + 1 / d.n[j])));
|
|
123
|
+
const p = adjustLower(z.map((row) => row.map((s) => 2 * upperNormal(s))), adjust);
|
|
124
|
+
return allPairs(d, "Dunn's all-pairs test", z, p, adjust, C !== 0 ? [TIES_WARNING] : []);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Dunn's many-to-one test of mean ranks against the control (the first group). Equivalent to PMCMRplus's
|
|
128
|
+
* `kwManyOneDunnTest()`. With `pAdjustMethod` `"single-step"` (the default) the p-values are the exact simultaneous
|
|
129
|
+
* normal probabilities (R approximates them by randomised integration).
|
|
130
|
+
*/
|
|
131
|
+
export function kwManyOneDunnTest(x, g, options = {}) {
|
|
132
|
+
const alternative = matchAlternative(options.alternative);
|
|
133
|
+
const adjust = options.pAdjustMethod ?? "single-step";
|
|
134
|
+
const d = layout(x, g);
|
|
135
|
+
const { R, C } = meanRanks(d);
|
|
136
|
+
const v = (d.N * (d.N + 1)) / 12 - C;
|
|
137
|
+
const idx = d.n.slice(1).map((_, i) => i + 1);
|
|
138
|
+
const z = idx.map((j) => (R[j] - R[0]) / Math.sqrt(v * (1 / d.n[0] + 1 / d.n[j])));
|
|
139
|
+
const lambda = idx.map((j) => d.n[j] / (d.n[j] + d.n[0]));
|
|
140
|
+
const p = manyOneNormalP(z, lambda, alternative, adjust);
|
|
141
|
+
return manyOne(d, "Dunn's many-to-one test", z, p, adjust, C !== 0 ? [TIES_WARNING] : []);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Many-to-one p-values of normal statistics z (one per group against the control): `"single-step"`, the exact
|
|
145
|
+
* simultaneous normal probabilities for the correlations sqrt(lambda_i lambda_j), or the marginal normal tails
|
|
146
|
+
* adjusted by `pAdjust`.
|
|
147
|
+
*/
|
|
148
|
+
function manyOneNormalP(z, lambda, alternative, adjust) {
|
|
149
|
+
if (adjust === "single-step") {
|
|
150
|
+
return z.map((s) => alternative === "two.sided"
|
|
151
|
+
? pDunnettUpper(s, lambda, Infinity, true)
|
|
152
|
+
: pDunnettUpper(alternative === "greater" ? s : -s, lambda, Infinity, false));
|
|
153
|
+
}
|
|
154
|
+
return pAdjust(z.map((s) => (alternative === "two.sided" ? 2 * upperNormal(Math.abs(s)) : upperNormal(alternative === "greater" ? s : -s))), adjust);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Dunnett's many-to-one test of means against the control (the first group), with the pooled variance. Equivalent to
|
|
158
|
+
* PMCMRplus's `dunnettTest()`. The p-values are the exact multivariate t probabilities (R approximates them by
|
|
159
|
+
* randomised integration); two-sided, as in PMCMRplus, `min(1, 2 P(max T_i >= |t|))`.
|
|
160
|
+
*/
|
|
161
|
+
export function dunnettTest(x, g, options = {}) {
|
|
162
|
+
const alternative = matchAlternative(options.alternative);
|
|
163
|
+
const d = layout(x, g);
|
|
164
|
+
const { m, s2, df } = pooled(d);
|
|
165
|
+
const s = Math.sqrt(s2);
|
|
166
|
+
const idx = d.n.slice(1).map((_, i) => i + 1);
|
|
167
|
+
const t = idx.map((j) => (m[j] - m[0]) / (s * Math.sqrt(1 / d.n[j] + 1 / d.n[0])));
|
|
168
|
+
const lambda = idx.map((j) => d.n[j] / (d.n[j] + d.n[0]));
|
|
169
|
+
const p = t.map((v) => alternative === "two.sided"
|
|
170
|
+
? Math.min(1, 2 * pDunnettUpper(Math.abs(v), lambda, df, false))
|
|
171
|
+
: pDunnettUpper(alternative === "greater" ? v : -v, lambda, df, false));
|
|
172
|
+
return manyOne(d, "Dunnett's-test for multiple\t\n comparisons with one control", t, p, "single-step");
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Steel's many-to-one rank test in its normal approximation: the Mann-Whitney statistic of each group against the
|
|
176
|
+
* control (the first group), each pair ranked on its own (midranks), standardised with the tie-corrected variance.
|
|
177
|
+
* Equivalent to PMCMRplus's `manyOneUTest()`. The statistic is positive when the group ranks higher than the
|
|
178
|
+
* control. With `pAdjustMethod` `"single-step"` (the default; at least three groups) the p-values are the exact
|
|
179
|
+
* simultaneous normal probabilities (R approximates them by randomised integration).
|
|
180
|
+
*
|
|
181
|
+
* @param x Response values.
|
|
182
|
+
* @param g Group labels, parallel to `x` (groups in the order of the sorted labels).
|
|
183
|
+
*/
|
|
184
|
+
export function manyOneUTest(x, g, options = {}) {
|
|
185
|
+
const alternative = matchAlternative(options.alternative);
|
|
186
|
+
const adjust = options.pAdjustMethod ?? "single-step";
|
|
187
|
+
const d = layout(x, g);
|
|
188
|
+
if (adjust === "single-step" && d.k < 3) {
|
|
189
|
+
throw new RangeError("for 'p.adjust.method = single-step' at least\n\n k = 3 groups are required.");
|
|
190
|
+
}
|
|
191
|
+
const n1 = d.n[0];
|
|
192
|
+
const idx = d.n.slice(1).map((_, i) => i + 1);
|
|
193
|
+
const z = idx.map((j) => {
|
|
194
|
+
const nj = d.n[j];
|
|
195
|
+
const pair = [...d.groups[0], ...d.groups[j]];
|
|
196
|
+
const r = rank(pair);
|
|
197
|
+
let rankSumControl = 0;
|
|
198
|
+
for (let a = 0; a < n1; a++)
|
|
199
|
+
rankSumControl += r[a];
|
|
200
|
+
const S = n1 + nj;
|
|
201
|
+
const rankSumGroup = (S * (S + 1)) / 2 - rankSumControl;
|
|
202
|
+
const uControl = n1 * nj + (n1 * (n1 + 1)) / 2 - rankSumControl;
|
|
203
|
+
const uGroup = n1 * nj + (nj * (nj + 1)) / 2 - rankSumGroup;
|
|
204
|
+
let t3 = 0;
|
|
205
|
+
for (const t of tieCounts(pair))
|
|
206
|
+
t3 += t * t * t - t;
|
|
207
|
+
const v = ((n1 * nj) / (S * (S - 1))) * ((S * S * S - S) / 12 - t3 / 12);
|
|
208
|
+
const stat = (Math.min(uControl, uGroup) - (n1 * nj) / 2) / Math.sqrt(v);
|
|
209
|
+
return rankSumControl < rankSumGroup ? -stat : stat;
|
|
210
|
+
});
|
|
211
|
+
const lambda = idx.map((j) => d.n[j] / (d.n[j] + n1));
|
|
212
|
+
const p = manyOneNormalP(z, lambda, alternative, adjust);
|
|
213
|
+
return manyOne(d, "Wilcoxon, Mann, Whittney U-test\n\tfor multiple comparisons with one control", z, p, adjust);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Dwass-Steel-Critchlow-Fligner all-pairs test of pairwise Mann-Whitney statistics. Equivalent to PMCMRplus's
|
|
217
|
+
* `dscfAllPairsTest()`. The statistic, sqrt(2) (U - n_i n_j / 2) / sqrt(Var U) with the smaller U, is never positive.
|
|
218
|
+
*/
|
|
219
|
+
export function dscfAllPairsTest(x, g) {
|
|
220
|
+
const d = layout(x, g);
|
|
221
|
+
const stat = lowerTable(d.k, (i, j) => {
|
|
222
|
+
const ni = d.n[i];
|
|
223
|
+
const nj = d.n[j];
|
|
224
|
+
const both = [...d.groups[i], ...d.groups[j]];
|
|
225
|
+
const r = rank(both);
|
|
226
|
+
let ri = 0;
|
|
227
|
+
for (let a = 0; a < ni; a++)
|
|
228
|
+
ri += r[a];
|
|
229
|
+
const ui = ri - (ni * (ni + 1)) / 2;
|
|
230
|
+
const u = Math.min(ui, ni * nj - ui);
|
|
231
|
+
const S = ni + nj;
|
|
232
|
+
let t3 = 0;
|
|
233
|
+
for (const t of tieCounts(both))
|
|
234
|
+
t3 += t * t * t - t;
|
|
235
|
+
const v = ((ni * nj) / (S * (S - 1))) * ((S * S * S - S) / 12 - t3 / 12);
|
|
236
|
+
return (Math.SQRT2 * (u - (ni * nj) / 2)) / Math.sqrt(v);
|
|
237
|
+
});
|
|
238
|
+
const p = stat.map((row) => row.map((q) => (Number.isNaN(q) ? NaN : upperRange(q, d.k, Infinity))));
|
|
239
|
+
return allPairs(d, "Dwass-Steele-Critchlow-Fligner all-pairs test", stat, p, "single-step");
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Games-Howell all-pairs test of means with Welch's standard errors and degrees of freedom. Equivalent to
|
|
243
|
+
* PMCMRplus's `gamesHowellTest()`.
|
|
244
|
+
*/
|
|
245
|
+
export function gamesHowellTest(x, g) {
|
|
246
|
+
const d = layout(x, g);
|
|
247
|
+
const m = d.groups.map(mean);
|
|
248
|
+
const w = d.groups.map((grp, i) => variance(grp) / d.n[i]);
|
|
249
|
+
const q = lowerTable(d.k, (i, j) => ((m[i] - m[j]) / Math.sqrt(w[i] + w[j])) * Math.SQRT2);
|
|
250
|
+
const p = lowerTable(d.k, (i, j) => {
|
|
251
|
+
const df = (w[i] + w[j]) ** 2 / (w[i] ** 2 / (d.n[i] - 1) + w[j] ** 2 / (d.n[j] - 1));
|
|
252
|
+
return upperRange(q[i - 1][j], d.k, df);
|
|
253
|
+
});
|
|
254
|
+
return allPairs(d, "Games-Howell test", q, p, "none");
|
|
255
|
+
}
|
|
256
|
+
/** Tukey's all-pairs test of means (Tukey-Kramer for unequal sizes). Equivalent to PMCMRplus's `tukeyTest()`. */
|
|
257
|
+
export function tukeyTest(x, g) {
|
|
258
|
+
const d = layout(x, g);
|
|
259
|
+
const { m, s2, df } = pooled(d);
|
|
260
|
+
const q = lowerTable(d.k, (i, j) => (m[i] - m[j]) / Math.sqrt((s2 * (1 / d.n[i] + 1 / d.n[j])) / 2));
|
|
261
|
+
const p = q.map((row) => row.map((v) => (Number.isNaN(v) ? NaN : upperRange(v, d.k, df))));
|
|
262
|
+
return allPairs(d, "Tukey's test", q, p, "single-step");
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Steel's many-to-one rank test, as PMCMRplus 1.9.12's `steelTest()` behaves: it fails on every input, so this
|
|
266
|
+
* reproduces its errors. A two-sided alternative is rejected (`'arg' should be one of “greater”, “less”`), unequal
|
|
267
|
+
* group sizes give `Steel's test is only valid for balanced designs.`, and balanced data the error PMCMRplus raises
|
|
268
|
+
* internally.
|
|
269
|
+
*
|
|
270
|
+
* @throws Always.
|
|
271
|
+
*/
|
|
272
|
+
export function steelTest(x, g, options = {}) {
|
|
273
|
+
const alternative = options.alternative === undefined ? "greater" : matchAlternative(options.alternative);
|
|
274
|
+
if (alternative === "two.sided")
|
|
275
|
+
throw new RangeError("'arg' should be one of “greater”, “less”");
|
|
276
|
+
const d = layout(x, g);
|
|
277
|
+
if (d.n.some((v) => v !== d.n[0]))
|
|
278
|
+
throw new RangeError("Steel's test is only valid for balanced designs.");
|
|
279
|
+
throw new Error("'data' must be of a vector type, was 'NULL'");
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Dunn's test of all pairs of mean ranks. Equivalent to dunn.test's `dunn.test(x, g, method, altp)`. The step-wise
|
|
283
|
+
* adjustments multiply (or transform) each p-value by the factor of its rank and cap it at 1, without enforcing
|
|
284
|
+
* monotonicity, as dunn.test does (unlike `pAdjust`).
|
|
285
|
+
*/
|
|
286
|
+
export function dunnTest(x, g, options = {}) {
|
|
287
|
+
const method = options.method ?? "none";
|
|
288
|
+
const altp = options.altp ?? false;
|
|
289
|
+
const d = layout(x, g);
|
|
290
|
+
const chi2 = kruskalTest(x, g).statistic;
|
|
291
|
+
const { R, C } = meanRanks(d);
|
|
292
|
+
const v = (d.N * (d.N + 1)) / 12 - C;
|
|
293
|
+
const Z = [];
|
|
294
|
+
const comparisons = [];
|
|
295
|
+
for (let i = 1; i < d.k; i++) {
|
|
296
|
+
for (let j = 0; j < i; j++) {
|
|
297
|
+
Z.push((R[j] - R[i]) / Math.sqrt(v * (1 / d.n[j] + 1 / d.n[i])));
|
|
298
|
+
comparisons.push(`${d.labels[j]} - ${d.labels[i]}`);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
const P = Z.map((z) => (altp ? 2 : 1) * upperNormal(Math.abs(z)));
|
|
302
|
+
return { chi2, Z, P, PAdjusted: dunnAdjust(P, method), comparisons };
|
|
303
|
+
}
|
|
304
|
+
/** dunn.test's adjustments: each value by the factor of its rank, capped at 1 */
|
|
305
|
+
function dunnAdjust(P, method) {
|
|
306
|
+
const m = P.length;
|
|
307
|
+
const sidakLike = (p, e) => Math.min(1, -Math.expm1(e * Math.log1p(-p)));
|
|
308
|
+
// i = 1..m: rank among the increasing (or decreasing) values, ties in the order of the comparisons
|
|
309
|
+
const ranks = (decreasing) => {
|
|
310
|
+
const order = P.map((_, k) => k).sort((a, b) => (decreasing ? P[b] - P[a] : P[a] - P[b]) || a - b);
|
|
311
|
+
const r = new Array(m);
|
|
312
|
+
order.forEach((k, pos) => (r[k] = pos + 1));
|
|
313
|
+
return r;
|
|
314
|
+
};
|
|
315
|
+
switch (method) {
|
|
316
|
+
case "none":
|
|
317
|
+
return P.slice();
|
|
318
|
+
case "bonferroni":
|
|
319
|
+
return P.map((p) => Math.min(1, m * p));
|
|
320
|
+
case "sidak":
|
|
321
|
+
return P.map((p) => sidakLike(p, m));
|
|
322
|
+
case "holm": {
|
|
323
|
+
const r = ranks(false);
|
|
324
|
+
return P.map((p, k) => Math.min(1, (m + 1 - r[k]) * p));
|
|
325
|
+
}
|
|
326
|
+
case "hs": {
|
|
327
|
+
const r = ranks(false);
|
|
328
|
+
return P.map((p, k) => sidakLike(p, m + 1 - r[k]));
|
|
329
|
+
}
|
|
330
|
+
case "hochberg": {
|
|
331
|
+
const r = ranks(true);
|
|
332
|
+
return P.map((p, k) => Math.min(1, r[k] * p));
|
|
333
|
+
}
|
|
334
|
+
case "bh": {
|
|
335
|
+
const r = ranks(true);
|
|
336
|
+
return P.map((p, k) => Math.min(1, (m / (m + 1 - r[k])) * p));
|
|
337
|
+
}
|
|
338
|
+
case "by": {
|
|
339
|
+
const r = ranks(true);
|
|
340
|
+
let h = 0;
|
|
341
|
+
for (let l = 1; l <= m; l++)
|
|
342
|
+
h += 1 / l;
|
|
343
|
+
return P.map((p, k) => Math.min(1, (m / (m + 1 - r[k])) * h * p));
|
|
344
|
+
}
|
|
345
|
+
default:
|
|
346
|
+
throw new RangeError(`invalid method "${String(method)}"`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Dunnett's many-to-one comparisons of a one-way ANOVA with simultaneous p-values. Equivalent to multcomp's
|
|
351
|
+
* `summary(glht(aov(y ~ g), linfct = mcp(g = "Dunnett"), alternative))`, with the exact multivariate t probabilities
|
|
352
|
+
* (R approximates them by randomised integration): two-sided `P(max |T_i| >= |t|)`, greater `P(max T_i >= t)`, less
|
|
353
|
+
* `P(min T_i <= t)`.
|
|
354
|
+
*/
|
|
355
|
+
export function glhtDunnett(x, g, options = {}) {
|
|
356
|
+
const alternative = matchAlternative(options.alternative);
|
|
357
|
+
const d = layout(x, g);
|
|
358
|
+
const { m, s2, df } = pooled(d);
|
|
359
|
+
const s = Math.sqrt(s2);
|
|
360
|
+
const idx = d.n.slice(1).map((_, i) => i + 1);
|
|
361
|
+
const estimate = idx.map((j) => m[j] - m[0]);
|
|
362
|
+
const se = idx.map((j) => s * Math.sqrt(1 / d.n[j] + 1 / d.n[0]));
|
|
363
|
+
const t = estimate.map((e, i) => e / se[i]);
|
|
364
|
+
const lambda = idx.map((j) => d.n[j] / (d.n[j] + d.n[0]));
|
|
365
|
+
const p = t.map((v) => alternative === "two.sided"
|
|
366
|
+
? pDunnettUpper(v, lambda, df, true)
|
|
367
|
+
: pDunnettUpper(alternative === "greater" ? v : -v, lambda, df, false));
|
|
368
|
+
return { names: idx.map((j) => `${d.labels[j]} - ${d.labels[0]}`), estimate, se, t, p, df };
|
|
369
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { HTest, Sample } from "./types.js";
|
|
2
|
+
/** Result of a normality test: no `alternative`, as nortest returns none. */
|
|
3
|
+
export type NormalityTestResult = Omit<HTest, "alternative">;
|
|
4
|
+
/** Result of {@link cvmTest}, with nortest's warning for a p-value below the approximation's range. */
|
|
5
|
+
export interface CvmTestResult extends NormalityTestResult {
|
|
6
|
+
warnings: string[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Anderson-Darling test for normality. Equivalent to nortest's `ad.test()`. `NaN` values are dropped; at least 8
|
|
10
|
+
* values are needed.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* adTest([2.1, 3.4, 1.9, 5.6, 4.4, 3.8, 2.9, 4.1, 3.3, 2.6]).statistic; // 0.15414195923525043
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function adTest(x: Sample): NormalityTestResult;
|
|
18
|
+
/**
|
|
19
|
+
* Cramer-von Mises test for normality. Equivalent to nortest's `cvm.test()`. `NaN` values are dropped; at least 8
|
|
20
|
+
* values are needed.
|
|
21
|
+
*/
|
|
22
|
+
export declare function cvmTest(x: Sample): CvmTestResult;
|
|
23
|
+
/**
|
|
24
|
+
* Lilliefors (Kolmogorov-Smirnov) test for normality. Equivalent to nortest's `lillie.test()`. `NaN` values are
|
|
25
|
+
* dropped; at least 5 values are needed.
|
|
26
|
+
*/
|
|
27
|
+
export declare function lillieTest(x: Sample): NormalityTestResult;
|
|
28
|
+
/**
|
|
29
|
+
* Shapiro-Francia test for normality. Equivalent to nortest's `sf.test()`. `NaN` values are dropped; the sample size
|
|
30
|
+
* must be between 5 and 5000.
|
|
31
|
+
*/
|
|
32
|
+
export declare function sfTest(x: Sample): NormalityTestResult;
|
|
33
|
+
/** Options for {@link pearsonTest}. */
|
|
34
|
+
export interface PearsonTestOptions {
|
|
35
|
+
/** Number of classes. Default `ceil(2 n^(2/5))`. */
|
|
36
|
+
nClasses?: number;
|
|
37
|
+
/** Subtract the 2 estimated parameters from the degrees of freedom. Default `true`. */
|
|
38
|
+
adjust?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Pearson chi-square test for normality, with classes of equal probability under the fitted normal distribution.
|
|
42
|
+
* Equivalent to nortest's `pearson.test()`. `NaN` values are dropped.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pearsonTest(x: Sample, options?: PearsonTestOptions): NormalityTestResult;
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Tests of normality as the nortest package computes them: Anderson-Darling (ad.test), Cramer-von Mises (cvm.test),
|
|
2
|
+
// Lilliefors' Kolmogorov-Smirnov (lillie.test), Shapiro-Francia (sf.test) and the Pearson chi-square test
|
|
3
|
+
// (pearson.test), each with the composite hypothesis (mean and standard deviation estimated).
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js from oracle/rm-tests-behaviour.md (no ported code; nortest's source was not read): the
|
|
6
|
+
// statistics from the standardised order statistics, the p-values from the closed-form approximations given there
|
|
7
|
+
// (piecewise exponentials of A and W, Dallal and Wilkinson's formula for D, a normal approximation of log(1 - W) for
|
|
8
|
+
// the Shapiro-Francia W), and the chi-square test on classes of equal probability.
|
|
9
|
+
import { pgamma1 } from "../distributions/gamma.js";
|
|
10
|
+
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
11
|
+
import { covariance, dropNaN, mean, variance } from "../internal/summary.js";
|
|
12
|
+
/** The sorted values without NaN */
|
|
13
|
+
function sorted(x) {
|
|
14
|
+
return dropNaN(Array.from(x)).sort((a, b) => a - b);
|
|
15
|
+
}
|
|
16
|
+
/** The sorted values standardised by their mean and standard deviation */
|
|
17
|
+
function standardized(xs) {
|
|
18
|
+
const m = mean(xs);
|
|
19
|
+
const s = Math.sqrt(variance(xs));
|
|
20
|
+
return xs.map((v) => (v - m) / s);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Anderson-Darling test for normality. Equivalent to nortest's `ad.test()`. `NaN` values are dropped; at least 8
|
|
24
|
+
* values are needed.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* adTest([2.1, 3.4, 1.9, 5.6, 4.4, 3.8, 2.9, 4.1, 3.3, 2.6]).statistic; // 0.15414195923525043
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function adTest(x) {
|
|
32
|
+
const xs = sorted(x);
|
|
33
|
+
const n = xs.length;
|
|
34
|
+
if (n < 8)
|
|
35
|
+
throw new RangeError("sample size must be greater than 7");
|
|
36
|
+
const z = standardized(xs);
|
|
37
|
+
let s = 0;
|
|
38
|
+
for (let i = 1; i <= n; i++) {
|
|
39
|
+
s += (2 * i - 1) * (pnorm(z[i - 1], { logP: true }) + pnorm(z[n - i], { lowerTail: false, logP: true }));
|
|
40
|
+
}
|
|
41
|
+
const a = -n - s / n;
|
|
42
|
+
const aa = (1 + 0.75 / n + 2.25 / n ** 2) * a;
|
|
43
|
+
let pValue;
|
|
44
|
+
if (aa < 0.2)
|
|
45
|
+
pValue = 1 - Math.exp(-13.436 + 101.14 * aa - 223.73 * aa ** 2);
|
|
46
|
+
else if (aa < 0.34)
|
|
47
|
+
pValue = 1 - Math.exp(-8.318 + 42.796 * aa - 59.938 * aa ** 2);
|
|
48
|
+
else if (aa < 0.6)
|
|
49
|
+
pValue = Math.exp(0.9177 - 4.279 * aa - 1.38 * aa ** 2);
|
|
50
|
+
else if (aa < 10)
|
|
51
|
+
pValue = Math.exp(1.2937 - 5.709 * aa + 0.0186 * aa ** 2);
|
|
52
|
+
else
|
|
53
|
+
pValue = 3.7e-24;
|
|
54
|
+
return { method: "Anderson-Darling normality test", statistic: a, statisticName: "A", pValue };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Cramer-von Mises test for normality. Equivalent to nortest's `cvm.test()`. `NaN` values are dropped; at least 8
|
|
58
|
+
* values are needed.
|
|
59
|
+
*/
|
|
60
|
+
export function cvmTest(x) {
|
|
61
|
+
const xs = sorted(x);
|
|
62
|
+
const n = xs.length;
|
|
63
|
+
if (n < 8)
|
|
64
|
+
throw new RangeError("sample size must be greater than 7");
|
|
65
|
+
const z = standardized(xs);
|
|
66
|
+
let w = 1 / (12 * n);
|
|
67
|
+
z.forEach((zi, k) => {
|
|
68
|
+
w += (pnorm(zi) - (2 * (k + 1) - 1) / (2 * n)) ** 2;
|
|
69
|
+
});
|
|
70
|
+
const ww = (1 + 0.5 / n) * w;
|
|
71
|
+
const warnings = [];
|
|
72
|
+
let pValue;
|
|
73
|
+
if (ww < 0.0275)
|
|
74
|
+
pValue = 1 - Math.exp(-13.953 + 775.5 * ww - 12542.61 * ww ** 2);
|
|
75
|
+
else if (ww < 0.051)
|
|
76
|
+
pValue = 1 - Math.exp(-5.903 + 179.546 * ww - 1515.29 * ww ** 2);
|
|
77
|
+
else if (ww < 0.092)
|
|
78
|
+
pValue = Math.exp(0.886 - 31.62 * ww + 10.897 * ww ** 2);
|
|
79
|
+
else if (ww < 1.1)
|
|
80
|
+
pValue = Math.exp(1.111 - 34.242 * ww + 12.832 * ww ** 2);
|
|
81
|
+
else {
|
|
82
|
+
pValue = 7.37e-10;
|
|
83
|
+
warnings.push("p-value is smaller than 7.37e-10, cannot be computed more accurately");
|
|
84
|
+
}
|
|
85
|
+
return { method: "Cramer-von Mises normality test", statistic: w, statisticName: "W", pValue, warnings };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Lilliefors (Kolmogorov-Smirnov) test for normality. Equivalent to nortest's `lillie.test()`. `NaN` values are
|
|
89
|
+
* dropped; at least 5 values are needed.
|
|
90
|
+
*/
|
|
91
|
+
export function lillieTest(x) {
|
|
92
|
+
const xs = sorted(x);
|
|
93
|
+
const n = xs.length;
|
|
94
|
+
if (n < 5)
|
|
95
|
+
throw new RangeError("sample size must be greater than 4");
|
|
96
|
+
const p = standardized(xs).map((v) => pnorm(v));
|
|
97
|
+
let dPlus = -Infinity;
|
|
98
|
+
let dMinus = -Infinity;
|
|
99
|
+
p.forEach((pi, k) => {
|
|
100
|
+
dPlus = Math.max(dPlus, (k + 1) / n - pi);
|
|
101
|
+
dMinus = Math.max(dMinus, pi - k / n);
|
|
102
|
+
});
|
|
103
|
+
const d = Math.max(dPlus, dMinus);
|
|
104
|
+
const kd = n <= 100 ? d : d * (n / 100) ** 0.49;
|
|
105
|
+
const nd = n <= 100 ? n : 100;
|
|
106
|
+
let pValue = Math.exp(-7.01256 * kd ** 2 * (nd + 2.78019) + 2.99587 * kd * Math.sqrt(nd + 2.78019) - 0.122119 + 0.974598 / Math.sqrt(nd) + 1.67997 / nd);
|
|
107
|
+
if (pValue > 0.1) {
|
|
108
|
+
const kk = (Math.sqrt(n) - 0.01 + 0.85 / Math.sqrt(n)) * d;
|
|
109
|
+
if (kk <= 0.302)
|
|
110
|
+
pValue = 1;
|
|
111
|
+
else if (kk <= 0.5)
|
|
112
|
+
pValue = 2.76773 - 19.828315 * kk + 80.709644 * kk ** 2 - 138.55152 * kk ** 3 + 81.218052 * kk ** 4;
|
|
113
|
+
else if (kk <= 0.9)
|
|
114
|
+
pValue = -4.901232 + 40.662806 * kk - 97.490286 * kk ** 2 + 94.029866 * kk ** 3 - 32.355711 * kk ** 4;
|
|
115
|
+
else if (kk <= 1.31)
|
|
116
|
+
pValue = 6.198765 - 19.558097 * kk + 23.186922 * kk ** 2 - 12.234627 * kk ** 3 + 2.423045 * kk ** 4;
|
|
117
|
+
else
|
|
118
|
+
pValue = 0;
|
|
119
|
+
}
|
|
120
|
+
return { method: "Lilliefors (Kolmogorov-Smirnov) normality test", statistic: d, statisticName: "D", pValue };
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Shapiro-Francia test for normality. Equivalent to nortest's `sf.test()`. `NaN` values are dropped; the sample size
|
|
124
|
+
* must be between 5 and 5000.
|
|
125
|
+
*/
|
|
126
|
+
export function sfTest(x) {
|
|
127
|
+
const xs = sorted(x);
|
|
128
|
+
const n = xs.length;
|
|
129
|
+
if (n < 5 || n > 5000)
|
|
130
|
+
throw new RangeError("sample size must be between 5 and 5000");
|
|
131
|
+
const q = xs.map((_, k) => qnorm((k + 1 - 3 / 8) / (n + 1 / 4)));
|
|
132
|
+
const w = covariance(xs, q) ** 2 / (variance(xs) * variance(q));
|
|
133
|
+
const u = Math.log(n);
|
|
134
|
+
const v = Math.log(u);
|
|
135
|
+
const mu = -1.2725 + 1.0521 * (v - u);
|
|
136
|
+
const sig = 1.0308 - 0.26758 * (v + 2 / u);
|
|
137
|
+
const pValue = pnorm((Math.log(1 - w) - mu) / sig, { lowerTail: false });
|
|
138
|
+
return { method: "Shapiro-Francia normality test", statistic: w, statisticName: "W", pValue };
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Pearson chi-square test for normality, with classes of equal probability under the fitted normal distribution.
|
|
142
|
+
* Equivalent to nortest's `pearson.test()`. `NaN` values are dropped.
|
|
143
|
+
*/
|
|
144
|
+
export function pearsonTest(x, options = {}) {
|
|
145
|
+
const xs = sorted(x);
|
|
146
|
+
const n = xs.length;
|
|
147
|
+
const k = options.nClasses ?? Math.ceil(2 * n ** (2 / 5));
|
|
148
|
+
const adjust = options.adjust ?? true;
|
|
149
|
+
const counts = new Array(k).fill(0);
|
|
150
|
+
for (const z of standardized(xs)) {
|
|
151
|
+
const c = Math.floor(1 + k * pnorm(z));
|
|
152
|
+
if (c >= 1 && c <= k)
|
|
153
|
+
counts[c - 1]++;
|
|
154
|
+
}
|
|
155
|
+
const e = n / k;
|
|
156
|
+
const statistic = counts.reduce((a, c) => a + (c - e) ** 2 / e, 0);
|
|
157
|
+
const df = k - 1 - (adjust ? 2 : 0);
|
|
158
|
+
return {
|
|
159
|
+
method: "Pearson chi-square normality test",
|
|
160
|
+
statistic,
|
|
161
|
+
statisticName: "P",
|
|
162
|
+
pValue: pgamma1(statistic, df / 2, 2, false, false),
|
|
163
|
+
};
|
|
164
|
+
}
|