@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
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// from the specification oracle/factanal-behaviour.md (no R code was read or used): the uniquenesses minimise the
|
|
3
3
|
// likelihood criterion by L-BFGS-B (this library's optim), the loadings come from the eigendecomposition of the
|
|
4
4
|
// scaled correlation matrix, and the rotations are Kaiser's varimax (normalised) and Hendrickson and White's promax.
|
|
5
|
+
import { checkCovmat } from "../internal/covmat.js";
|
|
5
6
|
import { pchisq } from "../distributions/chisq.js";
|
|
6
7
|
import { eigen } from "../linalg/eigen.js";
|
|
7
8
|
import { qr, qrCoef } from "../linalg/qr.js";
|
|
@@ -10,6 +11,11 @@ import { svd } from "../linalg/svd.js";
|
|
|
10
11
|
import { optim } from "../optim/optim.js";
|
|
11
12
|
import { twoSum } from "../special/dd.js";
|
|
12
13
|
import { cov } from "./cov.js";
|
|
14
|
+
import { unreported } from "../internal/warnings.js";
|
|
15
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
16
|
+
import { checkOptions, nestedOptions } from "../internal/checkOptions.js";
|
|
17
|
+
const FACTANAL_KEYS = ["covmat", "nObs", "rotation", "scores", "control"];
|
|
18
|
+
const FACTANAL_CONTROL_KEYS = ["lower", "start"];
|
|
13
19
|
/** Sum rounded once (R's long double accumulation, here double-double); also used by omega */
|
|
14
20
|
export function sumOnce(xs) {
|
|
15
21
|
let hi = 0;
|
|
@@ -54,11 +60,17 @@ const transpose = (A) => A[0].map((_, j) => A.map((r) => r[j]));
|
|
|
54
60
|
/** Varimax rotation (normalised, eps 1e-5, at most 1000 iterations); also used by omega */
|
|
55
61
|
export function varimax(L) {
|
|
56
62
|
const k = L[0].length;
|
|
57
|
-
const p = L.length;
|
|
58
63
|
const len = L.map((r) => Math.sqrt(sumOnce(r.map((v) => v * v))));
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
// Rows of zero loadings have no Kaiser normalisation (0/0) and say nothing about the rotation: the rotation is that
|
|
65
|
+
// of the other rows (as R's varimax of them), and they stay 0. R's varimax and factanal failed on them ("infinite or
|
|
66
|
+
// missing values in 'x'" in the SVD; issue #21)
|
|
67
|
+
const kept = L.flatMap((_, i) => (len[i] > 0 ? [i] : []));
|
|
68
|
+
const X = kept.map((i) => L[i].map((v) => v / len[i]));
|
|
69
|
+
const p = X.length;
|
|
61
70
|
let T = Array.from({ length: k }, (_, i) => Array.from({ length: k }, (_, j) => (i === j ? 1 : 0)));
|
|
71
|
+
if (p === 0)
|
|
72
|
+
return { loadings: L.map((r) => r.slice()), rotmat: T };
|
|
73
|
+
const Xt = transpose(X);
|
|
62
74
|
let total = 0;
|
|
63
75
|
for (let it = 0; it < 1000; it++) {
|
|
64
76
|
const Z = matMul(X, T);
|
|
@@ -78,7 +90,9 @@ export function varimax(L) {
|
|
|
78
90
|
break;
|
|
79
91
|
}
|
|
80
92
|
const Z = matMul(X, T);
|
|
81
|
-
|
|
93
|
+
const loadings = L.map((r) => r.map(() => 0));
|
|
94
|
+
kept.forEach((i, a) => (loadings[i] = Z[a].map((v) => v * len[i])));
|
|
95
|
+
return { loadings, rotmat: T };
|
|
82
96
|
}
|
|
83
97
|
/** Promax rotation (m = 4) */
|
|
84
98
|
function promax(L) {
|
|
@@ -109,6 +123,8 @@ function promax(L) {
|
|
|
109
123
|
* @category Agreement, reliability and factor analysis
|
|
110
124
|
*/
|
|
111
125
|
export function factanal(x, factors, options = {}) {
|
|
126
|
+
checkOptions(options, FACTANAL_KEYS, "factanal"); // generated: option-keys.mjs
|
|
127
|
+
checkOptions(nestedOptions(options, "control"), FACTANAL_CONTROL_KEYS, "factanal", "control"); // generated: option-keys.mjs
|
|
112
128
|
const scores = options.scores ?? "none";
|
|
113
129
|
if (scores !== "none" && scores !== "regression" && scores !== "Bartlett") {
|
|
114
130
|
throw new RangeError("'arg' should be one of “none”, “regression”, “Bartlett”");
|
|
@@ -118,6 +134,7 @@ export function factanal(x, factors, options = {}) {
|
|
|
118
134
|
if (options.covmat !== undefined) {
|
|
119
135
|
if (scores !== "none")
|
|
120
136
|
throw new RangeError("requested scores without an 'x' matrix");
|
|
137
|
+
checkCovmat(options.covmat);
|
|
121
138
|
cv = options.covmat.map((r) => r.slice());
|
|
122
139
|
nObs = options.nObs ?? NaN;
|
|
123
140
|
}
|
|
@@ -153,28 +170,27 @@ export function factanal(x, factors, options = {}) {
|
|
|
153
170
|
return (s + v - S[i][i]) / (v * v);
|
|
154
171
|
});
|
|
155
172
|
};
|
|
156
|
-
const fit = optim(start, objective, {
|
|
173
|
+
const fit = unreported(() => optim(start, objective, {
|
|
157
174
|
gr: gradient,
|
|
158
175
|
method: "L-BFGS-B",
|
|
159
176
|
lower,
|
|
160
177
|
upper: 1,
|
|
161
178
|
control: { fnscale: 1, parscale: new Array(p).fill(0.01) },
|
|
162
|
-
});
|
|
179
|
+
}));
|
|
163
180
|
if (fit.convergence !== 0)
|
|
164
181
|
throw new Error("unable to optimize from this starting value");
|
|
165
182
|
const psi = fit.par;
|
|
166
183
|
let L = orderAndSignColumns(loadingsAt(S, psi, q)).L;
|
|
167
184
|
let rotmat;
|
|
168
185
|
const rotation = options.rotation ?? "varimax";
|
|
186
|
+
if (rotation !== "varimax" && rotation !== "promax" && rotation !== "none")
|
|
187
|
+
throw new RangeError(`unknown rotation '${String(rotation)}'`);
|
|
169
188
|
if (rotation !== "none" && q >= 2) {
|
|
170
189
|
const r = rotation === "varimax" ? varimax(L) : promax(L);
|
|
171
190
|
const sorted = orderAndSignColumns(r.loadings, r.rotmat);
|
|
172
191
|
L = sorted.L;
|
|
173
192
|
rotmat = sorted.rot;
|
|
174
193
|
}
|
|
175
|
-
else if (rotation !== "varimax" && rotation !== "promax" && rotation !== "none") {
|
|
176
|
-
throw new RangeError(`unknown rotation '${String(rotation)}'`);
|
|
177
|
-
}
|
|
178
194
|
let STATISTIC = NaN;
|
|
179
195
|
let PVAL = NaN;
|
|
180
196
|
if (!Number.isNaN(nObs) && dof > 0) {
|
|
@@ -190,6 +206,7 @@ export function factanal(x, factors, options = {}) {
|
|
|
190
206
|
converged: true,
|
|
191
207
|
objective: fit.value,
|
|
192
208
|
counts: fit.counts,
|
|
209
|
+
warnings: [],
|
|
193
210
|
};
|
|
194
211
|
if (scores !== "none")
|
|
195
212
|
out.scores = factorScores(x, S, psi, L, scores);
|
|
@@ -15,11 +15,14 @@ export interface PrettyOptions {
|
|
|
15
15
|
* towards 0 to an integer (as in R); `NaN`, a negative value or one greater than `n` is an error.
|
|
16
16
|
*/
|
|
17
17
|
minN?: number;
|
|
18
|
-
/** Factor applied to the cell when the range is very small. Default `0.75`. */
|
|
18
|
+
/** Factor applied to the cell when the range is very small. Default `0.75`. Must be finite and positive (as in R). */
|
|
19
19
|
shrinkSml?: number;
|
|
20
|
-
/** Bias towards larger units. Default `1.5`. */
|
|
20
|
+
/** Bias towards larger units. Default `1.5`. Must be finite and non-negative (as in R). */
|
|
21
21
|
highUBias?: number;
|
|
22
|
-
/**
|
|
22
|
+
/**
|
|
23
|
+
* Bias towards 5 over 2 times a power of 10. Default `0.5 + 1.5 * highUBias`. Must be finite and non-negative (as
|
|
24
|
+
* in R).
|
|
25
|
+
*/
|
|
23
26
|
u5Bias?: number;
|
|
24
27
|
}
|
|
25
28
|
/**
|
|
@@ -41,8 +44,8 @@ export declare function pretty(x: Sample, options?: PrettyOptions): number[];
|
|
|
41
44
|
*/
|
|
42
45
|
export interface HistOptions {
|
|
43
46
|
/**
|
|
44
|
-
* Break points, a number of cells (a suggestion for pretty), or a
|
|
45
|
-
* `"FD"` (Freedman-Diaconis).
|
|
47
|
+
* Break points (finite: an infinite or NaN one is an error), a number of cells (a suggestion for pretty), or a
|
|
48
|
+
* rule: `"Sturges"` (default), `"Scott"` or `"FD"` (Freedman-Diaconis).
|
|
46
49
|
*/
|
|
47
50
|
breaks?: readonly number[] | number | "Sturges" | "Scott" | "FD" | "Freedman-Diaconis";
|
|
48
51
|
/** Cells closed on the right, (a, b] (default `true`). */
|
package/dist/descriptive/hist.js
CHANGED
|
@@ -11,6 +11,12 @@
|
|
|
11
11
|
import { median, range, sd } from "./basic.js";
|
|
12
12
|
import { IQR, quantile } from "./quantile.js";
|
|
13
13
|
import { seqLength } from "../internal/seq.js";
|
|
14
|
+
import { midpoint, minOf } from "../internal/extremes.js";
|
|
15
|
+
import { ldexp, scaleToUnit } from "../internal/summary.js";
|
|
16
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
17
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
18
|
+
const HIST_KEYS = ["breaks", "right", "includeLowest"];
|
|
19
|
+
const PRETTY_KEYS = ["n", "minN", "shrinkSml", "highUBias", "u5Bias"];
|
|
14
20
|
/**
|
|
15
21
|
* About n + 1 equally spaced "round" values (1, 2 or 5 times a power of 10) covering the range of `x`. Equivalent to
|
|
16
22
|
* R's `pretty()`.
|
|
@@ -23,6 +29,7 @@ import { seqLength } from "../internal/seq.js";
|
|
|
23
29
|
* @category Descriptive statistics
|
|
24
30
|
*/
|
|
25
31
|
export function pretty(x, options = {}) {
|
|
32
|
+
checkOptions(options, PRETTY_KEYS, "pretty"); // generated: option-keys.mjs
|
|
26
33
|
const finite = Array.from(x).filter(Number.isFinite);
|
|
27
34
|
if (finite.length === 0)
|
|
28
35
|
return [];
|
|
@@ -36,9 +43,17 @@ export function pretty(x, options = {}) {
|
|
|
36
43
|
throw new RangeError("invalid 'min.n' argument");
|
|
37
44
|
}
|
|
38
45
|
const minN = Math.trunc(minNGiven);
|
|
46
|
+
// R's checks (after n and min.n): shrink.sml finite and positive, the biases finite and non-negative. A NaN or
|
|
47
|
+
// non-positive shrink.sml left the unit NaN, and the search for a finite multiple of it did not end (#330)
|
|
39
48
|
const shrink = options.shrinkSml ?? 0.75;
|
|
49
|
+
if (!Number.isFinite(shrink) || shrink <= 0)
|
|
50
|
+
throw new RangeError("invalid 'shrink.sml' argument");
|
|
40
51
|
const h = options.highUBias ?? 1.5;
|
|
52
|
+
if (!Number.isFinite(h) || h < 0)
|
|
53
|
+
throw new RangeError("invalid 'high.u.bias' argument");
|
|
41
54
|
const h5 = options.u5Bias ?? 0.5 + 1.5 * h;
|
|
55
|
+
if (!Number.isFinite(h5) || h5 < 0)
|
|
56
|
+
throw new RangeError("invalid 'u5.bias' argument");
|
|
42
57
|
let lo = Infinity;
|
|
43
58
|
let up = -Infinity;
|
|
44
59
|
for (const v of finite) {
|
|
@@ -130,15 +145,8 @@ export function pretty(x, options = {}) {
|
|
|
130
145
|
// Step 6: the sequence, with values that are zero up to rounding set to 0
|
|
131
146
|
if (k <= 0)
|
|
132
147
|
return [L];
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
out = seqLength(L, U, k + 1);
|
|
136
|
-
else {
|
|
137
|
-
// U - L overflows: step by U/k - L/k instead
|
|
138
|
-
const step = U / k - L / k;
|
|
139
|
-
out = Array.from({ length: k + 1 }, (_, i) => L + i * step);
|
|
140
|
-
out[k] = U;
|
|
141
|
-
}
|
|
148
|
+
// seqLength works in half coordinates when U - L overflows (L + i (U/k - L/k) overflowed at interior points, #81)
|
|
149
|
+
const out = seqLength(L, U, k + 1);
|
|
142
150
|
const delta = U / k - L / k;
|
|
143
151
|
return out.map((v) => (Math.abs(v) < 1e-14 * delta ? 0 : v));
|
|
144
152
|
}
|
|
@@ -151,39 +159,55 @@ function round5(v) {
|
|
|
151
159
|
/** Suggested number of classes for the data `xs` (finite values) by the named rule */
|
|
152
160
|
function classCount(xs, rule) {
|
|
153
161
|
const n = xs.length;
|
|
154
|
-
const [lo, hi] = range(xs);
|
|
155
|
-
const r = hi - lo;
|
|
156
162
|
switch (rule.toLowerCase()) {
|
|
157
163
|
case "sturges":
|
|
158
164
|
return Math.ceil(Math.log2(n) + 1);
|
|
159
165
|
case "scott": {
|
|
160
|
-
const
|
|
161
|
-
|
|
166
|
+
const scott = (v) => [range(v), 3.5 * sd(v) * n ** (-1 / 3)];
|
|
167
|
+
let [[a, b], h] = scott(xs);
|
|
168
|
+
// the range or the width overflows (#230): both from the data divided by a power of 2 (the ratio is scale free)
|
|
169
|
+
if (!(Number.isFinite(b - a) && Number.isFinite(h)))
|
|
170
|
+
[[a, b], h] = scott(scaleToUnit([xs]).groups[0]);
|
|
171
|
+
return h > 0 ? Math.max(1, Math.ceil((b - a) / h)) : 1;
|
|
162
172
|
}
|
|
163
173
|
case "fd":
|
|
164
174
|
case "freedman-diaconis": {
|
|
175
|
+
// the range and the width from the data v and the data rounded to 5 significant digits vr
|
|
176
|
+
const fd = (v, vr) => {
|
|
177
|
+
let h = 2 * IQR(vr);
|
|
178
|
+
for (let alpha = 1 / 8; h === 0 && alpha >= 1 / 512; alpha /= 2) {
|
|
179
|
+
const [qa, qb] = quantile(vr, [alpha, 1 - alpha]);
|
|
180
|
+
h = (qb - qa) / (1 - 2 * alpha);
|
|
181
|
+
}
|
|
182
|
+
if (h === 0)
|
|
183
|
+
h = 3.5 * sd(v);
|
|
184
|
+
return [range(v), h];
|
|
185
|
+
};
|
|
165
186
|
const xr = xs.map(round5);
|
|
166
|
-
let h =
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
187
|
+
let [[a, b], h] = fd(xs, xr);
|
|
188
|
+
// the range or the width overflows (#231): both from the data divided by a power of 2, the rounding to 5 digits
|
|
189
|
+
// (not scale free) done first
|
|
190
|
+
if (!(Number.isFinite(b - a) && Number.isFinite(h))) {
|
|
191
|
+
const { k } = scaleToUnit([xs]);
|
|
192
|
+
[[a, b], h] = fd(xs.map((v) => ldexp(v, -k)), xr.map((v) => ldexp(v, -k)));
|
|
170
193
|
}
|
|
171
|
-
|
|
172
|
-
h = 3.5 * sd(xs);
|
|
173
|
-
return h > 0 ? Math.ceil((r / h) * n ** (1 / 3)) : 1;
|
|
194
|
+
return h > 0 ? Math.ceil(((b - a) / h) * n ** (1 / 3)) : 1;
|
|
174
195
|
}
|
|
175
196
|
default:
|
|
176
197
|
throw new RangeError("unknown 'breaks' algorithm");
|
|
177
198
|
}
|
|
178
199
|
}
|
|
179
|
-
/**
|
|
180
|
-
|
|
200
|
+
/**
|
|
201
|
+
* The counting fuzz, 1e-7 times a reference cell size: the median width (> 5 breaks), the data range [lo, hi] (<= 3)
|
|
202
|
+
* or the smallest positive width. Where the range of finite data overflows (#258), 2 (1e-7 (hi / 2 - lo / 2))
|
|
203
|
+
*/
|
|
204
|
+
function fuzz(br, lo, hi) {
|
|
181
205
|
const widths = br.slice(1).map((v, i) => v - br[i]);
|
|
182
206
|
if (br.length > 5)
|
|
183
|
-
return median(widths);
|
|
207
|
+
return 1e-7 * median(widths);
|
|
184
208
|
if (br.length <= 3)
|
|
185
|
-
return
|
|
186
|
-
return
|
|
209
|
+
return hi - lo === Infinity ? 2 * (1e-7 * (hi / 2 - lo / 2)) : 1e-7 * (hi - lo);
|
|
210
|
+
return 1e-7 * minOf(widths.filter((w) => w > 0));
|
|
187
211
|
}
|
|
188
212
|
/**
|
|
189
213
|
* Histogram counts. Equivalent to R's `hist(x, plot = FALSE)`. Break points (by default pretty points over the range
|
|
@@ -197,6 +221,7 @@ function fuzzScale(br, dataRange) {
|
|
|
197
221
|
* @category Descriptive statistics
|
|
198
222
|
*/
|
|
199
223
|
export function hist(x, options = {}) {
|
|
224
|
+
checkOptions(options, HIST_KEYS, "hist"); // generated: option-keys.mjs
|
|
200
225
|
const xs = Array.from(x).filter(Number.isFinite);
|
|
201
226
|
const n = xs.length;
|
|
202
227
|
const right = options.right ?? true;
|
|
@@ -204,7 +229,12 @@ export function hist(x, options = {}) {
|
|
|
204
229
|
const spec = options.breaks ?? "Sturges";
|
|
205
230
|
let br;
|
|
206
231
|
if (typeof spec !== "string" && typeof spec !== "number" && spec.length > 1) {
|
|
232
|
+
// an infinite cell has density 0 and an infinite or NaN midpoint (R returns them; #229)
|
|
233
|
+
if (!spec.every(Number.isFinite))
|
|
234
|
+
throw new RangeError("'breaks' are not finite");
|
|
207
235
|
br = [...spec].sort((a, b) => a - b);
|
|
236
|
+
if (br.some((v, i) => i > 0 && !(v > br[i - 1])))
|
|
237
|
+
throw new RangeError("'breaks' are not strictly increasing");
|
|
208
238
|
}
|
|
209
239
|
else {
|
|
210
240
|
includeLowest = true;
|
|
@@ -222,7 +252,7 @@ export function hist(x, options = {}) {
|
|
|
222
252
|
const nb = br.length;
|
|
223
253
|
// Breaks moved by a small tolerance so that values on a break up to rounding fall on the intended side
|
|
224
254
|
const [dlo, dhi] = n > 0 ? range(xs) : [0, 0];
|
|
225
|
-
const tau =
|
|
255
|
+
const tau = fuzz(br, dlo, dhi);
|
|
226
256
|
const f = br.map((v, i) => {
|
|
227
257
|
if (right)
|
|
228
258
|
return i === 0 && includeLowest ? v - tau : v + tau;
|
|
@@ -241,10 +271,26 @@ export function hist(x, options = {}) {
|
|
|
241
271
|
}
|
|
242
272
|
if (total < n)
|
|
243
273
|
throw new RangeError("some 'x' not counted; maybe 'breaks' do not span range of 'x'");
|
|
244
|
-
const density = counts.map((c, i) => c
|
|
245
|
-
const mids = counts.map((_, i) =>
|
|
274
|
+
const density = counts.map((c, i) => cellDensity(c, n, br[i], br[i + 1]));
|
|
275
|
+
const mids = counts.map((_, i) => midpoint(br[i + 1], br[i]));
|
|
246
276
|
return { breaks: br, counts, density, mids };
|
|
247
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* count / (n (b - a)), R's density of the cell [a, b]. Where n (b - a) overflows for finite ends (the width itself,
|
|
280
|
+
* #228, or only its product with n, #259), from the width ws (b - a, or b / 2 - a / 2 = (b - a) 2^-s with s = 1 where
|
|
281
|
+
* the width overflows) divided by its power of 2, 2^e: count 2^-(e + s) / (n ws 2^-e). The products by powers of 2 are
|
|
282
|
+
* exact (count is an integer below 2^53), so the density is rounded once (also where it is subnormal). Elsewhere R's
|
|
283
|
+
* expression, unchanged
|
|
284
|
+
*/
|
|
285
|
+
function cellDensity(count, n, a, b) {
|
|
286
|
+
const w = b - a;
|
|
287
|
+
if (!(n * w === Infinity && Number.isFinite(a) && Number.isFinite(b)))
|
|
288
|
+
return count / (n * w);
|
|
289
|
+
const s = w === Infinity ? 1 : 0;
|
|
290
|
+
const ws = s === 1 ? b / 2 - a / 2 : w;
|
|
291
|
+
const e = Math.floor(Math.log2(ws));
|
|
292
|
+
return ldexp(count, -(e + s)) / (n * ldexp(ws, -e));
|
|
293
|
+
}
|
|
248
294
|
/** 0-based cell of `v` among the (shifted) breaks `f`, or -1 when it is in no cell */
|
|
249
295
|
function cellOf(v, f, right, includeLowest) {
|
|
250
296
|
const last = f.length - 1;
|
|
@@ -259,7 +305,7 @@ function cellOf(v, f, right, includeLowest) {
|
|
|
259
305
|
let a = 1;
|
|
260
306
|
let b = last;
|
|
261
307
|
while (a < b) {
|
|
262
|
-
const m = (a + b) >> 1
|
|
308
|
+
const m = Math.floor((a + b) / 2); // not >> 1, which wraps beyond 2^31 breaks (issue #333)
|
|
263
309
|
if (v <= f[m])
|
|
264
310
|
b = m;
|
|
265
311
|
else
|
|
@@ -275,7 +321,7 @@ function cellOf(v, f, right, includeLowest) {
|
|
|
275
321
|
let a = 0;
|
|
276
322
|
let b = last - 1;
|
|
277
323
|
while (a < b) {
|
|
278
|
-
const m = (a + b + 1)
|
|
324
|
+
const m = Math.floor((a + b + 1) / 2);
|
|
279
325
|
if (f[m] <= v)
|
|
280
326
|
a = m;
|
|
281
327
|
else
|
|
@@ -307,7 +353,7 @@ export function ecdf(x) {
|
|
|
307
353
|
let lo = 0;
|
|
308
354
|
let hi = n;
|
|
309
355
|
while (lo < hi) {
|
|
310
|
-
const mid = (lo + hi) >> 1
|
|
356
|
+
const mid = Math.floor((lo + hi) / 2); // not >> 1, which wraps beyond 2^31 elements (issue #332)
|
|
311
357
|
if (s[mid] <= q)
|
|
312
358
|
lo = mid + 1;
|
|
313
359
|
else
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Written for rStats.js (no ported code) from oracle/formulatests-behaviour.md. A value is missing when it is `null`,
|
|
4
4
|
// or `NaN` (in a numeric column); a row is complete when no column of any argument is missing there.
|
|
5
|
+
import { addColumn } from "./dataFrames.js";
|
|
5
6
|
const isMissing = (v) => v === null || (typeof v === "number" && Number.isNaN(v));
|
|
6
7
|
const isVector = (x) => Array.isArray(x) || ArrayBuffer.isView(x);
|
|
7
8
|
/** The columns of an argument (a vector is one column) and its number of rows */
|
|
@@ -46,7 +47,8 @@ export function naOmit(x) {
|
|
|
46
47
|
const out = {};
|
|
47
48
|
for (const [name, column] of Object.entries(x)) {
|
|
48
49
|
const kept = Array.from(column).filter((_, i) => keep[i]);
|
|
49
|
-
|
|
50
|
+
// (addColumn: a column named "__proto__" stays an own column; issue #35)
|
|
51
|
+
addColumn(out, name, column instanceof Float64Array ? Float64Array.from(kept) : kept);
|
|
50
52
|
}
|
|
51
53
|
return out;
|
|
52
54
|
}
|
|
@@ -7,16 +7,36 @@
|
|
|
7
7
|
// (Joanes and Gill, The Statistician 47, 1998), type 3 (the default, as in MINITAB and BMDP) b1 = g1 ((n-1)/n)^1.5 and
|
|
8
8
|
// b2 = (g2 + 3) ((n-1)/n)^2 - 3. The central moments are summed after subtracting the double-double mean.
|
|
9
9
|
import { values } from "./basic.js";
|
|
10
|
-
import { meanDD, mean as meanOf, variance } from "../internal/summary.js";
|
|
10
|
+
import { ldexp, meanDD, mean as meanOf, scaleToUnit, variance } from "../internal/summary.js";
|
|
11
11
|
import { median } from "./basic.js";
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
import { maxOf } from "../internal/extremes.js";
|
|
13
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
14
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
15
|
+
const SKEWNESS_KEYS = ["type"];
|
|
16
|
+
const KURTOSIS_KEYS = ["type"];
|
|
17
|
+
const GEOMETRIC_MEAN_KEYS = ["naRm"];
|
|
18
|
+
const MEAN_AD_KEYS = ["center"];
|
|
19
|
+
const CV_KEYS = ["unbiased", "naRm"];
|
|
20
|
+
const SCALE_KEYS = ["center", "scale"];
|
|
21
|
+
/**
|
|
22
|
+
* Central sums sum (x - mean)^2, ^3, ^4 of values without NaN, of the data scaled by a power of 2 (exact) first, so
|
|
23
|
+
* that neither the deviations nor their powers overflow or underflow (R returns NaN there). The sums are those of the
|
|
24
|
+
* scaled deviations; only the scale-free ratios of skewness and kurtosis are taken from them. Deviations that are
|
|
25
|
+
* still tiny next to the values are scaled up as well.
|
|
26
|
+
*/
|
|
27
|
+
function centralSums(values) {
|
|
28
|
+
const xs = scaleToUnit([values]).groups[0];
|
|
14
29
|
const m = meanDD(xs);
|
|
30
|
+
const dev = xs.map((v) => v - m[0] - m[1]);
|
|
31
|
+
let mx = 0;
|
|
32
|
+
for (const d of dev)
|
|
33
|
+
mx = Math.max(mx, Math.abs(d));
|
|
34
|
+
const f = mx > 0 && (mx < 2 ** -120 || mx > 2 ** 120) ? 2 ** -Math.floor(Math.log2(mx)) : 1;
|
|
15
35
|
let s2 = 0;
|
|
16
36
|
let s3 = 0;
|
|
17
37
|
let s4 = 0;
|
|
18
|
-
for (const
|
|
19
|
-
const d =
|
|
38
|
+
for (const dv of dev) {
|
|
39
|
+
const d = dv * f;
|
|
20
40
|
const d2 = d * d;
|
|
21
41
|
s2 += d2;
|
|
22
42
|
s3 += d2 * d;
|
|
@@ -41,6 +61,7 @@ function checkType(type) {
|
|
|
41
61
|
* @category Descriptive statistics
|
|
42
62
|
*/
|
|
43
63
|
export function skewness(x, options = {}) {
|
|
64
|
+
checkOptions(options, SKEWNESS_KEYS, "skewness"); // generated: option-keys.mjs
|
|
44
65
|
const type = options.type ?? 3;
|
|
45
66
|
checkType(type);
|
|
46
67
|
const xs = values(x, true);
|
|
@@ -69,6 +90,7 @@ export function skewness(x, options = {}) {
|
|
|
69
90
|
* @category Descriptive statistics
|
|
70
91
|
*/
|
|
71
92
|
export function kurtosis(x, options = {}) {
|
|
93
|
+
checkOptions(options, KURTOSIS_KEYS, "kurtosis"); // generated: option-keys.mjs
|
|
72
94
|
const type = options.type ?? 3;
|
|
73
95
|
checkType(type);
|
|
74
96
|
const xs = values(x, true);
|
|
@@ -96,6 +118,7 @@ export function kurtosis(x, options = {}) {
|
|
|
96
118
|
* @category Descriptive statistics
|
|
97
119
|
*/
|
|
98
120
|
export function geometricMean(x, options = {}) {
|
|
121
|
+
checkOptions(options, GEOMETRIC_MEAN_KEYS, "geometricMean"); // generated: option-keys.mjs
|
|
99
122
|
const xs = values(x, options.naRm);
|
|
100
123
|
if (xs === undefined)
|
|
101
124
|
return NaN;
|
|
@@ -142,10 +165,14 @@ export function modes(x) {
|
|
|
142
165
|
* @category Descriptive statistics
|
|
143
166
|
*/
|
|
144
167
|
export function meanAD(x, options = {}) {
|
|
145
|
-
|
|
168
|
+
checkOptions(options, MEAN_AD_KEYS, "meanAD"); // generated: option-keys.mjs
|
|
146
169
|
const c = options.center ?? "mean";
|
|
147
|
-
|
|
148
|
-
|
|
170
|
+
// the deviations of the values (and a given centre) scaled by a power of 2 (exact), so that they cannot overflow;
|
|
171
|
+
// the mean deviation is scaled back
|
|
172
|
+
const { groups, k } = scaleToUnit([values(x, true), typeof c === "number" ? [c] : []]);
|
|
173
|
+
const xs = groups[0];
|
|
174
|
+
const center = c === "mean" ? meanOf(xs) : c === "median" ? median(xs) : groups[1][0];
|
|
175
|
+
return ldexp(meanOf(xs.map((v) => Math.abs(v - center))), k);
|
|
149
176
|
}
|
|
150
177
|
/**
|
|
151
178
|
* Coefficient of variation sd / mean (sample standard deviation). Equivalent to `DescTools::CoefVar()` in R.
|
|
@@ -158,15 +185,34 @@ export function meanAD(x, options = {}) {
|
|
|
158
185
|
* @category Descriptive statistics
|
|
159
186
|
*/
|
|
160
187
|
export function cv(x, options = {}) {
|
|
188
|
+
checkOptions(options, CV_KEYS, "cv"); // generated: option-keys.mjs
|
|
161
189
|
const xs = values(x, options.naRm);
|
|
162
190
|
if (xs === undefined)
|
|
163
191
|
return NaN;
|
|
164
192
|
const n = xs.length;
|
|
165
|
-
|
|
193
|
+
// sd and mean of the values scaled by a power of 2 (exact): their ratio has no units
|
|
194
|
+
const xu = scaleToUnit([xs]).groups[0];
|
|
195
|
+
let res = Math.sqrt(variance(xu)) / meanOf(xu);
|
|
166
196
|
if (options.unbiased)
|
|
167
197
|
res *= 1 - 1 / (4 * (n - 1)) + (res * res) / n + 1 / (2 * (n - 1) ** 2);
|
|
168
198
|
return res;
|
|
169
199
|
}
|
|
200
|
+
/** sqrt(sum v^2 / max(1, n - 1)), the scale of a centred column in R's scale() */
|
|
201
|
+
function rootMeanSquare(v) {
|
|
202
|
+
let ss = 0;
|
|
203
|
+
for (const t of v)
|
|
204
|
+
ss += t * t;
|
|
205
|
+
const mx = maxOf(v.map(Math.abs));
|
|
206
|
+
if ((ss === Infinity || ss < 2 ** -960) && mx > 0 && mx < Infinity) {
|
|
207
|
+
// The squares overflow or underflow (R's scale returns Inf or 0): sum the squares of the values divided by the
|
|
208
|
+
// largest
|
|
209
|
+
ss = 0;
|
|
210
|
+
for (const t of v)
|
|
211
|
+
ss += (t / mx) ** 2;
|
|
212
|
+
return Math.sqrt(ss / Math.max(1, v.length - 1)) * mx;
|
|
213
|
+
}
|
|
214
|
+
return Math.sqrt(ss / Math.max(1, v.length - 1));
|
|
215
|
+
}
|
|
170
216
|
/**
|
|
171
217
|
* Centre and scale the columns of a matrix (array of rows; a vector is one column). Equivalent to R's `scale()`.
|
|
172
218
|
* `center` and `scale` report the values used (R's attributes), when any.
|
|
@@ -179,31 +225,36 @@ export function cv(x, options = {}) {
|
|
|
179
225
|
* @category Descriptive statistics
|
|
180
226
|
*/
|
|
181
227
|
export function scale(x, options = {}) {
|
|
228
|
+
checkOptions(options, SCALE_KEYS, "scale"); // generated: option-keys.mjs
|
|
182
229
|
const rows = x.length > 0 && Array.isArray(x[0]) ? x.map((r) => [...r]) : Array.from(x, (v) => [v]);
|
|
183
230
|
const nc = rows[0]?.length ?? 0;
|
|
231
|
+
if (rows.some((r) => r.length !== nc))
|
|
232
|
+
throw new TypeError("all rows of the matrix must have the same length");
|
|
184
233
|
const col = (j) => rows.map((r) => r[j]);
|
|
185
234
|
const result = { scaled: rows };
|
|
235
|
+
const given = rows.map((r) => r.slice());
|
|
236
|
+
// columns whose deviations overflow although the values and the centre are finite (the deviation of 1.5e308 from
|
|
237
|
+
// -5e307 is 2e308): redone below in the coordinates 2^-k
|
|
238
|
+
const overflowed = [];
|
|
186
239
|
const c = options.center ?? true;
|
|
240
|
+
let center;
|
|
187
241
|
if (c !== false) {
|
|
188
|
-
|
|
242
|
+
center = c === true ? Array.from({ length: nc }, (_, j) => meanOf(col(j).filter((v) => !Number.isNaN(v)))) : [...c];
|
|
189
243
|
if (center.length !== nc)
|
|
190
244
|
throw new RangeError("length of 'center' must equal the number of columns of 'x'");
|
|
191
245
|
for (const r of rows)
|
|
192
246
|
for (let j = 0; j < nc; j++)
|
|
193
247
|
r[j] = r[j] - center[j];
|
|
194
248
|
result.center = center;
|
|
249
|
+
for (let j = 0; j < nc; j++) {
|
|
250
|
+
if (Number.isFinite(center[j]) && rows.some((r, i) => !Number.isFinite(r[j]) && Number.isFinite(given[i][j])))
|
|
251
|
+
overflowed.push(j);
|
|
252
|
+
}
|
|
195
253
|
}
|
|
196
254
|
const s = options.scale ?? true;
|
|
255
|
+
let sc;
|
|
197
256
|
if (s !== false) {
|
|
198
|
-
|
|
199
|
-
? Array.from({ length: nc }, (_, j) => {
|
|
200
|
-
const v = col(j).filter((t) => !Number.isNaN(t));
|
|
201
|
-
let ss = 0;
|
|
202
|
-
for (const t of v)
|
|
203
|
-
ss += t * t;
|
|
204
|
-
return Math.sqrt(ss / Math.max(1, v.length - 1));
|
|
205
|
-
})
|
|
206
|
-
: [...s];
|
|
257
|
+
sc = s === true ? Array.from({ length: nc }, (_, j) => rootMeanSquare(col(j).filter((t) => !Number.isNaN(t)))) : [...s];
|
|
207
258
|
if (sc.length !== nc)
|
|
208
259
|
throw new RangeError("length of 'scale' must equal the number of columns of 'x'");
|
|
209
260
|
for (const r of rows)
|
|
@@ -211,5 +262,21 @@ export function scale(x, options = {}) {
|
|
|
211
262
|
r[j] = r[j] / sc[j];
|
|
212
263
|
result.scale = sc;
|
|
213
264
|
}
|
|
265
|
+
for (const j of overflowed) {
|
|
266
|
+
const k = Math.floor(Math.log2(maxOf(given.map((r) => Math.abs(r[j])).filter(Number.isFinite))));
|
|
267
|
+
const cs = ldexp(center[j], -k);
|
|
268
|
+
const d = given.map((r) => ldexp(r[j], -k) - cs);
|
|
269
|
+
let divisor = 1;
|
|
270
|
+
if (sc !== undefined) {
|
|
271
|
+
if (s === true) {
|
|
272
|
+
divisor = rootMeanSquare(d.filter((t) => !Number.isNaN(t)));
|
|
273
|
+
sc[j] = ldexp(divisor, k);
|
|
274
|
+
}
|
|
275
|
+
else
|
|
276
|
+
divisor = ldexp(sc[j], -k);
|
|
277
|
+
}
|
|
278
|
+
// without scaling the deviations go back by 2^k (one that is out of range is Infinity, as before)
|
|
279
|
+
rows.forEach((r, i) => (r[j] = sc === undefined ? ldexp(d[i], k) : d[i] / divisor));
|
|
280
|
+
}
|
|
214
281
|
return result;
|
|
215
282
|
}
|
|
@@ -54,7 +54,7 @@ export interface OmegaResult {
|
|
|
54
54
|
* `option = "equal"`, `flip = TRUE`).
|
|
55
55
|
*
|
|
56
56
|
* @param x The data matrix (array of rows, NaN for missing values: correlations of pairwise complete observations),
|
|
57
|
-
* or `null` with `options.covmat`.
|
|
57
|
+
* or `null` with `options.covmat`. At least 2 variables.
|
|
58
58
|
* @param options See {@link OmegaOptions}.
|
|
59
59
|
*
|
|
60
60
|
* @example
|