@numero-uno-jpn/rstats 0.10.0 → 0.11.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 +24 -0
- package/README.ja.md +11 -8
- package/README.md +10 -7
- package/dist/chunks/{chunk-7HPVUVZB.js → chunk-5WDJPHYY.js} +1 -1
- package/dist/chunks/{chunk-EWC6I75B.js → chunk-7GQMKH3Z.js} +1 -1
- package/dist/chunks/chunk-AHHS6AC4.js +3 -0
- package/dist/chunks/chunk-ALM3VHFP.js +2 -0
- package/dist/chunks/{chunk-QFULZNHL.js → chunk-C5XTWMGB.js} +1 -1
- package/dist/chunks/chunk-CGBNKHNZ.js +2 -0
- package/dist/chunks/{chunk-QULPY6YM.js → chunk-EZAFN5CD.js} +1 -1
- package/dist/chunks/{chunk-2XKG3DX4.js → chunk-EZWBRCJL.js} +1 -1
- package/dist/chunks/{chunk-O6PT7IVU.js → chunk-F5EDJFEO.js} +2 -2
- package/dist/chunks/{chunk-4NM37MGA.js → chunk-JWBO5WVN.js} +2 -2
- package/dist/chunks/{chunk-ENL43FNO.js → chunk-TOGXTCZO.js} +2 -2
- package/dist/chunks/{chunk-IT354SHV.js → chunk-VJ4BDBMU.js} +2 -2
- package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-Y5CNQ266.js} +2 -2
- package/dist/chunks/{chunk-LIMZKONM.js → chunk-Y5PYRAYP.js} +2 -2
- package/dist/chunks/{chunk-QJJWGNJP.js → chunk-YSWGDOWK.js} +2 -2
- package/dist/data/grouping.d.ts +79 -0
- package/dist/data/grouping.js +262 -0
- package/dist/descriptive/dataFrames.d.ts +110 -0
- package/dist/descriptive/dataFrames.js +392 -0
- package/dist/descriptive/missing.d.ts +35 -0
- package/dist/descriptive/missing.js +50 -0
- package/dist/descriptive/pivot.d.ts +89 -0
- package/dist/descriptive/pivot.js +343 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +4 -0
- package/dist/entries/descriptive.js +4 -0
- package/dist/htest/ansariTest.d.ts +12 -0
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/cor.d.ts +12 -0
- package/dist/htest/cor.js +9 -27
- package/dist/htest/ksTest.d.ts +13 -0
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/onewayTest.d.ts +14 -0
- package/dist/htest/onewayTest.js +5 -7
- package/dist/htest/rankTests.d.ts +57 -0
- package/dist/htest/rankTests.js +19 -31
- package/dist/htest/tTest.d.ts +20 -0
- 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/varTest.d.ts +23 -0
- package/dist/htest/varTest.js +11 -21
- package/dist/htest/wilcoxTest.d.ts +14 -0
- package/dist/htest/wilcoxTest.js +9 -24
- package/dist/linalg/ops.d.ts +13 -2
- package/dist/linalg/ops.js +20 -5
- package/dist/linalg.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +14 -14
- package/dist/special.js +2 -2
- package/dist/tests.js +12 -13
- package/llms.txt +12 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-AECGWZHD.js +0 -2
- package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
|
@@ -4,5 +4,9 @@ export { quantile, IQR, fivenum, mad } from "../descriptive/quantile.js";
|
|
|
4
4
|
export { rank } from "../descriptive/rank.js";
|
|
5
5
|
export { cov, cor } from "../descriptive/cov.js";
|
|
6
6
|
export { tabulate, table, unique, cut, findInterval } from "../descriptive/table.js";
|
|
7
|
+
export { completeCases, naOmit } from "../descriptive/missing.js";
|
|
7
8
|
export { hist, pretty, ecdf } from "../descriptive/hist.js";
|
|
8
9
|
export { skewness, kurtosis, geometricMean, modes, meanAD, cv, scale, } from "../descriptive/moments.js";
|
|
10
|
+
export { tapply, split, aggregate, } from "../data/grouping.js";
|
|
11
|
+
export { merge, subset, fromRows, toRows, } from "../descriptive/dataFrames.js";
|
|
12
|
+
export { pivotLonger, pivotWider, } from "../descriptive/pivot.js";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
2
3
|
/** Options for {@link ansariTest}. */
|
|
3
4
|
export interface AnsariTestOptions {
|
|
@@ -26,5 +27,16 @@ export interface AnsariTestOptions {
|
|
|
26
27
|
* r.confInt; // [0.25, 0.6666666666666666]
|
|
27
28
|
* r.estimate; // 0.41666666666666663
|
|
28
29
|
* ```
|
|
30
|
+
*
|
|
31
|
+
* There is also a formula form, `ansariTest("y ~ g", data, options)` (next overload).
|
|
29
32
|
*/
|
|
30
33
|
export declare function ansariTest(x: Sample, y: Sample, options?: AnsariTestOptions): HTest;
|
|
34
|
+
/**
|
|
35
|
+
* Ansari-Bradley test from a formula and a data frame. Equivalent to R's `ansari.test(formula, data)`: `"y ~ g"`
|
|
36
|
+
* compares the two levels of `g` (the first level is `x`). Only the variables of the formula count: the rows where
|
|
37
|
+
* one of them is missing are dropped.
|
|
38
|
+
*
|
|
39
|
+
* @param formula `"y ~ g"`.
|
|
40
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
41
|
+
*/
|
|
42
|
+
export declare function ansariTest(formula: string, data: DataFrame, options?: AnsariTestOptions): HTest;
|
package/dist/htest/ansariTest.js
CHANGED
|
@@ -20,25 +20,13 @@ import { pnorm, qnorm } from "../distributions/normal.js";
|
|
|
20
20
|
import { rank } from "../internal/summary.js";
|
|
21
21
|
import { uniroot } from "../internal/uniroot.js";
|
|
22
22
|
import { pansariExact, qansariR } from "./core/ansari.js";
|
|
23
|
+
import { twoGroupData } from "./testFormula.js";
|
|
23
24
|
import { checkConfLevelOpen, matchAlternative } from "./types.js";
|
|
24
|
-
/**
|
|
25
|
-
* Ansari-Bradley test that two samples differ in scale. Equivalent to R's `ansari.test()`.
|
|
26
|
-
* `NaN` values are dropped.
|
|
27
|
-
*
|
|
28
|
-
* With `confInt: true`, `confInt` is the interval for the ratio of the scales of `x` and `y` and `estimate` its
|
|
29
|
-
* estimate, both computed with the locations taken as 0 (exact without ties when both samples have fewer than 50
|
|
30
|
-
* values, otherwise by the normal approximation, whose limits are roots found to a tolerance of 1e-4 as in R). Limits
|
|
31
|
-
* that cannot be formed (the samples differ in location) are `NaN`. With ties and `exact` unset or `true`, R fails
|
|
32
|
-
* ("object 'cint' not found"); rStats.js returns the normal-approximation interval, as with `exact: false`.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* const r = ansariTest([-2, -1, 1, 2], [-4, -3, 3, 4], { confInt: true });
|
|
37
|
-
* r.confInt; // [0.25, 0.6666666666666666]
|
|
38
|
-
* r.estimate; // 0.41666666666666663
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
25
|
export function ansariTest(x, y, options = {}) {
|
|
26
|
+
if (typeof x === "string") {
|
|
27
|
+
const d = twoGroupData(x, y);
|
|
28
|
+
return ansariTest(d.x, d.y, options);
|
|
29
|
+
}
|
|
42
30
|
const xs = Array.from(x).filter((v) => !Number.isNaN(v));
|
|
43
31
|
const ys = Array.from(y).filter((v) => !Number.isNaN(v));
|
|
44
32
|
const m = xs.length;
|
package/dist/htest/cor.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type DD } from "../special/dd.js";
|
|
2
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
2
3
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
3
4
|
/** Correlation coefficient: Pearson's r, Kendall's tau or Spearman's rho. */
|
|
4
5
|
export type CorMethod = "pearson" | "kendall" | "spearman";
|
|
@@ -90,6 +91,17 @@ export declare function kendall(x: readonly number[], y: readonly number[]): Ken
|
|
|
90
91
|
*
|
|
91
92
|
* corTest([1, 2, 3, 4, 5, 6], [2, 1, 4, 3, 7, 8], { method: "spearman" });
|
|
92
93
|
* ```
|
|
94
|
+
*
|
|
95
|
+
* There is also a formula form, `corTest("~ x + y", data, options)` (next overload).
|
|
93
96
|
*/
|
|
94
97
|
export declare function corTest(x: Sample, y: Sample, options?: CorTestOptions): CorTestResult;
|
|
98
|
+
/**
|
|
99
|
+
* Test for correlation from a formula and a data frame. Equivalent to R's `cor.test(formula, data)`: the one-sided
|
|
100
|
+
* formula `"~ x + y"` names the two variables. Only they count: the rows where one of them is missing are dropped.
|
|
101
|
+
*
|
|
102
|
+
* @param formula `"~ x + y"` (exactly two variables, no response).
|
|
103
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
104
|
+
* @param options See {@link CorTestOptions}.
|
|
105
|
+
*/
|
|
106
|
+
export declare function corTest(formula: string, data: DataFrame, options?: CorTestOptions): CorTestResult;
|
|
95
107
|
export {};
|
package/dist/htest/cor.js
CHANGED
|
@@ -27,6 +27,7 @@ import { pt1 } from "../distributions/t.js";
|
|
|
27
27
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
28
28
|
import { ddAdd, ddDiv, ddMul, ddSub, twoProd, twoSum } from "../special/dd.js";
|
|
29
29
|
import { completePairs, meanDD, rank, tieCounts } from "../internal/summary.js";
|
|
30
|
+
import { correlationData } from "./testFormula.js";
|
|
30
31
|
import { checkConfLevel, matchAlternative } from "./types.js";
|
|
31
32
|
const METHODS = ["pearson", "kendall", "spearman"];
|
|
32
33
|
function matchMethod(m) {
|
|
@@ -284,34 +285,15 @@ function spearmanTail(n, q, lower) {
|
|
|
284
285
|
return 0;
|
|
285
286
|
return spearmanEdgeworth(n, q, false);
|
|
286
287
|
}
|
|
287
|
-
// ---- cor.test ----
|
|
288
|
-
/**
|
|
289
|
-
* Test for correlation between paired samples. Equivalent to R's `cor.test()`.
|
|
290
|
-
*
|
|
291
|
-
* Supports Pearson's product-moment correlation (t test, with a Fisher-z confidence
|
|
292
|
-
* interval), Kendall's tau and Spearman's rho (exact or approximate p-values, chosen by
|
|
293
|
-
* the same rules as R). Pairs containing `NaN` are dropped; Pearson needs at least 3
|
|
294
|
-
* pairs and the others at least 2. If the correlation is undefined (constant data) the
|
|
295
|
-
* statistic, p-value and estimate are `NaN`. Correlations are computed in double-double
|
|
296
|
-
* arithmetic, so results can differ from R's when r is very close to ±1, where R's own
|
|
297
|
-
* value is less accurate.
|
|
298
|
-
*
|
|
299
|
-
* @param x First sample.
|
|
300
|
-
* @param y Second sample, of the same length as `x`.
|
|
301
|
-
* @param options See {@link CorTestOptions}.
|
|
302
|
-
* @returns The test result; see {@link CorTestResult}.
|
|
303
|
-
*
|
|
304
|
-
* @example
|
|
305
|
-
* ```ts
|
|
306
|
-
* const r = corTest([1, 2, 3, 4, 5, 6], [2, 1, 4, 3, 7, 8]);
|
|
307
|
-
* r.estimate; // 0.9014600868406593
|
|
308
|
-
* r.statistic; // 4.165113148435108 (t, df = 4)
|
|
309
|
-
* r.pValue; // 0.01408675480909387
|
|
310
|
-
*
|
|
311
|
-
* corTest([1, 2, 3, 4, 5, 6], [2, 1, 4, 3, 7, 8], { method: "spearman" });
|
|
312
|
-
* ```
|
|
313
|
-
*/
|
|
314
288
|
export function corTest(x, y, options = {}) {
|
|
289
|
+
if (typeof x === "string") {
|
|
290
|
+
const d = correlationData(x, y);
|
|
291
|
+
return corTest(d.x, d.y, options);
|
|
292
|
+
}
|
|
293
|
+
return corTestSamples(x, y, options);
|
|
294
|
+
}
|
|
295
|
+
/** cor.test's default form */
|
|
296
|
+
function corTestSamples(x, y, options) {
|
|
315
297
|
if (x.length !== y.length)
|
|
316
298
|
throw new TypeError("x and y must have the same length");
|
|
317
299
|
const alternative = matchAlternative(options.alternative);
|
package/dist/htest/ksTest.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
2
3
|
/** Options for {@link ksTest}. */
|
|
3
4
|
export interface KsTestOptions {
|
|
@@ -27,5 +28,17 @@ export interface KsTestResult extends Omit<HTest, "alternative"> {
|
|
|
27
28
|
* @param x Sample.
|
|
28
29
|
* @param y A continuous distribution function, or a second sample.
|
|
29
30
|
* @param options See {@link KsTestOptions}.
|
|
31
|
+
*
|
|
32
|
+
* There is also a formula form for two samples, `ksTest("y ~ g", data, options)` (next overload).
|
|
30
33
|
*/
|
|
31
34
|
export declare function ksTest(x: Sample, y: Sample | ((q: number) => number), options?: KsTestOptions): KsTestResult;
|
|
35
|
+
/**
|
|
36
|
+
* Two-sample Kolmogorov-Smirnov test from a formula and a data frame. Equivalent to R's `ks.test(formula, data)`:
|
|
37
|
+
* `"y ~ g"` compares the two levels of `g` (the first level is `x`). Only the variables of the formula count: the
|
|
38
|
+
* rows where one of them is missing are dropped.
|
|
39
|
+
*
|
|
40
|
+
* @param formula `"y ~ g"`.
|
|
41
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
42
|
+
* @param options See {@link KsTestOptions}.
|
|
43
|
+
*/
|
|
44
|
+
export declare function ksTest(formula: string, data: DataFrame, options?: KsTestOptions): KsTestResult;
|
package/dist/htest/ksTest.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
// by counting lattice paths, asymptotic ones as for one sample with n replaced by m n / (m + n).
|
|
9
9
|
import { pkolmogorovExact, pkolmogorovLimit, pkolmogorovOneSidedUpper } from "./core/kolmogorov.js";
|
|
10
10
|
import { psmirnovUpper } from "./core/smirnov.js";
|
|
11
|
+
import { twoGroupData } from "./testFormula.js";
|
|
11
12
|
import { matchAlternative } from "./types.js";
|
|
12
13
|
function describe(alternative, oneSample) {
|
|
13
14
|
if (alternative === "two.sided")
|
|
@@ -15,15 +16,11 @@ function describe(alternative, oneSample) {
|
|
|
15
16
|
const where = alternative === "less" ? "below" : "above";
|
|
16
17
|
return `the CDF of x lies ${where} ${oneSample ? "the null hypothesis" : "that of y"}`;
|
|
17
18
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Kolmogorov-Smirnov test. Equivalent to R's `ks.test()`, with a distribution function given as a function (for
|
|
20
|
-
* example `(q) => pnorm(q)`) or a second sample. `NaN` values are dropped.
|
|
21
|
-
*
|
|
22
|
-
* @param x Sample.
|
|
23
|
-
* @param y A continuous distribution function, or a second sample.
|
|
24
|
-
* @param options See {@link KsTestOptions}.
|
|
25
|
-
*/
|
|
26
19
|
export function ksTest(x, y, options = {}) {
|
|
20
|
+
if (typeof x === "string") {
|
|
21
|
+
const d = twoGroupData(x, y);
|
|
22
|
+
return ksTest(d.x, d.y, options);
|
|
23
|
+
}
|
|
27
24
|
const xs = Array.from(x)
|
|
28
25
|
.filter((v) => !Number.isNaN(v))
|
|
29
26
|
.sort((a, b) => a - b);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type GroupLabel } from "./groups.js";
|
|
2
3
|
import type { HTest, Sample } from "./types.js";
|
|
3
4
|
/** Options for {@link onewayTest}. */
|
|
@@ -17,8 +18,21 @@ export interface OnewayTestResult extends Omit<HTest, "alternative" | "parameter
|
|
|
17
18
|
*
|
|
18
19
|
* @param x An array of samples, or one sample with group labels `g`.
|
|
19
20
|
* @param g Group labels, parallel to `x` (or the options, when `x` is an array of samples).
|
|
21
|
+
*
|
|
22
|
+
* There is also a formula form, `onewayTest("y ~ A + B", data, options)` (next overload).
|
|
20
23
|
*/
|
|
21
24
|
export declare function onewayTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[] | OnewayTestOptions, options?: OnewayTestOptions): OnewayTestResult;
|
|
25
|
+
/**
|
|
26
|
+
* One-way analysis of means from a formula and a data frame. Equivalent to R's `oneway.test(formula, data)`:
|
|
27
|
+
* `"y ~ A"` has one group per level of `A`; with several factors, `"y ~ A + B"`, the groups are the combinations of
|
|
28
|
+
* their levels present in the data (R's `interaction()`, the first factor varying fastest). Only the variables of
|
|
29
|
+
* the formula count: the rows where one of them is missing are dropped.
|
|
30
|
+
*
|
|
31
|
+
* @param formula `"y ~ A"` or `"y ~ A + B + ..."`.
|
|
32
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
33
|
+
* @param options See {@link OnewayTestOptions}.
|
|
34
|
+
*/
|
|
35
|
+
export declare function onewayTest(formula: string, data: DataFrame, options?: OnewayTestOptions): OnewayTestResult;
|
|
22
36
|
/**
|
|
23
37
|
* Shapiro-Wilk test of normality. Equivalent to R's `shapiro.test()`. `NaN` values are dropped; the sample size must
|
|
24
38
|
* be between 3 and 5000.
|
package/dist/htest/onewayTest.js
CHANGED
|
@@ -9,14 +9,12 @@ import { pf1 } from "../distributions/f.js";
|
|
|
9
9
|
import { mean, variance } from "../internal/summary.js";
|
|
10
10
|
import { swilkOneMinusW, swilkPValueFromOneMinusW } from "./core/shapiro.js";
|
|
11
11
|
import { toGroups } from "./groups.js";
|
|
12
|
-
|
|
13
|
-
* Test that several normal samples have equal means, without (default) or with the assumption of equal variances.
|
|
14
|
-
* Equivalent to R's `oneway.test()`.
|
|
15
|
-
*
|
|
16
|
-
* @param x An array of samples, or one sample with group labels `g`.
|
|
17
|
-
* @param g Group labels, parallel to `x` (or the options, when `x` is an array of samples).
|
|
18
|
-
*/
|
|
12
|
+
import { onewayData } from "./testFormula.js";
|
|
19
13
|
export function onewayTest(x, g, options = {}) {
|
|
14
|
+
if (typeof x === "string") {
|
|
15
|
+
const d = onewayData(x, g);
|
|
16
|
+
return onewayTest(d.values, d.groups, options);
|
|
17
|
+
}
|
|
20
18
|
let labels;
|
|
21
19
|
if (g !== undefined && !Array.isArray(g))
|
|
22
20
|
options = g;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type GroupLabel } from "./groups.js";
|
|
2
3
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
3
4
|
/** Result of a k-sample test with a chi-squared statistic. Has no `alternative`, as R returns none. */
|
|
@@ -11,15 +12,38 @@ export interface ChisqStatTestResult extends Omit<HTest, "alternative"> {
|
|
|
11
12
|
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
12
13
|
* labels `g`.
|
|
13
14
|
* @param g Group labels, parallel to `x`.
|
|
15
|
+
*
|
|
16
|
+
* There is also a formula form, `kruskalTest("y ~ g", data)` (next overload).
|
|
14
17
|
*/
|
|
15
18
|
export declare function kruskalTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[]): ChisqStatTestResult;
|
|
19
|
+
/**
|
|
20
|
+
* Kruskal-Wallis test from a formula and a data frame. Equivalent to R's `kruskal.test(formula, data)`: `"y ~ g"`,
|
|
21
|
+
* one group per level of `g`. Only the variables of the formula count: the rows where one of them is missing are
|
|
22
|
+
* dropped.
|
|
23
|
+
*
|
|
24
|
+
* @param formula `"y ~ g"` (one variable on each side).
|
|
25
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
26
|
+
*/
|
|
27
|
+
export declare function kruskalTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
16
28
|
/**
|
|
17
29
|
* Friedman rank sum test for an unreplicated complete block design. Equivalent to R's `friedman.test()` with a
|
|
18
30
|
* matrix.
|
|
19
31
|
*
|
|
20
32
|
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
33
|
+
*
|
|
34
|
+
* There is also a formula form, `friedmanTest("y ~ w | b", data)` (next overload).
|
|
21
35
|
*/
|
|
22
36
|
export declare function friedmanTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
|
|
37
|
+
/**
|
|
38
|
+
* Friedman test from a formula and a data frame. Equivalent to R's `friedman.test(formula, data)`: `"y ~ w | b"`
|
|
39
|
+
* with the groups `w` and the blocks `b`. The rows where `y`, `w` or `b` is missing are dropped first; then every
|
|
40
|
+
* combination of group and block must occur exactly once (else the error `not an unreplicated complete block
|
|
41
|
+
* design`, so that one missing value fails, unlike the matrix form, which drops its block).
|
|
42
|
+
*
|
|
43
|
+
* @param formula `"y ~ w | b"` (one variable on each side of `|`).
|
|
44
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
45
|
+
*/
|
|
46
|
+
export declare function friedmanTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
23
47
|
/**
|
|
24
48
|
* Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to DescTools'
|
|
25
49
|
* `CochranQTest()` with a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
|
|
@@ -37,16 +61,38 @@ export interface QuadeTestResult extends Omit<HTest, "alternative" | "parameter"
|
|
|
37
61
|
* Quade test for an unreplicated complete block design. Equivalent to R's `quade.test()` with a matrix.
|
|
38
62
|
*
|
|
39
63
|
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
64
|
+
*
|
|
65
|
+
* There is also a formula form, `quadeTest("y ~ w | b", data)` (next overload).
|
|
40
66
|
*/
|
|
41
67
|
export declare function quadeTest(y: readonly (readonly number[])[]): QuadeTestResult;
|
|
68
|
+
/**
|
|
69
|
+
* Quade test from a formula and a data frame. Equivalent to R's `quade.test(formula, data)`: `"y ~ w | b"` with the
|
|
70
|
+
* groups `w` and the blocks `b`. The rows where `y`, `w` or `b` is missing are dropped first; then every combination
|
|
71
|
+
* of group and block must occur exactly once (else the error `not an unreplicated complete block design`).
|
|
72
|
+
*
|
|
73
|
+
* @param formula `"y ~ w | b"` (one variable on each side of `|`).
|
|
74
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
75
|
+
*/
|
|
76
|
+
export declare function quadeTest(formula: string, data: DataFrame): QuadeTestResult;
|
|
42
77
|
/**
|
|
43
78
|
* Fligner-Killeen (median) test of homogeneity of variances. Equivalent to R's `fligner.test()`.
|
|
44
79
|
*
|
|
45
80
|
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
46
81
|
* labels `g`.
|
|
47
82
|
* @param g Group labels, parallel to `x`.
|
|
83
|
+
*
|
|
84
|
+
* There is also a formula form, `flignerTest("y ~ g", data)` (next overload).
|
|
48
85
|
*/
|
|
49
86
|
export declare function flignerTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[]): ChisqStatTestResult;
|
|
87
|
+
/**
|
|
88
|
+
* Fligner-Killeen test from a formula and a data frame. Equivalent to R's `fligner.test(formula, data)`: `"y ~ g"`,
|
|
89
|
+
* one group per level of `g`. Only the variables of the formula count: the rows where one of them is missing are
|
|
90
|
+
* dropped.
|
|
91
|
+
*
|
|
92
|
+
* @param formula `"y ~ g"` (one variable on each side).
|
|
93
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
94
|
+
*/
|
|
95
|
+
export declare function flignerTest(formula: string, data: DataFrame): ChisqStatTestResult;
|
|
50
96
|
/** Options for {@link moodTest}. */
|
|
51
97
|
export interface MoodTestOptions {
|
|
52
98
|
/** Alternative hypothesis (unambiguous abbreviations accepted). Default `"two.sided"`. */
|
|
@@ -55,5 +101,16 @@ export interface MoodTestOptions {
|
|
|
55
101
|
/**
|
|
56
102
|
* Mood's two-sample test of scale. Equivalent to R's `mood.test()`. `NaN` values are dropped; ties get the average
|
|
57
103
|
* of the scores of their positions.
|
|
104
|
+
*
|
|
105
|
+
* There is also a formula form, `moodTest("y ~ g", data, options)` (next overload).
|
|
58
106
|
*/
|
|
59
107
|
export declare function moodTest(x: Sample, y: Sample, options?: MoodTestOptions): HTest;
|
|
108
|
+
/**
|
|
109
|
+
* Mood's test from a formula and a data frame. Equivalent to R's `mood.test(formula, data)`: `"y ~ g"` compares the
|
|
110
|
+
* two levels of `g` (the first level is `x`). Only the variables of the formula count: the rows where one of them is
|
|
111
|
+
* missing are dropped.
|
|
112
|
+
*
|
|
113
|
+
* @param formula `"y ~ g"`.
|
|
114
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
115
|
+
*/
|
|
116
|
+
export declare function moodTest(formula: string, data: DataFrame, options?: MoodTestOptions): HTest;
|
package/dist/htest/rankTests.js
CHANGED
|
@@ -21,6 +21,7 @@ import { pgamma1 } from "../distributions/gamma.js";
|
|
|
21
21
|
import { pnorm, qnorm } from "../distributions/normal.js";
|
|
22
22
|
import { rank, tieCounts, variance } from "../internal/summary.js";
|
|
23
23
|
import { completeRows, median, toGroups } from "./groups.js";
|
|
24
|
+
import { blockData, kSampleData, twoGroupData } from "./testFormula.js";
|
|
24
25
|
import { matchAlternative } from "./types.js";
|
|
25
26
|
function chisqResult(method, name, statistic, df) {
|
|
26
27
|
return {
|
|
@@ -38,14 +39,11 @@ function sumTies(x) {
|
|
|
38
39
|
s += t * t * t - t;
|
|
39
40
|
return s;
|
|
40
41
|
}
|
|
41
|
-
/**
|
|
42
|
-
* Kruskal-Wallis rank sum test. Equivalent to R's `kruskal.test()`.
|
|
43
|
-
*
|
|
44
|
-
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
45
|
-
* labels `g`.
|
|
46
|
-
* @param g Group labels, parallel to `x`.
|
|
47
|
-
*/
|
|
48
42
|
export function kruskalTest(x, g) {
|
|
43
|
+
if (typeof x === "string") {
|
|
44
|
+
const d = kSampleData(x, g);
|
|
45
|
+
return kruskalTest(d.values, d.group.codes);
|
|
46
|
+
}
|
|
49
47
|
const groups = toGroups(x, g, "error");
|
|
50
48
|
const k = groups.length;
|
|
51
49
|
if (k < 2)
|
|
@@ -65,13 +63,9 @@ export function kruskalTest(x, g) {
|
|
|
65
63
|
stat = ((12 * stat) / (n * (n + 1)) - 3 * (n + 1)) / (1 - sumTies(all) / (n * n * n - n));
|
|
66
64
|
return chisqResult("Kruskal-Wallis rank sum test", "Kruskal-Wallis chi-squared", stat, k - 1);
|
|
67
65
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
*
|
|
72
|
-
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
73
|
-
*/
|
|
74
|
-
export function friedmanTest(y) {
|
|
66
|
+
export function friedmanTest(y, data) {
|
|
67
|
+
if (typeof y === "string")
|
|
68
|
+
return friedmanTest(blockData(y, data));
|
|
75
69
|
const rows = completeRows(y);
|
|
76
70
|
const n = rows.length;
|
|
77
71
|
const k = y[0]?.length ?? 0;
|
|
@@ -101,12 +95,9 @@ export function friedmanTest(y) {
|
|
|
101
95
|
export function cochranQTest(y) {
|
|
102
96
|
return { ...friedmanTest(y), method: "Cochran's Q test", statisticName: "Q" };
|
|
103
97
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
* @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
|
|
108
|
-
*/
|
|
109
|
-
export function quadeTest(y) {
|
|
98
|
+
export function quadeTest(y, data) {
|
|
99
|
+
if (typeof y === "string")
|
|
100
|
+
return quadeTest(blockData(y, data));
|
|
110
101
|
const rows = completeRows(y);
|
|
111
102
|
// as in R, b counts all blocks, including those dropped for a missing value
|
|
112
103
|
const b = y.length;
|
|
@@ -155,14 +146,11 @@ export function quadeTest(y) {
|
|
|
155
146
|
pValue,
|
|
156
147
|
};
|
|
157
148
|
}
|
|
158
|
-
/**
|
|
159
|
-
* Fligner-Killeen (median) test of homogeneity of variances. Equivalent to R's `fligner.test()`.
|
|
160
|
-
*
|
|
161
|
-
* @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
|
|
162
|
-
* labels `g`.
|
|
163
|
-
* @param g Group labels, parallel to `x`.
|
|
164
|
-
*/
|
|
165
149
|
export function flignerTest(x, g) {
|
|
150
|
+
if (typeof x === "string") {
|
|
151
|
+
const d = kSampleData(x, g);
|
|
152
|
+
return flignerTest(d.values, d.group.codes);
|
|
153
|
+
}
|
|
166
154
|
const groups = toGroups(x, g, "error");
|
|
167
155
|
const k = groups.length;
|
|
168
156
|
if (k < 2)
|
|
@@ -191,11 +179,11 @@ export function flignerTest(x, g) {
|
|
|
191
179
|
stat /= variance(a);
|
|
192
180
|
return chisqResult("Fligner-Killeen test of homogeneity of variances", "Fligner-Killeen:med chi-squared", stat, k - 1);
|
|
193
181
|
}
|
|
194
|
-
/**
|
|
195
|
-
* Mood's two-sample test of scale. Equivalent to R's `mood.test()`. `NaN` values are dropped; ties get the average
|
|
196
|
-
* of the scores of their positions.
|
|
197
|
-
*/
|
|
198
182
|
export function moodTest(x, y, options = {}) {
|
|
183
|
+
if (typeof x === "string") {
|
|
184
|
+
const d = twoGroupData(x, y);
|
|
185
|
+
return moodTest(d.x, d.y, options);
|
|
186
|
+
}
|
|
199
187
|
// as in R, infinite values are dropped along with NaN
|
|
200
188
|
const xs = Array.from(x).filter(Number.isFinite);
|
|
201
189
|
const ys = Array.from(y).filter(Number.isFinite);
|
package/dist/htest/tTest.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { DataFrame } from "../models/modelFrame.js";
|
|
1
2
|
import { type Alternative, type HTest, type Sample } from "./types.js";
|
|
2
3
|
/** Options for {@link tTest}. */
|
|
3
4
|
export interface TTestOptions {
|
|
@@ -53,5 +54,24 @@ export interface TTestResult extends HTest {
|
|
|
53
54
|
*
|
|
54
55
|
* tTest([1, 2, 3, 4, 5], [3, 4, 5, 6, 8]).pValue; // 0.0849560662972433 (Welch)
|
|
55
56
|
* ```
|
|
57
|
+
*
|
|
58
|
+
* There is also a formula form, `tTest("y ~ g", data, options)` (next overload).
|
|
56
59
|
*/
|
|
57
60
|
export declare function tTest(x: Sample, y?: Sample | TTestOptions, options?: TTestOptions): TTestResult;
|
|
61
|
+
/**
|
|
62
|
+
* Student's t-test from a formula and a data frame. Equivalent to R's `t.test(formula, data)`: `"y ~ g"` compares
|
|
63
|
+
* the two levels of `g` (the first level is `x`; `estimateNames` and `nullValueName` name the levels, as in R),
|
|
64
|
+
* `"y ~ 1"` is the one-sample test and `"Pair(a, b) ~ 1"` the paired test of `a` and `b` (the option `paired` is not
|
|
65
|
+
* allowed). Only the variables of the formula count: the rows where one of them is missing are dropped.
|
|
66
|
+
*
|
|
67
|
+
* @param formula `"y ~ g"`, `"y ~ 1"` or `"Pair(a, b) ~ 1"`.
|
|
68
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
69
|
+
* @param options See {@link TTestOptions}.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* tTest("y ~ g", { y: [5.1, 4.9, 5.6, 5.8, 6.0, 5.4], g: ["a", "a", "a", "b", "b", "b"] }).estimateNames;
|
|
74
|
+
* // ["mean in group a", "mean in group b"]
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function tTest(formula: string, data: DataFrame, options?: TTestOptions): TTestResult;
|
package/dist/htest/tTest.js
CHANGED
|
@@ -7,32 +7,11 @@
|
|
|
7
7
|
// - If the standard error is below 10 eps times the absolute mean, the data are treated as constant and an error is raised (as in R)
|
|
8
8
|
import { pt1, qt1 } from "../distributions/t.js";
|
|
9
9
|
import { completePairs, dropNaN, mean, variance } from "../internal/summary.js";
|
|
10
|
+
import { twoSampleData } from "./testFormula.js";
|
|
10
11
|
import { checkConfLevel, matchAlternative } from "./types.js";
|
|
11
|
-
/**
|
|
12
|
-
* Student's t-test. Equivalent to R's `t.test()`.
|
|
13
|
-
*
|
|
14
|
-
* Performs a one-sample, two-sample (Welch's by default, or pooled-variance with
|
|
15
|
-
* `varEqual: true`) or paired t-test. `NaN` values are dropped. As in R, an error is
|
|
16
|
-
* thrown if the data are essentially constant.
|
|
17
|
-
*
|
|
18
|
-
* @param x Sample.
|
|
19
|
-
* @param y Second sample for a two-sample or paired test. May be omitted, and the options
|
|
20
|
-
* passed in its place: `tTest(x, { mu: 1 })`.
|
|
21
|
-
* @param options See {@link TTestOptions}.
|
|
22
|
-
* @returns The test result, with `statisticName` `"t"`; see {@link TTestResult}.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* const r = tTest([5.1, 4.9, 5.6, 5.8, 6.0, 5.4], { mu: 5 });
|
|
27
|
-
* r.statistic; // 2.7351263280759737
|
|
28
|
-
* r.parameter; // 5
|
|
29
|
-
* r.pValue; // 0.04102919855187924
|
|
30
|
-
* r.confInt; // [5.028074545960439, 5.905258787372895]
|
|
31
|
-
*
|
|
32
|
-
* tTest([1, 2, 3, 4, 5], [3, 4, 5, 6, 8]).pValue; // 0.0849560662972433 (Welch)
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
12
|
export function tTest(x, y, options = {}) {
|
|
13
|
+
if (typeof x === "string")
|
|
14
|
+
return tTestFormula(x, y, options);
|
|
36
15
|
// Also accept the form tTest(x, { mu: 1 })
|
|
37
16
|
let ys;
|
|
38
17
|
if (y !== undefined && !ArrayBuffer.isView(y) && !Array.isArray(y))
|
|
@@ -151,3 +130,19 @@ export function tTest(x, y, options = {}) {
|
|
|
151
130
|
stdErr,
|
|
152
131
|
};
|
|
153
132
|
}
|
|
133
|
+
/** t.test from a formula: the default form on the data the formula selects, with the estimates named after the levels */
|
|
134
|
+
function tTestFormula(formula, data, options) {
|
|
135
|
+
if (options.paired !== undefined)
|
|
136
|
+
throw new Error("cannot use 'paired' in formula method");
|
|
137
|
+
const d = twoSampleData(formula, data, true);
|
|
138
|
+
if (d.kind === "one")
|
|
139
|
+
return tTest(d.x, options);
|
|
140
|
+
if (d.kind === "paired")
|
|
141
|
+
return tTest(d.x, d.y, { ...options, paired: true });
|
|
142
|
+
const [first, second] = d.levels;
|
|
143
|
+
return {
|
|
144
|
+
...tTest(d.x, d.y, options),
|
|
145
|
+
estimateNames: [`mean in group ${first}`, `mean in group ${second}`],
|
|
146
|
+
nullValueName: `difference in means between group ${first} and group ${second}`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { type DataFrame } from "../models/modelFrame.js";
|
|
2
|
+
/** A grouping variable as a factor: its levels and the 0-based level of each row. */
|
|
3
|
+
interface GroupFactor {
|
|
4
|
+
levels: string[];
|
|
5
|
+
codes: number[];
|
|
6
|
+
}
|
|
7
|
+
/** The data of a two-sample test's formula: one sample (`y ~ 1`), a pair (`Pair(a, b) ~ 1`) or two groups (`y ~ g`) */
|
|
8
|
+
export type TwoSampleData = {
|
|
9
|
+
kind: "one";
|
|
10
|
+
x: number[];
|
|
11
|
+
} | {
|
|
12
|
+
kind: "paired";
|
|
13
|
+
x: number[];
|
|
14
|
+
y: number[];
|
|
15
|
+
} | {
|
|
16
|
+
kind: "two";
|
|
17
|
+
x: number[];
|
|
18
|
+
y: number[];
|
|
19
|
+
levels: [string, string];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The samples of a two-sample test's formula. `oneSample` allows `y ~ 1` and `Pair(a, b) ~ 1` (t.test and
|
|
23
|
+
* wilcox.test).
|
|
24
|
+
*/
|
|
25
|
+
export declare function twoSampleData(formula: string, data: DataFrame, oneSample: boolean): TwoSampleData;
|
|
26
|
+
/** The two samples of a formula `y ~ g` whose group has two levels (var.test, ansari.test, mood.test, ks.test) */
|
|
27
|
+
export declare function twoGroupData(formula: string, data: DataFrame): {
|
|
28
|
+
x: number[];
|
|
29
|
+
y: number[];
|
|
30
|
+
};
|
|
31
|
+
/** The response and group of a formula `y ~ g` with one variable on the right (kruskal.test, fligner.test, bartlett.test) */
|
|
32
|
+
export declare function kSampleData(formula: string, data: DataFrame): {
|
|
33
|
+
values: number[];
|
|
34
|
+
group: GroupFactor;
|
|
35
|
+
};
|
|
36
|
+
/** The samples of each level, in the order of the levels */
|
|
37
|
+
export declare function samplesByLevel(values: readonly number[], group: GroupFactor): number[][];
|
|
38
|
+
/**
|
|
39
|
+
* The response and groups of oneway.test's formula `y ~ A` or `y ~ A + B + ...`: with several factors the groups are
|
|
40
|
+
* the combinations present in the data, numbered with the first factor varying fastest (R's interaction()).
|
|
41
|
+
*/
|
|
42
|
+
export declare function onewayData(formula: string, data: DataFrame): {
|
|
43
|
+
values: number[];
|
|
44
|
+
groups: number[];
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The matrix (one row per block, one column per group, levels in order) of a blocked design's formula `y ~ w | b`
|
|
48
|
+
* (friedman.test, quade.test). Rows with a missing value are dropped first; every combination of group and block must
|
|
49
|
+
* then occur exactly once.
|
|
50
|
+
*/
|
|
51
|
+
export declare function blockData(formula: string, data: DataFrame): number[][];
|
|
52
|
+
/** The two variables of cor.test's one-sided formula `~ x + z` */
|
|
53
|
+
export declare function correlationData(formula: string, data: DataFrame): {
|
|
54
|
+
x: number[];
|
|
55
|
+
y: number[];
|
|
56
|
+
};
|
|
57
|
+
export {};
|