@numero-uno-jpn/rstats 1.0.0 → 1.1.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 +40 -0
- package/README.ja.md +8 -8
- package/README.md +8 -8
- package/THIRD_PARTY_NOTICES.md +3 -2
- package/dist/chunks/{chunk-WQWZ7FND.js → chunk-4KCSBTR5.js} +2 -2
- package/dist/chunks/{chunk-IEOD5GWC.js → chunk-4VRMAYPY.js} +2 -2
- package/dist/chunks/{chunk-BQSIBJVX.js → chunk-7IJCUSA5.js} +2 -2
- package/dist/chunks/{chunk-WIBKS73G.js → chunk-7KJ24LYK.js} +2 -2
- package/dist/chunks/{chunk-3W5NFFAA.js → chunk-DR4GQ7PW.js} +1 -1
- package/dist/chunks/{chunk-H7I32ZZE.js → chunk-DTJG2IL2.js} +2 -2
- package/dist/chunks/{chunk-BWKHYRM6.js → chunk-EJYCDE4Z.js} +1 -1
- package/dist/chunks/{chunk-YG5S5AVP.js → chunk-GGJA3JRO.js} +2 -2
- package/dist/chunks/{chunk-4OLDGML4.js → chunk-GJEKNJP7.js} +2 -2
- package/dist/chunks/{chunk-4F2IALD4.js → chunk-GRUQRNX7.js} +2 -2
- package/dist/chunks/chunk-GYGQUYEP.js +2 -0
- package/dist/chunks/{chunk-O2SQMGE3.js → chunk-HRA7PCQT.js} +2 -2
- package/dist/chunks/{chunk-63OPBEYO.js → chunk-HUWOSLKE.js} +1 -1
- package/dist/chunks/{chunk-6XJBCRBV.js → chunk-HZLSNJQM.js} +2 -2
- package/dist/chunks/{chunk-QZK3CCBA.js → chunk-JNWWLNE3.js} +2 -2
- package/dist/chunks/{chunk-NSWENS3S.js → chunk-N5BDSMXS.js} +1 -1
- package/dist/chunks/{chunk-WWVMVZUL.js → chunk-NDTJ23A6.js} +2 -2
- package/dist/chunks/{chunk-CKV4JWW2.js → chunk-OG7UORIX.js} +1 -1
- package/dist/chunks/{chunk-UKNDX4BA.js → chunk-QUTJEYDX.js} +2 -2
- package/dist/chunks/{chunk-LROMWFAK.js → chunk-U7BRXOUS.js} +2 -2
- package/dist/chunks/{chunk-QQX46TCI.js → chunk-UMLMY4XS.js} +2 -2
- package/dist/chunks/{chunk-UJ66VQIG.js → chunk-X4J6UJHN.js} +2 -2
- package/dist/chunks/{chunk-YHNMBYSW.js → chunk-Y4YWLSWU.js} +2 -2
- package/dist/chunks/{chunk-F7YGX7PN.js → chunk-YN5MLKDL.js} +2 -2
- package/dist/chunks/{chunk-DCAWMF4K.js → chunk-Z2HGLFZN.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions/signrank.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +1 -1
- package/dist/htest/ansariTest.js +32 -34
- package/dist/htest/cor.d.ts +2 -2
- package/dist/htest/cor.js +2 -1
- package/dist/htest/core/scoreDistribution.d.ts +39 -0
- package/dist/htest/core/scoreDistribution.js +151 -0
- package/dist/htest/wilcoxTest.d.ts +23 -8
- package/dist/htest/wilcoxTest.js +521 -199
- package/dist/linalg/chol.d.ts +3 -3
- package/dist/linalg/chol.js +10 -2
- package/dist/linalg/qr.js +39 -15
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +40 -5
- package/dist/models/aovStrata.js +21 -4
- package/dist/models/glm.d.ts +15 -4
- package/dist/models/glm.js +13 -6
- package/dist/models/survival.d.ts +4 -1
- package/dist/models/survival.js +7 -1
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +16 -16
- package/dist/special.js +2 -2
- package/dist/tests.js +13 -13
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/dist/chunks/chunk-KICBQGFN.js +0 -2
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Exact permutation distributions of sums of scores, for the conditional (tied or Pratt) exact Wilcoxon tests of
|
|
2
|
+
// wilcoxTest.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js from the definitions (the conditional distributions that Streitberg and Röhmel's shift
|
|
5
|
+
// algorithm computes, JASA 81, 1986; no ported code). The scores are mid-ranks, multiples of 1/2; with f = 1 when all
|
|
6
|
+
// are integers and f = 2 otherwise, the integer scores a_j = f score_j index the table.
|
|
7
|
+
// - Signed rank: each score is counted with probability 1/2, independently. The probability function of the sum is
|
|
8
|
+
// the coefficient list of prod_j (1 + t^{a_j}) / 2, built one factor at a time with the halving in each step, so
|
|
9
|
+
// that the entries stay probabilities (2^-k for k scores, exact for dyadic cases) and nothing overflows.
|
|
10
|
+
// - Rank sum: the m positions of the first sample are a uniformly random m-subset of the N scores. After the first i
|
|
11
|
+
// scores (ascending), P_i(j, s), the probability that a random j-subset of them sums to s, follows
|
|
12
|
+
// P_i(j, s) = (j / i) P_{i-1}(j - 1, s - a_i) + ((i - j) / i) P_{i-1}(j, s): convex combinations of probabilities,
|
|
13
|
+
// so no counts (C(100, 50) = 1e29) and no cancellation. Only k = min(m, N - m) rows are needed (the other sample's
|
|
14
|
+
// sum is the total minus this one), and a row j only while it can still reach k.
|
|
15
|
+
// Tails are cumulative sums of positive terms from either end, so small upper tails keep their digits (R's 1 - lower
|
|
16
|
+
// tail elsewhere).
|
|
17
|
+
/** Largest table (doubles in one array; CONTRIBUTING rule 4) */
|
|
18
|
+
export const MAX_SCORE_TABLE = 2 ** 24;
|
|
19
|
+
/** f and the integer scores f score_j, ascending */
|
|
20
|
+
function integerScores(scores) {
|
|
21
|
+
const f = scores.every((s) => Number.isInteger(s)) ? 1 : 2;
|
|
22
|
+
return [f, scores.map((s) => s * f).sort((a, b) => a - b)];
|
|
23
|
+
}
|
|
24
|
+
/** Key of the multiset of scores (for caching distributions) */
|
|
25
|
+
export function scoreKey(scores) {
|
|
26
|
+
return [...scores].sort((a, b) => a - b).join(",");
|
|
27
|
+
}
|
|
28
|
+
function withTails(f, offset, p) {
|
|
29
|
+
const len = p.length;
|
|
30
|
+
const lower = new Float64Array(len);
|
|
31
|
+
const upper = new Float64Array(len);
|
|
32
|
+
let s = 0;
|
|
33
|
+
for (let i = 0; i < len; i++)
|
|
34
|
+
lower[i] = s += p[i];
|
|
35
|
+
s = 0;
|
|
36
|
+
for (let i = len - 1; i >= 0; i--)
|
|
37
|
+
upper[i] = s += p[i];
|
|
38
|
+
return { f, offset, lower, upper };
|
|
39
|
+
}
|
|
40
|
+
/** Steps and table length of signedScoreDistribution for these scores */
|
|
41
|
+
export function signedScoreCost(scores) {
|
|
42
|
+
const [, a] = integerScores(scores);
|
|
43
|
+
let cur = 0;
|
|
44
|
+
let steps = 0;
|
|
45
|
+
for (const v of a)
|
|
46
|
+
steps += cur += v;
|
|
47
|
+
return { steps, size: cur + 1 };
|
|
48
|
+
}
|
|
49
|
+
/** Distribution of the sum of the scores with random signs (the scores counted with probability 1/2 each) */
|
|
50
|
+
export function signedScoreDistribution(scores) {
|
|
51
|
+
const [f, a] = integerScores(scores);
|
|
52
|
+
let total = 0;
|
|
53
|
+
for (const v of a)
|
|
54
|
+
total += v;
|
|
55
|
+
const p = new Float64Array(total + 1);
|
|
56
|
+
p[0] = 1;
|
|
57
|
+
let cur = 0;
|
|
58
|
+
for (const v of a) {
|
|
59
|
+
cur += v;
|
|
60
|
+
for (let s = cur; s >= v; s--)
|
|
61
|
+
p[s] = 0.5 * (p[s] + p[s - v]);
|
|
62
|
+
for (let s = v - 1; s >= 0; s--)
|
|
63
|
+
p[s] = 0.5 * p[s];
|
|
64
|
+
}
|
|
65
|
+
return withTails(f, 0, p);
|
|
66
|
+
}
|
|
67
|
+
/** Steps and largest row of rankSumScoreDistribution for these scores and first-sample size m */
|
|
68
|
+
export function rankSumScoreCost(scores, m) {
|
|
69
|
+
const [, a] = integerScores(scores);
|
|
70
|
+
const N = a.length;
|
|
71
|
+
const k = Math.min(m, N - m);
|
|
72
|
+
const pre = prefix(a);
|
|
73
|
+
let steps = 0;
|
|
74
|
+
for (let i = 1; i <= N; i++) {
|
|
75
|
+
for (let j = Math.max(1, k - (N - i)); j <= Math.min(i, k); j++)
|
|
76
|
+
steps += pre[i] - pre[i - j] - pre[j] + 1;
|
|
77
|
+
}
|
|
78
|
+
return { steps, size: pre[N] - pre[N - k] - pre[k] + 1 };
|
|
79
|
+
}
|
|
80
|
+
function prefix(a) {
|
|
81
|
+
const pre = [0];
|
|
82
|
+
for (const v of a)
|
|
83
|
+
pre.push(pre[pre.length - 1] + v);
|
|
84
|
+
return pre;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Distribution of W = (sum of the scores of a random m-subset) - m (m + 1) / 2, the rank sum statistic given the
|
|
88
|
+
* pooled scores
|
|
89
|
+
*/
|
|
90
|
+
export function rankSumScoreDistribution(scores, m) {
|
|
91
|
+
const [f, a] = integerScores(scores);
|
|
92
|
+
const N = a.length;
|
|
93
|
+
const k = Math.min(m, N - m);
|
|
94
|
+
const pre = prefix(a);
|
|
95
|
+
// row j: sums of j-subsets, from pre[j] (the j smallest) to the sum of the j largest
|
|
96
|
+
const rows = [];
|
|
97
|
+
for (let j = 0; j <= k; j++)
|
|
98
|
+
rows.push(new Float64Array(pre[N] - pre[N - j] - pre[j] + 1));
|
|
99
|
+
rows[0][0] = 1;
|
|
100
|
+
for (let i = 1; i <= N; i++) {
|
|
101
|
+
const ai = a[i - 1];
|
|
102
|
+
for (let j = Math.min(i, k); j >= Math.max(1, k - (N - i)); j--) {
|
|
103
|
+
const row = rows[j];
|
|
104
|
+
const prev = rows[j - 1];
|
|
105
|
+
const wNew = j / i;
|
|
106
|
+
const wOld = (i - j) / i;
|
|
107
|
+
const lo = pre[j];
|
|
108
|
+
const hi = pre[i] - pre[i - j];
|
|
109
|
+
// P_{i-1}(j - 1, .) lives on [pre[j - 1], pre[i - 1] - pre[i - j]], so s - a_i on [pre[j - 1] + a_i, hi]
|
|
110
|
+
const shift = pre[j - 1] + ai;
|
|
111
|
+
const from = Math.max(lo, shift);
|
|
112
|
+
for (let s = hi; s >= from; s--)
|
|
113
|
+
row[s - lo] = wOld * row[s - lo] + wNew * prev[s - shift];
|
|
114
|
+
for (let s = from - 1; s >= lo; s--)
|
|
115
|
+
row[s - lo] = wOld * row[s - lo];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const row = rows[k];
|
|
119
|
+
const base = (m * (m + 1)) / 2;
|
|
120
|
+
if (k === m)
|
|
121
|
+
return withTails(f, pre[k] / f - base, row);
|
|
122
|
+
// the subsets were of the other sample: the first sample's sum is the total minus theirs (reversed)
|
|
123
|
+
const len = row.length;
|
|
124
|
+
const rev = new Float64Array(len);
|
|
125
|
+
for (let i = 0; i < len; i++)
|
|
126
|
+
rev[i] = row[len - 1 - i];
|
|
127
|
+
const hiK = pre[N] - pre[N - k];
|
|
128
|
+
return withTails(f, (pre[N] - hiK) / f - base, rev);
|
|
129
|
+
}
|
|
130
|
+
/** P(S < q + 1e-8), at most 1 (R's tail convention for the conditional distributions) */
|
|
131
|
+
export function lowerTail(d, q) {
|
|
132
|
+
const last = Math.ceil(d.f * (q - d.offset) + d.f * 1e-8) - 1;
|
|
133
|
+
if (last < 0)
|
|
134
|
+
return 0;
|
|
135
|
+
if (last >= d.lower.length)
|
|
136
|
+
return 1;
|
|
137
|
+
return Math.min(1, d.lower[last]);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* P(S >= q + 1e-8): as R 4.6.2, 1 - P(S < q + 1e-8), except within 1/64 of the upper end of the lower tail (R's
|
|
141
|
+
* absolute threshold), where it is summed directly and keeps the digits of a small upper tail
|
|
142
|
+
*/
|
|
143
|
+
export function upperTail(d, q) {
|
|
144
|
+
const L = lowerTail(d, q);
|
|
145
|
+
if (Math.abs(L - 1) >= 1 / 64)
|
|
146
|
+
return 1 - L;
|
|
147
|
+
const first = Math.ceil(d.f * (q - d.offset) + d.f * 1e-8);
|
|
148
|
+
if (first >= d.upper.length)
|
|
149
|
+
return 0;
|
|
150
|
+
return Math.min(1, d.upper[Math.max(0, first)]);
|
|
151
|
+
}
|
|
@@ -16,13 +16,26 @@ export interface WilcoxTestOptions {
|
|
|
16
16
|
/** Paired test (signed rank test on `x - y`). Requires `y`. Default `false`. */
|
|
17
17
|
paired?: boolean;
|
|
18
18
|
/**
|
|
19
|
-
* Compute an exact p-value. Default `null`: exact when the sample size is below 50 (both
|
|
20
|
-
*
|
|
21
|
-
*
|
|
19
|
+
* Compute an exact p-value (and interval). Default `null`: exact when the sample size is below 50 (both samples,
|
|
20
|
+
* for two samples; zero differences count). With ties or zero differences the exact test is the conditional
|
|
21
|
+
* (permutation) test given the mid-ranks, as in R 4.6; the signed rank test then ranks the zeros with the other
|
|
22
|
+
* differences and leaves them out of the permutation distribution (Pratt).
|
|
23
|
+
*
|
|
24
|
+
* The conditional distribution is built once for the p-value and again for each multiset of scores that the
|
|
25
|
+
* interval's search meets. Where that would take more than about 8 seconds, a RangeError suggesting `exact: false`
|
|
26
|
+
* is thrown before the computation. Measured on a Mac mini (Apple M1, Node 24) with data rounded to one or two
|
|
27
|
+
* decimals: the p-value up to about n = 2400 (one sample) and m = n = 320 (two samples); the interval up to about
|
|
28
|
+
* n = 150 to 300 (one sample; 4 s at n = 150 with 3 decimals, 4.6 s at n = 300 with 1) and m = n = 80 (two samples;
|
|
29
|
+
* 49 x 49 takes 3 s). Ties in few places (or none) cost less.
|
|
22
30
|
*/
|
|
23
31
|
exact?: boolean | null;
|
|
24
|
-
/**
|
|
25
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Continuity correction of the normal approximation. Default `true`. As in R 4.6, also `0` (the same as `true`) or
|
|
34
|
+
* `1`, `2`, `3`: the continuity correction plus that many Edgeworth terms in the p-value (only without ties and zero
|
|
35
|
+
* differences; with them, the continuity correction only). The interval and estimate use the continuity correction
|
|
36
|
+
* alone. Any other number throws.
|
|
37
|
+
*/
|
|
38
|
+
correct?: boolean | 0 | 1 | 2 | 3;
|
|
26
39
|
/** Compute a confidence interval and the Hodges-Lehmann estimate. Default `false`. */
|
|
27
40
|
confInt?: boolean;
|
|
28
41
|
/** Confidence level of the interval. Default `0.95`. */
|
|
@@ -46,9 +59,11 @@ export interface WilcoxTestResult extends HTest {
|
|
|
46
59
|
*
|
|
47
60
|
* With one sample (or `paired: true`) performs the signed rank test; with two samples the
|
|
48
61
|
* rank sum test. The choice between exact and normal-approximation p-values, the
|
|
49
|
-
* Hodges-Lehmann estimates and the confidence intervals follow R
|
|
50
|
-
*
|
|
51
|
-
* values are kept and ranked (as
|
|
62
|
+
* Hodges-Lehmann estimates and the confidence intervals follow R 4.6: with ties or zeros the
|
|
63
|
+
* exact test is the conditional test given the mid-ranks, and an exact interval reports the
|
|
64
|
+
* confidence level it achieves. Only `NaN` is dropped; infinite values are kept and ranked (as
|
|
65
|
+
* R actually does). Where R 4.6.2 is wrong, the one-sample conditional interval and small
|
|
66
|
+
* two-sided normal p-values, see docs/differences-from-r.md.
|
|
52
67
|
*
|
|
53
68
|
* @param x Sample.
|
|
54
69
|
* @param y Second sample for a two-sample or paired test. May be omitted, and the options
|