@numero-uno-jpn/rstats 1.8.0 → 1.9.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 +20 -0
- package/README.ja.md +7 -6
- package/README.md +6 -5
- package/dist/chunks/{chunk-TUL6UFLQ.js → chunk-2COJEI44.js} +2 -2
- package/dist/chunks/{chunk-YT62DNDS.js → chunk-2SO7Z3NV.js} +2 -2
- package/dist/chunks/{chunk-73IRLCTF.js → chunk-3J2HFC4A.js} +2 -2
- package/dist/chunks/chunk-3LDUKHJZ.js +3 -0
- package/dist/chunks/{chunk-YD4U3QZ3.js → chunk-4IHHZ3F4.js} +1 -1
- package/dist/chunks/chunk-4QWW62QN.js +2 -0
- package/dist/chunks/chunk-575F6RXJ.js +2 -0
- package/dist/chunks/{chunk-IJST2ZGT.js → chunk-5C7XHQKP.js} +1 -1
- package/dist/chunks/{chunk-7ODUCEGX.js → chunk-7F7PY35D.js} +2 -2
- package/dist/chunks/{chunk-RWZ6ICYN.js → chunk-7VPO2LTR.js} +2 -2
- package/dist/chunks/{chunk-45HUI3M4.js → chunk-ANGKH5D4.js} +2 -2
- package/dist/chunks/{chunk-JXJDCQYE.js → chunk-BX47HGID.js} +2 -2
- package/dist/chunks/chunk-CWS62KPG.js +2 -0
- package/dist/chunks/chunk-D3CWLSLS.js +2 -0
- package/dist/chunks/{chunk-QQHF5T2A.js → chunk-DEAU6HWA.js} +1 -1
- package/dist/chunks/chunk-DRSA7ITF.js +2 -0
- package/dist/chunks/chunk-G3XMR7PA.js +2 -0
- package/dist/chunks/chunk-I7E5J5PG.js +3 -0
- package/dist/chunks/{chunk-2ZQ2TSZS.js → chunk-IEATFACS.js} +2 -2
- package/dist/chunks/{chunk-TYMC2D5I.js → chunk-IXK3JA2R.js} +2 -2
- package/dist/chunks/{chunk-LZB43TUL.js → chunk-J3TS4PGN.js} +2 -2
- package/dist/chunks/{chunk-V7ECXPWY.js → chunk-MNIICUNF.js} +2 -2
- package/dist/chunks/chunk-MT7XELUM.js +2 -0
- package/dist/chunks/{chunk-2X6PE6JF.js → chunk-OGHJJKUR.js} +2 -2
- package/dist/chunks/{chunk-52RWDNO6.js → chunk-RNK6EHEK.js} +2 -2
- package/dist/chunks/{chunk-D3LKPD4H.js → chunk-T3NUXZRL.js} +2 -2
- package/dist/chunks/chunk-TKMNASCD.js +2 -0
- package/dist/chunks/chunk-UA66IB37.js +2 -0
- package/dist/chunks/chunk-UUZVQS4O.js +2 -0
- package/dist/chunks/chunk-VO2NIKVB.js +2 -0
- package/dist/chunks/chunk-WFE52E6F.js +2 -0
- package/dist/chunks/{chunk-GRTQ7FYQ.js → chunk-WOZEQS2I.js} +2 -2
- package/dist/chunks/{chunk-DG2RTNIR.js → chunk-XCKQQVN7.js} +2 -2
- package/dist/chunks/chunk-XNLZFKL5.js +2 -0
- package/dist/chunks/chunk-YFZBGUIT.js +2 -0
- package/dist/chunks/{chunk-Y4KNH43T.js → chunk-YK7Q6CNX.js} +2 -2
- package/dist/chunks/{chunk-JIAFWEED.js → chunk-Z47NJMYU.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/multivariate.d.ts +3 -0
- package/dist/entries/multivariate.js +4 -0
- package/dist/entries/tests.d.ts +3 -1
- package/dist/entries/tests.js +3 -1
- package/dist/htest/grubbs.d.ts +68 -0
- package/dist/htest/grubbs.js +151 -0
- package/dist/htest/multipleComparisons.d.ts +21 -0
- package/dist/htest/multipleComparisons.js +47 -0
- package/dist/htest/rankTests.d.ts +35 -0
- package/dist/htest/rankTests.js +89 -1
- package/dist/internal/summary.js +8 -2
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/multivariate/cancor.d.ts +55 -0
- package/dist/multivariate/cancor.js +167 -0
- package/dist/multivariate/lda.d.ts +146 -0
- package/dist/multivariate/lda.js +325 -0
- package/dist/multivariate/manova.d.ts +106 -0
- package/dist/multivariate/manova.js +303 -0
- package/dist/multivariate.js +2 -0
- package/dist/numerics.js +2 -2
- package/dist/regression.js +2 -2
- package/dist/rstats.js +16 -16
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/llms.txt +2 -1
- package/package.json +6 -1
- package/dist/chunks/chunk-3DO726WZ.js +0 -3
- package/dist/chunks/chunk-53OM63EU.js +0 -2
- package/dist/chunks/chunk-ANZUXEMH.js +0 -3
- package/dist/chunks/chunk-BHQGMZTI.js +0 -2
- package/dist/chunks/chunk-HXYH5ZMB.js +0 -2
- package/dist/chunks/chunk-HYJ4MIZE.js +0 -2
- package/dist/chunks/chunk-MLNWWGGE.js +0 -2
- package/dist/chunks/chunk-TQM6KGKT.js +0 -2
- package/dist/chunks/chunk-VB6PVRSN.js +0 -2
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// Grubbs' single-outlier test, type 10 (Grubbs 1950; Pearson & Sekar 1936).
|
|
2
|
+
// Implemented from the published statistic and the independently checked Pearson–Sekar transform;
|
|
3
|
+
// no package implementation or R source is used.
|
|
4
|
+
import { groupMoments, standardize } from "../internal/summary.js";
|
|
5
|
+
import { vectorize } from "../internal/vectorize.js";
|
|
6
|
+
import { betaIncDD } from "../special/incbet.js";
|
|
7
|
+
import { ddDiv, ddMulNum, ddSub, twoProd } from "../special/dd.js";
|
|
8
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
9
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
10
|
+
const GRUBBS_TEST_KEYS = ["type", "opposite", "twoSided"];
|
|
11
|
+
/** Upper tail probability of the type-10 Grubbs statistic. */
|
|
12
|
+
function grubbsTail10(q, n) {
|
|
13
|
+
if (Number.isNaN(q) || Number.isNaN(n))
|
|
14
|
+
return NaN;
|
|
15
|
+
if (!Number.isSafeInteger(n) || n < 3)
|
|
16
|
+
return NaN;
|
|
17
|
+
if (q <= 0)
|
|
18
|
+
return 1;
|
|
19
|
+
if (q >= (n - 1) / Math.sqrt(n))
|
|
20
|
+
return 0;
|
|
21
|
+
const n1 = n - 1;
|
|
22
|
+
const n1sq = twoProd(n1, n1);
|
|
23
|
+
const nq2 = ddMulNum(twoProd(q, q), n);
|
|
24
|
+
const den = ddSub(n1sq, nq2);
|
|
25
|
+
if (den[0] + den[1] <= 0)
|
|
26
|
+
return 0;
|
|
27
|
+
// Algebraically, (n - 2)/(n - 2 + t^2) = ((n - 1)^2 - n q^2)/(n - 1)^2.
|
|
28
|
+
// Form this beta argument directly: deriving it through t^2 loses relative precision near the upper boundary.
|
|
29
|
+
const x = ddDiv(den, n1sq);
|
|
30
|
+
const xc = ddDiv(nq2, n1sq);
|
|
31
|
+
const lower = betaIncDD((n - 2) / 2, 0.5, x, xc, true, false);
|
|
32
|
+
return Math.min(1, (n * lower) / 2);
|
|
33
|
+
}
|
|
34
|
+
/** Pearson–Sekar CDF of the type-10 Grubbs statistic. */
|
|
35
|
+
function pgrubbs10(q, n) {
|
|
36
|
+
const tail = grubbsTail10(q, n);
|
|
37
|
+
return Number.isNaN(tail) ? NaN : 1 - tail;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Cumulative distribution of the type-10 Grubbs statistic. Vectorised over q.
|
|
41
|
+
* Equivalent to R's `outliers::pgrubbs()`. Type 10 only.
|
|
42
|
+
*
|
|
43
|
+
* @param q Statistic value(s).
|
|
44
|
+
* @param n Sample size.
|
|
45
|
+
* @param type Only 10 is supported; 11 and 20 are intentionally deferred.
|
|
46
|
+
*
|
|
47
|
+
* @category Distributions
|
|
48
|
+
*/
|
|
49
|
+
export function pgrubbs(q, n, type = 10) {
|
|
50
|
+
if (type !== 10)
|
|
51
|
+
throw new RangeError(`Grubbs type ${type} is not supported in rStats.js 1.9.0 (type 10 only)`);
|
|
52
|
+
return vectorize(q, (v) => pgrubbs10(v, n));
|
|
53
|
+
}
|
|
54
|
+
/** Invert the monotone type-10 CDF by bisection on the bounded Grubbs statistic. */
|
|
55
|
+
function qgrubbsLowerBound(n) {
|
|
56
|
+
let lo = 0, hi = (n - 1) / Math.sqrt(n);
|
|
57
|
+
for (let i = 0; i < 80; i++) {
|
|
58
|
+
const mid = lo + (hi - lo) / 2;
|
|
59
|
+
if (mid === lo || mid === hi)
|
|
60
|
+
break;
|
|
61
|
+
if (grubbsTail10(mid, n) >= 1)
|
|
62
|
+
lo = mid;
|
|
63
|
+
else
|
|
64
|
+
hi = mid;
|
|
65
|
+
}
|
|
66
|
+
return hi;
|
|
67
|
+
}
|
|
68
|
+
function qgrubbs10(p, n) {
|
|
69
|
+
if (Number.isNaN(p) || Number.isNaN(n))
|
|
70
|
+
return NaN;
|
|
71
|
+
if (!Number.isSafeInteger(n) || n < 3 || p < 0 || p > 1)
|
|
72
|
+
return NaN;
|
|
73
|
+
if (p === 0)
|
|
74
|
+
return qgrubbsLowerBound(n);
|
|
75
|
+
if (p === 1)
|
|
76
|
+
return (n - 1) / Math.sqrt(n);
|
|
77
|
+
let lo = 0;
|
|
78
|
+
let hi = (n - 1) / Math.sqrt(n);
|
|
79
|
+
for (let i = 0; i < 80; i++) {
|
|
80
|
+
const mid = lo + (hi - lo) / 2;
|
|
81
|
+
if (pgrubbs10(mid, n) < p)
|
|
82
|
+
lo = mid;
|
|
83
|
+
else
|
|
84
|
+
hi = mid;
|
|
85
|
+
}
|
|
86
|
+
return lo + (hi - lo) / 2;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Quantile of the type-10 Grubbs statistic. Vectorised over p.
|
|
90
|
+
* Equivalent to R's `outliers::qgrubbs()`. Type 10 only.
|
|
91
|
+
*
|
|
92
|
+
* @param p Probability value(s).
|
|
93
|
+
* @param n Sample size.
|
|
94
|
+
* @param type Only 10 is supported; 11 and 20 are intentionally deferred.
|
|
95
|
+
*
|
|
96
|
+
* @category Distributions
|
|
97
|
+
*/
|
|
98
|
+
export function qgrubbs(p, n, type = 10) {
|
|
99
|
+
if (type !== 10)
|
|
100
|
+
throw new RangeError(`Grubbs type ${type} is not supported in rStats.js 1.9.0 (type 10 only)`);
|
|
101
|
+
return vectorize(p, (v) => qgrubbs10(v, n));
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Grubbs' test for one outlier. Type 10 selects the highest or lowest observation and reports G and U.
|
|
105
|
+
* NaN observations are omitted; infinities are rejected. A constant sample has undefined statistics and p = NaN.
|
|
106
|
+
*
|
|
107
|
+
* Equivalent to R's `outliers::grubbs.test()`. Type 10 only.
|
|
108
|
+
*
|
|
109
|
+
* @param x Numeric sample.
|
|
110
|
+
* @param options Type, tail and sidedness.
|
|
111
|
+
*
|
|
112
|
+
* @category Hypothesis tests
|
|
113
|
+
*/
|
|
114
|
+
export function grubbsTest(x, options = {}) {
|
|
115
|
+
checkOptions(options, GRUBBS_TEST_KEYS, "grubbsTest"); // generated: option-keys.mjs
|
|
116
|
+
const type = options.type ?? 10;
|
|
117
|
+
if (type !== 10)
|
|
118
|
+
throw new RangeError(`Grubbs type ${type} is not supported in rStats.js 1.9.0 (type 10 only)`);
|
|
119
|
+
const values = Array.from(x).filter((v) => !Number.isNaN(v));
|
|
120
|
+
if (values.some((v) => !Number.isFinite(v)))
|
|
121
|
+
throw new RangeError("x must contain finite values");
|
|
122
|
+
const n = values.length;
|
|
123
|
+
if (n < 2)
|
|
124
|
+
throw new RangeError("sample size must be at least 2");
|
|
125
|
+
const opposite = options.opposite ?? false;
|
|
126
|
+
const twoSided = options.twoSided ?? false;
|
|
127
|
+
let extreme = values[0];
|
|
128
|
+
for (let i = 1; i < values.length; i++) {
|
|
129
|
+
if (opposite ? values[i] < extreme : values[i] > extreme)
|
|
130
|
+
extreme = values[i];
|
|
131
|
+
}
|
|
132
|
+
const z = standardize(values);
|
|
133
|
+
const index = values.indexOf(extreme);
|
|
134
|
+
const G = Math.abs(z[index]);
|
|
135
|
+
const rest = values.slice();
|
|
136
|
+
rest.splice(index, 1);
|
|
137
|
+
const moments = groupMoments([values, rest]);
|
|
138
|
+
const U = ((n - 2) * moments.variance[1]) / ((n - 1) * moments.variance[0]);
|
|
139
|
+
const tail = grubbsTail10(G, n);
|
|
140
|
+
const cdf = 1 - tail;
|
|
141
|
+
const pValue = Number.isNaN(G) ? NaN : Math.min(1, twoSided ? 2 * Math.min(cdf, tail) : tail);
|
|
142
|
+
const direction = opposite ? "lowest" : "highest";
|
|
143
|
+
return {
|
|
144
|
+
method: "Grubbs test for one outlier",
|
|
145
|
+
statistic: [G, U],
|
|
146
|
+
statisticName: ["G", "U"],
|
|
147
|
+
pValue,
|
|
148
|
+
alternative: `${direction} value ${String(extreme)} is an outlier`,
|
|
149
|
+
warnings: n === 2 ? ["NaNs produced"] : [],
|
|
150
|
+
};
|
|
151
|
+
}
|
|
@@ -146,6 +146,27 @@ export declare function manyOneUTest(x: Sample, g: readonly GroupLabel[], option
|
|
|
146
146
|
* @category Multiple comparisons
|
|
147
147
|
*/
|
|
148
148
|
export declare function dscfAllPairsTest(x: Sample, g: readonly GroupLabel[]): PMCMRResult;
|
|
149
|
+
/**
|
|
150
|
+
* Options of Tamhane's T2 and T2' all-pairs tests.
|
|
151
|
+
*
|
|
152
|
+
* @category Multiple comparisons
|
|
153
|
+
*/
|
|
154
|
+
export interface TamhaneT2Options {
|
|
155
|
+
/** Use Welch–Satterthwaite degrees of freedom (T2); false uses n_i + n_j − 2 (T2'). Default true. */
|
|
156
|
+
welch?: boolean;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Tamhane's T2 (Welch–Satterthwaite df) or T2' (pooled df) all-pairs test of means. Equivalent to
|
|
160
|
+
* `PMCMRplus::tamhaneT2Test()` in R. Character and numeric group labels are ordered as R's factor() levels; list input
|
|
161
|
+
* uses the one-based list positions as group labels.
|
|
162
|
+
*
|
|
163
|
+
* @param x Observations, or a list of samples (one per group).
|
|
164
|
+
* @param g Group labels parallel to x; omit for a list of samples.
|
|
165
|
+
* @param options Set `welch: false` for T2'.
|
|
166
|
+
*
|
|
167
|
+
* @category Multiple comparisons
|
|
168
|
+
*/
|
|
169
|
+
export declare function tamhaneT2Test(x: Sample | readonly Sample[], g?: readonly GroupLabel[], options?: TamhaneT2Options): PMCMRResult;
|
|
149
170
|
/**
|
|
150
171
|
* Games-Howell all-pairs test of means with Welch's standard errors and degrees of freedom. Equivalent to
|
|
151
172
|
* `PMCMRplus::gamesHowellTest()` in R.
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
// - Tukey (1953), Tukey-Kramer: the pooled standard error, studentized range on N - k degrees of freedom.
|
|
17
17
|
import { pnorm } from "../distributions/normal.js";
|
|
18
18
|
import { ptukey } from "../distributions/tukey.js";
|
|
19
|
+
import { pt } from "../distributions/t.js";
|
|
19
20
|
import { groupMoments, ldexp, rank, satterthwaiteDfPM, tieCounts } from "../internal/summary.js";
|
|
20
21
|
import { pDunnettUpper } from "./dunnett.js";
|
|
21
22
|
import { labelledGroups } from "./groups.js";
|
|
@@ -31,6 +32,7 @@ const DUNNETT_TEST_KEYS = ["alternative"];
|
|
|
31
32
|
const MANY_ONE_UTEST_KEYS = ["pAdjustMethod", "alternative"];
|
|
32
33
|
const DUNN_TEST_KEYS = ["method", "altp"];
|
|
33
34
|
const GLHT_DUNNETT_KEYS = ["alternative"];
|
|
35
|
+
const TAMHANE_T2_TEST_KEYS = ["welch"];
|
|
34
36
|
const TIES_WARNING = "Ties are present. z-quantiles were corrected for ties.";
|
|
35
37
|
function layout(x, g) {
|
|
36
38
|
const { labels, groups } = labelledGroups(x, g);
|
|
@@ -309,6 +311,51 @@ export function dscfAllPairsTest(x, g) {
|
|
|
309
311
|
const p = stat.map((row) => row.map((q) => (Number.isNaN(q) ? NaN : upperRange(q, d.k, Infinity))));
|
|
310
312
|
return reported("dscfAllPairsTest", allPairs(d, "Dwass-Steele-Critchlow-Fligner all-pairs test", stat, p, "single-step"));
|
|
311
313
|
}
|
|
314
|
+
/**
|
|
315
|
+
* Tamhane's T2 (Welch–Satterthwaite df) or T2' (pooled df) all-pairs test of means. Equivalent to
|
|
316
|
+
* `PMCMRplus::tamhaneT2Test()` in R. Character and numeric group labels are ordered as R's factor() levels; list input
|
|
317
|
+
* uses the one-based list positions as group labels.
|
|
318
|
+
*
|
|
319
|
+
* @param x Observations, or a list of samples (one per group).
|
|
320
|
+
* @param g Group labels parallel to x; omit for a list of samples.
|
|
321
|
+
* @param options Set `welch: false` for T2'.
|
|
322
|
+
*
|
|
323
|
+
* @category Multiple comparisons
|
|
324
|
+
*/
|
|
325
|
+
export function tamhaneT2Test(x, g, options = {}) {
|
|
326
|
+
checkOptions(options, TAMHANE_T2_TEST_KEYS, "tamhaneT2Test"); // generated: option-keys.mjs
|
|
327
|
+
const d = (() => {
|
|
328
|
+
if (g !== undefined)
|
|
329
|
+
return layout(x, g);
|
|
330
|
+
const grouped = labelledGroups(x);
|
|
331
|
+
const k = grouped.groups.length;
|
|
332
|
+
if (k < 2)
|
|
333
|
+
throw new RangeError("there must be at least two groups");
|
|
334
|
+
const n = grouped.groups.map((s) => s.length);
|
|
335
|
+
return { labels: grouped.labels, groups: grouped.groups, n, N: n.reduce((a, b) => a + b, 0), k };
|
|
336
|
+
})();
|
|
337
|
+
// Work in the common power-of-two-scaled coordinates. groupMoments exposes its scaled groups so the
|
|
338
|
+
// variances stay finite even when the original data are around 1e100 (the T2 choice avoids R's overflow artifact).
|
|
339
|
+
const scaled = groupMoments(d.groups);
|
|
340
|
+
const gm = groupMoments(scaled.groups);
|
|
341
|
+
const welch = options.welch ?? true;
|
|
342
|
+
const m = d.k * (d.k - 1) / 2;
|
|
343
|
+
const statistic = lowerTable(d.k, (i, j) => {
|
|
344
|
+
const se2 = gm.variance[i] / d.n[i] + gm.variance[j] / d.n[j];
|
|
345
|
+
return (gm.mean[i] - gm.mean[j]) / Math.sqrt(se2);
|
|
346
|
+
});
|
|
347
|
+
const pValue = lowerTable(d.k, (i, j) => {
|
|
348
|
+
const t = statistic[i - 1][j];
|
|
349
|
+
if (Number.isNaN(t))
|
|
350
|
+
return NaN;
|
|
351
|
+
const df = welch
|
|
352
|
+
? satterthwaiteDfPM([gm.variance[i], gm.variance[j]], [d.n[i], d.n[j]])
|
|
353
|
+
: d.n[i] + d.n[j] - 2;
|
|
354
|
+
const raw = 2 * pt(Math.abs(t), df, { lowerTail: false });
|
|
355
|
+
return raw >= 1 ? 1 : -Math.expm1(m * Math.log1p(-raw));
|
|
356
|
+
});
|
|
357
|
+
return reported("tamhaneT2Test", allPairs(d, welch ? "Tamhane's T2-test for unequal variances" : "Tamhane's T2'-test for unequal variances", statistic, pValue, welch ? "T2 (Sidak)" : "T2' (Sidak)"));
|
|
358
|
+
}
|
|
312
359
|
/**
|
|
313
360
|
* Games-Howell all-pairs test of means with Welch's standard errors and degrees of freedom. Equivalent to
|
|
314
361
|
* `PMCMRplus::gamesHowellTest()` in R.
|
|
@@ -66,6 +66,41 @@ export declare function friedmanTest(y: readonly (readonly number[])[]): ChisqSt
|
|
|
66
66
|
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
67
67
|
*/
|
|
68
68
|
export declare function friedmanTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
69
|
+
/**
|
|
70
|
+
* Options for {@link pageTest}.
|
|
71
|
+
*
|
|
72
|
+
* @category Hypothesis tests
|
|
73
|
+
*/
|
|
74
|
+
export interface PageTestOptions {
|
|
75
|
+
/** Alternative hypothesis. Abbreviations are accepted. Default "two.sided". */
|
|
76
|
+
alternative?: Alternative | "two" | "l" | "g";
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Page's ordered aligned rank-sum test. Equivalent to R's `PMCMRplus::pageTest()`.
|
|
80
|
+
*
|
|
81
|
+
* @param y A blocks-by-treatments matrix in the hypothesised treatment order.
|
|
82
|
+
* @param options Alternative hypothesis.
|
|
83
|
+
*
|
|
84
|
+
* @category Hypothesis tests
|
|
85
|
+
*/
|
|
86
|
+
export declare function pageTest(y: readonly (readonly number[])[], options?: PageTestOptions): HTest;
|
|
87
|
+
/**
|
|
88
|
+
* Page's test from long-form observations. Treatment and block labels follow R's factor() level order.
|
|
89
|
+
*
|
|
90
|
+
* @param x Numeric observations.
|
|
91
|
+
* @param groups Treatment labels, parallel to x.
|
|
92
|
+
* @param blocks Block labels, parallel to x.
|
|
93
|
+
* @param options Alternative hypothesis.
|
|
94
|
+
*/
|
|
95
|
+
export declare function pageTest(x: Sample, groups: readonly GroupLabel[], blocks: readonly GroupLabel[], options?: PageTestOptions): HTest;
|
|
96
|
+
/**
|
|
97
|
+
* Page's test from a formula and data frame: `"y ~ treatment | block"`.
|
|
98
|
+
*
|
|
99
|
+
* @param formula Formula with one treatment and one block variable.
|
|
100
|
+
* @param data Data frame.
|
|
101
|
+
* @param options Alternative hypothesis.
|
|
102
|
+
*/
|
|
103
|
+
export declare function pageTest(formula: string, data: DataFrame, options?: PageTestOptions): HTest;
|
|
69
104
|
/**
|
|
70
105
|
* Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to
|
|
71
106
|
* `DescTools::CochranQTest()` in R. Takes a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
|
package/dist/htest/rankTests.js
CHANGED
|
@@ -20,13 +20,15 @@ import { pf1 } from "../distributions/f.js";
|
|
|
20
20
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
21
21
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
22
22
|
import { rank, scaleToUnit, tieCounts, variance } from "../internal/summary.js";
|
|
23
|
-
import { completeRows, median, toGroups } from "./groups.js";
|
|
23
|
+
import { completeRows, median, toGroups, factorLabels, compareLabels } from "./groups.js";
|
|
24
24
|
import { blockData, kSampleData, twoGroupData } from "./testFormula.js";
|
|
25
25
|
import { matchAlternative } from "./types.js";
|
|
26
26
|
import { maxOf, minOf } from "../internal/extremes.js";
|
|
27
27
|
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
28
28
|
import { checkOptions } from "../internal/checkOptions.js";
|
|
29
|
+
import { isOptionsObject } from "../internal/optionalArgs.js";
|
|
29
30
|
const MOOD_TEST_KEYS = ["alternative"];
|
|
31
|
+
const PAGE_TEST_KEYS = ["alternative"];
|
|
30
32
|
function chisqResult(method, name, statistic, df) {
|
|
31
33
|
return {
|
|
32
34
|
method,
|
|
@@ -91,6 +93,92 @@ export function friedmanTest(y, data) {
|
|
|
91
93
|
const stat = (12 * ss) / (n * k * (k + 1) - ties / (k - 1));
|
|
92
94
|
return chisqResult("Friedman rank sum test", "Friedman chi-squared", stat, k - 1);
|
|
93
95
|
}
|
|
96
|
+
export function pageTest(y, groupsOrOptions = {}, blocks, maybeOptions = {}) {
|
|
97
|
+
if (typeof y !== "string" && isOptionsObject(groupsOrOptions))
|
|
98
|
+
checkOptions(groupsOrOptions, PAGE_TEST_KEYS, "pageTest"); // generated: option-keys.mjs
|
|
99
|
+
if (isOptionsObject(blocks))
|
|
100
|
+
checkOptions(blocks, PAGE_TEST_KEYS, "pageTest"); // generated: option-keys.mjs
|
|
101
|
+
checkOptions(maybeOptions, PAGE_TEST_KEYS, "pageTest"); // generated: option-keys.mjs
|
|
102
|
+
let matrix;
|
|
103
|
+
let options;
|
|
104
|
+
if (typeof y === "string") {
|
|
105
|
+
matrix = blockData(y, groupsOrOptions);
|
|
106
|
+
options = blocks ?? {};
|
|
107
|
+
}
|
|
108
|
+
else if (Array.isArray(blocks)) {
|
|
109
|
+
const x = Array.from(y);
|
|
110
|
+
const groups = groupsOrOptions;
|
|
111
|
+
const blockLabelsInput = blocks;
|
|
112
|
+
if (x.length !== groups.length || x.length !== blockLabelsInput.length) {
|
|
113
|
+
throw new RangeError("'x', 'groups' and 'blocks' must have the same length");
|
|
114
|
+
}
|
|
115
|
+
if (!x.every(Number.isFinite))
|
|
116
|
+
throw new RangeError("missing or non-finite values are not allowed");
|
|
117
|
+
const treatmentLabels = factorLabels(groups);
|
|
118
|
+
const blockLabels = factorLabels(blockLabelsInput);
|
|
119
|
+
if (treatmentLabels.some((v) => typeof v === "number" && Number.isNaN(v)) ||
|
|
120
|
+
blockLabels.some((v) => typeof v === "number" && Number.isNaN(v))) {
|
|
121
|
+
throw new RangeError("missing treatment or block labels are not allowed");
|
|
122
|
+
}
|
|
123
|
+
const ts = Array.from(new Set(treatmentLabels)).sort(compareLabels);
|
|
124
|
+
const bs = Array.from(new Set(blockLabels)).sort(compareLabels);
|
|
125
|
+
if (ts.length < 2 || bs.length < 1)
|
|
126
|
+
throw new RangeError("at least two treatments and one block are required");
|
|
127
|
+
const ti = new Map(ts.map((v, i) => [v, i]));
|
|
128
|
+
const bi = new Map(bs.map((v, i) => [v, i]));
|
|
129
|
+
matrix = bs.map(() => new Array(ts.length).fill(NaN));
|
|
130
|
+
for (let i = 0; i < x.length; i++) {
|
|
131
|
+
const r = bi.get(blockLabels[i]);
|
|
132
|
+
const col = ti.get(treatmentLabels[i]);
|
|
133
|
+
if (!Number.isNaN(matrix[r][col]))
|
|
134
|
+
throw new RangeError("not an unreplicated complete block design");
|
|
135
|
+
matrix[r][col] = x[i];
|
|
136
|
+
}
|
|
137
|
+
if (matrix.some((row) => row.some((v) => Number.isNaN(v)))) {
|
|
138
|
+
throw new RangeError("not an unreplicated complete block design");
|
|
139
|
+
}
|
|
140
|
+
options = maybeOptions;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
matrix = y.map((row) => Array.from(row));
|
|
144
|
+
options = groupsOrOptions;
|
|
145
|
+
}
|
|
146
|
+
const n = matrix.length;
|
|
147
|
+
const k = matrix[0]?.length ?? 0;
|
|
148
|
+
if (n < 1 || k < 2 || matrix.some((row) => row.length !== k)) {
|
|
149
|
+
throw new RangeError("'y' must be a non-empty matrix with at least two columns");
|
|
150
|
+
}
|
|
151
|
+
if (matrix.some((row) => row.some((v) => !Number.isFinite(v)))) {
|
|
152
|
+
throw new RangeError("missing or non-finite values are not allowed in a block");
|
|
153
|
+
}
|
|
154
|
+
const alternative = matchAlternative(options.alternative);
|
|
155
|
+
const sums = new Array(k).fill(0);
|
|
156
|
+
for (const row of matrix) {
|
|
157
|
+
const ranks = rank(row);
|
|
158
|
+
for (let j = 0; j < k; j++)
|
|
159
|
+
sums[j] += ranks[j];
|
|
160
|
+
}
|
|
161
|
+
let estimate = 0;
|
|
162
|
+
for (let j = 0; j < k; j++)
|
|
163
|
+
estimate += (j + 1) * sums[j];
|
|
164
|
+
const expected = n * k * (k + 1) ** 2 / 4;
|
|
165
|
+
const v = n * k ** 2 * (k - 1) * (k + 1) ** 2 / 144;
|
|
166
|
+
const statistic = (estimate - expected - 0.5) / Math.sqrt(v);
|
|
167
|
+
const pValue = alternative === "less"
|
|
168
|
+
? pnorm(statistic)
|
|
169
|
+
: alternative === "greater"
|
|
170
|
+
? pnorm(statistic, { lowerTail: false })
|
|
171
|
+
: 2 * pnorm(-Math.abs(statistic));
|
|
172
|
+
return {
|
|
173
|
+
method: "Page's ordered aligned rank sum test",
|
|
174
|
+
alternative,
|
|
175
|
+
statistic,
|
|
176
|
+
statisticName: "z",
|
|
177
|
+
pValue,
|
|
178
|
+
estimate,
|
|
179
|
+
warnings: [],
|
|
180
|
+
};
|
|
181
|
+
}
|
|
94
182
|
/**
|
|
95
183
|
* Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to
|
|
96
184
|
* `DescTools::CochranQTest()` in R. Takes a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
|
package/dist/internal/summary.js
CHANGED
|
@@ -253,8 +253,14 @@ export function standardize(x) {
|
|
|
253
253
|
const c = x.length < 2 ? undefined : centered(x);
|
|
254
254
|
if (c === undefined)
|
|
255
255
|
return x.map(() => NaN);
|
|
256
|
-
const
|
|
257
|
-
|
|
256
|
+
const variance = ddDivNum(crossSum(c.d, c.d), x.length - 1);
|
|
257
|
+
const sd = Math.sqrt(variance[0] + variance[1]);
|
|
258
|
+
return c.d.map((v) => {
|
|
259
|
+
if (!(sd > 0))
|
|
260
|
+
return NaN;
|
|
261
|
+
const z = ddDivNum(v, sd);
|
|
262
|
+
return z[0] + z[1];
|
|
263
|
+
});
|
|
258
264
|
}
|
|
259
265
|
export function groupMoments(groups) {
|
|
260
266
|
const { groups: scaled, k } = scaleToUnit(groups);
|
package/dist/linalg.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{a as qt,b as vt,c as Nt,d as kt,e as L,f as Et,g as St,h as Ft,i as Pt,j as It,k as Rt,l as Ot,m as Tt,n as At,o as jt,p as _t,q as Ht}from"./chunks/chunk-GRTQ7FYQ.js";import{g as U}from"./chunks/chunk-TYMC2D5I.js";import"./chunks/chunk-2X6PE6JF.js";import{a as Lt,b as Ut}from"./chunks/chunk-MLNWWGGE.js";import{a as P,c as pt,d as I,e as Y,f as H,g as Mt,h as gt,i as wt,j,k as bt,m as G,n as J}from"./chunks/chunk-53OM63EU.js";import"./chunks/chunk-45HUI3M4.js";import{a as k,c as F,e as v,g as A,i as K,j as at,m as st,n as ct,o as it,p as lt,q as ft,r as mt,s as ut,t as ht,u as dt,v as yt,x as xt}from"./chunks/chunk-VB6PVRSN.js";import"./chunks/chunk-JXJDCQYE.js";import"./chunks/chunk-IJST2ZGT.js";import{a as B}from"./chunks/chunk-YD4U3QZ3.js";import{l as T,u as q}from"./chunks/chunk-2ZQ2TSZS.js";function Ct(e){let n=F(e);A(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let r of n.data)if(!Number.isFinite(r))throw new RangeError("the matrix contains NaN or infinite values");let a=K(n.data),u={nrow:t,ncol:t,data:a===0?n.data.slice():n.data.map(r=>q(r,-a))},l=k(t,t),m=new Float64Array(t),h=new Float64Array(t);G(u,l),J(u,l,m,h,!0);let d=u.data,s=l.data,o=(r,c)=>r+c*t,M=new Uint8Array(t);for(let r=0;r+1<t;r++)h[r]>0&&h[r+1]<0&&(M[r++]=1);for(let r=0;r<t;r++)for(let c=r+1;c<t;c++)c===r+1&&M[r]||(d[o(c,r)]=0);let f=new Array(t),p=new Array(t);for(let r=0;r<t;r++){if(!M[r]){f[r]=d[o(r,r)],p[r]=0;continue}let c=Qt(d[o(r,r)],d[o(r,r+1)],d[o(r+1,r)],d[o(r+1,r+1)]);d[o(r,r)]=c.a,d[o(r,r+1)]=c.b,d[o(r+1,r)]=c.c,d[o(r+1,r+1)]=c.d;let{cs:y,sn:w}=c;for(let g=r+2;g<t;g++){let x=d[o(r,g)],b=d[o(r+1,g)];d[o(r,g)]=y*x+w*b,d[o(r+1,g)]=y*b-w*x}for(let g=0;g<r;g++){let x=d[o(g,r)],b=d[o(g,r+1)];d[o(g,r)]=y*x+w*b,d[o(g,r+1)]=y*b-w*x}for(let g=0;g<t;g++){let x=s[o(g,r)],b=s[o(g,r+1)];s[o(g,r)]=y*x+w*b,s[o(g,r+1)]=y*b-w*x}f[r]=c.rt1r,p[r]=c.rt1i,f[r+1]=c.rt2r,p[r+1]=c.rt2i,r++}if(a===0)return{T:v(u),Q:v(l),valuesRe:f,valuesIm:p};let i=r=>q(r,a);return{T:v(u).map(r=>r.map(i)),Q:v(l),valuesRe:f.map(i),valuesIm:p.map(i)}}var E=(e,n)=>n>=0&&!Object.is(n,-0)?Math.abs(e):-Math.abs(e);function Qt(e,n,t,a){let u=22250738585072014e-324,l=Number.EPSILON,m=4,h=2**Math.trunc(Math.log(u/l)/Math.log(2)/2),d=1/h,s,o;if(t===0)s=1,o=0;else if(n===0){s=0,o=1;let f=a;a=e,e=f,n=-t,t=0}else if(e-a===0&&E(1,n)!==E(1,t))s=1,o=0;else{let f=e-a,p=.5*f,i=Math.max(Math.abs(n),Math.abs(t)),r=Math.min(Math.abs(n),Math.abs(t))*E(1,n)*E(1,t),c=Math.max(Math.abs(p),i),y=p/c*p+i/c*r;if(y>=m*l){y=p+E(Math.sqrt(c)*Math.sqrt(y),p),e=a+y,a=a-i/y*r;let w=Math.hypot(t,y);s=y/w,o=t/w,n=n-t,t=0}else{let w=0,g=n+t;for(;w++,c=Math.max(Math.abs(f),Math.abs(g)),!(!(c>=d&&(g*=h,f*=h,w<=20))&&!(c<=h&&(g*=d,f*=d,w<=20))););p=.5*f;let x=Math.hypot(g,f);s=Math.sqrt(.5*(1+Math.abs(g)/x)),o=-(p/(x*s))*E(1,g);let b=e*s+n*o,S=-e*o+n*s,Q=t*s+a*o,X=-t*o+a*s;if(e=b*s+Q*o,n=S*s+X*o,t=-b*o+Q*s,a=-S*o+X*s,f=.5*(e+a),e=f,a=f,t!==0)if(n!==0){if(E(1,n)===E(1,t)){let $=Math.sqrt(Math.abs(n)),z=Math.sqrt(Math.abs(t));p=E($*z,t),x=1/Math.sqrt(Math.abs(n+t)),e=f+p,a=f-p,n=n-t,t=0;let D=$*x,V=z*x;f=s*D-o*V,o=s*V+o*D,s=f}}else n=-t,t=0,f=s,s=-o,o=f}}let M=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:e,b:n,c:t,d:a,cs:s,sn:o,rt1r:e,rt1i:M,rt2r:a,rt2i:-M}}var Xt=["method","order"],$t=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function zt(e,n={}){T(n,Xt,"expm");let t=n.method??"Higham08.b";if(!$t.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let a=n.order??8,u=t==="Pade"||t==="PadeO"||t==="Taylor"||t==="TaylorO";if(u&&!(Number.isInteger(a)&&a>=1&&a<=2147483647))throw new RangeError("order must be a positive integer at most 2147483647");let l=F(e);A(l);let m=l.nrow;if(m===0)return[];if(m===1)return[[Math.exp(l.data[0])]];if(u&&a*(m**3+64)>2**31)throw new RangeError(`order = ${a} is too large for a ${m} x ${m} matrix: order * (n^3 + 64) must be at most 2^31`);for(let f of l.data)if(!Number.isFinite(f))throw new RangeError("the matrix contains NaN or infinite values");if(t==="Taylor"||t==="TaylorO")return v(Gt(l,a));if(t==="Pade"||t==="PadeO")return v(Jt(l,a));let h,d;t==="Higham08.b"?(h=j("P",l),d=j("S",l)):Kt(l)&&(d=j("S",l));let s=Yt(l),o=s.data,M=(f,p)=>f+p*m;if(d){let f=d.scale;for(let p=0;p<m;p++)for(let i=0;i<m;i++)o[M(i,p)]=o[M(i,p)]*f[i]/f[p]}if(h){let f=h.scale,p=i=>{let r=f[i];if(r!==i){for(let c=0;c<m;c++)[o[M(c,i)],o[M(c,r)]]=[o[M(c,r)],o[M(c,i)]];for(let c=0;c<m;c++)[o[M(i,c)],o[M(r,c)]]=[o[M(r,c)],o[M(i,c)]]}};for(let i=h.ilo-1;i>=0;i--)p(i);for(let i=h.ihi+1;i<m;i++)p(i)}return v(s)}var Dt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Vt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function Bt(e,n){if(n<1/0)return Math.log2(n);let t=0;for(let u of e.data)t=Math.max(t,Math.abs(u));let a=Math.floor(Math.log2(t));return Math.log2(I({...e,data:e.data.map(u=>q(u,-a))}))+a}function Z(e,n){return Math.max(0,Math.ceil(Bt(e,n)-Math.log2(5.4)))}function Kt(e){let n=I(e);if(n<=2.1)return!1;let t=Z(e,n);return t>0&&e.data.some(a=>a!==0&&Math.abs(q(a,-t))<2**-1022)}function Yt(e){let n=e.nrow,t=I(e),a=C(n),u,l,m=0;if(t<=2.1){let o=t<=.015?3:t<=.25?5:t<=.95?7:9,M=Dt[o],f=N(e,e),p=_([[M[1],a]]),i=_([[M[0],a]]),r=f;for(let c=1;c<=(o-1)/2;c++)c>1&&(r=N(r,f)),R(p,M[2*c+1],r),R(i,M[2*c],r);u=N(e,p),l=i}else{m=Z(e,t);let o={...e,data:e.data.map(w=>q(w,-m))},M=N(o,o),f=N(M,M),p=N(M,f),i=Vt,r=_([[i[13],p],[i[11],f],[i[9],M]]),c=N(p,r);for(let[w,g]of[[7,p],[5,f],[3,M],[1,a]])R(c,i[w],g);u=N(o,c);let y=_([[i[12],p],[i[10],f],[i[8],M]]);l=N(p,y);for(let[w,g]of[[6,p],[4,f],[2,M],[0,a]])R(l,i[w],g)}let h=k(n,n),d=k(n,n);for(let o=0;o<n*n;o++)h.data[o]=l.data[o]-u.data[o],d.data[o]=l.data[o]+u.data[o];let s=F(H(v(h),v(d)));for(let o=0;o<m;o++)s=et(s,s);return s}function tt(e){let n=I(e),t=Y(e),a=n*t<1/0?Math.sqrt(n*t):Math.sqrt(n)*Math.sqrt(t),u=1;for(;2**u<a;)u++;let l=u+4;if(!e.data.some(h=>h!==0&&Math.abs(q(h,-l))<2**-1022))return l;if(a===0||n*t<2**-1e3)return Math.max(0,Math.ceil((Math.log2(n)+Math.log2(t))/2)+4);let m=u;for(;m>-4&&2**(m-1)>=a;)m--;return Math.max(0,m+4)}function nt(e,n){let t=Math.max(0,n-1e3);return{A:t===0?e:O(e,2**-t),N:2**(n-t)}}function ot(e,n){for(let t=0;t<n;t++)e=et(e,e);return e}function rt(e){let n=e.nrow;for(let t=0;t<n;t++)e.data[t+t*n]=e.data[t+t*n]+1}function Gt(e,n){let t=tt(e),{A:a,N:u}=nt(e,t),l=C(e.nrow);for(let m=n;m>=1;m--)l=O(N(l,a),1/(m*u)),rt(l);return ot(l,t)}function W(e,n,t,a){let u=a===1?e:O(e,-1),l=C(e.nrow);for(let m=n;m>=1;m--){let h=(n-m+1)/(m*(2*n-m+1)*t);l=O(N(l,u),h),rt(l)}return l}function Jt(e,n){let t=e.nrow,a=tt(e),{A:u,N:l}=nt(e,a),m=W(u,n,l,1),h=W(u,n,l,-1),d=k(t,t);for(let s=0;s<t;s++)d.data.set(Wt(h,m.data.subarray(s*t,(s+1)*t)),s*t);return ot(d,a)}function Wt(e,n){let t=e.nrow,a=1e-100,u=i=>{let r=new Float64Array(t);for(let c=0;c<t;c++){let y=0;for(let w=0;w<t;w++)y+=e.data[c+w*t]*i[w];r[c]=y}return r},l=(i,r)=>{let c=0;for(let y=0;y<t;y++)c+=i[y]*r[y];return c},m=(i,r,c)=>{let y=new Float64Array(t);for(let w=0;w<t;w++)y[w]=i[w]+r*c[w];return y},h=new Float64Array(t),d=Float64Array.from(n),s=Float64Array.from(n),o=Float64Array.from(n),M=Float64Array.from(n),f=Math.sqrt(l(d,d)),p=l(M,n);if(Math.abs(p)<=a)return h;for(let i=0;i<t;i++){let r=u(s),c=l(M,r);if(Math.abs(c)<=a)return h;let y=p/c;if(Math.abs(y)<=a)return h;let w=m(o,-y,r);r=m(o,1,w);let g=u(r);d=m(d,-y,g);let x=Math.sqrt(l(d,d));if(h=m(h,y,r),Math.abs(x/f)<=1e-30)return h;let b=l(M,d);if(Math.abs(b)<=a)return h;let S=b/p;p=b,o=m(d,S,w),g=m(w,S,s),s=m(o,S,g)}return h}function C(e){let n=k(e,e);for(let t=0;t<e;t++)n.data[t+t*e]=1;return n}function et(e,n){let t=e.nrow,a=e.ncol,u=n.ncol,l=k(t,u);for(let m=0;m<u;m++)for(let h=0;h<t;h++){let d=0;for(let s=0;s<a;s++){let o=e.data[h+s*t],M=n.data[s+m*a];o===0&&Math.abs(M)===1/0||M===0&&Math.abs(o)===1/0||(d+=o*M)}l.data[h+m*t]=d}return l}function N(e,n){let t=e.nrow,a=e.ncol,u=n.ncol,l=k(t,u);for(let m=0;m<u;m++)for(let h=0;h<t;h++){let d=0;for(let s=0;s<a;s++)d+=e.data[h+s*t]*n.data[s+m*a];l.data[h+m*t]=d}return l}function O(e,n){return{nrow:e.nrow,ncol:e.ncol,data:e.data.map(t=>t*n)}}function R(e,n,t){for(let a=0;a<e.data.length;a++)e.data[a]=e.data[a]+n*t.data[a]}function _(e){let[n,t]=e[0],a=O(t,n);for(let u=1;u<e.length;u++)R(a,e[u][0],e[u][1]);return a}var Zt=["center","scale","rank","tol"],tn=22250738585072014e-324;function nn(e){let n=e.length,t=n===0?0:e[0].length,a=e.map(s=>{if(s.length!==t)throw new TypeError("all rows of the matrix must have the same length");if(!s.every(Number.isFinite))throw new RangeError("the matrix contains NaN or infinite values");return Array.from(s)}),u=Array.from({length:n},(s,o)=>o),l=Math.min(n,t);for(let s=0;s<l;s++){let o=s,M=Math.abs(a[s][s]);for(let i=s+1;i<n;i++){let r=Math.abs(a[i][s]);(r>M||Number.isNaN(M))&&(M=r,o=i)}o!==s&&([a[o],a[s]]=[a[s],a[o]],[u[o],u[s]]=[u[s],u[o]]);let f=a[s][s];if(f===0)continue;let p=Math.abs(f)>=tn?1/f:NaN;for(let i=s+1;i<n;i++){let r=Number.isNaN(p)?a[i][s]/f:a[i][s]*p;a[i][s]=r;for(let c=s+1;c<t;c++)a[i][c]=a[i][c]-r*a[s][c]}}let m=Array.from({length:n},(s,o)=>Array.from({length:l},(M,f)=>o===f?1:o>f?a[o][f]:0)),h=Array.from({length:l},(s,o)=>Array.from({length:t},(M,f)=>f>=o?a[o][f]:0)),d=Array.from({length:n},()=>new Array(n).fill(0));return u.forEach((s,o)=>d[s][o]=1),{P:d,L:m,U:h}}function on(e,n={}){T(n,Zt,"prcomp");let{rank:t,tol:a}=n;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let u=e.map(c=>Array.from(c)),l=n.center??!0,m=n.scale??!1,h=U(u,{center:l,scale:m});if(h.scale?.some(c=>c===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let d=u.length,s=c=>c.every(y=>y.every(Number.isFinite)),o=s(u)&&s(h.scaled)?P(h.scaled,{nu:0}):void 0,M=0;if(s(u)&&(o===void 0||o.d.some(c=>c===1/0))){let c=Math.floor(Math.log2(B(u.flat().map(Math.abs)))),y=g=>typeof g=="boolean"?g:g.map(x=>q(x,-c)),w=U(u.map(g=>g.map(x=>q(x,-c))),{center:y(l),scale:y(m)});o=P(w.scaled,{nu:0}),m===!1&&(M=c),h={scaled:w.scaled},w.center!==void 0&&(h.center=w.center.map(g=>q(g,c))),w.scale!==void 0&&(h.scale=w.scale.map(g=>q(g,c)))}o??=P(h.scaled,{nu:0});let f=o.d.map(c=>q(c/Math.sqrt(Math.max(1,d-1)),M)),p=t===void 0?f.length:Math.min(Math.trunc(t),f.length);if(Number.isNaN(a))throw new RangeError("'tol' must not be NaN");a!==void 0&&(p=Math.min(p,f.filter(c=>c>f[0]*a).length));let i=o.v.map(c=>c.slice(0,p)),r={sdev:f,rotation:i,x:L(h.scaled,o.v).map(c=>c.slice(0,p).map(y=>q(y,M)))};return h.center!==void 0&&(r.center=h.center),h.scale!==void 0&&(r.scale=h.scale),r}export{Et as add,Nt as backsolve,Tt as cbind,yt as chol,xt as chol2inv,qt as crossprod,gt as det,Mt as determinant,Ot as diag,Pt as divide,bt as eigen,zt as expm,kt as forwardsolve,Ut as ginv,Lt as kappa,_t as kronecker,nn as lu,L as matMul,jt as matrix,Ft as multiply,pt as norm,It as outer,on as prcomp,at as qr,it as qrCoef,ft as qrFitted,mt as qrQ,st as qrQty,ct as qrQy,ut as qrR,lt as qrResid,dt as qrSolve,ht as qrX,At as rbind,wt as rcond,Ct as schur,Ht as seq,H as solve,St as subtract,P as svd,Rt as t,vt as tcrossprod};
|
|
1
|
+
/*! rStats.js 1.9.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as qt,b as vt,c as Nt,d as kt,e as L,f as Et,g as St,h as Ft,i as Pt,j as It,k as Rt,l as Ot,m as Tt,n as At,o as jt,p as _t,q as Ht}from"./chunks/chunk-WOZEQS2I.js";import{g as U}from"./chunks/chunk-IXK3JA2R.js";import"./chunks/chunk-OGHJJKUR.js";import{a as Lt,b as Ut}from"./chunks/chunk-YFZBGUIT.js";import{a as pt,b as I,c as Y,d as H,e as Mt,f as gt,g as wt}from"./chunks/chunk-WFE52E6F.js";import{a as P,d as j,e as bt,g as G,h as J}from"./chunks/chunk-XNLZFKL5.js";import"./chunks/chunk-ANGKH5D4.js";import{a as yt,c as xt}from"./chunks/chunk-MT7XELUM.js";import{a as k,c as F,e as v,g as A,i as K,j as at,m as st,n as ct,o as it,p as lt,q as ft,r as mt,s as ut,t as ht,u as dt}from"./chunks/chunk-TKMNASCD.js";import"./chunks/chunk-BX47HGID.js";import"./chunks/chunk-5C7XHQKP.js";import{a as B}from"./chunks/chunk-4IHHZ3F4.js";import{l as T,u as q}from"./chunks/chunk-IEATFACS.js";function Ct(e){let n=F(e);A(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let r of n.data)if(!Number.isFinite(r))throw new RangeError("the matrix contains NaN or infinite values");let a=K(n.data),u={nrow:t,ncol:t,data:a===0?n.data.slice():n.data.map(r=>q(r,-a))},l=k(t,t),m=new Float64Array(t),h=new Float64Array(t);G(u,l),J(u,l,m,h,!0);let d=u.data,s=l.data,o=(r,c)=>r+c*t,M=new Uint8Array(t);for(let r=0;r+1<t;r++)h[r]>0&&h[r+1]<0&&(M[r++]=1);for(let r=0;r<t;r++)for(let c=r+1;c<t;c++)c===r+1&&M[r]||(d[o(c,r)]=0);let f=new Array(t),p=new Array(t);for(let r=0;r<t;r++){if(!M[r]){f[r]=d[o(r,r)],p[r]=0;continue}let c=Qt(d[o(r,r)],d[o(r,r+1)],d[o(r+1,r)],d[o(r+1,r+1)]);d[o(r,r)]=c.a,d[o(r,r+1)]=c.b,d[o(r+1,r)]=c.c,d[o(r+1,r+1)]=c.d;let{cs:y,sn:w}=c;for(let g=r+2;g<t;g++){let x=d[o(r,g)],b=d[o(r+1,g)];d[o(r,g)]=y*x+w*b,d[o(r+1,g)]=y*b-w*x}for(let g=0;g<r;g++){let x=d[o(g,r)],b=d[o(g,r+1)];d[o(g,r)]=y*x+w*b,d[o(g,r+1)]=y*b-w*x}for(let g=0;g<t;g++){let x=s[o(g,r)],b=s[o(g,r+1)];s[o(g,r)]=y*x+w*b,s[o(g,r+1)]=y*b-w*x}f[r]=c.rt1r,p[r]=c.rt1i,f[r+1]=c.rt2r,p[r+1]=c.rt2i,r++}if(a===0)return{T:v(u),Q:v(l),valuesRe:f,valuesIm:p};let i=r=>q(r,a);return{T:v(u).map(r=>r.map(i)),Q:v(l),valuesRe:f.map(i),valuesIm:p.map(i)}}var E=(e,n)=>n>=0&&!Object.is(n,-0)?Math.abs(e):-Math.abs(e);function Qt(e,n,t,a){let u=22250738585072014e-324,l=Number.EPSILON,m=4,h=2**Math.trunc(Math.log(u/l)/Math.log(2)/2),d=1/h,s,o;if(t===0)s=1,o=0;else if(n===0){s=0,o=1;let f=a;a=e,e=f,n=-t,t=0}else if(e-a===0&&E(1,n)!==E(1,t))s=1,o=0;else{let f=e-a,p=.5*f,i=Math.max(Math.abs(n),Math.abs(t)),r=Math.min(Math.abs(n),Math.abs(t))*E(1,n)*E(1,t),c=Math.max(Math.abs(p),i),y=p/c*p+i/c*r;if(y>=m*l){y=p+E(Math.sqrt(c)*Math.sqrt(y),p),e=a+y,a=a-i/y*r;let w=Math.hypot(t,y);s=y/w,o=t/w,n=n-t,t=0}else{let w=0,g=n+t;for(;w++,c=Math.max(Math.abs(f),Math.abs(g)),!(!(c>=d&&(g*=h,f*=h,w<=20))&&!(c<=h&&(g*=d,f*=d,w<=20))););p=.5*f;let x=Math.hypot(g,f);s=Math.sqrt(.5*(1+Math.abs(g)/x)),o=-(p/(x*s))*E(1,g);let b=e*s+n*o,S=-e*o+n*s,Q=t*s+a*o,X=-t*o+a*s;if(e=b*s+Q*o,n=S*s+X*o,t=-b*o+Q*s,a=-S*o+X*s,f=.5*(e+a),e=f,a=f,t!==0)if(n!==0){if(E(1,n)===E(1,t)){let $=Math.sqrt(Math.abs(n)),z=Math.sqrt(Math.abs(t));p=E($*z,t),x=1/Math.sqrt(Math.abs(n+t)),e=f+p,a=f-p,n=n-t,t=0;let D=$*x,V=z*x;f=s*D-o*V,o=s*V+o*D,s=f}}else n=-t,t=0,f=s,s=-o,o=f}}let M=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:e,b:n,c:t,d:a,cs:s,sn:o,rt1r:e,rt1i:M,rt2r:a,rt2i:-M}}var Xt=["method","order"],$t=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function zt(e,n={}){T(n,Xt,"expm");let t=n.method??"Higham08.b";if(!$t.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let a=n.order??8,u=t==="Pade"||t==="PadeO"||t==="Taylor"||t==="TaylorO";if(u&&!(Number.isInteger(a)&&a>=1&&a<=2147483647))throw new RangeError("order must be a positive integer at most 2147483647");let l=F(e);A(l);let m=l.nrow;if(m===0)return[];if(m===1)return[[Math.exp(l.data[0])]];if(u&&a*(m**3+64)>2**31)throw new RangeError(`order = ${a} is too large for a ${m} x ${m} matrix: order * (n^3 + 64) must be at most 2^31`);for(let f of l.data)if(!Number.isFinite(f))throw new RangeError("the matrix contains NaN or infinite values");if(t==="Taylor"||t==="TaylorO")return v(Gt(l,a));if(t==="Pade"||t==="PadeO")return v(Jt(l,a));let h,d;t==="Higham08.b"?(h=j("P",l),d=j("S",l)):Kt(l)&&(d=j("S",l));let s=Yt(l),o=s.data,M=(f,p)=>f+p*m;if(d){let f=d.scale;for(let p=0;p<m;p++)for(let i=0;i<m;i++)o[M(i,p)]=o[M(i,p)]*f[i]/f[p]}if(h){let f=h.scale,p=i=>{let r=f[i];if(r!==i){for(let c=0;c<m;c++)[o[M(c,i)],o[M(c,r)]]=[o[M(c,r)],o[M(c,i)]];for(let c=0;c<m;c++)[o[M(i,c)],o[M(r,c)]]=[o[M(r,c)],o[M(i,c)]]}};for(let i=h.ilo-1;i>=0;i--)p(i);for(let i=h.ihi+1;i<m;i++)p(i)}return v(s)}var Dt={3:[120,60,12,1],5:[30240,15120,3360,420,30,1],7:[17297280,8648640,1995840,277200,25200,1512,56,1],9:[17643225600,8821612800,2075673600,302702400,30270240,2162160,110880,3960,90,1]},Vt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function Bt(e,n){if(n<1/0)return Math.log2(n);let t=0;for(let u of e.data)t=Math.max(t,Math.abs(u));let a=Math.floor(Math.log2(t));return Math.log2(I({...e,data:e.data.map(u=>q(u,-a))}))+a}function Z(e,n){return Math.max(0,Math.ceil(Bt(e,n)-Math.log2(5.4)))}function Kt(e){let n=I(e);if(n<=2.1)return!1;let t=Z(e,n);return t>0&&e.data.some(a=>a!==0&&Math.abs(q(a,-t))<2**-1022)}function Yt(e){let n=e.nrow,t=I(e),a=C(n),u,l,m=0;if(t<=2.1){let o=t<=.015?3:t<=.25?5:t<=.95?7:9,M=Dt[o],f=N(e,e),p=_([[M[1],a]]),i=_([[M[0],a]]),r=f;for(let c=1;c<=(o-1)/2;c++)c>1&&(r=N(r,f)),R(p,M[2*c+1],r),R(i,M[2*c],r);u=N(e,p),l=i}else{m=Z(e,t);let o={...e,data:e.data.map(w=>q(w,-m))},M=N(o,o),f=N(M,M),p=N(M,f),i=Vt,r=_([[i[13],p],[i[11],f],[i[9],M]]),c=N(p,r);for(let[w,g]of[[7,p],[5,f],[3,M],[1,a]])R(c,i[w],g);u=N(o,c);let y=_([[i[12],p],[i[10],f],[i[8],M]]);l=N(p,y);for(let[w,g]of[[6,p],[4,f],[2,M],[0,a]])R(l,i[w],g)}let h=k(n,n),d=k(n,n);for(let o=0;o<n*n;o++)h.data[o]=l.data[o]-u.data[o],d.data[o]=l.data[o]+u.data[o];let s=F(H(v(h),v(d)));for(let o=0;o<m;o++)s=et(s,s);return s}function tt(e){let n=I(e),t=Y(e),a=n*t<1/0?Math.sqrt(n*t):Math.sqrt(n)*Math.sqrt(t),u=1;for(;2**u<a;)u++;let l=u+4;if(!e.data.some(h=>h!==0&&Math.abs(q(h,-l))<2**-1022))return l;if(a===0||n*t<2**-1e3)return Math.max(0,Math.ceil((Math.log2(n)+Math.log2(t))/2)+4);let m=u;for(;m>-4&&2**(m-1)>=a;)m--;return Math.max(0,m+4)}function nt(e,n){let t=Math.max(0,n-1e3);return{A:t===0?e:O(e,2**-t),N:2**(n-t)}}function ot(e,n){for(let t=0;t<n;t++)e=et(e,e);return e}function rt(e){let n=e.nrow;for(let t=0;t<n;t++)e.data[t+t*n]=e.data[t+t*n]+1}function Gt(e,n){let t=tt(e),{A:a,N:u}=nt(e,t),l=C(e.nrow);for(let m=n;m>=1;m--)l=O(N(l,a),1/(m*u)),rt(l);return ot(l,t)}function W(e,n,t,a){let u=a===1?e:O(e,-1),l=C(e.nrow);for(let m=n;m>=1;m--){let h=(n-m+1)/(m*(2*n-m+1)*t);l=O(N(l,u),h),rt(l)}return l}function Jt(e,n){let t=e.nrow,a=tt(e),{A:u,N:l}=nt(e,a),m=W(u,n,l,1),h=W(u,n,l,-1),d=k(t,t);for(let s=0;s<t;s++)d.data.set(Wt(h,m.data.subarray(s*t,(s+1)*t)),s*t);return ot(d,a)}function Wt(e,n){let t=e.nrow,a=1e-100,u=i=>{let r=new Float64Array(t);for(let c=0;c<t;c++){let y=0;for(let w=0;w<t;w++)y+=e.data[c+w*t]*i[w];r[c]=y}return r},l=(i,r)=>{let c=0;for(let y=0;y<t;y++)c+=i[y]*r[y];return c},m=(i,r,c)=>{let y=new Float64Array(t);for(let w=0;w<t;w++)y[w]=i[w]+r*c[w];return y},h=new Float64Array(t),d=Float64Array.from(n),s=Float64Array.from(n),o=Float64Array.from(n),M=Float64Array.from(n),f=Math.sqrt(l(d,d)),p=l(M,n);if(Math.abs(p)<=a)return h;for(let i=0;i<t;i++){let r=u(s),c=l(M,r);if(Math.abs(c)<=a)return h;let y=p/c;if(Math.abs(y)<=a)return h;let w=m(o,-y,r);r=m(o,1,w);let g=u(r);d=m(d,-y,g);let x=Math.sqrt(l(d,d));if(h=m(h,y,r),Math.abs(x/f)<=1e-30)return h;let b=l(M,d);if(Math.abs(b)<=a)return h;let S=b/p;p=b,o=m(d,S,w),g=m(w,S,s),s=m(o,S,g)}return h}function C(e){let n=k(e,e);for(let t=0;t<e;t++)n.data[t+t*e]=1;return n}function et(e,n){let t=e.nrow,a=e.ncol,u=n.ncol,l=k(t,u);for(let m=0;m<u;m++)for(let h=0;h<t;h++){let d=0;for(let s=0;s<a;s++){let o=e.data[h+s*t],M=n.data[s+m*a];o===0&&Math.abs(M)===1/0||M===0&&Math.abs(o)===1/0||(d+=o*M)}l.data[h+m*t]=d}return l}function N(e,n){let t=e.nrow,a=e.ncol,u=n.ncol,l=k(t,u);for(let m=0;m<u;m++)for(let h=0;h<t;h++){let d=0;for(let s=0;s<a;s++)d+=e.data[h+s*t]*n.data[s+m*a];l.data[h+m*t]=d}return l}function O(e,n){return{nrow:e.nrow,ncol:e.ncol,data:e.data.map(t=>t*n)}}function R(e,n,t){for(let a=0;a<e.data.length;a++)e.data[a]=e.data[a]+n*t.data[a]}function _(e){let[n,t]=e[0],a=O(t,n);for(let u=1;u<e.length;u++)R(a,e[u][0],e[u][1]);return a}var Zt=["center","scale","rank","tol"],tn=22250738585072014e-324;function nn(e){let n=e.length,t=n===0?0:e[0].length,a=e.map(s=>{if(s.length!==t)throw new TypeError("all rows of the matrix must have the same length");if(!s.every(Number.isFinite))throw new RangeError("the matrix contains NaN or infinite values");return Array.from(s)}),u=Array.from({length:n},(s,o)=>o),l=Math.min(n,t);for(let s=0;s<l;s++){let o=s,M=Math.abs(a[s][s]);for(let i=s+1;i<n;i++){let r=Math.abs(a[i][s]);(r>M||Number.isNaN(M))&&(M=r,o=i)}o!==s&&([a[o],a[s]]=[a[s],a[o]],[u[o],u[s]]=[u[s],u[o]]);let f=a[s][s];if(f===0)continue;let p=Math.abs(f)>=tn?1/f:NaN;for(let i=s+1;i<n;i++){let r=Number.isNaN(p)?a[i][s]/f:a[i][s]*p;a[i][s]=r;for(let c=s+1;c<t;c++)a[i][c]=a[i][c]-r*a[s][c]}}let m=Array.from({length:n},(s,o)=>Array.from({length:l},(M,f)=>o===f?1:o>f?a[o][f]:0)),h=Array.from({length:l},(s,o)=>Array.from({length:t},(M,f)=>f>=o?a[o][f]:0)),d=Array.from({length:n},()=>new Array(n).fill(0));return u.forEach((s,o)=>d[s][o]=1),{P:d,L:m,U:h}}function on(e,n={}){T(n,Zt,"prcomp");let{rank:t,tol:a}=n;if(t!==void 0){if(!Number.isFinite(t))throw new RangeError("is.finite(rank.) is not TRUE");if(!(Math.trunc(t)>0&&Math.abs(t)<2**31))throw new RangeError("as.integer(rank.) > 0 is not TRUE")}let u=e.map(c=>Array.from(c)),l=n.center??!0,m=n.scale??!1,h=U(u,{center:l,scale:m});if(h.scale?.some(c=>c===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let d=u.length,s=c=>c.every(y=>y.every(Number.isFinite)),o=s(u)&&s(h.scaled)?P(h.scaled,{nu:0}):void 0,M=0;if(s(u)&&(o===void 0||o.d.some(c=>c===1/0))){let c=Math.floor(Math.log2(B(u.flat().map(Math.abs)))),y=g=>typeof g=="boolean"?g:g.map(x=>q(x,-c)),w=U(u.map(g=>g.map(x=>q(x,-c))),{center:y(l),scale:y(m)});o=P(w.scaled,{nu:0}),m===!1&&(M=c),h={scaled:w.scaled},w.center!==void 0&&(h.center=w.center.map(g=>q(g,c))),w.scale!==void 0&&(h.scale=w.scale.map(g=>q(g,c)))}o??=P(h.scaled,{nu:0});let f=o.d.map(c=>q(c/Math.sqrt(Math.max(1,d-1)),M)),p=t===void 0?f.length:Math.min(Math.trunc(t),f.length);if(Number.isNaN(a))throw new RangeError("'tol' must not be NaN");a!==void 0&&(p=Math.min(p,f.filter(c=>c>f[0]*a).length));let i=o.v.map(c=>c.slice(0,p)),r={sdev:f,rotation:i,x:L(h.scaled,o.v).map(c=>c.slice(0,p).map(y=>q(y,M)))};return h.center!==void 0&&(r.center=h.center),h.scale!==void 0&&(r.scale=h.scale),r}export{Et as add,Nt as backsolve,Tt as cbind,yt as chol,xt as chol2inv,qt as crossprod,gt as det,Mt as determinant,Ot as diag,Pt as divide,bt as eigen,zt as expm,kt as forwardsolve,Ut as ginv,Lt as kappa,_t as kronecker,nn as lu,L as matMul,jt as matrix,Ft as multiply,pt as norm,It as outer,on as prcomp,at as qr,it as qrCoef,ft as qrFitted,mt as qrQ,st as qrQty,ct as qrQy,ut as qrR,lt as qrResid,dt as qrSolve,ht as qrX,At as rbind,wt as rcond,Ct as schur,Ht as seq,H as solve,St as subtract,P as svd,Rt as t,vt as tcrossprod};
|