@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
@@ -21,10 +21,23 @@ function rowOf(model, cols, warnings) {
21
21
  /** Score statistic of the smaller model's fit against the larger model's matrix */
22
22
  const rao = (small, large) => scoreStatistic(large.x, small.fit.workingResiduals, small.fit.workingWeights, large.intercept);
23
23
  /**
24
- * Analysis of deviance. Equivalent to R's `anova()` for one `glm` fit (the sequential table, each column of `x`
25
- * added in order) or for several fits of the same data (`anova(fit1, fit2, ...)`).
24
+ * Analysis of deviance, for one `glm` fit (the sequential table, each column of `x` added in order) or for several
25
+ * fits of the same data (`anova(fit1, fit2, ...)`). Equivalent to R's `anova()`.
26
26
  *
27
27
  * @param fits One fit, or an array of fits to compare.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const y = [0, 0, 1, 0, 1, 1];
32
+ * const fit0 = glm([[], [], [], [], [], []], y, { family: "binomial" });
33
+ * const fit1 = glm([1, 2, 3, 4, 5, 6], y, { family: "binomial" });
34
+ * const a = glmAnova(fit1);
35
+ * a.colNames; // ["Df", "Deviance", "Resid. Df", "Resid. Dev", "Pr(>Chi)"]
36
+ * a.table[1]; // [1, 3.3618, 4, 4.956, 0.066725]
37
+ * glmAnova([fit0, fit1], { test: "LRT" }).table[1]; // [4, 4.956, 1, 3.3618, 0.066725]
38
+ * ```
39
+ *
40
+ * @category Generalized linear models
28
41
  */
29
42
  export function glmAnova(fits, options = {}) {
30
43
  const list = Array.isArray(fits) ? fits : [fits];
@@ -1,5 +1,9 @@
1
1
  import { type GlmResult } from "./glm.js";
2
- /** Options for {@link glmConfint}. */
2
+ /**
3
+ * Options for {@link glmConfint}.
4
+ *
5
+ * @category Generalized linear models
6
+ */
3
7
  export interface GlmConfintOptions {
4
8
  /** Indices of the coefficients (0-based, intercept first). Default: all. */
5
9
  parm?: readonly number[];
@@ -15,11 +19,21 @@ export interface GlmConfintOptions {
15
19
  export declare function scoreStatistic(x: number[][], r: number[], v: number[], intercept: boolean, tol?: number): number;
16
20
  /**
17
21
  * Profile likelihood confidence intervals for the coefficients of a generalized linear model (NaN for aliased
18
- * columns, and for a limit that the profile does not reach). Equivalent to R's `confint()` for a `glm` fit, but with
22
+ * columns, and for a limit that the profile does not reach), for a `glm` fit. Equivalent to R's `confint()`. With
19
23
  * the exact solutions of the profile equations, where R interpolates a spline through a grid of profile points (and
20
24
  * is up to about 5e-4 standard errors from them).
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
29
+ * glmConfint(fit); // { lower: [-15.124, -0.065819], upper: [0.57792, 4.1592] }
30
+ * ```
31
+ *
32
+ * @category Generalized linear models
21
33
  */
22
34
  export declare function glmConfint(fit: GlmResult, options?: GlmConfintOptions): {
35
+ /** Lower limits, one per coefficient (R's first column, `2.5 %` by default). */
23
36
  lower: number[];
37
+ /** Upper limits, one per coefficient (R's second column, `97.5 %` by default). */
24
38
  upper: number[];
25
39
  };
@@ -52,9 +52,17 @@ export function scoreStatistic(x, r, v, intercept, tol = 1e-11) {
52
52
  }
53
53
  /**
54
54
  * Profile likelihood confidence intervals for the coefficients of a generalized linear model (NaN for aliased
55
- * columns, and for a limit that the profile does not reach). Equivalent to R's `confint()` for a `glm` fit, but with
55
+ * columns, and for a limit that the profile does not reach), for a `glm` fit. Equivalent to R's `confint()`. With
56
56
  * the exact solutions of the profile equations, where R interpolates a spline through a grid of profile points (and
57
57
  * is up to about 5e-4 standard errors from them).
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
62
+ * glmConfint(fit); // { lower: [-15.124, -0.065819], upper: [0.57792, 4.1592] }
63
+ * ```
64
+ *
65
+ * @category Generalized linear models
58
66
  */
59
67
  export function glmConfint(fit, options = {}) {
60
68
  const level = options.level ?? 0.95;
@@ -1,15 +1,37 @@
1
- /** Names of the families of R's stats. */
1
+ /**
2
+ * Names of the families of R's stats.
3
+ *
4
+ * @category Generalized linear models
5
+ */
2
6
  export type GlmFamilyName = "gaussian" | "binomial" | "quasibinomial" | "poisson" | "quasipoisson" | "Gamma" | "inverse.gaussian" | "quasi";
3
- /** Names of the links of R's stats (`make.link`). */
7
+ /**
8
+ * Names of the links of R's stats (`make.link`).
9
+ *
10
+ * @category Generalized linear models
11
+ */
4
12
  export type GlmLinkName = "identity" | "log" | "inverse" | "logit" | "probit" | "cauchit" | "cloglog" | "sqrt" | "1/mu^2";
5
- /** A link: one of R's named links or a power link `mu^power` (R's `power(lambda)`; `power <= 0` is the log link). */
13
+ /**
14
+ * A link: one of R's named links or a power link `mu^power` (R's `power(lambda)`; `power <= 0` is the log link).
15
+ *
16
+ * @category Generalized linear models
17
+ */
6
18
  export type GlmLink = GlmLinkName | {
19
+ /** The exponent lambda of the power link (R's `power(lambda)`). */
7
20
  power: number;
8
21
  };
9
- /** Variance functions of the `quasi` family. */
22
+ /**
23
+ * Variance functions of the `quasi` family.
24
+ *
25
+ * @category Generalized linear models
26
+ */
10
27
  export type QuasiVariance = "constant" | "mu(1-mu)" | "mu" | "mu^2" | "mu^3";
11
- /** A family with its link (and, for `quasi`, its variance function), as R's family objects such as `quasi(link = "log", variance = "mu^2")`. */
28
+ /**
29
+ * A family with its link (and, for `quasi`, its variance function), as R's family objects such as `quasi(link = "log", variance = "mu^2")`.
30
+ *
31
+ * @category Generalized linear models
32
+ */
12
33
  export interface GlmFamilySpec {
34
+ /** The family's name, such as `"binomial"`. */
13
35
  family: GlmFamilyName;
14
36
  /** The link. Default: the family's default (the first of its allowed links). */
15
37
  link?: GlmLink;
@@ -3,19 +3,28 @@ import { type QR } from "../linalg/qr.js";
3
3
  import type { Sample } from "../htest/types.js";
4
4
  import type { DataFrame } from "./modelFrame.js";
5
5
  import { type Design, type ModelMatrixOptions } from "./modelMatrix.js";
6
- /** Options for {@link lm}. */
6
+ /**
7
+ * Options for {@link lm}.
8
+ *
9
+ * @category Linear models and ANOVA
10
+ */
7
11
  export interface LmOptions {
8
12
  /** Include an intercept (a column of ones before the columns of `x`). Default `true`. */
9
13
  intercept?: boolean;
10
14
  /** Weights for weighted least squares (non-negative; rows of weight 0 are left out of the fit). */
11
15
  weights?: Sample;
12
16
  }
13
- /** Result of {@link lm}: the fit and the quantities of R's `summary(lm)`. */
17
+ /**
18
+ * Result of {@link lm}: the fit and the quantities of R's `summary(lm)`.
19
+ *
20
+ * @category Linear models and ANOVA
21
+ */
14
22
  export interface LmResult {
15
23
  /** Coefficients (intercept first); NaN for aliased columns. */
16
24
  coefficients: number[];
17
25
  /** Residuals y - fitted (rows with a missing value dropped; rows of weight 0 included). */
18
26
  residuals: number[];
27
+ /** Fitted values, one per residual (R's `fitted.values`). */
19
28
  fittedValues: number[];
20
29
  /** Numerical rank of the model matrix. */
21
30
  rank: number;
@@ -23,9 +32,13 @@ export interface LmResult {
23
32
  dfResidual: number;
24
33
  /** The coefficient table of the columns that are not aliased, in their order. */
25
34
  coefTable: {
35
+ /** Estimates (R's `Estimate`). */
26
36
  estimate: number[];
37
+ /** Standard errors (R's `Std. Error`). */
27
38
  stdError: number[];
39
+ /** t statistics (R's `t value`). */
28
40
  tValue: number[];
41
+ /** Two-sided p-values (R's `Pr(>|t|)`). */
29
42
  pValue: number[];
30
43
  };
31
44
  /** Residual standard error. */
@@ -39,8 +52,11 @@ export interface LmResult {
39
52
  * intercept, where R's `summary(lm)` has no `fstatistic`.
40
53
  */
41
54
  fStatistic?: {
55
+ /** The F statistic. */
42
56
  value: number;
57
+ /** Numerator degrees of freedom (rank minus the intercept). */
43
58
  numdf: number;
59
+ /** Denominator degrees of freedom (the residual degrees of freedom). */
44
60
  dendf: number;
45
61
  };
46
62
  /** p-value of the overall F test; absent with `fStatistic`. */
@@ -50,12 +66,20 @@ export interface LmResult {
50
66
  /** The QR decomposition of the (weighted) model matrix. */
51
67
  qr: QR;
52
68
  }
53
- /** Options of {@link lm} with a formula. */
69
+ /**
70
+ * Options of {@link lm} with a formula.
71
+ *
72
+ * @category Linear models and ANOVA
73
+ */
54
74
  export interface LmFormulaOptions extends ModelMatrixOptions {
55
75
  /** Weights: an array parallel to the rows of `data`, or the name of a numeric column. */
56
76
  weights?: Sample | string;
57
77
  }
58
- /** Result of {@link lm} with a formula: the fit of the model matrix, with the formula's terms. */
78
+ /**
79
+ * Result of {@link lm} with a formula: the fit of the model matrix, with the formula's terms.
80
+ *
81
+ * @category Linear models and ANOVA
82
+ */
59
83
  export interface LmFormulaResult extends LmResult {
60
84
  /** The formula. */
61
85
  formula: string;
@@ -95,11 +119,11 @@ export interface LmFormulaResult extends LmResult {
95
119
  /** The formula fit (lm, and aov without Error()) */
96
120
  export declare function lmFormulaFit(formula: string, d: Design, aov: boolean): LmFormulaResult;
97
121
  /**
98
- * Fit a linear model from a formula. Equivalent to R's `lm(formula, data)` (with `weights` and `contrasts`)
99
- * together with `summary()`.
122
+ * Fit a linear model from a formula. Equivalent to R's `lm(formula, data)`. With `weights` and `contrasts`, and the
123
+ * quantities of `summary()`.
100
124
  *
101
- * The model matrix is that of {@link modelMatrix} (but levels without rows are dropped, as R's
102
- * `drop.unused.levels`); the fit is that of the matrix form (same pivoting and tolerance), its sums of squares computed
125
+ * The model matrix is that of {@link modelMatrix} (but the levels given by `levels` that have no row left are
126
+ * dropped, as R's `drop.unused.levels`; a boolean column keeps both levels FALSE and TRUE); the fit is that of the matrix form (same pivoting and tolerance), its sums of squares computed
103
127
  * in double-double so that data with a large mean keep their accuracy. Rows with a missing value in a variable of the
104
128
  * formula or in the weights are dropped (`rows` lists those used).
105
129
  *
@@ -112,12 +136,14 @@ export declare function lmFormulaFit(formula: string, d: Design, aov: boolean):
112
136
  * fit.coefNames; // ["(Intercept)", "gb", "gc"]
113
137
  * fit.coefficients; // [3, 4, 7.95]
114
138
  * ```
139
+ *
140
+ * @category Linear models and ANOVA
115
141
  */
116
142
  export declare function lm(formula: string, data: DataFrame, options?: LmFormulaOptions): LmFormulaResult;
117
143
  /**
118
- * Fit a linear model by least squares, the predictors given as a matrix. Equivalent to R's `lm(y ~ x)` (or
119
- * `y ~ x - 1` without intercept, with `weights` for weighted least squares) together with `summary()`. Rows with a
120
- * missing value in `x`, `y` or the weights are dropped.
144
+ * Fit a linear model by least squares, the predictors given as a matrix: R's `lm(y ~ x)` (or `y ~ x - 1` without
145
+ * intercept, with `weights` for weighted least squares) together with `summary()`. Rows with a missing value in `x`,
146
+ * `y` or the weights are dropped.
121
147
  *
122
148
  * @param x Predictors: a matrix (array of rows, one column per predictor) or a vector (one predictor).
123
149
  * @param y Response.
@@ -125,8 +151,8 @@ export declare function lm(formula: string, data: DataFrame, options?: LmFormula
125
151
  * @example
126
152
  * ```ts
127
153
  * const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
128
- * fit.coefficients; // [intercept, slope]
129
- * fit.rSquared;
154
+ * fit.coefficients; // [0.05, 1.99]
155
+ * fit.rSquared; // 0.99731
130
156
  * ```
131
157
  */
132
158
  export declare function lm(x: MatrixInput | Sample, y: Sample, options?: LmOptions): LmResult;
@@ -145,20 +171,39 @@ export interface LmCore {
145
171
  * left out of the fit and of the counts.
146
172
  */
147
173
  export declare function fitLm(Xall: number[][], Yall: number[], Wall: number[] | undefined, intercept: boolean, precise?: boolean): LmCore;
148
- /** Options for {@link confint}. */
174
+ /**
175
+ * Options for {@link confint}.
176
+ *
177
+ * @category Linear models and ANOVA
178
+ */
149
179
  export interface ConfintOptions {
150
180
  /** Confidence level. Default `0.95`. */
151
181
  level?: number;
152
182
  }
153
183
  /**
154
- * Confidence intervals for the coefficients of a linear model (NaN for aliased columns). Equivalent to R's
155
- * `confint()` for an `lm` fit.
184
+ * Confidence intervals for the coefficients of a linear model (NaN for aliased columns), for an `lm` fit.
185
+ * Equivalent to R's `confint()`.
186
+ *
187
+ * @example
188
+ * ```ts
189
+ * const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
190
+ * confint(fit); // { lower: [-0.58036, 1.79994], upper: [0.68036, 2.18006] }
191
+ * confint(fit, { level: 0.9 }).lower; // [-0.41614, 1.84945]
192
+ * ```
193
+ *
194
+ * @category Linear models and ANOVA
156
195
  */
157
196
  export declare function confint(fit: LmResult, options?: ConfintOptions): {
197
+ /** Lower limits, one per coefficient (R's first column, `2.5 %` by default). */
158
198
  lower: number[];
199
+ /** Upper limits, one per coefficient (R's second column, `97.5 %` by default). */
159
200
  upper: number[];
160
201
  };
161
- /** Options for {@link predict}. */
202
+ /**
203
+ * Options for {@link predict}.
204
+ *
205
+ * @category Linear models and ANOVA
206
+ */
162
207
  export interface PredictOptions {
163
208
  /** `"none"` (default), `"confidence"` (for the mean response) or `"prediction"` (for a new observation). */
164
209
  interval?: "none" | "confidence" | "prediction";
@@ -166,14 +211,27 @@ export interface PredictOptions {
166
211
  level?: number;
167
212
  }
168
213
  /**
169
- * Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals.
170
- * Equivalent to R's `predict()` for an `lm` fit (`se.fit = TRUE`). Aliased columns are left out, as R does.
214
+ * Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals, for
215
+ * an `lm` fit (`se.fit = TRUE`). Equivalent to R's `predict()`. Aliased columns are left out, as R does.
171
216
  *
172
217
  * @param newdata New predictor values, with the columns of `x` (no column of ones).
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
222
+ * predict(fit, [6, 7]); // { fit: [11.99, 13.98], seFit: [0.19807, 0.25338] }
223
+ * predict(fit, [6], { interval: "prediction" }); // { lwr: [11.119], upr: [12.861] }
224
+ * ```
225
+ *
226
+ * @category Linear models and ANOVA
173
227
  */
174
228
  export declare function predict(fit: LmResult, newdata: MatrixInput | Sample, options?: PredictOptions): {
229
+ /** Predicted values, one per row of `newdata`. */
175
230
  fit: number[];
231
+ /** Standard errors of the predicted means (R's `se.fit`). */
176
232
  seFit: number[];
233
+ /** Lower limits of the intervals (absent with `interval: "none"`). */
177
234
  lwr?: number[];
235
+ /** Upper limits of the intervals (absent with `interval: "none"`). */
178
236
  upr?: number[];
179
237
  };
package/dist/models/lm.js CHANGED
@@ -229,8 +229,17 @@ export function fitLm(Xall, Yall, Wall, intercept, precise = false) {
229
229
  return core;
230
230
  }
231
231
  /**
232
- * Confidence intervals for the coefficients of a linear model (NaN for aliased columns). Equivalent to R's
233
- * `confint()` for an `lm` fit.
232
+ * Confidence intervals for the coefficients of a linear model (NaN for aliased columns), for an `lm` fit.
233
+ * Equivalent to R's `confint()`.
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
238
+ * confint(fit); // { lower: [-0.58036, 1.79994], upper: [0.68036, 2.18006] }
239
+ * confint(fit, { level: 0.9 }).lower; // [-0.41614, 1.84945]
240
+ * ```
241
+ *
242
+ * @category Linear models and ANOVA
234
243
  */
235
244
  export function confint(fit, options = {}) {
236
245
  const level = options.level ?? 0.95;
@@ -249,10 +258,19 @@ export function confint(fit, options = {}) {
249
258
  return { lower, upper };
250
259
  }
251
260
  /**
252
- * Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals.
253
- * Equivalent to R's `predict()` for an `lm` fit (`se.fit = TRUE`). Aliased columns are left out, as R does.
261
+ * Predictions of a linear model at new values of the predictors, with standard errors and optionally intervals, for
262
+ * an `lm` fit (`se.fit = TRUE`). Equivalent to R's `predict()`. Aliased columns are left out, as R does.
254
263
  *
255
264
  * @param newdata New predictor values, with the columns of `x` (no column of ones).
265
+ *
266
+ * @example
267
+ * ```ts
268
+ * const fit = lm([1, 2, 3, 4, 5], [2.1, 3.9, 6.2, 7.8, 10.1]);
269
+ * predict(fit, [6, 7]); // { fit: [11.99, 13.98], seFit: [0.19807, 0.25338] }
270
+ * predict(fit, [6], { interval: "prediction" }); // { lwr: [11.119], upr: [12.861] }
271
+ * ```
272
+ *
273
+ * @category Linear models and ANOVA
256
274
  */
257
275
  export function predict(fit, newdata, options = {}) {
258
276
  const rows = newdata.length > 0 && typeof newdata[0] !== "number"
@@ -0,0 +1,162 @@
1
+ import type { DataFrame } from "./modelFrame.js";
2
+ import type { ContrastName } from "./modelMatrix.js";
3
+ import { type MixedModelStructure } from "./mixedStructure.js";
4
+ /**
5
+ * Options of {@link lmer}.
6
+ *
7
+ * @category Mixed models
8
+ */
9
+ export interface LmerOptions {
10
+ /** Whether to minimise the REML criterion (default `true`) or the ML deviance (`false`). */
11
+ REML?: boolean;
12
+ /**
13
+ * A contrast per factor of the fixed-effects part (default `"contr.treatment"`); the random-effects terms always use
14
+ * treatment contrasts. A name that is not a variable of the fixed-effects part (a grouping variable alone included)
15
+ * gives the warning `variable 'B' is absent, its contrast will be ignored`.
16
+ */
17
+ contrasts?: Record<string, ContrastName>;
18
+ /** The order of the levels of a factor (`{ A: ["c", "a", "b"] }`), as `factor(x, levels = ...)` in R. */
19
+ levels?: Record<string, readonly string[]>;
20
+ }
21
+ /**
22
+ * The variance and correlation parameters of a random-effects term in a {@link LmerResult}: one entry of R's
23
+ * `VarCorr()`.
24
+ *
25
+ * @category Mixed models
26
+ */
27
+ export interface LmerVarCorr {
28
+ /** The grouping factor's name, such as `"Subject"` or `"cask:batch"`. */
29
+ group: string;
30
+ /** The names of the term's columns, such as `["(Intercept)", "Days"]`. */
31
+ columns: string[];
32
+ /** The standard deviations of the random effects, one per column. */
33
+ sd: number[];
34
+ /** Their correlation matrix (array of rows; 1 on the diagonal). */
35
+ corr: number[][];
36
+ }
37
+ /**
38
+ * The conditional modes of the random effects of a grouping factor in a {@link LmerResult}: one entry of R's
39
+ * `ranef()`.
40
+ *
41
+ * @category Mixed models
42
+ */
43
+ export interface LmerRanef {
44
+ /** The grouping factor's name. */
45
+ group: string;
46
+ /** Its levels (the rows of `values`). */
47
+ levels: string[];
48
+ /** The columns of all the terms of this grouping factor, in term order. */
49
+ columns: string[];
50
+ /** One array per level, one value per column. */
51
+ values: number[][];
52
+ }
53
+ /**
54
+ * Result of {@link lmer}: the fit of a linear mixed model and the quantities of R's methods for it.
55
+ *
56
+ * @category Mixed models
57
+ */
58
+ export interface LmerResult {
59
+ /** The formula. */
60
+ formula: string;
61
+ /** Whether the REML criterion was minimised (otherwise the ML deviance). */
62
+ REML: boolean;
63
+ /** Number of rows used. */
64
+ nObs: number;
65
+ /** 0-based indices of the rows of `data` used (rows with a missing value in a variable of the formula dropped). */
66
+ rows: number[];
67
+ /** The estimate of the covariance parameters θ (R's `getME(fit, "theta")`): the lower triangles of the relative covariance factors, by term, column-major. */
68
+ theta: number[];
69
+ /** Names of the columns of the fixed-effects model matrix (aliased columns dropped). */
70
+ fixefNames: string[];
71
+ /** The fixed effects β̂ (R's `fixef()`). */
72
+ fixef: number[];
73
+ /** The coefficient table of the fixed effects (R's `coef(summary(fit))`). */
74
+ coefTable: {
75
+ /** Estimates (R's `Estimate`). */
76
+ estimate: number[];
77
+ /** Standard errors (R's `Std. Error`). */
78
+ stdError: number[];
79
+ /** t statistics (R's `t value`). */
80
+ tValue: number[];
81
+ };
82
+ /** The covariance matrix of the fixed effects (R's `vcov()`), array of rows. */
83
+ vcov: number[][];
84
+ /** The residual standard deviation (R's `sigma()`). */
85
+ sigma: number;
86
+ /** The variance components, one entry per random-effects term (R's `VarCorr()`). */
87
+ varCorr: LmerVarCorr[];
88
+ /** The conditional modes of the random effects, one entry per grouping factor (R's `ranef()`). */
89
+ ranef: LmerRanef[];
90
+ /** Fitted values Xβ̂ + Zb̂, one per row used (R's `fitted()`). */
91
+ fitted: number[];
92
+ /** Residuals y − fitted (R's `residuals()`). */
93
+ residuals: number[];
94
+ /** The log-likelihood, or the REML log-likelihood for a REML fit (R's `logLik()`). */
95
+ logLik: number;
96
+ /** Degrees of freedom of the log-likelihood: the numbers of fixed effects and of θ, plus 1 (for σ). */
97
+ df: number;
98
+ /** Akaike's information criterion (R's `AIC()`). */
99
+ AIC: number;
100
+ /** Schwarz's Bayesian information criterion (R's `BIC()`). */
101
+ BIC: number;
102
+ /** The ML deviance at the estimates, for either kind of fit (R's `deviance(fit, REML = FALSE)`). */
103
+ deviance: number;
104
+ /** The REML criterion at the estimates, for either kind of fit (R's `REMLcrit()`). */
105
+ REMLcrit: number;
106
+ /** Whether the fit is singular: a diagonal element of a relative covariance factor below 1e-4 (R's `isSingular()`). */
107
+ singular: boolean;
108
+ /** R's messages (a rank-deficient fixed-effects matrix, a singular fit). */
109
+ messages: string[];
110
+ /** R's warnings (predictors on very different scales). */
111
+ warnings: string[];
112
+ }
113
+ /**
114
+ * The criterion (REML criterion or ML deviance) and its gradient with respect to θ at `theta`. Internal: the fit uses
115
+ * the sparse {@link profiledDeviance}; the regression test injects the dense reference implementation.
116
+ */
117
+ export type MixedCriterion = (s: MixedModelStructure, theta: readonly number[], reml: boolean) => {
118
+ f: number;
119
+ g: number[];
120
+ };
121
+ /**
122
+ * Fit a linear mixed model by REML (default) or maximum likelihood. Equivalent to `lme4::lmer()` in R. With the
123
+ * quantities of its methods: `fixef()`, `coef(summary())` (without p-values), `vcov()`, `sigma()`, `VarCorr()`,
124
+ * `ranef()`, `fitted()`, `residuals()`, `logLik()`, `AIC()`, `BIC()`, `deviance()`, `REMLcrit()` and `isSingular()`.
125
+ *
126
+ * The formula has random-effects terms `(lhs | group)` (correlated effects of the columns of `lhs` within each level
127
+ * of `group`) or `(lhs || group)` (uncorrelated); `group` may be `a:b` (the combinations), `a/b` (nested: `b:a` and
128
+ * `a`) and so on, as in lme4. The fixed-effects part is coded as in {@link lm}; aliased columns are dropped (with a
129
+ * message). Rows with a missing value in a variable of the formula are dropped (`rows` lists those used).
130
+ *
131
+ * The covariance parameters θ are the lower triangles of the relative covariance factors of the terms; the estimate
132
+ * is the exact minimiser of the profiled criterion (lme4's optimiser stops close to it, so lme4's values typically
133
+ * differ in the 5th to 9th significant digit). Elements on the boundary are exactly 0, and the fit is then singular.
134
+ * The computation is sparse (a sparse Cholesky factorisation, as in lme4), so many groups are handled quickly.
135
+ *
136
+ * @param formula A formula such as `"Reaction ~ Days + (Days | Subject)"`.
137
+ * @param data The data frame: an object of columns.
138
+ * @param options `REML`, and `contrasts` and `levels` as in {@link lm}.
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * // R: lme4::lmer(y ~ 1 + (1 | g), data)
143
+ * const fit = lmer("y ~ 1 + (1 | g)", {
144
+ * y: [9.8, 10.4, 10.1, 12.3, 12.9, 12.0, 8.1, 7.6, 8.4],
145
+ * g: ["a", "a", "a", "b", "b", "b", "c", "c", "c"],
146
+ * });
147
+ * fit.fixef; // [10.177778]
148
+ * fit.sigma; // 0.39299
149
+ * fit.varCorr[0].sd; // [2.1726]
150
+ * fit.ranef[0].levels; // ["a", "b", "c"]
151
+ * fit.ranef[0].values; // [[-0.076939], [2.1982], [-2.1213]]
152
+ * fit.REMLcrit; // 19.015
153
+ * ```
154
+ *
155
+ * @category Mixed models
156
+ */
157
+ export declare function lmer(formula: string, data: DataFrame, options?: LmerOptions): LmerResult;
158
+ /**
159
+ * {@link lmer} with the criterion and its gradient given (internal: not exported from the package). The regression
160
+ * test of the sparse computation fits with the dense reference implementation through it.
161
+ */
162
+ export declare function lmerWithCriterion(formula: string, data: DataFrame, options: LmerOptions, criterion: MixedCriterion): LmerResult;