@numero-uno-jpn/rstats 0.15.2 → 0.16.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 (297) hide show
  1. package/CHANGELOG.md +280 -0
  2. package/README.ja.md +40 -33
  3. package/README.md +36 -28
  4. package/dist/chunks/chunk-2D2IQWUQ.js +2 -0
  5. package/dist/chunks/chunk-5UQCRBAG.js +2 -0
  6. package/dist/chunks/chunk-6CSTWA5E.js +2 -0
  7. package/dist/chunks/chunk-72Y3HZID.js +2 -0
  8. package/dist/chunks/chunk-BG2KLEHM.js +2 -0
  9. package/dist/chunks/chunk-CMEKBNHQ.js +2 -0
  10. package/dist/chunks/chunk-FLJNSXSW.js +2 -0
  11. package/dist/chunks/chunk-GM53LWSR.js +2 -0
  12. package/dist/chunks/chunk-JBCA56ZS.js +3 -0
  13. package/dist/chunks/chunk-K3RJAD2T.js +2 -0
  14. package/dist/chunks/chunk-KAZRWIB5.js +2 -0
  15. package/dist/chunks/chunk-KBVVIMXX.js +3 -0
  16. package/dist/chunks/chunk-N3SIFCZX.js +2 -0
  17. package/dist/chunks/chunk-QNNL7AZ3.js +2 -0
  18. package/dist/chunks/chunk-SC6CV4Z3.js +2 -0
  19. package/dist/chunks/chunk-SYWOBOTR.js +2 -0
  20. package/dist/chunks/chunk-URIO5LY5.js +2 -0
  21. package/dist/chunks/chunk-VTUGG2SV.js +2 -0
  22. package/dist/chunks/chunk-W26LRYOJ.js +2 -0
  23. package/dist/chunks/chunk-XHETEYVC.js +2 -0
  24. package/dist/chunks/{chunk-PWVQVJTP.js → chunk-YRZWONZB.js} +1 -1
  25. package/dist/chunks/chunk-ZGABZMWX.js +2 -0
  26. package/dist/data/grouping.js +20 -5
  27. package/dist/descriptive/basic.js +90 -8
  28. package/dist/descriptive/cov.js +12 -4
  29. package/dist/descriptive/dataFrames.d.ts +4 -2
  30. package/dist/descriptive/dataFrames.js +56 -18
  31. package/dist/descriptive/factanal.js +16 -7
  32. package/dist/descriptive/hist.d.ts +8 -5
  33. package/dist/descriptive/hist.js +71 -31
  34. package/dist/descriptive/missing.js +3 -1
  35. package/dist/descriptive/moments.js +72 -19
  36. package/dist/descriptive/omega.d.ts +1 -1
  37. package/dist/descriptive/omega.js +68 -9
  38. package/dist/descriptive/pivot.d.ts +21 -9
  39. package/dist/descriptive/pivot.js +161 -35
  40. package/dist/descriptive/quantile.js +2 -1
  41. package/dist/descriptive/rank.js +4 -1
  42. package/dist/descriptive/table.d.ts +3 -2
  43. package/dist/descriptive/table.js +24 -9
  44. package/dist/descriptive.js +2 -2
  45. package/dist/distributions/arcsine.js +19 -2
  46. package/dist/distributions/beta.js +35 -5
  47. package/dist/distributions/binomial.js +37 -14
  48. package/dist/distributions/bvnorm.js +11 -2
  49. package/dist/distributions/cauchy.js +87 -8
  50. package/dist/distributions/chisq.js +7 -3
  51. package/dist/distributions/exponential.js +11 -1
  52. package/dist/distributions/f.js +73 -22
  53. package/dist/distributions/gamma.js +61 -6
  54. package/dist/distributions/geometric.js +53 -14
  55. package/dist/distributions/hypergeometric.d.ts +5 -1
  56. package/dist/distributions/hypergeometric.js +93 -6
  57. package/dist/distributions/invgamma.js +6 -1
  58. package/dist/distributions/kumaraswamy.js +22 -4
  59. package/dist/distributions/laplace.js +31 -7
  60. package/dist/distributions/logistic.js +11 -2
  61. package/dist/distributions/lognormal.js +20 -3
  62. package/dist/distributions/multinomial.js +54 -11
  63. package/dist/distributions/nbeta.js +128 -35
  64. package/dist/distributions/nchisq.js +85 -14
  65. package/dist/distributions/negbinomial.js +100 -18
  66. package/dist/distributions/normal.js +35 -5
  67. package/dist/distributions/nt.d.ts +28 -3
  68. package/dist/distributions/nt.js +536 -35
  69. package/dist/distributions/pareto.js +9 -1
  70. package/dist/distributions/poisson.js +28 -3
  71. package/dist/distributions/signrank.js +7 -4
  72. package/dist/distributions/summary.js +450 -60
  73. package/dist/distributions/t.js +55 -3
  74. package/dist/distributions/triangular.js +45 -13
  75. package/dist/distributions/tukey.js +21 -2
  76. package/dist/distributions/uniform.js +2 -0
  77. package/dist/distributions/weibull.js +33 -3
  78. package/dist/distributions/wilcox.d.ts +9 -3
  79. package/dist/distributions/wilcox.js +155 -26
  80. package/dist/distributions.js +2 -2
  81. package/dist/htest/agreement.d.ts +3 -2
  82. package/dist/htest/agreement.js +76 -14
  83. package/dist/htest/ansariTest.d.ts +6 -1
  84. package/dist/htest/ansariTest.js +75 -8
  85. package/dist/htest/aov.d.ts +1 -1
  86. package/dist/htest/aov.js +48 -34
  87. package/dist/htest/binomTest.d.ts +5 -0
  88. package/dist/htest/binomTest.js +41 -17
  89. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  90. package/dist/htest/brunnerMunzelTest.js +7 -5
  91. package/dist/htest/chisqTest.d.ts +2 -1
  92. package/dist/htest/chisqTest.js +130 -43
  93. package/dist/htest/cor.d.ts +2 -1
  94. package/dist/htest/cor.js +17 -19
  95. package/dist/htest/core/ansari.d.ts +9 -0
  96. package/dist/htest/core/ansari.js +21 -1
  97. package/dist/htest/core/kolmogorov.d.ts +25 -0
  98. package/dist/htest/core/kolmogorov.js +123 -0
  99. package/dist/htest/core/mantelhaen.js +6 -4
  100. package/dist/htest/core/shapiro.d.ts +1 -1
  101. package/dist/htest/core/shapiro.js +5 -1
  102. package/dist/htest/dunnett.js +14 -5
  103. package/dist/htest/effectSize.d.ts +2 -0
  104. package/dist/htest/effectSize.js +91 -29
  105. package/dist/htest/fisherTest.d.ts +2 -2
  106. package/dist/htest/fisherTest.js +36 -18
  107. package/dist/htest/groups.d.ts +7 -0
  108. package/dist/htest/groups.js +21 -4
  109. package/dist/htest/ksTest.d.ts +3 -1
  110. package/dist/htest/ksTest.js +23 -2
  111. package/dist/htest/leveneTest.js +19 -3
  112. package/dist/htest/mantelhaenTest.js +125 -25
  113. package/dist/htest/mcnemarTest.js +32 -11
  114. package/dist/htest/multipleComparisons.d.ts +8 -1
  115. package/dist/htest/multipleComparisons.js +44 -21
  116. package/dist/htest/normalityTests.js +44 -10
  117. package/dist/htest/onewayTest.js +96 -5
  118. package/dist/htest/pAdjust.d.ts +3 -2
  119. package/dist/htest/pAdjust.js +38 -6
  120. package/dist/htest/pairwise.d.ts +1 -1
  121. package/dist/htest/pairwise.js +17 -15
  122. package/dist/htest/poissonTest.d.ts +1 -1
  123. package/dist/htest/poissonTest.js +111 -59
  124. package/dist/htest/polycor.js +27 -7
  125. package/dist/htest/power.d.ts +11 -1
  126. package/dist/htest/power.js +112 -8
  127. package/dist/htest/propTest.js +21 -4
  128. package/dist/htest/pwr.js +121 -24
  129. package/dist/htest/rankTests.js +29 -5
  130. package/dist/htest/tTest.js +44 -21
  131. package/dist/htest/trendTests.d.ts +7 -3
  132. package/dist/htest/trendTests.js +114 -20
  133. package/dist/htest/types.d.ts +5 -0
  134. package/dist/htest/types.js +7 -0
  135. package/dist/htest/varTest.js +45 -14
  136. package/dist/htest/wilcoxTest.js +114 -21
  137. package/dist/integrate/quadpack.js +27 -5
  138. package/dist/internal/covmat.d.ts +5 -0
  139. package/dist/internal/covmat.js +20 -0
  140. package/dist/internal/ddexp.d.ts +23 -1
  141. package/dist/internal/ddexp.js +86 -6
  142. package/dist/internal/discrete.d.ts +14 -0
  143. package/dist/internal/discrete.js +78 -5
  144. package/dist/internal/extremes.d.ts +9 -0
  145. package/dist/internal/extremes.js +24 -0
  146. package/dist/internal/fround.js +13 -4
  147. package/dist/internal/integer.d.ts +9 -0
  148. package/dist/internal/integer.js +12 -0
  149. package/dist/internal/invert.d.ts +5 -0
  150. package/dist/internal/invert.js +25 -7
  151. package/dist/internal/noncentral.d.ts +7 -0
  152. package/dist/internal/noncentral.js +9 -0
  153. package/dist/internal/poissonMixture.js +29 -6
  154. package/dist/internal/scaledInverse.d.ts +10 -0
  155. package/dist/internal/scaledInverse.js +22 -0
  156. package/dist/internal/seq.js +15 -2
  157. package/dist/internal/summary.d.ts +90 -0
  158. package/dist/internal/summary.js +458 -39
  159. package/dist/internal/uniroot.d.ts +7 -0
  160. package/dist/internal/uniroot.js +28 -3
  161. package/dist/linalg/basic.d.ts +3 -1
  162. package/dist/linalg/basic.js +51 -7
  163. package/dist/linalg/chol.js +4 -1
  164. package/dist/linalg/dot.d.ts +29 -0
  165. package/dist/linalg/dot.js +132 -0
  166. package/dist/linalg/eigen.d.ts +6 -1
  167. package/dist/linalg/eigen.js +71 -14
  168. package/dist/linalg/expm.d.ts +3 -2
  169. package/dist/linalg/expm.js +109 -15
  170. package/dist/linalg/kappa.js +58 -10
  171. package/dist/linalg/lu.d.ts +2 -1
  172. package/dist/linalg/lu.js +35 -5
  173. package/dist/linalg/matrix.d.ts +14 -0
  174. package/dist/linalg/matrix.js +24 -0
  175. package/dist/linalg/ops.js +60 -13
  176. package/dist/linalg/qr.d.ts +2 -1
  177. package/dist/linalg/qr.js +91 -23
  178. package/dist/linalg/schur.js +11 -3
  179. package/dist/linalg/solve.d.ts +6 -1
  180. package/dist/linalg/solve.js +147 -26
  181. package/dist/linalg/svd.d.ts +9 -2
  182. package/dist/linalg/svd.js +29 -11
  183. package/dist/linalg.js +2 -2
  184. package/dist/mixed.js +4 -4
  185. package/dist/models/anovaSequential.d.ts +16 -1
  186. package/dist/models/anovaSequential.js +124 -25
  187. package/dist/models/aovEz.js +140 -30
  188. package/dist/models/aovStrata.js +55 -22
  189. package/dist/models/carAnova.d.ts +1 -1
  190. package/dist/models/carAnova.js +83 -32
  191. package/dist/models/coxph.js +148 -66
  192. package/dist/models/estimable.d.ts +15 -0
  193. package/dist/models/estimable.js +68 -0
  194. package/dist/models/formula.d.ts +1 -1
  195. package/dist/models/formula.js +26 -8
  196. package/dist/models/glm.d.ts +35 -5
  197. package/dist/models/glm.js +398 -55
  198. package/dist/models/glmAnova.js +84 -13
  199. package/dist/models/glmConfint.d.ts +1 -1
  200. package/dist/models/glmConfint.js +115 -27
  201. package/dist/models/glmFamily.d.ts +10 -0
  202. package/dist/models/glmFamily.js +244 -40
  203. package/dist/models/lm.d.ts +12 -1
  204. package/dist/models/lm.js +235 -36
  205. package/dist/models/lmer.d.ts +20 -1
  206. package/dist/models/lmer.js +109 -27
  207. package/dist/models/lmerAnova.js +37 -4
  208. package/dist/models/lmerHypotheses.d.ts +6 -2
  209. package/dist/models/lmerHypotheses.js +14 -2
  210. package/dist/models/lmerTest.d.ts +9 -2
  211. package/dist/models/lmerTest.js +64 -19
  212. package/dist/models/mauchly.d.ts +4 -1
  213. package/dist/models/mauchly.js +34 -13
  214. package/dist/models/mixedStructure.js +6 -0
  215. package/dist/models/modelFrame.js +8 -2
  216. package/dist/models/modelMatrix.d.ts +3 -1
  217. package/dist/models/modelMatrix.js +40 -15
  218. package/dist/models/scaledTables.d.ts +8 -0
  219. package/dist/models/scaledTables.js +6 -0
  220. package/dist/models/survival.d.ts +6 -1
  221. package/dist/models/survival.js +67 -27
  222. package/dist/models/tiedTimes.d.ts +2 -0
  223. package/dist/models/tiedTimes.js +34 -0
  224. package/dist/numerics/deriv.d.ts +1 -1
  225. package/dist/numerics/deriv.js +64 -8
  226. package/dist/numerics/integrate.d.ts +9 -1
  227. package/dist/numerics/integrate.js +103 -16
  228. package/dist/numerics/interp.d.ts +8 -2
  229. package/dist/numerics/interp.js +352 -50
  230. package/dist/numerics/ode.js +55 -1
  231. package/dist/numerics.js +2 -2
  232. package/dist/optim/localmin.js +4 -0
  233. package/dist/optim/optim.d.ts +3 -1
  234. package/dist/optim/optim.js +80 -8
  235. package/dist/random/beta.js +53 -4
  236. package/dist/random/binomial.d.ts +14 -3
  237. package/dist/random/binomial.js +59 -24
  238. package/dist/random/continuous.js +33 -5
  239. package/dist/random/extra.js +42 -9
  240. package/dist/random/gamma.d.ts +4 -1
  241. package/dist/random/gamma.js +36 -8
  242. package/dist/random/hyper.js +9 -6
  243. package/dist/random/mt.js +17 -2
  244. package/dist/random/poisson.js +26 -3
  245. package/dist/random/r2dtable.d.ts +8 -1
  246. package/dist/random/r2dtable.js +24 -3
  247. package/dist/random/rank.d.ts +2 -0
  248. package/dist/random/rank.js +44 -4
  249. package/dist/random/sample.d.ts +12 -2
  250. package/dist/random/sample.js +57 -20
  251. package/dist/random/streams.js +4 -2
  252. package/dist/rstats.js +14 -14
  253. package/dist/special/besselIK.d.ts +12 -0
  254. package/dist/special/besselIK.js +168 -23
  255. package/dist/special/besselJY.d.ts +11 -1
  256. package/dist/special/besselJY.js +127 -19
  257. package/dist/special/besselSeries.d.ts +27 -0
  258. package/dist/special/besselSeries.js +45 -2
  259. package/dist/special/beta.js +11 -1
  260. package/dist/special/choose.js +17 -8
  261. package/dist/special/dd.js +122 -18
  262. package/dist/special/extra.js +14 -2
  263. package/dist/special/gamma.js +15 -2
  264. package/dist/special/igam.js +33 -6
  265. package/dist/special/incbet.js +100 -10
  266. package/dist/special/incbetLargeA.js +7 -2
  267. package/dist/special/incbetPrefactor.d.ts +5 -0
  268. package/dist/special/incbetPrefactor.js +14 -3
  269. package/dist/special/incbetTemme.d.ts +7 -0
  270. package/dist/special/incbetTemme.js +45 -12
  271. package/dist/special/loader.d.ts +1 -1
  272. package/dist/special/loader.js +179 -33
  273. package/dist/special/log1pmx.js +4 -0
  274. package/dist/special.js +2 -2
  275. package/dist/tests.js +11 -11
  276. package/llms.txt +12 -6
  277. package/package.json +1 -1
  278. package/dist/chunks/chunk-2CUKVVRO.js +0 -2
  279. package/dist/chunks/chunk-3WQCKGKP.js +0 -2
  280. package/dist/chunks/chunk-4Z2XDWKV.js +0 -2
  281. package/dist/chunks/chunk-AJBLCNZ7.js +0 -3
  282. package/dist/chunks/chunk-D4F7GQAY.js +0 -2
  283. package/dist/chunks/chunk-EBQMGCQZ.js +0 -2
  284. package/dist/chunks/chunk-HBMHR5SS.js +0 -2
  285. package/dist/chunks/chunk-IBPWRQIF.js +0 -2
  286. package/dist/chunks/chunk-JLAH3X77.js +0 -2
  287. package/dist/chunks/chunk-LG74QY5U.js +0 -2
  288. package/dist/chunks/chunk-PEYGUK32.js +0 -2
  289. package/dist/chunks/chunk-Q4PGYESP.js +0 -2
  290. package/dist/chunks/chunk-RCAL4VHM.js +0 -3
  291. package/dist/chunks/chunk-SREPSVTT.js +0 -2
  292. package/dist/chunks/chunk-UFT662QZ.js +0 -2
  293. package/dist/chunks/chunk-VWJ74UHC.js +0 -2
  294. package/dist/chunks/chunk-XNPU54NH.js +0 -2
  295. package/dist/chunks/chunk-XPLDEK6D.js +0 -2
  296. package/dist/chunks/chunk-YGT4P7EU.js +0 -2
  297. package/dist/chunks/chunk-YVREYVI2.js +0 -2
@@ -10,7 +10,9 @@ import { dgamma1 } from "../distributions/gamma.js";
10
10
  import { dnorm, pnorm, qnorm } from "../distributions/normal.js";
11
11
  import { dpois1, nonInteger } from "../distributions/poisson.js";
12
12
  import { roundHalfEven } from "../internal/integer.js";
13
- const EPS = 2 ** -52;
13
+ import { ldexp } from "../internal/summary.js";
14
+ import { bd0 } from "../special/loader.js";
15
+ import { log1pmx } from "../special/log1p.js";
14
16
  const ALLOWED = {
15
17
  gaussian: ["identity", "log", "inverse"],
16
18
  binomial: ["logit", "probit", "cauchit", "log", "cloglog"],
@@ -76,17 +78,22 @@ function logit(m) {
76
78
  return Math.log(m / (1 - m));
77
79
  }
78
80
  const LINK_NAMES = ["logit", "probit", "cauchit", "cloglog", "identity", "log", "sqrt", "1/mu^2", "inverse"];
81
+ /** A probability kept within (0, 1): the least double and the largest double below 1 at the ends */
82
+ const inUnit = (p) => Math.min(Math.max(p, Number.MIN_VALUE), 1 - 2 ** -53);
79
83
  function namedLink(name) {
80
84
  switch (name) {
81
85
  case "identity":
82
- return { name, link: (m) => m, linkinv: (e) => e, muEta: () => 1, etaOk: anyEta };
86
+ return { name, link: (m) => m, linkinv: (e) => e, muEta: () => 1, etaOk: anyEta, muPower: { c: 1, a: 0 } };
83
87
  case "log":
84
88
  return {
85
89
  name,
86
90
  link: Math.log,
87
- linkinv: (e) => Math.max(Math.exp(e), EPS),
88
- muEta: (e) => Math.max(Math.exp(e), EPS),
91
+ // exp(eta) down to the least double (R: pmax(exp(eta), eps), so a mean of e^-100 was 2.2e-16); never 0, so
92
+ // the variance stays positive
93
+ linkinv: (e) => Math.max(Math.exp(e), Number.MIN_VALUE),
94
+ muEta: (e) => Math.max(Math.exp(e), Number.MIN_VALUE),
89
95
  etaOk: anyEta,
96
+ muPower: { c: 1, a: 1 },
90
97
  };
91
98
  case "inverse":
92
99
  return {
@@ -95,9 +102,10 @@ function namedLink(name) {
95
102
  linkinv: (e) => 1 / e,
96
103
  muEta: (e) => -1 / (e * e),
97
104
  etaOk: (e) => Number.isFinite(e) && e !== 0,
105
+ muPower: { c: -1, a: 2 },
98
106
  };
99
107
  case "sqrt":
100
- return { name, link: Math.sqrt, linkinv: (e) => e * e, muEta: (e) => 2 * e, etaOk: positive };
108
+ return { name, link: Math.sqrt, linkinv: (e) => e * e, muEta: (e) => 2 * e, etaOk: positive, muPower: { c: 2, a: 0.5 } };
101
109
  case "1/mu^2":
102
110
  return {
103
111
  name,
@@ -105,51 +113,59 @@ function namedLink(name) {
105
113
  linkinv: (e) => 1 / sqrtWarn(e),
106
114
  muEta: (e) => -1 / (2 * e ** 1.5),
107
115
  etaOk: positive,
116
+ muPower: { c: -0.5, a: 3 },
108
117
  };
109
118
  case "logit":
110
119
  return {
111
120
  name,
112
121
  link: logit,
122
+ // R's C code clamps |eta| > 30 to the values at eps, a jump (1 - 9.4e-14 at 30, 1 - 2.2e-16 just beyond) that
123
+ // kept fits from their solution. Beyond 30 the exact values (as R's formula within), down to the least double
124
+ // and up to the largest double below 1: mu is never 0 or 1, so the variance stays positive
113
125
  linkinv: (e) => {
114
- const t = e < -30 ? EPS : e > 30 ? 1 / EPS : Math.exp(e);
115
- return t / (1 + t);
126
+ if (e > 30)
127
+ return Math.min(1 / (1 + Math.exp(-e)), 1 - 2 ** -53);
128
+ const t = Math.exp(e);
129
+ return Math.max(t / (1 + t), Number.MIN_VALUE);
116
130
  },
117
131
  muEta: (e) => {
118
- if (Math.abs(e) > 30)
119
- return EPS;
132
+ if (Math.abs(e) > 30) {
133
+ const z = Math.exp(-Math.abs(e));
134
+ return Math.max(z / ((1 + z) * (1 + z)), Number.MIN_VALUE);
135
+ }
120
136
  const t = Math.exp(e);
121
137
  return t / ((1 + t) * (1 + t));
122
138
  },
123
139
  etaOk: anyEta,
124
140
  };
125
- case "probit": {
126
- const c = -qnorm(EPS);
141
+ // probit, cauchit and cloglog: R clamps eta (or mu) to the values at eps and the derivative to eps (Phi(-10) =
142
+ // 7.6e-24 was 2.2e-16); here the exact values, kept within (0, 1) by the least double and the largest double
143
+ // below 1 as logit. Within R's range the same values
144
+ case "probit":
127
145
  return {
128
146
  name,
129
147
  link: (m) => qnorm(m),
130
- linkinv: (e) => pnorm(Math.min(Math.max(e, -c), c)),
131
- muEta: (e) => Math.max(dnorm(e), EPS),
148
+ linkinv: (e) => inUnit(pnorm(e)),
149
+ muEta: (e) => Math.max(dnorm(e), Number.MIN_VALUE),
132
150
  etaOk: anyEta,
133
151
  };
134
- }
135
- case "cauchit": {
136
- const c = -qcauchy1(EPS, 0, 1, true, false);
152
+ case "cauchit":
137
153
  return {
138
154
  name,
139
155
  link: (m) => qcauchy1(m, 0, 1, true, false),
140
- linkinv: (e) => pcauchy1(Math.min(Math.max(e, -c), c), 0, 1, true, false),
141
- muEta: (e) => Math.max(dcauchy1(e, 0, 1, false), EPS),
156
+ linkinv: (e) => inUnit(pcauchy1(e, 0, 1, true, false)),
157
+ muEta: (e) => Math.max(dcauchy1(e, 0, 1, false), Number.MIN_VALUE),
142
158
  etaOk: anyEta,
143
159
  };
144
- }
145
160
  case "cloglog":
146
161
  return {
147
162
  name,
148
- link: (m) => Math.log(-Math.log(1 - m)),
149
- linkinv: (e) => Math.min(Math.max(-Math.expm1(-Math.exp(e)), EPS), 1 - EPS),
163
+ // log1p: 1 - mu rounded to 1 for small means (log(-log(1 - 1e-20)) was -Infinity, R too)
164
+ link: (m) => Math.log(-Math.log1p(-m)),
165
+ linkinv: (e) => inUnit(-Math.expm1(-Math.exp(e))),
150
166
  muEta: (e) => {
151
167
  const t = Math.min(e, 700);
152
- return Math.max(Math.exp(t) * Math.exp(-Math.exp(t)), EPS);
168
+ return Math.max(Math.exp(t) * Math.exp(-Math.exp(t)), Number.MIN_VALUE);
153
169
  },
154
170
  etaOk: anyEta,
155
171
  };
@@ -169,44 +185,184 @@ function powerLink(lambda) {
169
185
  return {
170
186
  name: `mu^${Math.round(lambda * 1000) / 1000}`,
171
187
  link: (m) => m ** lambda,
172
- linkinv: (e) => Math.max(e ** inv, EPS),
173
- muEta: (e) => Math.max(inv * e ** (inv - 1), EPS),
188
+ // (R: pmax(..., eps), so a mean of 1e-16 was 2.2e-16)
189
+ // (1 ** Infinity is NaN in JavaScript, where 1/lambda overflows)
190
+ linkinv: (e) => Math.max(e === 1 ? 1 : e ** inv, Number.MIN_VALUE),
191
+ muEta: (e) => powerMuEta(e, lambda, inv),
174
192
  etaOk: positive,
193
+ // (eta^(1/lambda - 1) = mu^(1 - lambda); none where 1/lambda overflows)
194
+ ...(Number.isFinite(inv) ? { muPower: { c: inv, a: 1 - lambda } } : {}),
175
195
  };
176
196
  }
197
+ /**
198
+ * d mu/d eta = eta^(1/lambda - 1)/lambda of the power link. Where that form is not finite (1/lambda overflows, or
199
+ * eta^(1/lambda - 1) overflows against a tiny 1/lambda), as eta^(1/lambda)/(lambda eta), or from its logs
200
+ */
201
+ function powerMuEta(e, lambda, inv) {
202
+ const r = inv * e ** (inv - 1);
203
+ if (r < Infinity)
204
+ return Math.max(r, Number.MIN_VALUE);
205
+ if (e === 1)
206
+ return inv;
207
+ const a = e ** inv;
208
+ const b = lambda * e;
209
+ if (a > 0 && a < Infinity && b >= 2 ** -1022 && b < Infinity)
210
+ return Math.max(a / b, Number.MIN_VALUE);
211
+ return Math.max(Math.exp(inv * Math.log(e) - Math.log(lambda) - Math.log(e)), Number.MIN_VALUE);
212
+ }
177
213
  function quasiVariance(v) {
178
214
  switch (v) {
179
215
  case "constant":
180
- return { variance: () => 1, muOk: () => true, dev: (y, mu, w) => w * (y - mu) ** 2 };
216
+ return { variance: () => 1, muOk: () => true, dev: (y, mu, w) => w * (y - mu) ** 2, varPower: 0 };
181
217
  case "mu(1-mu)":
182
218
  return { variance: (m) => m * (1 - m), muOk: (m) => m > 0 && m < 1, dev: binomialDev };
183
219
  case "mu":
184
220
  return {
185
221
  variance: (m) => m,
186
222
  muOk: (m) => m > 0,
187
- dev: (y, mu, w) => 2 * w * ((y === 0 ? 0 : y * logWarn(y / mu)) - (y - mu)),
223
+ varPower: 1,
224
+ dev: poissonDev,
188
225
  };
189
226
  case "mu^2":
190
227
  return {
191
228
  variance: (m) => m * m,
192
229
  muOk: (m) => m > 0,
230
+ varPower: 2,
193
231
  // log(y/mu) for every y != 0 (NaN for a negative y, as in R), log(1/mu) for y = 0
194
- dev: (y, mu, w) => Math.max(-2 * w * (logWarn(y !== 0 ? y / mu : 1 / mu) - (y - mu) / mu), 0),
232
+ dev: (y, mu, w) => (nearMean(y, mu) ? gammaNear(y, mu, w) : Math.max(gammaFar(y, mu, w, y !== 0 ? y / mu : 1 / mu), 0)),
195
233
  };
196
234
  case "mu^3":
197
- return { variance: (m) => m * m * m, muOk: (m) => m > 0, dev: inverseGaussianDev };
235
+ return { variance: (m) => m * m * m, muOk: (m) => m > 0, dev: inverseGaussianDev, varPower: 3 };
198
236
  default:
199
237
  throw new RangeError(`'variance' "${String(v)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`);
200
238
  }
201
239
  }
202
- /** (R computes this deviance in compiled code, so a mean outside (0, 1) gives NaN without "NaNs produced") */
240
+ /**
241
+ * Whether y is within 10% of the mean (abs(y - mu) < 0.1 mu, so mu > 0), where the first-order terms of the poisson,
242
+ * binomial and Gamma deviances cancel: the direct forms lose about 2 eps / abs(t) relative (t = (y - mu)/mu; 1e-10 at
243
+ * t = 1e-3, and the sign at t = 2^-52, #161-#163), so there they are formed as bd0 (and log1pmx), as R's dpois_raw
244
+ * and dbinom_raw switch to bd0 for abs(x - np) < 0.1 (x + np). Beyond 10% the direct forms are good to about 20 ulp
245
+ * and kept, with R's bits
246
+ */
247
+ const nearMean = (y, mu) => Math.abs(y - mu) < 0.1 * mu;
248
+ /** The Gamma deviance -2 w (log(y/mu) - (y - mu)/mu) = -2 w log1pmx(t), t = (y - mu)/mu, for y near mu */
249
+ const gammaNear = (y, mu, w) => -2 * w * log1pmx((y - mu) / mu);
250
+ /**
251
+ * bd0(M + d, M) = (M + d) log(1 + d/M) - d = M (s log1p(s) + log1pmx(s)) with s = d/M, for abs(s) < 0.1: d is exact
252
+ * where M + d is not
253
+ */
254
+ function bd0Shift(M, d) {
255
+ const s = d / M;
256
+ return M * (s * Math.log1p(s) + log1pmx(s));
257
+ }
258
+ /** floor(log2 abs(x)) for a finite nonzero x (one too large just below a power of 2, which the scalings allow) */
259
+ const exponentOf = (x) => Math.floor(Math.log2(Math.abs(x)));
260
+ /**
261
+ * 2 w D 2^e for a deviance D formed in units of 2^e: w is brought to [1, 2) by its power of 2 first, so that neither
262
+ * w D nor D 2^e overflows or underflows on the way (#371)
263
+ */
264
+ function weightedScaled(w, D, e) {
265
+ if (w === 0)
266
+ return 0;
267
+ const ew = exponentOf(w);
268
+ return ldexp(2 * ldexp(w, -ew) * D, ew + e);
269
+ }
270
+ /**
271
+ * Whether a deviance 2 w D of positive finite y != mu and finite w >= 0 left the doubles on the way: D or the
272
+ * product is not finite, or D is below the normal range (where a large weight would bring back its lost bits)
273
+ */
274
+ const outOfRange = (y, mu, w, D, r) => !(Number.isFinite(r) && Math.abs(D) >= 2 ** -1022) &&
275
+ y > 0 && y < Infinity && mu > 0 && mu < Infinity && w >= 0 && w < Infinity && y !== mu;
276
+ /**
277
+ * The poisson deviance beyond 10% of the mean, where y/mu, y log(y/mu) or the product with w left the doubles
278
+ * (y = 1e308, mu = 1e-308, w = 1e-320 gave Infinity, #371): y and mu in units of 2^e (e from the larger), the
279
+ * logarithm of the ratio from the logarithms where the ratio itself is out of range (then abs(log) > 708, so their
280
+ * rounding is below 1e-15 of it)
281
+ */
282
+ function poissonFarScaled(y, mu, w) {
283
+ const e = exponentOf(Math.max(y, mu));
284
+ const ys = ldexp(y, -e);
285
+ const ms = ldexp(mu, -e);
286
+ const q = y / mu;
287
+ const L = q >= 2 ** -1022 && q < Infinity ? Math.log(q) : Math.log(y) - Math.log(mu);
288
+ return weightedScaled(w, ys * L - (ys - ms), e);
289
+ }
290
+ /**
291
+ * The Gamma deviance -2 w (log q - (y - mu)/mu) beyond 10% of the mean (q = y/mu; R's q for y = 0 is 1 for Gamma, 1/mu
292
+ * for quasi mu^2). Where y/mu overflows or underflows, or 2 w does (y = 1e308, mu = 1e-308, w = 1e-320 gave NaN, #372):
293
+ * 2 w (r - 1 - log r) with r = y/mu as m 2^E (m from y and mu brought to [1, 2)), log r = log m + E log 2, and for
294
+ * E > 0 in units of 2^E, the weight's exponent taken out
295
+ */
296
+ function gammaFar(y, mu, w, q) {
297
+ const D = logWarn(q) - (y - mu) / mu;
298
+ const r = -2 * w * D;
299
+ if (!outOfRange(y, mu, w, D, r))
300
+ return r;
301
+ const ey = exponentOf(y);
302
+ const E = ey - exponentOf(mu);
303
+ const m = ldexp(y, -ey) / ldexp(mu, -(ey - E));
304
+ const L = Math.log(m) + E * Math.LN2;
305
+ return E > 0 ? weightedScaled(w, m - ldexp(1 + L, -E), E) : weightedScaled(w, ldexp(m, E) - 1 - L, 0);
306
+ }
307
+ /** The poisson deviance 2 w (y log(y/mu) - (y - mu)); 2 w bd0(y, mu) for y near mu (nearMean) */
308
+ function poissonDev(y, mu, w) {
309
+ if (nearMean(y, mu)) {
310
+ const B = bd0(y, mu);
311
+ const r = 2 * w * B;
312
+ // (mu = 1e-300, y one ulp above, w = 1e308 gave Inf times 0 = NaN, #375): bd0 is homogeneous of degree 1, so in
313
+ // units of 2^e (e from mu, exact), the weight's exponent taken out
314
+ if (!outOfRange(y, mu, w, B, r))
315
+ return r;
316
+ const e = exponentOf(mu);
317
+ return weightedScaled(w, bd0(ldexp(y, -e), ldexp(mu, -e)), e);
318
+ }
319
+ const D = (y === 0 ? 0 : y * logWarn(y / mu)) - (y - mu);
320
+ const r = 2 * w * D;
321
+ return outOfRange(y, mu, w, D, r) ? poissonFarScaled(y, mu, w) : r;
322
+ }
323
+ /**
324
+ * The binomial deviance near the mean where 2 w overflows or the unweighted deviance underflows (mu = 1e-300, y one ulp
325
+ * above, w = 1e308 gave Inf times 0 = NaN, #373): bd0 is homogeneous of degree 1, so the first term is formed with y
326
+ * and mu in units of 2^e (e from mu, exact) and the second in the same units, the weight's exponent taken out
327
+ */
328
+ function binomialNearScaled(y, mu, w) {
329
+ const e = exponentOf(mu);
330
+ return weightedScaled(w, bd0(ldexp(y, -e), ldexp(mu, -e)) + ldexp(bd0Shift(1 - mu, mu - y), -e), e);
331
+ }
332
+ /**
333
+ * (R computes this deviance in compiled code, so a mean outside (0, 1) gives NaN without "NaNs produced"). For y
334
+ * within 10% of mu and of 1 - mu (as nearMean), 2 w (bd0(y, mu) + bd0(1 - y, 1 - mu)), the second term from the exact
335
+ * difference mu - y (bd0Shift): 1 - y is rounded for y < 1/2 (1 - 0.3 by 5.6e-17), which is 1e-13 of the deviance at
336
+ * y = 0.3, mu = 0.3003. For y = 0 and mu < 0.1, -log(1 - mu) is -log1p(-mu): 1 - mu is rounded (to 1 for mu below
337
+ * 1.1e-16, where the contribution 2 w mu was 0, R too), a relative error of about eps/(2 mu); from mu = 0.1 on, R's bits
338
+ */
203
339
  function binomialDev(y, mu, w) {
340
+ if (Math.abs(y - mu) < 0.1 * Math.min(mu, 1 - mu)) {
341
+ const D = bd0(y, mu) + bd0Shift(1 - mu, mu - y);
342
+ const r = 2 * w * D;
343
+ return outOfRange(y, mu, w, D, r) ? binomialNearScaled(y, mu, w) : r;
344
+ }
204
345
  const a = y === 0 ? 0 : y * Math.log(y / mu);
205
- const b = y === 1 ? 0 : (1 - y) * Math.log((1 - y) / (1 - mu));
346
+ const b = y === 1 ? 0 : y === 0 && mu < 0.1 ? -Math.log1p(-mu) : (1 - y) * Math.log((1 - y) / (1 - mu));
206
347
  return 2 * w * (a + b);
207
348
  }
208
349
  function inverseGaussianDev(y, mu, w) {
209
- return (w * (y - mu) ** 2) / (y * mu * mu);
350
+ const den = y * mu * mu;
351
+ const r = (w * (y - mu) ** 2) / den;
352
+ if (Number.isFinite(r) && !(Math.abs(den) < 2 ** -1022))
353
+ return r;
354
+ // ((y - mu)² or y mu² overflows, or y mu² is below the normal range: as ((y - mu)/mu)²/y)
355
+ const d = (y - mu) / mu;
356
+ const v = w * ((d * d) / y);
357
+ if (Number.isFinite(v) || !(w > 0 && w < Infinity && y > 0 && y < Infinity && mu > 0 && mu < Infinity))
358
+ return v;
359
+ // (d, d² or d²/y overflows: y = 1e200, mu = 1e-200, w = 1e-300 gave Inf for 1e300, #480) w, y - mu, y and mu brought
360
+ // to [1, 2) by their powers of 2, which are added back last
361
+ const t = y - mu;
362
+ const [ew, et, ey, em] = [exponentOf(w), exponentOf(t), exponentOf(y), exponentOf(mu)];
363
+ const ft = ldexp(t, -et);
364
+ const fm = ldexp(mu, -em);
365
+ return ldexp(ldexp(w, -ew) * ((ft * ft) / (ldexp(y, -ey) * fm * fm)), ew + 2 * et - ey - 2 * em);
210
366
  }
211
367
  /** Resolves a family name or specification (and a link given separately) to its functions */
212
368
  export function makeFamily(family, link) {
@@ -237,7 +393,7 @@ export function makeFamily(family, link) {
237
393
  const base = { ...lf, family: name, varianceName: undefined, fixedDispersion: false };
238
394
  switch (name) {
239
395
  case "gaussian":
240
- return { ...base, variance: () => 1, muOk: () => true, devContribution: (y, mu, w) => w * (y - mu) ** 2 };
396
+ return { ...base, varPower: 0, variance: () => 1, muOk: () => true, devContribution: (y, mu, w) => w * (y - mu) ** 2 };
241
397
  case "binomial":
242
398
  case "quasibinomial":
243
399
  return {
@@ -252,24 +408,41 @@ export function makeFamily(family, link) {
252
408
  return {
253
409
  ...base,
254
410
  fixedDispersion: name === "poisson",
411
+ varPower: 1,
255
412
  variance: (m) => m,
256
413
  muOk: (m) => Number.isFinite(m) && m > 0,
257
414
  // (a row with y = 0 takes no logarithm, so a negative mean there does not warn, as in R)
258
- devContribution: (y, mu, w) => (y > 0 ? 2 * w * (y * logWarn(y / mu) - (y - mu)) : 2 * w * mu),
415
+ devContribution: (y, mu, w) => (y > 0 ? poissonDev(y, mu, w) : 2 * w * mu),
259
416
  };
260
417
  case "Gamma":
261
418
  return {
262
419
  ...base,
420
+ varPower: 2,
263
421
  variance: (m) => m * m,
264
422
  muOk: (m) => Number.isFinite(m) && m > 0,
265
- devContribution: (y, mu, w) => -2 * w * ((y === 0 ? 0 : logWarn(y / mu)) - (y - mu) / mu),
423
+ devContribution: (y, mu, w) => (nearMean(y, mu) ? gammaNear(y, mu, w) : gammaFar(y, mu, w, y === 0 ? 1 : y / mu)),
266
424
  };
267
425
  case "inverse.gaussian":
268
- return { ...base, variance: (m) => m * m * m, muOk: () => true, devContribution: inverseGaussianDev };
426
+ // the mean of an inverse Gaussian is positive (R's validmu accepts any mean, and a negative one turns the
427
+ // working weights NaN); a step to an invalid mean is halved
428
+ return {
429
+ ...base,
430
+ varPower: 3,
431
+ variance: (m) => m * m * m,
432
+ muOk: (m) => Number.isFinite(m) && m > 0,
433
+ devContribution: inverseGaussianDev,
434
+ };
269
435
  case "quasi": {
270
436
  const vname = spec.variance ?? "constant";
271
437
  const q = quasiVariance(vname);
272
- return { ...base, varianceName: vname, variance: q.variance, muOk: q.muOk, devContribution: q.dev };
438
+ return {
439
+ ...base,
440
+ varianceName: vname,
441
+ variance: q.variance,
442
+ muOk: q.muOk,
443
+ devContribution: q.dev,
444
+ ...(q.varPower === undefined ? {} : { varPower: q.varPower }),
445
+ };
273
446
  }
274
447
  }
275
448
  }
@@ -295,7 +468,16 @@ export function prepareResponse(fam, y, prior, hasStart, warnings) {
295
468
  if (name === "binomial" && rows.some((r) => Math.abs(r[0] - Math.round(r[0])) > 1e-3 || Math.abs(r[1] - Math.round(r[1])) > 1e-3)) {
296
469
  warnings.add("non-integer counts in a binomial glm!");
297
470
  }
471
+ if (rows.some((r) => r[0] < 0 || r[1] < 0))
472
+ throw new RangeError("counts must be nonnegative");
298
473
  const size = rows.map((r) => r[0] + r[1]);
474
+ // Finite counts whose total overflows: the proportion could be formed (s/2 over s/2 + f/2), but the binomial size,
475
+ // the prior weight that the deviance, the AIC and the covariance are proportional to, is not a double (R stops with
476
+ // "cannot find valid starting values", the proportion 0 and the starting mean NaN; #160)
477
+ const over = rows.findIndex((r, i) => Number.isFinite(r[0]) && Number.isFinite(r[1]) && !Number.isFinite(size[i]));
478
+ if (over >= 0) {
479
+ throw new RangeError(`the total of successes and failures overflows in row ${over + 1} (${formatG(rows[over][0])} + ${formatG(rows[over][1])}): the binomial size must be a finite double`);
480
+ }
299
481
  const yy = rows.map((r, i) => (size[i] === 0 ? 0 : r[0] / size[i]));
300
482
  const weights = prior.map((w, i) => w * size[i]);
301
483
  const mustart = yy.map((v, i) => (size[i] * v + 0.5) / (size[i] + 1));
@@ -346,12 +528,22 @@ export function prepareResponse(fam, y, prior, hasStart, warnings) {
346
528
  export function familyAic(fam, y, mu, w, size, dev, warnings) {
347
529
  const n = y.length;
348
530
  let s = 0;
531
+ // Rows of weight 0 are not in the likelihood: left out of every sum (R's gaussian AIC took their log(0) = -Inf and
532
+ // counted them in n, so it was Inf; 0 times an infinite log density was NaN elsewhere)
349
533
  switch (fam.family) {
350
534
  case "gaussian": {
351
535
  let slw = 0;
352
- for (const wi of w)
536
+ let nPos = 0;
537
+ for (const wi of w) {
538
+ if (!(wi > 0))
539
+ continue;
353
540
  slw += Math.log(wi);
354
- return n * (Math.log((2 * Math.PI * dev) / n) + 1) + 2 - slw;
541
+ nPos++;
542
+ }
543
+ // log(2 pi dev / n) as log(2 pi) + log(dev / n) only where 2 pi dev overflows although dev is finite (#189)
544
+ const q = (2 * Math.PI * dev) / nPos;
545
+ const logQ = q === Infinity && dev < Infinity ? Math.log(2 * Math.PI) + Math.log(dev / nPos) : Math.log(q);
546
+ return nPos * (logQ + 1) + 2 - slw;
355
547
  }
356
548
  case "binomial": {
357
549
  const m = size !== undefined && size.some((v) => v > 1) ? size : w;
@@ -364,6 +556,8 @@ export function familyAic(fam, y, mu, w, size, dev, warnings) {
364
556
  }
365
557
  case "poisson":
366
558
  for (let i = 0; i < n; i++) {
559
+ if (!(w[i] > 0))
560
+ continue;
367
561
  if (nonInteger(y[i]))
368
562
  warnings.add(`non-integer x = ${y[i].toFixed(6)}`);
369
563
  s += w[i] * dpois1(y[i], mu[i], true);
@@ -375,17 +569,27 @@ export function familyAic(fam, y, mu, w, size, dev, warnings) {
375
569
  sw += wi;
376
570
  const disp = dev / sw;
377
571
  for (let i = 0; i < n; i++)
378
- s += w[i] * dgamma1(y[i], 1 / disp, mu[i] * disp, true);
572
+ if (w[i] > 0)
573
+ s += w[i] * dgamma1(y[i], 1 / disp, mu[i] * disp, true);
379
574
  return -2 * s + 2;
380
575
  }
381
576
  case "inverse.gaussian": {
382
577
  let sw = 0;
383
578
  let sly = 0;
384
579
  for (let i = 0; i < n; i++) {
580
+ if (!(w[i] > 0))
581
+ continue;
385
582
  sw += w[i];
386
583
  sly += w[i] * Math.log(y[i]);
387
584
  }
388
- return sw * (Math.log((2 * Math.PI * dev) / sw) + 1) + 3 * sly + 2;
585
+ // log(2 pi dev / sw) as log(2 pi) + log(dev / sw) only where 2 pi dev overflows although dev is finite (sw =
586
+ // 1e308, dev = 3e307 gave Inf, #479; as gaussian, #189), and as log(dev) - log(sw) where dev / sw overflows too
587
+ const q = (2 * Math.PI * dev) / sw;
588
+ const ratio = dev / sw;
589
+ const logQ = q === Infinity && dev < Infinity
590
+ ? Math.log(2 * Math.PI) + (ratio < Infinity ? Math.log(ratio) : Math.log(dev) - Math.log(sw))
591
+ : Math.log(q);
592
+ return sw * (logQ + 1) + 3 * sly + 2;
389
593
  }
390
594
  default:
391
595
  return NaN;
@@ -1,4 +1,4 @@
1
- import type { MatrixInput } from "../linalg/matrix.js";
1
+ import { type MatrixInput } from "../linalg/matrix.js";
2
2
  import { type QR } from "../linalg/qr.js";
3
3
  import type { Sample } from "../htest/types.js";
4
4
  import type { DataFrame } from "./modelFrame.js";
@@ -165,10 +165,17 @@ export interface LmCore {
165
165
  effects: number[];
166
166
  /** The weighted residual sum of squares */
167
167
  rss: number;
168
+ /** k when the response was fitted scaled by 2^-k, and the residual sum of squares of that scaled fit */
169
+ yScale?: number;
170
+ rssScaled?: number;
168
171
  }
169
172
  /**
170
173
  * Least squares on a full model matrix (the column of ones included when `intercept` is true). Rows of weight 0 are
171
174
  * left out of the fit and of the counts.
175
+ *
176
+ * The response is scaled by a power of 2, 2^-k, before the fit (exact; see src/internal/summary.ts), so that its sums
177
+ * of squares stay in range: t, F, R^2 and the p-values come from the scaled fit, and the coefficients, residuals,
178
+ * fitted values, effects, sigma and standard errors are multiplied by 2^k, the residual sum of squares by 2^2k.
172
179
  */
173
180
  export declare function fitLm(Xall: number[][], Yall: number[], Wall: number[] | undefined, intercept: boolean, precise?: boolean): LmCore;
174
181
  /**
@@ -234,4 +241,8 @@ export declare function predict(fit: LmResult, newdata: MatrixInput | Sample, op
234
241
  lwr?: number[];
235
242
  /** Upper limits of the intervals (absent with `interval: "none"`). */
236
243
  upr?: number[];
244
+ /** 0-based rows of `newdata` whose prediction is not estimable from a rank-deficient fit (R's `attr(, "non-estim")`); present only when there are some. */
245
+ nonEstimable?: number[];
246
+ /** R's warning for those rows; present only when there are some. */
247
+ warnings?: string[];
237
248
  };