@numero-uno-jpn/rstats 0.12.0 → 0.14.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 +76 -0
- package/README.ja.md +26 -17
- package/README.md +25 -16
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/dist/chunks/{chunk-VQHEOBTM.js → chunk-6JRESWVF.js} +2 -2
- package/dist/chunks/chunk-7CPM3M6Q.js +2 -0
- package/dist/chunks/chunk-7PSGKKOP.js +2 -0
- package/dist/chunks/{chunk-LEIH4U5X.js → chunk-7T74KLE5.js} +2 -2
- package/dist/chunks/{chunk-XJQKSR52.js → chunk-A76XBA3D.js} +1 -1
- package/dist/chunks/chunk-ACE64PMM.js +3 -0
- package/dist/chunks/chunk-DCGBYO44.js +2 -0
- package/dist/chunks/chunk-DWNGNTS6.js +2 -0
- package/dist/chunks/chunk-FBDZ6B2G.js +2 -0
- package/dist/chunks/chunk-IBJ7RI44.js +2 -0
- package/dist/chunks/chunk-IGSR4GIF.js +2 -0
- package/dist/chunks/{chunk-6BH65HGN.js → chunk-NTETWLP3.js} +1 -1
- package/dist/chunks/chunk-Q4JF4QHS.js +2 -0
- package/dist/chunks/chunk-RZIWVCHO.js +2 -0
- package/dist/chunks/chunk-SD7ML5PE.js +2 -0
- package/dist/chunks/chunk-SZ357HRC.js +3 -0
- package/dist/chunks/chunk-WM33ADZV.js +2 -0
- package/dist/chunks/{chunk-OX3SBG5X.js → chunk-XCAMWVNO.js} +1 -1
- package/dist/chunks/chunk-YQLEXH2Y.js +3 -0
- package/dist/chunks/chunk-YTV3RFCA.js +2 -0
- package/dist/chunks/chunk-ZUESNU7W.js +2 -0
- package/dist/data/grouping.js +11 -4
- package/dist/descriptive/basic.js +57 -49
- package/dist/descriptive/cov.js +7 -2
- package/dist/descriptive/factanal.d.ts +16 -0
- package/dist/descriptive/factanal.js +26 -0
- package/dist/descriptive/hist.d.ts +8 -2
- package/dist/descriptive/hist.js +10 -2
- package/dist/descriptive/quantile.d.ts +8 -3
- package/dist/descriptive/quantile.js +10 -6
- package/dist/descriptive/table.d.ts +7 -5
- package/dist/descriptive/table.js +39 -22
- package/dist/descriptive.js +2 -2
- package/dist/distributions/beta.js +17 -0
- package/dist/distributions/binomial.js +3 -0
- package/dist/distributions/gamma.js +7 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +3 -1
- package/dist/entries/mixed.js +4 -1
- package/dist/entries/tests.d.ts +1 -1
- package/dist/htest/agreement.js +7 -2
- package/dist/htest/groups.js +7 -3
- package/dist/htest/mcnemarTest.js +6 -2
- package/dist/htest/pairwise.js +6 -3
- package/dist/htest/polycor.js +5 -3
- package/dist/htest/tTest.d.ts +3 -2
- package/dist/htest/tTest.js +6 -3
- package/dist/htest/trendTests.js +10 -5
- package/dist/internal/fround.d.ts +12 -0
- package/dist/internal/fround.js +89 -0
- package/dist/internal/levels.d.ts +11 -0
- package/dist/internal/levels.js +48 -0
- package/dist/internal/summary.d.ts +19 -1
- package/dist/internal/summary.js +69 -8
- package/dist/linalg/balance.d.ts +21 -0
- package/dist/linalg/balance.js +191 -0
- package/dist/linalg/eigen.d.ts +1 -1
- package/dist/linalg/eigen.js +8 -2
- package/dist/linalg/expm.d.ts +1 -11
- package/dist/linalg/expm.js +2 -158
- package/dist/linalg/kappa.d.ts +6 -2
- package/dist/linalg/kappa.js +16 -7
- package/dist/linalg/lu.d.ts +13 -2
- package/dist/linalg/lu.js +17 -3
- package/dist/linalg/qr.d.ts +4 -1
- package/dist/linalg/qr.js +2 -2
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -3
- package/dist/models/aovEz.js +6 -4
- package/dist/models/coxph.js +5 -2
- package/dist/models/glm.d.ts +4 -1
- package/dist/models/glmFamily.d.ts +1 -1
- package/dist/models/glmFamily.js +24 -2
- package/dist/models/lmer.d.ts +37 -4
- package/dist/models/lmer.js +18 -1
- package/dist/models/lmerAnova.d.ts +138 -0
- package/dist/models/lmerAnova.js +134 -0
- package/dist/models/lmerHypotheses.d.ts +105 -0
- package/dist/models/lmerHypotheses.js +303 -0
- package/dist/models/lmerTest.d.ts +233 -0
- package/dist/models/lmerTest.js +306 -0
- package/dist/models/mauchly.js +2 -1
- package/dist/models/mixedDerivatives.d.ts +27 -0
- package/dist/models/mixedDerivatives.js +342 -0
- package/dist/models/mixedDeviance.d.ts +112 -0
- package/dist/models/mixedDeviance.js +216 -86
- package/dist/models/mixedStructure.d.ts +5 -3
- package/dist/models/modelFrame.js +2 -1
- package/dist/models/modelMatrix.d.ts +10 -3
- package/dist/models/modelMatrix.js +23 -2
- package/dist/models/survival.js +5 -1
- package/dist/numerics/integrate.d.ts +1 -1
- package/dist/numerics/integrate.js +13 -3
- package/dist/numerics/interp.d.ts +1 -1
- package/dist/numerics/interp.js +3 -0
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special/beta.js +10 -20
- package/dist/special.js +2 -2
- package/dist/tests.js +11 -12
- package/llms.txt +6 -1
- package/package.json +1 -1
- package/dist/chunks/chunk-3VMBBQLV.js +0 -3
- package/dist/chunks/chunk-4OKAZJJG.js +0 -3
- package/dist/chunks/chunk-ATC4F4BX.js +0 -2
- package/dist/chunks/chunk-EDH53DXN.js +0 -2
- package/dist/chunks/chunk-EZ2KD5NA.js +0 -2
- package/dist/chunks/chunk-GSPOO2G3.js +0 -2
- package/dist/chunks/chunk-K7AZ65H7.js +0 -2
- package/dist/chunks/chunk-SGZQDJRR.js +0 -2
- package/dist/chunks/chunk-UH2N3ZQW.js +0 -2
- package/dist/chunks/chunk-UV2MZYDQ.js +0 -2
- package/dist/chunks/chunk-WJQZZYOI.js +0 -2
- package/dist/chunks/chunk-WKEFHOFR.js +0 -2
- package/dist/chunks/chunk-WXC25OPJ.js +0 -2
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
// lmerTest's tests for the fixed effects of a linear mixed model fitted by lmer (src/models/lmer.ts): t- and F-tests
|
|
2
|
+
// of contrasts with Satterthwaite's approximation of the denominator degrees of freedom, the coefficient table of
|
|
3
|
+
// lmerTest's summary, and lmerTest's contest.
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js (no ported code) from oracle/lmertest-behaviour.md (section 1, "The Satterthwaite machinery")
|
|
6
|
+
// and A. Kuznetsova, P. B. Brockhoff, R. H. B. Christensen (2017), "lmerTest Package: Tests in Linear Mixed Effects
|
|
7
|
+
// Models", Journal of Statistical Software 82(13). The derivatives (the Hessian of the deviance in the variance
|
|
8
|
+
// parameters and the Jacobians of the covariance of the fixed effects) are computed analytically at the exact optimum
|
|
9
|
+
// by src/models/mixedDerivatives.ts; lmerTest differentiates numerically at lme4's approximate optimum, so its numbers
|
|
10
|
+
// typically differ from these in the 4th to 8th significant digit.
|
|
11
|
+
//
|
|
12
|
+
// The Satterthwaite quantities of a fit are computed once and kept with the fit (a WeakMap keyed by the result), so
|
|
13
|
+
// that the summary, the contrasts and the ANOVA tables of the same fit share them (and the Hessian's warnings are the
|
|
14
|
+
// same in every result).
|
|
15
|
+
import { pf1 } from "../distributions/f.js";
|
|
16
|
+
import { pt1, qt1 } from "../distributions/t.js";
|
|
17
|
+
import { eigen } from "../linalg/eigen.js";
|
|
18
|
+
import { ginv } from "../linalg/kappa.js";
|
|
19
|
+
import { Warnings } from "./glmFamily.js";
|
|
20
|
+
import { lmerInternals } from "./lmer.js";
|
|
21
|
+
import { varparDerivatives } from "./mixedDerivatives.js";
|
|
22
|
+
/** Eigenvalues of the Hessian within this of 0 (or below) are left out of its inverse and warned about */
|
|
23
|
+
const HESSIAN_TOLERANCE = 1e-8;
|
|
24
|
+
/** Eigenvalues of L V L' at most this times the largest are left out of a joint test */
|
|
25
|
+
const RANK_TOLERANCE = Math.sqrt(Number.EPSILON);
|
|
26
|
+
/** Satterthwaite's degrees of freedom of a joint test are averaged when they differ by less than this */
|
|
27
|
+
const EQUAL_DF_TOLERANCE = 1e-8;
|
|
28
|
+
const satterthwaiteByFit = new WeakMap();
|
|
29
|
+
/** A number as C's printf("%1.1e") writes it: "-3.2e-04" */
|
|
30
|
+
function formatE1(v) {
|
|
31
|
+
if (!Number.isFinite(v))
|
|
32
|
+
return Number.isNaN(v) ? "nan" : v > 0 ? "inf" : "-inf";
|
|
33
|
+
const [mantissa, exponent] = v.toExponential(1).split("e");
|
|
34
|
+
const e = Number(exponent);
|
|
35
|
+
return `${mantissa}e${e < 0 ? "-" : "+"}${String(Math.abs(e)).padStart(2, "0")}`;
|
|
36
|
+
}
|
|
37
|
+
/** The warnings about the Hessian's eigenvalues (negative, or close to 0; internal) */
|
|
38
|
+
export function hessianWarnings(values) {
|
|
39
|
+
const out = [];
|
|
40
|
+
const negative = values.filter((v) => v < -HESSIAN_TOLERANCE);
|
|
41
|
+
const zero = values.filter((v) => Math.abs(v) < HESSIAN_TOLERANCE);
|
|
42
|
+
const plural = (k) => (k === 1 ? "eigenvalue" : "eigenvalues");
|
|
43
|
+
if (negative.length > 0) {
|
|
44
|
+
out.push(`Model failed to converge with ${negative.length} negative ${plural(negative.length)}: ${negative.map(formatE1).join(" ")}`);
|
|
45
|
+
}
|
|
46
|
+
if (zero.length > 0) {
|
|
47
|
+
out.push(`Model may not have converged with ${zero.length} ${plural(zero.length)} close to zero: ${zero.map(formatE1).join(" ")}`);
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The Satterthwaite quantities of a fit by {@link lmer} (see {@link LmerSatterthwaite}), computed once per fit.
|
|
53
|
+
* Internal: the summary, the contrasts and the ANOVA tables use it.
|
|
54
|
+
*/
|
|
55
|
+
export function lmerSatterthwaite(fit) {
|
|
56
|
+
const cached = satterthwaiteByFit.get(fit);
|
|
57
|
+
if (cached !== undefined)
|
|
58
|
+
return cached;
|
|
59
|
+
const internals = lmerInternals(fit);
|
|
60
|
+
const result = satterthwaiteAt(internals.structure, internals.theta, internals.reml);
|
|
61
|
+
satterthwaiteByFit.set(fit, result);
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The Satterthwaite quantities at any θ (σ from the penalised residual sum of squares there). Internal: the test
|
|
66
|
+
* evaluates them at lme4's optimum too, to compare with lmerTest's numerical derivatives at the same point.
|
|
67
|
+
*/
|
|
68
|
+
export function satterthwaiteAt(structure, theta, reml) {
|
|
69
|
+
const d = varparDerivatives(structure, theta, reml);
|
|
70
|
+
const m = d.varpar.length;
|
|
71
|
+
const e = eigen(d.hessian, { symmetric: true });
|
|
72
|
+
const values = e.values;
|
|
73
|
+
const vectors = e.vectors;
|
|
74
|
+
const vcovVarpar = Array.from({ length: m }, () => new Array(m).fill(0));
|
|
75
|
+
values.forEach((lambda, k) => {
|
|
76
|
+
if (!(lambda > HESSIAN_TOLERANCE))
|
|
77
|
+
return;
|
|
78
|
+
for (let i = 0; i < m; i++) {
|
|
79
|
+
const vi = (2 * vectors[i][k]) / lambda;
|
|
80
|
+
for (let j = 0; j < m; j++)
|
|
81
|
+
vcovVarpar[i][j] += vi * vectors[j][k];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
varpar: d.varpar,
|
|
86
|
+
hessian: d.hessian,
|
|
87
|
+
hessianEigenvalues: values,
|
|
88
|
+
vcovVarpar,
|
|
89
|
+
beta: d.beta,
|
|
90
|
+
sigma: d.varpar[m - 1],
|
|
91
|
+
vcov: d.vcov,
|
|
92
|
+
jacobian: d.jacobian,
|
|
93
|
+
warnings: hessianWarnings(values),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const quadraticForm = (l, m) => {
|
|
97
|
+
let s = 0;
|
|
98
|
+
for (let i = 0; i < l.length; i++) {
|
|
99
|
+
const li = l[i];
|
|
100
|
+
if (li === 0)
|
|
101
|
+
continue;
|
|
102
|
+
const row = m[i];
|
|
103
|
+
for (let j = 0; j < l.length; j++)
|
|
104
|
+
s += li * row[j] * l[j];
|
|
105
|
+
}
|
|
106
|
+
return s;
|
|
107
|
+
};
|
|
108
|
+
const dot = (a, b) => a.reduce((s, v, i) => s + v * b[i], 0);
|
|
109
|
+
/** l'β̂ − rhs over its standard error, with Satterthwaite's degrees of freedom 2 var² / (g'Ag), g_k = l'J_k l */
|
|
110
|
+
export function tTestOf(s, l, rhs, beta = s.beta) {
|
|
111
|
+
if (l.some(Number.isNaN) || Number.isNaN(rhs))
|
|
112
|
+
return { estimate: NaN, stdError: NaN, df: NaN, tValue: NaN, pValue: NaN };
|
|
113
|
+
const estimate = dot(l, beta);
|
|
114
|
+
const variance = quadraticForm(l, s.vcov);
|
|
115
|
+
const stdError = Math.sqrt(variance);
|
|
116
|
+
const g = s.jacobian.map((j) => quadraticForm(l, j));
|
|
117
|
+
const df = (2 * variance * variance) / quadraticForm(g, s.vcovVarpar);
|
|
118
|
+
const tValue = (estimate - rhs) / stdError;
|
|
119
|
+
const pValue = 2 * pt1(Math.abs(tValue), df, false, false);
|
|
120
|
+
return { estimate, stdError, df, tValue, pValue };
|
|
121
|
+
}
|
|
122
|
+
function fRowOf(s, f, numDF, denDF) {
|
|
123
|
+
const s2 = s.sigma * s.sigma;
|
|
124
|
+
return { sumSq: f * s2 * numDF, meanSq: f * s2, numDF, denDF, F: f, pValue: pf1(f, numDF, denDF, false, false) };
|
|
125
|
+
}
|
|
126
|
+
/** The denominator degrees of freedom of a joint test from those of its rotated contrasts */
|
|
127
|
+
function jointDenDF(nu) {
|
|
128
|
+
if (nu.length === 1)
|
|
129
|
+
return nu[0];
|
|
130
|
+
if (nu.every((v, i) => i === 0 || Math.abs(v - nu[i - 1]) < EQUAL_DF_TOLERANCE))
|
|
131
|
+
return nu.reduce((a, b) => a + b, 0) / nu.length;
|
|
132
|
+
if (nu.some((v) => v <= 2))
|
|
133
|
+
return 2;
|
|
134
|
+
const e = nu.reduce((sum, v) => sum + v / (v - 2), 0);
|
|
135
|
+
return (2 * e) / (e - nu.length);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The joint F-test of the rows of L (r × p) with Satterthwaite's denominator degrees of freedom (internal; the ANOVA
|
|
139
|
+
* tables use it): undefined when the rows have rank 0 (no test). `rhs` has one value per row (default 0); a non-zero
|
|
140
|
+
* right-hand side with a rank-deficient L adds the warning to `warnings`.
|
|
141
|
+
*/
|
|
142
|
+
export function lmerFTest(fit, L, rhs, warnings) {
|
|
143
|
+
return fTestOf(lmerSatterthwaite(fit), L, rhs, warnings);
|
|
144
|
+
}
|
|
145
|
+
/** {@link lmerFTest} with the Satterthwaite quantities given (internal) */
|
|
146
|
+
export function fTestOf(s, L, rhs, warnings) {
|
|
147
|
+
const r = L.length;
|
|
148
|
+
if (r === 0)
|
|
149
|
+
return undefined;
|
|
150
|
+
const right = rhs ?? new Array(r).fill(0);
|
|
151
|
+
if (r === 1) {
|
|
152
|
+
const t = tTestOf(s, L[0], right[0]);
|
|
153
|
+
return fRowOf(s, t.tValue * t.tValue, 1, t.df);
|
|
154
|
+
}
|
|
155
|
+
if (L.some((row) => row.some(Number.isNaN)) || right.some(Number.isNaN))
|
|
156
|
+
return fRowOf(s, NaN, NaN, NaN);
|
|
157
|
+
// L V L' and its eigendecomposition
|
|
158
|
+
const lv = L.map((row) => s.vcov.map((_, j) => row.reduce((sum, v, k) => sum + v * s.vcov[k][j], 0)));
|
|
159
|
+
const e = eigen(lv.map((a) => L.map((rj) => dot(a, rj))), { symmetric: true });
|
|
160
|
+
const d = e.values;
|
|
161
|
+
const cut = Math.max(RANK_TOLERANCE * d[0], 0);
|
|
162
|
+
let kept = 0;
|
|
163
|
+
while (kept < r && d[kept] > cut)
|
|
164
|
+
kept++;
|
|
165
|
+
if (kept === 0)
|
|
166
|
+
return undefined;
|
|
167
|
+
if (kept < r && right.some((v) => v !== 0))
|
|
168
|
+
warnings.add("Contrast is rank deficient and test may be affected");
|
|
169
|
+
// β̂ − b0 with b0 = ginv(L) rhs, the minimum-norm solution of L b0 = rhs
|
|
170
|
+
let beta = s.beta;
|
|
171
|
+
if (right.some((v) => v !== 0)) {
|
|
172
|
+
const g = ginv(L.map((row) => [...row]));
|
|
173
|
+
beta = s.beta.map((b, i) => b - dot(g[i], right));
|
|
174
|
+
}
|
|
175
|
+
const p = s.beta.length;
|
|
176
|
+
const rotated = Array.from({ length: kept }, (_, k) => Array.from({ length: p }, (_, j) => L.reduce((sum, row, i) => sum + e.vectors[i][k] * row[j], 0)));
|
|
177
|
+
if (kept === 1) {
|
|
178
|
+
const t = tTestOf(s, rotated[0], 0, beta);
|
|
179
|
+
return fRowOf(s, t.tValue * t.tValue, 1, t.df);
|
|
180
|
+
}
|
|
181
|
+
let f = 0;
|
|
182
|
+
const nu = [];
|
|
183
|
+
rotated.forEach((row, i) => {
|
|
184
|
+
f += dot(row, beta) ** 2 / d[i];
|
|
185
|
+
const g = s.jacobian.map((j) => quadraticForm(row, j));
|
|
186
|
+
nu.push((2 * d[i] * d[i]) / quadraticForm(g, s.vcovVarpar));
|
|
187
|
+
});
|
|
188
|
+
return fRowOf(s, f / kept, kept, jointDenDF(nu));
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* The coefficient table of a linear mixed model fitted by {@link lmer}, with Satterthwaite's degrees of freedom and
|
|
192
|
+
* the p-values of the t-tests: the `summary()` of a fit by lmerTest. Equivalent to `lmerTest::summary()` in R.
|
|
193
|
+
*
|
|
194
|
+
* The degrees of freedom of each coefficient are Satterthwaite's: 2 var² / (g'Ag), with var its variance, g its
|
|
195
|
+
* derivative in the variance parameters (θ, σ) and A their asymptotic covariance (twice the inverse Hessian of the
|
|
196
|
+
* deviance). The derivatives are computed analytically at the exact optimum (lmerTest differentiates numerically at
|
|
197
|
+
* lme4's optimum, so its degrees of freedom typically differ in the 5th to 8th significant digit). With
|
|
198
|
+
* `ddf: "lme4"` the table is lme4's (as `coefTable` of the fit), without degrees of freedom and p-values.
|
|
199
|
+
*
|
|
200
|
+
* @param fit A result of {@link lmer} (the object it returned, not a copy).
|
|
201
|
+
* @param options `ddf`: `"Satterthwaite"` (default) or `"lme4"`.
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* ```ts
|
|
205
|
+
* // R: summary(lmerTest::lmer(y ~ x + (1 | g), data))
|
|
206
|
+
* const fit = lmer("y ~ x + (1 | g)", {
|
|
207
|
+
* y: [3.1, 4.0, 5.2, 6.8, 7.1, 8.3, 2.2, 3.5, 3.9, 5.9, 6.1, 7.4],
|
|
208
|
+
* x: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
|
|
209
|
+
* g: ["a", "a", "a", "b", "b", "b", "c", "c", "c", "d", "d", "d"],
|
|
210
|
+
* });
|
|
211
|
+
* const s = lmerSummary(fit);
|
|
212
|
+
* s.names; // ["(Intercept)", "x"]
|
|
213
|
+
* s.df; // [3.2717, 7]
|
|
214
|
+
* s.pValue; // [0.032598, 7.9895e-5]
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* @category Mixed models
|
|
218
|
+
*/
|
|
219
|
+
export function lmerSummary(fit, options = {}) {
|
|
220
|
+
const ddf = options.ddf ?? "Satterthwaite";
|
|
221
|
+
if (ddf !== "Satterthwaite" && ddf !== "lme4")
|
|
222
|
+
throw new RangeError(`ddf must be "Satterthwaite" or "lme4", not ${JSON.stringify(ddf)}`);
|
|
223
|
+
const internals = lmerInternals(fit);
|
|
224
|
+
const names = [...internals.structure.xColNames];
|
|
225
|
+
if (ddf === "lme4") {
|
|
226
|
+
const { estimate, stdError, tValue } = fit.coefTable;
|
|
227
|
+
return { ddf, names, estimate: [...estimate], stdError: [...stdError], tValue: [...tValue], messages: [], warnings: [] };
|
|
228
|
+
}
|
|
229
|
+
const s = lmerSatterthwaite(fit);
|
|
230
|
+
const p = names.length;
|
|
231
|
+
const rows = names.map((_, i) => tTestOf(s, Array.from({ length: p }, (_, j) => (j === i ? 1 : 0)), 0));
|
|
232
|
+
return {
|
|
233
|
+
ddf,
|
|
234
|
+
names,
|
|
235
|
+
estimate: rows.map((r) => r.estimate),
|
|
236
|
+
stdError: rows.map((r) => r.stdError),
|
|
237
|
+
df: rows.map((r) => r.df),
|
|
238
|
+
tValue: rows.map((r) => r.tValue),
|
|
239
|
+
pValue: rows.map((r) => r.pValue),
|
|
240
|
+
messages: [],
|
|
241
|
+
warnings: [...s.warnings],
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
/** The rows of a contrast argument: a vector is one row; each row must have one value per fixed effect */
|
|
245
|
+
function contrastRows(L, p) {
|
|
246
|
+
if (!Array.isArray(L))
|
|
247
|
+
throw new TypeError("L must be an array of numbers or an array of rows");
|
|
248
|
+
const rows = L.length > 0 && Array.isArray(L[0]) ? L.map((r) => [...r]) : [[...L]];
|
|
249
|
+
for (const row of rows) {
|
|
250
|
+
if (!Array.isArray(row) || row.some((v) => typeof v !== "number"))
|
|
251
|
+
throw new TypeError("L must be an array of numbers or an array of rows");
|
|
252
|
+
if (row.length !== p)
|
|
253
|
+
throw new TypeError(`each contrast must have ${p} values (one per fixed effect), not ${row.length}`);
|
|
254
|
+
}
|
|
255
|
+
return rows;
|
|
256
|
+
}
|
|
257
|
+
export function lmerContest(fit, L, options = {}) {
|
|
258
|
+
const internals = lmerInternals(fit);
|
|
259
|
+
const p = internals.structure.xColNames.length;
|
|
260
|
+
const rows = contrastRows(L, p);
|
|
261
|
+
const joint = options.joint ?? true;
|
|
262
|
+
const confint = options.confint ?? true;
|
|
263
|
+
const level = options.level ?? 0.95;
|
|
264
|
+
if (typeof level !== "number" || !(level >= 0 && level <= 1))
|
|
265
|
+
throw new RangeError("level must be a number between 0 and 1");
|
|
266
|
+
const rhsOption = options.rhs ?? 0;
|
|
267
|
+
const rhs = typeof rhsOption === "number" ? new Array(rows.length).fill(rhsOption) : [...rhsOption];
|
|
268
|
+
if (rhs.length !== rows.length || rhs.some((v) => typeof v !== "number")) {
|
|
269
|
+
throw new TypeError(`rhs must be a number or have one value per contrast (${rows.length})`);
|
|
270
|
+
}
|
|
271
|
+
const warnings = new Warnings();
|
|
272
|
+
const s = lmerSatterthwaite(fit);
|
|
273
|
+
s.warnings.forEach((w) => warnings.add(w));
|
|
274
|
+
if (joint) {
|
|
275
|
+
const row = p === 0 ? undefined : lmerFTest(fit, rows, rhs, warnings);
|
|
276
|
+
const pick = (k) => (row === undefined ? [] : [row[k]]);
|
|
277
|
+
return {
|
|
278
|
+
joint: true,
|
|
279
|
+
sumSq: pick("sumSq"),
|
|
280
|
+
meanSq: pick("meanSq"),
|
|
281
|
+
numDF: pick("numDF"),
|
|
282
|
+
denDF: pick("denDF"),
|
|
283
|
+
F: pick("F"),
|
|
284
|
+
pValue: pick("pValue"),
|
|
285
|
+
messages: [],
|
|
286
|
+
warnings: warnings.list,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
const tests = p === 0 ? [] : rows.map((row, i) => tTestOf(s, row, rhs[i]));
|
|
290
|
+
const out = {
|
|
291
|
+
joint: false,
|
|
292
|
+
estimate: tests.map((t) => t.estimate),
|
|
293
|
+
stdError: tests.map((t) => t.stdError),
|
|
294
|
+
df: tests.map((t) => t.df),
|
|
295
|
+
tValue: tests.map((t) => t.tValue),
|
|
296
|
+
pValue: tests.map((t) => t.pValue),
|
|
297
|
+
messages: [],
|
|
298
|
+
warnings: warnings.list,
|
|
299
|
+
};
|
|
300
|
+
if (confint) {
|
|
301
|
+
const quantiles = tests.map((t) => qt1((1 - level) / 2, t.df, false, false));
|
|
302
|
+
out.lower = tests.map((t, i) => t.estimate - t.stdError * quantiles[i]);
|
|
303
|
+
out.upper = tests.map((t, i) => t.estimate + t.stdError * quantiles[i]);
|
|
304
|
+
}
|
|
305
|
+
return out;
|
|
306
|
+
}
|
package/dist/models/mauchly.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// (Householder QR in double-double); the residual cross-products, U and its determinant are computed in double-double
|
|
8
8
|
// (src/models/ddMatrix.ts).
|
|
9
9
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
10
|
+
import { levelText } from "../internal/levels.js";
|
|
10
11
|
import { qr } from "../linalg/qr.js";
|
|
11
12
|
import { ddAdd, ddDiv, ddDivNum, ddMul, ddSub } from "../special/dd.js";
|
|
12
13
|
import { ddLeastSquaresMulti } from "./ddLeastSquares.js";
|
|
@@ -89,7 +90,7 @@ export function mauchlyTest(y, options = {}) {
|
|
|
89
90
|
const group = options.group;
|
|
90
91
|
if (group !== undefined && group.length !== nsub)
|
|
91
92
|
throw new RangeError("'group' must have one label per row of y");
|
|
92
|
-
const labels = group?.map(
|
|
93
|
+
const labels = group?.map(levelText) ?? rows.map(() => "");
|
|
93
94
|
const levels = [...new Set(labels)].sort(compareCodePoints);
|
|
94
95
|
// residuals from the group means (double-double), and their cross-products
|
|
95
96
|
const res = rows.map((r) => r.map(() => [0, 0]));
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { MixedModelStructure } from "./mixedStructure.js";
|
|
2
|
+
/** The derivatives of {@link varparDerivatives} */
|
|
3
|
+
export interface VarparDerivatives {
|
|
4
|
+
/** varpar = (θ, σ), σ = sqrt(r² / ν) */
|
|
5
|
+
varpar: number[];
|
|
6
|
+
/** The deviance d(θ, σ) at varpar */
|
|
7
|
+
deviance: number;
|
|
8
|
+
/** Its gradient in varpar (0 at the optimum) */
|
|
9
|
+
gradient: number[];
|
|
10
|
+
/** The derivatives of Φ (the log determinants) and of r² in θ, and r² */
|
|
11
|
+
phiGradient: number[];
|
|
12
|
+
prssGradient: number[];
|
|
13
|
+
prss: number;
|
|
14
|
+
/** Its Hessian in varpar (m × m, array of rows) */
|
|
15
|
+
hessian: number[][];
|
|
16
|
+
/** β̂ at θ */
|
|
17
|
+
beta: number[];
|
|
18
|
+
/** C = σ² S⁻¹ at varpar (p × p, array of rows) */
|
|
19
|
+
vcov: number[][];
|
|
20
|
+
/** J_k = ∂C/∂varpar_k, one p × p matrix (array of rows) per element of varpar */
|
|
21
|
+
jacobian: number[][][];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The deviance d(θ, σ) in the variance parameters, its gradient and Hessian, and the covariance of β̂ with its
|
|
25
|
+
* Jacobians, at θ with σ = sqrt(r²(θ) / ν) (see the header). Internal.
|
|
26
|
+
*/
|
|
27
|
+
export declare function varparDerivatives(structure: MixedModelStructure, theta: readonly number[], reml: boolean): VarparDerivatives;
|