@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.
Files changed (243) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/README.ja.md +57 -23
  3. package/README.md +60 -23
  4. package/dist/chunks/{chunk-5UQCRBAG.js → chunk-3W5NFFAA.js} +1 -1
  5. package/dist/chunks/chunk-4F2IALD4.js +3 -0
  6. package/dist/chunks/{chunk-QNNL7AZ3.js → chunk-4OLDGML4.js} +2 -2
  7. package/dist/chunks/chunk-63OPBEYO.js +2 -0
  8. package/dist/chunks/chunk-6XJBCRBV.js +2 -0
  9. package/dist/chunks/chunk-BQSIBJVX.js +2 -0
  10. package/dist/chunks/chunk-BWKHYRM6.js +3 -0
  11. package/dist/chunks/{chunk-W26LRYOJ.js → chunk-CKV4JWW2.js} +1 -1
  12. package/dist/chunks/chunk-DCAWMF4K.js +2 -0
  13. package/dist/chunks/chunk-F7YGX7PN.js +2 -0
  14. package/dist/chunks/chunk-H7I32ZZE.js +2 -0
  15. package/dist/chunks/chunk-IEOD5GWC.js +3 -0
  16. package/dist/chunks/chunk-KICBQGFN.js +2 -0
  17. package/dist/chunks/chunk-LROMWFAK.js +2 -0
  18. package/dist/chunks/chunk-NSWENS3S.js +2 -0
  19. package/dist/chunks/chunk-O2SQMGE3.js +2 -0
  20. package/dist/chunks/chunk-QQX46TCI.js +2 -0
  21. package/dist/chunks/chunk-QZK3CCBA.js +2 -0
  22. package/dist/chunks/chunk-UJ66VQIG.js +2 -0
  23. package/dist/chunks/chunk-UKNDX4BA.js +2 -0
  24. package/dist/chunks/chunk-WIBKS73G.js +2 -0
  25. package/dist/chunks/{chunk-URIO5LY5.js → chunk-WQWZ7FND.js} +2 -2
  26. package/dist/chunks/chunk-WWVMVZUL.js +2 -0
  27. package/dist/chunks/chunk-YG5S5AVP.js +2 -0
  28. package/dist/chunks/chunk-YHNMBYSW.js +2 -0
  29. package/dist/data/grouping.js +6 -2
  30. package/dist/descriptive/basic.js +20 -0
  31. package/dist/descriptive/cov.js +11 -0
  32. package/dist/descriptive/dataFrames.js +13 -5
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +10 -2
  35. package/dist/descriptive/hist.js +6 -0
  36. package/dist/descriptive/moments.js +14 -0
  37. package/dist/descriptive/omega.js +8 -3
  38. package/dist/descriptive/pivot.d.ts +4 -1
  39. package/dist/descriptive/pivot.js +27 -13
  40. package/dist/descriptive/quantile.js +8 -0
  41. package/dist/descriptive/rank.js +4 -0
  42. package/dist/descriptive/table.d.ts +20 -1
  43. package/dist/descriptive/table.js +14 -14
  44. package/dist/descriptive.js +2 -2
  45. package/dist/distributions/arcsine.js +8 -0
  46. package/dist/distributions/beta.js +9 -0
  47. package/dist/distributions/binomial.js +9 -0
  48. package/dist/distributions/cauchy.js +9 -0
  49. package/dist/distributions/chisq.js +9 -0
  50. package/dist/distributions/exponential.js +9 -0
  51. package/dist/distributions/f.js +9 -0
  52. package/dist/distributions/gamma.js +8 -0
  53. package/dist/distributions/geometric.js +9 -0
  54. package/dist/distributions/hypergeometric.js +8 -0
  55. package/dist/distributions/invgamma.js +8 -0
  56. package/dist/distributions/kumaraswamy.js +9 -0
  57. package/dist/distributions/laplace.js +9 -0
  58. package/dist/distributions/logistic.js +9 -0
  59. package/dist/distributions/lognormal.js +8 -0
  60. package/dist/distributions/multinomial.js +4 -0
  61. package/dist/distributions/negbinomial.js +8 -0
  62. package/dist/distributions/normal.js +8 -0
  63. package/dist/distributions/pareto.js +8 -0
  64. package/dist/distributions/poisson.js +9 -0
  65. package/dist/distributions/signrank.js +9 -0
  66. package/dist/distributions/summary.d.ts +2 -1
  67. package/dist/distributions/summary.js +31 -1
  68. package/dist/distributions/t.js +9 -0
  69. package/dist/distributions/triangular.js +9 -0
  70. package/dist/distributions/tukey.js +6 -0
  71. package/dist/distributions/uniform.js +8 -0
  72. package/dist/distributions/weibull.js +8 -0
  73. package/dist/distributions/wilcox.js +9 -0
  74. package/dist/distributions.js +2 -2
  75. package/dist/entries/descriptive.d.ts +3 -1
  76. package/dist/entries/descriptive.js +1 -1
  77. package/dist/entries/distributions.d.ts +1 -0
  78. package/dist/entries/distributions.js +2 -0
  79. package/dist/entries/linalg.d.ts +3 -1
  80. package/dist/entries/mixed.d.ts +9 -0
  81. package/dist/entries/mixed.js +8 -1
  82. package/dist/entries/numerics.d.ts +2 -1
  83. package/dist/entries/tests.d.ts +8 -5
  84. package/dist/entries/tests.js +3 -1
  85. package/dist/htest/agreement.d.ts +13 -0
  86. package/dist/htest/agreement.js +46 -7
  87. package/dist/htest/ansariTest.d.ts +2 -1
  88. package/dist/htest/ansariTest.js +43 -12
  89. package/dist/htest/aov.d.ts +28 -22
  90. package/dist/htest/aov.js +42 -13
  91. package/dist/htest/binomTest.js +6 -0
  92. package/dist/htest/brunnerMunzelTest.js +5 -0
  93. package/dist/htest/chisqTest.d.ts +3 -1
  94. package/dist/htest/chisqTest.js +21 -3
  95. package/dist/htest/cor.d.ts +7 -1
  96. package/dist/htest/cor.js +40 -7
  97. package/dist/htest/core/simulate.d.ts +6 -0
  98. package/dist/htest/core/simulate.js +20 -0
  99. package/dist/htest/effectSize.d.ts +3 -0
  100. package/dist/htest/effectSize.js +28 -8
  101. package/dist/htest/fisherTest.d.ts +2 -1
  102. package/dist/htest/fisherTest.js +52 -6
  103. package/dist/htest/ksTest.d.ts +4 -0
  104. package/dist/htest/ksTest.js +13 -4
  105. package/dist/htest/leveneTest.d.ts +2 -0
  106. package/dist/htest/leveneTest.js +9 -3
  107. package/dist/htest/mantelhaenTest.js +7 -0
  108. package/dist/htest/mcnemarTest.js +8 -0
  109. package/dist/htest/multipleComparisons.d.ts +16 -25
  110. package/dist/htest/multipleComparisons.js +26 -43
  111. package/dist/htest/normalityTests.js +10 -4
  112. package/dist/htest/onewayTest.js +10 -0
  113. package/dist/htest/pAdjust.js +4 -0
  114. package/dist/htest/pairwise.d.ts +2 -0
  115. package/dist/htest/pairwise.js +29 -6
  116. package/dist/htest/poissonTest.js +6 -0
  117. package/dist/htest/polycor.js +20 -10
  118. package/dist/htest/power.d.ts +4 -0
  119. package/dist/htest/power.js +16 -2
  120. package/dist/htest/propTest.d.ts +3 -0
  121. package/dist/htest/propTest.js +23 -2
  122. package/dist/htest/pwr.d.ts +38 -1
  123. package/dist/htest/pwr.js +38 -15
  124. package/dist/htest/rankTests.js +7 -1
  125. package/dist/htest/tTest.d.ts +2 -0
  126. package/dist/htest/tTest.js +9 -0
  127. package/dist/htest/testFormula.js +11 -11
  128. package/dist/htest/trendTests.d.ts +21 -1
  129. package/dist/htest/trendTests.js +13 -18
  130. package/dist/htest/types.d.ts +2 -0
  131. package/dist/htest/varTest.js +7 -0
  132. package/dist/htest/wilcoxTest.js +51 -9
  133. package/dist/internal/checkOptions.d.ts +11 -0
  134. package/dist/internal/checkOptions.js +31 -0
  135. package/dist/internal/optionalArgs.d.ts +12 -0
  136. package/dist/internal/optionalArgs.js +46 -0
  137. package/dist/internal/warnings.d.ts +66 -0
  138. package/dist/internal/warnings.js +122 -0
  139. package/dist/linalg/basic.js +7 -0
  140. package/dist/linalg/chol.js +6 -0
  141. package/dist/linalg/eigen.js +4 -0
  142. package/dist/linalg/expm.js +4 -0
  143. package/dist/linalg/kappa.js +6 -0
  144. package/dist/linalg/lu.js +5 -0
  145. package/dist/linalg/norm.js +4 -0
  146. package/dist/linalg/ops.d.ts +27 -2
  147. package/dist/linalg/ops.js +14 -13
  148. package/dist/linalg/qr.js +14 -0
  149. package/dist/linalg/solve.js +9 -0
  150. package/dist/linalg/svd.js +6 -2
  151. package/dist/linalg.js +2 -2
  152. package/dist/mixed.js +5 -4
  153. package/dist/models/anovaSequential.d.ts +16 -1
  154. package/dist/models/anovaSequential.js +50 -7
  155. package/dist/models/aovEz.js +16 -11
  156. package/dist/models/aovStrata.js +15 -4
  157. package/dist/models/carAnova.js +11 -17
  158. package/dist/models/coxph.d.ts +6 -0
  159. package/dist/models/coxph.js +21 -6
  160. package/dist/models/formula.d.ts +5 -2
  161. package/dist/models/formula.js +31 -8
  162. package/dist/models/glm.d.ts +22 -2
  163. package/dist/models/glm.js +32 -22
  164. package/dist/models/glmAnova.js +8 -3
  165. package/dist/models/glmConfint.js +6 -2
  166. package/dist/models/glmFamily.d.ts +1 -6
  167. package/dist/models/glmFamily.js +3 -22
  168. package/dist/models/glmer.d.ts +255 -0
  169. package/dist/models/glmer.js +635 -0
  170. package/dist/models/glmerAnova.d.ts +81 -0
  171. package/dist/models/glmerAnova.js +139 -0
  172. package/dist/models/glmerDeviance.d.ts +98 -0
  173. package/dist/models/glmerDeviance.js +576 -0
  174. package/dist/models/glmerFamily.d.ts +57 -0
  175. package/dist/models/glmerFamily.js +236 -0
  176. package/dist/models/glmerGradient.d.ts +46 -0
  177. package/dist/models/glmerGradient.js +301 -0
  178. package/dist/models/glmerStructure.d.ts +45 -0
  179. package/dist/models/glmerStructure.js +168 -0
  180. package/dist/models/glmerSummary.d.ts +102 -0
  181. package/dist/models/glmerSummary.js +94 -0
  182. package/dist/models/lm.js +13 -3
  183. package/dist/models/lmer.d.ts +27 -6
  184. package/dist/models/lmer.js +75 -321
  185. package/dist/models/lmerAnova.d.ts +52 -3
  186. package/dist/models/lmerAnova.js +101 -45
  187. package/dist/models/lmerTest.d.ts +12 -5
  188. package/dist/models/lmerTest.js +39 -18
  189. package/dist/models/mauchly.d.ts +2 -0
  190. package/dist/models/mauchly.js +7 -2
  191. package/dist/models/mixedDeviance.d.ts +28 -2
  192. package/dist/models/mixedDeviance.js +84 -56
  193. package/dist/models/mixedMinimiser.d.ts +83 -0
  194. package/dist/models/mixedMinimiser.js +438 -0
  195. package/dist/models/mixedPredict.d.ts +121 -0
  196. package/dist/models/mixedPredict.js +530 -0
  197. package/dist/models/mixedStructure.d.ts +62 -3
  198. package/dist/models/mixedStructure.js +74 -29
  199. package/dist/models/modelFrame.js +2 -2
  200. package/dist/models/modelMatrix.d.ts +8 -1
  201. package/dist/models/modelMatrix.js +56 -39
  202. package/dist/models/survival.d.ts +6 -0
  203. package/dist/models/survival.js +9 -2
  204. package/dist/numerics/deriv.js +8 -0
  205. package/dist/numerics/integrate.js +8 -0
  206. package/dist/numerics/interp.d.ts +32 -7
  207. package/dist/numerics/interp.js +27 -9
  208. package/dist/numerics/ode.js +4 -0
  209. package/dist/numerics.js +2 -2
  210. package/dist/optim/optim.js +17 -2
  211. package/dist/random/beta.js +5 -0
  212. package/dist/random/continuous.js +17 -0
  213. package/dist/random/extra.js +13 -0
  214. package/dist/random/gamma.js +11 -0
  215. package/dist/random/poisson.js +5 -0
  216. package/dist/random/rng.js +4 -0
  217. package/dist/random/sample.js +6 -0
  218. package/dist/rstats.js +17 -14
  219. package/dist/special/besselIK.js +7 -0
  220. package/dist/special/choose.js +7 -0
  221. package/dist/special/psigamma.js +7 -0
  222. package/dist/special.js +2 -2
  223. package/dist/tests.js +14 -11
  224. package/llms.txt +15 -4
  225. package/package.json +19 -10
  226. package/dist/chunks/chunk-2D2IQWUQ.js +0 -2
  227. package/dist/chunks/chunk-6CSTWA5E.js +0 -2
  228. package/dist/chunks/chunk-72Y3HZID.js +0 -2
  229. package/dist/chunks/chunk-BG2KLEHM.js +0 -2
  230. package/dist/chunks/chunk-CMEKBNHQ.js +0 -2
  231. package/dist/chunks/chunk-FLJNSXSW.js +0 -2
  232. package/dist/chunks/chunk-GM53LWSR.js +0 -2
  233. package/dist/chunks/chunk-JBCA56ZS.js +0 -3
  234. package/dist/chunks/chunk-K3RJAD2T.js +0 -2
  235. package/dist/chunks/chunk-KAZRWIB5.js +0 -2
  236. package/dist/chunks/chunk-KBVVIMXX.js +0 -3
  237. package/dist/chunks/chunk-N3SIFCZX.js +0 -2
  238. package/dist/chunks/chunk-SC6CV4Z3.js +0 -2
  239. package/dist/chunks/chunk-SYWOBOTR.js +0 -2
  240. package/dist/chunks/chunk-VTUGG2SV.js +0 -2
  241. package/dist/chunks/chunk-XHETEYVC.js +0 -2
  242. package/dist/chunks/chunk-YRZWONZB.js +0 -3
  243. package/dist/chunks/chunk-ZGABZMWX.js +0 -2
@@ -0,0 +1,236 @@
1
+ // The families and links of generalised linear mixed models as lme4's glmer computes them: its own inverse links,
2
+ // their derivatives and the variance functions, which clamp differently from R's family objects (and from glm's,
3
+ // src/models/glmFamily.ts, which is not changed); the link function itself, the deviance residuals, the checks of
4
+ // the response, the starting means and the aic term are those of glm.
5
+ //
6
+ // Written for rStats.js (no ported code) from oracle/glmer-behaviour.md ("Links and families as lme4 computes them").
7
+ import { dcauchy1, pcauchy1 } from "../distributions/cauchy.js";
8
+ import { dlogis1, plogis1 } from "../distributions/logistic.js";
9
+ import { dnorm, pnorm } from "../distributions/normal.js";
10
+ import { makeFamily } from "./glmFamily.js";
11
+ /** lme4's eps: the clamps of its links and of the binomial variance (DBL_EPSILON, 2^-52) */
12
+ const EPS = 2 ** -52;
13
+ /**
14
+ * lme4's inverse link and its derivative for the links it computes itself (logit, probit, cauchit, cloglog, log,
15
+ * identity, inverse); undefined for sqrt and 1/mu^2, which are R's family functions. The clamps are lme4's: μ never
16
+ * exactly 0 or 1 for logit and cloglog (but probit and cauchit have no lower clamp), the log link's mean at least eps
17
+ * (e^-40 is 2.2e-16 here, unlike glm), the derivatives of logit, cloglog and log at least eps.
18
+ */
19
+ function lme4Link(link) {
20
+ switch (link) {
21
+ case "logit":
22
+ return {
23
+ linkinv: (e) => Math.max(EPS, Math.min(1 - EPS, plogis1(e, 0, 1, true, false))),
24
+ muEta: (e) => Math.max(EPS, dlogis1(e, 0, 1, false)),
25
+ };
26
+ case "probit":
27
+ // (no lower clamp: lme4's)
28
+ return { linkinv: (e) => Math.min(1 - EPS, pnorm(e)), muEta: (e) => dnorm(e) };
29
+ case "cauchit":
30
+ return { linkinv: (e) => Math.min(1 - EPS, pcauchy1(e, 0, 1, true, false)), muEta: (e) => dcauchy1(e, 0, 1, false) };
31
+ case "cloglog":
32
+ return {
33
+ linkinv: (e) => Math.max(EPS, Math.min(1 - EPS, -Math.expm1(-Math.exp(e)))),
34
+ muEta: (e) => Math.max(EPS, Math.exp(e - Math.exp(e))),
35
+ };
36
+ case "log":
37
+ return { linkinv: (e) => Math.max(EPS, Math.exp(e)), muEta: (e) => Math.max(EPS, Math.exp(e)) };
38
+ case "identity":
39
+ return { linkinv: (e) => e, muEta: () => 1 };
40
+ case "inverse":
41
+ // (no clamp: lme4's)
42
+ return { linkinv: (e) => 1 / e, muEta: (e) => -1 / (e * e) };
43
+ default:
44
+ return undefined;
45
+ }
46
+ }
47
+ /**
48
+ * d² mu / d eta² of the inverse links of {@link lme4Link} and of R's sqrt and 1/mu^2: the derivative of the muEta in
49
+ * use, so 0 where that is clamped at eps (logit, cloglog, log)
50
+ */
51
+ function linkSecondDerivative(link) {
52
+ switch (link) {
53
+ case "logit":
54
+ // mu' = mu (1 − mu), mu'' = mu' (1 − 2 mu) = −mu' tanh(eta / 2)
55
+ return (e) => {
56
+ const d = dlogis1(e, 0, 1, false);
57
+ return d < EPS ? 0 : -d * Math.tanh(e / 2);
58
+ };
59
+ case "probit":
60
+ return (e) => -e * dnorm(e);
61
+ case "cauchit":
62
+ return (e) => (-2 * e) / (Math.PI * (1 + e * e) * (1 + e * e));
63
+ case "cloglog":
64
+ return (e) => {
65
+ const d = Math.exp(e - Math.exp(e));
66
+ return d < EPS ? 0 : d * -Math.expm1(e);
67
+ };
68
+ case "log":
69
+ return (e) => (Math.exp(e) < EPS ? 0 : Math.exp(e));
70
+ case "identity":
71
+ return () => 0;
72
+ case "inverse":
73
+ return (e) => 2 / (e * e * e);
74
+ case "sqrt":
75
+ // (R's linkinv eta², muEta 2 eta)
76
+ return () => 2;
77
+ case "1/mu^2":
78
+ // (R's linkinv eta^(−1/2), muEta −eta^(−3/2) / 2)
79
+ return (e) => 0.75 / (e * e * Math.sqrt(e));
80
+ }
81
+ }
82
+ /** dV / d mu of lme4's variance functions (0 where the binomial one is clamped at eps) */
83
+ function varianceDerivativeOf(family) {
84
+ switch (family) {
85
+ case "binomial":
86
+ return (m) => (m * (1 - m) < EPS ? 0 : 1 - 2 * m);
87
+ case "poisson":
88
+ return () => 1;
89
+ case "Gamma":
90
+ return (m) => 2 * m;
91
+ case "inverse.gaussian":
92
+ return (m) => 3 * m * m;
93
+ case "gaussian":
94
+ return () => 0;
95
+ }
96
+ }
97
+ /** lme4's variance functions (the binomial one at least eps) */
98
+ function lme4Variance(family) {
99
+ switch (family) {
100
+ case "binomial":
101
+ return (m) => Math.max(EPS, m * (1 - m));
102
+ case "poisson":
103
+ return (m) => m;
104
+ case "Gamma":
105
+ return (m) => m * m;
106
+ case "inverse.gaussian":
107
+ return (m) => m * m * m;
108
+ case "gaussian":
109
+ return () => 1;
110
+ }
111
+ }
112
+ const GLMER_FAMILIES = ["binomial", "poisson", "Gamma", "inverse.gaussian", "gaussian"];
113
+ /**
114
+ * The family of glmer from a family name or specification and a link given separately (glm's arguments): the quasi
115
+ * families are an error (lme4's first check), and so is gaussian with the identity link, which R fits with lmer
116
+ * (rStats.js's choice G3).
117
+ */
118
+ export function glmerFamily(family, link) {
119
+ const spec = typeof family === "object" && family !== null ? family : { family: family ?? "gaussian" };
120
+ if (typeof spec.family === "string" && spec.family.startsWith("quasi"))
121
+ throw new RangeError('"quasi" families cannot be used in glmer');
122
+ // (glm's resolution: unknown families and links, the power links of quasi only)
123
+ const glm = makeFamily(spec, link);
124
+ if (!GLMER_FAMILIES.includes(glm.family))
125
+ throw new RangeError(`unknown family "${String(glm.family)}"`);
126
+ const name = glm.family;
127
+ const linkName = glm.name;
128
+ if (name === "gaussian" && linkName === "identity") {
129
+ throw new RangeError("family gaussian with the identity link is a linear mixed model: use lmer (R's glmer calls lmer with a deprecation warning)");
130
+ }
131
+ const own = lme4Link(linkName);
132
+ return {
133
+ family: name,
134
+ link: linkName,
135
+ linkfun: glm.link,
136
+ // (sqrt and 1/mu^2: R's family functions, as glm's)
137
+ linkinv: own?.linkinv ?? glm.linkinv,
138
+ muEta: own?.muEta ?? glm.muEta,
139
+ variance: lme4Variance(name),
140
+ muEta2: linkSecondDerivative(linkName),
141
+ varianceDerivative: varianceDerivativeOf(name),
142
+ devResid: glm.devContribution,
143
+ glm,
144
+ };
145
+ }
146
+ // ---------------------------------------------------------------------------------------------------------------------
147
+ // The products of μ' and V of a row where they leave the range of the doubles (as glm's working weights, #159 #165-#168
148
+ // in src/models/glm.ts): a poisson mean beyond 1.34e154 squared μ' to Infinity, a Gamma mean beyond that V = μ², an
149
+ // inverse.gaussian mean beyond 5.6e102 V = μ³. Where μ' = c μ^a and V = μ^p (the families other than binomial with the
150
+ // links other than logit, probit, cauchit and cloglog; lme4's log link only above its clamp, which these means are),
151
+ // the quantities are formed from the powers of μ instead, which are in range wherever the quantity is. Elsewhere, and
152
+ // for every row whose parts are in range, R's form and bits.
153
+ const MIN_NORMAL = 2 ** -1022;
154
+ /** Whether v is a finite number of the normal range (not 0) */
155
+ const normal = (v) => Math.abs(v) >= MIN_NORMAL && Math.abs(v) < Infinity;
156
+ /** c, a and p of μ' = c μ^a and V = μ^p, where the family and link have that form and μ > 0 */
157
+ function powerForm(fam, mu) {
158
+ const m = fam.glm.muPower;
159
+ const p = fam.glm.varPower;
160
+ if (m === undefined || p === undefined || fam.family === "binomial" || !(mu > 0))
161
+ return undefined;
162
+ return { c: m.c, a: m.a, p };
163
+ }
164
+ /** Whether μ' and V of a row are such that their squares and products stay in range (R's forms then) */
165
+ function inRange(me, v) {
166
+ return normal(me * me) && normal(v);
167
+ }
168
+ /** The working weight w μ'² / V(μ) of a row of prior weight w > 0 (as w c² μ^(2a − p) where that overflows) */
169
+ export function workingWeight(fam, w, me, mu) {
170
+ const v = fam.variance(mu);
171
+ const ww = (w * me * me) / v;
172
+ if (inRange(me, v) && Number.isFinite(ww))
173
+ return ww;
174
+ const f = powerForm(fam, mu);
175
+ return f === undefined ? ww : w * f.c * f.c * mu ** (2 * f.a - f.p);
176
+ }
177
+ /** The score w (y − μ) μ' / V(μ) of a row (as w (y − μ) c μ^(a − p) where that overflows) */
178
+ export function workingScore(fam, w, y, me, mu) {
179
+ const v = fam.variance(mu);
180
+ const s = (w * (y - mu) * me) / v;
181
+ if (inRange(me, v) && Number.isFinite(s))
182
+ return s;
183
+ const f = powerForm(fam, mu);
184
+ return f === undefined ? s : w * ((y - mu) * f.c * mu ** (f.a - f.p));
185
+ }
186
+ /**
187
+ * The weights of the gradient of a row of prior weight w > 0 (src/models/glmerGradient.ts rowWeights): W, dW/dη, the
188
+ * score s and the observed weight Ŵ = W − w (y − μ)(μ''/V − (μ'/V)² V'), in R's form, or from the powers of μ
189
+ * (dW/dη = w c³ (2a − p) μ^(3a − p − 1), Ŵ = W − w (y − μ) c² (a − p) μ^(2a − p − 1)) where that overflows
190
+ */
191
+ export function gradientWeights(fam, w, y, eta, mu, canonical) {
192
+ const me = fam.muEta(eta);
193
+ const me2 = fam.muEta2(eta);
194
+ const v = fam.variance(mu);
195
+ const dv = fam.varianceDerivative(mu);
196
+ const r = y - mu;
197
+ const ww = (w * me * me) / v;
198
+ // (the products in this order so that μ'² and μ'³ do not overflow before the division where V is large)
199
+ const dww = w * ((2 * me * me2) / v - ((me / v) * (me / v)) * me * dv);
200
+ const score = (w * r * me) / v;
201
+ const observed = canonical ? ww : ww - w * r * (me2 / v - (me / v) * (me / v) * dv);
202
+ if (inRange(me, v) && Number.isFinite(ww) && Number.isFinite(dww) && Number.isFinite(score) && Number.isFinite(observed)) {
203
+ return { ww, dww, score, observed };
204
+ }
205
+ const f = powerForm(fam, mu);
206
+ if (f === undefined)
207
+ return { ww, dww, score, observed };
208
+ const { c, a, p } = f;
209
+ const wwPower = w * c * c * mu ** (2 * a - p);
210
+ return {
211
+ ww: wwPower,
212
+ dww: w * c * c * c * (2 * a - p) * mu ** (3 * a - p - 1),
213
+ score: w * (r * c * mu ** (a - p)),
214
+ observed: canonical ? wwPower : wwPower - w * (r * c * c * (a - p) * mu ** (2 * a - p - 1)),
215
+ };
216
+ }
217
+ /** The Pearson residual √w (y − μ) / √V(μ) (as ((y − μ)/h) √w / h, h = μ^(p/4), where V is out of range) */
218
+ export function pearsonResidualOf(fam, w, y, mu) {
219
+ const v = fam.variance(mu);
220
+ const r = (Math.sqrt(w) * (y - mu)) / Math.sqrt(v);
221
+ const f = powerForm(fam, mu);
222
+ if ((normal(v) && Number.isFinite(r)) || f === undefined)
223
+ return r;
224
+ const h = mu ** (f.p / 4);
225
+ return (((y - mu) / h) * Math.sqrt(w)) / h;
226
+ }
227
+ /** The working residual (y − μ) / μ' (as ((y − μ)/c)/μ^(a/2)/μ^(a/2) where μ' is out of range) */
228
+ export function workingResidualOf(fam, y, eta, mu) {
229
+ const me = fam.muEta(eta);
230
+ const r = (y - mu) / me;
231
+ const f = powerForm(fam, mu);
232
+ if (normal(me) || f === undefined)
233
+ return r;
234
+ const h = mu ** (f.a / 2);
235
+ return (y - mu) / f.c / h / h;
236
+ }
@@ -0,0 +1,46 @@
1
+ import { type GlmerFamily } from "./glmerFamily.js";
2
+ import type { GlmerStructure } from "./glmerStructure.js";
3
+ import { type SparseSystem } from "./mixedDeviance.js";
4
+ /** Whether the link is the family's canonical one (then Ŵ = W: s_i' = −W_i exactly) */
5
+ export declare function isCanonical(fam: GlmerFamily): boolean;
6
+ /**
7
+ * The derivative of the family's aic term (src/models/glmFamily.ts familyAic) with respect to each μ_i, the
8
+ * dispersion of Gamma, inverse.gaussian and gaussian (dev / Σ w or dev / n) differentiated through dev = Σ d_i, with
9
+ * ∂d_i/∂μ_i = −2 w_i (y_i − μ_i) / V(μ_i). Rows of prior weight 0 are not in the aic term (0).
10
+ */
11
+ export declare function aicDerivative(fam: GlmerFamily, y: readonly number[], mu: ArrayLike<number>, w: readonly number[], size: readonly number[] | undefined, dev: number): Float64Array;
12
+ /** The weights of the rows at η, μ: W, dW/dη, Ŵ and the score s */
13
+ export interface RowWeights {
14
+ ww: Float64Array;
15
+ dww: Float64Array;
16
+ observed: Float64Array;
17
+ score: Float64Array;
18
+ }
19
+ /** {@link RowWeights} at the mode (rows of prior weight 0: all 0) */
20
+ export declare function rowWeights(s: GlmerStructure, eta: ArrayLike<number>, mu: ArrayLike<number>): RowWeights;
21
+ /**
22
+ * h_i = m_i' A⁻¹ m_i for every row (m_i = Λ_θ' z_i, the i-th row of ZΛ_θ), from the selected inverse `inv` of A (the
23
+ * layout of L): the groups of a row are adjacent in A, so the blocks of A⁻¹ needed are on the pattern of L
24
+ */
25
+ export declare function rowLeverages(s: GlmerStructure, sys: SparseSystem, t: Float64Array[], inv: Float64Array, rows: ArrayLike<number>): Float64Array;
26
+ /** What the Laplace gradient needs from the evaluation at the mode */
27
+ export interface LaplaceState {
28
+ structure: GlmerStructure;
29
+ sys: SparseSystem;
30
+ /** The blocks T_k at θ */
31
+ t: Float64Array[];
32
+ /** The factor of A = Λ'Z'WZΛ + I at the mode */
33
+ l: Float64Array;
34
+ u: Float64Array;
35
+ eta: Float64Array;
36
+ mu: Float64Array;
37
+ /** Σ d_i at the mode (the dev of the aic term) */
38
+ devResid: number;
39
+ /** nAGQ = 0: β from the joint mode (the gradient is in θ only) */
40
+ joint: boolean;
41
+ }
42
+ /**
43
+ * The gradient of the Laplace deviance (see the header): with respect to (θ, β) for nAGQ = 1, θ for nAGQ = 0. Throws
44
+ * the Error of {@link factorise} if H is not positive definite (a non-canonical link far from a minimum of pdev).
45
+ */
46
+ export declare function laplaceGradient(state: LaplaceState): number[];
@@ -0,0 +1,301 @@
1
+ // The analytic gradient of glmer's deviances at the exact conditional mode: the Laplace deviance d₁(θ, β) (nAGQ = 1)
2
+ // and d₀(θ) (nAGQ = 0, β from the joint mode), by implicit differentiation through the mode. The adaptive
3
+ // Gauss-Hermite deviance (nAGQ > 1) has its own, in src/models/glmerDeviance.ts next to the deviance.
4
+ //
5
+ // Written for rStats.js (no ported code; lme4 has no analytic gradient) from the derivation in oracle/glmer-behaviour.md
6
+ // ("The gradient (rStats.js)"). In short, with M = ZΛ_θ, η = o + Xβ + Mu, μ = μ(η), the working (Fisher) weights
7
+ // W_i = w_i μ'_i² / V(μ_i), the score s_i = w_i (y_i − μ_i) μ'_i / V(μ_i), the observed weights Ŵ_i = −∂s_i/∂η_i,
8
+ // A = M'WM + I and H = M'ŴM + I (the Hessian of pdev / 2 in u; H = A for a canonical link):
9
+ // D = log det A + ‖u‖² + aic(μ), the mode u solves M's(η) = u.
10
+ // D depends on η directly (through aic and through W in A): ∂D/∂η_i = c_i = W'_i h_i + ∂aic/∂η_i, with W'_i = dW_i/dη_i
11
+ // and h_i = m_i' A⁻¹ m_i (m_i the i-th row of M), and on θ through Λ in A (2 tr(A⁻¹ Λ'Z'WZ ∂Λ), lmer's trace term with
12
+ // Z'WZ in place of Z'Z). The mode moves with the parameters: one solve of the adjoint system H v = M'c + 2u (nAGQ = 1;
13
+ // for nAGQ = 0 the bordered system of (u, β) with X'c below) replaces the derivatives of u. With r = c − Ŵ(Mv) (plus
14
+ // Ŵ X v_β for nAGQ = 0),
15
+ // ∂D/∂β = X'r, ∂D/∂θ_j = 2 tr(A⁻¹ Λ'Z'WZ ∂Λ) + Σ_(a,b) [(Z'r)_a u_b + (Z's)_a v_b],
16
+ // the sum over the entries (a, b) of Λ_θ that hold θ_j. The cost is about two factorisations (one when H = A), the
17
+ // selected inverse of A and a few products with Z, X and Λ.
18
+ import { potf2 } from "../linalg/chol.js";
19
+ import { roundHalfEven } from "../internal/integer.js";
20
+ import { digamma } from "../special/psigamma.js";
21
+ import { gradientWeights } from "./glmerFamily.js";
22
+ import { backSolve, blocksOfA, factorise, forwardSolve, lambdaTimes, lambdaTransposeTimes, levelTraceSums, selectedInverse, solveL, solveLt, weightedCrossProduct, zTimes, zTransposeTimes, } from "./mixedDeviance.js";
23
+ /** Whether the link is the family's canonical one (then Ŵ = W: s_i' = −W_i exactly) */
24
+ export function isCanonical(fam) {
25
+ return ((fam.family === "binomial" && fam.link === "logit") ||
26
+ (fam.family === "poisson" && fam.link === "log") ||
27
+ (fam.family === "Gamma" && fam.link === "inverse") ||
28
+ (fam.family === "inverse.gaussian" && fam.link === "1/mu^2"));
29
+ }
30
+ /**
31
+ * ψ(a) − log a + 1 / (2a) without cancellation for large a (the asymptotic series from a = 10 on: its next term is
32
+ * below 1e-17 relative there)
33
+ */
34
+ function digammaRemainder(a) {
35
+ if (a < 10)
36
+ return digamma(a) - Math.log(a) + 1 / (2 * a);
37
+ const a2 = 1 / (a * a);
38
+ return -a2 * (1 / 12 - a2 * (1 / 120 - a2 * (1 / 252 - a2 * (1 / 240 - a2 / 132))));
39
+ }
40
+ /**
41
+ * The derivative of the family's aic term (src/models/glmFamily.ts familyAic) with respect to each μ_i, the
42
+ * dispersion of Gamma, inverse.gaussian and gaussian (dev / Σ w or dev / n) differentiated through dev = Σ d_i, with
43
+ * ∂d_i/∂μ_i = −2 w_i (y_i − μ_i) / V(μ_i). Rows of prior weight 0 are not in the aic term (0).
44
+ */
45
+ export function aicDerivative(fam, y, mu, w, size, dev) {
46
+ const n = y.length;
47
+ const out = new Float64Array(n);
48
+ switch (fam.family) {
49
+ case "binomial": {
50
+ // −2 Σ (w/m) log dbinom(k, N, μ), k = round(m y), N = round(m)
51
+ const m = size !== undefined && size.some((v) => v > 1) ? size : w;
52
+ for (let i = 0; i < n; i++) {
53
+ const mi = m[i];
54
+ if (!(mi > 0))
55
+ continue;
56
+ const k = roundHalfEven(mi * y[i]);
57
+ const total = roundHalfEven(mi);
58
+ const ui = mu[i];
59
+ out[i] = -2 * (w[i] / mi) * ((k === 0 ? 0 : k / ui) - (total - k === 0 ? 0 : (total - k) / (1 - ui)));
60
+ }
61
+ return out;
62
+ }
63
+ case "poisson":
64
+ for (let i = 0; i < n; i++)
65
+ if (w[i] > 0)
66
+ out[i] = -2 * w[i] * (y[i] / mu[i] - 1);
67
+ return out;
68
+ case "Gamma": {
69
+ // −2 Σ w log dgamma(y; 1/D, μ D) + 2, D = dev / Σ w: directly −2 w (y − μ) / (D μ²), and through D, with
70
+ // ∂aic/∂D = −2 Σ w (ψ(a) − log a + 1/(2a)) / D² (a = 1/D; Σ w (y/μ − 1 − log(y/μ)) = dev / 2 used)
71
+ let sw = 0;
72
+ for (const wi of w)
73
+ sw += wi;
74
+ const a = sw / dev;
75
+ const factor = a - 2 * a * a * digammaRemainder(a);
76
+ for (let i = 0; i < n; i++) {
77
+ if (!(w[i] > 0))
78
+ continue;
79
+ const num = -2 * w[i] * (y[i] - mu[i]) * factor;
80
+ const m2 = mu[i] * mu[i];
81
+ // (divided by μ twice where μ² overflows: a mean of 1e200; the gradient multiplies by μ' again)
82
+ out[i] = m2 < Infinity ? num / m2 : num / mu[i] / mu[i];
83
+ }
84
+ return out;
85
+ }
86
+ case "inverse.gaussian": {
87
+ // Σw (log(2π dev / Σw) + 1) + ...: Σw / dev times ∂dev/∂μ
88
+ let sw = 0;
89
+ for (const wi of w)
90
+ if (wi > 0)
91
+ sw += wi;
92
+ for (let i = 0; i < n; i++) {
93
+ if (!(w[i] > 0))
94
+ continue;
95
+ const num = -2 * w[i] * (y[i] - mu[i]);
96
+ const m3 = mu[i] * mu[i] * mu[i];
97
+ // (divided by μ three times where μ³ is out of range: a mean of 1e110 or 1e-110)
98
+ out[i] = (sw / dev) * (Math.abs(m3) >= 2 ** -1022 && m3 < Infinity ? num / m3 : num / mu[i] / mu[i] / mu[i]);
99
+ }
100
+ return out;
101
+ }
102
+ case "gaussian": {
103
+ let nPos = 0;
104
+ for (const wi of w)
105
+ if (wi > 0)
106
+ nPos++;
107
+ for (let i = 0; i < n; i++)
108
+ if (w[i] > 0)
109
+ out[i] = (nPos / dev) * -2 * w[i] * (y[i] - mu[i]);
110
+ return out;
111
+ }
112
+ }
113
+ }
114
+ /** {@link RowWeights} at the mode (rows of prior weight 0: all 0) */
115
+ export function rowWeights(s, eta, mu) {
116
+ const n = s.y.length;
117
+ const fam = s.family;
118
+ const canonical = isCanonical(fam);
119
+ const ww = new Float64Array(n);
120
+ const dww = new Float64Array(n);
121
+ const observed = new Float64Array(n);
122
+ const score = new Float64Array(n);
123
+ for (let i = 0; i < n; i++) {
124
+ const wi = s.weights[i];
125
+ if (wi === 0)
126
+ continue;
127
+ const g = gradientWeights(fam, wi, s.y[i], eta[i], mu[i], canonical);
128
+ ww[i] = g.ww;
129
+ dww[i] = g.dww;
130
+ score[i] = g.score;
131
+ observed[i] = g.observed;
132
+ }
133
+ return { ww, dww, observed, score };
134
+ }
135
+ /**
136
+ * h_i = m_i' A⁻¹ m_i for every row (m_i = Λ_θ' z_i, the i-th row of ZΛ_θ), from the selected inverse `inv` of A (the
137
+ * layout of L): the groups of a row are adjacent in A, so the blocks of A⁻¹ needed are on the pattern of L
138
+ */
139
+ export function rowLeverages(s, sys, t, inv, rows) {
140
+ const n = s.y.length;
141
+ const nTerms = s.terms.length;
142
+ const nPairs = (nTerms * (nTerms + 1)) / 2;
143
+ const { colStart, blockPatternOffset, rowBlocks } = sys;
144
+ const h = new Float64Array(n);
145
+ const m = s.terms.map((term) => new Float64Array(term.columns.length));
146
+ const first = new Int32Array(nTerms);
147
+ for (let i = 0; i < n; i++) {
148
+ if (rows[i] === 0)
149
+ continue;
150
+ for (let k = 0; k < nTerms; k++) {
151
+ const term = s.terms[k];
152
+ const nc = term.columns.length;
153
+ const zi = term.matrix[i];
154
+ const tk = t[k];
155
+ const mk = m[k];
156
+ for (let c = 0; c < nc; c++) {
157
+ let sum = 0;
158
+ for (let r = c; r < nc; r++)
159
+ sum += tk[r + c * nc] * zi[r];
160
+ mk[c] = sum;
161
+ }
162
+ first[k] = term.zOffset + s.groups[term.group].codes[i] * nc;
163
+ }
164
+ let sum = 0;
165
+ for (let k = 0; k < nTerms; k++) {
166
+ const nck = m[k].length;
167
+ const mk = m[k];
168
+ // the diagonal block (lower triangle stored)
169
+ for (let cc = 0; cc < nck; cc++) {
170
+ const col = colStart[first[k] + cc];
171
+ sum += mk[cc] * mk[cc] * inv[col];
172
+ for (let rr = cc + 1; rr < nck; rr++)
173
+ sum += 2 * mk[rr] * mk[cc] * inv[col + rr - cc];
174
+ }
175
+ for (let l = 0; l < k; l++) {
176
+ const ml = m[l];
177
+ const ncl = ml.length;
178
+ const b = rowBlocks[i * nPairs + (k * (k + 1)) / 2 + l];
179
+ for (let cc = 0; cc < ncl; cc++) {
180
+ const at = colStart[first[l] + cc] + (ncl - cc) + blockPatternOffset[b];
181
+ for (let rr = 0; rr < nck; rr++)
182
+ sum += 2 * mk[rr] * ml[cc] * inv[at + rr];
183
+ }
184
+ }
185
+ }
186
+ h[i] = sum;
187
+ }
188
+ return h;
189
+ }
190
+ /**
191
+ * The gradient of the Laplace deviance (see the header): with respect to (θ, β) for nAGQ = 1, θ for nAGQ = 0. Throws
192
+ * the Error of {@link factorise} if H is not positive definite (a non-canonical link far from a minimum of pdev).
193
+ */
194
+ export function laplaceGradient(state) {
195
+ const { structure: s, sys, t, l, u, eta, mu, devResid, joint } = state;
196
+ const { n, p, q } = sys;
197
+ const fam = s.family;
198
+ const x = s.x;
199
+ const { ww, dww, observed, score } = rowWeights(s, eta, mu);
200
+ const dAic = aicDerivative(fam, s.y, mu, s.weights, s.size, devResid);
201
+ const inv = selectedInverse(sys, l);
202
+ const h = rowLeverages(s, sys, t, inv, dww);
203
+ const c = new Float64Array(n);
204
+ for (let i = 0; i < n; i++)
205
+ if (s.weights[i] !== 0)
206
+ c[i] = dww[i] * h[i] + dAic[i] * fam.muEta(eta[i]);
207
+ // the trace term: lmer's, with Z'WZ
208
+ const nTheta = s.theta.length;
209
+ const gradient = new Array(nTheta + (joint ? 0 : p)).fill(0);
210
+ const sums = levelTraceSums(sys, inv, t, weightedCrossProduct(sys, s, ww));
211
+ sys.termTheta.forEach((idx, k) => {
212
+ const nc = sys.termNc[k];
213
+ for (let cl = 0; cl < nc; cl++)
214
+ for (let rl = cl; rl < nc; rl++)
215
+ gradient[idx[rl + cl * nc]] += 2 * sums[k][cl + rl * nc];
216
+ });
217
+ // the adjoint system H v = Λ'Z'c + 2u (with X'c for nAGQ = 0)
218
+ const lh = isCanonical(fam) ? l : factorise(sys, blocksOfA(sys, t, weightedCrossProduct(sys, s, observed)));
219
+ const v = lambdaTransposeTimes(sys, t, zTransposeTimes(sys, c));
220
+ for (let j = 0; j < q; j++)
221
+ v[j] += 2 * u[j];
222
+ let xi;
223
+ if (joint) {
224
+ const vb = new Float64Array(p);
225
+ for (let i = 0; i < n; i++)
226
+ for (let k = 0; k < p; k++)
227
+ vb[k] += x[i][k] * c[i];
228
+ solveBordered(s, sys, t, lh, observed, v, vb);
229
+ xi = zTimes(sys, lambdaTimes(sys, t, v));
230
+ for (let i = 0; i < n; i++) {
231
+ for (let k = 0; k < p; k++)
232
+ xi[i] += x[i][k] * vb[k];
233
+ }
234
+ }
235
+ else {
236
+ solveL(sys, lh, v);
237
+ solveLt(sys, lh, v);
238
+ xi = zTimes(sys, lambdaTimes(sys, t, v));
239
+ }
240
+ const r = new Float64Array(n);
241
+ for (let i = 0; i < n; i++)
242
+ r[i] = c[i] - observed[i] * xi[i];
243
+ const zr = zTransposeTimes(sys, r);
244
+ const zs = zTransposeTimes(sys, score);
245
+ for (const { row, col, theta } of s.lambda)
246
+ gradient[theta] += zr[row] * u[col] + zs[row] * v[col];
247
+ if (!joint) {
248
+ for (let i = 0; i < n; i++) {
249
+ const ri = r[i];
250
+ if (ri === 0)
251
+ continue;
252
+ const xr = x[i];
253
+ for (let k = 0; k < p; k++)
254
+ gradient[nTheta + k] += xr[k] * ri;
255
+ }
256
+ }
257
+ return gradient;
258
+ }
259
+ /**
260
+ * Solves [Λ'Z'ŴZΛ + I, Λ'Z'ŴX; X'ŴZΛ, X'ŴX] (v, vb) = (v, vb) in place, `lh` the factor of the upper left block (the
261
+ * Schur complement X'ŴX − C'C, C = L_H⁻¹ Λ'Z'ŴX, by Cholesky)
262
+ */
263
+ function solveBordered(s, sys, t, lh, weights, v, vb) {
264
+ const { n, p, q } = sys;
265
+ const x = s.x;
266
+ const cm = new Float64Array(q * p);
267
+ for (let k = 0; k < p; k++) {
268
+ const col = lambdaTransposeTimes(sys, t, zTransposeTimes(sys, Float64Array.from(x, (row, i) => weights[i] * row[k])));
269
+ solveL(sys, lh, col);
270
+ cm.set(col, k * q);
271
+ }
272
+ const sm = new Float64Array(p * p);
273
+ for (let k = 0; k < p; k++) {
274
+ for (let m = k; m < p; m++) {
275
+ let sum = 0;
276
+ for (let i = 0; i < n; i++)
277
+ sum += weights[i] * x[i][k] * x[i][m];
278
+ for (let i = 0; i < q; i++)
279
+ sum -= cm[i + k * q] * cm[i + m * q];
280
+ sm[k + m * p] = sum;
281
+ sm[m + k * p] = sum;
282
+ }
283
+ }
284
+ if (p > 0 && potf2({ nrow: p, ncol: p, data: sm }) > 0)
285
+ throw new Error("the fixed-effects matrix of the penalised least squares is not positive definite");
286
+ solveL(sys, lh, v);
287
+ for (let k = 0; k < p; k++) {
288
+ let sum = vb[k];
289
+ for (let i = 0; i < q; i++)
290
+ sum -= cm[i + k * q] * v[i];
291
+ vb[k] = sum;
292
+ }
293
+ forwardSolve(sm, p, vb);
294
+ backSolve(sm, p, vb);
295
+ for (let k = 0; k < p; k++) {
296
+ const bk = vb[k];
297
+ for (let i = 0; i < q; i++)
298
+ v[i] -= cm[i + k * q] * bk;
299
+ }
300
+ solveLt(sys, lh, v);
301
+ }
@@ -0,0 +1,45 @@
1
+ import type { GlmFamilyName, GlmFamilySpec, GlmLink } from "./glmFamily.js";
2
+ import { type GlmerFamily } from "./glmerFamily.js";
3
+ import type { DataFrame } from "./modelFrame.js";
4
+ import { type MixedModelStructure, type MixedModelStructureOptions } from "./mixedStructure.js";
5
+ /** Options of {@link glmerStructure}. */
6
+ export interface GlmerStructureOptions extends MixedModelStructureOptions {
7
+ /** The family: a name or an object with the link, as glm's (`"binomial"`, `{ family: "poisson", link: "sqrt" }`). */
8
+ family?: GlmFamilyName | GlmFamilySpec;
9
+ /** The link (overrides the family object's). */
10
+ link?: GlmLink;
11
+ /** Prior weights: one per row of `data` (`null` or NaN is missing), or the name of a numeric column. */
12
+ weights?: readonly (number | null)[] | Float64Array | string;
13
+ /** Offset: one per row of `data` (`null` or NaN is missing), or the name of a numeric column. */
14
+ offset?: readonly (number | null)[] | Float64Array | string;
15
+ /** The number of points of the adaptive Gauss-Hermite quadrature (0 to 100; default 1, the Laplace approximation). */
16
+ nAGQ?: number;
17
+ }
18
+ /** The structure of a generalised linear mixed model (nothing fitted). */
19
+ export interface GlmerStructure extends MixedModelStructure {
20
+ /** The family with lme4's links and variance functions. */
21
+ family: GlmerFamily;
22
+ /** The response as the family reads it (binomial: the proportions; rows kept). */
23
+ y: number[];
24
+ /** The prior weights after the family has read the response (binomial `cbind(s, f)`: times s + f). */
25
+ weights: number[];
26
+ /** The offset (0 without), rows kept. */
27
+ offset: number[];
28
+ /** The binomial sizes (s + f for `cbind(s, f)`, 1 otherwise); undefined for the other families. */
29
+ size: number[] | undefined;
30
+ /** The family's starting means (Gamma: their mean, lme4's). */
31
+ mustart: number[];
32
+ /** nAGQ (truncated to an integer). */
33
+ nAGQ: number;
34
+ }
35
+ /**
36
+ * The structure of a generalised linear mixed model `response ~ fixed part + (lhs | group) + ...` with lme4's checks,
37
+ * errors and warnings (oracle/glmer-behaviour.md, stage 2a). The response may be `cbind(successes, failures)` (two
38
+ * column names, binomial only) and, for binomial, strings (a factor: the first level is failure). Internal: not
39
+ * exported from the package.
40
+ *
41
+ * @param formula A formula such as `"y ~ x + (1 | g)"` or `"cbind(s, f) ~ period + (1 | herd)"`.
42
+ * @param data The data frame: an object of columns.
43
+ * @param options The family and link, prior weights, offset, nAGQ, contrasts and level order of factors.
44
+ */
45
+ export declare function glmerStructure(formula: string, data: DataFrame, options?: GlmerStructureOptions): GlmerStructure;