@numero-uno-jpn/rstats 0.16.0 → 1.0.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 +205 -0
- package/README.ja.md +57 -23
- package/README.md +60 -23
- package/dist/chunks/{chunk-5UQCRBAG.js → chunk-3W5NFFAA.js} +1 -1
- package/dist/chunks/chunk-4F2IALD4.js +3 -0
- package/dist/chunks/{chunk-QNNL7AZ3.js → chunk-4OLDGML4.js} +2 -2
- package/dist/chunks/chunk-63OPBEYO.js +2 -0
- package/dist/chunks/chunk-6XJBCRBV.js +2 -0
- package/dist/chunks/chunk-BQSIBJVX.js +2 -0
- package/dist/chunks/chunk-BWKHYRM6.js +3 -0
- package/dist/chunks/{chunk-W26LRYOJ.js → chunk-CKV4JWW2.js} +1 -1
- package/dist/chunks/chunk-DCAWMF4K.js +2 -0
- package/dist/chunks/chunk-F7YGX7PN.js +2 -0
- package/dist/chunks/chunk-H7I32ZZE.js +2 -0
- package/dist/chunks/chunk-IEOD5GWC.js +3 -0
- package/dist/chunks/chunk-KICBQGFN.js +2 -0
- package/dist/chunks/chunk-LROMWFAK.js +2 -0
- package/dist/chunks/chunk-NSWENS3S.js +2 -0
- package/dist/chunks/chunk-O2SQMGE3.js +2 -0
- package/dist/chunks/chunk-QQX46TCI.js +2 -0
- package/dist/chunks/chunk-QZK3CCBA.js +2 -0
- package/dist/chunks/chunk-UJ66VQIG.js +2 -0
- package/dist/chunks/chunk-UKNDX4BA.js +2 -0
- package/dist/chunks/chunk-WIBKS73G.js +2 -0
- package/dist/chunks/{chunk-URIO5LY5.js → chunk-WQWZ7FND.js} +2 -2
- package/dist/chunks/chunk-WWVMVZUL.js +2 -0
- package/dist/chunks/chunk-YG5S5AVP.js +2 -0
- package/dist/chunks/chunk-YHNMBYSW.js +2 -0
- package/dist/data/grouping.js +6 -2
- package/dist/descriptive/basic.js +20 -0
- package/dist/descriptive/cov.js +11 -0
- package/dist/descriptive/dataFrames.js +13 -5
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +10 -2
- package/dist/descriptive/hist.js +6 -0
- package/dist/descriptive/moments.js +14 -0
- package/dist/descriptive/omega.js +8 -3
- package/dist/descriptive/pivot.d.ts +4 -1
- package/dist/descriptive/pivot.js +27 -13
- package/dist/descriptive/quantile.js +8 -0
- package/dist/descriptive/rank.js +4 -0
- package/dist/descriptive/table.d.ts +20 -1
- package/dist/descriptive/table.js +14 -14
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +8 -0
- package/dist/distributions/beta.js +9 -0
- package/dist/distributions/binomial.js +9 -0
- package/dist/distributions/cauchy.js +9 -0
- package/dist/distributions/chisq.js +9 -0
- package/dist/distributions/exponential.js +9 -0
- package/dist/distributions/f.js +9 -0
- package/dist/distributions/gamma.js +8 -0
- package/dist/distributions/geometric.js +9 -0
- package/dist/distributions/hypergeometric.js +8 -0
- package/dist/distributions/invgamma.js +8 -0
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +9 -0
- package/dist/distributions/logistic.js +9 -0
- package/dist/distributions/lognormal.js +8 -0
- package/dist/distributions/multinomial.js +4 -0
- package/dist/distributions/negbinomial.js +8 -0
- package/dist/distributions/normal.js +8 -0
- package/dist/distributions/pareto.js +8 -0
- package/dist/distributions/poisson.js +9 -0
- package/dist/distributions/signrank.js +9 -0
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +31 -1
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +9 -0
- package/dist/distributions/tukey.js +6 -0
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +8 -0
- package/dist/distributions/wilcox.js +9 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +9 -0
- package/dist/entries/mixed.js +8 -1
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +13 -0
- package/dist/htest/agreement.js +46 -7
- package/dist/htest/ansariTest.d.ts +2 -1
- package/dist/htest/ansariTest.js +43 -12
- package/dist/htest/aov.d.ts +28 -22
- package/dist/htest/aov.js +42 -13
- package/dist/htest/binomTest.js +6 -0
- package/dist/htest/brunnerMunzelTest.js +5 -0
- package/dist/htest/chisqTest.d.ts +3 -1
- package/dist/htest/chisqTest.js +21 -3
- package/dist/htest/cor.d.ts +7 -1
- package/dist/htest/cor.js +40 -7
- package/dist/htest/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/effectSize.d.ts +3 -0
- package/dist/htest/effectSize.js +28 -8
- package/dist/htest/fisherTest.d.ts +2 -1
- package/dist/htest/fisherTest.js +52 -6
- package/dist/htest/ksTest.d.ts +4 -0
- package/dist/htest/ksTest.js +13 -4
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +9 -3
- package/dist/htest/mantelhaenTest.js +7 -0
- package/dist/htest/mcnemarTest.js +8 -0
- package/dist/htest/multipleComparisons.d.ts +16 -25
- package/dist/htest/multipleComparisons.js +26 -43
- package/dist/htest/normalityTests.js +10 -4
- package/dist/htest/onewayTest.js +10 -0
- package/dist/htest/pAdjust.js +4 -0
- package/dist/htest/pairwise.d.ts +2 -0
- package/dist/htest/pairwise.js +29 -6
- package/dist/htest/poissonTest.js +6 -0
- package/dist/htest/polycor.js +20 -10
- package/dist/htest/power.d.ts +4 -0
- package/dist/htest/power.js +16 -2
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +23 -2
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +38 -15
- package/dist/htest/rankTests.js +7 -1
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +9 -0
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +21 -1
- package/dist/htest/trendTests.js +13 -18
- package/dist/htest/types.d.ts +2 -0
- package/dist/htest/varTest.js +7 -0
- package/dist/htest/wilcoxTest.js +51 -9
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- package/dist/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.js +7 -0
- package/dist/linalg/chol.js +6 -0
- package/dist/linalg/eigen.js +4 -0
- package/dist/linalg/expm.js +4 -0
- package/dist/linalg/kappa.js +6 -0
- package/dist/linalg/lu.js +5 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +14 -13
- package/dist/linalg/qr.js +14 -0
- package/dist/linalg/solve.js +9 -0
- package/dist/linalg/svd.js +6 -2
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +5 -4
- package/dist/models/anovaSequential.d.ts +16 -1
- package/dist/models/anovaSequential.js +50 -7
- package/dist/models/aovEz.js +16 -11
- package/dist/models/aovStrata.js +15 -4
- package/dist/models/carAnova.js +11 -17
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +21 -6
- package/dist/models/formula.d.ts +5 -2
- package/dist/models/formula.js +31 -8
- package/dist/models/glm.d.ts +22 -2
- package/dist/models/glm.js +32 -22
- package/dist/models/glmAnova.js +8 -3
- package/dist/models/glmConfint.js +6 -2
- package/dist/models/glmFamily.d.ts +1 -6
- package/dist/models/glmFamily.js +3 -22
- package/dist/models/glmer.d.ts +255 -0
- package/dist/models/glmer.js +635 -0
- package/dist/models/glmerAnova.d.ts +81 -0
- package/dist/models/glmerAnova.js +139 -0
- package/dist/models/glmerDeviance.d.ts +98 -0
- package/dist/models/glmerDeviance.js +576 -0
- package/dist/models/glmerFamily.d.ts +57 -0
- package/dist/models/glmerFamily.js +236 -0
- package/dist/models/glmerGradient.d.ts +46 -0
- package/dist/models/glmerGradient.js +301 -0
- package/dist/models/glmerStructure.d.ts +45 -0
- package/dist/models/glmerStructure.js +168 -0
- package/dist/models/glmerSummary.d.ts +102 -0
- package/dist/models/glmerSummary.js +94 -0
- package/dist/models/lm.js +13 -3
- package/dist/models/lmer.d.ts +27 -6
- package/dist/models/lmer.js +75 -321
- package/dist/models/lmerAnova.d.ts +52 -3
- package/dist/models/lmerAnova.js +101 -45
- package/dist/models/lmerTest.d.ts +12 -5
- package/dist/models/lmerTest.js +39 -18
- package/dist/models/mauchly.d.ts +2 -0
- package/dist/models/mauchly.js +7 -2
- package/dist/models/mixedDeviance.d.ts +28 -2
- package/dist/models/mixedDeviance.js +84 -56
- package/dist/models/mixedMinimiser.d.ts +83 -0
- package/dist/models/mixedMinimiser.js +438 -0
- package/dist/models/mixedPredict.d.ts +121 -0
- package/dist/models/mixedPredict.js +530 -0
- package/dist/models/mixedStructure.d.ts +62 -3
- package/dist/models/mixedStructure.js +74 -29
- package/dist/models/modelFrame.js +2 -2
- package/dist/models/modelMatrix.d.ts +8 -1
- package/dist/models/modelMatrix.js +56 -39
- package/dist/models/survival.d.ts +6 -0
- package/dist/models/survival.js +9 -2
- package/dist/numerics/deriv.js +8 -0
- package/dist/numerics/integrate.js +8 -0
- package/dist/numerics/interp.d.ts +32 -7
- package/dist/numerics/interp.js +27 -9
- package/dist/numerics/ode.js +4 -0
- package/dist/numerics.js +2 -2
- package/dist/optim/optim.js +17 -2
- package/dist/random/beta.js +5 -0
- package/dist/random/continuous.js +17 -0
- package/dist/random/extra.js +13 -0
- package/dist/random/gamma.js +11 -0
- package/dist/random/poisson.js +5 -0
- package/dist/random/rng.js +4 -0
- package/dist/random/sample.js +6 -0
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.js +7 -0
- package/dist/special/choose.js +7 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +14 -11
- package/llms.txt +15 -4
- package/package.json +19 -10
- package/dist/chunks/chunk-2D2IQWUQ.js +0 -2
- package/dist/chunks/chunk-6CSTWA5E.js +0 -2
- package/dist/chunks/chunk-72Y3HZID.js +0 -2
- package/dist/chunks/chunk-BG2KLEHM.js +0 -2
- package/dist/chunks/chunk-CMEKBNHQ.js +0 -2
- package/dist/chunks/chunk-FLJNSXSW.js +0 -2
- package/dist/chunks/chunk-GM53LWSR.js +0 -2
- package/dist/chunks/chunk-JBCA56ZS.js +0 -3
- package/dist/chunks/chunk-K3RJAD2T.js +0 -2
- package/dist/chunks/chunk-KAZRWIB5.js +0 -2
- package/dist/chunks/chunk-KBVVIMXX.js +0 -3
- package/dist/chunks/chunk-N3SIFCZX.js +0 -2
- package/dist/chunks/chunk-SC6CV4Z3.js +0 -2
- package/dist/chunks/chunk-SYWOBOTR.js +0 -2
- package/dist/chunks/chunk-VTUGG2SV.js +0 -2
- package/dist/chunks/chunk-XHETEYVC.js +0 -2
- package/dist/chunks/chunk-YRZWONZB.js +0 -3
- package/dist/chunks/chunk-ZGABZMWX.js +0 -2
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { GlmResult } from "./glm.js";
|
|
2
|
+
import { type GlmerResult } from "./glmer.js";
|
|
3
|
+
import { type LmerResult } from "./lmer.js";
|
|
4
|
+
import { type LmerCompareOptions, type LmerCompareTable } from "./lmerAnova.js";
|
|
5
|
+
/**
|
|
6
|
+
* Options of {@link glmerAnova} for one fit: there is none (R's `anova()` of one fit has none); any key is a
|
|
7
|
+
* RangeError. For several fits the options are {@link LmerCompareOptions}.
|
|
8
|
+
*
|
|
9
|
+
* @category Mixed models
|
|
10
|
+
*/
|
|
11
|
+
export interface GlmerAnovaOptions {
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Result of {@link glmerAnova} for one fit: lme4's table of the fixed-effects terms (sequential Wald statistics), one
|
|
15
|
+
* row per term, without the intercept.
|
|
16
|
+
*
|
|
17
|
+
* @category Mixed models
|
|
18
|
+
*/
|
|
19
|
+
export interface GlmerAnovaTable {
|
|
20
|
+
/** The terms (R's row names), in the order of the columns of the fixed-effects matrix, without `(Intercept)`. */
|
|
21
|
+
terms: string[];
|
|
22
|
+
/** R's `npar`: the term's number of columns (aliased columns dropped). */
|
|
23
|
+
npar: number[];
|
|
24
|
+
/** R's `Sum Sq`: the squared length of the term's part of R_X β̂ (R_X the Cholesky factor of R_X'R_X). */
|
|
25
|
+
sumSq: number[];
|
|
26
|
+
/** R's `Mean Sq`: Sum Sq / npar. */
|
|
27
|
+
meanSq: number[];
|
|
28
|
+
/** R's `F value`: Mean Sq / sigma² (Mean Sq for binomial and poisson). */
|
|
29
|
+
F: number[];
|
|
30
|
+
/** R's messages (none). */
|
|
31
|
+
messages: string[];
|
|
32
|
+
/** R's warnings (none). */
|
|
33
|
+
warnings: string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The ANOVA table of the fixed-effects terms of a generalized linear mixed model fitted by {@link glmer}, or, given an
|
|
37
|
+
* array of fits, the likelihood-ratio comparison of the models. Equivalent to `lme4::anova()` in R.
|
|
38
|
+
*
|
|
39
|
+
* For one fit: for each term of the fixed effects (in the order of the columns, the intercept left out), the
|
|
40
|
+
* sequential Wald sum of squares, the squared length of the term's part of R_X β̂ (R_X the Cholesky factor of
|
|
41
|
+
* R_X'R_X at the conditional mode), its number of columns (`npar`), the mean square and F = mean square / σ² (σ = 1
|
|
42
|
+
* for binomial and poisson). As in lme4, there are no p-values (the statistics need not be F distributed).
|
|
43
|
+
*
|
|
44
|
+
* For several fits: fits of {@link glmer}, {@link lmer} and {@link glm} together (at least one of glmer or lmer). The
|
|
45
|
+
* glmer and lmer fits must be fitted to the same data object with the same rows and the same response (the left-hand
|
|
46
|
+
* side of the formula); a glm fit must have the same number of observations and the same response values; all must
|
|
47
|
+
* have the same prior weights. lmer's REML fits are refitted by maximum likelihood (unless `refit: false`); the models
|
|
48
|
+
* are sorted by their number of parameters, and each is compared with the one before by the likelihood-ratio test
|
|
49
|
+
* where that one is nested in it: the same family, link and offset (an lmer fit is gaussian with the identity link),
|
|
50
|
+
* its fixed-effects columns in the column space of the larger fit's and its random-effects terms within the larger
|
|
51
|
+
* fit's. Unlike R's, the log-likelihood of a glmer fit with `nAGQ > 1` includes the saturated model's constant (see
|
|
52
|
+
* {@link glmer}), so that it can be compared with the Laplace fit and with glm.
|
|
53
|
+
*
|
|
54
|
+
* @param fit A result of {@link glmer} (the object it returned, not a copy), or an array of at least two fits of
|
|
55
|
+
* glmer, lmer and glm.
|
|
56
|
+
* @param options For one fit none; for several `refit` and `names` (see {@link LmerCompareOptions}).
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* // R: anova(lme4::glmer(y ~ x + f + (1 | g), data, family = binomial))
|
|
61
|
+
* const data = {
|
|
62
|
+
* y: [0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1],
|
|
63
|
+
* x: [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
|
|
64
|
+
* f: ["u", "v", "u", "v", "u", "v", "v", "u", "v", "u", "v", "u", "u", "v", "u", "v", "u", "v", "v", "u", "v", "u", "v", "u"],
|
|
65
|
+
* g: ["a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d", "d"],
|
|
66
|
+
* };
|
|
67
|
+
* const fit = glmer("y ~ x + f + (1 | g)", data, { family: "binomial" });
|
|
68
|
+
* const t = glmerAnova(fit);
|
|
69
|
+
* t.terms; // ["x", "f"]
|
|
70
|
+
* t.npar; // [1, 1]
|
|
71
|
+
* t.F; // [3.6704, 0.83614]
|
|
72
|
+
* // R: anova(lme4::glmer(y ~ x + (1 | g), data, family = binomial), fit)
|
|
73
|
+
* const c = glmerAnova([glmer("y ~ x + (1 | g)", data, { family: "binomial" }), fit]);
|
|
74
|
+
* c.npar; // [3, 4]
|
|
75
|
+
* c.Chisq; // [NaN, 0.8673]
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @category Mixed models
|
|
79
|
+
*/
|
|
80
|
+
export declare function glmerAnova(fit: GlmerResult, options?: GlmerAnovaOptions): GlmerAnovaTable;
|
|
81
|
+
export declare function glmerAnova(fits: readonly (GlmerResult | LmerResult | GlmResult)[], options?: LmerCompareOptions): LmerCompareTable;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// ANOVA of generalized linear mixed models fitted by glmer (src/models/glmer.ts): lme4's table of the fixed-effects
|
|
2
|
+
// terms of one fit (sequential Wald sums of squares from R_X), and lme4's likelihood-ratio comparison of several fits
|
|
3
|
+
// of glmer, lmer and glm.
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js (no ported code) from oracle/glmer-behaviour.md ("glmerAnova (stage 3)" and "rStats.js's
|
|
6
|
+
// choices", G15). The comparison is lmerAnova's (src/models/lmerAnova.ts, compareModels), with the prior weights,
|
|
7
|
+
// the family, the link and the offset of each fit.
|
|
8
|
+
import { potf2 } from "../linalg/chol.js";
|
|
9
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
10
|
+
import { reported } from "../internal/warnings.js";
|
|
11
|
+
import { glmerInternals, isGlmerFit } from "./glmer.js";
|
|
12
|
+
import { checkLmerOptions, logicalOption } from "./lmer.js";
|
|
13
|
+
import { comparedLmer, compareModels } from "./lmerAnova.js";
|
|
14
|
+
/** The table of the fixed-effects terms of one fit */
|
|
15
|
+
function anovaOfFit(fit) {
|
|
16
|
+
const { structure: s, rxtrx, beta } = glmerInternals(fit);
|
|
17
|
+
const p = beta.length;
|
|
18
|
+
// R_X, the upper triangular Cholesky factor of R_X'R_X (column-major), and c = R_X β̂
|
|
19
|
+
const data = new Float64Array(p * p);
|
|
20
|
+
for (let j = 0; j < p; j++)
|
|
21
|
+
for (let i = 0; i < p; i++)
|
|
22
|
+
data[i + j * p] = rxtrx[i][j];
|
|
23
|
+
const failed = p > 0 && (data.some((v) => !Number.isFinite(v)) || potf2({ nrow: p, ncol: p, data }) > 0);
|
|
24
|
+
const c = Array.from({ length: p }, (_, i) => {
|
|
25
|
+
if (failed)
|
|
26
|
+
return NaN;
|
|
27
|
+
let sum = 0;
|
|
28
|
+
for (let j = i; j < p; j++)
|
|
29
|
+
sum += data[i + j * p] * beta[j];
|
|
30
|
+
return sum;
|
|
31
|
+
});
|
|
32
|
+
// one row per term with columns, in the order of the columns; the intercept (term 0) left out
|
|
33
|
+
const terms = [];
|
|
34
|
+
const npar = [];
|
|
35
|
+
const sumSq = [];
|
|
36
|
+
s.xAssign.forEach((term, j) => {
|
|
37
|
+
if (term === 0)
|
|
38
|
+
return;
|
|
39
|
+
const label = s.xTermLabels[term - 1];
|
|
40
|
+
if (terms[terms.length - 1] !== label) {
|
|
41
|
+
terms.push(label);
|
|
42
|
+
npar.push(0);
|
|
43
|
+
sumSq.push(0);
|
|
44
|
+
}
|
|
45
|
+
npar[npar.length - 1]++;
|
|
46
|
+
sumSq[sumSq.length - 1] += c[j] ** 2;
|
|
47
|
+
});
|
|
48
|
+
const meanSq = sumSq.map((v, k) => v / npar[k]);
|
|
49
|
+
// (σ is 1 for binomial and poisson, so that F = Mean Sq there, as R's)
|
|
50
|
+
const sigma2 = fit.sigma * fit.sigma;
|
|
51
|
+
return { terms, npar, sumSq, meanSq, F: meanSq.map((v) => v / sigma2), messages: [], warnings: [] };
|
|
52
|
+
}
|
|
53
|
+
/** Whether an object looks like a result of glm (src/models/glm.ts; glm keeps no record of its results) */
|
|
54
|
+
function isGlmFit(fit) {
|
|
55
|
+
if (typeof fit !== "object" || fit === null)
|
|
56
|
+
return false;
|
|
57
|
+
const f = fit;
|
|
58
|
+
return (typeof f.aic === "number" &&
|
|
59
|
+
typeof f.rank === "number" &&
|
|
60
|
+
typeof f.family === "string" &&
|
|
61
|
+
typeof f.link === "string" &&
|
|
62
|
+
Array.isArray(f.y) &&
|
|
63
|
+
Array.isArray(f.priorWeights) &&
|
|
64
|
+
typeof f.model === "object" &&
|
|
65
|
+
f.model !== null &&
|
|
66
|
+
Array.isArray(f.model.x) &&
|
|
67
|
+
Array.isArray(f.model.offset));
|
|
68
|
+
}
|
|
69
|
+
/** The model of a glmer fit in a comparison */
|
|
70
|
+
function comparedGlmer(fit) {
|
|
71
|
+
const { structure: s, data, snapshot } = glmerInternals(fit);
|
|
72
|
+
return {
|
|
73
|
+
nObs: fit.nObs,
|
|
74
|
+
df: fit.df,
|
|
75
|
+
// (G12: for nAGQ > 1 the log-likelihood with the saturated model's constant, so comparisons across nAGQ and with
|
|
76
|
+
// glm are commensurate)
|
|
77
|
+
logLik: fit.logLik,
|
|
78
|
+
reml: false,
|
|
79
|
+
mixed: { data, snapshot, rows: fit.rows, response: fit.formula.slice(0, fit.formula.indexOf("~")).replace(/\s+/g, "") },
|
|
80
|
+
response: () => s.y,
|
|
81
|
+
weights: () => s.weights,
|
|
82
|
+
family: fit.family,
|
|
83
|
+
link: fit.link,
|
|
84
|
+
offset: () => s.offset,
|
|
85
|
+
x: () => s.x,
|
|
86
|
+
randomTerms: fit.varCorr,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/** The model of a glm fit in a comparison: R's logLik.glm (df the rank, plus 1 for a family with a dispersion) */
|
|
90
|
+
function comparedGlm(fit) {
|
|
91
|
+
const dispersion = fit.family === "gaussian" || fit.family === "Gamma" || fit.family === "inverse.gaussian";
|
|
92
|
+
const df = fit.rank + (dispersion ? 1 : 0);
|
|
93
|
+
return {
|
|
94
|
+
nObs: fit.y.length,
|
|
95
|
+
df,
|
|
96
|
+
logLik: df - fit.aic / 2,
|
|
97
|
+
reml: false,
|
|
98
|
+
response: () => fit.y,
|
|
99
|
+
weights: () => fit.priorWeights,
|
|
100
|
+
family: fit.family,
|
|
101
|
+
link: fit.link,
|
|
102
|
+
offset: () => fit.model.offset,
|
|
103
|
+
x: () => fit.model.x,
|
|
104
|
+
randomTerms: [],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export function glmerAnova(fit, options) {
|
|
108
|
+
if (Array.isArray(fit)) {
|
|
109
|
+
checkLmerOptions("glmerAnova", options, ["modelNames", "model.names"]);
|
|
110
|
+
checkOptions(options, ["refit", "names"], "glmerAnova");
|
|
111
|
+
const checked = (options ?? {});
|
|
112
|
+
// (the logical value is checked with the options, before the fits)
|
|
113
|
+
const refit = logicalOption(checked.refit, true);
|
|
114
|
+
const fits = fit;
|
|
115
|
+
if (fits.length < 2)
|
|
116
|
+
throw new TypeError("glmerAnova needs one fit, or an array of at least two fits to compare");
|
|
117
|
+
const models = fits.map((f) => {
|
|
118
|
+
if (isGlmerFit(f))
|
|
119
|
+
return comparedGlmer(f);
|
|
120
|
+
if (isGlmFit(f))
|
|
121
|
+
return comparedGlm(f);
|
|
122
|
+
try {
|
|
123
|
+
return comparedLmer(f);
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
if (e instanceof TypeError)
|
|
127
|
+
throw new TypeError("each fit must be a result returned by glmer, lmer or glm (not a copy of one)");
|
|
128
|
+
throw e;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
// (glm fits alone are glmAnova's)
|
|
132
|
+
if (fits.every(isGlmFit))
|
|
133
|
+
throw new TypeError("glmerAnova needs at least one fit of glmer or lmer (glmAnova compares glm fits)");
|
|
134
|
+
return reported("glmerAnova", compareModels("glmerAnova", models, { ...checked, refit }), ...fits);
|
|
135
|
+
}
|
|
136
|
+
checkLmerOptions("glmerAnova", options, []);
|
|
137
|
+
checkOptions(options, [], "glmerAnova");
|
|
138
|
+
return reported("glmerAnova", anovaOfFit(fit), fit);
|
|
139
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { GlmerStructure } from "./glmerStructure.js";
|
|
2
|
+
/** The Laplace deviance and the conditional mode at given parameters. */
|
|
3
|
+
export interface GlmerDeviance {
|
|
4
|
+
/** The deviance ldL2 + ussq + aic (−2 times the Laplace approximation of the log-likelihood). */
|
|
5
|
+
deviance: number;
|
|
6
|
+
/** The fixed effects: the given ones (nAGQ = 1), those of the joint mode (nAGQ = 0). */
|
|
7
|
+
beta: number[];
|
|
8
|
+
/** The conditional mode ũ of the spherical random effects. */
|
|
9
|
+
u: number[];
|
|
10
|
+
/** The random effects b = Λ_θ ũ. */
|
|
11
|
+
b: number[];
|
|
12
|
+
/** The conditional means μ (one per row used). */
|
|
13
|
+
mu: number[];
|
|
14
|
+
/** The linear predictor η = o + Xβ + ZΛ_θ ũ at the mode (one per row used). */
|
|
15
|
+
eta: number[];
|
|
16
|
+
/** The sum of the deviance residuals at μ. */
|
|
17
|
+
devResid: number;
|
|
18
|
+
/** ‖ũ‖². */
|
|
19
|
+
ussq: number;
|
|
20
|
+
/** The penalised deviance devResid + ussq. */
|
|
21
|
+
pdev: number;
|
|
22
|
+
/** log det(Λ_θ' Z'WZ Λ_θ + I), W the working weights at the mode. */
|
|
23
|
+
ldL2: number;
|
|
24
|
+
/** The family's aic term at μ (−2 log-likelihood; the dispersion from the deviance for Gamma, inverse.gaussian, gaussian). */
|
|
25
|
+
aic: number;
|
|
26
|
+
/** The number of PIRLS iterations. */
|
|
27
|
+
iterations: number;
|
|
28
|
+
/** R's warnings of the evaluation (poisson's non-integer counts). */
|
|
29
|
+
warnings: string[];
|
|
30
|
+
/**
|
|
31
|
+
* R_X'R_X = X'WX − X'WZΛ (Λ'Z'WZΛ + I)⁻¹ Λ'Z'WX with the working weights at the mode (array of rows), when asked
|
|
32
|
+
* for (`{ rx: true }`).
|
|
33
|
+
*/
|
|
34
|
+
rxtrx?: number[][];
|
|
35
|
+
/**
|
|
36
|
+
* The gradient of the deviance (src/models/glmerGradient.ts), when asked for (`{ gradient: true }`): with respect to
|
|
37
|
+
* (θ, β) for nAGQ = 1 (and nAGQ > 1), θ for nAGQ = 0.
|
|
38
|
+
*/
|
|
39
|
+
gradient?: number[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The Laplace deviance of a generalised linear mixed model at `theta` (and `beta` for nAGQ = 1), at the exact
|
|
43
|
+
* conditional mode (oracle/glmer-behaviour.md, stage 2a). Internal: not exported from the package.
|
|
44
|
+
*
|
|
45
|
+
* @param structure The structure from {@link glmerStructure}.
|
|
46
|
+
* @param theta The covariance parameters (finite, of the length of `structure.theta`; values below the lower bounds
|
|
47
|
+
* are accepted).
|
|
48
|
+
* @param beta The fixed effects (one per column of X) for nAGQ = 1; ignored for nAGQ = 0.
|
|
49
|
+
* @param nAGQ 1: the deviance is a function of (θ, β), the mode is found for u; 0: a function of θ, the mode is found
|
|
50
|
+
* for u and β jointly.
|
|
51
|
+
* @param options `rx`: also return R_X'R_X at the mode (for the covariance of the fixed effects); `gradient`: also
|
|
52
|
+
* return the gradient of the deviance.
|
|
53
|
+
*/
|
|
54
|
+
export declare function glmerDeviance(structure: GlmerStructure, theta: readonly number[], beta: readonly number[] | undefined, nAGQ?: 0 | 1, options?: {
|
|
55
|
+
rx?: boolean;
|
|
56
|
+
gradient?: boolean;
|
|
57
|
+
}): GlmerDeviance;
|
|
58
|
+
/** A Gauss-Hermite rule for the standard normal density: nodes in increasing order and their weights (sum 1). */
|
|
59
|
+
export interface GaussHermiteRule {
|
|
60
|
+
nodes: number[];
|
|
61
|
+
weights: number[];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The K-point Gauss-Hermite rule for the standard normal density (K = 1..100): the nodes are the zeros of the
|
|
65
|
+
* probabilists' Hermite polynomial He_K, the weights 1 / Σ_(j<K) He_j(z)² / j!. The positive zeros are bracketed by a
|
|
66
|
+
* scan of the sign of He_K (step 0.01, well below the smallest spacing of the zeros, about π / √(2K + 1) = 0.22 for
|
|
67
|
+
* K = 100), narrowed by bisection and finished by Newton's method (He_K' = K He_(K−1)); the negative nodes are their
|
|
68
|
+
* mirror images and the middle node of an odd rule is exactly 0, so the rule is symmetric as lme4's symmetrised table
|
|
69
|
+
* is. Cached per K.
|
|
70
|
+
*/
|
|
71
|
+
export declare function gaussHermiteRule(k: number): GaussHermiteRule;
|
|
72
|
+
/** The AGQ deviance and the conditional mode at given parameters. */
|
|
73
|
+
export interface GlmerAgqDeviance extends GlmerDeviance {
|
|
74
|
+
/**
|
|
75
|
+
* The AGQ deviance devResid + ussq + ldL2 + quad (no aic term: lme4's likelihood relative to the saturated model,
|
|
76
|
+
* with the dispersion held at 1).
|
|
77
|
+
*/
|
|
78
|
+
deviance: number;
|
|
79
|
+
/** −2 Σ_j log Σ_k w_k exp(z_k²/2 − Δ_jk/2), the quadrature's term. */
|
|
80
|
+
quad: number;
|
|
81
|
+
/** The conditional standard deviations σ_j = 1 / L_j of the levels. */
|
|
82
|
+
condSd: number[];
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The adaptive Gauss-Hermite quadrature deviance D_K(θ, β) of a model with one scalar random-effects term, at the
|
|
86
|
+
* exact conditional mode (that of nAGQ = 1, {@link glmerDeviance}) with the rule of {@link gaussHermiteRule}
|
|
87
|
+
* (oracle/glmer-behaviour.md, "The deviance at given (θ, β)"). Internal: not exported from the package.
|
|
88
|
+
*
|
|
89
|
+
* @param structure The structure (one random-effects term with one column).
|
|
90
|
+
* @param theta θ (one value).
|
|
91
|
+
* @param beta The fixed effects.
|
|
92
|
+
* @param nAGQ The number of points K (2 to 100).
|
|
93
|
+
* @param options `rx`: also return R_X'R_X at the mode; `gradient`: also the gradient in (θ, β).
|
|
94
|
+
*/
|
|
95
|
+
export declare function glmerAgqDeviance(structure: GlmerStructure, theta: readonly number[], beta: readonly number[], nAGQ: number, options?: {
|
|
96
|
+
rx?: boolean;
|
|
97
|
+
gradient?: boolean;
|
|
98
|
+
}): GlmerAgqDeviance;
|