@numero-uno-jpn/rstats 0.11.0 → 0.12.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 (289) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/README.ja.md +44 -13
  3. package/README.md +43 -11
  4. package/dist/chunks/{chunk-VJ4BDBMU.js → chunk-3VMBBQLV.js} +2 -2
  5. package/dist/chunks/chunk-4OKAZJJG.js +3 -0
  6. package/dist/chunks/{chunk-EZAFN5CD.js → chunk-6BH65HGN.js} +1 -1
  7. package/dist/chunks/chunk-ATC4F4BX.js +2 -0
  8. package/dist/chunks/{chunk-JWBO5WVN.js → chunk-EDH53DXN.js} +2 -2
  9. package/dist/chunks/{chunk-Y5CNQ266.js → chunk-EZ2KD5NA.js} +2 -2
  10. package/dist/chunks/{chunk-Y5PYRAYP.js → chunk-GSPOO2G3.js} +2 -2
  11. package/dist/chunks/{chunk-F5EDJFEO.js → chunk-K7AZ65H7.js} +2 -2
  12. package/dist/chunks/{chunk-YSWGDOWK.js → chunk-LEIH4U5X.js} +2 -2
  13. package/dist/chunks/{chunk-EZWBRCJL.js → chunk-OX3SBG5X.js} +1 -1
  14. package/dist/chunks/chunk-SGZQDJRR.js +2 -0
  15. package/dist/chunks/{chunk-C5XTWMGB.js → chunk-UH2N3ZQW.js} +1 -1
  16. package/dist/chunks/chunk-UV2MZYDQ.js +2 -0
  17. package/dist/chunks/{chunk-TOGXTCZO.js → chunk-VQHEOBTM.js} +2 -2
  18. package/dist/chunks/chunk-WJQZZYOI.js +2 -0
  19. package/dist/chunks/chunk-WKEFHOFR.js +2 -0
  20. package/dist/chunks/{chunk-CGBNKHNZ.js → chunk-WXC25OPJ.js} +2 -2
  21. package/dist/chunks/{chunk-5WDJPHYY.js → chunk-XJQKSR52.js} +1 -1
  22. package/dist/data/grouping.d.ts +39 -15
  23. package/dist/data/grouping.js +21 -22
  24. package/dist/descriptive/basic.d.ts +130 -17
  25. package/dist/descriptive/basic.js +115 -14
  26. package/dist/descriptive/cov.d.ts +22 -12
  27. package/dist/descriptive/dataFrames.d.ts +39 -11
  28. package/dist/descriptive/dataFrames.js +14 -6
  29. package/dist/descriptive/factanal.d.ts +26 -6
  30. package/dist/descriptive/factanal.js +7 -3
  31. package/dist/descriptive/hist.d.ts +27 -6
  32. package/dist/descriptive/hist.js +9 -3
  33. package/dist/descriptive/missing.d.ts +13 -6
  34. package/dist/descriptive/missing.js +4 -2
  35. package/dist/descriptive/moments.d.ts +91 -13
  36. package/dist/descriptive/moments.js +66 -9
  37. package/dist/descriptive/omega.d.ts +28 -6
  38. package/dist/descriptive/omega.js +16 -4
  39. package/dist/descriptive/pivot.d.ts +22 -6
  40. package/dist/descriptive/pivot.js +7 -3
  41. package/dist/descriptive/quantile.d.ts +33 -5
  42. package/dist/descriptive/quantile.js +20 -2
  43. package/dist/descriptive/rank.d.ts +12 -2
  44. package/dist/descriptive/rank.js +2 -0
  45. package/dist/descriptive/table.d.ts +55 -7
  46. package/dist/descriptive/table.js +38 -4
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.d.ts +40 -5
  49. package/dist/distributions/arcsine.js +35 -4
  50. package/dist/distributions/beta.d.ts +12 -0
  51. package/dist/distributions/beta.js +12 -0
  52. package/dist/distributions/binomial.d.ts +18 -0
  53. package/dist/distributions/binomial.js +18 -0
  54. package/dist/distributions/bvnorm.d.ts +4 -1
  55. package/dist/distributions/bvnorm.js +4 -1
  56. package/dist/distributions/cauchy.d.ts +24 -0
  57. package/dist/distributions/cauchy.js +24 -0
  58. package/dist/distributions/chisq.d.ts +18 -0
  59. package/dist/distributions/chisq.js +18 -0
  60. package/dist/distributions/exponential.d.ts +29 -1
  61. package/dist/distributions/exponential.js +24 -0
  62. package/dist/distributions/f.d.ts +18 -0
  63. package/dist/distributions/f.js +18 -0
  64. package/dist/distributions/gamma.d.ts +20 -0
  65. package/dist/distributions/gamma.js +18 -0
  66. package/dist/distributions/geometric.d.ts +24 -0
  67. package/dist/distributions/geometric.js +24 -0
  68. package/dist/distributions/hypergeometric.d.ts +24 -0
  69. package/dist/distributions/hypergeometric.js +24 -0
  70. package/dist/distributions/invgamma.d.ts +32 -4
  71. package/dist/distributions/invgamma.js +27 -3
  72. package/dist/distributions/kumaraswamy.d.ts +31 -3
  73. package/dist/distributions/kumaraswamy.js +31 -3
  74. package/dist/distributions/laplace.d.ts +36 -4
  75. package/dist/distributions/laplace.js +31 -3
  76. package/dist/distributions/logistic.d.ts +29 -1
  77. package/dist/distributions/logistic.js +24 -0
  78. package/dist/distributions/lognormal.d.ts +29 -1
  79. package/dist/distributions/lognormal.js +24 -0
  80. package/dist/distributions/multinomial.d.ts +7 -1
  81. package/dist/distributions/multinomial.js +2 -0
  82. package/dist/distributions/negbinomial.d.ts +29 -1
  83. package/dist/distributions/negbinomial.js +24 -0
  84. package/dist/distributions/normal.d.ts +11 -1
  85. package/dist/distributions/normal.js +6 -0
  86. package/dist/distributions/pareto.d.ts +36 -4
  87. package/dist/distributions/pareto.js +31 -3
  88. package/dist/distributions/poisson.d.ts +18 -0
  89. package/dist/distributions/poisson.js +18 -0
  90. package/dist/distributions/signrank.d.ts +24 -0
  91. package/dist/distributions/signrank.js +24 -0
  92. package/dist/distributions/summary.d.ts +20 -2
  93. package/dist/distributions/summary.js +4 -0
  94. package/dist/distributions/t.d.ts +18 -0
  95. package/dist/distributions/t.js +18 -0
  96. package/dist/distributions/triangular.d.ts +36 -4
  97. package/dist/distributions/triangular.js +31 -3
  98. package/dist/distributions/tukey.d.ts +15 -1
  99. package/dist/distributions/tukey.js +10 -0
  100. package/dist/distributions/uniform.d.ts +29 -1
  101. package/dist/distributions/uniform.js +24 -0
  102. package/dist/distributions/weibull.d.ts +29 -1
  103. package/dist/distributions/weibull.js +24 -0
  104. package/dist/distributions/wilcox.d.ts +24 -0
  105. package/dist/distributions/wilcox.js +24 -0
  106. package/dist/distributions.js +2 -2
  107. package/dist/entries/descriptive.d.ts +1 -1
  108. package/dist/entries/mixed.d.ts +1 -0
  109. package/dist/entries/mixed.js +2 -0
  110. package/dist/entries/numerics.d.ts +1 -1
  111. package/dist/entries/tests.js +1 -1
  112. package/dist/htest/agreement.d.ts +61 -23
  113. package/dist/htest/agreement.js +19 -9
  114. package/dist/htest/ansariTest.d.ts +8 -2
  115. package/dist/htest/aov.d.ts +43 -10
  116. package/dist/htest/aov.js +17 -3
  117. package/dist/htest/binomTest.d.ts +12 -2
  118. package/dist/htest/binomTest.js +2 -0
  119. package/dist/htest/brunnerMunzelTest.d.ts +9 -3
  120. package/dist/htest/brunnerMunzelTest.js +4 -2
  121. package/dist/htest/chisqTest.d.ts +19 -3
  122. package/dist/htest/cor.d.ts +20 -4
  123. package/dist/htest/effectSize.d.ts +70 -12
  124. package/dist/htest/effectSize.js +40 -7
  125. package/dist/htest/fisherTest.d.ts +9 -1
  126. package/dist/htest/fisherTest.js +2 -0
  127. package/dist/htest/groups.d.ts +5 -1
  128. package/dist/htest/ksTest.d.ts +20 -3
  129. package/dist/htest/leveneTest.d.ts +15 -4
  130. package/dist/htest/leveneTest.js +5 -2
  131. package/dist/htest/mantelhaenTest.d.ts +25 -3
  132. package/dist/htest/mantelhaenTest.js +15 -1
  133. package/dist/htest/mcnemarTest.d.ts +14 -4
  134. package/dist/htest/mcnemarTest.js +4 -2
  135. package/dist/htest/multipleComparisons.d.ts +173 -29
  136. package/dist/htest/multipleComparisons.js +123 -19
  137. package/dist/htest/normalityTests.d.ts +56 -9
  138. package/dist/htest/normalityTests.js +40 -6
  139. package/dist/htest/onewayTest.d.ts +32 -3
  140. package/dist/htest/onewayTest.js +10 -0
  141. package/dist/htest/pAdjust.d.ts +9 -1
  142. package/dist/htest/pAdjust.js +2 -0
  143. package/dist/htest/pairwise.d.ts +50 -5
  144. package/dist/htest/pairwise.js +25 -0
  145. package/dist/htest/poissonTest.d.ts +12 -2
  146. package/dist/htest/poissonTest.js +2 -0
  147. package/dist/htest/polycor.d.ts +29 -10
  148. package/dist/htest/power.d.ts +60 -6
  149. package/dist/htest/power.js +6 -0
  150. package/dist/htest/propTest.d.ts +13 -2
  151. package/dist/htest/propTest.js +2 -0
  152. package/dist/htest/pwr.d.ts +286 -36
  153. package/dist/htest/pwr.js +116 -15
  154. package/dist/htest/rankTests.d.ts +80 -12
  155. package/dist/htest/rankTests.js +11 -2
  156. package/dist/htest/tTest.d.ts +13 -3
  157. package/dist/htest/testFormula.js +6 -3
  158. package/dist/htest/trendTests.d.ts +29 -8
  159. package/dist/htest/trendTests.js +8 -4
  160. package/dist/htest/types.d.ts +14 -2
  161. package/dist/htest/varTest.d.ts +25 -8
  162. package/dist/htest/wilcoxTest.d.ts +10 -2
  163. package/dist/internal/dpq.d.ts +4 -0
  164. package/dist/internal/noncentral.d.ts +2 -0
  165. package/dist/internal/vectorize.d.ts +4 -0
  166. package/dist/linalg/basic.d.ts +39 -5
  167. package/dist/linalg/basic.js +15 -0
  168. package/dist/linalg/chol.d.ts +26 -5
  169. package/dist/linalg/chol.js +7 -0
  170. package/dist/linalg/eigen.d.ts +16 -7
  171. package/dist/linalg/eigen.js +6 -5
  172. package/dist/linalg/expm.d.ts +15 -5
  173. package/dist/linalg/expm.js +5 -3
  174. package/dist/linalg/kappa.d.ts +21 -1
  175. package/dist/linalg/kappa.js +16 -0
  176. package/dist/linalg/lu.d.ts +30 -6
  177. package/dist/linalg/lu.js +15 -3
  178. package/dist/linalg/matrix.d.ts +4 -0
  179. package/dist/linalg/norm.d.ts +16 -1
  180. package/dist/linalg/norm.js +9 -0
  181. package/dist/linalg/ops.d.ts +156 -35
  182. package/dist/linalg/ops.js +96 -15
  183. package/dist/linalg/qr.d.ts +87 -12
  184. package/dist/linalg/qr.js +31 -0
  185. package/dist/linalg/schur.d.ts +8 -2
  186. package/dist/linalg/schur.js +3 -1
  187. package/dist/linalg/solve.d.ts +42 -6
  188. package/dist/linalg/solve.js +22 -0
  189. package/dist/linalg/svd.d.ts +12 -2
  190. package/dist/linalg/svd.js +2 -0
  191. package/dist/linalg.js +2 -2
  192. package/dist/mixed.js +3 -0
  193. package/dist/models/anovaSequential.d.ts +26 -4
  194. package/dist/models/anovaSequential.js +3 -1
  195. package/dist/models/aovEz.d.ts +29 -6
  196. package/dist/models/aovEz.js +5 -2
  197. package/dist/models/aovStrata.d.ts +13 -3
  198. package/dist/models/aovStrata.js +5 -1
  199. package/dist/models/carAnova.d.ts +9 -3
  200. package/dist/models/carAnova.js +4 -2
  201. package/dist/models/coxph.d.ts +33 -7
  202. package/dist/models/coxph.js +8 -3
  203. package/dist/models/formula.d.ts +26 -1
  204. package/dist/models/formula.js +125 -9
  205. package/dist/models/glm.d.ts +128 -21
  206. package/dist/models/glm.js +71 -14
  207. package/dist/models/glmAnova.d.ts +30 -5
  208. package/dist/models/glmAnova.js +15 -2
  209. package/dist/models/glmConfint.d.ts +16 -2
  210. package/dist/models/glmConfint.js +9 -1
  211. package/dist/models/glmFamily.d.ts +27 -5
  212. package/dist/models/lm.d.ts +77 -19
  213. package/dist/models/lm.js +22 -4
  214. package/dist/models/lmer.d.ts +162 -0
  215. package/dist/models/lmer.js +457 -0
  216. package/dist/models/mauchly.d.ts +20 -5
  217. package/dist/models/mauchly.js +7 -3
  218. package/dist/models/mixedDeviance.d.ts +44 -0
  219. package/dist/models/mixedDeviance.js +872 -0
  220. package/dist/models/mixedStructure.d.ts +82 -0
  221. package/dist/models/mixedStructure.js +309 -0
  222. package/dist/models/modelFrame.d.ts +17 -2
  223. package/dist/models/modelFrame.js +36 -21
  224. package/dist/models/modelMatrix.d.ts +33 -8
  225. package/dist/models/modelMatrix.js +21 -5
  226. package/dist/models/survival.d.ts +41 -9
  227. package/dist/models/survival.js +10 -4
  228. package/dist/numerics/deriv.d.ts +21 -6
  229. package/dist/numerics/deriv.js +16 -5
  230. package/dist/numerics/integrate.d.ts +37 -7
  231. package/dist/numerics/integrate.js +8 -2
  232. package/dist/numerics/interp.d.ts +63 -12
  233. package/dist/numerics/interp.js +30 -6
  234. package/dist/numerics/ode.d.ts +15 -5
  235. package/dist/numerics/ode.js +5 -3
  236. package/dist/numerics.js +2 -2
  237. package/dist/optim/optim.d.ts +32 -8
  238. package/dist/optim/optim.js +8 -4
  239. package/dist/random/beta.d.ts +8 -0
  240. package/dist/random/beta.js +8 -0
  241. package/dist/random/binomial.d.ts +16 -0
  242. package/dist/random/binomial.js +16 -0
  243. package/dist/random/continuous.d.ts +51 -0
  244. package/dist/random/continuous.js +51 -0
  245. package/dist/random/extra.d.ts +66 -6
  246. package/dist/random/extra.js +66 -6
  247. package/dist/random/gamma.d.ts +33 -0
  248. package/dist/random/gamma.js +33 -0
  249. package/dist/random/hyper.d.ts +8 -0
  250. package/dist/random/hyper.js +8 -0
  251. package/dist/random/poisson.d.ts +25 -0
  252. package/dist/random/poisson.js +25 -0
  253. package/dist/random/r2dtable.d.ts +2 -0
  254. package/dist/random/r2dtable.js +2 -0
  255. package/dist/random/rank.d.ts +16 -0
  256. package/dist/random/rank.js +16 -0
  257. package/dist/random/rng.d.ts +49 -6
  258. package/dist/random/rng.js +29 -2
  259. package/dist/random/sample.d.ts +10 -2
  260. package/dist/random/sample.js +5 -1
  261. package/dist/random/streams.d.ts +13 -0
  262. package/dist/random/streams.js +13 -0
  263. package/dist/rstats.js +14 -14
  264. package/dist/special/besselIK.d.ts +11 -3
  265. package/dist/special/besselIK.js +6 -2
  266. package/dist/special/besselJY.d.ts +6 -2
  267. package/dist/special/besselJY.js +6 -2
  268. package/dist/special/beta.d.ts +16 -2
  269. package/dist/special/beta.js +16 -2
  270. package/dist/special/choose.d.ts +13 -5
  271. package/dist/special/choose.js +13 -5
  272. package/dist/special/erf.d.ts +8 -3
  273. package/dist/special/erf.js +8 -3
  274. package/dist/special/extra.d.ts +30 -10
  275. package/dist/special/extra.js +25 -9
  276. package/dist/special/gamma.d.ts +18 -2
  277. package/dist/special/gamma.js +18 -2
  278. package/dist/special/log1p.d.ts +16 -7
  279. package/dist/special/log1p.js +16 -7
  280. package/dist/special/psigamma.d.ts +9 -3
  281. package/dist/special/psigamma.js +9 -3
  282. package/dist/special/rCompatible.d.ts +5 -1
  283. package/dist/special.js +2 -2
  284. package/dist/tests.js +12 -12
  285. package/llms.txt +13 -3
  286. package/package.json +5 -1
  287. package/dist/chunks/chunk-7GQMKH3Z.js +0 -2
  288. package/dist/chunks/chunk-AHHS6AC4.js +0 -3
  289. package/dist/chunks/chunk-ALM3VHFP.js +0 -2
@@ -3,11 +3,18 @@
3
3
  // Written for rStats.js (no ported code) from oracle/anova-behaviour.md ("Formulas" and "Coding"). The parser is an
4
4
  // operator-precedence parser with R's precedences (^, then unary - and +, then :, then %in%, then * and /, then binary
5
5
  // + and -). The expansion follows the rules listed there: + concatenates, : forms the unions of pairs, * is a + b + a:b,
6
- // ^n the unions of n terms, / and %in% nest, - removes terms (and in its right operand 0 and 1 swap meanings). The
6
+ // ^n the unions of n terms (n truncated towards zero), / and %in% nest, - removes terms (and in its right operand 0
7
+ // and 1 swap meanings); *, / and %in% with a left operand that expands to no terms give no terms. The
7
8
  // terms are then sorted stably by degree, and the variables of each term listed in order of first appearance in the
8
9
  // formula. `codes` gives R's "factors" attribute (1: contrasts, 2: indicators) from the rule on earlier terms.
10
+ //
11
+ // Random-effects terms `(lhs | group)` and `(lhs || group)` of linear mixed models (oracle/lmer-behaviour.md,
12
+ // "Formulas with random-effects terms") are parsed only when asked for: `|` and `||` bind more loosely than every
13
+ // other operator, `splitBarTerms` separates them from the fixed-effects part, and `deparseFormula` prints a node as
14
+ // R deparses it (for the terms' labels).
15
+ import { formatR } from "../htest/core/simulate.js";
9
16
  const invalid = (why) => new SyntaxError(`invalid formula: ${why}`);
10
- function tokenize(s) {
17
+ function tokenize(s, allowBars) {
11
18
  const out = [];
12
19
  let i = 0;
13
20
  while (i < s.length) {
@@ -33,6 +40,12 @@ function tokenize(s) {
33
40
  i += 4;
34
41
  continue;
35
42
  }
43
+ if (allowBars && c === "|") {
44
+ const text = s[i + 1] === "|" ? "||" : "|";
45
+ out.push({ kind: "op", text });
46
+ i += text.length;
47
+ continue;
48
+ }
36
49
  if ("~+-*/:^()".includes(c)) {
37
50
  out.push({ kind: "op", text: c });
38
51
  i++;
@@ -43,19 +56,20 @@ function tokenize(s) {
43
56
  out.push({ kind: "end" });
44
57
  return out;
45
58
  }
46
- /** Binding powers of the binary operators (higher binds tighter); ^ is right associative */
47
- const BINARY = { "+": 1, "-": 1, "*": 2, "/": 2, "%in%": 3, ":": 4, "^": 6 };
59
+ /** Binding powers of the binary operators (higher binds tighter); ^ is right associative; | and || bind loosest */
60
+ const BINARY = { "|": 0.5, "||": 0.5, "+": 1, "-": 1, "*": 2, "/": 2, "%in%": 3, ":": 4, "^": 6 };
48
61
  const UNARY = 5;
49
62
  /**
50
63
  * Parses a formula string. Throws a SyntaxError whose message starts with "invalid formula" if it cannot be parsed
51
64
  * or uses something that is not supported (function calls other than Error(), backquoted names, ...).
52
65
  *
53
66
  * @param allowError Accept `Error(...)` terms (aov only).
67
+ * @param allowBars Accept the random-effects terms `(lhs | group)` and `(lhs || group)` (mixed models only).
54
68
  */
55
- export function parseFormula(formula, allowError = false) {
69
+ export function parseFormula(formula, allowError = false, allowBars = false) {
56
70
  if (typeof formula !== "string")
57
71
  throw new TypeError("the formula must be a string");
58
- const toks = tokenize(formula);
72
+ const toks = tokenize(formula, allowBars);
59
73
  let pos = 0;
60
74
  const peek = () => toks[pos];
61
75
  const next = () => toks[pos++];
@@ -103,7 +117,7 @@ export function parseFormula(formula, allowError = false) {
103
117
  next();
104
118
  // ^ is right associative: its right operand may contain another ^
105
119
  const right = expr(t.text === "^" ? bp - 1 : bp);
106
- left = { type: "binary", op: t.text, left, right };
120
+ left = t.text === "|" || t.text === "||" ? { type: "bar", double: t.text === "||", lhs: left, group: right } : { type: "binary", op: t.text, left, right };
107
121
  }
108
122
  return left;
109
123
  }
@@ -162,6 +176,88 @@ export function splitErrorTerms(rhs) {
162
176
  errors.forEach(check);
163
177
  return { treatment, errors };
164
178
  }
179
+ /** The random-effects terms of a right-hand side (summands, possibly in parentheses) and the right-hand side without them. */
180
+ export function splitBarTerms(rhs) {
181
+ const bars = [];
182
+ const strip = (n) => {
183
+ switch (n.type) {
184
+ case "bar":
185
+ bars.push(n);
186
+ return undefined;
187
+ case "paren": {
188
+ const a = strip(n.arg);
189
+ return a === undefined ? undefined : { ...n, arg: a };
190
+ }
191
+ case "binary":
192
+ if (n.op === "+") {
193
+ const l = strip(n.left);
194
+ const r = strip(n.right);
195
+ if (l === undefined)
196
+ return r;
197
+ if (r === undefined)
198
+ return l;
199
+ return { ...n, left: l, right: r };
200
+ }
201
+ if (n.op === "-") {
202
+ // y ~ x + (1 | g) - 1: the random-effects term is a summand of the left operand
203
+ const l = strip(n.left);
204
+ if (l === undefined)
205
+ return { type: "unary", op: "-", arg: n.right };
206
+ return { ...n, left: l };
207
+ }
208
+ return n;
209
+ default:
210
+ return n;
211
+ }
212
+ };
213
+ const fixed = strip(rhs);
214
+ const check = (n) => {
215
+ if (n.type === "bar")
216
+ throw invalid("a random-effects term (lhs | group) must be a term of the right-hand side");
217
+ if (n.type === "binary") {
218
+ check(n.left);
219
+ check(n.right);
220
+ }
221
+ else if (n.type === "unary" || n.type === "paren" || n.type === "Error")
222
+ check(n.arg);
223
+ };
224
+ if (fixed !== undefined)
225
+ check(fixed);
226
+ for (const b of bars) {
227
+ check(b.lhs);
228
+ check(b.group);
229
+ }
230
+ return { fixed, bars };
231
+ }
232
+ /** Binary operators that R deparses with a space on each side */
233
+ const SPACED = new Set(["+", "-", "*", "%in%"]);
234
+ /**
235
+ * A formula node as R deparses it: `+`, `-`, `*` and `%in%` with a space on each side, `:`, `/` and `^` without,
236
+ * unary minus and plus without a space, parentheses as written.
237
+ */
238
+ export function deparseFormula(n) {
239
+ switch (n.type) {
240
+ case "var":
241
+ return n.name;
242
+ case "dot":
243
+ return ".";
244
+ case "num":
245
+ return formatR(n.value);
246
+ case "binary": {
247
+ const l = deparseFormula(n.left);
248
+ const r = deparseFormula(n.right);
249
+ return SPACED.has(n.op) ? `${l} ${n.op} ${r}` : `${l}${n.op}${r}`;
250
+ }
251
+ case "unary":
252
+ return `${n.op}${deparseFormula(n.arg)}`;
253
+ case "paren":
254
+ return `(${deparseFormula(n.arg)})`;
255
+ case "Error":
256
+ return `Error(${deparseFormula(n.arg)})`;
257
+ case "bar":
258
+ return `${deparseFormula(n.lhs)} ${n.double ? "||" : "|"} ${deparseFormula(n.group)}`;
259
+ }
260
+ }
165
261
  /** The variable names of a node, in order of first appearance (`.` expands to `dot`) */
166
262
  function collectVariables(n, dot, out) {
167
263
  if (n === undefined)
@@ -188,8 +284,21 @@ function collectVariables(n, dot, out) {
188
284
  case "paren":
189
285
  case "Error":
190
286
  collectVariables(n.arg, dot, out);
287
+ return;
288
+ case "bar":
289
+ collectVariables(n.lhs, dot, out);
290
+ collectVariables(n.group, dot, out);
191
291
  }
192
292
  }
293
+ /**
294
+ * The variable names of a right-hand side, in order of first appearance (`.` stands for `dot`), whatever its
295
+ * operators: R's variables of the model frame (aggregate's grouping variables).
296
+ */
297
+ export function formulaVariables(rhs, dot) {
298
+ const out = [];
299
+ collectVariables(rhs, dot, out);
300
+ return out;
301
+ }
193
302
  const keyOf = (t) => t.join(",");
194
303
  function dedupe(terms) {
195
304
  const seen = new Set();
@@ -228,6 +337,8 @@ export function expandRhs(rhs, index, dot) {
228
337
  return L(n.arg, negated);
229
338
  case "Error":
230
339
  throw invalid("Error() must be a term of the right-hand side");
340
+ case "bar":
341
+ throw invalid("a random-effects term (lhs | group) must be a term of the right-hand side");
231
342
  case "unary":
232
343
  if (n.op === "+")
233
344
  return L(n.arg, negated);
@@ -239,8 +350,9 @@ export function expandRhs(rhs, index, dot) {
239
350
  let e = n.right;
240
351
  while (e.type === "paren")
241
352
  e = e.arg;
242
- const p = e.type === "num" ? e.value : NaN;
243
- if (!Number.isInteger(p) || p < 2)
353
+ // n is truncated towards zero (R's asInteger: ^2.5 is ^2)
354
+ const p = e.type === "num" ? Math.trunc(e.value) : NaN;
355
+ if (!(p >= 2))
244
356
  throw new Error("invalid power in formula");
245
357
  let cur = a;
246
358
  for (let k = 1; k < p; k++) {
@@ -259,6 +371,10 @@ export function expandRhs(rhs, index, dot) {
259
371
  }
260
372
  const a = L(n.left, negated);
261
373
  const b = L(n.right, negated);
374
+ // R builds *, / and %in% by appending to the left operand's list: an empty left operand gives no terms (the
375
+ // right operand has still been expanded, for its effect on the intercept)
376
+ if (a.length === 0 && n.op !== "+")
377
+ return [];
262
378
  const cross = () => {
263
379
  const out = [];
264
380
  for (const s of a)
@@ -3,7 +3,11 @@ import { type QR } from "../linalg/qr.js";
3
3
  import type { Sample } from "../htest/types.js";
4
4
  import { Warnings, type Family, type GlmFamilyName, type GlmFamilySpec, type GlmLink } from "./glmFamily.js";
5
5
  export type { GlmFamilyName, GlmFamilySpec, GlmLink, GlmLinkName, QuasiVariance } from "./glmFamily.js";
6
- /** Options for {@link glm}. */
6
+ /**
7
+ * Options for {@link glm}.
8
+ *
9
+ * @category Generalized linear models
10
+ */
7
11
  export interface GlmOptions {
8
12
  /**
9
13
  * The family: a name (`"gaussian"`, the default, `"binomial"`, `"quasibinomial"`, `"poisson"`, `"quasipoisson"`,
@@ -26,7 +30,11 @@ export interface GlmOptions {
26
30
  /** Include an intercept (a column of ones before the columns of `x`). Default `true`. */
27
31
  intercept?: boolean;
28
32
  }
29
- /** The data and settings of a fit, kept for the refits of {@link glmConfint} and {@link glmAnova}. */
33
+ /**
34
+ * The data and settings of a fit, kept for the refits of {@link glmConfint} and {@link glmAnova}.
35
+ *
36
+ * @category Generalized linear models
37
+ */
30
38
  export interface GlmModel {
31
39
  /** Model matrix (with the column of ones), rows with a missing value dropped. */
32
40
  x: number[][];
@@ -36,31 +44,48 @@ export interface GlmModel {
36
44
  yGiven: number[] | number[][];
37
45
  /** Prior weights as given (1 when not given). */
38
46
  priorGiven: number[];
47
+ /** Offset, one per row (0 when not given). */
39
48
  offset: number[];
40
49
  /** Whether an offset was given. */
41
50
  hasOffset: boolean;
51
+ /** Whether the model has an intercept. */
42
52
  intercept: boolean;
53
+ /** The family and link of the fit. */
43
54
  familySpec: GlmFamilySpec;
55
+ /** Convergence tolerance (R's `glm.control(epsilon)`). */
44
56
  epsilon: number;
57
+ /** Maximum number of iterations (R's `glm.control(maxit)`). */
45
58
  maxit: number;
59
+ /** Starting coefficients, if given. */
46
60
  start: number[] | undefined;
47
61
  }
48
- /** Result of {@link glm}: R's `glm` object. */
62
+ /**
63
+ * Result of {@link glm}: R's `glm` object.
64
+ *
65
+ * @category Generalized linear models
66
+ */
49
67
  export interface GlmResult {
50
68
  /** Coefficients (intercept first); NaN for aliased columns. */
51
69
  coefficients: number[];
52
70
  /** Fitted means, one per row (rows with a missing value dropped). */
53
71
  fittedValues: number[];
72
+ /** Linear predictors, one per row, offset included (R's `linear.predictors`). */
54
73
  linearPredictors: number[];
74
+ /** Residual deviance. */
55
75
  deviance: number;
76
+ /** Deviance of the model with the intercept only, or of the empty model without intercept (R's `null.deviance`). */
56
77
  nullDeviance: number;
57
78
  /** AIC; NaN for the quasi families. */
58
79
  aic: number;
80
+ /** Numerical rank of the model matrix. */
59
81
  rank: number;
82
+ /** Residual degrees of freedom (R's `df.residual`). */
60
83
  dfResidual: number;
84
+ /** Degrees of freedom of the null deviance (R's `df.null`). */
61
85
  dfNull: number;
62
86
  /** Number of iterations. */
63
87
  iter: number;
88
+ /** Whether the iterations converged. */
64
89
  converged: boolean;
65
90
  /** Whether the step was truncated (halved) in the last iteration. */
66
91
  boundary: boolean;
@@ -74,11 +99,13 @@ export interface GlmResult {
74
99
  residuals: number[];
75
100
  /** The family's name and the link's name (`"mu^0.333"` for a power link). */
76
101
  family: GlmFamilyName;
102
+ /** The link's name, such as `"logit"`. */
77
103
  link: string;
78
104
  /** R's warnings, each once, in order of first appearance. */
79
105
  warnings: string[];
80
106
  /** QR decomposition of the last weighted least squares problem (undefined for a model without columns). */
81
107
  qr: QR | undefined;
108
+ /** The data and settings of the fit, for the refits of {@link glmConfint} and {@link glmAnova}. */
82
109
  model: GlmModel;
83
110
  }
84
111
  /** Input of the iteratively reweighted least squares (after the response has been read) */
@@ -137,7 +164,7 @@ export declare function refit(model: GlmModel, cols: number[], warnings: Warning
137
164
  };
138
165
  /**
139
166
  * Fit a generalized linear model by iteratively reweighted least squares. Equivalent to R's
140
- * `glm(y ~ x, family = ...)` (or `y ~ x - 1` without intercept); the predictors are given as a matrix instead of a
167
+ * `glm(y ~ x, family = ...)`. Or `y ~ x - 1` without intercept; the predictors are given as a matrix instead of a
141
168
  * formula. Rows with a missing value (NaN) in `x`, `y`, the weights or the offset are dropped. Every family and link
142
169
  * of R's `stats` is available; R's warnings are returned in `warnings`. See {@link glmSummary}, {@link glmResiduals},
143
170
  * {@link glmPredict}, {@link glmConfint} and {@link glmAnova} for the methods.
@@ -149,27 +176,41 @@ export declare function refit(model: GlmModel, cols: number[], warnings: Warning
149
176
  * @example
150
177
  * ```ts
151
178
  * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
152
- * fit.coefficients; // [intercept, slope]
153
- * glmSummary(fit).pValue;
179
+ * fit.coefficients; // [-4.2491, 1.214]
180
+ * glmSummary(fit).pValue; // [0.20976, 0.18341]
154
181
  * ```
182
+ *
183
+ * @category Generalized linear models
155
184
  */
156
185
  export declare function glm(x: MatrixInput | Sample, y: Sample | MatrixInput, options?: GlmOptions): GlmResult;
157
186
  /** The family object of a fit */
158
187
  export declare function familyOf(fit: GlmResult): Family;
159
188
  /** Pearson dispersion of summary.glm (1 for binomial and poisson) */
160
189
  export declare function glmDispersion(fit: GlmResult, warnings?: Warnings): number;
161
- /** Options for {@link glmSummary}. */
190
+ /**
191
+ * Options for {@link glmSummary}.
192
+ *
193
+ * @category Generalized linear models
194
+ */
162
195
  export interface GlmSummaryOptions {
163
196
  /** Dispersion to use instead of the estimate (or 1 for binomial and poisson). */
164
197
  dispersion?: number;
165
198
  }
166
- /** Result of {@link glmSummary}: the quantities of R's `summary(glm)`. */
199
+ /**
200
+ * Result of {@link glmSummary}: the quantities of R's `summary(glm)`.
201
+ *
202
+ * @category Generalized linear models
203
+ */
167
204
  export interface GlmSummary {
205
+ /** The dispersion used (estimated, given, or 1 for binomial and poisson). */
168
206
  dispersion: number;
169
207
  /** The coefficient table of the columns that are not aliased, in their order. */
170
208
  estimate: number[];
209
+ /** Standard errors (R's `Std. Error`). */
171
210
  stdError: number[];
211
+ /** z or t statistics (see `statisticName`). */
172
212
  statistic: number[];
213
+ /** Two-sided p-values (R's `Pr(>|z|)` or `Pr(>|t|)`). */
173
214
  pValue: number[];
174
215
  /** `"z value"` when the dispersion is fixed (binomial, poisson), else `"t value"`. */
175
216
  statisticName: "z value" | "t value";
@@ -177,35 +218,90 @@ export interface GlmSummary {
177
218
  covUnscaled: number[][];
178
219
  /** covUnscaled times the dispersion. */
179
220
  covScaled: number[][];
221
+ /** Residual degrees of freedom (R's `df.residual`). */
180
222
  dfResidual: number;
181
223
  /** Warnings (such as "observations with zero weight not used for calculating dispersion"). */
182
224
  warnings: string[];
183
225
  }
184
226
  /**
185
- * Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates.
186
- * Equivalent to R's `summary()` for a `glm` fit.
227
+ * Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates,
228
+ * for a `glm` fit. Equivalent to R's `summary()`.
229
+ *
230
+ * @example
231
+ * ```ts
232
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
233
+ * const s = glmSummary(fit);
234
+ * s.estimate; // [-4.2491, 1.214]
235
+ * s.stdError; // [3.3878, 0.91258]
236
+ * s.statisticName; // "z value"
237
+ * ```
238
+ *
239
+ * @category Generalized linear models
187
240
  */
188
241
  export declare function glmSummary(fit: GlmResult, options?: GlmSummaryOptions): GlmSummary;
189
- /** Kinds of residuals of {@link glmResiduals}. */
242
+ /**
243
+ * Kinds of residuals of {@link glmResiduals}.
244
+ *
245
+ * @category Generalized linear models
246
+ */
190
247
  export type GlmResidualType = "deviance" | "pearson" | "working" | "response";
191
248
  /**
192
- * Residuals of a generalized linear model, one per row. Equivalent to R's `residuals(fit, type)` for a `glm` fit
193
- * (default `"deviance"`).
249
+ * Residuals of a generalized linear model, one per row, for a `glm` fit (default `"deviance"`). Equivalent to R's
250
+ * `residuals(fit, type)`.
251
+ *
252
+ * @example
253
+ * ```ts
254
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
255
+ * glmResiduals(fit); // [-0.30644, -0.54776, 1.4436, -1.4436, 0.54776, 0.30644]
256
+ * glmResiduals(fit, "pearson"); // [-0.21925, -0.40231, 1.3546, -1.3546, 0.40231, 0.21925]
257
+ * ```
258
+ *
259
+ * @category Generalized linear models
194
260
  */
195
261
  export declare function glmResiduals(fit: GlmResult, type?: GlmResidualType): number[];
196
262
  /**
197
- * Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight.
198
- * Equivalent to R's `hatvalues()` for a `glm` fit.
263
+ * Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight, for a
264
+ * `glm` fit. Equivalent to R's `hatvalues()`.
265
+ *
266
+ * @example
267
+ * ```ts
268
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
269
+ * glmHatvalues(fit); // [0.28361, 0.37762, 0.33877, 0.33877, 0.37762, 0.28361]
270
+ * ```
271
+ *
272
+ * @category Generalized linear models
199
273
  */
200
274
  export declare function glmHatvalues(fit: GlmResult): number[];
201
275
  /**
202
- * Standardized deviance residuals, for the rows of non-zero prior weight. Equivalent to R's `rstandard()` for a
203
- * `glm` fit.
276
+ * Standardized deviance residuals, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's
277
+ * `rstandard()`.
278
+ *
279
+ * @example
280
+ * ```ts
281
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
282
+ * glmRstandard(fit); // [-0.36205, -0.69432, 1.7753, -1.7753, 0.69432, 0.36205]
283
+ * ```
284
+ *
285
+ * @category Generalized linear models
204
286
  */
205
287
  export declare function glmRstandard(fit: GlmResult, options?: GlmSummaryOptions): number[];
206
- /** Cook's distances, for the rows of non-zero prior weight. Equivalent to R's `cooks.distance()` for a `glm` fit. */
288
+ /**
289
+ * Cook's distances, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's `cooks.distance()`.
290
+ *
291
+ * @example
292
+ * ```ts
293
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
294
+ * glmCooksDistance(fit); // [0.013283, 0.078894, 0.71087, 0.71087, 0.078894, 0.013283]
295
+ * ```
296
+ *
297
+ * @category Generalized linear models
298
+ */
207
299
  export declare function glmCooksDistance(fit: GlmResult, options?: GlmSummaryOptions): number[];
208
- /** Options for {@link glmPredict}. */
300
+ /**
301
+ * Options for {@link glmPredict}.
302
+ *
303
+ * @category Generalized linear models
304
+ */
209
305
  export interface GlmPredictOptions {
210
306
  /** New predictor values, with the columns of `x` (no column of ones). Default: the data of the fit. */
211
307
  newdata?: MatrixInput | Sample;
@@ -217,10 +313,21 @@ export interface GlmPredictOptions {
217
313
  dispersion?: number;
218
314
  }
219
315
  /**
220
- * Predictions of a generalized linear model with their standard errors. Equivalent to R's
221
- * `predict(fit, newdata, type, se.fit = TRUE)` for a `glm` fit. Aliased columns are left out, as R does.
316
+ * Predictions of a generalized linear model with their standard errors, for a `glm` fit. Equivalent to R's
317
+ * `predict(fit, newdata, type, se.fit = TRUE)`. Aliased columns are left out, as R does.
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
322
+ * glmPredict(fit, { newdata: [7] }); // { fit: [4.2491], seFit: [3.3878] }
323
+ * glmPredict(fit, { newdata: [7], type: "response" }).fit; // [0.98592]
324
+ * ```
325
+ *
326
+ * @category Generalized linear models
222
327
  */
223
328
  export declare function glmPredict(fit: GlmResult, options?: GlmPredictOptions): {
329
+ /** Predictions on the scale of `type`, one per row. */
224
330
  fit: number[];
331
+ /** Their standard errors (R's `se.fit`). */
225
332
  seFit: number[];
226
333
  };
@@ -247,7 +247,7 @@ export function refit(model, cols, warnings, control = {}) {
247
247
  }
248
248
  /**
249
249
  * Fit a generalized linear model by iteratively reweighted least squares. Equivalent to R's
250
- * `glm(y ~ x, family = ...)` (or `y ~ x - 1` without intercept); the predictors are given as a matrix instead of a
250
+ * `glm(y ~ x, family = ...)`. Or `y ~ x - 1` without intercept; the predictors are given as a matrix instead of a
251
251
  * formula. Rows with a missing value (NaN) in `x`, `y`, the weights or the offset are dropped. Every family and link
252
252
  * of R's `stats` is available; R's warnings are returned in `warnings`. See {@link glmSummary}, {@link glmResiduals},
253
253
  * {@link glmPredict}, {@link glmConfint} and {@link glmAnova} for the methods.
@@ -259,9 +259,11 @@ export function refit(model, cols, warnings, control = {}) {
259
259
  * @example
260
260
  * ```ts
261
261
  * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
262
- * fit.coefficients; // [intercept, slope]
263
- * glmSummary(fit).pValue;
262
+ * fit.coefficients; // [-4.2491, 1.214]
263
+ * glmSummary(fit).pValue; // [0.20976, 0.18341]
264
264
  * ```
265
+ *
266
+ * @category Generalized linear models
265
267
  */
266
268
  export function glm(x, y, options = {}) {
267
269
  const intercept = options.intercept ?? true;
@@ -394,8 +396,19 @@ function unscaledCovariance(fit) {
394
396
  return chol2inv(Rr);
395
397
  }
396
398
  /**
397
- * Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates.
398
- * Equivalent to R's `summary()` for a `glm` fit.
399
+ * Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates,
400
+ * for a `glm` fit. Equivalent to R's `summary()`.
401
+ *
402
+ * @example
403
+ * ```ts
404
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
405
+ * const s = glmSummary(fit);
406
+ * s.estimate; // [-4.2491, 1.214]
407
+ * s.stdError; // [3.3878, 0.91258]
408
+ * s.statisticName; // "z value"
409
+ * ```
410
+ *
411
+ * @category Generalized linear models
399
412
  */
400
413
  export function glmSummary(fit, options = {}) {
401
414
  const warnings = new Warnings();
@@ -427,8 +440,17 @@ export function glmSummary(fit, options = {}) {
427
440
  };
428
441
  }
429
442
  /**
430
- * Residuals of a generalized linear model, one per row. Equivalent to R's `residuals(fit, type)` for a `glm` fit
431
- * (default `"deviance"`).
443
+ * Residuals of a generalized linear model, one per row, for a `glm` fit (default `"deviance"`). Equivalent to R's
444
+ * `residuals(fit, type)`.
445
+ *
446
+ * @example
447
+ * ```ts
448
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
449
+ * glmResiduals(fit); // [-0.30644, -0.54776, 1.4436, -1.4436, 0.54776, 0.30644]
450
+ * glmResiduals(fit, "pearson"); // [-0.21925, -0.40231, 1.3546, -1.3546, 0.40231, 0.21925]
451
+ * ```
452
+ *
453
+ * @category Generalized linear models
432
454
  */
433
455
  export function glmResiduals(fit, type = "deviance") {
434
456
  const fam = familyOf(fit);
@@ -449,8 +471,16 @@ export function glmResiduals(fit, type = "deviance") {
449
471
  }
450
472
  }
451
473
  /**
452
- * Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight.
453
- * Equivalent to R's `hatvalues()` for a `glm` fit.
474
+ * Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight, for a
475
+ * `glm` fit. Equivalent to R's `hatvalues()`.
476
+ *
477
+ * @example
478
+ * ```ts
479
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
480
+ * glmHatvalues(fit); // [0.28361, 0.37762, 0.33877, 0.33877, 0.37762, 0.28361]
481
+ * ```
482
+ *
483
+ * @category Generalized linear models
454
484
  */
455
485
  export function glmHatvalues(fit) {
456
486
  const out = [];
@@ -478,8 +508,16 @@ function median(v) {
478
508
  return s.length % 2 === 1 ? s[m] : (s[m - 1] + s[m]) / 2;
479
509
  }
480
510
  /**
481
- * Standardized deviance residuals, for the rows of non-zero prior weight. Equivalent to R's `rstandard()` for a
482
- * `glm` fit.
511
+ * Standardized deviance residuals, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's
512
+ * `rstandard()`.
513
+ *
514
+ * @example
515
+ * ```ts
516
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
517
+ * glmRstandard(fit); // [-0.36205, -0.69432, 1.7753, -1.7753, 0.69432, 0.36205]
518
+ * ```
519
+ *
520
+ * @category Generalized linear models
483
521
  */
484
522
  export function glmRstandard(fit, options = {}) {
485
523
  const h = glmHatvalues(fit);
@@ -491,7 +529,17 @@ export function glmRstandard(fit, options = {}) {
491
529
  return Number.isFinite(s) ? s : NaN;
492
530
  });
493
531
  }
494
- /** Cook's distances, for the rows of non-zero prior weight. Equivalent to R's `cooks.distance()` for a `glm` fit. */
532
+ /**
533
+ * Cook's distances, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's `cooks.distance()`.
534
+ *
535
+ * @example
536
+ * ```ts
537
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
538
+ * glmCooksDistance(fit); // [0.013283, 0.078894, 0.71087, 0.71087, 0.078894, 0.013283]
539
+ * ```
540
+ *
541
+ * @category Generalized linear models
542
+ */
495
543
  export function glmCooksDistance(fit, options = {}) {
496
544
  const h = glmHatvalues(fit);
497
545
  const disp = options.dispersion ?? glmDispersion(fit);
@@ -502,8 +550,17 @@ export function glmCooksDistance(fit, options = {}) {
502
550
  });
503
551
  }
504
552
  /**
505
- * Predictions of a generalized linear model with their standard errors. Equivalent to R's
506
- * `predict(fit, newdata, type, se.fit = TRUE)` for a `glm` fit. Aliased columns are left out, as R does.
553
+ * Predictions of a generalized linear model with their standard errors, for a `glm` fit. Equivalent to R's
554
+ * `predict(fit, newdata, type, se.fit = TRUE)`. Aliased columns are left out, as R does.
555
+ *
556
+ * @example
557
+ * ```ts
558
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
559
+ * glmPredict(fit, { newdata: [7] }); // { fit: [4.2491], seFit: [3.3878] }
560
+ * glmPredict(fit, { newdata: [7], type: "response" }).fit; // [0.98592]
561
+ * ```
562
+ *
563
+ * @category Generalized linear models
507
564
  */
508
565
  export function glmPredict(fit, options = {}) {
509
566
  const fam = familyOf(fit);
@@ -1,7 +1,15 @@
1
1
  import { type GlmResult } from "./glm.js";
2
- /** Tests of {@link glmAnova}. */
2
+ /**
3
+ * Tests of {@link glmAnova}.
4
+ *
5
+ * @category Generalized linear models
6
+ */
3
7
  export type GlmAnovaTest = "Chisq" | "LRT" | "Rao" | "F" | "Cp";
4
- /** Options for {@link glmAnova}. */
8
+ /**
9
+ * Options for {@link glmAnova}.
10
+ *
11
+ * @category Generalized linear models
12
+ */
5
13
  export interface GlmAnovaOptions {
6
14
  /**
7
15
  * The test: `"Chisq"` (or its synonym `"LRT"`), `"Rao"`, `"F"`, `"Cp"`, or `false` for none. Default, as R since
@@ -11,7 +19,11 @@ export interface GlmAnovaOptions {
11
19
  /** Dispersion for the tests instead of the estimate of the largest model. */
12
20
  dispersion?: number;
13
21
  }
14
- /** Result of {@link glmAnova}: R's anova table. */
22
+ /**
23
+ * Result of {@link glmAnova}: R's anova table.
24
+ *
25
+ * @category Generalized linear models
26
+ */
15
27
  export interface GlmAnovaResult {
16
28
  /** Column names, such as `"Df"`, `"Deviance"`, `"Resid. Df"`, `"Resid. Dev"`, `"Pr(>Chi)"`. */
17
29
  colNames: string[];
@@ -23,9 +35,22 @@ export interface GlmAnovaResult {
23
35
  warnings: string[];
24
36
  }
25
37
  /**
26
- * Analysis of deviance. Equivalent to R's `anova()` for one `glm` fit (the sequential table, each column of `x`
27
- * added in order) or for several fits of the same data (`anova(fit1, fit2, ...)`).
38
+ * Analysis of deviance, for one `glm` fit (the sequential table, each column of `x` added in order) or for several
39
+ * fits of the same data (`anova(fit1, fit2, ...)`). Equivalent to R's `anova()`.
28
40
  *
29
41
  * @param fits One fit, or an array of fits to compare.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const y = [0, 0, 1, 0, 1, 1];
46
+ * const fit0 = glm([[], [], [], [], [], []], y, { family: "binomial" });
47
+ * const fit1 = glm([1, 2, 3, 4, 5, 6], y, { family: "binomial" });
48
+ * const a = glmAnova(fit1);
49
+ * a.colNames; // ["Df", "Deviance", "Resid. Df", "Resid. Dev", "Pr(>Chi)"]
50
+ * a.table[1]; // [1, 3.3618, 4, 4.956, 0.066725]
51
+ * glmAnova([fit0, fit1], { test: "LRT" }).table[1]; // [4, 4.956, 1, 3.3618, 0.066725]
52
+ * ```
53
+ *
54
+ * @category Generalized linear models
30
55
  */
31
56
  export declare function glmAnova(fits: GlmResult | readonly GlmResult[], options?: GlmAnovaOptions): GlmAnovaResult;