@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
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Estimability of predictions from a rank-deficient fit (R's predict.lm with rankdeficient = "warnif"). Written for
|
|
2
|
+
// rStats.js: a row x of the model matrix gives an estimable prediction when it is orthogonal to the null space of the
|
|
3
|
+
// fit's model matrix, whose basis follows from the pivoted QR decomposition: for each aliased column k (pivoted
|
|
4
|
+
// position >= rank), the vector with 1 at k and -R11^-1 R12[, k] on the first rank pivoted positions.
|
|
5
|
+
import { qr } from "../linalg/qr.js";
|
|
6
|
+
import { scaleToUnit } from "../internal/summary.js";
|
|
7
|
+
/** The warning R gives when some rows are not estimable */
|
|
8
|
+
export const NON_ESTIMABLE_WARNING = 'prediction from rank-deficient fit; attr(*, "non-estim") has doubtful cases';
|
|
9
|
+
/**
|
|
10
|
+
* 0-based indices of the rows of X (full rows, one value per coefficient) whose prediction is not estimable: |x · n|
|
|
11
|
+
* above sqrt(eps) |x| |n| for a null-space vector n. Empty for a full-rank fit.
|
|
12
|
+
*/
|
|
13
|
+
export function nonEstimableRows(qr, rank, X) {
|
|
14
|
+
if (qr === undefined)
|
|
15
|
+
return [];
|
|
16
|
+
const p = qr.pivot.length;
|
|
17
|
+
if (rank >= p)
|
|
18
|
+
return [];
|
|
19
|
+
const R = qr.qr;
|
|
20
|
+
const piv = qr.pivot;
|
|
21
|
+
const basis = [];
|
|
22
|
+
for (let k = rank; k < p; k++) {
|
|
23
|
+
// z = -R11^-1 R12[, k] by back substitution; the vector in coefficient order
|
|
24
|
+
const z = new Array(rank).fill(0);
|
|
25
|
+
for (let i = rank - 1; i >= 0; i--) {
|
|
26
|
+
let s = -R[i][k];
|
|
27
|
+
for (let j = i + 1; j < rank; j++)
|
|
28
|
+
s -= R[i][j] * z[j];
|
|
29
|
+
z[i] = s / R[i][i];
|
|
30
|
+
}
|
|
31
|
+
const v = new Array(p).fill(0);
|
|
32
|
+
for (let i = 0; i < rank; i++)
|
|
33
|
+
v[piv[i]] = z[i];
|
|
34
|
+
v[piv[k]] = 1;
|
|
35
|
+
basis.push(v);
|
|
36
|
+
}
|
|
37
|
+
const tol = Math.sqrt(Number.EPSILON);
|
|
38
|
+
const norm = (v) => Math.hypot(...v);
|
|
39
|
+
// the test does not depend on the units of x and v: each is divided by a power of 2 that brings its largest entry
|
|
40
|
+
// into [1, 2), so that the dot product and the product of the norms cannot overflow (with x of 1e308, both were
|
|
41
|
+
// Infinity and the row counted as estimable, #260). The division is exact, so where nothing overflowed or
|
|
42
|
+
// underflowed the comparison is the same
|
|
43
|
+
const unit = (v) => scaleToUnit([v]).groups[0];
|
|
44
|
+
const units = basis.map((v) => {
|
|
45
|
+
const u = unit(v);
|
|
46
|
+
return { u, n: norm(u) };
|
|
47
|
+
});
|
|
48
|
+
const out = [];
|
|
49
|
+
X.forEach((row, r) => {
|
|
50
|
+
const x = unit(row);
|
|
51
|
+
const nx = norm(x);
|
|
52
|
+
if (units.some(({ u, n }) => Math.abs(x.reduce((s, xi, j) => s + xi * u[j], 0)) > tol * nx * n))
|
|
53
|
+
out.push(r);
|
|
54
|
+
});
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Whether the columns of the model matrix `small` lie in the column space of `large` (the same rows): the smaller model
|
|
59
|
+
* is then a special case of the larger one, and their comparison by an F or likelihood ratio test has its usual
|
|
60
|
+
* meaning. Judged by the rank of [large, small] against that of large (LINPACK's tolerance 1e-7, relative to each
|
|
61
|
+
* column's norm, so the units of the predictors do not matter)
|
|
62
|
+
*/
|
|
63
|
+
export function nestedColumns(small, large) {
|
|
64
|
+
if (small.length === 0 || small[0].length === 0)
|
|
65
|
+
return true;
|
|
66
|
+
const rl = large.length === 0 || large[0].length === 0 ? 0 : qr(large).rank;
|
|
67
|
+
return qr(large.map((r, i) => [...r, ...small[i]])).rank === rl;
|
|
68
|
+
}
|
package/dist/models/formula.d.ts
CHANGED
|
@@ -46,7 +46,7 @@ export interface ParsedFormula {
|
|
|
46
46
|
* @param allowBars Accept the random-effects terms `(lhs | group)` and `(lhs || group)` (mixed models only).
|
|
47
47
|
*/
|
|
48
48
|
export declare function parseFormula(formula: string, allowError?: boolean, allowBars?: boolean): ParsedFormula;
|
|
49
|
-
/** The `Error()` terms of a right-hand side (
|
|
49
|
+
/** The `Error()` terms of a right-hand side (summands, possibly in parentheses) and the right-hand side without them. */
|
|
50
50
|
export declare function splitErrorTerms(rhs: FormulaNode): {
|
|
51
51
|
treatment: FormulaNode | undefined;
|
|
52
52
|
errors: FormulaNode[];
|
package/dist/models/formula.js
CHANGED
|
@@ -14,6 +14,13 @@
|
|
|
14
14
|
// R deparses it (for the terms' labels).
|
|
15
15
|
import { formatR } from "../htest/core/simulate.js";
|
|
16
16
|
const invalid = (why) => new SyntaxError(`invalid formula: ${why}`);
|
|
17
|
+
/**
|
|
18
|
+
* A syntactic name (#461): it starts with a letter, a letter number, `.` or a digit that is not ASCII (an ASCII digit,
|
|
19
|
+
* or `.` and an ASCII digit, starts a number; `_` cannot start a name), and goes on with letters, numbers that are not
|
|
20
|
+
* symbols, combining marks, `.` and `_`. The classes are Unicode properties: R on Windows takes the same letters and
|
|
21
|
+
* digits, but no combining marks and nothing outside the BMP (R on Linux takes both, from glibc's tables).
|
|
22
|
+
*/
|
|
23
|
+
const NAME = /^(?:[\p{L}\p{Nl}.]|(?![0-9])\p{Nd})[\p{L}\p{Nl}\p{Nd}\p{M}._]*/u;
|
|
17
24
|
function tokenize(s, allowBars) {
|
|
18
25
|
const out = [];
|
|
19
26
|
let i = 0;
|
|
@@ -29,10 +36,10 @@ function tokenize(s, allowBars) {
|
|
|
29
36
|
i += m[0].length;
|
|
30
37
|
continue;
|
|
31
38
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
out.push({ kind: "name", text:
|
|
35
|
-
i +=
|
|
39
|
+
const name = NAME.exec(s.slice(i));
|
|
40
|
+
if (name !== null) {
|
|
41
|
+
out.push({ kind: "name", text: name[0] });
|
|
42
|
+
i += name[0].length;
|
|
36
43
|
continue;
|
|
37
44
|
}
|
|
38
45
|
if (s.startsWith("%in%", i)) {
|
|
@@ -134,7 +141,7 @@ export function parseFormula(formula, allowError = false, allowBars = false) {
|
|
|
134
141
|
throw invalid("unexpected input after the formula");
|
|
135
142
|
return { response, rhs };
|
|
136
143
|
}
|
|
137
|
-
/** The `Error()` terms of a right-hand side (
|
|
144
|
+
/** The `Error()` terms of a right-hand side (summands, possibly in parentheses) and the right-hand side without them. */
|
|
138
145
|
export function splitErrorTerms(rhs) {
|
|
139
146
|
const errors = [];
|
|
140
147
|
const strip = (n) => {
|
|
@@ -142,6 +149,11 @@ export function splitErrorTerms(rhs) {
|
|
|
142
149
|
errors.push(n.arg);
|
|
143
150
|
return undefined;
|
|
144
151
|
}
|
|
152
|
+
if (n.type === "paren") {
|
|
153
|
+
// y ~ x + (Error(id)) and y ~ (x + Error(id)): parentheses do not hide the term, as R's terms()
|
|
154
|
+
const a = strip(n.arg);
|
|
155
|
+
return a === undefined ? undefined : { ...n, arg: a };
|
|
156
|
+
}
|
|
145
157
|
if (n.type === "binary" && n.op === "+") {
|
|
146
158
|
const l = strip(n.left);
|
|
147
159
|
const r = strip(n.right);
|
|
@@ -350,9 +362,9 @@ export function expandRhs(rhs, index, dot) {
|
|
|
350
362
|
let e = n.right;
|
|
351
363
|
while (e.type === "paren")
|
|
352
364
|
e = e.arg;
|
|
353
|
-
// n is truncated towards zero (R's asInteger: ^2.5 is ^2)
|
|
365
|
+
// n is truncated towards zero (R's asInteger: ^2.5 is ^2; beyond the int range, as 1e999, NA and an error)
|
|
354
366
|
const p = e.type === "num" ? Math.trunc(e.value) : NaN;
|
|
355
|
-
if (!(p >= 2))
|
|
367
|
+
if (!(p >= 2 && p <= 2147483647))
|
|
356
368
|
throw new Error("invalid power in formula");
|
|
357
369
|
let cur = a;
|
|
358
370
|
for (let k = 1; k < p; k++) {
|
|
@@ -360,7 +372,13 @@ export function expandRhs(rhs, index, dot) {
|
|
|
360
372
|
for (const s of a)
|
|
361
373
|
for (const t of cur)
|
|
362
374
|
nxt.push(union(s, t));
|
|
363
|
-
|
|
375
|
+
const next = dedupe(nxt);
|
|
376
|
+
// a fixed point (the same terms in the same order) stays one: the remaining steps change nothing, so a
|
|
377
|
+
// large power ends after at most as many steps as there are variables (#294)
|
|
378
|
+
const same = next.length === cur.length && next.every((t, i) => keyOf(t) === keyOf(cur[i]));
|
|
379
|
+
cur = next;
|
|
380
|
+
if (same)
|
|
381
|
+
break;
|
|
364
382
|
}
|
|
365
383
|
return cur;
|
|
366
384
|
}
|
package/dist/models/glm.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
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
|
-
import {
|
|
4
|
+
import { type Family, type GlmFamilyName, type GlmFamilySpec, type GlmLink } from "./glmFamily.js";
|
|
5
|
+
import { Warnings } from "../internal/warnings.js";
|
|
5
6
|
export type { GlmFamilyName, GlmFamilySpec, GlmLink, GlmLinkName, QuasiVariance } from "./glmFamily.js";
|
|
6
7
|
/**
|
|
7
8
|
* Options for {@link glm}.
|
|
@@ -61,6 +62,8 @@ export interface GlmModel {
|
|
|
61
62
|
maxit: number;
|
|
62
63
|
/** Starting coefficients, if given. */
|
|
63
64
|
start: number[] | undefined;
|
|
65
|
+
/** k when the fit was computed on the response scaled by 2^-k (gaussian on the identity link; see scaledView). */
|
|
66
|
+
yScale?: number;
|
|
64
67
|
}
|
|
65
68
|
/**
|
|
66
69
|
* Result of {@link glm}: R's `glm` object.
|
|
@@ -147,7 +150,6 @@ export interface IrlsFit {
|
|
|
147
150
|
workingWeights: number[];
|
|
148
151
|
workingResiduals: number[];
|
|
149
152
|
}
|
|
150
|
-
/** Iteratively reweighted least squares (glm-behaviour.md, "Fitting") */
|
|
151
153
|
export declare function irls(inp: IrlsInput, warnings: Warnings): IrlsFit;
|
|
152
154
|
/** Fits a model from its stored data, with the columns `cols` of the model matrix (all by default) */
|
|
153
155
|
export declare function refit(model: GlmModel, cols: number[], warnings: Warnings, control?: {
|
|
@@ -186,6 +188,15 @@ export declare function refit(model: GlmModel, cols: number[], warnings: Warning
|
|
|
186
188
|
* @category Generalized linear models
|
|
187
189
|
*/
|
|
188
190
|
export declare function glm(x: MatrixInput | Sample, y: Sample | MatrixInput, options?: GlmOptions): GlmResult;
|
|
191
|
+
/**
|
|
192
|
+
* The fit to compute with and its scale k: for a fit of a response scaled by 2^-k (see glm), that scaled fit, whose
|
|
193
|
+
* quantities with units are 2^-k (or 2^-2k) times the fit's; otherwise the fit itself with k = 0. The functions on a
|
|
194
|
+
* fit compute on it (statistics without units unchanged) and scale back only what has units
|
|
195
|
+
*/
|
|
196
|
+
export declare function scaledView(fit: GlmResult): {
|
|
197
|
+
fit: GlmResult;
|
|
198
|
+
k: number;
|
|
199
|
+
};
|
|
189
200
|
/** The family object of a fit */
|
|
190
201
|
export declare function familyOf(fit: GlmResult): Family;
|
|
191
202
|
/** Pearson dispersion of summary.glm (1 for binomial and poisson) */
|
|
@@ -226,6 +237,22 @@ export interface GlmSummary {
|
|
|
226
237
|
/** Warnings (such as "observations with zero weight not used for calculating dispersion"). */
|
|
227
238
|
warnings: string[];
|
|
228
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* A dispersion given by the user: a non-negative finite number (a negative one gave NaN standard errors, as R's with a
|
|
242
|
+
* warning; 0 gives standard errors of 0, as R)
|
|
243
|
+
*/
|
|
244
|
+
export declare function checkDispersion(d: number): number;
|
|
245
|
+
/**
|
|
246
|
+
* A dispersion d given by the user, in the units of a fit computed on the response scaled by 2^-k (scaledView), as
|
|
247
|
+
* m 4^s: d 2^-2k with s = 0 where that is 0 or in the normal range (the old value, the same bits); where it underflows
|
|
248
|
+
* or overflows although d does not (k = 1023 for a response of 1e308, where d = 1 gave 0 and standard errors 0,
|
|
249
|
+
* #349), m is d brought to [1, 4) by an even power of 2, 4^h, and s = h - k, for the callers to apply 2^s and 4^s
|
|
250
|
+
* where the result is in range
|
|
251
|
+
*/
|
|
252
|
+
export declare function scaledDispersion(d: number, k: number): {
|
|
253
|
+
m: number;
|
|
254
|
+
s: number;
|
|
255
|
+
};
|
|
229
256
|
/**
|
|
230
257
|
* Summary of a generalized linear model: the dispersion, the coefficient table and the covariance of the estimates,
|
|
231
258
|
* for a `glm` fit. Equivalent to R's `summary()`.
|
|
@@ -241,16 +268,25 @@ export interface GlmSummary {
|
|
|
241
268
|
*
|
|
242
269
|
* @category Generalized linear models
|
|
243
270
|
*/
|
|
244
|
-
export declare function glmSummary(
|
|
271
|
+
export declare function glmSummary(given: GlmResult, options?: GlmSummaryOptions): GlmSummary;
|
|
245
272
|
/**
|
|
246
273
|
* Kinds of residuals of {@link glmResiduals}.
|
|
247
274
|
*
|
|
248
275
|
* @category Generalized linear models
|
|
249
276
|
*/
|
|
250
277
|
export type GlmResidualType = "deviance" | "pearson" | "working" | "response";
|
|
278
|
+
/**
|
|
279
|
+
* Options for {@link glmResiduals}.
|
|
280
|
+
*
|
|
281
|
+
* @category Generalized linear models
|
|
282
|
+
*/
|
|
283
|
+
export interface GlmResidualsOptions {
|
|
284
|
+
/** Kind of residuals. Default `"deviance"`. */
|
|
285
|
+
type?: GlmResidualType;
|
|
286
|
+
}
|
|
251
287
|
/**
|
|
252
288
|
* Residuals of a generalized linear model, one per row, for a `glm` fit (default `"deviance"`). Equivalent to R's
|
|
253
|
-
* `residuals(fit, type)`.
|
|
289
|
+
* `residuals(fit, type)`. The kind is the second argument or the option `type` (giving both is a RangeError).
|
|
254
290
|
*
|
|
255
291
|
* @example
|
|
256
292
|
* ```ts
|
|
@@ -262,6 +298,16 @@ export type GlmResidualType = "deviance" | "pearson" | "working" | "response";
|
|
|
262
298
|
* @category Generalized linear models
|
|
263
299
|
*/
|
|
264
300
|
export declare function glmResiduals(fit: GlmResult, type?: GlmResidualType): number[];
|
|
301
|
+
/**
|
|
302
|
+
* Residuals of a generalized linear model, with the kind in the options: `glmResiduals(fit, { type: "pearson" })`.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* const fit = glm([1, 2, 3, 4, 5, 6], [0, 0, 1, 0, 1, 1], { family: "binomial" });
|
|
307
|
+
* glmResiduals(fit, { type: "pearson" }); // [-0.21925, -0.40231, 1.3546, -1.3546, 0.40231, 0.21925]
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
export declare function glmResiduals(fit: GlmResult, options: GlmResidualsOptions): number[];
|
|
265
311
|
/**
|
|
266
312
|
* Diagonal of the hat matrix of the last weighted least squares step, for the rows of non-zero prior weight, for a
|
|
267
313
|
* `glm` fit. Equivalent to R's `hatvalues()`.
|
|
@@ -287,7 +333,7 @@ export declare function glmHatvalues(fit: GlmResult): number[];
|
|
|
287
333
|
*
|
|
288
334
|
* @category Generalized linear models
|
|
289
335
|
*/
|
|
290
|
-
export declare function glmRstandard(
|
|
336
|
+
export declare function glmRstandard(given: GlmResult, options?: GlmSummaryOptions): number[];
|
|
291
337
|
/**
|
|
292
338
|
* Cook's distances, for the rows of non-zero prior weight, for a `glm` fit. Equivalent to R's `cooks.distance()`.
|
|
293
339
|
*
|
|
@@ -299,7 +345,7 @@ export declare function glmRstandard(fit: GlmResult, options?: GlmSummaryOptions
|
|
|
299
345
|
*
|
|
300
346
|
* @category Generalized linear models
|
|
301
347
|
*/
|
|
302
|
-
export declare function glmCooksDistance(
|
|
348
|
+
export declare function glmCooksDistance(given: GlmResult, options?: GlmSummaryOptions): number[];
|
|
303
349
|
/**
|
|
304
350
|
* Options for {@link glmPredict}.
|
|
305
351
|
*
|
|
@@ -328,9 +374,13 @@ export interface GlmPredictOptions {
|
|
|
328
374
|
*
|
|
329
375
|
* @category Generalized linear models
|
|
330
376
|
*/
|
|
331
|
-
export declare function glmPredict(
|
|
377
|
+
export declare function glmPredict(given: GlmResult, options?: GlmPredictOptions): {
|
|
332
378
|
/** Predictions on the scale of `type`, one per row. */
|
|
333
379
|
fit: number[];
|
|
334
380
|
/** Their standard errors (R's `se.fit`). */
|
|
335
381
|
seFit: number[];
|
|
382
|
+
/** 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. */
|
|
383
|
+
nonEstimable?: number[];
|
|
384
|
+
/** R's warning for those rows; present only when there are some. */
|
|
385
|
+
warnings?: string[];
|
|
336
386
|
};
|