@numero-uno-jpn/rstats 0.15.3 → 0.17.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 (322) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/README.ja.md +68 -35
  3. package/README.md +65 -30
  4. package/dist/chunks/chunk-2C33MXSW.js +3 -0
  5. package/dist/chunks/chunk-34S5SQY6.js +2 -0
  6. package/dist/chunks/chunk-4WE3OO62.js +2 -0
  7. package/dist/chunks/chunk-5AYHCITP.js +2 -0
  8. package/dist/chunks/chunk-6JJQL6HL.js +2 -0
  9. package/dist/chunks/chunk-C7V2FH6L.js +3 -0
  10. package/dist/chunks/chunk-CZI7KUO3.js +2 -0
  11. package/dist/chunks/chunk-DXI5N2ET.js +2 -0
  12. package/dist/chunks/chunk-FY3FVOBM.js +2 -0
  13. package/dist/chunks/chunk-H3WVPZDL.js +2 -0
  14. package/dist/chunks/chunk-LHUIJQHY.js +2 -0
  15. package/dist/chunks/chunk-LV3W2F7N.js +2 -0
  16. package/dist/chunks/chunk-M4ITG7YR.js +2 -0
  17. package/dist/chunks/chunk-NSYZTS26.js +2 -0
  18. package/dist/chunks/chunk-ONY5CISH.js +2 -0
  19. package/dist/chunks/chunk-RFE7MVP7.js +2 -0
  20. package/dist/chunks/chunk-SE7V2KLO.js +2 -0
  21. package/dist/chunks/chunk-THIGHOKK.js +2 -0
  22. package/dist/chunks/chunk-TNMJPQPV.js +2 -0
  23. package/dist/chunks/chunk-WM7VFXTV.js +2 -0
  24. package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
  25. package/dist/chunks/chunk-XLSFJTTK.js +2 -0
  26. package/dist/chunks/chunk-Z37NELOS.js +2 -0
  27. package/dist/chunks/chunk-ZFQWB222.js +2 -0
  28. package/dist/data/grouping.js +26 -7
  29. package/dist/descriptive/basic.js +110 -8
  30. package/dist/descriptive/cov.js +23 -4
  31. package/dist/descriptive/dataFrames.d.ts +4 -2
  32. package/dist/descriptive/dataFrames.js +69 -23
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +26 -9
  35. package/dist/descriptive/hist.d.ts +8 -5
  36. package/dist/descriptive/hist.js +77 -31
  37. package/dist/descriptive/missing.js +3 -1
  38. package/dist/descriptive/moments.js +86 -19
  39. package/dist/descriptive/omega.d.ts +1 -1
  40. package/dist/descriptive/omega.js +74 -10
  41. package/dist/descriptive/pivot.d.ts +25 -10
  42. package/dist/descriptive/pivot.js +182 -42
  43. package/dist/descriptive/quantile.js +10 -1
  44. package/dist/descriptive/rank.js +8 -1
  45. package/dist/descriptive/table.d.ts +23 -3
  46. package/dist/descriptive/table.js +36 -21
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.js +27 -2
  49. package/dist/distributions/beta.js +33 -4
  50. package/dist/distributions/binomial.js +46 -14
  51. package/dist/distributions/bvnorm.js +11 -2
  52. package/dist/distributions/cauchy.js +21 -4
  53. package/dist/distributions/chisq.js +16 -3
  54. package/dist/distributions/exponential.js +14 -0
  55. package/dist/distributions/f.js +41 -17
  56. package/dist/distributions/gamma.js +35 -3
  57. package/dist/distributions/geometric.js +58 -13
  58. package/dist/distributions/hypergeometric.d.ts +5 -1
  59. package/dist/distributions/hypergeometric.js +101 -6
  60. package/dist/distributions/invgamma.js +10 -1
  61. package/dist/distributions/kumaraswamy.js +9 -0
  62. package/dist/distributions/laplace.js +36 -6
  63. package/dist/distributions/logistic.js +13 -1
  64. package/dist/distributions/lognormal.js +16 -2
  65. package/dist/distributions/multinomial.js +58 -11
  66. package/dist/distributions/nbeta.js +62 -11
  67. package/dist/distributions/nchisq.js +30 -6
  68. package/dist/distributions/negbinomial.js +102 -17
  69. package/dist/distributions/normal.js +17 -4
  70. package/dist/distributions/nt.js +392 -17
  71. package/dist/distributions/pareto.js +12 -1
  72. package/dist/distributions/poisson.js +37 -3
  73. package/dist/distributions/signrank.js +16 -4
  74. package/dist/distributions/summary.d.ts +2 -1
  75. package/dist/distributions/summary.js +481 -61
  76. package/dist/distributions/t.js +9 -0
  77. package/dist/distributions/triangular.js +19 -5
  78. package/dist/distributions/tukey.js +11 -2
  79. package/dist/distributions/uniform.js +8 -0
  80. package/dist/distributions/weibull.js +18 -0
  81. package/dist/distributions/wilcox.d.ts +9 -3
  82. package/dist/distributions/wilcox.js +164 -26
  83. package/dist/distributions.js +2 -2
  84. package/dist/entries/descriptive.d.ts +3 -1
  85. package/dist/entries/descriptive.js +1 -1
  86. package/dist/entries/distributions.d.ts +1 -0
  87. package/dist/entries/distributions.js +2 -0
  88. package/dist/entries/linalg.d.ts +3 -1
  89. package/dist/entries/mixed.d.ts +3 -0
  90. package/dist/entries/mixed.js +2 -0
  91. package/dist/entries/numerics.d.ts +2 -1
  92. package/dist/entries/tests.d.ts +8 -5
  93. package/dist/entries/tests.js +3 -1
  94. package/dist/htest/agreement.d.ts +16 -2
  95. package/dist/htest/agreement.js +122 -21
  96. package/dist/htest/ansariTest.d.ts +8 -2
  97. package/dist/htest/ansariTest.js +116 -18
  98. package/dist/htest/aov.d.ts +29 -23
  99. package/dist/htest/aov.js +90 -47
  100. package/dist/htest/binomTest.d.ts +5 -0
  101. package/dist/htest/binomTest.js +47 -17
  102. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  103. package/dist/htest/brunnerMunzelTest.js +12 -5
  104. package/dist/htest/chisqTest.d.ts +5 -2
  105. package/dist/htest/chisqTest.js +151 -46
  106. package/dist/htest/cor.d.ts +9 -2
  107. package/dist/htest/cor.js +57 -26
  108. package/dist/htest/core/ansari.d.ts +9 -0
  109. package/dist/htest/core/ansari.js +21 -1
  110. package/dist/htest/core/kolmogorov.d.ts +25 -0
  111. package/dist/htest/core/kolmogorov.js +123 -0
  112. package/dist/htest/core/mantelhaen.js +6 -4
  113. package/dist/htest/core/shapiro.d.ts +1 -1
  114. package/dist/htest/core/shapiro.js +5 -1
  115. package/dist/htest/core/simulate.d.ts +6 -0
  116. package/dist/htest/core/simulate.js +20 -0
  117. package/dist/htest/dunnett.js +14 -5
  118. package/dist/htest/effectSize.d.ts +5 -0
  119. package/dist/htest/effectSize.js +117 -35
  120. package/dist/htest/fisherTest.d.ts +4 -3
  121. package/dist/htest/fisherTest.js +88 -24
  122. package/dist/htest/groups.d.ts +7 -0
  123. package/dist/htest/groups.js +21 -4
  124. package/dist/htest/ksTest.d.ts +7 -1
  125. package/dist/htest/ksTest.js +36 -6
  126. package/dist/htest/leveneTest.d.ts +2 -0
  127. package/dist/htest/leveneTest.js +28 -6
  128. package/dist/htest/mantelhaenTest.js +132 -25
  129. package/dist/htest/mcnemarTest.js +40 -11
  130. package/dist/htest/multipleComparisons.d.ts +24 -26
  131. package/dist/htest/multipleComparisons.js +70 -64
  132. package/dist/htest/normalityTests.js +54 -14
  133. package/dist/htest/onewayTest.js +106 -5
  134. package/dist/htest/pAdjust.d.ts +3 -2
  135. package/dist/htest/pAdjust.js +42 -6
  136. package/dist/htest/pairwise.d.ts +3 -1
  137. package/dist/htest/pairwise.js +46 -21
  138. package/dist/htest/poissonTest.d.ts +1 -1
  139. package/dist/htest/poissonTest.js +117 -59
  140. package/dist/htest/polycor.js +47 -17
  141. package/dist/htest/power.d.ts +15 -1
  142. package/dist/htest/power.js +128 -10
  143. package/dist/htest/propTest.d.ts +3 -0
  144. package/dist/htest/propTest.js +44 -6
  145. package/dist/htest/pwr.d.ts +38 -1
  146. package/dist/htest/pwr.js +159 -39
  147. package/dist/htest/rankTests.js +36 -6
  148. package/dist/htest/tTest.d.ts +2 -0
  149. package/dist/htest/tTest.js +53 -21
  150. package/dist/htest/testFormula.js +11 -11
  151. package/dist/htest/trendTests.d.ts +28 -4
  152. package/dist/htest/trendTests.js +124 -35
  153. package/dist/htest/types.d.ts +7 -0
  154. package/dist/htest/types.js +7 -0
  155. package/dist/htest/varTest.js +52 -14
  156. package/dist/htest/wilcoxTest.js +164 -29
  157. package/dist/integrate/quadpack.js +27 -5
  158. package/dist/internal/checkOptions.d.ts +11 -0
  159. package/dist/internal/checkOptions.js +31 -0
  160. package/dist/internal/covmat.d.ts +5 -0
  161. package/dist/internal/covmat.js +20 -0
  162. package/dist/internal/ddexp.d.ts +15 -1
  163. package/dist/internal/ddexp.js +59 -6
  164. package/dist/internal/discrete.d.ts +14 -0
  165. package/dist/internal/discrete.js +78 -5
  166. package/dist/internal/extremes.d.ts +9 -0
  167. package/dist/internal/extremes.js +24 -0
  168. package/dist/internal/fround.js +13 -4
  169. package/dist/internal/integer.d.ts +9 -0
  170. package/dist/internal/integer.js +12 -0
  171. package/dist/internal/invert.js +15 -2
  172. package/dist/internal/optionalArgs.d.ts +12 -0
  173. package/dist/internal/optionalArgs.js +46 -0
  174. package/dist/internal/scaledInverse.d.ts +10 -0
  175. package/dist/internal/scaledInverse.js +22 -0
  176. package/dist/internal/seq.js +15 -2
  177. package/dist/internal/summary.d.ts +90 -0
  178. package/dist/internal/summary.js +458 -39
  179. package/dist/internal/uniroot.d.ts +7 -0
  180. package/dist/internal/uniroot.js +28 -3
  181. package/dist/internal/warnings.d.ts +66 -0
  182. package/dist/internal/warnings.js +122 -0
  183. package/dist/linalg/basic.d.ts +3 -1
  184. package/dist/linalg/basic.js +58 -7
  185. package/dist/linalg/chol.js +10 -1
  186. package/dist/linalg/dot.d.ts +29 -0
  187. package/dist/linalg/dot.js +132 -0
  188. package/dist/linalg/eigen.d.ts +6 -1
  189. package/dist/linalg/eigen.js +75 -14
  190. package/dist/linalg/expm.d.ts +3 -2
  191. package/dist/linalg/expm.js +113 -15
  192. package/dist/linalg/kappa.js +64 -10
  193. package/dist/linalg/lu.d.ts +2 -1
  194. package/dist/linalg/lu.js +40 -5
  195. package/dist/linalg/matrix.d.ts +14 -0
  196. package/dist/linalg/matrix.js +24 -0
  197. package/dist/linalg/norm.js +4 -0
  198. package/dist/linalg/ops.d.ts +27 -2
  199. package/dist/linalg/ops.js +74 -26
  200. package/dist/linalg/qr.d.ts +2 -1
  201. package/dist/linalg/qr.js +105 -23
  202. package/dist/linalg/schur.js +11 -3
  203. package/dist/linalg/solve.d.ts +6 -1
  204. package/dist/linalg/solve.js +156 -26
  205. package/dist/linalg/svd.d.ts +9 -2
  206. package/dist/linalg/svd.js +33 -11
  207. package/dist/linalg.js +2 -2
  208. package/dist/mixed.js +4 -4
  209. package/dist/models/anovaSequential.d.ts +32 -2
  210. package/dist/models/anovaSequential.js +170 -28
  211. package/dist/models/aovEz.js +154 -39
  212. package/dist/models/aovStrata.js +68 -24
  213. package/dist/models/carAnova.d.ts +1 -1
  214. package/dist/models/carAnova.js +78 -33
  215. package/dist/models/coxph.d.ts +6 -0
  216. package/dist/models/coxph.js +166 -69
  217. package/dist/models/estimable.d.ts +15 -0
  218. package/dist/models/estimable.js +68 -0
  219. package/dist/models/formula.d.ts +1 -1
  220. package/dist/models/formula.js +26 -8
  221. package/dist/models/glm.d.ts +57 -7
  222. package/dist/models/glm.js +427 -74
  223. package/dist/models/glmAnova.js +90 -14
  224. package/dist/models/glmConfint.d.ts +1 -1
  225. package/dist/models/glmConfint.js +121 -29
  226. package/dist/models/glmFamily.d.ts +11 -6
  227. package/dist/models/glmFamily.js +247 -62
  228. package/dist/models/lm.d.ts +12 -1
  229. package/dist/models/lm.js +246 -37
  230. package/dist/models/lmer.d.ts +31 -7
  231. package/dist/models/lmer.js +129 -42
  232. package/dist/models/lmerAnova.d.ts +3 -3
  233. package/dist/models/lmerAnova.js +47 -9
  234. package/dist/models/lmerHypotheses.d.ts +6 -2
  235. package/dist/models/lmerHypotheses.js +14 -2
  236. package/dist/models/lmerTest.d.ts +13 -6
  237. package/dist/models/lmerTest.js +82 -29
  238. package/dist/models/mauchly.d.ts +6 -1
  239. package/dist/models/mauchly.js +41 -15
  240. package/dist/models/mixedStructure.js +6 -0
  241. package/dist/models/modelFrame.js +10 -4
  242. package/dist/models/modelMatrix.d.ts +3 -1
  243. package/dist/models/modelMatrix.js +47 -17
  244. package/dist/models/scaledTables.d.ts +8 -0
  245. package/dist/models/scaledTables.js +6 -0
  246. package/dist/models/survival.d.ts +12 -1
  247. package/dist/models/survival.js +75 -28
  248. package/dist/models/tiedTimes.d.ts +2 -0
  249. package/dist/models/tiedTimes.js +34 -0
  250. package/dist/numerics/deriv.d.ts +1 -1
  251. package/dist/numerics/deriv.js +72 -8
  252. package/dist/numerics/integrate.d.ts +9 -1
  253. package/dist/numerics/integrate.js +111 -16
  254. package/dist/numerics/interp.d.ts +40 -9
  255. package/dist/numerics/interp.js +375 -55
  256. package/dist/numerics/ode.js +59 -1
  257. package/dist/numerics.js +2 -2
  258. package/dist/optim/localmin.js +4 -0
  259. package/dist/optim/optim.d.ts +3 -1
  260. package/dist/optim/optim.js +97 -10
  261. package/dist/random/beta.js +58 -4
  262. package/dist/random/binomial.d.ts +14 -3
  263. package/dist/random/binomial.js +59 -24
  264. package/dist/random/continuous.js +50 -5
  265. package/dist/random/extra.js +55 -9
  266. package/dist/random/gamma.d.ts +4 -1
  267. package/dist/random/gamma.js +47 -8
  268. package/dist/random/hyper.js +9 -6
  269. package/dist/random/mt.js +17 -2
  270. package/dist/random/poisson.js +31 -3
  271. package/dist/random/r2dtable.d.ts +8 -1
  272. package/dist/random/r2dtable.js +24 -3
  273. package/dist/random/rank.d.ts +2 -0
  274. package/dist/random/rank.js +44 -4
  275. package/dist/random/rng.js +4 -0
  276. package/dist/random/sample.d.ts +12 -2
  277. package/dist/random/sample.js +63 -20
  278. package/dist/random/streams.js +4 -2
  279. package/dist/rstats.js +17 -14
  280. package/dist/special/besselIK.d.ts +12 -0
  281. package/dist/special/besselIK.js +175 -23
  282. package/dist/special/besselJY.d.ts +11 -1
  283. package/dist/special/besselJY.js +127 -19
  284. package/dist/special/besselSeries.d.ts +27 -0
  285. package/dist/special/besselSeries.js +45 -2
  286. package/dist/special/beta.js +4 -1
  287. package/dist/special/choose.js +24 -8
  288. package/dist/special/dd.js +122 -18
  289. package/dist/special/extra.js +14 -2
  290. package/dist/special/gamma.js +11 -2
  291. package/dist/special/incbet.js +30 -4
  292. package/dist/special/incbetTemme.d.ts +7 -0
  293. package/dist/special/incbetTemme.js +45 -12
  294. package/dist/special/loader.d.ts +1 -1
  295. package/dist/special/loader.js +117 -20
  296. package/dist/special/log1pmx.js +4 -0
  297. package/dist/special/psigamma.js +7 -0
  298. package/dist/special.js +2 -2
  299. package/dist/tests.js +15 -11
  300. package/llms.txt +15 -6
  301. package/package.json +19 -10
  302. package/dist/chunks/chunk-3THFTB36.js +0 -2
  303. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  304. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  305. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  306. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  307. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  308. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  309. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  310. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  311. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  312. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  313. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  314. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  315. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  316. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  317. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  318. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  319. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  320. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  321. package/dist/chunks/chunk-VLUL2HFW.js +0 -3
  322. package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
@@ -10,7 +10,10 @@ 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 { warn } from "../internal/warnings.js";
15
+ import { bd0 } from "../special/loader.js";
16
+ import { log1pmx } from "../special/log1p.js";
14
17
  const ALLOWED = {
15
18
  gaussian: ["identity", "log", "inverse"],
16
19
  binomial: ["logit", "probit", "cauchit", "log", "cloglog"],
@@ -23,30 +26,10 @@ const ALLOWED = {
23
26
  };
24
27
  const positive = (eta) => Number.isFinite(eta) && eta > 0;
25
28
  const anyEta = () => true;
26
- /** Collects warnings in order of first appearance, each once (as the fixtures list R's warnings) */
27
- export class Warnings {
28
- list = [];
29
- add(message) {
30
- if (!this.list.includes(message))
31
- this.list.push(message);
32
- }
33
- }
34
- /** The warnings sink of the link functions that warn ("NaNs produced"); set while a fit runs */
35
- let currentWarnings;
36
- export function withWarnings(sink, f) {
37
- const saved = currentWarnings;
38
- currentWarnings = sink;
39
- try {
40
- return f();
41
- }
42
- finally {
43
- currentWarnings = saved;
44
- }
45
- }
46
29
  /** Square root that warns as R's sqrt does when it produces NaN */
47
30
  function sqrtWarn(x) {
48
31
  if (x < 0)
49
- currentWarnings?.add("NaNs produced");
32
+ warn("NaNs produced");
50
33
  return Math.sqrt(x);
51
34
  }
52
35
  /**
@@ -55,7 +38,7 @@ function sqrtWarn(x) {
55
38
  */
56
39
  function logWarn(x) {
57
40
  if (x < 0)
58
- currentWarnings?.add("NaNs produced");
41
+ warn("NaNs produced");
59
42
  return Math.log(x);
60
43
  }
61
44
  /** A number as C's printf("%g") writes it: 6 significant digits, fixed or scientific, trailing zeros dropped */
@@ -76,17 +59,22 @@ function logit(m) {
76
59
  return Math.log(m / (1 - m));
77
60
  }
78
61
  const LINK_NAMES = ["logit", "probit", "cauchit", "cloglog", "identity", "log", "sqrt", "1/mu^2", "inverse"];
62
+ /** A probability kept within (0, 1): the least double and the largest double below 1 at the ends */
63
+ const inUnit = (p) => Math.min(Math.max(p, Number.MIN_VALUE), 1 - 2 ** -53);
79
64
  function namedLink(name) {
80
65
  switch (name) {
81
66
  case "identity":
82
- return { name, link: (m) => m, linkinv: (e) => e, muEta: () => 1, etaOk: anyEta };
67
+ return { name, link: (m) => m, linkinv: (e) => e, muEta: () => 1, etaOk: anyEta, muPower: { c: 1, a: 0 } };
83
68
  case "log":
84
69
  return {
85
70
  name,
86
71
  link: Math.log,
87
- linkinv: (e) => Math.max(Math.exp(e), EPS),
88
- muEta: (e) => Math.max(Math.exp(e), EPS),
72
+ // 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
73
+ // the variance stays positive
74
+ linkinv: (e) => Math.max(Math.exp(e), Number.MIN_VALUE),
75
+ muEta: (e) => Math.max(Math.exp(e), Number.MIN_VALUE),
89
76
  etaOk: anyEta,
77
+ muPower: { c: 1, a: 1 },
90
78
  };
91
79
  case "inverse":
92
80
  return {
@@ -95,9 +83,10 @@ function namedLink(name) {
95
83
  linkinv: (e) => 1 / e,
96
84
  muEta: (e) => -1 / (e * e),
97
85
  etaOk: (e) => Number.isFinite(e) && e !== 0,
86
+ muPower: { c: -1, a: 2 },
98
87
  };
99
88
  case "sqrt":
100
- return { name, link: Math.sqrt, linkinv: (e) => e * e, muEta: (e) => 2 * e, etaOk: positive };
89
+ return { name, link: Math.sqrt, linkinv: (e) => e * e, muEta: (e) => 2 * e, etaOk: positive, muPower: { c: 2, a: 0.5 } };
101
90
  case "1/mu^2":
102
91
  return {
103
92
  name,
@@ -105,51 +94,59 @@ function namedLink(name) {
105
94
  linkinv: (e) => 1 / sqrtWarn(e),
106
95
  muEta: (e) => -1 / (2 * e ** 1.5),
107
96
  etaOk: positive,
97
+ muPower: { c: -0.5, a: 3 },
108
98
  };
109
99
  case "logit":
110
100
  return {
111
101
  name,
112
102
  link: logit,
103
+ // 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
104
+ // kept fits from their solution. Beyond 30 the exact values (as R's formula within), down to the least double
105
+ // and up to the largest double below 1: mu is never 0 or 1, so the variance stays positive
113
106
  linkinv: (e) => {
114
- const t = e < -30 ? EPS : e > 30 ? 1 / EPS : Math.exp(e);
115
- return t / (1 + t);
107
+ if (e > 30)
108
+ return Math.min(1 / (1 + Math.exp(-e)), 1 - 2 ** -53);
109
+ const t = Math.exp(e);
110
+ return Math.max(t / (1 + t), Number.MIN_VALUE);
116
111
  },
117
112
  muEta: (e) => {
118
- if (Math.abs(e) > 30)
119
- return EPS;
113
+ if (Math.abs(e) > 30) {
114
+ const z = Math.exp(-Math.abs(e));
115
+ return Math.max(z / ((1 + z) * (1 + z)), Number.MIN_VALUE);
116
+ }
120
117
  const t = Math.exp(e);
121
118
  return t / ((1 + t) * (1 + t));
122
119
  },
123
120
  etaOk: anyEta,
124
121
  };
125
- case "probit": {
126
- const c = -qnorm(EPS);
122
+ // probit, cauchit and cloglog: R clamps eta (or mu) to the values at eps and the derivative to eps (Phi(-10) =
123
+ // 7.6e-24 was 2.2e-16); here the exact values, kept within (0, 1) by the least double and the largest double
124
+ // below 1 as logit. Within R's range the same values
125
+ case "probit":
127
126
  return {
128
127
  name,
129
128
  link: (m) => qnorm(m),
130
- linkinv: (e) => pnorm(Math.min(Math.max(e, -c), c)),
131
- muEta: (e) => Math.max(dnorm(e), EPS),
129
+ linkinv: (e) => inUnit(pnorm(e)),
130
+ muEta: (e) => Math.max(dnorm(e), Number.MIN_VALUE),
132
131
  etaOk: anyEta,
133
132
  };
134
- }
135
- case "cauchit": {
136
- const c = -qcauchy1(EPS, 0, 1, true, false);
133
+ case "cauchit":
137
134
  return {
138
135
  name,
139
136
  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),
137
+ linkinv: (e) => inUnit(pcauchy1(e, 0, 1, true, false)),
138
+ muEta: (e) => Math.max(dcauchy1(e, 0, 1, false), Number.MIN_VALUE),
142
139
  etaOk: anyEta,
143
140
  };
144
- }
145
141
  case "cloglog":
146
142
  return {
147
143
  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),
144
+ // log1p: 1 - mu rounded to 1 for small means (log(-log(1 - 1e-20)) was -Infinity, R too)
145
+ link: (m) => Math.log(-Math.log1p(-m)),
146
+ linkinv: (e) => inUnit(-Math.expm1(-Math.exp(e))),
150
147
  muEta: (e) => {
151
148
  const t = Math.min(e, 700);
152
- return Math.max(Math.exp(t) * Math.exp(-Math.exp(t)), EPS);
149
+ return Math.max(Math.exp(t) * Math.exp(-Math.exp(t)), Number.MIN_VALUE);
153
150
  },
154
151
  etaOk: anyEta,
155
152
  };
@@ -169,44 +166,184 @@ function powerLink(lambda) {
169
166
  return {
170
167
  name: `mu^${Math.round(lambda * 1000) / 1000}`,
171
168
  link: (m) => m ** lambda,
172
- linkinv: (e) => Math.max(e ** inv, EPS),
173
- muEta: (e) => Math.max(inv * e ** (inv - 1), EPS),
169
+ // (R: pmax(..., eps), so a mean of 1e-16 was 2.2e-16)
170
+ // (1 ** Infinity is NaN in JavaScript, where 1/lambda overflows)
171
+ linkinv: (e) => Math.max(e === 1 ? 1 : e ** inv, Number.MIN_VALUE),
172
+ muEta: (e) => powerMuEta(e, lambda, inv),
174
173
  etaOk: positive,
174
+ // (eta^(1/lambda - 1) = mu^(1 - lambda); none where 1/lambda overflows)
175
+ ...(Number.isFinite(inv) ? { muPower: { c: inv, a: 1 - lambda } } : {}),
175
176
  };
176
177
  }
178
+ /**
179
+ * d mu/d eta = eta^(1/lambda - 1)/lambda of the power link. Where that form is not finite (1/lambda overflows, or
180
+ * eta^(1/lambda - 1) overflows against a tiny 1/lambda), as eta^(1/lambda)/(lambda eta), or from its logs
181
+ */
182
+ function powerMuEta(e, lambda, inv) {
183
+ const r = inv * e ** (inv - 1);
184
+ if (r < Infinity)
185
+ return Math.max(r, Number.MIN_VALUE);
186
+ if (e === 1)
187
+ return inv;
188
+ const a = e ** inv;
189
+ const b = lambda * e;
190
+ if (a > 0 && a < Infinity && b >= 2 ** -1022 && b < Infinity)
191
+ return Math.max(a / b, Number.MIN_VALUE);
192
+ return Math.max(Math.exp(inv * Math.log(e) - Math.log(lambda) - Math.log(e)), Number.MIN_VALUE);
193
+ }
177
194
  function quasiVariance(v) {
178
195
  switch (v) {
179
196
  case "constant":
180
- return { variance: () => 1, muOk: () => true, dev: (y, mu, w) => w * (y - mu) ** 2 };
197
+ return { variance: () => 1, muOk: () => true, dev: (y, mu, w) => w * (y - mu) ** 2, varPower: 0 };
181
198
  case "mu(1-mu)":
182
199
  return { variance: (m) => m * (1 - m), muOk: (m) => m > 0 && m < 1, dev: binomialDev };
183
200
  case "mu":
184
201
  return {
185
202
  variance: (m) => m,
186
203
  muOk: (m) => m > 0,
187
- dev: (y, mu, w) => 2 * w * ((y === 0 ? 0 : y * logWarn(y / mu)) - (y - mu)),
204
+ varPower: 1,
205
+ dev: poissonDev,
188
206
  };
189
207
  case "mu^2":
190
208
  return {
191
209
  variance: (m) => m * m,
192
210
  muOk: (m) => m > 0,
211
+ varPower: 2,
193
212
  // 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),
213
+ 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
214
  };
196
215
  case "mu^3":
197
- return { variance: (m) => m * m * m, muOk: (m) => m > 0, dev: inverseGaussianDev };
216
+ return { variance: (m) => m * m * m, muOk: (m) => m > 0, dev: inverseGaussianDev, varPower: 3 };
198
217
  default:
199
218
  throw new RangeError(`'variance' "${String(v)}" is invalid: possible values are "mu(1-mu)", "mu", "mu^2", "mu^3" and "constant"`);
200
219
  }
201
220
  }
202
- /** (R computes this deviance in compiled code, so a mean outside (0, 1) gives NaN without "NaNs produced") */
221
+ /**
222
+ * 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,
223
+ * binomial and Gamma deviances cancel: the direct forms lose about 2 eps / abs(t) relative (t = (y - mu)/mu; 1e-10 at
224
+ * 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
225
+ * 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
226
+ * and kept, with R's bits
227
+ */
228
+ const nearMean = (y, mu) => Math.abs(y - mu) < 0.1 * mu;
229
+ /** The Gamma deviance -2 w (log(y/mu) - (y - mu)/mu) = -2 w log1pmx(t), t = (y - mu)/mu, for y near mu */
230
+ const gammaNear = (y, mu, w) => -2 * w * log1pmx((y - mu) / mu);
231
+ /**
232
+ * 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
233
+ * where M + d is not
234
+ */
235
+ function bd0Shift(M, d) {
236
+ const s = d / M;
237
+ return M * (s * Math.log1p(s) + log1pmx(s));
238
+ }
239
+ /** floor(log2 abs(x)) for a finite nonzero x (one too large just below a power of 2, which the scalings allow) */
240
+ const exponentOf = (x) => Math.floor(Math.log2(Math.abs(x)));
241
+ /**
242
+ * 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
243
+ * w D nor D 2^e overflows or underflows on the way (#371)
244
+ */
245
+ function weightedScaled(w, D, e) {
246
+ if (w === 0)
247
+ return 0;
248
+ const ew = exponentOf(w);
249
+ return ldexp(2 * ldexp(w, -ew) * D, ew + e);
250
+ }
251
+ /**
252
+ * Whether a deviance 2 w D of positive finite y != mu and finite w >= 0 left the doubles on the way: D or the
253
+ * product is not finite, or D is below the normal range (where a large weight would bring back its lost bits)
254
+ */
255
+ const outOfRange = (y, mu, w, D, r) => !(Number.isFinite(r) && Math.abs(D) >= 2 ** -1022) &&
256
+ y > 0 && y < Infinity && mu > 0 && mu < Infinity && w >= 0 && w < Infinity && y !== mu;
257
+ /**
258
+ * The poisson deviance beyond 10% of the mean, where y/mu, y log(y/mu) or the product with w left the doubles
259
+ * (y = 1e308, mu = 1e-308, w = 1e-320 gave Infinity, #371): y and mu in units of 2^e (e from the larger), the
260
+ * logarithm of the ratio from the logarithms where the ratio itself is out of range (then abs(log) > 708, so their
261
+ * rounding is below 1e-15 of it)
262
+ */
263
+ function poissonFarScaled(y, mu, w) {
264
+ const e = exponentOf(Math.max(y, mu));
265
+ const ys = ldexp(y, -e);
266
+ const ms = ldexp(mu, -e);
267
+ const q = y / mu;
268
+ const L = q >= 2 ** -1022 && q < Infinity ? Math.log(q) : Math.log(y) - Math.log(mu);
269
+ return weightedScaled(w, ys * L - (ys - ms), e);
270
+ }
271
+ /**
272
+ * 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
273
+ * 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):
274
+ * 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
275
+ * E > 0 in units of 2^E, the weight's exponent taken out
276
+ */
277
+ function gammaFar(y, mu, w, q) {
278
+ const D = logWarn(q) - (y - mu) / mu;
279
+ const r = -2 * w * D;
280
+ if (!outOfRange(y, mu, w, D, r))
281
+ return r;
282
+ const ey = exponentOf(y);
283
+ const E = ey - exponentOf(mu);
284
+ const m = ldexp(y, -ey) / ldexp(mu, -(ey - E));
285
+ const L = Math.log(m) + E * Math.LN2;
286
+ return E > 0 ? weightedScaled(w, m - ldexp(1 + L, -E), E) : weightedScaled(w, ldexp(m, E) - 1 - L, 0);
287
+ }
288
+ /** The poisson deviance 2 w (y log(y/mu) - (y - mu)); 2 w bd0(y, mu) for y near mu (nearMean) */
289
+ function poissonDev(y, mu, w) {
290
+ if (nearMean(y, mu)) {
291
+ const B = bd0(y, mu);
292
+ const r = 2 * w * B;
293
+ // (mu = 1e-300, y one ulp above, w = 1e308 gave Inf times 0 = NaN, #375): bd0 is homogeneous of degree 1, so in
294
+ // units of 2^e (e from mu, exact), the weight's exponent taken out
295
+ if (!outOfRange(y, mu, w, B, r))
296
+ return r;
297
+ const e = exponentOf(mu);
298
+ return weightedScaled(w, bd0(ldexp(y, -e), ldexp(mu, -e)), e);
299
+ }
300
+ const D = (y === 0 ? 0 : y * logWarn(y / mu)) - (y - mu);
301
+ const r = 2 * w * D;
302
+ return outOfRange(y, mu, w, D, r) ? poissonFarScaled(y, mu, w) : r;
303
+ }
304
+ /**
305
+ * The binomial deviance near the mean where 2 w overflows or the unweighted deviance underflows (mu = 1e-300, y one ulp
306
+ * above, w = 1e308 gave Inf times 0 = NaN, #373): bd0 is homogeneous of degree 1, so the first term is formed with y
307
+ * and mu in units of 2^e (e from mu, exact) and the second in the same units, the weight's exponent taken out
308
+ */
309
+ function binomialNearScaled(y, mu, w) {
310
+ const e = exponentOf(mu);
311
+ return weightedScaled(w, bd0(ldexp(y, -e), ldexp(mu, -e)) + ldexp(bd0Shift(1 - mu, mu - y), -e), e);
312
+ }
313
+ /**
314
+ * (R computes this deviance in compiled code, so a mean outside (0, 1) gives NaN without "NaNs produced"). For y
315
+ * 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
316
+ * 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
317
+ * 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
318
+ * 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
319
+ */
203
320
  function binomialDev(y, mu, w) {
321
+ if (Math.abs(y - mu) < 0.1 * Math.min(mu, 1 - mu)) {
322
+ const D = bd0(y, mu) + bd0Shift(1 - mu, mu - y);
323
+ const r = 2 * w * D;
324
+ return outOfRange(y, mu, w, D, r) ? binomialNearScaled(y, mu, w) : r;
325
+ }
204
326
  const a = y === 0 ? 0 : y * Math.log(y / mu);
205
- const b = y === 1 ? 0 : (1 - y) * Math.log((1 - y) / (1 - mu));
327
+ const b = y === 1 ? 0 : y === 0 && mu < 0.1 ? -Math.log1p(-mu) : (1 - y) * Math.log((1 - y) / (1 - mu));
206
328
  return 2 * w * (a + b);
207
329
  }
208
330
  function inverseGaussianDev(y, mu, w) {
209
- return (w * (y - mu) ** 2) / (y * mu * mu);
331
+ const den = y * mu * mu;
332
+ const r = (w * (y - mu) ** 2) / den;
333
+ if (Number.isFinite(r) && !(Math.abs(den) < 2 ** -1022))
334
+ return r;
335
+ // ((y - mu)² or y mu² overflows, or y mu² is below the normal range: as ((y - mu)/mu)²/y)
336
+ const d = (y - mu) / mu;
337
+ const v = w * ((d * d) / y);
338
+ if (Number.isFinite(v) || !(w > 0 && w < Infinity && y > 0 && y < Infinity && mu > 0 && mu < Infinity))
339
+ return v;
340
+ // (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
341
+ // to [1, 2) by their powers of 2, which are added back last
342
+ const t = y - mu;
343
+ const [ew, et, ey, em] = [exponentOf(w), exponentOf(t), exponentOf(y), exponentOf(mu)];
344
+ const ft = ldexp(t, -et);
345
+ const fm = ldexp(mu, -em);
346
+ return ldexp(ldexp(w, -ew) * ((ft * ft) / (ldexp(y, -ey) * fm * fm)), ew + 2 * et - ey - 2 * em);
210
347
  }
211
348
  /** Resolves a family name or specification (and a link given separately) to its functions */
212
349
  export function makeFamily(family, link) {
@@ -237,7 +374,7 @@ export function makeFamily(family, link) {
237
374
  const base = { ...lf, family: name, varianceName: undefined, fixedDispersion: false };
238
375
  switch (name) {
239
376
  case "gaussian":
240
- return { ...base, variance: () => 1, muOk: () => true, devContribution: (y, mu, w) => w * (y - mu) ** 2 };
377
+ return { ...base, varPower: 0, variance: () => 1, muOk: () => true, devContribution: (y, mu, w) => w * (y - mu) ** 2 };
241
378
  case "binomial":
242
379
  case "quasibinomial":
243
380
  return {
@@ -252,24 +389,41 @@ export function makeFamily(family, link) {
252
389
  return {
253
390
  ...base,
254
391
  fixedDispersion: name === "poisson",
392
+ varPower: 1,
255
393
  variance: (m) => m,
256
394
  muOk: (m) => Number.isFinite(m) && m > 0,
257
395
  // (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),
396
+ devContribution: (y, mu, w) => (y > 0 ? poissonDev(y, mu, w) : 2 * w * mu),
259
397
  };
260
398
  case "Gamma":
261
399
  return {
262
400
  ...base,
401
+ varPower: 2,
263
402
  variance: (m) => m * m,
264
403
  muOk: (m) => Number.isFinite(m) && m > 0,
265
- devContribution: (y, mu, w) => -2 * w * ((y === 0 ? 0 : logWarn(y / mu)) - (y - mu) / mu),
404
+ devContribution: (y, mu, w) => (nearMean(y, mu) ? gammaNear(y, mu, w) : gammaFar(y, mu, w, y === 0 ? 1 : y / mu)),
266
405
  };
267
406
  case "inverse.gaussian":
268
- return { ...base, variance: (m) => m * m * m, muOk: () => true, devContribution: inverseGaussianDev };
407
+ // the mean of an inverse Gaussian is positive (R's validmu accepts any mean, and a negative one turns the
408
+ // working weights NaN); a step to an invalid mean is halved
409
+ return {
410
+ ...base,
411
+ varPower: 3,
412
+ variance: (m) => m * m * m,
413
+ muOk: (m) => Number.isFinite(m) && m > 0,
414
+ devContribution: inverseGaussianDev,
415
+ };
269
416
  case "quasi": {
270
417
  const vname = spec.variance ?? "constant";
271
418
  const q = quasiVariance(vname);
272
- return { ...base, varianceName: vname, variance: q.variance, muOk: q.muOk, devContribution: q.dev };
419
+ return {
420
+ ...base,
421
+ varianceName: vname,
422
+ variance: q.variance,
423
+ muOk: q.muOk,
424
+ devContribution: q.dev,
425
+ ...(q.varPower === undefined ? {} : { varPower: q.varPower }),
426
+ };
273
427
  }
274
428
  }
275
429
  }
@@ -295,7 +449,16 @@ export function prepareResponse(fam, y, prior, hasStart, warnings) {
295
449
  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
450
  warnings.add("non-integer counts in a binomial glm!");
297
451
  }
452
+ if (rows.some((r) => r[0] < 0 || r[1] < 0))
453
+ throw new RangeError("counts must be nonnegative");
298
454
  const size = rows.map((r) => r[0] + r[1]);
455
+ // Finite counts whose total overflows: the proportion could be formed (s/2 over s/2 + f/2), but the binomial size,
456
+ // the prior weight that the deviance, the AIC and the covariance are proportional to, is not a double (R stops with
457
+ // "cannot find valid starting values", the proportion 0 and the starting mean NaN; #160)
458
+ const over = rows.findIndex((r, i) => Number.isFinite(r[0]) && Number.isFinite(r[1]) && !Number.isFinite(size[i]));
459
+ if (over >= 0) {
460
+ 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`);
461
+ }
299
462
  const yy = rows.map((r, i) => (size[i] === 0 ? 0 : r[0] / size[i]));
300
463
  const weights = prior.map((w, i) => w * size[i]);
301
464
  const mustart = yy.map((v, i) => (size[i] * v + 0.5) / (size[i] + 1));
@@ -346,12 +509,22 @@ export function prepareResponse(fam, y, prior, hasStart, warnings) {
346
509
  export function familyAic(fam, y, mu, w, size, dev, warnings) {
347
510
  const n = y.length;
348
511
  let s = 0;
512
+ // Rows of weight 0 are not in the likelihood: left out of every sum (R's gaussian AIC took their log(0) = -Inf and
513
+ // counted them in n, so it was Inf; 0 times an infinite log density was NaN elsewhere)
349
514
  switch (fam.family) {
350
515
  case "gaussian": {
351
516
  let slw = 0;
352
- for (const wi of w)
517
+ let nPos = 0;
518
+ for (const wi of w) {
519
+ if (!(wi > 0))
520
+ continue;
353
521
  slw += Math.log(wi);
354
- return n * (Math.log((2 * Math.PI * dev) / n) + 1) + 2 - slw;
522
+ nPos++;
523
+ }
524
+ // log(2 pi dev / n) as log(2 pi) + log(dev / n) only where 2 pi dev overflows although dev is finite (#189)
525
+ const q = (2 * Math.PI * dev) / nPos;
526
+ const logQ = q === Infinity && dev < Infinity ? Math.log(2 * Math.PI) + Math.log(dev / nPos) : Math.log(q);
527
+ return nPos * (logQ + 1) + 2 - slw;
355
528
  }
356
529
  case "binomial": {
357
530
  const m = size !== undefined && size.some((v) => v > 1) ? size : w;
@@ -364,6 +537,8 @@ export function familyAic(fam, y, mu, w, size, dev, warnings) {
364
537
  }
365
538
  case "poisson":
366
539
  for (let i = 0; i < n; i++) {
540
+ if (!(w[i] > 0))
541
+ continue;
367
542
  if (nonInteger(y[i]))
368
543
  warnings.add(`non-integer x = ${y[i].toFixed(6)}`);
369
544
  s += w[i] * dpois1(y[i], mu[i], true);
@@ -375,17 +550,27 @@ export function familyAic(fam, y, mu, w, size, dev, warnings) {
375
550
  sw += wi;
376
551
  const disp = dev / sw;
377
552
  for (let i = 0; i < n; i++)
378
- s += w[i] * dgamma1(y[i], 1 / disp, mu[i] * disp, true);
553
+ if (w[i] > 0)
554
+ s += w[i] * dgamma1(y[i], 1 / disp, mu[i] * disp, true);
379
555
  return -2 * s + 2;
380
556
  }
381
557
  case "inverse.gaussian": {
382
558
  let sw = 0;
383
559
  let sly = 0;
384
560
  for (let i = 0; i < n; i++) {
561
+ if (!(w[i] > 0))
562
+ continue;
385
563
  sw += w[i];
386
564
  sly += w[i] * Math.log(y[i]);
387
565
  }
388
- return sw * (Math.log((2 * Math.PI * dev) / sw) + 1) + 3 * sly + 2;
566
+ // log(2 pi dev / sw) as log(2 pi) + log(dev / sw) only where 2 pi dev overflows although dev is finite (sw =
567
+ // 1e308, dev = 3e307 gave Inf, #479; as gaussian, #189), and as log(dev) - log(sw) where dev / sw overflows too
568
+ const q = (2 * Math.PI * dev) / sw;
569
+ const ratio = dev / sw;
570
+ const logQ = q === Infinity && dev < Infinity
571
+ ? Math.log(2 * Math.PI) + (ratio < Infinity ? Math.log(ratio) : Math.log(dev) - Math.log(sw))
572
+ : Math.log(q);
573
+ return sw * (logQ + 1) + 3 * sly + 2;
389
574
  }
390
575
  default:
391
576
  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
  };