@numero-uno-jpn/rstats 1.14.0 → 1.16.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 +9 -9
- package/README.md +9 -9
- package/dist/causal/mediate.d.ts +146 -0
- package/dist/causal/mediate.js +399 -0
- package/dist/causal/mediationProcedures.d.ts +231 -0
- package/dist/causal/mediationProcedures.js +240 -0
- package/dist/causal/moderation.d.ts +148 -0
- package/dist/causal/moderation.js +165 -0
- package/dist/causal.js +2 -2
- package/dist/chunks/{chunk-CKUBQW2A.js → chunk-244M5C2B.js} +2 -2
- package/dist/chunks/chunk-37YHUVXL.js +3 -0
- package/dist/chunks/chunk-3L333CTJ.js +2 -0
- package/dist/chunks/{chunk-I2OTBVRK.js → chunk-3RO5FWJ5.js} +2 -2
- package/dist/chunks/chunk-3VKN6K3P.js +2 -0
- package/dist/chunks/{chunk-QLKZ2OFQ.js → chunk-57BGTHS7.js} +2 -2
- package/dist/chunks/{chunk-EWNJT27V.js → chunk-6DP2U23T.js} +2 -2
- package/dist/chunks/{chunk-62Y36UKH.js → chunk-6MHLWTBF.js} +2 -2
- package/dist/chunks/{chunk-BCXYAILN.js → chunk-7IU5OE5I.js} +2 -2
- package/dist/chunks/{chunk-PHDTVTKV.js → chunk-7MFMRNMT.js} +1 -1
- package/dist/chunks/chunk-AC22FPFB.js +4 -0
- package/dist/chunks/{chunk-AQHG7BEZ.js → chunk-AIII5VRA.js} +2 -2
- package/dist/chunks/{chunk-TGXUU5NB.js → chunk-B5QE2FDM.js} +1 -1
- package/dist/chunks/{chunk-FPJ2CWLK.js → chunk-BHNDCNWM.js} +2 -2
- package/dist/chunks/{chunk-YLUF4WTL.js → chunk-DP6TE35N.js} +2 -2
- package/dist/chunks/{chunk-ZJJJDLRL.js → chunk-FCE6N6PI.js} +2 -2
- package/dist/chunks/{chunk-D25AVVVI.js → chunk-FHHI3PYN.js} +2 -2
- package/dist/chunks/chunk-GKDA5W53.js +2 -0
- package/dist/chunks/{chunk-H5GBKP57.js → chunk-H6WBOIFZ.js} +1 -1
- package/dist/chunks/{chunk-B6NPCYFO.js → chunk-HOH5KGSU.js} +2 -2
- package/dist/chunks/{chunk-7VMGP2U7.js → chunk-IPMC3VOC.js} +2 -2
- package/dist/chunks/chunk-ISOLPXQV.js +2 -0
- package/dist/chunks/{chunk-NA7EDZ4Q.js → chunk-IVJTWTGV.js} +2 -2
- package/dist/chunks/{chunk-IXFWR3LH.js → chunk-J74CELYL.js} +2 -2
- package/dist/chunks/chunk-JYE3T7FS.js +2 -0
- package/dist/chunks/chunk-K23MXTLC.js +2 -0
- package/dist/chunks/chunk-K5IP4ZJ5.js +2 -0
- package/dist/chunks/chunk-KSN6A4OK.js +2 -0
- package/dist/chunks/{chunk-XXH5OIUO.js → chunk-LHMG6Q5C.js} +2 -2
- package/dist/chunks/{chunk-ZWSHIUBZ.js → chunk-MNPZUMPL.js} +2 -2
- package/dist/chunks/chunk-NCVEJDRI.js +2 -0
- package/dist/chunks/{chunk-SEKOIW43.js → chunk-NQWFURFV.js} +2 -2
- package/dist/chunks/chunk-P3Q6IJMR.js +2 -0
- package/dist/chunks/{chunk-6HUTVWPO.js → chunk-PIWXBWMD.js} +2 -2
- package/dist/chunks/{chunk-A5DZG7FH.js → chunk-QBY63DGM.js} +2 -2
- package/dist/chunks/{chunk-DZQWGBTW.js → chunk-RV4SUKXV.js} +2 -2
- package/dist/chunks/chunk-SALMA4XX.js +2 -0
- package/dist/chunks/{chunk-MXOBCQQP.js → chunk-UMXY4R4G.js} +2 -2
- package/dist/chunks/{chunk-2426YL2X.js → chunk-W5D6CIZM.js} +2 -2
- package/dist/chunks/{chunk-6VY3XNAF.js → chunk-XAMWBGTP.js} +2 -2
- package/dist/chunks/{chunk-3XUHCWLU.js → chunk-XMA2NWYX.js} +1 -1
- package/dist/chunks/{chunk-EDRBKZ3C.js → chunk-ZTZX2UYP.js} +2 -2
- package/dist/chunks/chunk-ZZ6KYMBW.js +2 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/causal.d.ts +3 -0
- package/dist/entries/causal.js +3 -0
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +1 -0
- package/dist/entries/tests.d.ts +2 -1
- package/dist/entries/tests.js +1 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -5
- package/dist/models/fitOptions.d.ts +4 -0
- package/dist/models/fitOptions.js +16 -0
- package/dist/models/glm.d.ts +67 -5
- package/dist/models/glm.js +105 -27
- package/dist/models/glmerStructure.d.ts +5 -0
- package/dist/models/glmerStructure.js +2 -2
- package/dist/models/lm.js +3 -1
- package/dist/models/step.d.ts +250 -0
- package/dist/models/step.js +592 -0
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/random/mvrnorm.d.ts +60 -0
- package/dist/random/mvrnorm.js +237 -0
- package/dist/regression.js +2 -2
- package/dist/rstats.js +17 -17
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -15
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-3746VXPM.js +0 -2
- package/dist/chunks/chunk-3DA7CMFN.js +0 -2
- package/dist/chunks/chunk-4O6XLQBN.js +0 -2
- package/dist/chunks/chunk-7BBIQY4W.js +0 -2
- package/dist/chunks/chunk-7FGVMDJE.js +0 -3
- package/dist/chunks/chunk-AONDWNMF.js +0 -2
- package/dist/chunks/chunk-F6KKW5YN.js +0 -2
- package/dist/chunks/chunk-JCGCJT7T.js +0 -2
- package/dist/chunks/chunk-KITNZUYC.js +0 -3
- package/dist/chunks/chunk-MCEZRRQO.js +0 -2
- package/dist/chunks/chunk-MJHFGGPU.js +0 -2
- package/dist/chunks/chunk-SDIIGAY5.js +0 -2
- package/dist/chunks/chunk-VWCCPBBG.js +0 -2
- package/dist/chunks/chunk-Y7VD43TO.js +0 -2
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// Multivariate normal random vectors (MASS's mvrnorm), and the symmetric square root of a covariance matrix that
|
|
2
|
+
// mediation's mediate draws its parameters with. Written for rStats.js.
|
|
3
|
+
//
|
|
4
|
+
// Sources: the specification oracle/mediation-behaviour.md, from MASS's documentation and published output
|
|
5
|
+
// (experiments/mediation116.R, mediation116-eigensign.R; every attempt in experiments/mediation116-log.md); the 2 x 2
|
|
6
|
+
// eigendecomposition is a port of LAPACK 3.12.0's SRC/dlaev2.f (modified BSD; see THIRD_PARTY_NOTICES.md).
|
|
7
|
+
// - mvrnorm(n, mu, Sigma) = mu + V diag(sqrt(max(lambda, 0))) Z' with Sigma = V diag(lambda) V' (eigenvalues
|
|
8
|
+
// decreasing) and Z an n x p matrix of standard normals filled by column (p blocks of n draws)
|
|
9
|
+
// - Its values depend on the signs of the eigenvectors. R takes them from LAPACK's dsyevr: for 2 x 2 matrices that is
|
|
10
|
+
// dlaev2's closed form (ported here, so the values are R's); for 3 or more dimensions dsyevr's MRRR path (dstemr)
|
|
11
|
+
// picks the signs from intermediate quantities, which is not ported (the user's decision 2026-11-25): the vectors
|
|
12
|
+
// come from rStats.js's eigen, the draws have R's distribution but not R's values
|
|
13
|
+
// - The symmetric square root V diag(sqrt(max(lambda, 0))) V' does not depend on the signs. It is computed with the
|
|
14
|
+
// cyclic Jacobi method on the matrix itself, which finds the eigenvalues of a graded matrix (variances on very
|
|
15
|
+
// different scales, such as the covariance of coefficients of variables x 1e150 and x 1) to relative accuracy; LAPACK's
|
|
16
|
+
// (and EISPACK's) methods lose the small ones, so R's draws break down there (experiments/mediation116-edge.R). At
|
|
17
|
+
// ordinary scales the result equals R's to rounding
|
|
18
|
+
// - 3 or more dimensions in mvrnorm: the same Jacobi decomposition, each eigenvector's largest component positive
|
|
19
|
+
import { rnorm } from "./continuous.js";
|
|
20
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
21
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
22
|
+
const MVRNORM_KEYS = ["tol"];
|
|
23
|
+
/** LAPACK's dlaev2: the eigendecomposition of [[a, b], [b, c]]; rt1 has the larger absolute value, (cs1, sn1) its vector */
|
|
24
|
+
function laev2(a, b, c) {
|
|
25
|
+
const sm = a + c, df = a - c, adf = Math.abs(df), tb = b + b, ab = Math.abs(tb);
|
|
26
|
+
const [acmx, acmn] = Math.abs(a) > Math.abs(c) ? [a, c] : [c, a];
|
|
27
|
+
const rt = adf > ab ? adf * Math.sqrt(1 + (ab / adf) ** 2) : adf < ab ? ab * Math.sqrt(1 + (adf / ab) ** 2) : ab * Math.SQRT2;
|
|
28
|
+
let rt1, rt2, sgn1;
|
|
29
|
+
if (sm < 0) {
|
|
30
|
+
rt1 = 0.5 * (sm - rt);
|
|
31
|
+
sgn1 = -1;
|
|
32
|
+
rt2 = (acmx / rt1) * acmn - (b / rt1) * b;
|
|
33
|
+
}
|
|
34
|
+
else if (sm > 0) {
|
|
35
|
+
rt1 = 0.5 * (sm + rt);
|
|
36
|
+
sgn1 = 1;
|
|
37
|
+
rt2 = (acmx / rt1) * acmn - (b / rt1) * b;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
rt1 = 0.5 * rt;
|
|
41
|
+
rt2 = -0.5 * rt;
|
|
42
|
+
sgn1 = 1;
|
|
43
|
+
}
|
|
44
|
+
let cs, sgn2;
|
|
45
|
+
if (df >= 0) {
|
|
46
|
+
cs = df + rt;
|
|
47
|
+
sgn2 = 1;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
cs = df - rt;
|
|
51
|
+
sgn2 = -1;
|
|
52
|
+
}
|
|
53
|
+
let cs1, sn1;
|
|
54
|
+
if (Math.abs(cs) > ab) {
|
|
55
|
+
const ct = -tb / cs;
|
|
56
|
+
sn1 = 1 / Math.sqrt(1 + ct * ct);
|
|
57
|
+
cs1 = ct * sn1;
|
|
58
|
+
}
|
|
59
|
+
else if (ab === 0) {
|
|
60
|
+
cs1 = 1;
|
|
61
|
+
sn1 = 0;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const tn = -cs / tb;
|
|
65
|
+
cs1 = 1 / Math.sqrt(1 + tn * tn);
|
|
66
|
+
sn1 = tn * cs1;
|
|
67
|
+
}
|
|
68
|
+
if (sgn1 === sgn2) {
|
|
69
|
+
const tn = cs1;
|
|
70
|
+
cs1 = -sn1;
|
|
71
|
+
sn1 = tn;
|
|
72
|
+
}
|
|
73
|
+
return { rt1, rt2, cs1, sn1 };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The cyclic Jacobi method for a symmetric matrix: eigenvalues (decreasing) and eigenvectors (columns, array of rows,
|
|
77
|
+
* each with its largest component positive). Rotations are taken while an off-diagonal entry exceeds eps times the
|
|
78
|
+
* geometric mean of its diagonal entries, so that graded matrices keep their small eigenvalues to relative accuracy.
|
|
79
|
+
*/
|
|
80
|
+
export function jacobiEigen(S) {
|
|
81
|
+
const p = S.length;
|
|
82
|
+
// the lower triangle, as R's eigen(symmetric = TRUE) reads it
|
|
83
|
+
const a = Array.from({ length: p }, (_, i) => Array.from({ length: p }, (_, j) => (i >= j ? S[i][j] : S[j][i])));
|
|
84
|
+
const v = Array.from({ length: p }, (_, i) => Array.from({ length: p }, (_, j) => (i === j ? 1 : 0)));
|
|
85
|
+
const eps = 2 ** -53;
|
|
86
|
+
for (let sweep = 0; sweep < 100; sweep++) {
|
|
87
|
+
let rotated = false;
|
|
88
|
+
for (let i = 0; i < p - 1; i++)
|
|
89
|
+
for (let j = i + 1; j < p; j++) {
|
|
90
|
+
const aij = a[i][j];
|
|
91
|
+
if (aij === 0)
|
|
92
|
+
continue;
|
|
93
|
+
const aii = a[i][i], ajj = a[j][j];
|
|
94
|
+
if (Math.abs(aij) <= eps * Math.sqrt(Math.abs(aii)) * Math.sqrt(Math.abs(ajj))) {
|
|
95
|
+
a[i][j] = a[j][i] = 0;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
rotated = true;
|
|
99
|
+
// the rotation zeroing a_ij: t = tan, the smaller root of t^2 + 2 theta t - 1 = 0
|
|
100
|
+
const theta = (ajj - aii) / (2 * aij);
|
|
101
|
+
const t = Math.abs(theta) > 1e150 ? 1 / (2 * theta) : Math.sign(theta || 1) / (Math.abs(theta) + Math.sqrt(theta * theta + 1));
|
|
102
|
+
const c = 1 / Math.sqrt(1 + t * t), sn = t * c, tau = sn / (1 + c);
|
|
103
|
+
a[i][i] = aii - t * aij;
|
|
104
|
+
a[j][j] = ajj + t * aij;
|
|
105
|
+
a[i][j] = a[j][i] = 0;
|
|
106
|
+
for (let k = 0; k < p; k++) {
|
|
107
|
+
if (k !== i && k !== j) {
|
|
108
|
+
const aki = a[k][i], akj = a[k][j];
|
|
109
|
+
a[k][i] = a[i][k] = aki - sn * (akj + tau * aki);
|
|
110
|
+
a[k][j] = a[j][k] = akj + sn * (aki - tau * akj);
|
|
111
|
+
}
|
|
112
|
+
const vki = v[k][i], vkj = v[k][j];
|
|
113
|
+
v[k][i] = vki - sn * (vkj + tau * vki);
|
|
114
|
+
v[k][j] = vkj + sn * (vki - tau * vkj);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (!rotated)
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
const order = Array.from({ length: p }, (_, k) => k).sort((x, y) => a[y][y] - a[x][x]);
|
|
121
|
+
const vectors = Array.from({ length: p }, () => new Array(p));
|
|
122
|
+
order.forEach((k, c) => {
|
|
123
|
+
let m = 0;
|
|
124
|
+
for (let r = 1; r < p; r++)
|
|
125
|
+
if (Math.abs(v[r][k]) > Math.abs(v[m][k]))
|
|
126
|
+
m = r;
|
|
127
|
+
const sg = v[m][k] < 0 ? -1 : 1;
|
|
128
|
+
for (let r = 0; r < p; r++)
|
|
129
|
+
vectors[r][c] = sg * v[r][k];
|
|
130
|
+
});
|
|
131
|
+
return { values: order.map((k) => a[k][k]), vectors };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* The symmetric eigendecomposition with R's (LAPACK's) eigenvector signs where they are known: eigenvalues in
|
|
135
|
+
* decreasing order, eigenvectors as columns (array of rows). 1 x 1 and 2 x 2 as R's; larger by the Jacobi method.
|
|
136
|
+
*/
|
|
137
|
+
export function symmetricEigen(S) {
|
|
138
|
+
const p = S.length;
|
|
139
|
+
if (p === 1)
|
|
140
|
+
return { values: [S[0][0]], vectors: [[1]] };
|
|
141
|
+
if (p === 2) {
|
|
142
|
+
// dsyevr reads the lower triangle (R calls it with uplo = "L")
|
|
143
|
+
const { rt1, rt2, cs1, sn1 } = laev2(S[0][0], S[1][0], S[1][1]);
|
|
144
|
+
// dlaev2's (cs1, sn1) belongs to rt1, (-sn1, cs1) to rt2; R lists the larger eigenvalue first
|
|
145
|
+
return rt1 >= rt2
|
|
146
|
+
? { values: [rt1, rt2], vectors: [[cs1, -sn1], [sn1, cs1]] }
|
|
147
|
+
: { values: [rt2, rt1], vectors: [[-sn1, cs1], [cs1, sn1]] };
|
|
148
|
+
}
|
|
149
|
+
return jacobiEigen(S);
|
|
150
|
+
}
|
|
151
|
+
/** V diag(sqrt(max(lambda, 0))) V', the symmetric square root of a covariance matrix (sign-free), by the Jacobi method */
|
|
152
|
+
export function symmetricSqrt(S) {
|
|
153
|
+
const p = S.length;
|
|
154
|
+
if (p === 1)
|
|
155
|
+
return [[Math.sqrt(Math.max(S[0][0], 0))]];
|
|
156
|
+
const { values, vectors } = jacobiEigen(S);
|
|
157
|
+
const r = values.map((v) => Math.sqrt(Math.max(v, 0)));
|
|
158
|
+
const out = [];
|
|
159
|
+
for (let i = 0; i < p; i++) {
|
|
160
|
+
const row = [];
|
|
161
|
+
for (let j = 0; j < p; j++) {
|
|
162
|
+
let s = 0;
|
|
163
|
+
for (let k = 0; k < p; k++)
|
|
164
|
+
s += vectors[i][k] * r[k] * vectors[j][k];
|
|
165
|
+
row.push(s);
|
|
166
|
+
}
|
|
167
|
+
out.push(row);
|
|
168
|
+
}
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
/** Checks a covariance matrix for mvrnorm and the parameter draws (square, finite, symmetric, matching mu) */
|
|
172
|
+
export function checkCovariance(Sigma, p) {
|
|
173
|
+
if (Sigma.length !== p || Sigma.some((r) => r.length !== p))
|
|
174
|
+
throw new RangeError("incompatible arguments");
|
|
175
|
+
for (const r of Sigma)
|
|
176
|
+
for (const v of r)
|
|
177
|
+
if (!Number.isFinite(v))
|
|
178
|
+
throw new RangeError("'Sigma' must be finite");
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Random vectors from the multivariate normal distribution. Equivalent to R's `MASS::mvrnorm()`.
|
|
182
|
+
*
|
|
183
|
+
* `Sigma` is decomposed as `V diag(lambda) V'` and each draw is `mu + V diag(sqrt(lambda)) z` with `z` taken from
|
|
184
|
+
* `rnorm(n * p)` column by column (all `n` draws of the first component, then the second, ...), as in R. The values
|
|
185
|
+
* depend on the signs of the eigenvectors: for 1 or 2 dimensions they are R's (LAPACK's closed form for 2 x 2), so the
|
|
186
|
+
* draws equal R's under the same {@link setSeed}; for 3 or more dimensions rStats.js's eigenvectors may differ from
|
|
187
|
+
* LAPACK's in sign, and the draws then have R's distribution but not R's values.
|
|
188
|
+
*
|
|
189
|
+
* Unlike R, which returns a vector when `n` is 1, the result always has one row per draw. MASS's `empirical`
|
|
190
|
+
* argument is not offered.
|
|
191
|
+
*
|
|
192
|
+
* @param n Number of draws (fractions are truncated, as in R).
|
|
193
|
+
* @param mu The mean vector (length p).
|
|
194
|
+
* @param Sigma The covariance matrix (p x p, symmetric; only the lower triangle is read for 2 x 2, as R).
|
|
195
|
+
* @param options See {@link MvrnormOptions}.
|
|
196
|
+
* @returns An array of `n` rows of length p.
|
|
197
|
+
* @throws RangeError if the dimensions do not match ("incompatible arguments") or `Sigma` is not positive
|
|
198
|
+
* semi-definite within `tol` ("'Sigma' is not positive definite").
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* setSeed(42);
|
|
203
|
+
* mvrnorm(2, [0, 0], [[2, 0.6], [0.6, 1]]); // [[-1.7445, -1.1571], [0.99996, -0.12421]] (R: set.seed(42); MASS::mvrnorm(...))
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @category Random numbers
|
|
207
|
+
*/
|
|
208
|
+
export function mvrnorm(n, mu, Sigma, options = {}) {
|
|
209
|
+
checkOptions(options, MVRNORM_KEYS, "mvrnorm"); // generated: option-keys.mjs
|
|
210
|
+
const tol = options.tol ?? 1e-6;
|
|
211
|
+
const S = Array.from(Sigma, (r) => Array.from(r));
|
|
212
|
+
if (S.some((r) => r.length !== S[0].length))
|
|
213
|
+
throw new TypeError("all rows of the matrix must have the same length");
|
|
214
|
+
const p = mu.length;
|
|
215
|
+
checkCovariance(S, p);
|
|
216
|
+
const count = Math.trunc(n);
|
|
217
|
+
// R's mvrnorm errors for n = 0 ("non-conformable arguments")
|
|
218
|
+
if (!(count >= 1))
|
|
219
|
+
throw new RangeError(count === 0 ? "non-conformable arguments" : "invalid arguments");
|
|
220
|
+
const { values, vectors } = symmetricEigen(S);
|
|
221
|
+
if (!values.every((v) => v >= -tol * Math.abs(values[0])))
|
|
222
|
+
throw new RangeError("'Sigma' is not positive definite");
|
|
223
|
+
const r = values.map((v) => Math.sqrt(Math.max(v, 0)));
|
|
224
|
+
const z = rnorm(count * p);
|
|
225
|
+
const out = [];
|
|
226
|
+
for (let i = 0; i < count; i++) {
|
|
227
|
+
const row = [];
|
|
228
|
+
for (let a = 0; a < p; a++) {
|
|
229
|
+
let s = 0;
|
|
230
|
+
for (let k = 0; k < p; k++)
|
|
231
|
+
s += vectors[a][k] * r[k] * z[k * count + i];
|
|
232
|
+
row.push(mu[a] + s);
|
|
233
|
+
}
|
|
234
|
+
out.push(row);
|
|
235
|
+
}
|
|
236
|
+
return out;
|
|
237
|
+
}
|