@numero-uno-jpn/rstats 0.10.0 → 0.11.1
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 +37 -0
- package/README.ja.md +13 -9
- package/README.md +13 -8
- package/dist/chunks/{chunk-QJJWGNJP.js → chunk-2X7JYQBI.js} +2 -2
- package/dist/chunks/{chunk-O6PT7IVU.js → chunk-36GVER3O.js} +2 -2
- package/dist/chunks/{chunk-EWC6I75B.js → chunk-3WUXRSM6.js} +1 -1
- package/dist/chunks/{chunk-4NM37MGA.js → chunk-6I6QKTFK.js} +2 -2
- package/dist/chunks/{chunk-2XKG3DX4.js → chunk-ETOEZTGL.js} +1 -1
- package/dist/chunks/{chunk-IT354SHV.js → chunk-FVPDVRJQ.js} +2 -2
- package/dist/chunks/{chunk-QULPY6YM.js → chunk-GG4BJMJW.js} +1 -1
- package/dist/chunks/{chunk-7HPVUVZB.js → chunk-HITD7CX6.js} +1 -1
- package/dist/chunks/{chunk-LIMZKONM.js → chunk-KK4GE65D.js} +2 -2
- package/dist/chunks/chunk-LAUYSGV4.js +3 -0
- package/dist/chunks/chunk-M6GDXKKS.js +2 -0
- package/dist/chunks/{chunk-QFULZNHL.js → chunk-PHHUHR7T.js} +1 -1
- package/dist/chunks/{chunk-ENL43FNO.js → chunk-WQPFDDIY.js} +2 -2
- package/dist/chunks/chunk-X2AWQSVS.js +2 -0
- package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-ZRJQD7VS.js} +2 -2
- package/dist/data/grouping.d.ts +100 -0
- package/dist/data/grouping.js +264 -0
- package/dist/descriptive/basic.d.ts +130 -17
- package/dist/descriptive/basic.js +115 -14
- package/dist/descriptive/cov.d.ts +22 -12
- package/dist/descriptive/dataFrames.d.ts +138 -0
- package/dist/descriptive/dataFrames.js +400 -0
- package/dist/descriptive/factanal.d.ts +26 -6
- package/dist/descriptive/factanal.js +7 -3
- package/dist/descriptive/hist.d.ts +27 -6
- package/dist/descriptive/hist.js +9 -3
- package/dist/descriptive/missing.d.ts +42 -0
- package/dist/descriptive/missing.js +52 -0
- package/dist/descriptive/moments.d.ts +91 -13
- package/dist/descriptive/moments.js +66 -9
- package/dist/descriptive/omega.d.ts +28 -6
- package/dist/descriptive/omega.js +16 -4
- package/dist/descriptive/pivot.d.ts +105 -0
- package/dist/descriptive/pivot.js +347 -0
- package/dist/descriptive/quantile.d.ts +33 -5
- package/dist/descriptive/quantile.js +20 -2
- package/dist/descriptive/rank.d.ts +12 -2
- package/dist/descriptive/rank.js +2 -0
- package/dist/descriptive/table.d.ts +55 -7
- package/dist/descriptive/table.js +38 -4
- package/dist/descriptive.js +2 -2
- package/dist/distributions/arcsine.d.ts +40 -5
- package/dist/distributions/arcsine.js +35 -4
- package/dist/distributions/beta.d.ts +12 -0
- package/dist/distributions/beta.js +12 -0
- package/dist/distributions/binomial.d.ts +18 -0
- package/dist/distributions/binomial.js +18 -0
- package/dist/distributions/bvnorm.d.ts +4 -1
- package/dist/distributions/bvnorm.js +4 -1
- package/dist/distributions/cauchy.d.ts +24 -0
- package/dist/distributions/cauchy.js +24 -0
- package/dist/distributions/chisq.d.ts +18 -0
- package/dist/distributions/chisq.js +18 -0
- package/dist/distributions/exponential.d.ts +29 -1
- package/dist/distributions/exponential.js +24 -0
- package/dist/distributions/f.d.ts +18 -0
- package/dist/distributions/f.js +18 -0
- package/dist/distributions/gamma.d.ts +20 -0
- package/dist/distributions/gamma.js +18 -0
- package/dist/distributions/geometric.d.ts +24 -0
- package/dist/distributions/geometric.js +24 -0
- package/dist/distributions/hypergeometric.d.ts +24 -0
- package/dist/distributions/hypergeometric.js +24 -0
- package/dist/distributions/invgamma.d.ts +32 -4
- package/dist/distributions/invgamma.js +27 -3
- package/dist/distributions/kumaraswamy.d.ts +31 -3
- package/dist/distributions/kumaraswamy.js +31 -3
- package/dist/distributions/laplace.d.ts +36 -4
- package/dist/distributions/laplace.js +31 -3
- package/dist/distributions/logistic.d.ts +29 -1
- package/dist/distributions/logistic.js +24 -0
- package/dist/distributions/lognormal.d.ts +29 -1
- package/dist/distributions/lognormal.js +24 -0
- package/dist/distributions/multinomial.d.ts +7 -1
- package/dist/distributions/multinomial.js +2 -0
- package/dist/distributions/negbinomial.d.ts +29 -1
- package/dist/distributions/negbinomial.js +24 -0
- package/dist/distributions/normal.d.ts +11 -1
- package/dist/distributions/normal.js +6 -0
- package/dist/distributions/pareto.d.ts +36 -4
- package/dist/distributions/pareto.js +31 -3
- package/dist/distributions/poisson.d.ts +18 -0
- package/dist/distributions/poisson.js +18 -0
- package/dist/distributions/signrank.d.ts +24 -0
- package/dist/distributions/signrank.js +24 -0
- package/dist/distributions/summary.d.ts +20 -2
- package/dist/distributions/summary.js +4 -0
- package/dist/distributions/t.d.ts +18 -0
- package/dist/distributions/t.js +18 -0
- package/dist/distributions/triangular.d.ts +36 -4
- package/dist/distributions/triangular.js +31 -3
- package/dist/distributions/tukey.d.ts +15 -1
- package/dist/distributions/tukey.js +10 -0
- package/dist/distributions/uniform.d.ts +29 -1
- package/dist/distributions/uniform.js +24 -0
- package/dist/distributions/weibull.d.ts +29 -1
- package/dist/distributions/weibull.js +24 -0
- package/dist/distributions/wilcox.d.ts +24 -0
- package/dist/distributions/wilcox.js +24 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +5 -1
- package/dist/entries/descriptive.js +4 -0
- package/dist/entries/numerics.d.ts +1 -1
- package/dist/entries/tests.js +1 -1
- package/dist/htest/agreement.d.ts +61 -23
- package/dist/htest/agreement.js +19 -9
- package/dist/htest/ansariTest.d.ts +19 -1
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/aov.d.ts +43 -10
- package/dist/htest/aov.js +17 -3
- package/dist/htest/binomTest.d.ts +12 -2
- package/dist/htest/binomTest.js +2 -0
- package/dist/htest/brunnerMunzelTest.d.ts +9 -3
- package/dist/htest/brunnerMunzelTest.js +4 -2
- package/dist/htest/chisqTest.d.ts +19 -3
- package/dist/htest/cor.d.ts +31 -3
- package/dist/htest/cor.js +9 -27
- package/dist/htest/effectSize.d.ts +70 -12
- package/dist/htest/effectSize.js +40 -7
- package/dist/htest/fisherTest.d.ts +9 -1
- package/dist/htest/fisherTest.js +2 -0
- package/dist/htest/groups.d.ts +5 -1
- package/dist/htest/ksTest.d.ts +32 -2
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/leveneTest.d.ts +15 -4
- package/dist/htest/leveneTest.js +5 -2
- package/dist/htest/mantelhaenTest.d.ts +25 -3
- package/dist/htest/mantelhaenTest.js +15 -1
- package/dist/htest/mcnemarTest.d.ts +14 -4
- package/dist/htest/mcnemarTest.js +4 -2
- package/dist/htest/multipleComparisons.d.ts +173 -29
- package/dist/htest/multipleComparisons.js +123 -19
- package/dist/htest/normalityTests.d.ts +56 -9
- package/dist/htest/normalityTests.js +40 -6
- package/dist/htest/onewayTest.d.ts +45 -2
- package/dist/htest/onewayTest.js +15 -7
- package/dist/htest/pAdjust.d.ts +9 -1
- package/dist/htest/pAdjust.js +2 -0
- package/dist/htest/pairwise.d.ts +50 -5
- package/dist/htest/pairwise.js +25 -0
- package/dist/htest/poissonTest.d.ts +12 -2
- package/dist/htest/poissonTest.js +2 -0
- package/dist/htest/polycor.d.ts +29 -10
- package/dist/htest/power.d.ts +60 -6
- package/dist/htest/power.js +6 -0
- package/dist/htest/propTest.d.ts +13 -2
- package/dist/htest/propTest.js +2 -0
- package/dist/htest/pwr.d.ts +286 -36
- package/dist/htest/pwr.js +116 -15
- package/dist/htest/rankTests.d.ts +132 -7
- package/dist/htest/rankTests.js +30 -33
- package/dist/htest/tTest.d.ts +32 -2
- package/dist/htest/tTest.js +19 -24
- package/dist/htest/testFormula.d.ts +57 -0
- package/dist/htest/testFormula.js +171 -0
- package/dist/htest/trendTests.d.ts +29 -8
- package/dist/htest/trendTests.js +8 -4
- package/dist/htest/types.d.ts +14 -2
- package/dist/htest/varTest.d.ts +46 -6
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +23 -1
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/internal/dpq.d.ts +4 -0
- package/dist/internal/noncentral.d.ts +2 -0
- package/dist/internal/vectorize.d.ts +4 -0
- package/dist/linalg/basic.d.ts +39 -5
- package/dist/linalg/basic.js +15 -0
- package/dist/linalg/chol.d.ts +26 -5
- package/dist/linalg/chol.js +7 -0
- package/dist/linalg/eigen.d.ts +16 -7
- package/dist/linalg/eigen.js +6 -5
- package/dist/linalg/expm.d.ts +15 -5
- package/dist/linalg/expm.js +5 -3
- package/dist/linalg/kappa.d.ts +21 -1
- package/dist/linalg/kappa.js +16 -0
- package/dist/linalg/lu.d.ts +30 -6
- package/dist/linalg/lu.js +15 -3
- package/dist/linalg/matrix.d.ts +4 -0
- package/dist/linalg/norm.d.ts +16 -1
- package/dist/linalg/norm.js +9 -0
- package/dist/linalg/ops.d.ts +168 -36
- package/dist/linalg/ops.js +116 -20
- package/dist/linalg/qr.d.ts +87 -12
- package/dist/linalg/qr.js +31 -0
- package/dist/linalg/schur.d.ts +8 -2
- package/dist/linalg/schur.js +3 -1
- package/dist/linalg/solve.d.ts +42 -6
- package/dist/linalg/solve.js +22 -0
- package/dist/linalg/svd.d.ts +12 -2
- package/dist/linalg/svd.js +2 -0
- package/dist/linalg.js +2 -2
- package/dist/models/anovaSequential.d.ts +26 -4
- package/dist/models/anovaSequential.js +3 -1
- package/dist/models/aovEz.d.ts +29 -6
- package/dist/models/aovEz.js +5 -2
- package/dist/models/aovStrata.d.ts +12 -2
- package/dist/models/carAnova.d.ts +9 -3
- package/dist/models/carAnova.js +4 -2
- package/dist/models/coxph.d.ts +33 -7
- package/dist/models/coxph.js +8 -3
- package/dist/models/glm.d.ts +128 -21
- package/dist/models/glm.js +71 -14
- package/dist/models/glmAnova.d.ts +30 -5
- package/dist/models/glmAnova.js +15 -2
- package/dist/models/glmConfint.d.ts +16 -2
- package/dist/models/glmConfint.js +9 -1
- package/dist/models/glmFamily.d.ts +27 -5
- package/dist/models/lm.d.ts +75 -17
- package/dist/models/lm.js +22 -4
- package/dist/models/mauchly.d.ts +20 -5
- package/dist/models/mauchly.js +7 -3
- package/dist/models/modelFrame.d.ts +5 -1
- package/dist/models/modelMatrix.d.ts +18 -4
- package/dist/models/modelMatrix.js +3 -1
- package/dist/models/survival.d.ts +41 -9
- package/dist/models/survival.js +10 -4
- package/dist/numerics/deriv.d.ts +21 -6
- package/dist/numerics/deriv.js +16 -5
- package/dist/numerics/integrate.d.ts +37 -7
- package/dist/numerics/integrate.js +8 -2
- package/dist/numerics/interp.d.ts +63 -12
- package/dist/numerics/interp.js +30 -6
- package/dist/numerics/ode.d.ts +15 -5
- package/dist/numerics/ode.js +5 -3
- package/dist/numerics.js +2 -2
- package/dist/optim/optim.d.ts +32 -8
- package/dist/optim/optim.js +8 -4
- package/dist/random/beta.d.ts +8 -0
- package/dist/random/beta.js +8 -0
- package/dist/random/binomial.d.ts +16 -0
- package/dist/random/binomial.js +16 -0
- package/dist/random/continuous.d.ts +51 -0
- package/dist/random/continuous.js +51 -0
- package/dist/random/extra.d.ts +66 -6
- package/dist/random/extra.js +66 -6
- package/dist/random/gamma.d.ts +33 -0
- package/dist/random/gamma.js +33 -0
- package/dist/random/hyper.d.ts +8 -0
- package/dist/random/hyper.js +8 -0
- package/dist/random/poisson.d.ts +25 -0
- package/dist/random/poisson.js +25 -0
- package/dist/random/r2dtable.d.ts +2 -0
- package/dist/random/r2dtable.js +2 -0
- package/dist/random/rank.d.ts +16 -0
- package/dist/random/rank.js +16 -0
- package/dist/random/rng.d.ts +49 -6
- package/dist/random/rng.js +29 -2
- package/dist/random/sample.d.ts +10 -2
- package/dist/random/sample.js +5 -1
- package/dist/random/streams.d.ts +13 -0
- package/dist/random/streams.js +13 -0
- package/dist/rstats.js +14 -14
- package/dist/special/besselIK.d.ts +11 -3
- package/dist/special/besselIK.js +6 -2
- package/dist/special/besselJY.d.ts +6 -2
- package/dist/special/besselJY.js +6 -2
- package/dist/special/beta.d.ts +16 -2
- package/dist/special/beta.js +16 -2
- package/dist/special/choose.d.ts +13 -5
- package/dist/special/choose.js +13 -5
- package/dist/special/erf.d.ts +8 -3
- package/dist/special/erf.js +8 -3
- package/dist/special/extra.d.ts +30 -10
- package/dist/special/extra.js +25 -9
- package/dist/special/gamma.d.ts +18 -2
- package/dist/special/gamma.js +18 -2
- package/dist/special/log1p.d.ts +16 -7
- package/dist/special/log1p.js +16 -7
- package/dist/special/psigamma.d.ts +9 -3
- package/dist/special/psigamma.js +9 -3
- package/dist/special/rCompatible.d.ts +5 -1
- package/dist/special.js +2 -2
- package/dist/tests.js +12 -13
- package/llms.txt +14 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-AECGWZHD.js +0 -2
- package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// Wide and long formats: tidyr's pivot_longer and pivot_wider.
|
|
2
|
+
//
|
|
3
|
+
// Written for rStats.js (no ported code) from oracle/pivot-behaviour.md. A data frame is the library's DataFrame (see
|
|
4
|
+
// dataFrames.ts): `null`, and `NaN` in a numeric column, is a missing value. Columns that are copied through (the
|
|
5
|
+
// columns not pivoted, the id columns) keep their kind (a Float64Array stays a Float64Array); a value column is a
|
|
6
|
+
// Float64Array when its sources are, else an array. Where tidyr would make a list column (a cell that several rows
|
|
7
|
+
// fill), pivotWider throws instead, since a data frame here cannot hold one. pivotLonger passes tidyr's warnings to
|
|
8
|
+
// its `onWarning` option.
|
|
9
|
+
import { formatR } from "../htest/core/simulate.js";
|
|
10
|
+
import { addColumn, compareValues, isMissing, pick, rowCount } from "./dataFrames.js";
|
|
11
|
+
const nameList = (v) => (typeof v === "string" ? [v] : [...v]);
|
|
12
|
+
function requireColumns(data, names) {
|
|
13
|
+
for (const n of names)
|
|
14
|
+
if (!Object.hasOwn(data, n))
|
|
15
|
+
throw new Error(`Column \`${n}\` doesn't exist.`);
|
|
16
|
+
}
|
|
17
|
+
/** The type of a column; `null` for an array of missing values only (which combines with any type) */
|
|
18
|
+
function columnType(name, col) {
|
|
19
|
+
if (col instanceof Float64Array)
|
|
20
|
+
return "double";
|
|
21
|
+
let type = null;
|
|
22
|
+
for (const v of col) {
|
|
23
|
+
if (v === null)
|
|
24
|
+
continue;
|
|
25
|
+
const t = typeof v === "number" ? "double" : typeof v === "string" ? "character" : "logical";
|
|
26
|
+
if (type !== null && t !== type)
|
|
27
|
+
throw new TypeError(`column '${name}' mixes ${type} and ${t} values`);
|
|
28
|
+
type = t;
|
|
29
|
+
}
|
|
30
|
+
return type;
|
|
31
|
+
}
|
|
32
|
+
/** The common type of the source columns of a value column: numbers absorb booleans, strings combine with neither */
|
|
33
|
+
function commonType(data, sources) {
|
|
34
|
+
let common = null;
|
|
35
|
+
for (const s of sources) {
|
|
36
|
+
const t = columnType(s, data[s]);
|
|
37
|
+
if (t === null)
|
|
38
|
+
continue;
|
|
39
|
+
if (common === null)
|
|
40
|
+
common = { type: t, column: s };
|
|
41
|
+
else if (common.type !== t) {
|
|
42
|
+
if (common.type === "character" || t === "character") {
|
|
43
|
+
throw new TypeError(`Can't combine \`${common.column}\` <${common.type}> and \`${s}\` <${t}>.`);
|
|
44
|
+
}
|
|
45
|
+
if (t === "double")
|
|
46
|
+
common = { type: t, column: s };
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return common?.type ?? null;
|
|
50
|
+
}
|
|
51
|
+
/** The matches of a regular expression (as a string) in each name: its capture groups, `null` if it does not match */
|
|
52
|
+
function captures(names, pattern, count) {
|
|
53
|
+
const groups = new RegExp(`${pattern}|`).exec("").length - 1;
|
|
54
|
+
if (groups !== count)
|
|
55
|
+
throw new Error(`\`regex\` should define ${count} groups; ${groups} found.`);
|
|
56
|
+
const re = new RegExp(pattern);
|
|
57
|
+
return names.map((name) => {
|
|
58
|
+
const m = re.exec(name);
|
|
59
|
+
return Array.from({ length: count }, (_, j) => (m === null ? null : (m[j + 1] ?? null)));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** The names split at the separator into `count` parts, and tidyr's warnings for names with more or fewer pieces */
|
|
63
|
+
function separated(names, sep, count, warnings) {
|
|
64
|
+
const re = new RegExp(sep, "g");
|
|
65
|
+
const extra = [];
|
|
66
|
+
const short = [];
|
|
67
|
+
const parts = names.map((name, i) => {
|
|
68
|
+
const pieces = [];
|
|
69
|
+
let start = 0;
|
|
70
|
+
for (const m of name.matchAll(re)) {
|
|
71
|
+
if (m[0].length === 0)
|
|
72
|
+
continue;
|
|
73
|
+
pieces.push(name.slice(start, m.index));
|
|
74
|
+
start = m.index + m[0].length;
|
|
75
|
+
}
|
|
76
|
+
pieces.push(name.slice(start));
|
|
77
|
+
if (pieces.length > count)
|
|
78
|
+
extra.push(i + 1);
|
|
79
|
+
if (pieces.length < count)
|
|
80
|
+
short.push(i + 1);
|
|
81
|
+
return Array.from({ length: count }, (_, j) => pieces[j] ?? null);
|
|
82
|
+
});
|
|
83
|
+
const rows = (r) => `${r.length} rows [${r.join(", ")}]`;
|
|
84
|
+
if (extra.length > 0)
|
|
85
|
+
warnings.push(`Expected ${count} pieces. Additional pieces discarded in ${rows(extra)}.`);
|
|
86
|
+
if (short.length > 0)
|
|
87
|
+
warnings.push(`Expected ${count} pieces. Missing pieces filled with \`NA\` in ${rows(short)}.`);
|
|
88
|
+
return parts;
|
|
89
|
+
}
|
|
90
|
+
/** The parts of each name, one per element of `namesTo` */
|
|
91
|
+
function nameParts(names, namesTo, options, warnings) {
|
|
92
|
+
const { namesSep, namesPattern } = options;
|
|
93
|
+
if (namesTo.length === 1) {
|
|
94
|
+
if (namesSep !== undefined)
|
|
95
|
+
throw new Error("`names_sep` can't be used with a length 1 `names_to`.");
|
|
96
|
+
if (namesPattern === undefined)
|
|
97
|
+
return names.map((n) => [n]);
|
|
98
|
+
const re = new RegExp(namesPattern);
|
|
99
|
+
return names.map((n) => [re.exec(n)?.[1] ?? null]);
|
|
100
|
+
}
|
|
101
|
+
if ((namesSep === undefined) === (namesPattern === undefined)) {
|
|
102
|
+
throw new Error("If you supply multiple names in `names_to` you must also supply one of `names_sep` or `names_pattern`.");
|
|
103
|
+
}
|
|
104
|
+
return namesSep !== undefined
|
|
105
|
+
? separated(names, namesSep, namesTo.length, warnings)
|
|
106
|
+
: captures(names, namesPattern, namesTo.length);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Reshapes a data frame from wide to long: the selected columns become rows. Equivalent to `tidyr::pivot_longer()` in
|
|
110
|
+
* R. The result has the columns not selected, a column per part of the selected names (`namesTo`), and the value
|
|
111
|
+
* column (`valuesTo`), or with `".value"` in `namesTo` one value column per value of that part. The values of the
|
|
112
|
+
* selected columns are combined into one type: numbers with booleans give numbers; strings with either are an error.
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```ts
|
|
116
|
+
* const wide = { id: ["s1", "s2"], t1: [5.1, 4.8], t2: [6.3, 7.2] };
|
|
117
|
+
* pivotLonger(wide, { cols: ["t1", "t2"], namesTo: "time", valuesTo: "score" });
|
|
118
|
+
* // { id: ["s1", "s1", "s2", "s2"], time: ["t1", "t2", "t1", "t2"], score: [5.1, 6.3, 4.8, 7.2] }
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* @category Data frames
|
|
122
|
+
*/
|
|
123
|
+
export function pivotLonger(data, options) {
|
|
124
|
+
const n = rowCount(data);
|
|
125
|
+
const allNames = Object.keys(data);
|
|
126
|
+
const cols = typeof options.cols === "function" ? allNames.filter(options.cols) : [...new Set(options.cols)];
|
|
127
|
+
requireColumns(data, cols);
|
|
128
|
+
if (cols.length === 0)
|
|
129
|
+
throw new Error("`cols` must select at least one column.");
|
|
130
|
+
const namesTo = nameList(options.namesTo ?? "name");
|
|
131
|
+
const valuePosition = namesTo.indexOf(".value");
|
|
132
|
+
if (namesTo.lastIndexOf(".value") !== valuePosition)
|
|
133
|
+
throw new Error('`names_to` may contain ".value" only once');
|
|
134
|
+
const warnings = [];
|
|
135
|
+
const prefix = options.namesPrefix === undefined ? null : new RegExp(`^(?:${options.namesPrefix})`);
|
|
136
|
+
const stripped = cols.map((c) => (prefix === null ? c : c.replace(prefix, "")));
|
|
137
|
+
const parts = nameParts(stripped, namesTo, options, warnings);
|
|
138
|
+
// The keys (the parts other than ".value") and the value columns with their sources
|
|
139
|
+
const keys = [];
|
|
140
|
+
const values = [];
|
|
141
|
+
if (valuePosition < 0) {
|
|
142
|
+
keys.push(...parts);
|
|
143
|
+
values.push({ name: options.valuesTo ?? "value", sources: [...cols] });
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
const keyIndex = new Map();
|
|
147
|
+
const valueIndex = new Map();
|
|
148
|
+
parts.forEach((p, c) => {
|
|
149
|
+
const key = p.filter((_, j) => j !== valuePosition);
|
|
150
|
+
const id = JSON.stringify(key);
|
|
151
|
+
let k = keyIndex.get(id);
|
|
152
|
+
if (k === undefined) {
|
|
153
|
+
k = keys.length;
|
|
154
|
+
keyIndex.set(id, k);
|
|
155
|
+
keys.push(key);
|
|
156
|
+
for (const v of values)
|
|
157
|
+
v.sources.push(null);
|
|
158
|
+
}
|
|
159
|
+
const name = p[valuePosition] ?? "NA";
|
|
160
|
+
let value = valueIndex.get(name);
|
|
161
|
+
if (value === undefined) {
|
|
162
|
+
value = { name, sources: keys.map(() => null) };
|
|
163
|
+
valueIndex.set(name, value);
|
|
164
|
+
values.push(value);
|
|
165
|
+
}
|
|
166
|
+
if (value.sources[k] !== null)
|
|
167
|
+
throw new Error(`the names give the value column '${name}' twice for one key`);
|
|
168
|
+
value.sources[k] = cols[c];
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
const types = values.map((v) => commonType(data, v.sources.filter((s) => s !== null)));
|
|
172
|
+
// Rows of the result: [input row, key]
|
|
173
|
+
let rows = [];
|
|
174
|
+
if (options.colsVary === "slowest") {
|
|
175
|
+
for (let k = 0; k < keys.length; k++)
|
|
176
|
+
for (let i = 0; i < n; i++)
|
|
177
|
+
rows.push([i, k]);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
for (let i = 0; i < n; i++)
|
|
181
|
+
for (let k = 0; k < keys.length; k++)
|
|
182
|
+
rows.push([i, k]);
|
|
183
|
+
}
|
|
184
|
+
const cellOf = (v, i, k) => {
|
|
185
|
+
const s = v.sources[k];
|
|
186
|
+
return s === null || s === undefined ? null : data[s][i];
|
|
187
|
+
};
|
|
188
|
+
if (options.valuesDropNa === true) {
|
|
189
|
+
rows = rows.filter(([i, k]) => values.some((v) => !isMissing(cellOf(v, i, k))));
|
|
190
|
+
}
|
|
191
|
+
const out = {};
|
|
192
|
+
const inputRows = rows.map(([i]) => i);
|
|
193
|
+
for (const name of allNames)
|
|
194
|
+
if (!cols.includes(name))
|
|
195
|
+
addColumn(out, name, pick(data[name], inputRows));
|
|
196
|
+
namesTo.forEach((to, j) => {
|
|
197
|
+
if (j === valuePosition)
|
|
198
|
+
return;
|
|
199
|
+
const transform = options.namesTransform?.[to];
|
|
200
|
+
const part = (k) => {
|
|
201
|
+
const p = keys[k][j < valuePosition || valuePosition < 0 ? j : j - 1];
|
|
202
|
+
return p === null || transform === undefined ? p : transform(p);
|
|
203
|
+
};
|
|
204
|
+
addColumn(out, to, rows.map(([, k]) => part(k)));
|
|
205
|
+
});
|
|
206
|
+
values.forEach((v, m) => {
|
|
207
|
+
const double = types[m] === "double";
|
|
208
|
+
if (double && v.sources.every((s) => s !== null && data[s] instanceof Float64Array)) {
|
|
209
|
+
addColumn(out, v.name, Float64Array.from(rows, ([i, k]) => cellOf(v, i, k)));
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
const cells = rows.map(([i, k]) => {
|
|
213
|
+
const x = cellOf(v, i, k);
|
|
214
|
+
return double && typeof x === "boolean" ? Number(x) : x;
|
|
215
|
+
});
|
|
216
|
+
addColumn(out, v.name, cells);
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
if (options.onWarning !== undefined)
|
|
220
|
+
for (const w of new Set(warnings))
|
|
221
|
+
options.onWarning(w);
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
// ---- pivotWider ----
|
|
225
|
+
/** A value as an exact key (missing values are all one key) */
|
|
226
|
+
const keyOf = (v) => isMissing(v) ? "NA" : typeof v === "number" ? `n${v}` : typeof v === "string" ? `s${v}` : `b${String(v)}`;
|
|
227
|
+
/**
|
|
228
|
+
* The distinct combinations of the values of the columns, in order of first appearance: the combination of each row,
|
|
229
|
+
* and the first row of each combination
|
|
230
|
+
*/
|
|
231
|
+
function combinations(cols, n) {
|
|
232
|
+
const index = new Map();
|
|
233
|
+
const firsts = [];
|
|
234
|
+
const codes = Array.from({ length: n }, (_, i) => {
|
|
235
|
+
const id = JSON.stringify(cols.map((c) => keyOf(c[i])));
|
|
236
|
+
let code = index.get(id);
|
|
237
|
+
if (code === undefined) {
|
|
238
|
+
code = firsts.length;
|
|
239
|
+
index.set(id, code);
|
|
240
|
+
firsts.push(i);
|
|
241
|
+
}
|
|
242
|
+
return code;
|
|
243
|
+
});
|
|
244
|
+
return { codes, firsts };
|
|
245
|
+
}
|
|
246
|
+
/** A value as part of a column name: numbers as R's as.character, booleans as TRUE and FALSE, missing as NA */
|
|
247
|
+
function labelText(v) {
|
|
248
|
+
if (isMissing(v))
|
|
249
|
+
return "NA";
|
|
250
|
+
if (typeof v === "number")
|
|
251
|
+
return Number.isFinite(v) ? formatR(v) : v > 0 ? "Inf" : "-Inf";
|
|
252
|
+
if (typeof v === "boolean")
|
|
253
|
+
return v ? "TRUE" : "FALSE";
|
|
254
|
+
return v;
|
|
255
|
+
}
|
|
256
|
+
/** An option given once for all value columns or by value column */
|
|
257
|
+
function perColumn(option, isSingle, name) {
|
|
258
|
+
if (option === undefined)
|
|
259
|
+
return undefined;
|
|
260
|
+
return isSingle(option) ? option : option[name];
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Reshapes a data frame from long to wide: the values of `namesFrom` become columns, filled from `valuesFrom`.
|
|
264
|
+
* Equivalent to `tidyr::pivot_wider()` in R. One row per combination of the id columns (in order of first appearance),
|
|
265
|
+
* the new columns in order of first appearance of their names (unless `namesSort`). A cell that no row fills is
|
|
266
|
+
* `valuesFill` (default missing). A cell that several rows fill is an error unless `valuesFn` summarises the values
|
|
267
|
+
* (tidyr makes a list column there, which a data frame here cannot hold).
|
|
268
|
+
*
|
|
269
|
+
* @example
|
|
270
|
+
* ```ts
|
|
271
|
+
* const long = { id: ["s1", "s1", "s2"], time: ["t1", "t2", "t1"], score: [5.1, 6.3, 4.8] };
|
|
272
|
+
* pivotWider(long, { namesFrom: "time", valuesFrom: "score" });
|
|
273
|
+
* // { id: ["s1", "s2"], t1: [5.1, 4.8], t2: [6.3, null] }
|
|
274
|
+
* ```
|
|
275
|
+
*
|
|
276
|
+
* @category Data frames
|
|
277
|
+
*/
|
|
278
|
+
export function pivotWider(data, options = {}) {
|
|
279
|
+
const n = rowCount(data);
|
|
280
|
+
const namesFrom = nameList(options.namesFrom ?? "name");
|
|
281
|
+
const valuesFrom = nameList(options.valuesFrom ?? "value");
|
|
282
|
+
const idCols = options.idCols === undefined
|
|
283
|
+
? Object.keys(data).filter((c) => !namesFrom.includes(c) && !valuesFrom.includes(c))
|
|
284
|
+
: nameList(options.idCols);
|
|
285
|
+
requireColumns(data, [...idCols, ...namesFrom, ...valuesFrom]);
|
|
286
|
+
const prefix = options.namesPrefix ?? "";
|
|
287
|
+
const sep = options.namesSep ?? "_";
|
|
288
|
+
const ids = combinations(idCols.map((c) => data[c]), n);
|
|
289
|
+
const labels = combinations(namesFrom.map((c) => data[c]), n);
|
|
290
|
+
// The name combinations in output order (positions in labels.firsts)
|
|
291
|
+
const nameOrder = labels.firsts.map((_, m) => m);
|
|
292
|
+
if (options.namesSort === true) {
|
|
293
|
+
nameOrder.sort((a, b) => {
|
|
294
|
+
for (const c of namesFrom) {
|
|
295
|
+
const d = compareValues(data[c][labels.firsts[a]], data[c][labels.firsts[b]]);
|
|
296
|
+
if (d !== 0)
|
|
297
|
+
return d;
|
|
298
|
+
}
|
|
299
|
+
return 0;
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
const nameText = labels.firsts.map((r) => prefix + namesFrom.map((c) => labelText(data[c][r])).join(sep));
|
|
303
|
+
// The rows of each cell (id combination, name combination), in data order
|
|
304
|
+
const nNames = labels.firsts.length;
|
|
305
|
+
const cellRows = Array.from({ length: ids.firsts.length * nNames }, () => []);
|
|
306
|
+
for (let i = 0; i < n; i++)
|
|
307
|
+
cellRows[ids.codes[i] * nNames + labels.codes[i]].push(i);
|
|
308
|
+
const isSummary = (o) => typeof o === "function";
|
|
309
|
+
const isFill = (o) => typeof o !== "object" || o === null;
|
|
310
|
+
const columnOf = (value, m) => {
|
|
311
|
+
const source = data[value];
|
|
312
|
+
const summary = perColumn(options.valuesFn, isSummary, value);
|
|
313
|
+
const fill = perColumn(options.valuesFill, isFill, value) ?? null;
|
|
314
|
+
const cells = ids.firsts.map((_, r) => {
|
|
315
|
+
const rows = cellRows[r * nNames + m];
|
|
316
|
+
if (rows.length === 0)
|
|
317
|
+
return fill;
|
|
318
|
+
if (summary !== undefined)
|
|
319
|
+
return summary(rows.map((i) => source[i]));
|
|
320
|
+
return source[rows[0]];
|
|
321
|
+
});
|
|
322
|
+
if (source instanceof Float64Array && cells.every((x) => x === null || typeof x === "number")) {
|
|
323
|
+
return Float64Array.from(cells, (x) => (x === null ? NaN : x));
|
|
324
|
+
}
|
|
325
|
+
return cells;
|
|
326
|
+
};
|
|
327
|
+
for (const value of valuesFrom) {
|
|
328
|
+
if (perColumn(options.valuesFn, isSummary, value) === undefined && cellRows.some((rows) => rows.length > 1)) {
|
|
329
|
+
throw new Error(`values from '${value}' are not uniquely identified; use valuesFn to summarise them`);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
const out = {};
|
|
333
|
+
for (const c of idCols)
|
|
334
|
+
addColumn(out, c, pick(data[c], ids.firsts));
|
|
335
|
+
const columnName = (value, m) => valuesFrom.length === 1 ? nameText[m] : `${value}${sep}${nameText[m]}`;
|
|
336
|
+
if (options.namesVary === "slowest") {
|
|
337
|
+
for (const m of nameOrder)
|
|
338
|
+
for (const v of valuesFrom)
|
|
339
|
+
addColumn(out, columnName(v, m), columnOf(v, m));
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
for (const v of valuesFrom)
|
|
343
|
+
for (const m of nameOrder)
|
|
344
|
+
addColumn(out, columnName(v, m), columnOf(v, m));
|
|
345
|
+
}
|
|
346
|
+
return out;
|
|
347
|
+
}
|
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { type NaRmOptions } from "./basic.js";
|
|
2
2
|
import type { Sample } from "../htest/types.js";
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* Options for {@link quantile}.
|
|
5
|
+
*
|
|
6
|
+
* @category Descriptive statistics
|
|
7
|
+
*/
|
|
4
8
|
export interface QuantileOptions extends NaRmOptions {
|
|
5
9
|
/** Hyndman and Fan's definition, 1 to 9. Default `7`. */
|
|
6
10
|
type?: number;
|
|
7
11
|
}
|
|
8
12
|
/**
|
|
9
|
-
* Sample quantiles. Equivalent to R's `quantile()
|
|
13
|
+
* Sample quantiles. Equivalent to R's `quantile()`. Without names (R's `names = FALSE`).
|
|
10
14
|
*
|
|
11
15
|
* @param x The data.
|
|
12
16
|
* @param probs Probabilities in [0, 1]. Default `[0, 0.25, 0.5, 0.75, 1]`. A single number gives a single number.
|
|
@@ -17,17 +21,39 @@ export interface QuantileOptions extends NaRmOptions {
|
|
|
17
21
|
* quantile([1, 2, 3, 4, 5, 6], 0.25); // 2.25 (type 7)
|
|
18
22
|
* quantile([1, 2, 3, 4, 5, 6], 0.25, { type: 9 }); // 1.9375 (jStat's quantiles)
|
|
19
23
|
* ```
|
|
24
|
+
*
|
|
25
|
+
* @category Descriptive statistics
|
|
20
26
|
*/
|
|
21
27
|
export declare function quantile(x: Sample, probs: number, options?: QuantileOptions): number;
|
|
22
28
|
export declare function quantile(x: Sample, probs?: readonly number[], options?: QuantileOptions): number[];
|
|
23
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* Interquartile range, the difference of the 0.75 and 0.25 quantiles. Equivalent to R's `IQR()`.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* IQR([1, 2, 3, 4, 5, 6]); // 2.5
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @category Descriptive statistics
|
|
38
|
+
*/
|
|
24
39
|
export declare function IQR(x: Sample, options?: QuantileOptions): number;
|
|
25
40
|
/**
|
|
26
|
-
* Tukey's five-number summary (minimum, lower hinge, median, upper hinge, maximum). Equivalent to R's `fivenum()
|
|
41
|
+
* Tukey's five-number summary (minimum, lower hinge, median, upper hinge, maximum). Equivalent to R's `fivenum()`.
|
|
27
42
|
* `NaN` values are dropped.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* fivenum([1, 2, 3, 4, 5, 6]); // [1, 2, 3.5, 5, 6]
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @category Descriptive statistics
|
|
28
50
|
*/
|
|
29
51
|
export declare function fivenum(x: Sample): [number, number, number, number, number];
|
|
30
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Options for {@link mad}.
|
|
54
|
+
*
|
|
55
|
+
* @category Descriptive statistics
|
|
56
|
+
*/
|
|
31
57
|
export interface MadOptions extends NaRmOptions {
|
|
32
58
|
/** Centre. Default: the median. */
|
|
33
59
|
center?: number;
|
|
@@ -46,5 +72,7 @@ export interface MadOptions extends NaRmOptions {
|
|
|
46
72
|
* mad([1, 2, 3, 4, 100]); // 1.4826
|
|
47
73
|
* mad([1, 2, 3, 4, 100], { constant: 1 }); // 1 (jStat's meddev)
|
|
48
74
|
* ```
|
|
75
|
+
*
|
|
76
|
+
* @category Descriptive statistics
|
|
49
77
|
*/
|
|
50
78
|
export declare function mad(x: Sample, options?: MadOptions): number;
|
|
@@ -81,14 +81,30 @@ export function quantile(x, probs = [0, 0.25, 0.5, 0.75, 1], options = {}) {
|
|
|
81
81
|
});
|
|
82
82
|
return typeof probs === "number" ? out[0] : out;
|
|
83
83
|
}
|
|
84
|
-
/**
|
|
84
|
+
/**
|
|
85
|
+
* Interquartile range, the difference of the 0.75 and 0.25 quantiles. Equivalent to R's `IQR()`.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* IQR([1, 2, 3, 4, 5, 6]); // 2.5
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @category Descriptive statistics
|
|
93
|
+
*/
|
|
85
94
|
export function IQR(x, options = {}) {
|
|
86
95
|
const [a, b] = quantile(x, [0.25, 0.75], options);
|
|
87
96
|
return b - a;
|
|
88
97
|
}
|
|
89
98
|
/**
|
|
90
|
-
* Tukey's five-number summary (minimum, lower hinge, median, upper hinge, maximum). Equivalent to R's `fivenum()
|
|
99
|
+
* Tukey's five-number summary (minimum, lower hinge, median, upper hinge, maximum). Equivalent to R's `fivenum()`.
|
|
91
100
|
* `NaN` values are dropped.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* fivenum([1, 2, 3, 4, 5, 6]); // [1, 2, 3.5, 5, 6]
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* @category Descriptive statistics
|
|
92
108
|
*/
|
|
93
109
|
export function fivenum(x) {
|
|
94
110
|
const s = Array.from(x)
|
|
@@ -109,6 +125,8 @@ export function fivenum(x) {
|
|
|
109
125
|
* mad([1, 2, 3, 4, 100]); // 1.4826
|
|
110
126
|
* mad([1, 2, 3, 4, 100], { constant: 1 }); // 1 (jStat's meddev)
|
|
111
127
|
* ```
|
|
128
|
+
*
|
|
129
|
+
* @category Descriptive statistics
|
|
112
130
|
*/
|
|
113
131
|
export function mad(x, options = {}) {
|
|
114
132
|
const xs = values(x, options.naRm);
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import type { Sample } from "../htest/types.js";
|
|
2
|
-
/**
|
|
2
|
+
/**
|
|
3
|
+
* How ties are ranked, as R's `ties.method`.
|
|
4
|
+
*
|
|
5
|
+
* @category Descriptive statistics
|
|
6
|
+
*/
|
|
3
7
|
export type TiesMethod = "average" | "first" | "last" | "random" | "max" | "min";
|
|
4
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* Options for {@link rank}.
|
|
10
|
+
*
|
|
11
|
+
* @category Descriptive statistics
|
|
12
|
+
*/
|
|
5
13
|
export interface RankOptions {
|
|
6
14
|
/** Ranks of tied values. Default `"average"`. */
|
|
7
15
|
tiesMethod?: TiesMethod;
|
|
@@ -19,5 +27,7 @@ export interface RankOptions {
|
|
|
19
27
|
* rank([10, 20, 20, 30]); // [1, 2.5, 2.5, 4]
|
|
20
28
|
* rank([10, 20, 20, 30], { tiesMethod: "min" }); // [1, 2, 2, 4]
|
|
21
29
|
* ```
|
|
30
|
+
*
|
|
31
|
+
* @category Descriptive statistics
|
|
22
32
|
*/
|
|
23
33
|
export declare function rank(x: Sample, options?: RankOptions): number[];
|
package/dist/descriptive/rank.js
CHANGED
|
@@ -45,6 +45,8 @@ function rankValues(x, method) {
|
|
|
45
45
|
* rank([10, 20, 20, 30]); // [1, 2.5, 2.5, 4]
|
|
46
46
|
* rank([10, 20, 20, 30], { tiesMethod: "min" }); // [1, 2, 2, 4]
|
|
47
47
|
* ```
|
|
48
|
+
*
|
|
49
|
+
* @category Descriptive statistics
|
|
48
50
|
*/
|
|
49
51
|
export function rank(x, options = {}) {
|
|
50
52
|
const method = options.tiesMethod ?? "average";
|
|
@@ -1,30 +1,59 @@
|
|
|
1
1
|
import type { Sample } from "../htest/types.js";
|
|
2
2
|
/**
|
|
3
3
|
* Counts of the integers 1..nbins in `bin` (values truncated towards 0; others and NaN are not counted).
|
|
4
|
-
* Equivalent to R's `tabulate()
|
|
4
|
+
* Equivalent to R's `tabulate()`. `nbins` defaults to max(1, bin).
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* tabulate([1, 2, 2, 5]); // [1, 2, 0, 0, 1]
|
|
9
|
+
* tabulate([1, 2, 2, 5], 3); // [1, 2, 0]
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* @category Descriptive statistics
|
|
5
13
|
*/
|
|
6
14
|
export declare function tabulate(bin: Sample, nbins?: number): number[];
|
|
7
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Options for {@link table}.
|
|
17
|
+
*
|
|
18
|
+
* @category Descriptive statistics
|
|
19
|
+
*/
|
|
8
20
|
export interface TableOptions {
|
|
9
21
|
/** Count missing values: `"no"` (default), `"ifany"` (when there are some) or `"always"`. */
|
|
10
22
|
useNA?: "no" | "ifany" | "always";
|
|
11
23
|
}
|
|
12
24
|
/**
|
|
13
|
-
* Frequency table of the distinct values. Equivalent to R's `table()
|
|
25
|
+
* Frequency table of the distinct values. Equivalent to R's `table()`. For one numeric vector: `values` in increasing
|
|
14
26
|
* order (NaN last when counted) with their `counts`.
|
|
15
27
|
*
|
|
16
28
|
* @example
|
|
17
29
|
* ```ts
|
|
18
30
|
* table([3, 1, 3, 2, 3]); // { values: [1, 2, 3], counts: [1, 1, 3] }
|
|
19
31
|
* ```
|
|
32
|
+
*
|
|
33
|
+
* @category Descriptive statistics
|
|
20
34
|
*/
|
|
21
35
|
export declare function table(x: Sample, options?: TableOptions): {
|
|
36
|
+
/** The distinct values, in increasing order (NaN last when missing values are counted); R's names. */
|
|
22
37
|
values: number[];
|
|
38
|
+
/** How often each value occurs. */
|
|
23
39
|
counts: number[];
|
|
24
40
|
};
|
|
25
|
-
/**
|
|
41
|
+
/**
|
|
42
|
+
* Distinct values in order of first appearance (one NaN at most). Equivalent to R's `unique()`.
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* unique([3, 1, 3, 2, 1]); // [3, 1, 2]
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @category Descriptive statistics
|
|
50
|
+
*/
|
|
26
51
|
export declare function unique(x: Sample): number[];
|
|
27
|
-
/**
|
|
52
|
+
/**
|
|
53
|
+
* Options for {@link cut}.
|
|
54
|
+
*
|
|
55
|
+
* @category Descriptive statistics
|
|
56
|
+
*/
|
|
28
57
|
export interface CutOptions {
|
|
29
58
|
/** Intervals closed on the right, (a, b] (default `true`); otherwise [a, b). */
|
|
30
59
|
right?: boolean;
|
|
@@ -36,9 +65,21 @@ export interface CutOptions {
|
|
|
36
65
|
*
|
|
37
66
|
* @param breaks The break points, or a number of intervals (at least 2): then equal intervals over the range of `x`,
|
|
38
67
|
* widened by 1/1000 of the range at each end, as R does.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* cut([1, 2.5, 4, 6], [0, 2, 4, 6]); // [1, 2, 2, 3]
|
|
72
|
+
* cut([1, 2.5, 4, 6], [0, 2, 4, 6], { right: false }); // [1, 2, 3, NaN]
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* @category Descriptive statistics
|
|
39
76
|
*/
|
|
40
77
|
export declare function cut(x: Sample, breaks: number | readonly number[], options?: CutOptions): number[];
|
|
41
|
-
/**
|
|
78
|
+
/**
|
|
79
|
+
* Options for {@link findInterval}.
|
|
80
|
+
*
|
|
81
|
+
* @category Descriptive statistics
|
|
82
|
+
*/
|
|
42
83
|
export interface FindIntervalOptions {
|
|
43
84
|
/** Treat the last interval as closed on the right (with `leftOpen`, the first as closed on the left). */
|
|
44
85
|
rightmostClosed?: boolean;
|
|
@@ -49,6 +90,13 @@ export interface FindIntervalOptions {
|
|
|
49
90
|
}
|
|
50
91
|
/**
|
|
51
92
|
* For each value, the number of break points at or below it (the interval index). Equivalent to R's
|
|
52
|
-
* `findInterval()
|
|
93
|
+
* `findInterval()`. `vec` must be non-decreasing.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* findInterval([0, 1.5, 3, 5], [1, 2, 3]); // [0, 1, 3, 3]
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @category Descriptive statistics
|
|
53
101
|
*/
|
|
54
102
|
export declare function findInterval(x: Sample, vec: readonly number[], options?: FindIntervalOptions): number[];
|
|
@@ -6,7 +6,15 @@
|
|
|
6
6
|
import { seqLength } from "../internal/seq.js";
|
|
7
7
|
/**
|
|
8
8
|
* Counts of the integers 1..nbins in `bin` (values truncated towards 0; others and NaN are not counted).
|
|
9
|
-
* Equivalent to R's `tabulate()
|
|
9
|
+
* Equivalent to R's `tabulate()`. `nbins` defaults to max(1, bin).
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* tabulate([1, 2, 2, 5]); // [1, 2, 0, 0, 1]
|
|
14
|
+
* tabulate([1, 2, 2, 5], 3); // [1, 2, 0]
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @category Descriptive statistics
|
|
10
18
|
*/
|
|
11
19
|
export function tabulate(bin, nbins) {
|
|
12
20
|
const b = Array.from(bin).map((v) => Math.trunc(v));
|
|
@@ -18,13 +26,15 @@ export function tabulate(bin, nbins) {
|
|
|
18
26
|
return out;
|
|
19
27
|
}
|
|
20
28
|
/**
|
|
21
|
-
* Frequency table of the distinct values. Equivalent to R's `table()
|
|
29
|
+
* Frequency table of the distinct values. Equivalent to R's `table()`. For one numeric vector: `values` in increasing
|
|
22
30
|
* order (NaN last when counted) with their `counts`.
|
|
23
31
|
*
|
|
24
32
|
* @example
|
|
25
33
|
* ```ts
|
|
26
34
|
* table([3, 1, 3, 2, 3]); // { values: [1, 2, 3], counts: [1, 1, 3] }
|
|
27
35
|
* ```
|
|
36
|
+
*
|
|
37
|
+
* @category Descriptive statistics
|
|
28
38
|
*/
|
|
29
39
|
export function table(x, options = {}) {
|
|
30
40
|
const useNA = options.useNA ?? "no";
|
|
@@ -45,7 +55,16 @@ export function table(x, options = {}) {
|
|
|
45
55
|
}
|
|
46
56
|
return { values, counts };
|
|
47
57
|
}
|
|
48
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* Distinct values in order of first appearance (one NaN at most). Equivalent to R's `unique()`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* unique([3, 1, 3, 2, 1]); // [3, 1, 2]
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @category Descriptive statistics
|
|
67
|
+
*/
|
|
49
68
|
export function unique(x) {
|
|
50
69
|
const seen = new Set();
|
|
51
70
|
const out = [];
|
|
@@ -63,6 +82,14 @@ export function unique(x) {
|
|
|
63
82
|
*
|
|
64
83
|
* @param breaks The break points, or a number of intervals (at least 2): then equal intervals over the range of `x`,
|
|
65
84
|
* widened by 1/1000 of the range at each end, as R does.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```ts
|
|
88
|
+
* cut([1, 2.5, 4, 6], [0, 2, 4, 6]); // [1, 2, 2, 3]
|
|
89
|
+
* cut([1, 2.5, 4, 6], [0, 2, 4, 6], { right: false }); // [1, 2, 3, NaN]
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @category Descriptive statistics
|
|
66
93
|
*/
|
|
67
94
|
export function cut(x, breaks, options = {}) {
|
|
68
95
|
const right = options.right ?? true;
|
|
@@ -122,7 +149,14 @@ function binCode(xs, br, right, includeLowest) {
|
|
|
122
149
|
}
|
|
123
150
|
/**
|
|
124
151
|
* For each value, the number of break points at or below it (the interval index). Equivalent to R's
|
|
125
|
-
* `findInterval()
|
|
152
|
+
* `findInterval()`. `vec` must be non-decreasing.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* findInterval([0, 1.5, 3, 5], [1, 2, 3]); // [0, 1, 3, 3]
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @category Descriptive statistics
|
|
126
160
|
*/
|
|
127
161
|
export function findInterval(x, vec, options = {}) {
|
|
128
162
|
const n = vec.length;
|