@numero-uno-jpn/rstats 0.17.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 +77 -0
- package/README.ja.md +18 -10
- package/README.md +20 -10
- package/dist/chunks/{chunk-Z37NELOS.js → chunk-3W5NFFAA.js} +1 -1
- package/dist/chunks/{chunk-2C33MXSW.js → chunk-4F2IALD4.js} +2 -2
- package/dist/chunks/{chunk-H3WVPZDL.js → chunk-4OLDGML4.js} +2 -2
- package/dist/chunks/{chunk-WM7VFXTV.js → chunk-63OPBEYO.js} +1 -1
- package/dist/chunks/{chunk-TNMJPQPV.js → chunk-6XJBCRBV.js} +2 -2
- package/dist/chunks/{chunk-RFE7MVP7.js → chunk-BQSIBJVX.js} +2 -2
- package/dist/chunks/{chunk-C7V2FH6L.js → chunk-BWKHYRM6.js} +1 -1
- package/dist/chunks/{chunk-SE7V2KLO.js → chunk-CKV4JWW2.js} +1 -1
- package/dist/chunks/{chunk-CZI7KUO3.js → chunk-DCAWMF4K.js} +2 -2
- package/dist/chunks/{chunk-5AYHCITP.js → chunk-F7YGX7PN.js} +2 -2
- package/dist/chunks/chunk-H7I32ZZE.js +2 -0
- package/dist/chunks/chunk-IEOD5GWC.js +3 -0
- package/dist/chunks/{chunk-XLSFJTTK.js → chunk-KICBQGFN.js} +2 -2
- package/dist/chunks/chunk-LROMWFAK.js +2 -0
- package/dist/chunks/{chunk-DXI5N2ET.js → chunk-NSWENS3S.js} +1 -1
- package/dist/chunks/chunk-O2SQMGE3.js +2 -0
- package/dist/chunks/{chunk-FY3FVOBM.js → chunk-QQX46TCI.js} +2 -2
- package/dist/chunks/{chunk-ZFQWB222.js → chunk-QZK3CCBA.js} +2 -2
- package/dist/chunks/{chunk-6JJQL6HL.js → chunk-UJ66VQIG.js} +2 -2
- package/dist/chunks/{chunk-ONY5CISH.js → chunk-UKNDX4BA.js} +2 -2
- package/dist/chunks/chunk-WIBKS73G.js +2 -0
- package/dist/chunks/{chunk-4WE3OO62.js → chunk-WQWZ7FND.js} +2 -2
- package/dist/chunks/{chunk-LHUIJQHY.js → chunk-WWVMVZUL.js} +2 -2
- package/dist/chunks/{chunk-LV3W2F7N.js → chunk-YG5S5AVP.js} +2 -2
- package/dist/chunks/{chunk-34S5SQY6.js → chunk-YHNMBYSW.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/mixed.d.ts +6 -0
- package/dist/entries/mixed.js +6 -1
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +5 -4
- package/dist/models/formula.d.ts +5 -2
- package/dist/models/formula.js +31 -8
- 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/lmer.d.ts +16 -0
- package/dist/models/lmer.js +55 -306
- package/dist/models/lmerAnova.d.ts +49 -0
- package/dist/models/lmerAnova.js +91 -40
- package/dist/models/lmerTest.d.ts +8 -1
- package/dist/models/lmerTest.js +21 -8
- 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/modelMatrix.d.ts +8 -1
- package/dist/models/modelMatrix.js +49 -37
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +11 -11
- package/dist/special.js +2 -2
- package/dist/tests.js +13 -14
- package/llms.txt +10 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-M4ITG7YR.js +0 -2
- package/dist/chunks/chunk-NSYZTS26.js +0 -2
- package/dist/chunks/chunk-THIGHOKK.js +0 -2
- package/dist/chunks/chunk-WMDYDTVQ.js +0 -2
package/dist/models/formula.js
CHANGED
|
@@ -12,6 +12,10 @@
|
|
|
12
12
|
// "Formulas with random-effects terms") are parsed only when asked for: `|` and `||` bind more loosely than every
|
|
13
13
|
// other operator, `splitBarTerms` separates them from the fixed-effects part, and `deparseFormula` prints a node as
|
|
14
14
|
// R deparses it (for the terms' labels).
|
|
15
|
+
//
|
|
16
|
+
// The two-column binomial response of generalised linear mixed models, `cbind(successes, failures) ~ ...`, is parsed
|
|
17
|
+
// only when asked for, as the one function call allowed, on the left-hand side and with two column names (rStats.js's
|
|
18
|
+
// choice G1 in oracle/glmer-behaviour.md; R evaluates any expression there).
|
|
15
19
|
import { formatR } from "../htest/core/simulate.js";
|
|
16
20
|
const invalid = (why) => new SyntaxError(`invalid formula: ${why}`);
|
|
17
21
|
/**
|
|
@@ -21,7 +25,7 @@ const invalid = (why) => new SyntaxError(`invalid formula: ${why}`);
|
|
|
21
25
|
* digits, but no combining marks and nothing outside the BMP (R on Linux takes both, from glibc's tables).
|
|
22
26
|
*/
|
|
23
27
|
const NAME = /^(?:[\p{L}\p{Nl}.]|(?![0-9])\p{Nd})[\p{L}\p{Nl}\p{Nd}\p{M}._]*/u;
|
|
24
|
-
function tokenize(s, allowBars) {
|
|
28
|
+
function tokenize(s, allowBars, allowComma = false) {
|
|
25
29
|
const out = [];
|
|
26
30
|
let i = 0;
|
|
27
31
|
while (i < s.length) {
|
|
@@ -53,7 +57,7 @@ function tokenize(s, allowBars) {
|
|
|
53
57
|
i += text.length;
|
|
54
58
|
continue;
|
|
55
59
|
}
|
|
56
|
-
if ("~+-*/:^()".includes(c)) {
|
|
60
|
+
if ("~+-*/:^()".includes(c) || (allowComma && c === ",")) {
|
|
57
61
|
out.push({ kind: "op", text: c });
|
|
58
62
|
i++;
|
|
59
63
|
continue;
|
|
@@ -72,11 +76,12 @@ const UNARY = 5;
|
|
|
72
76
|
*
|
|
73
77
|
* @param allowError Accept `Error(...)` terms (aov only).
|
|
74
78
|
* @param allowBars Accept the random-effects terms `(lhs | group)` and `(lhs || group)` (mixed models only).
|
|
79
|
+
* @param allowCbind Accept the response `cbind(a, b)` with two column names (generalised linear mixed models only).
|
|
75
80
|
*/
|
|
76
|
-
export function parseFormula(formula, allowError = false, allowBars = false) {
|
|
81
|
+
export function parseFormula(formula, allowError = false, allowBars = false, allowCbind = false) {
|
|
77
82
|
if (typeof formula !== "string")
|
|
78
83
|
throw new TypeError("the formula must be a string");
|
|
79
|
-
const toks = tokenize(formula, allowBars);
|
|
84
|
+
const toks = tokenize(formula, allowBars, allowCbind);
|
|
80
85
|
let pos = 0;
|
|
81
86
|
const peek = () => toks[pos];
|
|
82
87
|
const next = () => toks[pos++];
|
|
@@ -129,7 +134,21 @@ export function parseFormula(formula, allowError = false, allowBars = false) {
|
|
|
129
134
|
return left;
|
|
130
135
|
}
|
|
131
136
|
let response;
|
|
132
|
-
|
|
137
|
+
let responseColumns;
|
|
138
|
+
const t0 = toks[0];
|
|
139
|
+
if (allowCbind && t0.kind === "name" && t0.text === "cbind" && isOp(toks[1], "(")) {
|
|
140
|
+
// cbind(a, b) ~: two column names (a comma is a token only here; anywhere else it is unexpected)
|
|
141
|
+
const a = toks[2];
|
|
142
|
+
const b = toks[4];
|
|
143
|
+
const isName = (t) => t.kind === "name" && t.text !== "." && !/^\.[0-9]/.test(t.text);
|
|
144
|
+
if (!(isName(a) && isOp(toks[3], ",") && isName(b) && isOp(toks[5], ")") && isOp(toks[6], "~"))) {
|
|
145
|
+
throw invalid("the response cbind() must have two column names: cbind(successes, failures)");
|
|
146
|
+
}
|
|
147
|
+
responseColumns = [a.text, b.text];
|
|
148
|
+
response = responseColumns[0];
|
|
149
|
+
pos = 6;
|
|
150
|
+
}
|
|
151
|
+
else if (!isOp(peek(), "~")) {
|
|
133
152
|
const t = next();
|
|
134
153
|
if (t.kind !== "name" || t.text === "." || !isOp(peek(), "~"))
|
|
135
154
|
throw invalid("the response must be a variable name followed by '~'");
|
|
@@ -139,7 +158,7 @@ export function parseFormula(formula, allowError = false, allowBars = false) {
|
|
|
139
158
|
const rhs = expr(0);
|
|
140
159
|
if (peek().kind !== "end")
|
|
141
160
|
throw invalid("unexpected input after the formula");
|
|
142
|
-
return { response, rhs };
|
|
161
|
+
return responseColumns === undefined ? { response, rhs } : { response, responseColumns, rhs };
|
|
143
162
|
}
|
|
144
163
|
/** The `Error()` terms of a right-hand side (summands, possibly in parentheses) and the right-hand side without them. */
|
|
145
164
|
export function splitErrorTerms(rhs) {
|
|
@@ -431,9 +450,13 @@ export function expandRhs(rhs, index, dot) {
|
|
|
431
450
|
* @param rhs The right-hand side to expand (default: the parsed formula's).
|
|
432
451
|
*/
|
|
433
452
|
export function termsOf(parsed, columns, rhs = parsed.rhs) {
|
|
434
|
-
const { response } = parsed;
|
|
435
|
-
|
|
453
|
+
const { response, responseColumns } = parsed;
|
|
454
|
+
// (`.` stands for the columns that are not in the response, both of cbind(a, b))
|
|
455
|
+
const dot = columns.filter((c) => c !== response && !(responseColumns?.includes(c) ?? false));
|
|
456
|
+
// the response first; the second column of cbind(a, b) next, so that the model frame drops its missing rows too
|
|
436
457
|
const variables = response === undefined ? [] : [response];
|
|
458
|
+
if (responseColumns !== undefined && responseColumns[1] !== response)
|
|
459
|
+
variables.push(responseColumns[1]);
|
|
437
460
|
collectVariables(parsed.rhs, dot, variables);
|
|
438
461
|
const index = (name) => variables.indexOf(name);
|
|
439
462
|
const expanded = expandRhs(rhs, index, dot);
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { type GlmLinkName } from "./glmFamily.js";
|
|
2
|
+
import { type GlmerFamilyName } from "./glmerFamily.js";
|
|
3
|
+
import { type GlmerStructure } from "./glmerStructure.js";
|
|
4
|
+
import { type LmerContrastName, type LmerRanef, type LmerVarCorr } from "./lmer.js";
|
|
5
|
+
import type { DataFrame } from "./modelFrame.js";
|
|
6
|
+
/**
|
|
7
|
+
* Options of {@link glmer}. Only the keys below are accepted: R's `subset`, `naAction`, `na.action`, `control`,
|
|
8
|
+
* `verbose`, `devFunOnly`, `mustart` and `etastart` give the Error `glmer: argument '<key>' is not supported`, any
|
|
9
|
+
* other key a RangeError (a key whose value is `undefined` included).
|
|
10
|
+
*
|
|
11
|
+
* @category Mixed models
|
|
12
|
+
*/
|
|
13
|
+
export interface GlmerOptions {
|
|
14
|
+
/**
|
|
15
|
+
* The family: `"binomial"`, `"poisson"`, `"Gamma"`, `"inverse.gaussian"` or `"gaussian"` (default, as R's; with the
|
|
16
|
+
* identity link it is a linear mixed model, a RangeError telling to use {@link lmer}). The quasi families are the
|
|
17
|
+
* error `"quasi" families cannot be used in glmer`.
|
|
18
|
+
*/
|
|
19
|
+
family?: GlmerFamilyName;
|
|
20
|
+
/**
|
|
21
|
+
* The link: any of `"logit"`, `"probit"`, `"cauchit"`, `"cloglog"`, `"identity"`, `"log"`, `"sqrt"`, `"1/mu^2"`,
|
|
22
|
+
* `"inverse"` (default: the family's, as in R: binomial logit, poisson log, Gamma inverse, inverse.gaussian
|
|
23
|
+
* 1/mu^2, gaussian identity).
|
|
24
|
+
*/
|
|
25
|
+
link?: GlmLinkName;
|
|
26
|
+
/**
|
|
27
|
+
* Prior weights (non-negative): one per row of `data` (`null` or NaN is missing and drops the row), or the name of
|
|
28
|
+
* a numeric column. For the binomial family with proportions as the response, the numbers of trials.
|
|
29
|
+
*/
|
|
30
|
+
weights?: readonly (number | null)[] | Float64Array | string;
|
|
31
|
+
/** Offset added to the linear predictor: one per row of `data` (`null` or NaN drops the row), or a column name. */
|
|
32
|
+
offset?: readonly (number | null)[] | Float64Array | string;
|
|
33
|
+
/**
|
|
34
|
+
* 1 (default): the Laplace approximation, the deviance minimised over θ and β; 0: over θ only, with β from the
|
|
35
|
+
* penalised IRLS (faster, less exact); 2 to 100: adaptive Gauss-Hermite quadrature with that many points, more
|
|
36
|
+
* exact than the Laplace approximation. A non-integer is truncated as in R (−0.5 is 0); negative values after that
|
|
37
|
+
* are a RangeError (R uses 0), above 100 R's error. Above 1 the model must have a single random-effects term with one column, such as
|
|
38
|
+
* `(1 | g)` (otherwise R's errors `nAGQ > 1 is only available for models with a single, scalar random-effects
|
|
39
|
+
* term` or `AGQ only defined for a single scalar random-effects term`), and the family must be binomial or
|
|
40
|
+
* poisson: for Gamma, inverse.gaussian and gaussian it is a RangeError, since lme4 holds the dispersion at 1 there
|
|
41
|
+
* and its estimates are not maximum likelihood (R fits them).
|
|
42
|
+
*/
|
|
43
|
+
nAGQ?: number;
|
|
44
|
+
/**
|
|
45
|
+
* Where the search begins (it does not change the estimate unless the deviance has several minima): `theta`, the
|
|
46
|
+
* covariance parameters (the length of θ, diagonal elements not negative), and `fixef`, the fixed effects (nAGQ = 1
|
|
47
|
+
* only), each with R's error messages.
|
|
48
|
+
*/
|
|
49
|
+
start?: {
|
|
50
|
+
/** Starting values of θ (R's `start = list(theta = ...)`). */
|
|
51
|
+
theta?: readonly number[];
|
|
52
|
+
/** Starting values of the fixed effects (R's `start = list(fixef = ...)`; not with nAGQ = 0, as in R). */
|
|
53
|
+
fixef?: readonly number[];
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* A contrast per factor of the fixed-effects part, as in {@link lmer} (see {@link LmerContrastName}); the
|
|
57
|
+
* random-effects terms always use treatment contrasts.
|
|
58
|
+
*/
|
|
59
|
+
contrasts?: Record<string, LmerContrastName>;
|
|
60
|
+
/**
|
|
61
|
+
* The order of the levels of a factor (`{ A: ["c", "a", "b"] }`), as `factor(x, levels = ...)` in R. For a binomial
|
|
62
|
+
* response given as strings, the first level is failure and the others success.
|
|
63
|
+
*/
|
|
64
|
+
levels?: Record<string, readonly string[]>;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Result of {@link glmer}: the fit of a generalized linear mixed model and the quantities of R's methods for it.
|
|
68
|
+
*
|
|
69
|
+
* @category Mixed models
|
|
70
|
+
*/
|
|
71
|
+
export interface GlmerResult {
|
|
72
|
+
/** The formula. */
|
|
73
|
+
formula: string;
|
|
74
|
+
/** The family. */
|
|
75
|
+
family: GlmerFamilyName;
|
|
76
|
+
/** The link. */
|
|
77
|
+
link: GlmLinkName;
|
|
78
|
+
/** 1 (the Laplace approximation), 0 (β from the penalised IRLS) or the number of quadrature points (2 to 100). */
|
|
79
|
+
nAGQ: number;
|
|
80
|
+
/** Number of rows used (rows with prior weight 0 included; R's `nobs()`). */
|
|
81
|
+
nObs: number;
|
|
82
|
+
/** 0-based indices of the rows of `data` used (rows with a missing value in a variable, a weight or the offset dropped). */
|
|
83
|
+
rows: number[];
|
|
84
|
+
/** The estimate of the covariance parameters θ (R's `getME(fit, "theta")`): the lower triangles of the relative covariance factors, by term, column-major. */
|
|
85
|
+
theta: number[];
|
|
86
|
+
/** Names of the columns of the fixed-effects model matrix (aliased columns dropped). */
|
|
87
|
+
fixefNames: string[];
|
|
88
|
+
/** The fixed effects β̂ (R's `fixef()`). */
|
|
89
|
+
fixef: number[];
|
|
90
|
+
/** The coefficient table of the fixed effects (R's `coef(summary(fit))`, without the p-values). */
|
|
91
|
+
coefTable: {
|
|
92
|
+
/** Estimates (R's `Estimate`). */
|
|
93
|
+
estimate: number[];
|
|
94
|
+
/** Standard errors (R's `Std. Error`). */
|
|
95
|
+
stdError: number[];
|
|
96
|
+
/** Estimates over standard errors (R's `z value`; R labels it `t value` for Gamma, inverse.gaussian and gaussian). */
|
|
97
|
+
zValue: number[];
|
|
98
|
+
};
|
|
99
|
+
/** The covariance matrix of the fixed effects (R's `vcov()`), array of rows. */
|
|
100
|
+
vcov: number[][];
|
|
101
|
+
/**
|
|
102
|
+
* How `vcov` was computed: `"hessian"`, 2 times the block of the fixed effects of the inverse Hessian of the
|
|
103
|
+
* deviance in (θ, β) (R's default for nAGQ = 1 when the fit is not singular, nObs < 10000 and there are fewer than
|
|
104
|
+
* 20 parameters), or `"RX"`, σ² (R_X'R_X)⁻¹ of the penalised IRLS at the conditional mode.
|
|
105
|
+
*/
|
|
106
|
+
vcovFrom: "hessian" | "RX";
|
|
107
|
+
/**
|
|
108
|
+
* R's `sigma()`: 1 for binomial and poisson; otherwise the square root of the sum of the squared Pearson residuals
|
|
109
|
+
* plus the squared length of the spherical random effects, over nObs.
|
|
110
|
+
*/
|
|
111
|
+
sigma: number;
|
|
112
|
+
/** The variance components, one entry per random-effects term (R's `VarCorr()`; not scaled by sigma). */
|
|
113
|
+
varCorr: LmerVarCorr[];
|
|
114
|
+
/** The conditional modes of the random effects, one entry per grouping factor (R's `ranef()`). */
|
|
115
|
+
ranef: LmerRanef[];
|
|
116
|
+
/** The fitted means μ̂, one per row used (R's `fitted()`; for a binomial response the probabilities). */
|
|
117
|
+
fitted: number[];
|
|
118
|
+
/** The linear predictor η̂ = offset + Xβ̂ + Zb̂, one per row used (R's `predict()`). */
|
|
119
|
+
linearPredictor: number[];
|
|
120
|
+
/** The deviance residuals (R's `residuals()`, whose default type for glmer is `"deviance"`). */
|
|
121
|
+
residuals: number[];
|
|
122
|
+
/** The Pearson residuals √w (y − μ̂) / √V(μ̂) (R's `residuals(fit, "pearson")`). */
|
|
123
|
+
pearsonResiduals: number[];
|
|
124
|
+
/** The working residuals (y − μ̂) / μ'(η̂) (R's `residuals(fit, "working")`). */
|
|
125
|
+
workingResiduals: number[];
|
|
126
|
+
/** The response residuals y − μ̂ (R's `residuals(fit, "response")`; y the proportion for `cbind(s, f)`). */
|
|
127
|
+
responseResiduals: number[];
|
|
128
|
+
/** The sum of the squared deviance residuals (R's `deviance()`; not −2 logLik). */
|
|
129
|
+
deviance: number;
|
|
130
|
+
/**
|
|
131
|
+
* The log-likelihood: of the Laplace approximation, −1/2 times the deviance minimised (R's `logLik()`), or with
|
|
132
|
+
* nAGQ > 1 of the quadrature, including the constant of the saturated model that R's `logLik()` leaves out there
|
|
133
|
+
* (so that it is comparable with nAGQ = 1 and with glm).
|
|
134
|
+
*/
|
|
135
|
+
logLik: number;
|
|
136
|
+
/** Degrees of freedom of the log-likelihood: the numbers of fixed effects and of θ, plus 1 for the families with a dispersion. */
|
|
137
|
+
df: number;
|
|
138
|
+
/** Akaike's information criterion (R's `AIC()`). */
|
|
139
|
+
AIC: number;
|
|
140
|
+
/** Schwarz's Bayesian information criterion (R's `BIC()`). */
|
|
141
|
+
BIC: number;
|
|
142
|
+
/** Whether the fit is singular: a diagonal element of a relative covariance factor below 1e-4 (R's `isSingular()`). */
|
|
143
|
+
singular: boolean;
|
|
144
|
+
/** R's messages (a rank-deficient fixed-effects matrix, a singular fit). */
|
|
145
|
+
messages: string[];
|
|
146
|
+
/** R's warnings (predictors on very different scales, the response, the fallback of vcov). */
|
|
147
|
+
warnings: string[];
|
|
148
|
+
}
|
|
149
|
+
export declare const VCOV_FALLBACK = "variance-covariance matrix computed from finite-difference Hessian is\nnot positive definite or contains NA values: falling back to var-cov estimated from RX";
|
|
150
|
+
/**
|
|
151
|
+
* What a fit by {@link glmer} was made from and with (internal: not exported from the package), for the functions that
|
|
152
|
+
* work on a fit beyond its result fields (glmerSummary, glmerAnova, prediction): the model's structure, the data object
|
|
153
|
+
* (model comparison requires the same object) and its snapshot (prediction rebuilds the model from it), the options as
|
|
154
|
+
* given (the weights and the offset as an array or a column name), and the estimate with R_X'R_X, the random effects,
|
|
155
|
+
* the linear predictor and the means at it.
|
|
156
|
+
*/
|
|
157
|
+
export interface GlmerInternals {
|
|
158
|
+
/** The structure of the model (X, Z, the terms, the family, the response, prior weights and offset of the rows used) */
|
|
159
|
+
structure: GlmerStructure;
|
|
160
|
+
/** The data frame given to glmer (the same object, not a copy: fits are compared on the same data object) */
|
|
161
|
+
data: DataFrame;
|
|
162
|
+
/** A copy of the columns of `data` at the time of the fit */
|
|
163
|
+
snapshot: DataFrame;
|
|
164
|
+
/** The options given to glmer (a copy; `weights` and `offset` as given, arrays copied) */
|
|
165
|
+
options: GlmerOptions;
|
|
166
|
+
/** The estimate θ̂ (a copy) */
|
|
167
|
+
theta: number[];
|
|
168
|
+
/** The fixed effects β̂ (a copy) */
|
|
169
|
+
beta: number[];
|
|
170
|
+
/**
|
|
171
|
+
* R_X'R_X = X'WX − X'WZΛ (Λ'Z'WZΛ + I)⁻¹ Λ'Z'WX at θ̂ with the Fisher weights at the conditional mode (not scaled by
|
|
172
|
+
* σ²; array of rows)
|
|
173
|
+
*/
|
|
174
|
+
rxtrx: number[][];
|
|
175
|
+
/** The random effects b̂ = Λ_θ̂ û at the conditional mode, in the order of the columns of Z (a copy) */
|
|
176
|
+
b: number[];
|
|
177
|
+
/** The linear predictor η̂ at the mode, one per row used (a copy) */
|
|
178
|
+
eta: number[];
|
|
179
|
+
/** The fitted means μ̂ (lme4's inverse link), one per row used (a copy) */
|
|
180
|
+
mu: number[];
|
|
181
|
+
/** R's sigma (1 for binomial and poisson) */
|
|
182
|
+
sigma: number;
|
|
183
|
+
}
|
|
184
|
+
/** Whether an object is a result returned by {@link glmer} (internal) */
|
|
185
|
+
export declare function isGlmerFit(fit: unknown): fit is GlmerResult;
|
|
186
|
+
/**
|
|
187
|
+
* The internals of a fit (see {@link GlmerInternals}). Internal. Throws a TypeError for an object that is not a result
|
|
188
|
+
* returned by {@link glmer}.
|
|
189
|
+
*/
|
|
190
|
+
export declare function glmerInternals(fit: GlmerResult): GlmerInternals;
|
|
191
|
+
/**
|
|
192
|
+
* Fit a generalized linear mixed model by the Laplace approximation (`nAGQ = 1`, default), by adaptive Gauss-Hermite
|
|
193
|
+
* quadrature (`nAGQ` from 2 to 100) or with the fixed effects from the penalised IRLS (`nAGQ = 0`). Equivalent to `lme4::glmer()` in R. With the quantities of its methods:
|
|
194
|
+
* `fixef()`, `coef(summary())` (without p-values), `vcov()`, `sigma()`, `VarCorr()`, `ranef()`, `fitted()`,
|
|
195
|
+
* `predict()`, `residuals()` of the four types, `deviance()`, `logLik()`, `AIC()`, `BIC()` and `isSingular()`.
|
|
196
|
+
*
|
|
197
|
+
* The families are binomial, poisson, Gamma, inverse.gaussian and gaussian (not with the identity link: that is
|
|
198
|
+
* {@link lmer}), with the links of {@link glm} and lme4's own inverse links and variance functions. The formula is
|
|
199
|
+
* as in {@link lmer}; a binomial response may be 0/1 values, booleans, strings or a factor (the first level failure),
|
|
200
|
+
* proportions with the numbers of trials as `weights`, or `cbind(successes, failures)` with two column names on the
|
|
201
|
+
* left-hand side.
|
|
202
|
+
*
|
|
203
|
+
* The estimate is the exact minimiser of the Laplace deviance computed at the exact conditional mode of the random
|
|
204
|
+
* effects (lme4's optimiser and its penalised IRLS stop early, so lme4's values typically differ in the 4th to 6th
|
|
205
|
+
* significant digit, its standard errors by up to about 1%). Elements of θ on the boundary are exactly 0, and the fit
|
|
206
|
+
* is then singular. lme4's warnings about its optimiser and its convergence checks are not given.
|
|
207
|
+
*
|
|
208
|
+
* The scale of the response: lme4's Laplace criterion leaves out the dispersion, so for the poisson,
|
|
209
|
+
* inverse.gaussian and gaussian families it is not invariant to the scale of the response (or of the prior weights).
|
|
210
|
+
* Where the information about the random effects at the start, Σ W z² of a column of Z (W the working weights at
|
|
211
|
+
* the starting means), is above 2^40 (the penalised system then loses about 40 of its 53 bits), or below 2^-40 and
|
|
212
|
+
* the fit fails or a term ends at θ = 0 on a criterion flat to rounding, the result is a RangeError saying so. For
|
|
213
|
+
* data like the example below: poisson counts up to about 1e11 (prior weights up to about 2^34), inverse.gaussian
|
|
214
|
+
* with the log link up to about 1e13, gaussian with the log link from about 1e-6 to 5e4 fit; beyond, the RangeError.
|
|
215
|
+
* Gamma fits are not limited by the scale of the response (its criterion is invariant; with the log link a tiny
|
|
216
|
+
* response is rescaled).
|
|
217
|
+
*
|
|
218
|
+
* Adaptive Gauss-Hermite quadrature (`nAGQ > 1`) needs a single scalar random-effects term such as `(1 | g)`, with
|
|
219
|
+
* R's errors otherwise. It is offered for binomial and poisson only: for Gamma, inverse.gaussian and gaussian it is a
|
|
220
|
+
* RangeError, since lme4 holds the dispersion at 1 there, so that its estimates would not be maximum likelihood. Its
|
|
221
|
+
* `logLik`, `AIC` and `BIC` are those of the log-likelihood, unlike R's, which leave out the constant of the
|
|
222
|
+
* saturated model (`aic − Σ` deviance residuals) for nAGQ > 1: they differ from R's by that constant and are
|
|
223
|
+
* comparable with the Laplace fit's.
|
|
224
|
+
*
|
|
225
|
+
* @param formula A formula such as `"cbind(inc, notinc) ~ period + (1 | herd)"` or `"y ~ x + (1 | g)"`.
|
|
226
|
+
* @param data The data frame: an object of columns.
|
|
227
|
+
* @param options `family`, `link`, `weights`, `offset`, `nAGQ`, `start`, and `contrasts` and `levels` as in
|
|
228
|
+
* {@link lmer} (see {@link GlmerOptions} for the keys accepted).
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```ts
|
|
232
|
+
* // R: lme4::glmer(y ~ x + (1 | g), data, family = poisson)
|
|
233
|
+
* const fit = glmer("y ~ x + (1 | g)", {
|
|
234
|
+
* y: [2, 3, 6, 7, 8, 1, 0, 3, 2, 5, 4, 6, 9, 8, 13, 0, 2, 1, 4, 3],
|
|
235
|
+
* x: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
|
|
236
|
+
* g: ["a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", "d", "d", "d", "d", "d"],
|
|
237
|
+
* }, { family: "poisson" });
|
|
238
|
+
* fit.fixef; // [0.2512, 0.3242]
|
|
239
|
+
* fit.coefTable.stdError; // [0.4183, 0.08098]
|
|
240
|
+
* fit.varCorr[0].sd; // [0.5423]
|
|
241
|
+
* fit.ranef[0].values; // [[0.2840], [-0.4170], [0.6920], [-0.4819]]
|
|
242
|
+
* fit.logLik; // -39.392
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* @category Mixed models
|
|
246
|
+
*/
|
|
247
|
+
export declare function glmer(formula: string, data: DataFrame, options?: GlmerOptions): GlmerResult;
|
|
248
|
+
/**
|
|
249
|
+
* The largest information of a column of Z about its random effect at the start, Σ_i W_i z_ij² over the rows of the
|
|
250
|
+
* column (Z scaled as in the fit, so that θ' = 1 is the start of the minimisation; W the working weights at the
|
|
251
|
+
* family's starting means). The Laplace criterion resolves θ' where θ'² times this is neither negligible against the
|
|
252
|
+
* 1 of Λ'Z'WZΛ + I nor so large that the system loses its digits against the fixed effects (internal; exported for
|
|
253
|
+
* the tests)
|
|
254
|
+
*/
|
|
255
|
+
export declare function startInformation(s: GlmerStructure): number;
|