@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.
- package/CHANGELOG.md +294 -0
- package/README.ja.md +68 -35
- package/README.md +65 -30
- package/dist/chunks/chunk-2C33MXSW.js +3 -0
- package/dist/chunks/chunk-34S5SQY6.js +2 -0
- package/dist/chunks/chunk-4WE3OO62.js +2 -0
- package/dist/chunks/chunk-5AYHCITP.js +2 -0
- package/dist/chunks/chunk-6JJQL6HL.js +2 -0
- package/dist/chunks/chunk-C7V2FH6L.js +3 -0
- package/dist/chunks/chunk-CZI7KUO3.js +2 -0
- package/dist/chunks/chunk-DXI5N2ET.js +2 -0
- package/dist/chunks/chunk-FY3FVOBM.js +2 -0
- package/dist/chunks/chunk-H3WVPZDL.js +2 -0
- package/dist/chunks/chunk-LHUIJQHY.js +2 -0
- package/dist/chunks/chunk-LV3W2F7N.js +2 -0
- package/dist/chunks/chunk-M4ITG7YR.js +2 -0
- package/dist/chunks/chunk-NSYZTS26.js +2 -0
- package/dist/chunks/chunk-ONY5CISH.js +2 -0
- package/dist/chunks/chunk-RFE7MVP7.js +2 -0
- package/dist/chunks/chunk-SE7V2KLO.js +2 -0
- package/dist/chunks/chunk-THIGHOKK.js +2 -0
- package/dist/chunks/chunk-TNMJPQPV.js +2 -0
- package/dist/chunks/chunk-WM7VFXTV.js +2 -0
- package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
- package/dist/chunks/chunk-XLSFJTTK.js +2 -0
- package/dist/chunks/chunk-Z37NELOS.js +2 -0
- package/dist/chunks/chunk-ZFQWB222.js +2 -0
- package/dist/data/grouping.js +26 -7
- package/dist/descriptive/basic.js +110 -8
- package/dist/descriptive/cov.js +23 -4
- package/dist/descriptive/dataFrames.d.ts +4 -2
- package/dist/descriptive/dataFrames.js +69 -23
- package/dist/descriptive/factanal.d.ts +2 -0
- package/dist/descriptive/factanal.js +26 -9
- package/dist/descriptive/hist.d.ts +8 -5
- package/dist/descriptive/hist.js +77 -31
- package/dist/descriptive/missing.js +3 -1
- package/dist/descriptive/moments.js +86 -19
- package/dist/descriptive/omega.d.ts +1 -1
- package/dist/descriptive/omega.js +74 -10
- package/dist/descriptive/pivot.d.ts +25 -10
- package/dist/descriptive/pivot.js +182 -42
- package/dist/descriptive/quantile.js +10 -1
- package/dist/descriptive/rank.js +8 -1
- package/dist/descriptive/table.d.ts +23 -3
- package/dist/descriptive/table.js +36 -21
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.js +27 -2
- package/dist/distributions/beta.js +33 -4
- package/dist/distributions/binomial.js +46 -14
- package/dist/distributions/bvnorm.js +11 -2
- package/dist/distributions/cauchy.js +21 -4
- package/dist/distributions/chisq.js +16 -3
- package/dist/distributions/exponential.js +14 -0
- package/dist/distributions/f.js +41 -17
- package/dist/distributions/gamma.js +35 -3
- package/dist/distributions/geometric.js +58 -13
- package/dist/distributions/hypergeometric.d.ts +5 -1
- package/dist/distributions/hypergeometric.js +101 -6
- package/dist/distributions/invgamma.js +10 -1
- package/dist/distributions/kumaraswamy.js +9 -0
- package/dist/distributions/laplace.js +36 -6
- package/dist/distributions/logistic.js +13 -1
- package/dist/distributions/lognormal.js +16 -2
- package/dist/distributions/multinomial.js +58 -11
- package/dist/distributions/nbeta.js +62 -11
- package/dist/distributions/nchisq.js +30 -6
- package/dist/distributions/negbinomial.js +102 -17
- package/dist/distributions/normal.js +17 -4
- package/dist/distributions/nt.js +392 -17
- package/dist/distributions/pareto.js +12 -1
- package/dist/distributions/poisson.js +37 -3
- package/dist/distributions/signrank.js +16 -4
- package/dist/distributions/summary.d.ts +2 -1
- package/dist/distributions/summary.js +481 -61
- package/dist/distributions/t.js +9 -0
- package/dist/distributions/triangular.js +19 -5
- package/dist/distributions/tukey.js +11 -2
- package/dist/distributions/uniform.js +8 -0
- package/dist/distributions/weibull.js +18 -0
- package/dist/distributions/wilcox.d.ts +9 -3
- package/dist/distributions/wilcox.js +164 -26
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +3 -1
- package/dist/entries/descriptive.js +1 -1
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +2 -0
- package/dist/entries/linalg.d.ts +3 -1
- package/dist/entries/mixed.d.ts +3 -0
- package/dist/entries/mixed.js +2 -0
- package/dist/entries/numerics.d.ts +2 -1
- package/dist/entries/tests.d.ts +8 -5
- package/dist/entries/tests.js +3 -1
- package/dist/htest/agreement.d.ts +16 -2
- package/dist/htest/agreement.js +122 -21
- package/dist/htest/ansariTest.d.ts +8 -2
- package/dist/htest/ansariTest.js +116 -18
- package/dist/htest/aov.d.ts +29 -23
- package/dist/htest/aov.js +90 -47
- package/dist/htest/binomTest.d.ts +5 -0
- package/dist/htest/binomTest.js +47 -17
- package/dist/htest/brunnerMunzelTest.d.ts +4 -3
- package/dist/htest/brunnerMunzelTest.js +12 -5
- package/dist/htest/chisqTest.d.ts +5 -2
- package/dist/htest/chisqTest.js +151 -46
- package/dist/htest/cor.d.ts +9 -2
- package/dist/htest/cor.js +57 -26
- package/dist/htest/core/ansari.d.ts +9 -0
- package/dist/htest/core/ansari.js +21 -1
- package/dist/htest/core/kolmogorov.d.ts +25 -0
- package/dist/htest/core/kolmogorov.js +123 -0
- package/dist/htest/core/mantelhaen.js +6 -4
- package/dist/htest/core/shapiro.d.ts +1 -1
- package/dist/htest/core/shapiro.js +5 -1
- package/dist/htest/core/simulate.d.ts +6 -0
- package/dist/htest/core/simulate.js +20 -0
- package/dist/htest/dunnett.js +14 -5
- package/dist/htest/effectSize.d.ts +5 -0
- package/dist/htest/effectSize.js +117 -35
- package/dist/htest/fisherTest.d.ts +4 -3
- package/dist/htest/fisherTest.js +88 -24
- package/dist/htest/groups.d.ts +7 -0
- package/dist/htest/groups.js +21 -4
- package/dist/htest/ksTest.d.ts +7 -1
- package/dist/htest/ksTest.js +36 -6
- package/dist/htest/leveneTest.d.ts +2 -0
- package/dist/htest/leveneTest.js +28 -6
- package/dist/htest/mantelhaenTest.js +132 -25
- package/dist/htest/mcnemarTest.js +40 -11
- package/dist/htest/multipleComparisons.d.ts +24 -26
- package/dist/htest/multipleComparisons.js +70 -64
- package/dist/htest/normalityTests.js +54 -14
- package/dist/htest/onewayTest.js +106 -5
- package/dist/htest/pAdjust.d.ts +3 -2
- package/dist/htest/pAdjust.js +42 -6
- package/dist/htest/pairwise.d.ts +3 -1
- package/dist/htest/pairwise.js +46 -21
- package/dist/htest/poissonTest.d.ts +1 -1
- package/dist/htest/poissonTest.js +117 -59
- package/dist/htest/polycor.js +47 -17
- package/dist/htest/power.d.ts +15 -1
- package/dist/htest/power.js +128 -10
- package/dist/htest/propTest.d.ts +3 -0
- package/dist/htest/propTest.js +44 -6
- package/dist/htest/pwr.d.ts +38 -1
- package/dist/htest/pwr.js +159 -39
- package/dist/htest/rankTests.js +36 -6
- package/dist/htest/tTest.d.ts +2 -0
- package/dist/htest/tTest.js +53 -21
- package/dist/htest/testFormula.js +11 -11
- package/dist/htest/trendTests.d.ts +28 -4
- package/dist/htest/trendTests.js +124 -35
- package/dist/htest/types.d.ts +7 -0
- package/dist/htest/types.js +7 -0
- package/dist/htest/varTest.js +52 -14
- package/dist/htest/wilcoxTest.js +164 -29
- package/dist/integrate/quadpack.js +27 -5
- package/dist/internal/checkOptions.d.ts +11 -0
- package/dist/internal/checkOptions.js +31 -0
- package/dist/internal/covmat.d.ts +5 -0
- package/dist/internal/covmat.js +20 -0
- package/dist/internal/ddexp.d.ts +15 -1
- package/dist/internal/ddexp.js +59 -6
- package/dist/internal/discrete.d.ts +14 -0
- package/dist/internal/discrete.js +78 -5
- package/dist/internal/extremes.d.ts +9 -0
- package/dist/internal/extremes.js +24 -0
- package/dist/internal/fround.js +13 -4
- package/dist/internal/integer.d.ts +9 -0
- package/dist/internal/integer.js +12 -0
- package/dist/internal/invert.js +15 -2
- package/dist/internal/optionalArgs.d.ts +12 -0
- package/dist/internal/optionalArgs.js +46 -0
- package/dist/internal/scaledInverse.d.ts +10 -0
- package/dist/internal/scaledInverse.js +22 -0
- package/dist/internal/seq.js +15 -2
- package/dist/internal/summary.d.ts +90 -0
- package/dist/internal/summary.js +458 -39
- package/dist/internal/uniroot.d.ts +7 -0
- package/dist/internal/uniroot.js +28 -3
- package/dist/internal/warnings.d.ts +66 -0
- package/dist/internal/warnings.js +122 -0
- package/dist/linalg/basic.d.ts +3 -1
- package/dist/linalg/basic.js +58 -7
- package/dist/linalg/chol.js +10 -1
- package/dist/linalg/dot.d.ts +29 -0
- package/dist/linalg/dot.js +132 -0
- package/dist/linalg/eigen.d.ts +6 -1
- package/dist/linalg/eigen.js +75 -14
- package/dist/linalg/expm.d.ts +3 -2
- package/dist/linalg/expm.js +113 -15
- package/dist/linalg/kappa.js +64 -10
- package/dist/linalg/lu.d.ts +2 -1
- package/dist/linalg/lu.js +40 -5
- package/dist/linalg/matrix.d.ts +14 -0
- package/dist/linalg/matrix.js +24 -0
- package/dist/linalg/norm.js +4 -0
- package/dist/linalg/ops.d.ts +27 -2
- package/dist/linalg/ops.js +74 -26
- package/dist/linalg/qr.d.ts +2 -1
- package/dist/linalg/qr.js +105 -23
- package/dist/linalg/schur.js +11 -3
- package/dist/linalg/solve.d.ts +6 -1
- package/dist/linalg/solve.js +156 -26
- package/dist/linalg/svd.d.ts +9 -2
- package/dist/linalg/svd.js +33 -11
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +4 -4
- package/dist/models/anovaSequential.d.ts +32 -2
- package/dist/models/anovaSequential.js +170 -28
- package/dist/models/aovEz.js +154 -39
- package/dist/models/aovStrata.js +68 -24
- package/dist/models/carAnova.d.ts +1 -1
- package/dist/models/carAnova.js +78 -33
- package/dist/models/coxph.d.ts +6 -0
- package/dist/models/coxph.js +166 -69
- package/dist/models/estimable.d.ts +15 -0
- package/dist/models/estimable.js +68 -0
- package/dist/models/formula.d.ts +1 -1
- package/dist/models/formula.js +26 -8
- package/dist/models/glm.d.ts +57 -7
- package/dist/models/glm.js +427 -74
- package/dist/models/glmAnova.js +90 -14
- package/dist/models/glmConfint.d.ts +1 -1
- package/dist/models/glmConfint.js +121 -29
- package/dist/models/glmFamily.d.ts +11 -6
- package/dist/models/glmFamily.js +247 -62
- package/dist/models/lm.d.ts +12 -1
- package/dist/models/lm.js +246 -37
- package/dist/models/lmer.d.ts +31 -7
- package/dist/models/lmer.js +129 -42
- package/dist/models/lmerAnova.d.ts +3 -3
- package/dist/models/lmerAnova.js +47 -9
- package/dist/models/lmerHypotheses.d.ts +6 -2
- package/dist/models/lmerHypotheses.js +14 -2
- package/dist/models/lmerTest.d.ts +13 -6
- package/dist/models/lmerTest.js +82 -29
- package/dist/models/mauchly.d.ts +6 -1
- package/dist/models/mauchly.js +41 -15
- package/dist/models/mixedStructure.js +6 -0
- package/dist/models/modelFrame.js +10 -4
- package/dist/models/modelMatrix.d.ts +3 -1
- package/dist/models/modelMatrix.js +47 -17
- package/dist/models/scaledTables.d.ts +8 -0
- package/dist/models/scaledTables.js +6 -0
- package/dist/models/survival.d.ts +12 -1
- package/dist/models/survival.js +75 -28
- package/dist/models/tiedTimes.d.ts +2 -0
- package/dist/models/tiedTimes.js +34 -0
- package/dist/numerics/deriv.d.ts +1 -1
- package/dist/numerics/deriv.js +72 -8
- package/dist/numerics/integrate.d.ts +9 -1
- package/dist/numerics/integrate.js +111 -16
- package/dist/numerics/interp.d.ts +40 -9
- package/dist/numerics/interp.js +375 -55
- package/dist/numerics/ode.js +59 -1
- package/dist/numerics.js +2 -2
- package/dist/optim/localmin.js +4 -0
- package/dist/optim/optim.d.ts +3 -1
- package/dist/optim/optim.js +97 -10
- package/dist/random/beta.js +58 -4
- package/dist/random/binomial.d.ts +14 -3
- package/dist/random/binomial.js +59 -24
- package/dist/random/continuous.js +50 -5
- package/dist/random/extra.js +55 -9
- package/dist/random/gamma.d.ts +4 -1
- package/dist/random/gamma.js +47 -8
- package/dist/random/hyper.js +9 -6
- package/dist/random/mt.js +17 -2
- package/dist/random/poisson.js +31 -3
- package/dist/random/r2dtable.d.ts +8 -1
- package/dist/random/r2dtable.js +24 -3
- package/dist/random/rank.d.ts +2 -0
- package/dist/random/rank.js +44 -4
- package/dist/random/rng.js +4 -0
- package/dist/random/sample.d.ts +12 -2
- package/dist/random/sample.js +63 -20
- package/dist/random/streams.js +4 -2
- package/dist/rstats.js +17 -14
- package/dist/special/besselIK.d.ts +12 -0
- package/dist/special/besselIK.js +175 -23
- package/dist/special/besselJY.d.ts +11 -1
- package/dist/special/besselJY.js +127 -19
- package/dist/special/besselSeries.d.ts +27 -0
- package/dist/special/besselSeries.js +45 -2
- package/dist/special/beta.js +4 -1
- package/dist/special/choose.js +24 -8
- package/dist/special/dd.js +122 -18
- package/dist/special/extra.js +14 -2
- package/dist/special/gamma.js +11 -2
- package/dist/special/incbet.js +30 -4
- package/dist/special/incbetTemme.d.ts +7 -0
- package/dist/special/incbetTemme.js +45 -12
- package/dist/special/loader.d.ts +1 -1
- package/dist/special/loader.js +117 -20
- package/dist/special/log1pmx.js +4 -0
- package/dist/special/psigamma.js +7 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +15 -11
- package/llms.txt +15 -6
- package/package.json +19 -10
- package/dist/chunks/chunk-3THFTB36.js +0 -2
- package/dist/chunks/chunk-3TSWYE5L.js +0 -2
- package/dist/chunks/chunk-3WPU6LGK.js +0 -2
- package/dist/chunks/chunk-5ITR67MB.js +0 -2
- package/dist/chunks/chunk-5NRFU6F6.js +0 -2
- package/dist/chunks/chunk-E2LCSVQV.js +0 -2
- package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
- package/dist/chunks/chunk-FKAKALOZ.js +0 -2
- package/dist/chunks/chunk-HD73UKPT.js +0 -2
- package/dist/chunks/chunk-IUULP3LE.js +0 -2
- package/dist/chunks/chunk-JQG3YQOC.js +0 -2
- package/dist/chunks/chunk-KT66SB7R.js +0 -2
- package/dist/chunks/chunk-MAUAOQG3.js +0 -2
- package/dist/chunks/chunk-MDYE7QP2.js +0 -2
- package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
- package/dist/chunks/chunk-PMJI5QO6.js +0 -3
- package/dist/chunks/chunk-UF7KOTHF.js +0 -3
- package/dist/chunks/chunk-UOE57JGT.js +0 -2
- package/dist/chunks/chunk-USPEBXSR.js +0 -2
- package/dist/chunks/chunk-VLUL2HFW.js +0 -3
- package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
package/dist/models/glmFamily.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
88
|
-
|
|
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
|
-
|
|
115
|
-
|
|
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
|
-
|
|
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
|
-
|
|
126
|
-
|
|
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(
|
|
131
|
-
muEta: (e) => Math.max(dnorm(e),
|
|
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(
|
|
141
|
-
muEta: (e) => Math.max(dcauchy1(e, 0, 1, false),
|
|
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
|
-
|
|
149
|
-
|
|
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)),
|
|
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
|
-
|
|
173
|
-
|
|
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
|
-
|
|
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) =>
|
|
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
|
-
/**
|
|
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
|
-
|
|
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 ?
|
|
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) =>
|
|
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
|
-
|
|
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 {
|
|
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
|
-
|
|
517
|
+
let nPos = 0;
|
|
518
|
+
for (const wi of w) {
|
|
519
|
+
if (!(wi > 0))
|
|
520
|
+
continue;
|
|
353
521
|
slw += Math.log(wi);
|
|
354
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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;
|
package/dist/models/lm.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
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
|
};
|