@numero-uno-jpn/rstats 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/README.ja.md +64 -12
- package/README.md +63 -11
- package/THIRD_PARTY_NOTICES.md +7 -2
- package/dist/chunks/chunk-5WDJPHYY.js +2 -0
- package/dist/chunks/chunk-7GQMKH3Z.js +2 -0
- package/dist/chunks/chunk-AHHS6AC4.js +3 -0
- package/dist/chunks/chunk-ALM3VHFP.js +2 -0
- package/dist/chunks/{chunk-TOVTN5Y2.js → chunk-C5XTWMGB.js} +1 -1
- package/dist/chunks/chunk-CGBNKHNZ.js +2 -0
- package/dist/chunks/{chunk-LWUSYIQ4.js → chunk-EZAFN5CD.js} +1 -1
- package/dist/chunks/{chunk-H2AP6TCR.js → chunk-EZWBRCJL.js} +1 -1
- package/dist/chunks/chunk-F5EDJFEO.js +2 -0
- package/dist/chunks/{chunk-FC2TLHZC.js → chunk-JWBO5WVN.js} +2 -2
- package/dist/chunks/{chunk-LE7TBHNI.js → chunk-TOGXTCZO.js} +2 -2
- package/dist/chunks/chunk-VJ4BDBMU.js +3 -0
- package/dist/chunks/chunk-Y5CNQ266.js +2 -0
- package/dist/chunks/{chunk-BGVMSDVJ.js → chunk-Y5PYRAYP.js} +2 -2
- package/dist/chunks/{chunk-5K4PS7IV.js → chunk-YSWGDOWK.js} +2 -2
- package/dist/data/grouping.d.ts +79 -0
- package/dist/data/grouping.js +262 -0
- package/dist/descriptive/dataFrames.d.ts +110 -0
- package/dist/descriptive/dataFrames.js +392 -0
- package/dist/descriptive/factanal.d.ts +56 -0
- package/dist/descriptive/factanal.js +186 -0
- package/dist/descriptive/missing.d.ts +35 -0
- package/dist/descriptive/missing.js +50 -0
- package/dist/descriptive/omega.d.ts +56 -0
- package/dist/descriptive/omega.js +323 -0
- package/dist/descriptive/pivot.d.ts +89 -0
- package/dist/descriptive/pivot.js +343 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions/bvnorm.d.ts +15 -0
- package/dist/distributions/bvnorm.js +131 -0
- package/dist/distributions/nbeta.js +34 -1
- package/dist/distributions/nchisq.d.ts +10 -0
- package/dist/distributions/nchisq.js +46 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +4 -0
- package/dist/entries/descriptive.js +4 -0
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +1 -0
- package/dist/entries/linalg.d.ts +2 -0
- package/dist/entries/linalg.js +2 -0
- package/dist/entries/numerics.d.ts +2 -0
- package/dist/entries/numerics.js +1 -0
- package/dist/entries/tests.d.ts +22 -1
- package/dist/entries/tests.js +19 -1
- package/dist/htest/agreement.d.ts +141 -0
- package/dist/htest/agreement.js +274 -0
- package/dist/htest/ansariTest.d.ts +12 -0
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/aov.d.ts +19 -0
- package/dist/htest/aov.js +22 -15
- package/dist/htest/cor.d.ts +12 -0
- package/dist/htest/cor.js +9 -27
- package/dist/htest/core/simulate.d.ts +2 -0
- package/dist/htest/core/simulate.js +1 -1
- package/dist/htest/dunnett.d.ts +5 -0
- package/dist/htest/dunnett.js +89 -0
- package/dist/htest/effectSize.d.ts +112 -0
- package/dist/htest/effectSize.js +385 -0
- package/dist/htest/ksTest.d.ts +13 -0
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/leveneTest.d.ts +30 -0
- package/dist/htest/leveneTest.js +41 -0
- package/dist/htest/multipleComparisons.d.ts +147 -0
- package/dist/htest/multipleComparisons.js +369 -0
- package/dist/htest/normalityTests.d.ts +44 -0
- package/dist/htest/normalityTests.js +164 -0
- package/dist/htest/onewayTest.d.ts +14 -0
- package/dist/htest/onewayTest.js +5 -7
- package/dist/htest/polycor.d.ts +117 -0
- package/dist/htest/polycor.js +468 -0
- package/dist/htest/power.d.ts +132 -0
- package/dist/htest/power.js +227 -0
- package/dist/htest/pwr.d.ts +263 -0
- package/dist/htest/pwr.js +440 -0
- package/dist/htest/rankTests.d.ts +64 -0
- package/dist/htest/rankTests.js +27 -29
- package/dist/htest/tTest.d.ts +20 -0
- package/dist/htest/tTest.js +19 -24
- package/dist/htest/testFormula.d.ts +57 -0
- package/dist/htest/testFormula.js +171 -0
- package/dist/htest/trendTests.d.ts +57 -0
- package/dist/htest/trendTests.js +167 -0
- package/dist/htest/varTest.d.ts +23 -0
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +14 -0
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/linalg/eigen.d.ts +8 -0
- package/dist/linalg/eigen.js +5 -4
- package/dist/linalg/expm.d.ts +48 -0
- package/dist/linalg/expm.js +472 -0
- package/dist/linalg/ops.d.ts +13 -2
- package/dist/linalg/ops.js +20 -5
- package/dist/linalg/schur.d.ts +48 -0
- package/dist/linalg/schur.js +225 -0
- package/dist/linalg.js +2 -2
- package/dist/models/anovaSequential.d.ts +58 -0
- package/dist/models/anovaSequential.js +138 -0
- package/dist/models/aovEz.d.ts +64 -0
- package/dist/models/aovEz.js +332 -0
- package/dist/models/aovStrata.d.ts +32 -0
- package/dist/models/aovStrata.js +104 -0
- package/dist/models/carAnova.d.ts +30 -0
- package/dist/models/carAnova.js +121 -0
- package/dist/models/coxph.d.ts +101 -0
- package/dist/models/coxph.js +502 -0
- package/dist/models/ddLeastSquares.d.ts +30 -0
- package/dist/models/ddLeastSquares.js +90 -0
- package/dist/models/ddMatrix.d.ts +10 -0
- package/dist/models/ddMatrix.js +91 -0
- package/dist/models/formula.d.ts +83 -0
- package/dist/models/formula.js +333 -0
- package/dist/models/lm.d.ts +84 -3
- package/dist/models/lm.js +141 -44
- package/dist/models/mauchly.d.ts +48 -0
- package/dist/models/mauchly.js +123 -0
- package/dist/models/modelFrame.d.ts +40 -0
- package/dist/models/modelFrame.js +131 -0
- package/dist/models/modelMatrix.d.ts +88 -0
- package/dist/models/modelMatrix.js +170 -0
- package/dist/models/survival.d.ts +87 -0
- package/dist/models/survival.js +238 -0
- package/dist/numerics.js +2 -2
- package/dist/optim/lbfgsb.d.ts +20 -0
- package/dist/optim/lbfgsb.js +1240 -0
- package/dist/optim/optim.d.ts +85 -0
- package/dist/optim/optim.js +610 -0
- package/dist/rstats.js +19 -4
- package/dist/special.js +2 -2
- package/dist/tests.js +17 -4
- package/llms.txt +114 -0
- package/package.json +3 -2
- package/dist/chunks/chunk-24U4DKRY.js +0 -2
- package/dist/chunks/chunk-IVOBWYLV.js +0 -2
- package/dist/chunks/chunk-JLSTEMSN.js +0 -2
- package/dist/chunks/chunk-MF4VBCHQ.js +0 -2
- package/dist/chunks/chunk-MSAUU6PY.js +0 -2
- package/dist/chunks/chunk-YC4JWUKG.js +0 -2
- package/dist/chunks/chunk-ZRO446BP.js +0 -2
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type MatrixInput } from "./matrix.js";
|
|
2
|
+
/** Result of {@link schur}: `x = Q T Q'`. */
|
|
3
|
+
export interface SchurResult {
|
|
4
|
+
/** The upper quasi-triangular factor (array of rows). */
|
|
5
|
+
T: number[][];
|
|
6
|
+
/** The orthogonal factor (array of rows). */
|
|
7
|
+
Q: number[][];
|
|
8
|
+
/** Real parts of the eigenvalues, in the order of T's diagonal blocks. */
|
|
9
|
+
valuesRe: number[];
|
|
10
|
+
/** Imaginary parts of the eigenvalues (0 for real ones; a complex pair has the positive one first). */
|
|
11
|
+
valuesIm: number[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Real Schur decomposition of a square matrix. Equivalent to R's `Matrix::Schur(x)`.
|
|
15
|
+
*
|
|
16
|
+
* Returns an orthogonal `Q` and an upper quasi-triangular `T` with `x = Q T Q'`. `T` has 1 x 1 diagonal blocks for
|
|
17
|
+
* real eigenvalues and standardised 2 x 2 blocks `[[a, b], [c, a]]` (`b c < 0`) for complex conjugate pairs
|
|
18
|
+
* `a ± sqrt(-b c) i`. The decomposition is not unique: the order of the diagonal blocks and the signs of `Q`'s
|
|
19
|
+
* columns may differ from R's (both are correct). Throws on `NaN` or infinite values.
|
|
20
|
+
*
|
|
21
|
+
* @param x A square matrix (array of rows).
|
|
22
|
+
* @returns `T`, `Q` and the eigenvalues read off `T`. See {@link SchurResult}.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* schur([[0, -1], [1, 0]]);
|
|
27
|
+
* // T: [[0, -1], [1, 0]], Q: [[1, 0], [0, 1]], valuesRe: [0, 0], valuesIm: [1, -1]
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function schur(x: MatrixInput): SchurResult;
|
|
31
|
+
/**
|
|
32
|
+
* Schur factorisation of a real 2 x 2 matrix in standard form (dlanv2):
|
|
33
|
+
* [[a, b], [c, d]] = [[cs, -sn], [sn, cs]] [[aa, bb], [cc, dd]] [[cs, sn], [-sn, cs]], where either cc = 0 (real
|
|
34
|
+
* eigenvalues aa, dd) or aa = dd and bb cc < 0 (complex pair). Returns the standardised entries, the rotation and the
|
|
35
|
+
* eigenvalues
|
|
36
|
+
*/
|
|
37
|
+
export declare function lanv2(a: number, b: number, c: number, d: number): {
|
|
38
|
+
a: number;
|
|
39
|
+
b: number;
|
|
40
|
+
c: number;
|
|
41
|
+
d: number;
|
|
42
|
+
cs: number;
|
|
43
|
+
sn: number;
|
|
44
|
+
rt1r: number;
|
|
45
|
+
rt1i: number;
|
|
46
|
+
rt2r: number;
|
|
47
|
+
rt2i: number;
|
|
48
|
+
};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// Real Schur decomposition (Matrix's Schur, which calls LAPACK's dgees without sorting). Written for rStats.js.
|
|
2
|
+
//
|
|
3
|
+
// Sources: the specification oracle/matfn-behaviour.md; the Hessenberg reduction and Francis double-shift QR iteration
|
|
4
|
+
// of src/linalg/eigen.ts (EISPACK's orthes and hqr2 via JAMA, public domain), stopped at the real Schur form; the
|
|
5
|
+
// standardisation of the 2 x 2 diagonal blocks is a port of LAPACK 3.12.0's SRC/dlanv2.f (modified BSD; see
|
|
6
|
+
// THIRD_PARTY_NOTICES.md).
|
|
7
|
+
// - x = Q T Q' with Q orthogonal and T upper quasi-triangular: 1 x 1 blocks for real eigenvalues, 2 x 2 blocks
|
|
8
|
+
// [[a, b], [c, a]] with b c < 0 for complex pairs. Entries below the block diagonal are exactly 0
|
|
9
|
+
// - The eigenvalues are read off T's diagonal blocks in order (a complex pair as a ± sqrt(|b|) sqrt(|c|) i, positive
|
|
10
|
+
// imaginary part first)
|
|
11
|
+
// - The decomposition is not unique; the order of the blocks and the signs of Q's columns may differ from LAPACK's
|
|
12
|
+
// (no balancing, a different QR iteration)
|
|
13
|
+
// - NaN or Inf is an error, as in eigen
|
|
14
|
+
import { orthes, hqr2 } from "./eigen.js";
|
|
15
|
+
import { fromMat, newMat, requireSquare, toMat } from "./matrix.js";
|
|
16
|
+
/**
|
|
17
|
+
* Real Schur decomposition of a square matrix. Equivalent to R's `Matrix::Schur(x)`.
|
|
18
|
+
*
|
|
19
|
+
* Returns an orthogonal `Q` and an upper quasi-triangular `T` with `x = Q T Q'`. `T` has 1 x 1 diagonal blocks for
|
|
20
|
+
* real eigenvalues and standardised 2 x 2 blocks `[[a, b], [c, a]]` (`b c < 0`) for complex conjugate pairs
|
|
21
|
+
* `a ± sqrt(-b c) i`. The decomposition is not unique: the order of the diagonal blocks and the signs of `Q`'s
|
|
22
|
+
* columns may differ from R's (both are correct). Throws on `NaN` or infinite values.
|
|
23
|
+
*
|
|
24
|
+
* @param x A square matrix (array of rows).
|
|
25
|
+
* @returns `T`, `Q` and the eigenvalues read off `T`. See {@link SchurResult}.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* schur([[0, -1], [1, 0]]);
|
|
30
|
+
* // T: [[0, -1], [1, 0]], Q: [[1, 0], [0, 1]], valuesRe: [0, 0], valuesIm: [1, -1]
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function schur(x) {
|
|
34
|
+
const A = toMat(x);
|
|
35
|
+
requireSquare(A);
|
|
36
|
+
const n = A.nrow;
|
|
37
|
+
if (n === 0)
|
|
38
|
+
throw new RangeError("the matrix has zero dimension");
|
|
39
|
+
for (const v of A.data)
|
|
40
|
+
if (!Number.isFinite(v))
|
|
41
|
+
throw new RangeError("the matrix contains NaN or infinite values");
|
|
42
|
+
const H = { nrow: n, ncol: n, data: A.data.slice() };
|
|
43
|
+
const V = newMat(n, n);
|
|
44
|
+
const d = new Float64Array(n);
|
|
45
|
+
const e = new Float64Array(n);
|
|
46
|
+
orthes(H, V);
|
|
47
|
+
hqr2(H, V, d, e, true);
|
|
48
|
+
const h = H.data;
|
|
49
|
+
const v = V.data;
|
|
50
|
+
const at = (i, j) => i + j * n;
|
|
51
|
+
// The 2 x 2 blocks are where hqr2 found complex pairs (e > 0 at the first position); everything else below the
|
|
52
|
+
// diagonal is negligible (deflated subdiagonal elements) or left over from the reduction, and is set to 0
|
|
53
|
+
const block = new Uint8Array(n);
|
|
54
|
+
for (let j = 0; j + 1 < n; j++)
|
|
55
|
+
if (e[j] > 0 && e[j + 1] < 0)
|
|
56
|
+
block[j++] = 1;
|
|
57
|
+
for (let j = 0; j < n; j++)
|
|
58
|
+
for (let i = j + 1; i < n; i++)
|
|
59
|
+
if (!(i === j + 1 && block[j]))
|
|
60
|
+
h[at(i, j)] = 0;
|
|
61
|
+
const valuesRe = new Array(n);
|
|
62
|
+
const valuesIm = new Array(n);
|
|
63
|
+
for (let j = 0; j < n; j++) {
|
|
64
|
+
if (!block[j]) {
|
|
65
|
+
valuesRe[j] = h[at(j, j)];
|
|
66
|
+
valuesIm[j] = 0;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const s = lanv2(h[at(j, j)], h[at(j, j + 1)], h[at(j + 1, j)], h[at(j + 1, j + 1)]);
|
|
70
|
+
h[at(j, j)] = s.a;
|
|
71
|
+
h[at(j, j + 1)] = s.b;
|
|
72
|
+
h[at(j + 1, j)] = s.c;
|
|
73
|
+
h[at(j + 1, j + 1)] = s.d;
|
|
74
|
+
// T <- R' T R and Q <- Q R with R = [[cs, -sn], [sn, cs]] acting on positions j, j + 1
|
|
75
|
+
const { cs, sn } = s;
|
|
76
|
+
for (let k = j + 2; k < n; k++) {
|
|
77
|
+
const p = h[at(j, k)];
|
|
78
|
+
const q = h[at(j + 1, k)];
|
|
79
|
+
h[at(j, k)] = cs * p + sn * q;
|
|
80
|
+
h[at(j + 1, k)] = cs * q - sn * p;
|
|
81
|
+
}
|
|
82
|
+
for (let k = 0; k < j; k++) {
|
|
83
|
+
const p = h[at(k, j)];
|
|
84
|
+
const q = h[at(k, j + 1)];
|
|
85
|
+
h[at(k, j)] = cs * p + sn * q;
|
|
86
|
+
h[at(k, j + 1)] = cs * q - sn * p;
|
|
87
|
+
}
|
|
88
|
+
for (let k = 0; k < n; k++) {
|
|
89
|
+
const p = v[at(k, j)];
|
|
90
|
+
const q = v[at(k, j + 1)];
|
|
91
|
+
v[at(k, j)] = cs * p + sn * q;
|
|
92
|
+
v[at(k, j + 1)] = cs * q - sn * p;
|
|
93
|
+
}
|
|
94
|
+
valuesRe[j] = s.rt1r;
|
|
95
|
+
valuesIm[j] = s.rt1i;
|
|
96
|
+
valuesRe[j + 1] = s.rt2r;
|
|
97
|
+
valuesIm[j + 1] = s.rt2i;
|
|
98
|
+
j++;
|
|
99
|
+
}
|
|
100
|
+
return { T: fromMat(H), Q: fromMat(V), valuesRe, valuesIm };
|
|
101
|
+
}
|
|
102
|
+
const sign = (a, b) => (b >= 0 && !Object.is(b, -0) ? Math.abs(a) : -Math.abs(a));
|
|
103
|
+
/**
|
|
104
|
+
* Schur factorisation of a real 2 x 2 matrix in standard form (dlanv2):
|
|
105
|
+
* [[a, b], [c, d]] = [[cs, -sn], [sn, cs]] [[aa, bb], [cc, dd]] [[cs, sn], [-sn, cs]], where either cc = 0 (real
|
|
106
|
+
* eigenvalues aa, dd) or aa = dd and bb cc < 0 (complex pair). Returns the standardised entries, the rotation and the
|
|
107
|
+
* eigenvalues
|
|
108
|
+
*/
|
|
109
|
+
export function lanv2(a, b, c, d) {
|
|
110
|
+
const SAFMIN = 2 ** -1022;
|
|
111
|
+
const EPS = Number.EPSILON; // DLAMCH('P')
|
|
112
|
+
const MULTPL = 4;
|
|
113
|
+
// DLAMCH('B')**INT(LOG(SAFMIN / EPS) / LOG(2) / 2)
|
|
114
|
+
const SAFMN2 = 2 ** Math.trunc(Math.log(SAFMIN / EPS) / Math.log(2) / 2);
|
|
115
|
+
const SAFMX2 = 1 / SAFMN2;
|
|
116
|
+
let cs;
|
|
117
|
+
let sn;
|
|
118
|
+
if (c === 0) {
|
|
119
|
+
cs = 1;
|
|
120
|
+
sn = 0;
|
|
121
|
+
}
|
|
122
|
+
else if (b === 0) {
|
|
123
|
+
// Swap rows and columns
|
|
124
|
+
cs = 0;
|
|
125
|
+
sn = 1;
|
|
126
|
+
const temp = d;
|
|
127
|
+
d = a;
|
|
128
|
+
a = temp;
|
|
129
|
+
b = -c;
|
|
130
|
+
c = 0;
|
|
131
|
+
}
|
|
132
|
+
else if (a - d === 0 && sign(1, b) !== sign(1, c)) {
|
|
133
|
+
cs = 1;
|
|
134
|
+
sn = 0;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
let temp = a - d;
|
|
138
|
+
let p = 0.5 * temp;
|
|
139
|
+
const bcmax = Math.max(Math.abs(b), Math.abs(c));
|
|
140
|
+
const bcmis = Math.min(Math.abs(b), Math.abs(c)) * sign(1, b) * sign(1, c);
|
|
141
|
+
let scale = Math.max(Math.abs(p), bcmax);
|
|
142
|
+
let z = (p / scale) * p + (bcmax / scale) * bcmis;
|
|
143
|
+
// If z is of the order of the machine accuracy, postpone the decision on the nature of the eigenvalues
|
|
144
|
+
if (z >= MULTPL * EPS) {
|
|
145
|
+
// Real eigenvalues: compute a and d
|
|
146
|
+
z = p + sign(Math.sqrt(scale) * Math.sqrt(z), p);
|
|
147
|
+
a = d + z;
|
|
148
|
+
d = d - (bcmax / z) * bcmis;
|
|
149
|
+
// Compute b and the rotation
|
|
150
|
+
const tau = Math.hypot(c, z);
|
|
151
|
+
cs = z / tau;
|
|
152
|
+
sn = c / tau;
|
|
153
|
+
b = b - c;
|
|
154
|
+
c = 0;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// Complex eigenvalues, or real (almost) equal eigenvalues: make the diagonal elements equal
|
|
158
|
+
let count = 0;
|
|
159
|
+
let sigma = b + c;
|
|
160
|
+
for (;;) {
|
|
161
|
+
count++;
|
|
162
|
+
scale = Math.max(Math.abs(temp), Math.abs(sigma));
|
|
163
|
+
if (scale >= SAFMX2) {
|
|
164
|
+
sigma *= SAFMN2;
|
|
165
|
+
temp *= SAFMN2;
|
|
166
|
+
if (count <= 20)
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (scale <= SAFMN2) {
|
|
170
|
+
sigma *= SAFMX2;
|
|
171
|
+
temp *= SAFMX2;
|
|
172
|
+
if (count <= 20)
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
p = 0.5 * temp;
|
|
178
|
+
let tau = Math.hypot(sigma, temp);
|
|
179
|
+
cs = Math.sqrt(0.5 * (1 + Math.abs(sigma) / tau));
|
|
180
|
+
sn = -(p / (tau * cs)) * sign(1, sigma);
|
|
181
|
+
// [aa bb; cc dd] = [a b; c d] [cs -sn; sn cs]
|
|
182
|
+
const aa = a * cs + b * sn;
|
|
183
|
+
const bb = -a * sn + b * cs;
|
|
184
|
+
const cc = c * cs + d * sn;
|
|
185
|
+
const dd = -c * sn + d * cs;
|
|
186
|
+
// [a b; c d] = [cs sn; -sn cs] [aa bb; cc dd]
|
|
187
|
+
a = aa * cs + cc * sn;
|
|
188
|
+
b = bb * cs + dd * sn;
|
|
189
|
+
c = -aa * sn + cc * cs;
|
|
190
|
+
d = -bb * sn + dd * cs;
|
|
191
|
+
temp = 0.5 * (a + d);
|
|
192
|
+
a = temp;
|
|
193
|
+
d = temp;
|
|
194
|
+
if (c !== 0) {
|
|
195
|
+
if (b !== 0) {
|
|
196
|
+
if (sign(1, b) === sign(1, c)) {
|
|
197
|
+
// Real eigenvalues: reduce to upper triangular form
|
|
198
|
+
const sab = Math.sqrt(Math.abs(b));
|
|
199
|
+
const sac = Math.sqrt(Math.abs(c));
|
|
200
|
+
p = sign(sab * sac, c);
|
|
201
|
+
tau = 1 / Math.sqrt(Math.abs(b + c));
|
|
202
|
+
a = temp + p;
|
|
203
|
+
d = temp - p;
|
|
204
|
+
b = b - c;
|
|
205
|
+
c = 0;
|
|
206
|
+
const cs1 = sab * tau;
|
|
207
|
+
const sn1 = sac * tau;
|
|
208
|
+
temp = cs * cs1 - sn * sn1;
|
|
209
|
+
sn = cs * sn1 + sn * cs1;
|
|
210
|
+
cs = temp;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
b = -c;
|
|
215
|
+
c = 0;
|
|
216
|
+
temp = cs;
|
|
217
|
+
cs = -sn;
|
|
218
|
+
sn = temp;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const rt1i = c === 0 ? 0 : Math.sqrt(Math.abs(b)) * Math.sqrt(Math.abs(c));
|
|
224
|
+
return { a, b, c, d, cs, sn, rt1r: a, rt1i, rt2r: d, rt2i: -rt1i };
|
|
225
|
+
}
|
package/dist/linalg.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 0.
|
|
2
|
-
import{a as x,b as K,c as $,d as P,e as I,f as T,g as W,h as Q,i as ot,j as at,k as st,l as lt,m as ft,n as it,o as C,p as ct,q as ht,r as L,s as U,t as X,u as ut,v as mt,w as D,x as bt,y as wt}from"./chunks/chunk-JLSTEMSN.js";import{u as Y}from"./chunks/chunk-YC4JWUKG.js";import"./chunks/chunk-BGVMSDVJ.js";import"./chunks/chunk-H2AP6TCR.js";function dt(i,s){let e=P(i),l=s===void 0?e:P(s);if(e.nrow!==l.nrow)throw new TypeError(`non-conformable arguments (${e.nrow} and ${l.nrow} rows)`);let c=e.nrow,n=x(e.ncol,l.ncol);for(let a=0;a<l.ncol;a++)for(let r=0;r<e.ncol;r++){if(s===void 0&&r>a)continue;let t=0;for(let o=0;o<c;o++)t+=e.data[o+r*c]*l.data[o+a*c];n.data[r+a*e.ncol]=t}return s===void 0&&Z(n),I(n)}function Mt(i,s){let e=P(i),l=s===void 0?e:P(s);if(e.ncol!==l.ncol)throw new TypeError(`non-conformable arguments (${e.ncol} and ${l.ncol} columns)`);let c=x(e.nrow,l.nrow);for(let n=0;n<l.nrow;n++)for(let a=0;a<e.nrow;a++){if(s===void 0&&a>n)continue;let r=0;for(let t=0;t<e.ncol;t++)r+=e.data[a+t*e.nrow]*l.data[n+t*l.nrow];c.data[a+n*e.nrow]=r}return s===void 0&&Z(c),I(c)}function Z(i){let s=i.nrow;for(let e=0;e<s;e++)for(let l=e+1;l<s;l++)i.data[l+e*s]=i.data[e+l*s]}function gt(i,s,e={}){return B(i,s,e.k,e.upperTri??!0,e.transpose??!1)}function pt(i,s,e={}){return B(i,s,e.k,e.upperTri??!1,e.transpose??!1)}function B(i,s,e,l,c){let n=$(i),a=P(s),r=e??n.ncol;if(!Number.isInteger(r)||r<1||r>n.nrow||r>n.ncol||r>a.nrow)throw new RangeError(`invalid k (got ${r})`);for(let o=0;o<r;o++)if(n.data[o+o*n.nrow]===0)throw new Error(`singular matrix: diagonal element ${o+1} is zero`);let t=x(r,a.ncol);for(let o=0;o<a.ncol;o++)for(let u=0;u<r;u++)t.data[u+o*r]=a.data[u+o*a.nrow];return vt(n,r,t,l,c),K(s)?I(t):Array.from(t.data)}function vt(i,s,e,l,c){let n=i.nrow,a=i.data,r=e.nrow,t=e.data;for(let o=0;o<e.ncol;o++){let u=o*r;if(!c&&l){for(let w=s-1;w>=0;w--)if(t[u+w]!==0){t[u+w]/=a[w+w*n];let h=t[u+w];for(let f=0;f<w;f++)t[u+f]-=h*a[f+w*n]}}else if(c)if(l)for(let w=0;w<s;w++){let h=t[u+w];for(let f=0;f<w;f++)h-=a[f+w*n]*t[u+f];t[u+w]=h/a[w+w*n]}else for(let w=s-1;w>=0;w--){let h=t[u+w];for(let f=w+1;f<s;f++)h-=a[f+w*n]*t[u+f];t[u+w]=h/a[w+w*n]}else for(let w=0;w<s;w++)if(t[u+w]!==0){t[u+w]/=a[w+w*n];let h=t[u+w];for(let f=w+1;f<s;f++)t[u+f]-=h*a[f+w*n]}}}function kt(i,s={}){let e=s.norm===void 0?s.exact?"2":"O":yt(s.norm);if(s.exact){if(e==="2"){let n=L(i,{nu:0,nv:0}).d,a=Math.min(...n);return a===0?1/0:Math.max(...n)/a}return U(i,{type:e})*U(X(i),{type:e})}if(e!=="O"&&e!=="I")throw new RangeError(`norm must be "1", "O" or "I" when exact is false (got "${s.norm}")`);if((s.method??"qr")==="direct")return s.triangular?1/tt($(i),(s.uplo??"U")==="U",e):1/D(i,{norm:e});let l=$(i),c=l.nrow<l.ncol?I(T(l)):i;return 1/tt($(C(Q(c))),!0,e)}function tt(i,s,e){return Et(e==="I"?T(i):i,s)}function yt(i){let s=i.toUpperCase();if(s==="1"||s==="O")return"O";if(s==="E")return"F";if(s==="I"||s==="F"||s==="M"||s==="2")return s;throw new RangeError(`invalid norm "${i}"`)}function Et(i,s){let e=Math.min(i.nrow,i.ncol),l=i.nrow,c=(h,f)=>i.data[h+f*l],n=!s,a=0;for(let h=0;h<e;h++){let[f,d]=n?[h,e-h]:[0,h+1],b=0;for(let M=f;M<f+d;M++)b+=Math.abs(c(M,h));a=Math.max(a,b)}let r=1,t=new Float64Array(e),o=h=>{for(let f=0;f<e;f++)t[f]*=h};for(let h=0;h<e;h++){let f=n?e-1-h:h,d=c(f,f);if(t[f]!==0&&(r=t[f]>0?-Math.abs(r):Math.abs(r)),Math.abs(r-t[f])>Math.abs(d)){let j=Math.abs(d)/Math.abs(r-t[f]);o(j),r*=j}let b=r-t[f],M=-r-t[f],p=Math.abs(b),A=Math.abs(M);if(d!==0?(b/=d,M/=d):(b=1,M=1),h!==e-1){let[j,k]=n?[0,f-1]:[f+1,e-1];for(let y=j;y<=k;y++)A+=Math.abs(t[y]+M*c(f,y)),t[y]+=b*c(f,y),p+=Math.abs(t[y]);if(p<A){let y=M-b;b=M;for(let _=j;_<=k;_++)t[_]+=y*c(f,_)}}t[f]=b}o(1/nt(t));let u=1;for(let h=0;h<e;h++){let f=n?h:e-1-h,d=c(f,f);if(Math.abs(t[f])>Math.abs(d)){let b=Math.abs(d)/Math.abs(t[f]);o(b),u*=b}if(d!==0?t[f]/=d:t[f]=1,h<e-1){let b=-t[f];if(n)for(let M=f+1;M<e;M++)t[M]+=b*c(M,f);else for(let M=0;M<f;M++)t[M]+=b*c(M,f)}}let w=1/nt(t);return o(w),u*=w,a!==0?u/a:0}function nt(i){let s=0;for(let e of i)s+=Math.abs(e);return s}function jt(i,s={}){let e=s.tol??Math.sqrt(Number.EPSILON),l=$(i),{d:c,u:n,v:a}=L(i),r=l.nrow,t=l.ncol,o=x(t,r),u=Math.max(e*(c[0]??0),0);for(let w=0;w<c.length;w++){if(!(c[w]>u))continue;let h=1/c[w];for(let f=0;f<r;f++){let d=n[f][w]*h;for(let b=0;b<t;b++)o.data[b+f*t]+=a[b][w]*d}}return I(o)}function At(i,s={}){let e=$(i);if(W(e),e.nrow===0)throw new RangeError("the matrix has zero dimension");for(let n of e.data)if(!Number.isFinite(n))throw new RangeError("the matrix contains NaN or infinite values");let l=s.symmetric??Nt(e),c=!(s.onlyValues??!1);return l?qt(e,c):_t(e,c)}function Nt(i){let s=i.nrow,e=0,l=0;for(let t=0;t<s;t++)for(let o=0;o<s;o++)e+=Math.abs(i.data[o+t*s]-i.data[t+o*s]),l+=Math.abs(i.data[o+t*s]);let c=100*Number.EPSILON,n=s*s,a=l/n,r=e/n;return(Number.isFinite(a)&&a>c?r/a:r)<=c}function qt(i,s){let e=i.nrow,l=x(e,e);for(let t=0;t<e;t++)for(let o=t;o<e;o++)l.data[o+t*e]=i.data[o+t*e],l.data[t+o*e]=i.data[o+t*e];let c=new Float64Array(e),n=new Float64Array(e);xt(l,c,n),Rt(l,c,n);let a=Array.from(c).reverse();if(!s)return{values:a};let r=x(e,e);for(let t=0;t<e;t++)r.data.set(l.data.subarray((e-1-t)*e,(e-t)*e),t*e);return{values:a,vectors:I(r)}}function xt(i,s,e){let l=i.nrow,c=i.data,n=(a,r)=>a+r*l;for(let a=0;a<l;a++)s[a]=c[n(l-1,a)];for(let a=l-1;a>0;a--){let r=0,t=0;for(let o=0;o<a;o++)r+=Math.abs(s[o]);if(r===0){e[a]=s[a-1];for(let o=0;o<a;o++)s[o]=c[n(a-1,o)],c[n(a,o)]=0,c[n(o,a)]=0}else{for(let h=0;h<a;h++)s[h]/=r,t+=s[h]*s[h];let o=s[a-1],u=Math.sqrt(t);o>0&&(u=-u),e[a]=r*u,t-=o*u,s[a-1]=o-u;for(let h=0;h<a;h++)e[h]=0;for(let h=0;h<a;h++){o=s[h],c[n(h,a)]=o,u=e[h]+c[n(h,h)]*o;for(let f=h+1;f<=a-1;f++)u+=c[n(f,h)]*s[f],e[f]+=c[n(f,h)]*o;e[h]=u}o=0;for(let h=0;h<a;h++)e[h]/=t,o+=e[h]*s[h];let w=o/(t+t);for(let h=0;h<a;h++)e[h]-=w*s[h];for(let h=0;h<a;h++){o=s[h],u=e[h];for(let f=h;f<=a-1;f++)c[n(f,h)]-=o*e[f]+u*s[f];s[h]=c[n(a-1,h)],c[n(a,h)]=0}}s[a]=t}for(let a=0;a<l-1;a++){c[n(l-1,a)]=c[n(a,a)],c[n(a,a)]=1;let r=s[a+1];if(r!==0){for(let t=0;t<=a;t++)s[t]=c[n(t,a+1)]/r;for(let t=0;t<=a;t++){let o=0;for(let u=0;u<=a;u++)o+=c[n(u,a+1)]*c[n(u,t)];for(let u=0;u<=a;u++)c[n(u,t)]-=o*s[u]}}for(let t=0;t<=a;t++)c[n(t,a+1)]=0}for(let a=0;a<l;a++)s[a]=c[n(l-1,a)],c[n(l-1,a)]=0;c[n(l-1,l-1)]=1,e[0]=0}function Rt(i,s,e){let l=i.nrow,c=i.data,n=(o,u)=>o+u*l;for(let o=1;o<l;o++)e[o-1]=e[o];e[l-1]=0;let a=0,r=0,t=Number.EPSILON;for(let o=0;o<l;o++){r=Math.max(r,Math.abs(s[o])+Math.abs(e[o]));let u=o;for(;u<l-1&&Math.abs(e[u])>t*r;)u++;if(u>o){let w=0;do{if(++w>100)throw new Error("the eigendecomposition did not converge");let h=s[o],f=(s[o+1]-h)/(2*e[o]),d=Math.hypot(f,1);f<0&&(d=-d),s[o]=e[o]/(f+d),s[o+1]=e[o]*(f+d);let b=s[o+1],M=h-s[o];for(let N=o+2;N<l;N++)s[N]-=M;a+=M,f=s[u];let p=1,A=p,j=p,k=e[o+1],y=0,_=0;for(let N=u-1;N>=o;N--){j=A,A=p,_=y,h=p*e[N],M=p*f,d=Math.hypot(f,e[N]),e[N+1]=y*d,y=e[N]/d,p=f/d,f=p*s[N]-y*h,s[N+1]=M+y*(p*h+y*s[N]);for(let S=0;S<l;S++)M=c[n(S,N+1)],c[n(S,N+1)]=y*c[n(S,N)]+p*M,c[n(S,N)]=p*c[n(S,N)]-y*M}f=-y*_*j*k*e[o]/b,e[o]=y*f,s[o]=p*f}while(Math.abs(e[o])>t*r)}s[o]=s[o]+a,e[o]=0}for(let o=0;o<l-1;o++){let u=o,w=s[o];for(let h=o+1;h<l;h++)s[h]<w&&(u=h,w=s[h]);if(u!==o){s[u]=s[o],s[o]=w;for(let h=0;h<l;h++){let f=c[n(h,o)];c[n(h,o)]=c[n(h,u)],c[n(h,u)]=f}}}}function _t(i,s){let e=i.nrow,l={nrow:e,ncol:e,data:i.data.slice()},c=x(e,e),n=new Float64Array(e),a=new Float64Array(e);It(l,c),Ot(l,c,n,a);let r=Array.from(n,(b,M)=>Math.hypot(b,a[M])),t=Array.from({length:e},(b,M)=>M).sort((b,M)=>r[M]-r[b]||b-M),o=a.some(b=>b!==0),u={values:t.map(b=>n[b])};if(o&&(u.valuesIm=t.map(b=>a[b])),!s)return u;let w=x(e,e),h=x(e,e);for(let b=0;b<e;b++){let M=c.data.subarray(b*e,(b+1)*e);if(a[b]===0)w.data.set(M,b*e);else if(a[b]>0){let p=c.data.subarray((b+1)*e,(b+2)*e);w.data.set(M,b*e),h.data.set(p,b*e),w.data.set(M,(b+1)*e);for(let A=0;A<e;A++)h.data[A+(b+1)*e]=-p[A]}}Ft(w,h);let f=x(e,e),d=x(e,e);return t.forEach((b,M)=>{f.data.set(w.data.subarray(b*e,(b+1)*e),M*e),d.data.set(h.data.subarray(b*e,(b+1)*e),M*e)}),u.vectors=I(f),o&&(u.vectorsIm=I(d)),u}function Ft(i,s){let e=i.nrow;for(let l=0;l<e;l++){let c=l*e,n=0,a=0,r=-1;for(let w=0;w<e;w++){let h=Math.hypot(i.data[c+w],s.data[c+w]);n+=h*h,h>r&&(r=h,a=w)}let t=Math.sqrt(n);if(t===0)continue;let o=i.data[c+a]/r/t,u=-s.data[c+a]/r/t;for(let w=0;w<e;w++){let h=i.data[c+w],f=s.data[c+w];i.data[c+w]=h*o-f*u,s.data[c+w]=h*u+f*o}s.data[c+a]=0}}function It(i,s){let e=i.nrow,l=i.data,c=s.data,n=(o,u)=>o+u*e,a=new Float64Array(e),r=0,t=e-1;for(let o=r+1;o<=t-1;o++){let u=0;for(let f=o;f<=t;f++)u+=Math.abs(l[n(f,o-1)]);if(u===0)continue;let w=0;for(let f=t;f>=o;f--)a[f]=l[n(f,o-1)]/u,w+=a[f]*a[f];let h=Math.sqrt(w);a[o]>0&&(h=-h),w-=a[o]*h,a[o]=a[o]-h;for(let f=o;f<e;f++){let d=0;for(let b=t;b>=o;b--)d+=a[b]*l[n(b,f)];d/=w;for(let b=o;b<=t;b++)l[n(b,f)]-=d*a[b]}for(let f=0;f<=t;f++){let d=0;for(let b=t;b>=o;b--)d+=a[b]*l[n(f,b)];d/=w;for(let b=o;b<=t;b++)l[n(f,b)]-=d*a[b]}a[o]=u*a[o],l[n(o,o-1)]=u*h}for(let o=0;o<e;o++)for(let u=0;u<e;u++)c[n(o,u)]=o===u?1:0;for(let o=t-1;o>=r+1;o--)if(l[n(o,o-1)]!==0){for(let u=o+1;u<=t;u++)a[u]=l[n(u,o-1)];for(let u=o;u<=t;u++){let w=0;for(let h=o;h<=t;h++)w+=a[h]*c[n(h,u)];w=w/a[o]/l[n(o,o-1)];for(let h=o;h<=t;h++)c[n(h,u)]+=w*a[h]}}}function z(i,s,e,l){if(Math.abs(e)>Math.abs(l)){let a=l/e,r=e+a*l;return[(i+a*s)/r,(s-a*i)/r]}let c=e/l,n=l+c*e;return[(c*i+s)/n,(c*s-i)/n]}function Ot(i,s,e,l){let c=i.nrow,n=i.data,a=s.data,r=(E,m)=>E+m*c,t=c-1,o=0,u=c-1,w=Number.EPSILON,h=0,f=0,d=0,b=0,M=0,p=0,A,j,k,y,_=0;for(let E=0;E<c;E++)for(let m=Math.max(E-1,0);m<c;m++)_+=Math.abs(n[r(E,m)]);let N=0,S=0;for(;t>=o;){let E=t;for(;E>o&&(M=Math.abs(n[r(E-1,E-1)])+Math.abs(n[r(E,E)]),M===0&&(M=_),!(Math.abs(n[r(E,E-1)])<w*M));)E--;if(E===t)n[r(t,t)]=n[r(t,t)]+h,e[t]=n[r(t,t)],l[t]=0,t--,N=0;else if(E===t-1){if(j=n[r(t,t-1)]*n[r(t-1,t)],f=(n[r(t-1,t-1)]-n[r(t,t)])/2,d=f*f+j,p=Math.sqrt(Math.abs(d)),n[r(t,t)]=n[r(t,t)]+h,n[r(t-1,t-1)]=n[r(t-1,t-1)]+h,k=n[r(t,t)],d>=0){p=f>=0?f+p:f-p,e[t-1]=k+p,e[t]=e[t-1],p!==0&&(e[t]=k-j/p),l[t-1]=0,l[t]=0,k=n[r(t,t-1)],M=Math.abs(k)+Math.abs(p),f=k/M,d=p/M,b=Math.sqrt(f*f+d*d),f/=b,d/=b;for(let m=t-1;m<c;m++)p=n[r(t-1,m)],n[r(t-1,m)]=d*p+f*n[r(t,m)],n[r(t,m)]=d*n[r(t,m)]-f*p;for(let m=0;m<=t;m++)p=n[r(m,t-1)],n[r(m,t-1)]=d*p+f*n[r(m,t)],n[r(m,t)]=d*n[r(m,t)]-f*p;for(let m=o;m<=u;m++)p=a[r(m,t-1)],a[r(m,t-1)]=d*p+f*a[r(m,t)],a[r(m,t)]=d*a[r(m,t)]-f*p}else e[t-1]=k+f,e[t]=k+f,l[t-1]=p,l[t]=-p;t-=2,N=0}else{if(k=n[r(t,t)],y=0,j=0,E<t&&(y=n[r(t-1,t-1)],j=n[r(t,t-1)]*n[r(t-1,t)]),N===10){h+=k;for(let g=o;g<=t;g++)n[r(g,g)]-=k;M=Math.abs(n[r(t,t-1)])+Math.abs(n[r(t-1,t-2)]),k=y=.75*M,j=-.4375*M*M}if(N===30&&(M=(y-k)/2,M=M*M+j,M>0)){M=Math.sqrt(M),y<k&&(M=-M),M=k-j/((y-k)/2+M);for(let g=o;g<=t;g++)n[r(g,g)]-=M;h+=M,k=y=j=.964}if(N++,++S>100*c)throw new Error("the eigendecomposition did not converge");let m=t-2;for(;m>=E&&(p=n[r(m,m)],b=k-p,M=y-p,f=(b*M-j)/n[r(m+1,m)]+n[r(m,m+1)],d=n[r(m+1,m+1)]-p-b-M,b=n[r(m+2,m+1)],M=Math.abs(f)+Math.abs(d)+Math.abs(b),f/=M,d/=M,b/=M,!(m===E||Math.abs(n[r(m,m-1)])*(Math.abs(d)+Math.abs(b))<w*(Math.abs(f)*(Math.abs(n[r(m-1,m-1)])+Math.abs(p)+Math.abs(n[r(m+1,m+1)])))));)m--;for(let g=m+2;g<=t;g++)n[r(g,g-2)]=0,g>m+2&&(n[r(g,g-3)]=0);for(let g=m;g<=t-1;g++){let F=g!==t-1;if(g!==m){if(f=n[r(g,g-1)],d=n[r(g+1,g-1)],b=F?n[r(g+2,g-1)]:0,k=Math.abs(f)+Math.abs(d)+Math.abs(b),k===0)continue;f/=k,d/=k,b/=k}if(M=Math.sqrt(f*f+d*d+b*b),f<0&&(M=-M),M!==0){g!==m?n[r(g,g-1)]=-M*k:E!==m&&(n[r(g,g-1)]=-n[r(g,g-1)]),f+=M,k=f/M,y=d/M,p=b/M,d/=f,b/=f;for(let v=g;v<c;v++)f=n[r(g,v)]+d*n[r(g+1,v)],F&&(f+=b*n[r(g+2,v)],n[r(g+2,v)]=n[r(g+2,v)]-f*p),n[r(g,v)]=n[r(g,v)]-f*k,n[r(g+1,v)]=n[r(g+1,v)]-f*y;for(let v=0;v<=Math.min(t,g+3);v++)f=k*n[r(v,g)]+y*n[r(v,g+1)],F&&(f+=p*n[r(v,g+2)],n[r(v,g+2)]=n[r(v,g+2)]-f*b),n[r(v,g)]=n[r(v,g)]-f,n[r(v,g+1)]=n[r(v,g+1)]-f*d;for(let v=o;v<=u;v++)f=k*a[r(v,g)]+y*a[r(v,g+1)],F&&(f+=p*a[r(v,g+2)],a[r(v,g+2)]=a[r(v,g+2)]-f*b),a[r(v,g)]=a[r(v,g)]-f,a[r(v,g+1)]=a[r(v,g+1)]-f*d}}}}if(_!==0){for(t=c-1;t>=0;t--)if(f=e[t],d=l[t],d===0){let E=t;n[r(t,t)]=1;for(let m=t-1;m>=0;m--){j=n[r(m,m)]-f,b=0;for(let g=E;g<=t;g++)b+=n[r(m,g)]*n[r(g,t)];if(l[m]<0)p=j,M=b;else if(E=m,l[m]===0?n[r(m,t)]=j!==0?-b/j:-b/(w*_):(k=n[r(m,m+1)],y=n[r(m+1,m)],d=(e[m]-f)*(e[m]-f)+l[m]*l[m],A=(k*M-p*b)/d,n[r(m,t)]=A,n[r(m+1,t)]=Math.abs(k)>Math.abs(p)?(-b-j*A)/k:(-M-y*A)/p),A=Math.abs(n[r(m,t)]),w*A*A>1)for(let g=m;g<=t;g++)n[r(g,t)]=n[r(g,t)]/A}}else if(d<0){let E=t-1;Math.abs(n[r(t,t-1)])>Math.abs(n[r(t-1,t)])?(n[r(t-1,t-1)]=d/n[r(t,t-1)],n[r(t-1,t)]=-(n[r(t,t)]-f)/n[r(t,t-1)]):[n[r(t-1,t-1)],n[r(t-1,t)]]=z(0,-n[r(t-1,t)],n[r(t-1,t-1)]-f,d),n[r(t,t-1)]=0,n[r(t,t)]=1;for(let m=t-2;m>=0;m--){let g=0,F=0;for(let v=E;v<=t;v++)g+=n[r(m,v)]*n[r(v,t-1)],F+=n[r(m,v)]*n[r(v,t)];if(j=n[r(m,m)]-f,l[m]<0)p=j,b=g,M=F;else{if(E=m,l[m]===0)[n[r(m,t-1)],n[r(m,t)]]=z(-g,-F,j,d);else{k=n[r(m,m+1)],y=n[r(m+1,m)];let v=(e[m]-f)*(e[m]-f)+l[m]*l[m]-d*d,J=(e[m]-f)*2*d;v===0&&J===0&&(v=w*_*(Math.abs(j)+Math.abs(d)+Math.abs(k)+Math.abs(y)+Math.abs(p))),[n[r(m,t-1)],n[r(m,t)]]=z(k*b-p*g+d*F,k*M-p*F-d*g,v,J),Math.abs(k)>Math.abs(p)+Math.abs(d)?(n[r(m+1,t-1)]=(-g-j*n[r(m,t-1)]+d*n[r(m,t)])/k,n[r(m+1,t)]=(-F-j*n[r(m,t)]-d*n[r(m,t-1)])/k):[n[r(m+1,t-1)],n[r(m+1,t)]]=z(-b-y*n[r(m,t-1)],-M-y*n[r(m,t)],p,d)}if(A=Math.max(Math.abs(n[r(m,t-1)]),Math.abs(n[r(m,t)])),w*A*A>1)for(let v=m;v<=t;v++)n[r(v,t-1)]=n[r(v,t-1)]/A,n[r(v,t)]=n[r(v,t)]/A}}}for(let E=c-1;E>=o;E--)for(let m=o;m<=u;m++){p=0;for(let g=o;g<=Math.min(E,u);g++)p+=a[r(m,g)]*n[r(g,E)];a[r(m,E)]=p}}}function R(i){return typeof i!="number"&&i.length>0&&Array.isArray(i[0])}function q(i){return typeof i=="number"?[i]:Array.from(i)}function O(i){let s=i[0].length;if(i.some(e=>e.length!==s))throw new TypeError("all rows of the matrix must have the same length");return s}function G(i,s){let e,l;if(R(i)&&R(s))e=i.map(a=>[...a]),l=s.map(a=>[...a]);else if(R(i)){let a=q(s),r=O(i);e=i.map(t=>[...t]),l=r===a.length?a.map(t=>[t]):r===1?[a]:[]}else if(R(s)){let a=q(i);l=s.map(r=>[...r]),e=s.length===a.length?[a]:s.length===1?a.map(r=>[r]):[]}else{let a=q(i),r=q(s);if(a.length===r.length)e=[a],l=r.map(t=>[t]);else if(a.length===1)e=[a],l=[r];else if(r.length===1)e=a.map(t=>[t]),l=[r];else throw new RangeError("non-conformable arguments")}let c=e[0]?.length??0;if(e.length===0||l.length!==c)throw new RangeError("non-conformable arguments");O(e);let n=O(l);return e.map(a=>{let r=new Array(n).fill(0);for(let t=0;t<n;t++){let o=0;for(let u=0;u<c;u++)o+=a[u]*l[u][t];r[t]=o}return r})}function V(i,s,e){let l=R(i),c=R(s);if(l&&c){let t=O(i);if(i.length!==s.length||t!==O(s))throw new RangeError("non-conformable arrays");return i.map((o,u)=>o.map((w,h)=>e(w,s[u][h])))}if(l||c){let t=l?i:s,o=q(l?s:i),u=t.length,w=O(t);if(o.length===0)return t.map(h=>h.map(()=>NaN));if(o.length>u*w)throw new RangeError("dims [product "+u*w+"] do not match the length of object ["+o.length+"]");return t.map((h,f)=>h.map((d,b)=>l?e(d,o[(f+b*u)%o.length]):e(o[(f+b*u)%o.length],d)))}let n=q(i),a=q(s);if(n.length===0||a.length===0)return[];let r=Math.max(n.length,a.length);return Array.from({length:r},(t,o)=>e(n[o%n.length],a[o%a.length]))}function St(i,s){return V(i,s,(e,l)=>e+l)}function $t(i,s){return V(i,s,(e,l)=>e-l)}function Pt(i,s){return V(i,s,(e,l)=>e*l)}function Lt(i,s){return V(i,s,(e,l)=>e/l)}var Ut={"*":(i,s)=>i*s,"+":(i,s)=>i+s,"-":(i,s)=>i-s,"/":(i,s)=>i/s,"^":(i,s)=>i**s};function zt(i,s,e="*"){let l=typeof e=="function"?e:Ut[e];if(l===void 0)throw new RangeError(`invalid fun "${String(e)}"`);let c=Array.from(i),n=Array.from(s);return c.map(a=>n.map(r=>l(a,r)))}function H(i){if(!R(i))return[q(i)];let s=O(i);return Array.from({length:s},(e,l)=>i.map(c=>c[l]))}function Vt(i,s={}){if(R(i)){let r=O(i);return Array.from({length:Math.min(i.length,r)},(t,o)=>i[o][o])}let e=q(i),l,c;if(e.length===1&&s.nrow===void 0&&s.ncol===void 0){if(c=Math.trunc(e[0]),!(c>=0))throw new RangeError("invalid 'nrow' value (< 0)");l=[1]}else l=e,c=s.nrow??e.length;let n=s.ncol??c,a=Array.from({length:c},()=>new Array(n).fill(0));for(let r=0;r<Math.min(c,n);r++)a[r][r]=l[r%l.length];return c===0?[]:a}function et(i,s){let e=i.map(n=>R(n)?s?H(n):n.map(a=>[...a]):[q(n)]),l=i.filter(R),c=l.length>0?s?l[0].length:O(l[0]):Math.max(...e.map(n=>n[0].length));return l.forEach((n,a)=>{if((s?n.length:O(n))!==c)throw new RangeError(`number of ${s?"rows":"columns"} of matrices must match (see arg ${a+2})`)}),e.flatMap(n=>n.map(a=>Array.from({length:c},(r,t)=>a[t%a.length])))}function Tt(...i){return H(et(i,!0))}function Qt(...i){return et(i,!1)}function Ct(i,s={}){let e=q(i),l=e.length,{nrow:c,ncol:n}=s;c===void 0&&n===void 0?(c=l,n=1):c===void 0?c=Math.ceil(l/n):n===void 0&&(n=Math.ceil(l/c));let a=s.byrow??!1;return Array.from({length:c},(r,t)=>Array.from({length:n},(o,u)=>l===0?NaN:e[(a?t*n+u:t+u*c)%l]))}function Xt(i,s){if(!R(i)&&!R(s)){let n=q(i),a=q(s);return n.flatMap(r=>a.map(t=>r*t))}let e=R(i)?i:q(i).map(n=>[n]),l=R(s)?s:q(s).map(n=>[n]),c=[];for(let n of e)for(let a of l)c.push(n.flatMap(r=>a.map(t=>r*t)));return c}function Dt(i,s,e={}){let{by:l,lengthOut:c}=e;if(s===void 0&&l===void 0&&c===void 0&&i!==void 0){if(!Number.isFinite(i))throw new RangeError("'from' must be a finite number");return rt(1,i)}if(i!==void 0&&!Number.isFinite(i))throw new RangeError("'from' must be a finite number");if(s!==void 0&&!Number.isFinite(s))throw new RangeError("'to' must be a finite number");if(c===void 0){if(l===void 0)return rt(i??1,s??1);let a=i??1,r=s??1,t=r-a;if(t===0&&r===0)return[r];let o=t/l;if(!Number.isFinite(o)){if(l===0&&t===0)return[a];throw new RangeError("invalid '(to - from)/by'")}if(o<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(t)/Math.max(Math.abs(r),Math.abs(a))<100*Number.EPSILON)return[a];let u=Math.trunc(o+1e-10);return Array.from({length:u+1},(w,h)=>l>0?Math.min(a+h*l,r):Math.max(a+h*l,r))}if(!Number.isFinite(c)||c<0)throw new RangeError("'length.out' must be a non-negative number");let n=Math.ceil(c);if(n===0)return[];if(l===void 0){let a=s??(i??1)+(n-1),r=i??a-(n-1);if(n<=2)return[r,a].slice(0,n);if(r===a)return new Array(n).fill(r);let t=(a-r)/(n-1);return Array.from({length:n},(o,u)=>u===0?r:u===n-1?a:r+u*t)}if(s===void 0){let a=i??1;return Array.from({length:n},(r,t)=>a+t*l)}if(i===void 0)return Array.from({length:n},(a,r)=>s-(n-1-r)*l);throw new RangeError("too many arguments")}function rt(i,s){let e=Math.trunc(Math.abs(s-i)+1+11920928955078125e-23),l=i<=s?1:-1;return Array.from({length:e},(c,n)=>i+l*n)}var Gt=22250738585072014e-324;function Ht(i){let s=i.length,e=s===0?0:i[0].length,l=i.map(o=>{if(o.length!==e)throw new TypeError("all rows of the matrix must have the same length");return Array.from(o)}),c=Array.from({length:s},(o,u)=>u),n=Math.min(s,e);for(let o=0;o<n;o++){let u=o,w=Math.abs(l[o][o]);for(let d=o+1;d<s;d++){let b=Math.abs(l[d][o]);(b>w||Number.isNaN(w))&&(w=b,u=d)}u!==o&&([l[u],l[o]]=[l[o],l[u]],[c[u],c[o]]=[c[o],c[u]]);let h=l[o][o];if(h===0)continue;let f=Math.abs(h)>=Gt?1/h:NaN;for(let d=o+1;d<s;d++){let b=Number.isNaN(f)?l[d][o]/h:l[d][o]*f;l[d][o]=b;for(let M=o+1;M<e;M++)l[d][M]=l[d][M]-b*l[o][M]}}let a=Array.from({length:s},(o,u)=>Array.from({length:n},(w,h)=>u===h?1:u>h?l[u][h]:0)),r=Array.from({length:n},(o,u)=>Array.from({length:e},(w,h)=>h>=u?l[u][h]:0)),t=Array.from({length:s},()=>new Array(s).fill(0));return c.forEach((o,u)=>t[o][u]=1),{P:t,L:a,U:r}}function Jt(i,s={}){let e=i.map(t=>Array.from(t)),l=Y(e,{center:s.center??!0,scale:s.scale??!1});if(l.scale?.some(t=>t===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let c=L(l.scaled,{nu:0}),n=e.length,a=c.v,r={sdev:c.d.map(t=>t/Math.sqrt(Math.max(1,n-1))),rotation:a,x:G(l.scaled,a)};return l.center!==void 0&&(r.center=l.center),l.scale!==void 0&&(r.scale=l.scale),r}export{St as add,gt as backsolve,Tt as cbind,bt as chol,wt as chol2inv,dt as crossprod,mt as det,ut as determinant,Vt as diag,Lt as divide,At as eigen,pt as forwardsolve,jt as ginv,kt as kappa,Xt as kronecker,Ht as lu,G as matMul,Ct as matrix,Pt as multiply,U as norm,zt as outer,Jt as prcomp,Q as qr,st as qrCoef,ft as qrFitted,it as qrQ,ot as qrQty,at as qrQy,C as qrR,lt as qrResid,ht as qrSolve,ct as qrX,Qt as rbind,D as rcond,Dt as seq,X as solve,$t as subtract,L as svd,H as t,Mt as tcrossprod};
|
|
1
|
+
/*! rStats.js 0.11.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 Ot,B as It,C as rt,D as et,a as A,b as tt,c as S,d as P,e as x,f as D,g as U,h as Q,i as vt,j as yt,k as kt,l as qt,m as xt,n as Et,o as X,p as Nt,q as At,r as j,s as H,t as z,u as nt,v as _,w as Rt,x as Ft,y as B,z as St}from"./chunks/chunk-7GQMKH3Z.js";import{F as st,h as ot,i as at}from"./chunks/chunk-CGBNKHNZ.js";import"./chunks/chunk-ALM3VHFP.js";import"./chunks/chunk-Y5PYRAYP.js";import"./chunks/chunk-EZWBRCJL.js";function Pt(r,n){let t=P(r),e=n===void 0?t:P(n);if(t.nrow!==e.nrow)throw new TypeError(`non-conformable arguments (${t.nrow} and ${e.nrow} rows)`);let f=t.nrow,c=A(t.ncol,e.ncol);for(let l=0;l<e.ncol;l++)for(let i=0;i<t.ncol;i++){if(n===void 0&&i>l)continue;let s=0;for(let a=0;a<f;a++)s+=t.data[a+i*f]*e.data[a+l*f];c.data[i+l*t.ncol]=s}return n===void 0&&ct(c),x(c)}function jt(r,n){let t=P(r),e=n===void 0?t:P(n);if(t.ncol!==e.ncol)throw new TypeError(`non-conformable arguments (${t.ncol} and ${e.ncol} columns)`);let f=A(t.nrow,e.nrow);for(let c=0;c<e.nrow;c++)for(let l=0;l<t.nrow;l++){if(n===void 0&&l>c)continue;let i=0;for(let s=0;s<t.ncol;s++)i+=t.data[l+s*t.nrow]*e.data[c+s*e.nrow];f.data[l+c*t.nrow]=i}return n===void 0&&ct(f),x(f)}function ct(r){let n=r.nrow;for(let t=0;t<n;t++)for(let e=t+1;e<n;e++)r.data[e+t*n]=r.data[t+e*n]}function _t(r,n,t={}){return it(r,n,t.k,t.upperTri??!0,t.transpose??!1)}function Tt(r,n,t={}){return it(r,n,t.k,t.upperTri??!1,t.transpose??!1)}function it(r,n,t,e,f){let c=S(r),l=P(n),i=t??c.ncol;if(!Number.isInteger(i)||i<1||i>c.nrow||i>c.ncol||i>l.nrow)throw new RangeError(`invalid k (got ${i})`);for(let a=0;a<i;a++)if(c.data[a+a*c.nrow]===0)throw new Error(`singular matrix: diagonal element ${a+1} is zero`);let s=A(i,l.ncol);for(let a=0;a<l.ncol;a++)for(let u=0;u<i;u++)s.data[u+a*i]=l.data[u+a*l.nrow];return $t(c,i,s,e,f),tt(n)?x(s):Array.from(s.data)}function $t(r,n,t,e,f){let c=r.nrow,l=r.data,i=t.nrow,s=t.data;for(let a=0;a<t.ncol;a++){let u=a*i;if(!f&&e){for(let h=n-1;h>=0;h--)if(s[u+h]!==0){s[u+h]/=l[h+h*c];let m=s[u+h];for(let o=0;o<h;o++)s[u+o]-=m*l[o+h*c]}}else if(f)if(e)for(let h=0;h<n;h++){let m=s[u+h];for(let o=0;o<h;o++)m-=l[o+h*c]*s[u+o];s[u+h]=m/l[h+h*c]}else for(let h=n-1;h>=0;h--){let m=s[u+h];for(let o=h+1;o<n;o++)m-=l[o+h*c]*s[u+o];s[u+h]=m/l[h+h*c]}else for(let h=0;h<n;h++)if(s[u+h]!==0){s[u+h]/=l[h+h*c];let m=s[u+h];for(let o=h+1;o<n;o++)s[u+o]-=m*l[o+h*c]}}}function Lt(r,n={}){let t=n.norm===void 0?n.exact?"2":"O":Ut(n.norm);if(n.exact){if(t==="2"){let c=j(r,{nu:0,nv:0}).d,l=Math.min(...c);return l===0?1/0:Math.max(...c)/l}return H(r,{type:t})*H(_(r),{type:t})}if(t!=="O"&&t!=="I")throw new RangeError(`norm must be "1", "O" or "I" when exact is false (got "${n.norm}")`);if((n.method??"qr")==="direct")return n.triangular?1/lt(S(r),(n.uplo??"U")==="U",t):1/B(r,{norm:t});let e=S(r),f=e.nrow<e.ncol?x(D(e)):r;return 1/lt(S(X(Q(f))),!0,t)}function lt(r,n,t){return Ht(t==="I"?D(r):r,n)}function Ut(r){let n=r.toUpperCase();if(n==="1"||n==="O")return"O";if(n==="E")return"F";if(n==="I"||n==="F"||n==="M"||n==="2")return n;throw new RangeError(`invalid norm "${r}"`)}function Ht(r,n){let t=Math.min(r.nrow,r.ncol),e=r.nrow,f=(m,o)=>r.data[m+o*e],c=!n,l=0;for(let m=0;m<t;m++){let[o,w]=c?[m,t-m]:[0,m+1],p=0;for(let M=o;M<o+w;M++)p+=Math.abs(f(M,m));l=Math.max(l,p)}let i=1,s=new Float64Array(t),a=m=>{for(let o=0;o<t;o++)s[o]*=m};for(let m=0;m<t;m++){let o=c?t-1-m:m,w=f(o,o);if(s[o]!==0&&(i=s[o]>0?-Math.abs(i):Math.abs(i)),Math.abs(i-s[o])>Math.abs(w)){let b=Math.abs(w)/Math.abs(i-s[o]);a(b),i*=b}let p=i-s[o],M=-i-s[o],d=Math.abs(p),g=Math.abs(M);if(w!==0?(p/=w,M/=w):(p=1,M=1),m!==t-1){let[b,v]=c?[0,o-1]:[o+1,t-1];for(let y=b;y<=v;y++)g+=Math.abs(s[y]+M*f(o,y)),s[y]+=p*f(o,y),d+=Math.abs(s[y]);if(d<g){let y=M-p;p=M;for(let k=b;k<=v;k++)s[k]+=y*f(o,k)}}s[o]=p}a(1/ft(s));let u=1;for(let m=0;m<t;m++){let o=c?m:t-1-m,w=f(o,o);if(Math.abs(s[o])>Math.abs(w)){let p=Math.abs(w)/Math.abs(s[o]);a(p),u*=p}if(w!==0?s[o]/=w:s[o]=1,m<t-1){let p=-s[o];if(c)for(let M=o+1;M<t;M++)s[M]+=p*f(M,o);else for(let M=0;M<o;M++)s[M]+=p*f(M,o)}}let h=1/ft(s);return a(h),u*=h,l!==0?u/l:0}function ft(r){let n=0;for(let t of r)n+=Math.abs(t);return n}function Ct(r,n={}){let t=n.tol??Math.sqrt(Number.EPSILON),e=S(r),{d:f,u:c,v:l}=j(r),i=e.nrow,s=e.ncol,a=A(s,i),u=Math.max(t*(f[0]??0),0);for(let h=0;h<f.length;h++){if(!(f[h]>u))continue;let m=1/f[h];for(let o=0;o<i;o++){let w=c[o][h]*m;for(let p=0;p<s;p++)a.data[p+o*s]+=l[p][h]*w}}return x(a)}function Vt(r){let n=S(r);U(n);let t=n.nrow;if(t===0)throw new RangeError("the matrix has zero dimension");for(let o of n.data)if(!Number.isFinite(o))throw new RangeError("the matrix contains NaN or infinite values");let e={nrow:t,ncol:t,data:n.data.slice()},f=A(t,t),c=new Float64Array(t),l=new Float64Array(t);rt(e,f),et(e,f,c,l,!0);let i=e.data,s=f.data,a=(o,w)=>o+w*t,u=new Uint8Array(t);for(let o=0;o+1<t;o++)l[o]>0&&l[o+1]<0&&(u[o++]=1);for(let o=0;o<t;o++)for(let w=o+1;w<t;w++)w===o+1&&u[o]||(i[a(w,o)]=0);let h=new Array(t),m=new Array(t);for(let o=0;o<t;o++){if(!u[o]){h[o]=i[a(o,o)],m[o]=0;continue}let w=Dt(i[a(o,o)],i[a(o,o+1)],i[a(o+1,o)],i[a(o+1,o+1)]);i[a(o,o)]=w.a,i[a(o,o+1)]=w.b,i[a(o+1,o)]=w.c,i[a(o+1,o+1)]=w.d;let{cs:p,sn:M}=w;for(let d=o+2;d<t;d++){let g=i[a(o,d)],b=i[a(o+1,d)];i[a(o,d)]=p*g+M*b,i[a(o+1,d)]=p*b-M*g}for(let d=0;d<o;d++){let g=i[a(d,o)],b=i[a(d,o+1)];i[a(d,o)]=p*g+M*b,i[a(d,o+1)]=p*b-M*g}for(let d=0;d<t;d++){let g=s[a(d,o)],b=s[a(d,o+1)];s[a(d,o)]=p*g+M*b,s[a(d,o+1)]=p*b-M*g}h[o]=w.rt1r,m[o]=w.rt1i,h[o+1]=w.rt2r,m[o+1]=w.rt2i,o++}return{T:x(e),Q:x(f),valuesRe:h,valuesIm:m}}var I=(r,n)=>n>=0&&!Object.is(n,-0)?Math.abs(r):-Math.abs(r);function Dt(r,n,t,e){let f=22250738585072014e-324,c=Number.EPSILON,l=4,i=2**Math.trunc(Math.log(f/c)/Math.log(2)/2),s=1/i,a,u;if(t===0)a=1,u=0;else if(n===0){a=0,u=1;let m=e;e=r,r=m,n=-t,t=0}else if(r-e===0&&I(1,n)!==I(1,t))a=1,u=0;else{let m=r-e,o=.5*m,w=Math.max(Math.abs(n),Math.abs(t)),p=Math.min(Math.abs(n),Math.abs(t))*I(1,n)*I(1,t),M=Math.max(Math.abs(o),w),d=o/M*o+w/M*p;if(d>=l*c){d=o+I(Math.sqrt(M)*Math.sqrt(d),o),r=e+d,e=e-w/d*p;let g=Math.hypot(t,d);a=d/g,u=t/g,n=n-t,t=0}else{let g=0,b=n+t;for(;g++,M=Math.max(Math.abs(m),Math.abs(b)),!(!(M>=s&&(b*=i,m*=i,g<=20))&&!(M<=i&&(b*=s,m*=s,g<=20))););o=.5*m;let v=Math.hypot(b,m);a=Math.sqrt(.5*(1+Math.abs(b)/v)),u=-(o/(v*a))*I(1,b);let y=r*a+n*u,k=-r*u+n*a,N=t*a+e*u,L=-t*u+e*a;if(r=y*a+N*u,n=k*a+L*u,t=-y*u+N*a,e=-k*u+L*a,m=.5*(r+e),r=m,e=m,t!==0)if(n!==0){if(I(1,n)===I(1,t)){let q=Math.sqrt(Math.abs(n)),W=Math.sqrt(Math.abs(t));o=I(q*W,t),v=1/Math.sqrt(Math.abs(n+t)),r=m+o,e=m-o,n=n-t,t=0;let Y=q*v,Z=W*v;m=a*Y-u*Z,u=a*Z+u*Y,a=m}}else n=-t,t=0,m=a,a=-u,u=m}}let h=t===0?0:Math.sqrt(Math.abs(n))*Math.sqrt(Math.abs(t));return{a:r,b:n,c:t,d:e,cs:a,sn:u,rt1r:r,rt1i:h,rt2r:e,rt2i:-h}}var Qt=["Higham08.b","Higham08","Pade","Taylor","PadeO","TaylorO"];function Xt(r,n={}){let t=n.method??"Higham08.b";if(!Qt.includes(t))throw new RangeError(`unknown method: ${String(t)}`);let e=n.order??8;if(!Number.isInteger(e)||e<1)throw new RangeError("order must be a positive integer");let f=S(r);U(f);let c=f.nrow;if(c===0)return[];if(c===1)return[[Math.exp(f.data[0])]];if(t==="Taylor"||t==="TaylorO")return x(Jt(f,e));if(t==="Pade"||t==="PadeO")return x(Kt(f,e));let l,i;t==="Higham08.b"&&(l=ht("P",f),i=ht("S",f));let s=Gt(f),a=s.data,u=(h,m)=>h+m*c;if(i){let h=i.scale;for(let m=0;m<c;m++)for(let o=0;o<c;o++)a[u(o,m)]=a[u(o,m)]*h[o]/h[m]}if(l){let h=l.scale,m=o=>{let w=h[o];if(w!==o){for(let p=0;p<c;p++)[a[u(p,o)],a[u(p,w)]]=[a[u(p,w)],a[u(p,o)]];for(let p=0;p<c;p++)[a[u(o,p)],a[u(w,p)]]=[a[u(w,p)],a[u(o,p)]]}};for(let o=l.ilo-1;o>=0;o--)m(o);for(let o=l.ihi+1;o<c;o++)m(o)}return x(s)}var zt={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]},Bt=[6476475253248e4,3238237626624e4,7771770303897600,0x437e459424400,129060195264e3,0x99a917d4d00,670442572800,33522128640,1323241920,40840800,960960,16380,182,1];function Gt(r){let n=r.nrow,t=z(r),e=G(n),f,c,l=0;if(t<=2.1){let u=t<=.015?3:t<=.25?5:t<=.95?7:9,h=zt[u],m=F(r,r),o=C([[h[1],e]]),w=C([[h[0],e]]),p=m;for(let M=1;M<=(u-1)/2;M++)M>1&&(p=F(p,m)),T(o,h[2*M+1],p),T(w,h[2*M],p);f=F(r,o),c=w}else{l=Math.max(0,Math.ceil(Math.log2(t/5.4)));let u=$(r,1/2**l),h=F(u,u),m=F(h,h),o=F(h,m),w=Bt,p=C([[w[13],o],[w[11],m],[w[9],h]]),M=F(o,p);for(let[g,b]of[[7,o],[5,m],[3,h],[1,e]])T(M,w[g],b);f=F(u,M);let d=C([[w[12],o],[w[10],m],[w[8],h]]);c=F(o,d);for(let[g,b]of[[6,o],[4,m],[2,h],[0,e]])T(c,w[g],b)}let i=A(n,n),s=A(n,n);for(let u=0;u<n*n;u++)i.data[u]=c.data[u]-f.data[u],s.data[u]=c.data[u]+f.data[u];let a=S(_(x(i),x(s)));for(let u=0;u<l;u++)a=F(a,a);return a}function dt(r){let n=Math.sqrt(z(r)*nt(r)),t=1;for(;2**t<n;)t++;return t+4}function wt(r,n){for(let t=0;t<n;t++)r=F(r,r);return r}function pt(r){let n=r.nrow;for(let t=0;t<n;t++)r.data[t+t*n]=r.data[t+t*n]+1}function Jt(r,n){let t=dt(r),e=2**t,f=G(r.nrow);for(let c=n;c>=1;c--)f=$(F(f,r),1/(c*e)),pt(f);return wt(f,t)}function ut(r,n,t,e){let f=e===1?r:$(r,-1),c=G(r.nrow);for(let l=n;l>=1;l--){let i=(n-l+1)/(l*(2*n-l+1)*t);c=$(F(c,f),i),pt(c)}return c}function Kt(r,n){let t=r.nrow,e=dt(r),f=2**e,c=ut(r,n,f,1),l=ut(r,n,f,-1),i=A(t,t);for(let s=0;s<t;s++)i.data.set(Wt(l,c.data.subarray(s*t,(s+1)*t)),s*t);return wt(i,e)}function Wt(r,n){let t=r.nrow,e=1e-100,f=w=>{let p=new Float64Array(t);for(let M=0;M<t;M++){let d=0;for(let g=0;g<t;g++)d+=r.data[M+g*t]*w[g];p[M]=d}return p},c=(w,p)=>{let M=0;for(let d=0;d<t;d++)M+=w[d]*p[d];return M},l=(w,p,M)=>{let d=new Float64Array(t);for(let g=0;g<t;g++)d[g]=w[g]+p*M[g];return d},i=new Float64Array(t),s=Float64Array.from(n),a=Float64Array.from(n),u=Float64Array.from(n),h=Float64Array.from(n),m=Math.sqrt(c(s,s)),o=c(h,n);if(Math.abs(o)<=e)return i;for(let w=0;w<t;w++){let p=f(a),M=c(h,p);if(Math.abs(M)<=e)return i;let d=o/M;if(Math.abs(d)<=e)return i;let g=l(u,-d,p);p=l(u,1,g);let b=f(p);s=l(s,-d,b);let v=Math.sqrt(c(s,s));if(i=l(i,d,p),Math.abs(v/m)<=1e-30)return i;let y=c(h,s);if(Math.abs(y)<=e)return i;let k=y/o;o=y,u=l(s,k,g),b=l(g,k,a),a=l(u,k,b)}return i}function G(r){let n=A(r,r);for(let t=0;t<r;t++)n.data[t+t*r]=1;return n}function F(r,n){let t=r.nrow,e=r.ncol,f=n.ncol,c=A(t,f);for(let l=0;l<f;l++)for(let i=0;i<t;i++){let s=0;for(let a=0;a<e;a++)s+=r.data[i+a*t]*n.data[a+l*e];c.data[i+l*t]=s}return c}function $(r,n){return{nrow:r.nrow,ncol:r.ncol,data:r.data.map(t=>t*n)}}function T(r,n,t){for(let e=0;e<r.data.length;e++)r.data[e]=r.data[e]+n*t.data[e]}function C(r){let[n,t]=r[0],e=$(t,n);for(let f=1;f<r.length;f++)T(e,r[f][0],r[f][1]);return e}function mt(r,n,t,e){let f=0,c=1;for(let l=0;l<t;l++){let i=r[n+l*e];if(i===0)continue;let s=Math.abs(i);f<s?(c=1+c*(f/s)**2,f=s):c+=(s/f)**2}return f*Math.sqrt(c)}function ht(r,n){let t=n.nrow,e=n.data,f=(d,g)=>d+g*t,c=new Float64Array(t),l=(d,g,b)=>{for(let v=0;v<b;v++)[e[f(v,d)],e[f(v,g)]]=[e[f(v,g)],e[f(v,d)]]},i=(d,g,b)=>{for(let v=b;v<t;v++)[e[f(d,v)],e[f(g,v)]]=[e[f(g,v)],e[f(d,v)]]},s=0,a=t-1;if(r==="P"){let d=!0;for(;d;){d=!1;for(let g=a;g>=0;g--){let b=!0;for(let v=0;v<=a;v++)if(g!==v&&e[f(g,v)]!==0){b=!1;break}if(b){if(c[a]=g,g!==a&&(l(g,a,a+1),i(g,a,s)),d=!0,a===0)return{ilo:0,ihi:0,scale:c};a--}}}for(d=!0;d;){d=!1;for(let g=s,b=a;g<=b;g++){let v=!0;for(let y=s;y<=a;y++)if(y!==g&&e[f(y,g)]!==0){v=!1;break}v&&(c[s]=g,g!==s&&(l(g,s,a+1),i(g,s,s)),d=!0,s++)}}}for(let d=s;d<=a;d++)c[d]=1;if(r==="P")return{ilo:s,ihi:a,scale:c};let u=2,h=.95,m=2**-1022/Number.EPSILON,o=1/m,w=m*u,p=1/w,M=!0;for(;M;){M=!1;for(let d=s;d<=a;d++){let g=mt(e,f(s,d),a-s+1,1),b=mt(e,f(d,s),a-s+1,t),v=0;for(let q=0;q<=a;q++)v=Math.max(v,Math.abs(e[f(q,d)]));let y=0;for(let q=s;q<t;q++)y=Math.max(y,Math.abs(e[f(d,q)]));if(g===0||b===0)continue;if(Number.isNaN(g+v+b+y))throw new RangeError("the matrix contains NaN or infinite values");let k=b/u,N=1,L=g+b;for(;g<k&&Math.max(N,g,v)<p&&Math.min(b,k,y)>w;)N*=u,g*=u,v*=u,b/=u,k/=u,y/=u;for(k=g/u;k>=b&&Math.max(b,y)<p&&Math.min(N,g,k,v)>w;)N/=u,g/=u,k/=u,v/=u,b*=u,y*=u;if(!(g+b>=h*L)&&!(N<1&&c[d]<1&&N*c[d]<=m)&&!(N>1&&c[d]>1&&c[d]>=o/N)){k=1/N,c[d]=c[d]*N,M=!0;for(let q=s;q<t;q++)e[f(d,q)]=e[f(d,q)]*k;for(let q=0;q<=a;q++)e[f(q,d)]=e[f(q,d)]*N}}}return{ilo:s,ihi:a,scale:c}}function R(r){return typeof r!="number"&&r.length>0&&Array.isArray(r[0])}function E(r){return typeof r=="number"?[r]:Array.from(r)}function O(r){let n=r[0].length;if(r.some(t=>t.length!==n))throw new TypeError("all rows of the matrix must have the same length");return n}function J(r,n){let t,e;if(R(r)&&R(n))t=r.map(l=>[...l]),e=n.map(l=>[...l]);else if(R(r)){let l=E(n),i=O(r);t=r.map(s=>[...s]),e=i===l.length?l.map(s=>[s]):i===1?[l]:[]}else if(R(n)){let l=E(r);e=n.map(i=>[...i]),t=n.length===l.length?[l]:n.length===1?l.map(i=>[i]):[]}else{let l=E(r),i=E(n);if(l.length===i.length)t=[l],e=i.map(s=>[s]);else if(l.length===1)t=[l],e=[i];else if(i.length===1)t=l.map(s=>[s]),e=[i];else throw new RangeError("non-conformable arguments")}let f=t[0]?.length??0;if(t.length===0||e.length!==f)throw new RangeError("non-conformable arguments");O(t);let c=O(e);return t.map(l=>{let i=new Array(c).fill(0);for(let s=0;s<c;s++){let a=0;for(let u=0;u<f;u++)a+=l[u]*e[u][s];i[s]=a}return i})}function V(r,n,t){let e=R(r),f=R(n);if(e&&f){let s=O(r);if(r.length!==n.length||s!==O(n))throw new RangeError("non-conformable arrays");return r.map((a,u)=>a.map((h,m)=>t(h,n[u][m])))}if(e||f){let s=e?r:n,a=E(e?n:r),u=s.length,h=O(s);if(a.length===0)return s.map(m=>m.map(()=>NaN));if(a.length>u*h)throw new RangeError("dims [product "+u*h+"] do not match the length of object ["+a.length+"]");return s.map((m,o)=>m.map((w,p)=>e?t(w,a[(o+p*u)%a.length]):t(a[(o+p*u)%a.length],w)))}let c=E(r),l=E(n);if(c.length===0||l.length===0)return[];let i=Math.max(c.length,l.length);return Array.from({length:i},(s,a)=>t(c[a%c.length],l[a%l.length]))}function Yt(r,n){return V(r,n,(t,e)=>t+e)}function Zt(r,n){return V(r,n,(t,e)=>t-e)}function tn(r,n){return V(r,n,(t,e)=>t*e)}function nn(r,n){return V(r,n,(t,e)=>t/e)}var rn={"*":(r,n)=>r*n,"+":(r,n)=>r+n,"-":(r,n)=>r-n,"/":(r,n)=>r/n,"^":(r,n)=>r**n};function en(r,n,t="*"){let e=typeof t=="function"?t:rn[t];if(e===void 0)throw new RangeError(`invalid fun "${String(t)}"`);let f=Array.from(r),c=Array.from(n);return f.map(l=>c.map(i=>e(l,i)))}function K(r){if(!R(r))return[E(r)];let n=O(r);return Array.from({length:n},(t,e)=>r.map(f=>f[e]))}function on(r,n={}){if(R(r)){let i=O(r);return Array.from({length:Math.min(r.length,i)},(s,a)=>r[a][a])}let t=E(r),e,f;if(t.length===1&&n.nrow===void 0&&n.ncol===void 0){if(f=Math.trunc(t[0]),!(f>=0))throw new RangeError("invalid 'nrow' value (< 0)");e=[1]}else e=t,f=n.nrow??t.length;let c=n.ncol??f,l=Array.from({length:f},()=>new Array(c).fill(0));for(let i=0;i<Math.min(f,c);i++)l[i][i]=e[i%e.length];return f===0?[]:l}function Mt(r,n){let t=r.map(c=>R(c)?n?K(c):c.map(l=>[...l]):[E(c)]),e=r.filter(R),f=e.length>0?n?e[0].length:O(e[0]):Math.max(...t.map(c=>c[0].length));return e.forEach((c,l)=>{if((n?c.length:O(c))!==f)throw new RangeError(`number of ${n?"rows":"columns"} of matrices must match (see arg ${l+2})`)}),t.flatMap(c=>c.map(l=>Array.from({length:f},(i,s)=>l[s%l.length])))}function an(r){return typeof r=="object"&&!Array.isArray(r)&&!ArrayBuffer.isView(r)}function bt(r){let n=r.filter(an);if(n.length!==0){if(n.length<r.length)throw new TypeError("cannot combine data frames with vectors or matrices");return n}}function sn(...r){let n=bt(r);return n===void 0?K(Mt(r,!0)):at(n)}function cn(...r){let n=bt(r);return n===void 0?Mt(r,!1):ot(n)}function ln(r,n={}){let t=E(r),e=t.length,{nrow:f,ncol:c}=n;f===void 0&&c===void 0?(f=e,c=1):f===void 0?f=Math.ceil(e/c):c===void 0&&(c=Math.ceil(e/f));let l=n.byrow??!1;return Array.from({length:f},(i,s)=>Array.from({length:c},(a,u)=>e===0?NaN:t[(l?s*c+u:s+u*f)%e]))}function fn(r,n){if(!R(r)&&!R(n)){let c=E(r),l=E(n);return c.flatMap(i=>l.map(s=>i*s))}let t=R(r)?r:E(r).map(c=>[c]),e=R(n)?n:E(n).map(c=>[c]),f=[];for(let c of t)for(let l of e)f.push(c.flatMap(i=>l.map(s=>i*s)));return f}function un(r,n,t={}){let{by:e,lengthOut:f}=t;if(n===void 0&&e===void 0&&f===void 0&&r!==void 0){if(!Number.isFinite(r))throw new RangeError("'from' must be a finite number");return gt(1,r)}if(r!==void 0&&!Number.isFinite(r))throw new RangeError("'from' must be a finite number");if(n!==void 0&&!Number.isFinite(n))throw new RangeError("'to' must be a finite number");if(f===void 0){if(e===void 0)return gt(r??1,n??1);let l=r??1,i=n??1,s=i-l;if(s===0&&i===0)return[i];let a=s/e;if(!Number.isFinite(a)){if(e===0&&s===0)return[l];throw new RangeError("invalid '(to - from)/by'")}if(a<0)throw new RangeError("wrong sign in 'by' argument");if(Math.abs(s)/Math.max(Math.abs(i),Math.abs(l))<100*Number.EPSILON)return[l];let u=Math.trunc(a+1e-10);return Array.from({length:u+1},(h,m)=>e>0?Math.min(l+m*e,i):Math.max(l+m*e,i))}if(!Number.isFinite(f)||f<0)throw new RangeError("'length.out' must be a non-negative number");let c=Math.ceil(f);if(c===0)return[];if(e===void 0){let l=n??(r??1)+(c-1),i=r??l-(c-1);if(c<=2)return[i,l].slice(0,c);if(i===l)return new Array(c).fill(i);let s=(l-i)/(c-1);return Array.from({length:c},(a,u)=>u===0?i:u===c-1?l:i+u*s)}if(n===void 0){let l=r??1;return Array.from({length:c},(i,s)=>l+s*e)}if(r===void 0)return Array.from({length:c},(l,i)=>n-(c-1-i)*e);throw new RangeError("too many arguments")}function gt(r,n){let t=Math.trunc(Math.abs(n-r)+1+11920928955078125e-23),e=r<=n?1:-1;return Array.from({length:t},(f,c)=>r+e*c)}var mn=22250738585072014e-324;function hn(r){let n=r.length,t=n===0?0:r[0].length,e=r.map(a=>{if(a.length!==t)throw new TypeError("all rows of the matrix must have the same length");return Array.from(a)}),f=Array.from({length:n},(a,u)=>u),c=Math.min(n,t);for(let a=0;a<c;a++){let u=a,h=Math.abs(e[a][a]);for(let w=a+1;w<n;w++){let p=Math.abs(e[w][a]);(p>h||Number.isNaN(h))&&(h=p,u=w)}u!==a&&([e[u],e[a]]=[e[a],e[u]],[f[u],f[a]]=[f[a],f[u]]);let m=e[a][a];if(m===0)continue;let o=Math.abs(m)>=mn?1/m:NaN;for(let w=a+1;w<n;w++){let p=Number.isNaN(o)?e[w][a]/m:e[w][a]*o;e[w][a]=p;for(let M=a+1;M<t;M++)e[w][M]=e[w][M]-p*e[a][M]}}let l=Array.from({length:n},(a,u)=>Array.from({length:c},(h,m)=>u===m?1:u>m?e[u][m]:0)),i=Array.from({length:c},(a,u)=>Array.from({length:t},(h,m)=>m>=u?e[u][m]:0)),s=Array.from({length:n},()=>new Array(n).fill(0));return f.forEach((a,u)=>s[a][u]=1),{P:s,L:l,U:i}}function dn(r,n={}){let t=r.map(s=>Array.from(s)),e=st(t,{center:n.center??!0,scale:n.scale??!1});if(e.scale?.some(s=>s===0))throw new RangeError("cannot rescale a constant/zero column to unit variance");let f=j(e.scaled,{nu:0}),c=t.length,l=f.v,i={sdev:f.d.map(s=>s/Math.sqrt(Math.max(1,c-1))),rotation:l,x:J(e.scaled,l)};return e.center!==void 0&&(i.center=e.center),e.scale!==void 0&&(i.scale=e.scale),i}export{Yt as add,_t as backsolve,sn as cbind,St as chol,Ot as chol2inv,Pt as crossprod,Ft as det,Rt as determinant,on as diag,nn as divide,It as eigen,Xt as expm,Tt as forwardsolve,Ct as ginv,Lt as kappa,fn as kronecker,hn as lu,J as matMul,ln as matrix,tn as multiply,H as norm,en as outer,dn as prcomp,Q as qr,kt as qrCoef,xt as qrFitted,Et as qrQ,vt as qrQty,yt as qrQy,X as qrR,qt as qrResid,At as qrSolve,Nt as qrX,cn as rbind,B as rcond,Vt as schur,un as seq,_ as solve,Zt as subtract,j as svd,K as t,jt as tcrossprod};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { AovStrataFit, AovStratum } from "./aovStrata.js";
|
|
2
|
+
import type { LmFormulaResult } from "./lm.js";
|
|
3
|
+
/** An analysis of variance table, in the form of R's printed table. */
|
|
4
|
+
export interface AnovaTable {
|
|
5
|
+
/** R's column names, such as `"Df"`, `"Sum Sq"`, `"Mean Sq"`, `"F value"`, `"Pr(>F)"`. */
|
|
6
|
+
colNames: string[];
|
|
7
|
+
/** Row names (term labels and `"Residuals"`, or `"1"`, `"2"`, ... for a comparison). */
|
|
8
|
+
rowNames: string[];
|
|
9
|
+
/** One array per row; NaN where R prints nothing or NA. */
|
|
10
|
+
table: number[][];
|
|
11
|
+
/** R's warnings. */
|
|
12
|
+
warnings: string[];
|
|
13
|
+
}
|
|
14
|
+
/** Sequential sums of squares of the terms (intercept row included when the model has one) and the residuals */
|
|
15
|
+
export declare function sequentialTerms(fit: LmFormulaResult): {
|
|
16
|
+
terms: {
|
|
17
|
+
term: number;
|
|
18
|
+
df: number;
|
|
19
|
+
ss: number;
|
|
20
|
+
}[];
|
|
21
|
+
rdf: number;
|
|
22
|
+
rss: number;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Analysis of variance of linear models fitted from a formula. With one fit, the table of sequential (type I) sums
|
|
26
|
+
* of squares, equivalent to R's `anova(fit)`: one row per term (the intercept left out), in the order of the model,
|
|
27
|
+
* and `Residuals`. With several fits, the comparison of the models in the order given, equivalent to R's
|
|
28
|
+
* `anova(fit1, fit2, ...)`: columns `Res.Df`, `RSS`, `Df`, `Sum of Sq`, `F`, `Pr(>F)`, the F tests scaled by the
|
|
29
|
+
* residual mean square of the model with the fewest residual degrees of freedom.
|
|
30
|
+
*
|
|
31
|
+
* @param fit A fit from {@link lm} (or {@link aov} without `Error()`) with a formula.
|
|
32
|
+
* @param fits More fits to compare with the first.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const data = { y: [4.2, 5.1, 6.3, 5.8, 7.9, 8.4], g: ["a", "a", "b", "b", "c", "c"] };
|
|
37
|
+
* const t = anova(lm("y ~ g", data));
|
|
38
|
+
* t.rowNames; // ["g", "Residuals"]
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function anova(fit: LmFormulaResult, ...fits: LmFormulaResult[]): AnovaTable;
|
|
42
|
+
/** Result of {@link aovSummary} for a fit with error strata. */
|
|
43
|
+
export interface AovStrataSummary {
|
|
44
|
+
/** The table of each stratum, in order, without the intercept stratum. */
|
|
45
|
+
strata: AovStratum[];
|
|
46
|
+
warnings: string[];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The table of R's `summary(aov(formula, data))` for a fit without `Error()`: the rows of {@link anova}'s
|
|
50
|
+
* sequential table, except that without residual degrees of freedom there is no `Residuals` row and no `F value` and
|
|
51
|
+
* `Pr(>F)` columns. For a fit with an `Error()` term, the tables of the strata (`{ strata: [{ stratum: "Error: id",
|
|
52
|
+
* colNames, rowNames, table }, ...] }`), without the intercept stratum.
|
|
53
|
+
*
|
|
54
|
+
* @param fit A fit from {@link aov} (or {@link lm}) with a formula.
|
|
55
|
+
*/
|
|
56
|
+
export declare function aovSummary(fit: AovStrataFit): AovStrataSummary;
|
|
57
|
+
export declare function aovSummary(fit: LmFormulaResult): AnovaTable;
|
|
58
|
+
export declare function aovSummary(fit: LmFormulaResult | AovStrataFit): AnovaTable | AovStrataSummary;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// Analysis of variance tables of linear models fitted from a formula: R's anova(fit) (sequential sums of squares),
|
|
2
|
+
// anova(fit1, fit2, ...) (comparison of models) and summary(aov(...)) for a fit without Error().
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js (no ported code) from oracle/anova-behaviour.md. The sequential sum of squares of a term is
|
|
5
|
+
// the sum of the squared effects (Q'y of the pivoted QR decomposition) of its columns that are not aliased; the
|
|
6
|
+
// terms appear in the order of their first column among the columns kept.
|
|
7
|
+
import { pf1 } from "../distributions/f.js";
|
|
8
|
+
/** Sequential sums of squares of the terms (intercept row included when the model has one) and the residuals */
|
|
9
|
+
export function sequentialTerms(fit) {
|
|
10
|
+
const { pivot } = fit.qr;
|
|
11
|
+
const terms = [];
|
|
12
|
+
const byTerm = new Map();
|
|
13
|
+
for (let i = 0; i < fit.rank; i++) {
|
|
14
|
+
const t = fit.assign[pivot[i]];
|
|
15
|
+
let row = byTerm.get(t);
|
|
16
|
+
if (row === undefined) {
|
|
17
|
+
row = { term: t, df: 0, ss: 0 };
|
|
18
|
+
byTerm.set(t, row);
|
|
19
|
+
terms.push(row);
|
|
20
|
+
}
|
|
21
|
+
row.df++;
|
|
22
|
+
row.ss += fit.effects[i] ** 2;
|
|
23
|
+
}
|
|
24
|
+
return { terms, rdf: fit.dfResidual, rss: fit.rss };
|
|
25
|
+
}
|
|
26
|
+
const upperF = (f, df1, df2) => pf1(f, df1, df2, false, false);
|
|
27
|
+
/** The rows of the sequential table without the intercept, and the warning on a perfect fit */
|
|
28
|
+
function sequentialRows(fit) {
|
|
29
|
+
const s = sequentialTerms(fit);
|
|
30
|
+
const kept = s.terms.filter((t) => t.term !== 0);
|
|
31
|
+
let fss = 0;
|
|
32
|
+
fit.fittedValues.forEach((f, i) => (fss += (fit.weights?.[i] ?? 1) * f * f));
|
|
33
|
+
return {
|
|
34
|
+
names: kept.map((t) => fit.termLabels[t.term - 1]),
|
|
35
|
+
rows: kept,
|
|
36
|
+
rdf: s.rdf,
|
|
37
|
+
rss: s.rss,
|
|
38
|
+
perfect: s.rss < 1e-10 * fss,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function sequentialTable(fit) {
|
|
42
|
+
const { names, rows, rdf, rss, perfect } = sequentialRows(fit);
|
|
43
|
+
const rms = rss / rdf;
|
|
44
|
+
const table = rows.map(({ df, ss }) => {
|
|
45
|
+
const f = ss / df / rms;
|
|
46
|
+
return [df, ss, ss / df, f, upperF(f, df, rdf)];
|
|
47
|
+
});
|
|
48
|
+
table.push([rdf, rss, rdf === 0 ? NaN : rms, NaN, NaN]);
|
|
49
|
+
return {
|
|
50
|
+
colNames: ["Df", "Sum Sq", "Mean Sq", "F value", "Pr(>F)"],
|
|
51
|
+
rowNames: [...names, "Residuals"],
|
|
52
|
+
table,
|
|
53
|
+
warnings: perfect ? ["ANOVA F-tests on an essentially perfect fit are unreliable"] : [],
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function comparisonTable(fits) {
|
|
57
|
+
const rdf = fits.map((f) => f.dfResidual);
|
|
58
|
+
const rss = fits.map((f) => f.rss);
|
|
59
|
+
// the scale: the residual mean square of the (first) model with the smallest residual df
|
|
60
|
+
let big = 0;
|
|
61
|
+
rdf.forEach((d, i) => {
|
|
62
|
+
if (d < rdf[big])
|
|
63
|
+
big = i;
|
|
64
|
+
});
|
|
65
|
+
const scale = rss[big] / rdf[big];
|
|
66
|
+
const table = fits.map((_, i) => {
|
|
67
|
+
if (i === 0)
|
|
68
|
+
return [rdf[0], rss[0], NaN, NaN, NaN, NaN];
|
|
69
|
+
const df = rdf[i - 1] - rdf[i];
|
|
70
|
+
const ss = rss[i - 1] - rss[i];
|
|
71
|
+
let f = df === 0 ? NaN : ss / df / scale;
|
|
72
|
+
if (f < 0)
|
|
73
|
+
f = NaN;
|
|
74
|
+
return [rdf[i], rss[i], df, ss, f, Number.isNaN(f) ? NaN : upperF(f, Math.abs(df), rdf[big])];
|
|
75
|
+
});
|
|
76
|
+
return {
|
|
77
|
+
colNames: ["Res.Df", "RSS", "Df", "Sum of Sq", "F", "Pr(>F)"],
|
|
78
|
+
rowNames: fits.map((_, i) => String(i + 1)),
|
|
79
|
+
table,
|
|
80
|
+
warnings: [],
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Analysis of variance of linear models fitted from a formula. With one fit, the table of sequential (type I) sums
|
|
85
|
+
* of squares, equivalent to R's `anova(fit)`: one row per term (the intercept left out), in the order of the model,
|
|
86
|
+
* and `Residuals`. With several fits, the comparison of the models in the order given, equivalent to R's
|
|
87
|
+
* `anova(fit1, fit2, ...)`: columns `Res.Df`, `RSS`, `Df`, `Sum of Sq`, `F`, `Pr(>F)`, the F tests scaled by the
|
|
88
|
+
* residual mean square of the model with the fewest residual degrees of freedom.
|
|
89
|
+
*
|
|
90
|
+
* @param fit A fit from {@link lm} (or {@link aov} without `Error()`) with a formula.
|
|
91
|
+
* @param fits More fits to compare with the first.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* const data = { y: [4.2, 5.1, 6.3, 5.8, 7.9, 8.4], g: ["a", "a", "b", "b", "c", "c"] };
|
|
96
|
+
* const t = anova(lm("y ~ g", data));
|
|
97
|
+
* t.rowNames; // ["g", "Residuals"]
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export function anova(fit, ...fits) {
|
|
101
|
+
const all = [fit, ...fits];
|
|
102
|
+
for (const f of all) {
|
|
103
|
+
if (typeof f !== "object" || f === null || !Array.isArray(f.effects)) {
|
|
104
|
+
throw new TypeError("anova() needs fits from lm() or aov() with a formula");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (all.length === 1)
|
|
108
|
+
return sequentialTable(fit);
|
|
109
|
+
if (all.some((f) => f.rows.length !== fit.rows.length))
|
|
110
|
+
throw new Error("models were not all fitted to the same size of dataset");
|
|
111
|
+
const same = all.filter((f) => f.response === fit.response);
|
|
112
|
+
const warnings = [];
|
|
113
|
+
if (same.length < all.length) {
|
|
114
|
+
const removed = [...new Set(all.filter((f) => f.response !== fit.response).map((f) => f.response))];
|
|
115
|
+
warnings.push(`models with response ${removed.map((r) => `‘${r}’`).join(", ")} removed because response differs from model 1`);
|
|
116
|
+
}
|
|
117
|
+
const tab = same.length === 1 ? sequentialTable(fit) : comparisonTable(same);
|
|
118
|
+
tab.warnings = [...warnings, ...tab.warnings];
|
|
119
|
+
return tab;
|
|
120
|
+
}
|
|
121
|
+
export function aovSummary(fit) {
|
|
122
|
+
if (typeof fit === "object" && fit !== null && fit.multistratum === true) {
|
|
123
|
+
const f = fit;
|
|
124
|
+
return { strata: f.strata.filter((s) => s.stratum !== "Error: (Intercept)").map((s) => ({ ...s })), warnings: [] };
|
|
125
|
+
}
|
|
126
|
+
if (typeof fit !== "object" || fit === null || !Array.isArray(fit.effects)) {
|
|
127
|
+
throw new TypeError("aovSummary() needs a fit from aov() with a formula");
|
|
128
|
+
}
|
|
129
|
+
const t = sequentialTable(fit);
|
|
130
|
+
if (fit.dfResidual > 0)
|
|
131
|
+
return { ...t, warnings: [] };
|
|
132
|
+
return {
|
|
133
|
+
colNames: t.colNames.slice(0, 3),
|
|
134
|
+
rowNames: t.rowNames.slice(0, -1),
|
|
135
|
+
table: t.table.slice(0, -1).map((r) => r.slice(0, 3)),
|
|
136
|
+
warnings: [],
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type DataFrame } from "./modelFrame.js";
|
|
2
|
+
/** An effect size of {@link aovEz}: generalized (`"ges"`) or partial (`"pes"`) eta squared. */
|
|
3
|
+
export type AovEzEffectSize = "ges" | "pes";
|
|
4
|
+
/** Options for {@link aovEz}. */
|
|
5
|
+
export interface AovEzOptions {
|
|
6
|
+
/** Column of the subject identifiers. */
|
|
7
|
+
id: string;
|
|
8
|
+
/** Column of the response. */
|
|
9
|
+
dv: string;
|
|
10
|
+
/** Between-subjects factors (columns). */
|
|
11
|
+
between?: readonly string[] | string;
|
|
12
|
+
/** Within-subjects factors (columns). */
|
|
13
|
+
within?: readonly string[] | string;
|
|
14
|
+
/** Type of the sums of squares: 3 (default) or 2. */
|
|
15
|
+
type?: 2 | 3;
|
|
16
|
+
/** Sphericity correction of the anova table: `"GG"` (default), `"HF"` or `"none"`. */
|
|
17
|
+
correction?: "GG" | "HF" | "none";
|
|
18
|
+
/** Effect sizes: `"ges"` (default), `"pes"`, or both. */
|
|
19
|
+
es?: AovEzEffectSize | readonly AovEzEffectSize[];
|
|
20
|
+
/** Observed (not manipulated) factors, for the generalized eta squared. */
|
|
21
|
+
observed?: readonly string[] | string;
|
|
22
|
+
}
|
|
23
|
+
/** A table of {@link aovEz}. */
|
|
24
|
+
export interface AovEzTable {
|
|
25
|
+
colNames: string[];
|
|
26
|
+
rowNames: string[];
|
|
27
|
+
table: number[][];
|
|
28
|
+
}
|
|
29
|
+
/** Result of {@link aovEz}. */
|
|
30
|
+
export interface AovEzResult {
|
|
31
|
+
/** afex's anova table (without the intercept). */
|
|
32
|
+
anova: AovEzTable;
|
|
33
|
+
/** car's univariate tests (intercept included); without within-subject factors, car's Anova table. */
|
|
34
|
+
univariate: AovEzTable;
|
|
35
|
+
/** Greenhouse-Geisser and Huynh-Feldt corrections of the rows with more than one within-subject df. */
|
|
36
|
+
corrections?: AovEzTable;
|
|
37
|
+
/** Mauchly's tests of the same rows. */
|
|
38
|
+
sphericity?: AovEzTable;
|
|
39
|
+
/** The within-subject factors (columns), as given. */
|
|
40
|
+
within: string[];
|
|
41
|
+
warnings: string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Repeated-measures (within-subjects), between-subjects and mixed analysis of variance from long data, as afex's
|
|
45
|
+
* `aov_ez(id, dv, data, between, within, type, observed, anova_table = list(correction, es))`. Returns afex's
|
|
46
|
+
* anova table and car's univariate tests, sphericity corrections (Greenhouse-Geisser, Huynh-Feldt) and Mauchly tests.
|
|
47
|
+
*
|
|
48
|
+
* Between-subjects factors use `contr.sum` and sorted levels; the levels of within-subjects factors are taken in order
|
|
49
|
+
* of first appearance. Several rows for a subject and a cell are averaged (with afex's warning); subjects that miss
|
|
50
|
+
* a cell are removed (with a warning).
|
|
51
|
+
*
|
|
52
|
+
* @param data Long data: one row per observation.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const data = {
|
|
57
|
+
* id: ["s1", "s1", "s2", "s2", "s3", "s3", "s4", "s4"],
|
|
58
|
+
* W: ["a", "b", "a", "b", "a", "b", "a", "b"],
|
|
59
|
+
* y: [4.1, 5.0, 3.8, 4.9, 4.4, 5.6, 4.0, 4.7],
|
|
60
|
+
* };
|
|
61
|
+
* aovEz(data, { id: "id", dv: "y", within: "W" }).anova.rowNames; // ["W"]
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function aovEz(data: DataFrame, options: AovEzOptions): AovEzResult;
|