@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/data/grouping.js
CHANGED
|
@@ -11,6 +11,10 @@ import { formatR } from "../htest/core/simulate.js";
|
|
|
11
11
|
import { numberLevels } from "../internal/levels.js";
|
|
12
12
|
import { expandRhs, formulaVariables, parseFormula } from "../models/formula.js";
|
|
13
13
|
import { compareCodePoints } from "../models/modelFrame.js";
|
|
14
|
+
import { addColumn } from "../descriptive/dataFrames.js";
|
|
15
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
16
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
17
|
+
const SPLIT_KEYS = ["drop", "sep"];
|
|
14
18
|
const isMissing = (v) => v === null || (typeof v === "number" && Number.isNaN(v));
|
|
15
19
|
const levelOrder = (a, b) => typeof a === "number" ? a - b : typeof a === "string" ? compareCodePoints(a, b) : Number(a) - Number(b);
|
|
16
20
|
/** A level as R names it: numbers as as.character (15 significant digits), booleans as TRUE and FALSE */
|
|
@@ -48,6 +52,10 @@ function groupingOf(lists, names, n) {
|
|
|
48
52
|
});
|
|
49
53
|
const factors = lists.map((l, j) => factorOf(names[j], l));
|
|
50
54
|
const size = factors.reduce((s, f) => s * f.levels.length, 1);
|
|
55
|
+
// R's tapply limit, before the per-cell arrays: 2^32 cells failed with V8's "Invalid array length", and from 2^31
|
|
56
|
+
// up the allocation was attempted (#363)
|
|
57
|
+
if (size >= 2 ** 31)
|
|
58
|
+
throw new RangeError("total number of levels >= 2^31");
|
|
51
59
|
const cells = new Array(n);
|
|
52
60
|
for (let i = 0; i < n; i++) {
|
|
53
61
|
let cell = 0;
|
|
@@ -114,7 +122,8 @@ export function tapply(x, index, fun) {
|
|
|
114
122
|
function rowsOf(data, rows) {
|
|
115
123
|
const out = {};
|
|
116
124
|
for (const [name, col] of Object.entries(data)) {
|
|
117
|
-
|
|
125
|
+
// (addColumn: a column named "__proto__" stays an own column; issue #35)
|
|
126
|
+
addColumn(out, name, col instanceof Float64Array ? Float64Array.from(rows, (i) => col[i]) : rows.map((i) => col[i]));
|
|
118
127
|
}
|
|
119
128
|
return out;
|
|
120
129
|
}
|
|
@@ -130,6 +139,7 @@ function rowCount(data) {
|
|
|
130
139
|
return n;
|
|
131
140
|
}
|
|
132
141
|
export function split(x, f, options = {}) {
|
|
142
|
+
checkOptions(options, SPLIT_KEYS, "split"); // generated: option-keys.mjs
|
|
133
143
|
const sep = options.sep ?? ".";
|
|
134
144
|
const lists = labelLists(f);
|
|
135
145
|
const frame = !isLabelArray(x);
|
|
@@ -143,6 +153,9 @@ export function split(x, f, options = {}) {
|
|
|
143
153
|
if (rows.length === 0 && (options.drop || g.factors.length === 1))
|
|
144
154
|
return;
|
|
145
155
|
const name = cellLevels(g, cell).map(levelName).join(sep);
|
|
156
|
+
// Two different combinations of levels whose names joined with sep coincide would overwrite one another
|
|
157
|
+
if (out.has(name))
|
|
158
|
+
throw new RangeError(`the group names joined with '${sep}' are not unique ('${name}'); use another sep`);
|
|
146
159
|
out.set(name, frame ? rowsOf(x, rows) : rows.map((i) => xs[i]));
|
|
147
160
|
});
|
|
148
161
|
return out;
|
|
@@ -150,7 +163,7 @@ export function split(x, f, options = {}) {
|
|
|
150
163
|
/** A summarised column as numbers (null is NaN) */
|
|
151
164
|
function numericColumn(name, col) {
|
|
152
165
|
if (col === undefined)
|
|
153
|
-
throw new
|
|
166
|
+
throw new RangeError(`object '${name}' not found`);
|
|
154
167
|
if (!isLabelArray(col))
|
|
155
168
|
throw new TypeError(`column '${name}' must be an array`);
|
|
156
169
|
return Array.from(col, (v) => {
|
|
@@ -164,7 +177,7 @@ function numericColumn(name, col) {
|
|
|
164
177
|
function labelColumn(data, name) {
|
|
165
178
|
const col = Object.prototype.hasOwnProperty.call(data, name) ? data[name] : undefined;
|
|
166
179
|
if (col === undefined)
|
|
167
|
-
throw new
|
|
180
|
+
throw new RangeError(`object '${name}' not found`);
|
|
168
181
|
if (!isLabelArray(col))
|
|
169
182
|
throw new TypeError(`column '${name}' must be an array`);
|
|
170
183
|
return col;
|
|
@@ -214,11 +227,17 @@ function aggregateRows(valueNames, valueCols, groupNames, groupCols, keep, fun)
|
|
|
214
227
|
// The labels of each group's first row (-0 as 0), as R keeps them
|
|
215
228
|
groupNames.forEach((name, j) => {
|
|
216
229
|
const col = groupCols[j];
|
|
217
|
-
out
|
|
230
|
+
addColumn(out, name, cells.map((c) => {
|
|
218
231
|
const v = col[c.rows[0]];
|
|
219
232
|
return v === 0 ? 0 : v;
|
|
220
|
-
});
|
|
233
|
+
}));
|
|
221
234
|
});
|
|
235
|
+
// A result column whose name is already taken (by a grouping column or another result) would overwrite it
|
|
236
|
+
const setColumn = (name, v) => {
|
|
237
|
+
if (Object.hasOwn(out, name))
|
|
238
|
+
throw new RangeError(`duplicate column name '${name}' in the result`);
|
|
239
|
+
addColumn(out, name, v);
|
|
240
|
+
};
|
|
222
241
|
valueNames.forEach((name, j) => {
|
|
223
242
|
const col = valueCols[j];
|
|
224
243
|
const results = cells.map((c) => fun(c.rows.map((i) => col[i])));
|
|
@@ -234,12 +253,12 @@ function aggregateRows(valueNames, valueCols, groupNames, groupCols, keep, fun)
|
|
|
234
253
|
if (flat.some((r) => r.values.length !== k))
|
|
235
254
|
throw new RangeError("fun must return values of the same length for every group");
|
|
236
255
|
if (k === 1 && flat[0]?.names === undefined) {
|
|
237
|
-
|
|
256
|
+
setColumn(name, flat.map((r) => r.values[0]));
|
|
238
257
|
return;
|
|
239
258
|
}
|
|
240
259
|
for (let m = 0; m < k; m++) {
|
|
241
260
|
const suffix = flat[0].names?.[m] ?? String(m + 1);
|
|
242
|
-
|
|
261
|
+
setColumn(`${name}.${suffix}`, flat.map((r) => r.values[m]));
|
|
243
262
|
}
|
|
244
263
|
});
|
|
245
264
|
return out;
|
|
@@ -8,8 +8,20 @@
|
|
|
8
8
|
// overflow (src/internal/summary.ts), and products keep a separate power-of-two exponent, with factors far from 1
|
|
9
9
|
// scaled before they are multiplied.
|
|
10
10
|
// As everywhere in rStats.js, NaN stands for R's NA: with `naRm` it is dropped, otherwise it propagates.
|
|
11
|
-
import { ldexp, mean as meanOf, meanDD, sum as sumOf, variance as varianceOf, wideSum, wideSumAdd, wideSumValue, } from "../internal/summary.js";
|
|
11
|
+
import { ldexp, scaleToUnit, mean as meanOf, meanDD, sum as sumOf, standardDeviation, variance as varianceOf, wideSum, wideSumAdd, wideSumValue, } from "../internal/summary.js";
|
|
12
12
|
import { ddMul } from "../special/dd.js";
|
|
13
|
+
import { roundScaled, splitDouble } from "../linalg/dot.js";
|
|
14
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
15
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
16
|
+
const SUM_KEYS = ["naRm"];
|
|
17
|
+
const PROD_KEYS = ["naRm"];
|
|
18
|
+
const MEAN_KEYS = ["trim", "naRm"];
|
|
19
|
+
const WEIGHTED_MEAN_KEYS = ["naRm"];
|
|
20
|
+
const VARIANCE_KEYS = ["naRm"];
|
|
21
|
+
const SD_KEYS = ["naRm"];
|
|
22
|
+
const MEDIAN_KEYS = ["naRm"];
|
|
23
|
+
const RANGE_KEYS = ["naRm"];
|
|
24
|
+
const DIFF_KEYS = ["lag", "differences"];
|
|
13
25
|
/** The values as an array, NaN dropped when naRm; undefined when a NaN remains (the result is then NaN) */
|
|
14
26
|
export function values(x, naRm) {
|
|
15
27
|
const xs = Array.from(x);
|
|
@@ -28,6 +40,7 @@ export function values(x, naRm) {
|
|
|
28
40
|
* @category Descriptive statistics
|
|
29
41
|
*/
|
|
30
42
|
export function sum(x, options = {}) {
|
|
43
|
+
checkOptions(options, SUM_KEYS, "sum"); // generated: option-keys.mjs
|
|
31
44
|
const xs = values(x, options.naRm);
|
|
32
45
|
return xs === undefined ? NaN : sumOf(xs);
|
|
33
46
|
}
|
|
@@ -42,6 +55,7 @@ export function sum(x, options = {}) {
|
|
|
42
55
|
* @category Descriptive statistics
|
|
43
56
|
*/
|
|
44
57
|
export function prod(x, options = {}) {
|
|
58
|
+
checkOptions(options, PROD_KEYS, "prod"); // generated: option-keys.mjs
|
|
45
59
|
const xs = values(x, options.naRm);
|
|
46
60
|
if (xs === undefined)
|
|
47
61
|
return NaN;
|
|
@@ -93,15 +107,19 @@ function prodSign(acc) {
|
|
|
93
107
|
* @category Descriptive statistics
|
|
94
108
|
*/
|
|
95
109
|
export function mean(x, options = {}) {
|
|
110
|
+
checkOptions(options, MEAN_KEYS, "mean"); // generated: option-keys.mjs
|
|
96
111
|
const xs = values(x, options.naRm);
|
|
112
|
+
let trim = options.trim ?? 0;
|
|
113
|
+
// R tests `trim > 0 && n` with n the length after na.rm, before looking at missing values: a missing value in x
|
|
114
|
+
// (n >= 1) is an error too, only an empty x escapes (mean(c(1, NA), trim = NaN) stops in R)
|
|
115
|
+
if (Number.isNaN(trim) && (xs === undefined || xs.length > 0)) {
|
|
116
|
+
throw new RangeError("'trim' must be numeric of length one");
|
|
117
|
+
}
|
|
97
118
|
if (xs === undefined)
|
|
98
119
|
return NaN;
|
|
99
120
|
const n = xs.length;
|
|
100
121
|
if (n === 0)
|
|
101
122
|
return NaN;
|
|
102
|
-
let trim = options.trim ?? 0;
|
|
103
|
-
if (Number.isNaN(trim))
|
|
104
|
-
throw new RangeError("'trim' must be numeric of length one");
|
|
105
123
|
if (trim > 0 && n > 0) {
|
|
106
124
|
if (trim >= 0.5)
|
|
107
125
|
return median(xs);
|
|
@@ -124,6 +142,7 @@ export function mean(x, options = {}) {
|
|
|
124
142
|
* @category Descriptive statistics
|
|
125
143
|
*/
|
|
126
144
|
export function weightedMean(x, w, options = {}) {
|
|
145
|
+
checkOptions(options, WEIGHTED_MEAN_KEYS, "weightedMean"); // generated: option-keys.mjs
|
|
127
146
|
let xs = Array.from(x);
|
|
128
147
|
let ws = Array.from(w);
|
|
129
148
|
if (xs.length !== ws.length)
|
|
@@ -134,10 +153,84 @@ export function weightedMean(x, w, options = {}) {
|
|
|
134
153
|
ws = ws.filter((_, i) => keep[i]);
|
|
135
154
|
}
|
|
136
155
|
const prods = [];
|
|
137
|
-
|
|
156
|
+
// a product of nonzero factors below the normal range has lost bits (1e-200 * 1e-200 is 0, #364)
|
|
157
|
+
let lost = false;
|
|
158
|
+
for (let i = 0; i < xs.length; i++) {
|
|
159
|
+
if (ws[i] === 0)
|
|
160
|
+
continue;
|
|
161
|
+
const p = xs[i] * ws[i];
|
|
162
|
+
if (xs[i] !== 0 && Math.abs(p) < 2 ** -1022)
|
|
163
|
+
lost = true;
|
|
164
|
+
prods.push(p);
|
|
165
|
+
}
|
|
166
|
+
const sw = sumOf(ws);
|
|
167
|
+
const m = sumOf(prods) / sw;
|
|
168
|
+
if ((Number.isFinite(m) && Number.isFinite(sw) && !lost) || !xs.every(Number.isFinite) || !ws.every(Number.isFinite)) {
|
|
169
|
+
return m;
|
|
170
|
+
}
|
|
171
|
+
// Exact quotient where a product underflowed, or where the scaling below would lose a weight: signed weights whose
|
|
172
|
+
// sum cancels to far below the largest weight (1e-308 next to 1e308 and -1e308 became 0 when divided by 2^1024, so
|
|
173
|
+
// the mean was 0 / 0, #334)
|
|
174
|
+
const exact = exactWeightedMean(xs, ws);
|
|
175
|
+
if (exact !== undefined)
|
|
176
|
+
return exact;
|
|
177
|
+
// Finite values whose products or sums overflow (R returns Inf, NaN, or 0 where only the sum of the weights
|
|
178
|
+
// overflows, #201): the values of nonzero weight and the weights divided by powers of 2 that bring their largest
|
|
179
|
+
// absolute values into [1, 2), so that neither the products nor the sums overflow, and small values are not lost to
|
|
180
|
+
// the division (dividing by the largest values themselves turned 1e-308 / 1e308 into 0)
|
|
181
|
+
const used = xs.filter((_, i) => ws[i] !== 0);
|
|
182
|
+
const { groups: [sx], k } = scaleToUnit([used]);
|
|
183
|
+
const { groups: [sws] } = scaleToUnit([ws]);
|
|
184
|
+
const scaled = [];
|
|
185
|
+
for (let i = 0, j = 0; i < xs.length; i++)
|
|
138
186
|
if (ws[i] !== 0)
|
|
139
|
-
|
|
140
|
-
return sumOf(
|
|
187
|
+
scaled.push(sx[j++] * sws[i]);
|
|
188
|
+
return ldexp(sumOf(scaled) / sumOf(sws), k);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* sum(x w) / sum(w) for finite x and w, each sum exact in BigInt (the products as integer mantissas times powers of
|
|
192
|
+
* 2) and the quotient rounded once. undefined where the sum of the weights is exactly 0 (no mean)
|
|
193
|
+
*/
|
|
194
|
+
function exactWeightedMean(xs, ws) {
|
|
195
|
+
const num = [];
|
|
196
|
+
const den = [];
|
|
197
|
+
for (let i = 0; i < xs.length; i++) {
|
|
198
|
+
if (ws[i] === 0)
|
|
199
|
+
continue;
|
|
200
|
+
const [mw, ew] = splitDouble(ws[i]);
|
|
201
|
+
den.push([mw, ew]);
|
|
202
|
+
if (xs[i] === 0)
|
|
203
|
+
continue;
|
|
204
|
+
const [mx, ex] = splitDouble(xs[i]);
|
|
205
|
+
num.push([mx * mw, ex + ew]);
|
|
206
|
+
}
|
|
207
|
+
const exactSum = (terms) => {
|
|
208
|
+
let low = 0;
|
|
209
|
+
for (const [, e] of terms)
|
|
210
|
+
low = Math.min(low, e);
|
|
211
|
+
let s = 0n;
|
|
212
|
+
for (const [m, e] of terms)
|
|
213
|
+
s += m << BigInt(e - low);
|
|
214
|
+
return [s, low];
|
|
215
|
+
};
|
|
216
|
+
let [n, ln] = exactSum(num);
|
|
217
|
+
let [d, ld] = exactSum(den);
|
|
218
|
+
if (d === 0n)
|
|
219
|
+
return undefined;
|
|
220
|
+
if (n === 0n)
|
|
221
|
+
return 0;
|
|
222
|
+
if (d < 0n) {
|
|
223
|
+
n = -n;
|
|
224
|
+
d = -d;
|
|
225
|
+
}
|
|
226
|
+
// n / d 2^(ln - ld): the integer quotient with at least 66 bits, its last bit set where it is not exact
|
|
227
|
+
const negative = n < 0n;
|
|
228
|
+
const a = negative ? -n : n;
|
|
229
|
+
const shift = Math.max(0, d.toString(2).length - a.toString(2).length + 66);
|
|
230
|
+
let q = (a << BigInt(shift)) / d;
|
|
231
|
+
if (q * d !== a << BigInt(shift))
|
|
232
|
+
q |= 1n;
|
|
233
|
+
return roundScaled(negative ? -q : q, ln - ld - shift);
|
|
141
234
|
}
|
|
142
235
|
/**
|
|
143
236
|
* Sample variance (denominator n - 1). Equivalent to R's `var()`. For a vector (`NaN` for fewer than 2 values); named
|
|
@@ -151,6 +244,7 @@ export function weightedMean(x, w, options = {}) {
|
|
|
151
244
|
* @category Descriptive statistics
|
|
152
245
|
*/
|
|
153
246
|
export function variance(x, options = {}) {
|
|
247
|
+
checkOptions(options, VARIANCE_KEYS, "variance"); // generated: option-keys.mjs
|
|
154
248
|
const xs = values(x, options.naRm);
|
|
155
249
|
if (xs === undefined)
|
|
156
250
|
return NaN;
|
|
@@ -167,7 +261,12 @@ export function variance(x, options = {}) {
|
|
|
167
261
|
* @category Descriptive statistics
|
|
168
262
|
*/
|
|
169
263
|
export function sd(x, options = {}) {
|
|
170
|
-
|
|
264
|
+
checkOptions(options, SD_KEYS, "sd"); // generated: option-keys.mjs
|
|
265
|
+
// not sqrt(variance): the variance can overflow or underflow where the standard deviation is in range
|
|
266
|
+
const xs = values(x, options.naRm);
|
|
267
|
+
if (xs === undefined)
|
|
268
|
+
return NaN;
|
|
269
|
+
return standardDeviation(xs);
|
|
171
270
|
}
|
|
172
271
|
/**
|
|
173
272
|
* Median of the values (the mean of the two middle ones for an even count). Equivalent to R's `median()`.
|
|
@@ -181,6 +280,7 @@ export function sd(x, options = {}) {
|
|
|
181
280
|
* @category Descriptive statistics
|
|
182
281
|
*/
|
|
183
282
|
export function median(x, options = {}) {
|
|
283
|
+
checkOptions(options, MEDIAN_KEYS, "median"); // generated: option-keys.mjs
|
|
184
284
|
const xs = values(x, options.naRm);
|
|
185
285
|
if (xs === undefined)
|
|
186
286
|
return NaN;
|
|
@@ -204,6 +304,7 @@ export function median(x, options = {}) {
|
|
|
204
304
|
* @category Descriptive statistics
|
|
205
305
|
*/
|
|
206
306
|
export function range(x, options = {}) {
|
|
307
|
+
checkOptions(options, RANGE_KEYS, "range"); // generated: option-keys.mjs
|
|
207
308
|
const xs = values(x, options.naRm);
|
|
208
309
|
if (xs === undefined)
|
|
209
310
|
return [NaN, NaN];
|
|
@@ -347,6 +448,7 @@ export function cummin(x) {
|
|
|
347
448
|
* @category Descriptive statistics
|
|
348
449
|
*/
|
|
349
450
|
export function diff(x, options = {}) {
|
|
451
|
+
checkOptions(options, DIFF_KEYS, "diff"); // generated: option-keys.mjs
|
|
350
452
|
const lag = options.lag ?? 1;
|
|
351
453
|
const differences = options.differences ?? 1;
|
|
352
454
|
if (!(Number.isInteger(lag) && lag >= 1 && Number.isInteger(differences) && differences >= 1)) {
|
package/dist/descriptive/cov.js
CHANGED
|
@@ -10,6 +10,11 @@
|
|
|
10
10
|
import { kendall, pearsonDD } from "../htest/cor.js";
|
|
11
11
|
import { covariance } from "../internal/summary.js";
|
|
12
12
|
import { rank } from "./rank.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
|
+
import { isOptionsObject } from "../internal/optionalArgs.js";
|
|
16
|
+
const COV_KEYS = ["method", "use"];
|
|
17
|
+
const COR_KEYS = ["method", "use"];
|
|
13
18
|
const METHODS = ["pearson", "kendall", "spearman"];
|
|
14
19
|
const USES = ["all.obs", "complete.obs", "pairwise.complete.obs", "everything", "na.or.complete"];
|
|
15
20
|
function match(v, all, dflt, what) {
|
|
@@ -30,6 +35,8 @@ function columns(x) {
|
|
|
30
35
|
if (!isMatrix(x))
|
|
31
36
|
return [Array.from(x)];
|
|
32
37
|
const ncol = x[0].length;
|
|
38
|
+
if (x.some((r) => !Array.isArray(r) || r.length !== ncol))
|
|
39
|
+
throw new TypeError("all rows of the matrix must have the same length");
|
|
33
40
|
return Array.from({ length: ncol }, (_, j) => x.map((r) => r[j]));
|
|
34
41
|
}
|
|
35
42
|
function compute(x, y, options, correlation) {
|
|
@@ -91,19 +98,31 @@ function compute(x, y, options, correlation) {
|
|
|
91
98
|
return covariance(u, v);
|
|
92
99
|
};
|
|
93
100
|
const out = px.map((a) => py.map((b) => entry(a, b)));
|
|
94
|
-
//
|
|
95
|
-
//
|
|
101
|
+
// the correlation matrix of x alone has 1 on the diagonal (a computed 0.9999999999999998 made 1, as in R) where the
|
|
102
|
+
// correlation of a column with itself is defined; it stays NaN for a constant column (0/0) and, with "everything", for
|
|
103
|
+
// a column with a missing value (R puts 1 there too, whatever the column holds). With fewer than 2 observations
|
|
104
|
+
// used, all is NaN
|
|
96
105
|
const used = keepRows === undefined ? n : keepRows.filter(Boolean).length;
|
|
97
|
-
if (correlation && y === undefined && use !== "pairwise.complete.obs" && used >= 2)
|
|
98
|
-
out.forEach((r, i) =>
|
|
106
|
+
if (correlation && y === undefined && use !== "pairwise.complete.obs" && used >= 2) {
|
|
107
|
+
out.forEach((r, i) => {
|
|
108
|
+
if (!Number.isNaN(r[i]))
|
|
109
|
+
r[i] = 1;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
99
112
|
return (y === undefined ? false : !isMatrix(x) && !isMatrix(y)) ? out[0][0] : out;
|
|
100
113
|
}
|
|
101
114
|
export function cov(x, y, options = {}) {
|
|
115
|
+
if (isOptionsObject(y))
|
|
116
|
+
checkOptions(y, COV_KEYS, "cov"); // generated: option-keys.mjs
|
|
117
|
+
checkOptions(options, COV_KEYS, "cov"); // generated: option-keys.mjs
|
|
102
118
|
if (y !== undefined && !Array.isArray(y) && !ArrayBuffer.isView(y))
|
|
103
119
|
return compute(x, undefined, y, false);
|
|
104
120
|
return compute(x, y, options, false);
|
|
105
121
|
}
|
|
106
122
|
export function cor(x, y, options = {}) {
|
|
123
|
+
if (isOptionsObject(y))
|
|
124
|
+
checkOptions(y, COR_KEYS, "cor"); // generated: option-keys.mjs
|
|
125
|
+
checkOptions(options, COR_KEYS, "cor"); // generated: option-keys.mjs
|
|
107
126
|
if (y !== undefined && !Array.isArray(y) && !ArrayBuffer.isView(y))
|
|
108
127
|
return compute(x, undefined, y, true);
|
|
109
128
|
return compute(x, y, options, true);
|
|
@@ -47,8 +47,10 @@ export interface MergeOptions {
|
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Join two data frames on key columns. Equivalent to R's `merge()`. For data frames: every pair of rows of x and y
|
|
50
|
-
* with equal keys (missing keys match each other
|
|
51
|
-
*
|
|
50
|
+
* with equal keys (missing keys match each other; key columns of different types are compared in their common type,
|
|
51
|
+
* as R's match: text, or numbers with TRUE as 1), and with `allX` / `allY` the rows without a match. Columns: the
|
|
52
|
+
* keys (named as in x; in the common type when rows only in y are kept), x's other columns, y's other columns; a
|
|
53
|
+
* non-key name present in both gets the suffixes. Rows are
|
|
52
54
|
* ordered by the key unless `sort` is false (several keys: as text joined with "\r" unless `allX` or `allY`, as R
|
|
53
55
|
* does); without key columns, the cross join (x varying fastest).
|
|
54
56
|
*
|
|
@@ -7,6 +7,11 @@
|
|
|
7
7
|
// null). merge reproduces R's row order exactly, including the order that R's (unstable) Shell sort of the row
|
|
8
8
|
// indices gives to rows that share a key.
|
|
9
9
|
import { compareCodePoints } from "../models/modelFrame.js";
|
|
10
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
11
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
12
|
+
const MERGE_KEYS = ["by", "byX", "byY", "all", "allX", "allY", "sort", "suffixes"];
|
|
13
|
+
const SUBSET_KEYS = ["select"];
|
|
14
|
+
const FROM_ROWS_KEYS = ["columns"];
|
|
10
15
|
/** The number of rows of a data frame (0 without columns); all columns must have the same length */
|
|
11
16
|
export function rowCount(frame) {
|
|
12
17
|
const cols = Object.values(frame);
|
|
@@ -36,8 +41,9 @@ export function pick(col, rows) {
|
|
|
36
41
|
/** Adds a column to a result under construction; names must be unique */
|
|
37
42
|
export function addColumn(out, name, col) {
|
|
38
43
|
if (Object.hasOwn(out, name))
|
|
39
|
-
throw new
|
|
40
|
-
|
|
44
|
+
throw new RangeError(`duplicate column name '${name}'`);
|
|
45
|
+
// defineProperty, so that a column named "__proto__" is an own column rather than setting the prototype
|
|
46
|
+
Object.defineProperty(out, name, { value: col, enumerable: true, writable: true, configurable: true });
|
|
41
47
|
}
|
|
42
48
|
/**
|
|
43
49
|
* A number as R's as.character writes it: up to 15 significant digits, in fixed notation unless the scientific one
|
|
@@ -145,14 +151,16 @@ const nameList = (v) => (typeof v === "string" ? [v] : [...v]);
|
|
|
145
151
|
function keyColumns(frame, names) {
|
|
146
152
|
return names.map((n) => {
|
|
147
153
|
if (!Object.hasOwn(frame, n))
|
|
148
|
-
throw new
|
|
154
|
+
throw new RangeError("'by' must specify a uniquely valid column");
|
|
149
155
|
return frame[n];
|
|
150
156
|
});
|
|
151
157
|
}
|
|
152
158
|
/**
|
|
153
159
|
* Join two data frames on key columns. Equivalent to R's `merge()`. For data frames: every pair of rows of x and y
|
|
154
|
-
* with equal keys (missing keys match each other
|
|
155
|
-
*
|
|
160
|
+
* with equal keys (missing keys match each other; key columns of different types are compared in their common type,
|
|
161
|
+
* as R's match: text, or numbers with TRUE as 1), and with `allX` / `allY` the rows without a match. Columns: the
|
|
162
|
+
* keys (named as in x; in the common type when rows only in y are kept), x's other columns, y's other columns; a
|
|
163
|
+
* non-key name present in both gets the suffixes. Rows are
|
|
156
164
|
* ordered by the key unless `sort` is false (several keys: as text joined with "\r" unless `allX` or `allY`, as R
|
|
157
165
|
* does); without key columns, the cross join (x varying fastest).
|
|
158
166
|
*
|
|
@@ -167,6 +175,7 @@ function keyColumns(frame, names) {
|
|
|
167
175
|
* @category Data frames
|
|
168
176
|
*/
|
|
169
177
|
export function merge(x, y, options = {}) {
|
|
178
|
+
checkOptions(options, MERGE_KEYS, "merge"); // generated: option-keys.mjs
|
|
170
179
|
const xNames = Object.keys(x);
|
|
171
180
|
const yNames = Object.keys(y);
|
|
172
181
|
const nx = rowCount(x);
|
|
@@ -177,7 +186,7 @@ export function merge(x, y, options = {}) {
|
|
|
177
186
|
const keysX = keyColumns(x, byX);
|
|
178
187
|
const keysY = keyColumns(y, byY);
|
|
179
188
|
if (byX.length !== byY.length)
|
|
180
|
-
throw new
|
|
189
|
+
throw new TypeError("'by.x' and 'by.y' specify different numbers of columns");
|
|
181
190
|
const allX = options.allX ?? options.all ?? false;
|
|
182
191
|
const allY = options.allY ?? options.all ?? false;
|
|
183
192
|
const sort = options.sort ?? true;
|
|
@@ -194,12 +203,28 @@ export function merge(x, y, options = {}) {
|
|
|
194
203
|
pairs.push([i, j]);
|
|
195
204
|
}
|
|
196
205
|
else {
|
|
206
|
+
// Each key column of x and y compared in their common type, as R's match: text if either holds text, numbers
|
|
207
|
+
// (TRUE as 1) if numbers meet logicals (merge({ id: [1] }, { id: ["1"] }) matched nothing, #82)
|
|
208
|
+
const common = keysX.map((cx, k) => {
|
|
209
|
+
const kinds = new Set([...cx, ...keysY[k]].filter((v) => !isMissing(v)).map((v) => typeof v));
|
|
210
|
+
if (kinds.has("string"))
|
|
211
|
+
return (v) => (typeof v === "string" ? v : valueText(v));
|
|
212
|
+
if (kinds.has("number") && kinds.has("boolean"))
|
|
213
|
+
return (v) => (typeof v === "boolean" ? Number(v) : v);
|
|
214
|
+
return (v) => v;
|
|
215
|
+
});
|
|
216
|
+
const commonAt = (cols, k, i) => {
|
|
217
|
+
const v = valueAt(cols[k], i);
|
|
218
|
+
return isMissing(v) ? MISSING_KEY : common[k](v);
|
|
219
|
+
};
|
|
197
220
|
const keyOf = byX.length === 1
|
|
198
|
-
? (cols, i) =>
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
221
|
+
? (cols, i) => commonAt(cols, 0, i)
|
|
222
|
+
: // Matched on the values' text, as R, but encoded unambiguously: R pastes them with "\r", so that a key
|
|
223
|
+
// containing "\r", or "NA" against a missing value, matches a different key
|
|
224
|
+
(cols, i) => JSON.stringify(cols.map((_, k) => {
|
|
225
|
+
const v = commonAt(cols, k, i);
|
|
226
|
+
return v === MISSING_KEY ? null : valueText(v);
|
|
227
|
+
}));
|
|
203
228
|
const kx = Array.from({ length: nx }, (_, i) => keyOf(keysX, i));
|
|
204
229
|
const ky = Array.from({ length: ny }, (_, j) => keyOf(keysY, j));
|
|
205
230
|
const inY = new Set(ky);
|
|
@@ -228,13 +253,21 @@ export function merge(x, y, options = {}) {
|
|
|
228
253
|
for (const j of sy.alone)
|
|
229
254
|
pairs.push([-1, j]);
|
|
230
255
|
}
|
|
256
|
+
// The key columns of the result (in the order of pairs): x's values, and y's for the rows only in y; those rows
|
|
257
|
+
// bring the columns to their common type, as R's rbind of the two sides does (x's type otherwise)
|
|
258
|
+
const yOnly = pairs.some(([i]) => i < 0);
|
|
259
|
+
const fromX = pairs.some(([i]) => i >= 0);
|
|
260
|
+
const keyCols = keysX.map((cx, k) => {
|
|
261
|
+
const cy = keysY[k];
|
|
262
|
+
// a Float64Array when the columns the values come from are (x's alone without rows only in y, #449)
|
|
263
|
+
const sources = fromX || !yOnly ? (yOnly ? [cx, cy] : [cx]) : [cy];
|
|
264
|
+
const col = gather(sources, pairs.map(([i, j]) => (i >= 0 ? [cx, i] : [cy, j])));
|
|
265
|
+
return yOnly ? commonType(col) : col;
|
|
266
|
+
});
|
|
231
267
|
// Rows sorted stably by the key
|
|
232
268
|
let order = pairs.map((_, r) => r);
|
|
233
269
|
if (sort && byX.length > 0) {
|
|
234
|
-
const keyValue = (r, k) =>
|
|
235
|
-
const [i, j] = pairs[r];
|
|
236
|
-
return i >= 0 ? valueAt(keysX[k], i) : valueAt(keysY[k], j);
|
|
237
|
-
};
|
|
270
|
+
const keyValue = (r, k) => valueAt(keyCols[k], r);
|
|
238
271
|
if (byX.length > 1 && !allX && !allY) {
|
|
239
272
|
const text = pairs.map((_, r) => byX.map((_, k) => valueText(keyValue(r, k))).join("\r"));
|
|
240
273
|
order.sort((a, b) => compareCodePoints(text[a], text[b]));
|
|
@@ -252,11 +285,7 @@ export function merge(x, y, options = {}) {
|
|
|
252
285
|
}
|
|
253
286
|
const sorted = order.map((r) => pairs[r]);
|
|
254
287
|
const out = {};
|
|
255
|
-
byX.forEach((n, k) =>
|
|
256
|
-
const cx = keysX[k];
|
|
257
|
-
const cy = keysY[k];
|
|
258
|
-
addColumn(out, n, gather([cx, cy], sorted.map(([i, j]) => (i >= 0 ? [cx, i] : [cy, j]))));
|
|
259
|
-
});
|
|
288
|
+
byX.forEach((n, k) => addColumn(out, n, pick(keyCols[k], order)));
|
|
260
289
|
for (const n of restX) {
|
|
261
290
|
const c = x[n];
|
|
262
291
|
addColumn(out, nameX(n), gather([c], sorted.map(([i]) => (i >= 0 ? [c, i] : null))));
|
|
@@ -282,6 +311,7 @@ export function merge(x, y, options = {}) {
|
|
|
282
311
|
* @category Data frames
|
|
283
312
|
*/
|
|
284
313
|
export function subset(data, keep, options = {}) {
|
|
314
|
+
checkOptions(options, SUBSET_KEYS, "subset"); // generated: option-keys.mjs
|
|
285
315
|
const names = Object.keys(data);
|
|
286
316
|
const n = rowCount(data);
|
|
287
317
|
if (typeof keep !== "function" && keep.length !== n) {
|
|
@@ -296,7 +326,7 @@ export function subset(data, keep, options = {}) {
|
|
|
296
326
|
const out = {};
|
|
297
327
|
for (const c of select) {
|
|
298
328
|
if (!Object.hasOwn(data, c))
|
|
299
|
-
throw new
|
|
329
|
+
throw new RangeError("undefined columns selected");
|
|
300
330
|
addColumn(out, c, pick(data[c], rows));
|
|
301
331
|
}
|
|
302
332
|
return out;
|
|
@@ -313,17 +343,32 @@ export function rbindFrames(frames) {
|
|
|
313
343
|
rowCount(f);
|
|
314
344
|
const other = Object.keys(f);
|
|
315
345
|
if (other.length !== names.length || !names.every((n) => Object.hasOwn(f, n))) {
|
|
316
|
-
throw new
|
|
346
|
+
throw new TypeError("names do not match previous names");
|
|
317
347
|
}
|
|
318
348
|
}
|
|
319
349
|
const out = {};
|
|
320
350
|
for (const n of names) {
|
|
321
351
|
const cols = frames.map((f) => f[n]);
|
|
322
352
|
const rows = cols.flatMap((c) => Array.from({ length: c.length }, (_, i) => [c, i]));
|
|
323
|
-
out
|
|
353
|
+
addColumn(out, n, commonType(gather(cols, rows)));
|
|
324
354
|
}
|
|
325
355
|
return out;
|
|
326
356
|
}
|
|
357
|
+
/**
|
|
358
|
+
* A column of mixed value types converted to their common type, as R's rbind does: text if any value is text
|
|
359
|
+
* (numbers as as.character writes them, logicals as "TRUE" / "FALSE"), otherwise numbers (TRUE as 1) when numbers
|
|
360
|
+
* and logicals are mixed
|
|
361
|
+
*/
|
|
362
|
+
function commonType(col) {
|
|
363
|
+
if (col instanceof Float64Array)
|
|
364
|
+
return col;
|
|
365
|
+
const kinds = new Set(col.filter((v) => !isMissing(v)).map((v) => typeof v));
|
|
366
|
+
if (kinds.size < 2)
|
|
367
|
+
return col;
|
|
368
|
+
if (kinds.has("string"))
|
|
369
|
+
return col.map((v) => (isMissing(v) ? null : typeof v === "string" ? v : valueText(v)));
|
|
370
|
+
return col.map((v) => (isMissing(v) ? null : typeof v === "boolean" ? (v ? 1 : 0) : v));
|
|
371
|
+
}
|
|
327
372
|
/**
|
|
328
373
|
* Puts data frames with the same number of rows side by side. The data frame form of R's `cbind()`; see
|
|
329
374
|
* {@link cbind}. A column name that occurs twice is an error (an object cannot hold two columns of the same name).
|
|
@@ -354,6 +399,7 @@ export function cbindFrames(frames) {
|
|
|
354
399
|
* @category Data frames
|
|
355
400
|
*/
|
|
356
401
|
export function fromRows(rows, options = {}) {
|
|
402
|
+
checkOptions(options, FROM_ROWS_KEYS, "fromRows"); // generated: option-keys.mjs
|
|
357
403
|
let names = options.columns;
|
|
358
404
|
if (names === undefined) {
|
|
359
405
|
const seen = new Set();
|
|
@@ -63,6 +63,8 @@ export interface FactanalResult {
|
|
|
63
63
|
objective: number;
|
|
64
64
|
/** optim's counts of function and gradient evaluations */
|
|
65
65
|
counts: [number, number];
|
|
66
|
+
/** R's warnings, each once, in R's order (an empty array when there are none). */
|
|
67
|
+
warnings: string[];
|
|
66
68
|
}
|
|
67
69
|
/** Sum rounded once (R's long double accumulation, here double-double); also used by omega */
|
|
68
70
|
export declare function sumOnce(xs: readonly number[]): number;
|