@numero-uno-jpn/rstats 0.9.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 +87 -0
- package/README.ja.md +64 -12
- package/README.md +63 -11
- package/THIRD_PARTY_NOTICES.md +7 -2
- package/dist/chunks/chunk-5WDJPHYY.js +2 -0
- package/dist/chunks/chunk-7GQMKH3Z.js +2 -0
- package/dist/chunks/chunk-AHHS6AC4.js +3 -0
- package/dist/chunks/chunk-ALM3VHFP.js +2 -0
- package/dist/chunks/{chunk-TOVTN5Y2.js → chunk-C5XTWMGB.js} +1 -1
- package/dist/chunks/chunk-CGBNKHNZ.js +2 -0
- package/dist/chunks/{chunk-LWUSYIQ4.js → chunk-EZAFN5CD.js} +1 -1
- package/dist/chunks/{chunk-H2AP6TCR.js → chunk-EZWBRCJL.js} +1 -1
- package/dist/chunks/chunk-F5EDJFEO.js +2 -0
- package/dist/chunks/{chunk-FC2TLHZC.js → chunk-JWBO5WVN.js} +2 -2
- package/dist/chunks/{chunk-LE7TBHNI.js → chunk-TOGXTCZO.js} +2 -2
- package/dist/chunks/chunk-VJ4BDBMU.js +3 -0
- package/dist/chunks/chunk-Y5CNQ266.js +2 -0
- package/dist/chunks/{chunk-BGVMSDVJ.js → chunk-Y5PYRAYP.js} +2 -2
- package/dist/chunks/{chunk-5K4PS7IV.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/factanal.d.ts +56 -0
- package/dist/descriptive/factanal.js +186 -0
- package/dist/descriptive/missing.d.ts +35 -0
- package/dist/descriptive/missing.js +50 -0
- package/dist/descriptive/omega.d.ts +56 -0
- package/dist/descriptive/omega.js +323 -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/bvnorm.d.ts +15 -0
- package/dist/distributions/bvnorm.js +131 -0
- package/dist/distributions/nbeta.js +34 -1
- package/dist/distributions/nchisq.d.ts +10 -0
- package/dist/distributions/nchisq.js +46 -0
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +4 -0
- package/dist/entries/descriptive.js +4 -0
- package/dist/entries/distributions.d.ts +1 -0
- package/dist/entries/distributions.js +1 -0
- package/dist/entries/linalg.d.ts +2 -0
- package/dist/entries/linalg.js +2 -0
- package/dist/entries/numerics.d.ts +2 -0
- package/dist/entries/numerics.js +1 -0
- package/dist/entries/tests.d.ts +22 -1
- package/dist/entries/tests.js +19 -1
- package/dist/htest/agreement.d.ts +141 -0
- package/dist/htest/agreement.js +274 -0
- package/dist/htest/ansariTest.d.ts +12 -0
- package/dist/htest/ansariTest.js +5 -17
- package/dist/htest/aov.d.ts +19 -0
- package/dist/htest/aov.js +22 -15
- package/dist/htest/cor.d.ts +12 -0
- package/dist/htest/cor.js +9 -27
- package/dist/htest/core/simulate.d.ts +2 -0
- package/dist/htest/core/simulate.js +1 -1
- package/dist/htest/dunnett.d.ts +5 -0
- package/dist/htest/dunnett.js +89 -0
- package/dist/htest/effectSize.d.ts +112 -0
- package/dist/htest/effectSize.js +385 -0
- package/dist/htest/ksTest.d.ts +13 -0
- package/dist/htest/ksTest.js +5 -8
- package/dist/htest/leveneTest.d.ts +30 -0
- package/dist/htest/leveneTest.js +41 -0
- package/dist/htest/multipleComparisons.d.ts +147 -0
- package/dist/htest/multipleComparisons.js +369 -0
- package/dist/htest/normalityTests.d.ts +44 -0
- package/dist/htest/normalityTests.js +164 -0
- package/dist/htest/onewayTest.d.ts +14 -0
- package/dist/htest/onewayTest.js +5 -7
- package/dist/htest/polycor.d.ts +117 -0
- package/dist/htest/polycor.js +468 -0
- package/dist/htest/power.d.ts +132 -0
- package/dist/htest/power.js +227 -0
- package/dist/htest/pwr.d.ts +263 -0
- package/dist/htest/pwr.js +440 -0
- package/dist/htest/rankTests.d.ts +64 -0
- package/dist/htest/rankTests.js +27 -29
- 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/trendTests.d.ts +57 -0
- package/dist/htest/trendTests.js +167 -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/eigen.d.ts +8 -0
- package/dist/linalg/eigen.js +5 -4
- package/dist/linalg/expm.d.ts +48 -0
- package/dist/linalg/expm.js +472 -0
- package/dist/linalg/ops.d.ts +13 -2
- package/dist/linalg/ops.js +20 -5
- package/dist/linalg/schur.d.ts +48 -0
- package/dist/linalg/schur.js +225 -0
- package/dist/linalg.js +2 -2
- package/dist/models/anovaSequential.d.ts +58 -0
- package/dist/models/anovaSequential.js +138 -0
- package/dist/models/aovEz.d.ts +64 -0
- package/dist/models/aovEz.js +332 -0
- package/dist/models/aovStrata.d.ts +32 -0
- package/dist/models/aovStrata.js +104 -0
- package/dist/models/carAnova.d.ts +30 -0
- package/dist/models/carAnova.js +121 -0
- package/dist/models/coxph.d.ts +101 -0
- package/dist/models/coxph.js +502 -0
- package/dist/models/ddLeastSquares.d.ts +30 -0
- package/dist/models/ddLeastSquares.js +90 -0
- package/dist/models/ddMatrix.d.ts +10 -0
- package/dist/models/ddMatrix.js +91 -0
- package/dist/models/formula.d.ts +83 -0
- package/dist/models/formula.js +333 -0
- package/dist/models/lm.d.ts +84 -3
- package/dist/models/lm.js +141 -44
- package/dist/models/mauchly.d.ts +48 -0
- package/dist/models/mauchly.js +123 -0
- package/dist/models/modelFrame.d.ts +40 -0
- package/dist/models/modelFrame.js +131 -0
- package/dist/models/modelMatrix.d.ts +88 -0
- package/dist/models/modelMatrix.js +170 -0
- package/dist/models/survival.d.ts +87 -0
- package/dist/models/survival.js +238 -0
- package/dist/numerics.js +2 -2
- package/dist/optim/lbfgsb.d.ts +20 -0
- package/dist/optim/lbfgsb.js +1240 -0
- package/dist/optim/optim.d.ts +85 -0
- package/dist/optim/optim.js +610 -0
- package/dist/rstats.js +19 -4
- package/dist/special.js +2 -2
- package/dist/tests.js +17 -4
- package/llms.txt +114 -0
- package/package.json +3 -2
- package/dist/chunks/chunk-24U4DKRY.js +0 -2
- package/dist/chunks/chunk-IVOBWYLV.js +0 -2
- package/dist/chunks/chunk-JLSTEMSN.js +0 -2
- package/dist/chunks/chunk-MF4VBCHQ.js +0 -2
- package/dist/chunks/chunk-MSAUU6PY.js +0 -2
- package/dist/chunks/chunk-YC4JWUKG.js +0 -2
- package/dist/chunks/chunk-ZRO446BP.js +0 -2
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 {};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Formula forms of the hypothesis tests (R's formula methods of t.test, wilcox.test, kruskal.test, friedman.test,
|
|
2
|
+
// cor.test, ...): the data a formula selects from a data frame, which the tests then pass to their default forms.
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js (no ported code) from oracle/formulatests-behaviour.md. Only the variables of the formula
|
|
5
|
+
// count: the rows where one of them is missing are dropped (src/models/modelFrame.ts), and a grouping variable becomes
|
|
6
|
+
// a factor of the remaining values, with sorted levels (numbers as numbers, strings by code point) and no unused
|
|
7
|
+
// level. The formula is parsed with src/models/formula.ts; `Pair(a, b)` on the left and `|` on the right, which that
|
|
8
|
+
// parser does not know, are split off the text first.
|
|
9
|
+
import { parseFormula, termsOf } from "../models/formula.js";
|
|
10
|
+
import { modelFrame } from "../models/modelFrame.js";
|
|
11
|
+
import { formatR } from "./core/simulate.js";
|
|
12
|
+
/** A variable of a model frame as a factor: numbers sorted as numbers and labelled as R prints them */
|
|
13
|
+
function groupFactor(v) {
|
|
14
|
+
if (v.kind === "factor")
|
|
15
|
+
return { levels: v.levels, codes: v.codes };
|
|
16
|
+
const sorted = [...new Set(v.values)].sort((a, b) => a - b);
|
|
17
|
+
const levels = [...new Set(sorted.map(formatR))];
|
|
18
|
+
const position = new Map(levels.map((l, i) => [l, i]));
|
|
19
|
+
return { levels, codes: v.values.map((x) => position.get(formatR(x))) };
|
|
20
|
+
}
|
|
21
|
+
/** The values of a numeric variable of a model frame (a response or a variable of a correlation) */
|
|
22
|
+
function numericValues(v, name) {
|
|
23
|
+
if (v.kind !== "numeric")
|
|
24
|
+
throw new TypeError(`'${name}' must be a numeric column`);
|
|
25
|
+
return v.values;
|
|
26
|
+
}
|
|
27
|
+
/** The terms of a formula without `Pair()` or `|`, and the names of the variables of its right-hand side */
|
|
28
|
+
function plainTerms(formula, data) {
|
|
29
|
+
const parsed = parseFormula(formula);
|
|
30
|
+
const terms = termsOf(parsed, Object.keys(data));
|
|
31
|
+
return { terms, rhsVariables: terms.variables.slice(terms.response === 0 ? 1 : 0) };
|
|
32
|
+
}
|
|
33
|
+
/** The text on each side of the formula's `~` (the left side empty for a one-sided formula) */
|
|
34
|
+
function splitTilde(formula) {
|
|
35
|
+
if (typeof formula !== "string")
|
|
36
|
+
throw new TypeError("the formula must be a string");
|
|
37
|
+
const at = formula.indexOf("~");
|
|
38
|
+
return at < 0 ? undefined : { lhs: formula.slice(0, at).trim(), rhs: formula.slice(at + 1) };
|
|
39
|
+
}
|
|
40
|
+
const PAIR = /^Pair\s*\(\s*([A-Za-z.][A-Za-z0-9._]*)\s*,\s*([A-Za-z.][A-Za-z0-9._]*)\s*\)$/;
|
|
41
|
+
const MISSING_OR_INCORRECT = "'formula' missing or incorrect";
|
|
42
|
+
/** Whether a right-hand side is the intercept alone (`~ 1`) */
|
|
43
|
+
const interceptOnly = (terms, rhsVariables) => rhsVariables.length === 0 && terms.terms.length === 0 && terms.intercept;
|
|
44
|
+
/**
|
|
45
|
+
* The samples of a two-sample test's formula. `oneSample` allows `y ~ 1` and `Pair(a, b) ~ 1` (t.test and
|
|
46
|
+
* wilcox.test).
|
|
47
|
+
*/
|
|
48
|
+
export function twoSampleData(formula, data, oneSample) {
|
|
49
|
+
const sides = splitTilde(formula);
|
|
50
|
+
const pair = sides === undefined ? null : PAIR.exec(sides.lhs);
|
|
51
|
+
if (pair !== null) {
|
|
52
|
+
const { terms, rhsVariables } = plainTerms(`~${sides.rhs}`, data);
|
|
53
|
+
if (!oneSample || !interceptOnly(terms, rhsVariables))
|
|
54
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
55
|
+
const frame = modelFrame(data, [pair[1], pair[2]]);
|
|
56
|
+
return { kind: "paired", x: numericValues(frame.variables[0], pair[1]), y: numericValues(frame.variables[1], pair[2]) };
|
|
57
|
+
}
|
|
58
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
59
|
+
if (terms.response < 0)
|
|
60
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
61
|
+
const response = terms.variables[0];
|
|
62
|
+
if (oneSample && interceptOnly(terms, rhsVariables)) {
|
|
63
|
+
return { kind: "one", x: numericValues(modelFrame(data, [response]).variables[0], response) };
|
|
64
|
+
}
|
|
65
|
+
if (rhsVariables.length !== 1 || terms.terms.length !== 1)
|
|
66
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
67
|
+
const frame = modelFrame(data, [response, rhsVariables[0]], { dropUnusedLevels: true });
|
|
68
|
+
const values = numericValues(frame.variables[0], response);
|
|
69
|
+
const group = groupFactor(frame.variables[1]);
|
|
70
|
+
if (group.levels.length !== 2)
|
|
71
|
+
throw new Error("grouping factor must have exactly 2 levels");
|
|
72
|
+
return {
|
|
73
|
+
kind: "two",
|
|
74
|
+
x: values.filter((_, i) => group.codes[i] === 0),
|
|
75
|
+
y: values.filter((_, i) => group.codes[i] === 1),
|
|
76
|
+
levels: [group.levels[0], group.levels[1]],
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** The two samples of a formula `y ~ g` whose group has two levels (var.test, ansari.test, mood.test, ks.test) */
|
|
80
|
+
export function twoGroupData(formula, data) {
|
|
81
|
+
const d = twoSampleData(formula, data, false);
|
|
82
|
+
if (d.kind !== "two")
|
|
83
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
84
|
+
return d;
|
|
85
|
+
}
|
|
86
|
+
/** The response and group of a formula `y ~ g` with one variable on the right (kruskal.test, fligner.test, bartlett.test) */
|
|
87
|
+
export function kSampleData(formula, data) {
|
|
88
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
89
|
+
if (terms.response < 0)
|
|
90
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
91
|
+
if (rhsVariables.length !== 1)
|
|
92
|
+
throw new Error("'formula' should be of the form response ~ group");
|
|
93
|
+
const response = terms.variables[0];
|
|
94
|
+
const frame = modelFrame(data, [response, rhsVariables[0]], { dropUnusedLevels: true });
|
|
95
|
+
return { values: numericValues(frame.variables[0], response), group: groupFactor(frame.variables[1]) };
|
|
96
|
+
}
|
|
97
|
+
/** The samples of each level, in the order of the levels */
|
|
98
|
+
export function samplesByLevel(values, group) {
|
|
99
|
+
const out = group.levels.map(() => []);
|
|
100
|
+
values.forEach((v, i) => out[group.codes[i]].push(v));
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The response and groups of oneway.test's formula `y ~ A` or `y ~ A + B + ...`: with several factors the groups are
|
|
105
|
+
* the combinations present in the data, numbered with the first factor varying fastest (R's interaction()).
|
|
106
|
+
*/
|
|
107
|
+
export function onewayData(formula, data) {
|
|
108
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
109
|
+
if (terms.response < 0 || rhsVariables.length === 0)
|
|
110
|
+
throw new Error(MISSING_OR_INCORRECT);
|
|
111
|
+
const response = terms.variables[0];
|
|
112
|
+
const frame = modelFrame(data, [response, ...rhsVariables], { dropUnusedLevels: true });
|
|
113
|
+
const factors = frame.variables.slice(1).map(groupFactor);
|
|
114
|
+
const groups = frame.rows.map((_, i) => {
|
|
115
|
+
let code = 0;
|
|
116
|
+
let stride = 1;
|
|
117
|
+
for (const f of factors) {
|
|
118
|
+
code += f.codes[i] * stride;
|
|
119
|
+
stride *= f.levels.length;
|
|
120
|
+
}
|
|
121
|
+
return code;
|
|
122
|
+
});
|
|
123
|
+
return { values: numericValues(frame.variables[0], response), groups };
|
|
124
|
+
}
|
|
125
|
+
const INCORRECT_BLOCKS = "incorrect specification for 'formula'";
|
|
126
|
+
/**
|
|
127
|
+
* The matrix (one row per block, one column per group, levels in order) of a blocked design's formula `y ~ w | b`
|
|
128
|
+
* (friedman.test, quade.test). Rows with a missing value are dropped first; every combination of group and block must
|
|
129
|
+
* then occur exactly once.
|
|
130
|
+
*/
|
|
131
|
+
export function blockData(formula, data) {
|
|
132
|
+
const sides = splitTilde(formula);
|
|
133
|
+
const parts = sides?.rhs.split("|");
|
|
134
|
+
if (sides === undefined || sides.lhs === "" || parts.length !== 2)
|
|
135
|
+
throw new Error(INCORRECT_BLOCKS);
|
|
136
|
+
const one = (text) => {
|
|
137
|
+
const { terms, rhsVariables } = plainTerms(text, data);
|
|
138
|
+
if (rhsVariables.length !== 1 || terms.terms.length !== 1)
|
|
139
|
+
throw new Error(INCORRECT_BLOCKS);
|
|
140
|
+
return rhsVariables[0];
|
|
141
|
+
};
|
|
142
|
+
const { terms } = plainTerms(`${sides.lhs} ~ 1`, data);
|
|
143
|
+
const response = terms.variables[0];
|
|
144
|
+
const groupName = one(`~${parts[0]}`);
|
|
145
|
+
const blockName = one(`~${parts[1]}`);
|
|
146
|
+
const frame = modelFrame(data, [response, groupName, blockName], { dropUnusedLevels: true });
|
|
147
|
+
const values = numericValues(frame.variables[0], response);
|
|
148
|
+
const group = groupFactor(frame.variables[1]);
|
|
149
|
+
const block = groupFactor(frame.variables[2]);
|
|
150
|
+
const matrix = block.levels.map(() => group.levels.map(() => NaN));
|
|
151
|
+
const counts = block.levels.map(() => group.levels.map(() => 0));
|
|
152
|
+
values.forEach((v, i) => {
|
|
153
|
+
const b = block.codes[i];
|
|
154
|
+
const w = group.codes[i];
|
|
155
|
+
counts[b][w]++;
|
|
156
|
+
matrix[b][w] = v;
|
|
157
|
+
});
|
|
158
|
+
if (counts.some((row) => row.some((c) => c !== 1)))
|
|
159
|
+
throw new Error("not an unreplicated complete block design");
|
|
160
|
+
return matrix;
|
|
161
|
+
}
|
|
162
|
+
/** The two variables of cor.test's one-sided formula `~ x + z` */
|
|
163
|
+
export function correlationData(formula, data) {
|
|
164
|
+
const { terms, rhsVariables } = plainTerms(formula, data);
|
|
165
|
+
if (terms.response >= 0)
|
|
166
|
+
throw new Error("'formula' missing or invalid");
|
|
167
|
+
if (rhsVariables.length !== 2)
|
|
168
|
+
throw new Error("invalid formula");
|
|
169
|
+
const frame = modelFrame(data, rhsVariables);
|
|
170
|
+
return { x: numericValues(frame.variables[0], rhsVariables[0]), y: numericValues(frame.variables[1], rhsVariables[1]) };
|
|
171
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { GroupLabel } from "./groups.js";
|
|
2
|
+
import type { HTest, Sample } from "./types.js";
|
|
3
|
+
/** Result of {@link propTrendTest}: no `alternative`, as R returns none. */
|
|
4
|
+
export interface PropTrendTestResult extends Omit<HTest, "alternative"> {
|
|
5
|
+
/** Degrees of freedom (1). */
|
|
6
|
+
parameter: number;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Chi-squared test for trend in proportions. Equivalent to R's `prop.trend.test(x, n, score)`.
|
|
10
|
+
*
|
|
11
|
+
* @param x Numbers of events.
|
|
12
|
+
* @param n Numbers of trials.
|
|
13
|
+
* @param score Scores of the groups. Default `1, 2, ..., k`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* propTrendTest([15, 10, 8, 4], [30, 30, 30, 30]).statistic; // 9.573428850537288
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function propTrendTest(x: Sample, n: Sample, score?: Sample): PropTrendTestResult;
|
|
21
|
+
/** Alternative of {@link jonckheereTest}. */
|
|
22
|
+
export type JonckheereAlternative = "two.sided" | "increasing" | "decreasing";
|
|
23
|
+
/** Options for {@link jonckheereTest}. */
|
|
24
|
+
export interface JonckheereTestOptions {
|
|
25
|
+
/** `"two.sided"` (default), `"increasing"` or `"decreasing"` (unambiguous abbreviations accepted). */
|
|
26
|
+
alternative?: JonckheereAlternative | (string & {});
|
|
27
|
+
/**
|
|
28
|
+
* The exact p-value (`"DescTools"` only). Default: exact when there are no ties and at most 100 observations. With
|
|
29
|
+
* ties the normal approximation is used anyway, with a warning.
|
|
30
|
+
*/
|
|
31
|
+
exact?: boolean;
|
|
32
|
+
/** `"DescTools"` (default: `JonckheereTerpstraTest`) or `"clinfun"` (`jonckheere.test`). */
|
|
33
|
+
variant?: "DescTools" | "clinfun";
|
|
34
|
+
/** The order of the groups. Default: the labels sorted (numbers as numbers, strings by code point). */
|
|
35
|
+
levels?: readonly GroupLabel[];
|
|
36
|
+
}
|
|
37
|
+
/** Result of {@link jonckheereTest}. */
|
|
38
|
+
export interface JonckheereTestResult extends Omit<HTest, "alternative"> {
|
|
39
|
+
alternative: JonckheereAlternative;
|
|
40
|
+
/** The packages' warnings (normal approximation). */
|
|
41
|
+
warnings: string[];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Jonckheere-Terpstra test of equal distributions against ordered alternatives. Equivalent to DescTools'
|
|
45
|
+
* `JonckheereTerpstraTest(x, g, alternative, exact)` (with `g` an ordered factor) or, with `variant: "clinfun"`, to
|
|
46
|
+
* clinfun's `jonckheere.test(x, g, alternative)` (whose two-sided p-value is `2 min(p_increasing, p_decreasing,
|
|
47
|
+
* 0.5)`; DescTools caps it at 1 instead of 0.5). Pairs with a missing value are dropped.
|
|
48
|
+
*
|
|
49
|
+
* @param x The values.
|
|
50
|
+
* @param g Group labels, parallel to `x`, in the order of the alternative.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* jonckheereTest([1.1, 2.0, 2.4, 3.5, 3.1, 4.6], [1, 1, 2, 2, 3, 3], { alternative: "increasing" }).statistic; // 11
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function jonckheereTest(x: Sample, g: readonly GroupLabel[], options?: JonckheereTestOptions): JonckheereTestResult;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// Tests for a trend: the chi-squared test for trend in proportions (R's prop.trend.test) and the Jonckheere-Terpstra
|
|
2
|
+
// test for ordered alternatives (DescTools' JonckheereTerpstraTest and clinfun's jonckheere.test).
|
|
3
|
+
//
|
|
4
|
+
// Written for rStats.js from oracle/rm-tests-behaviour.md (no ported code; the source of R, DescTools and clinfun was
|
|
5
|
+
// not read):
|
|
6
|
+
// - prop.trend.test: the weighted regression sum of squares of the proportions on the scores, with weights
|
|
7
|
+
// n_i / (p (1 - p)), chi-squared with 1 degree of freedom.
|
|
8
|
+
// - Jonckheere-Terpstra: JT is the sum over ordered pairs of groups of the Mann-Whitney counts (ties count 1/2). Its
|
|
9
|
+
// exact null distribution is the convolution of the Mann-Whitney distributions of each group against all later
|
|
10
|
+
// groups; the normal approximation uses mean sum n_i m_i / 2 and variance sum n_i m_i (n_i + m_i + 1) / 12 (m_i the
|
|
11
|
+
// number of observations after group i; no tie correction).
|
|
12
|
+
import { pgamma1 } from "../distributions/gamma.js";
|
|
13
|
+
import { pnorm } from "../distributions/normal.js";
|
|
14
|
+
import { dwilcox1 } from "../distributions/wilcox.js";
|
|
15
|
+
import { compareCodePoints } from "../models/modelFrame.js";
|
|
16
|
+
import { matchChoice } from "./power.js";
|
|
17
|
+
/**
|
|
18
|
+
* Chi-squared test for trend in proportions. Equivalent to R's `prop.trend.test(x, n, score)`.
|
|
19
|
+
*
|
|
20
|
+
* @param x Numbers of events.
|
|
21
|
+
* @param n Numbers of trials.
|
|
22
|
+
* @param score Scores of the groups. Default `1, 2, ..., k`.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* propTrendTest([15, 10, 8, 4], [30, 30, 30, 30]).statistic; // 9.573428850537288
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function propTrendTest(x, n, score) {
|
|
30
|
+
const xs = Array.from(x);
|
|
31
|
+
const ns = Array.from(n);
|
|
32
|
+
const k = xs.length;
|
|
33
|
+
const s = score === undefined ? xs.map((_, i) => i + 1) : Array.from(score);
|
|
34
|
+
if (ns.length !== k || s.length !== k)
|
|
35
|
+
throw new RangeError("'x', 'n' and 'score' must have the same length");
|
|
36
|
+
const p = xs.reduce((a, v) => a + v, 0) / ns.reduce((a, v) => a + v, 0);
|
|
37
|
+
const w = ns.map((v) => v / (p * (1 - p)));
|
|
38
|
+
const f = xs.map((v, i) => v / ns[i]);
|
|
39
|
+
const sw = w.reduce((a, v) => a + v, 0);
|
|
40
|
+
const sBar = w.reduce((a, wi, i) => a + wi * s[i], 0) / sw;
|
|
41
|
+
const fBar = w.reduce((a, wi, i) => a + wi * f[i], 0) / sw;
|
|
42
|
+
let sxy = 0;
|
|
43
|
+
let sxx = 0;
|
|
44
|
+
w.forEach((wi, i) => {
|
|
45
|
+
const ds = s[i] - sBar;
|
|
46
|
+
sxy += wi * ds * (f[i] - fBar);
|
|
47
|
+
sxx += wi * ds * ds;
|
|
48
|
+
});
|
|
49
|
+
const statistic = (sxy * sxy) / sxx;
|
|
50
|
+
return {
|
|
51
|
+
method: "Chi-squared Test for Trend in Proportions",
|
|
52
|
+
statistic,
|
|
53
|
+
statisticName: "X-squared",
|
|
54
|
+
parameter: 1,
|
|
55
|
+
parameterName: "df",
|
|
56
|
+
pValue: pgamma1(statistic, 0.5, 2, false, false),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
const NOT_EXACT = "Sample size > 100 or data with ties \n p-value based on normal approximation. Specify nperm for permutation p-value";
|
|
60
|
+
const TIES = "Sample data with ties \n p-value based on normal approximation. Specify nperm for permutation p-value.";
|
|
61
|
+
/** The groups in order: the labels sorted (numbers as numbers, strings by code point) or as `levels` */
|
|
62
|
+
function orderedGroups(x, g, levels) {
|
|
63
|
+
const xs = Array.from(x);
|
|
64
|
+
if (xs.length !== g.length)
|
|
65
|
+
throw new RangeError("'x' and 'g' must have the same length");
|
|
66
|
+
const keep = xs.flatMap((v, i) => (Number.isNaN(v) || (typeof g[i] === "number" && Number.isNaN(g[i])) ? [] : [i]));
|
|
67
|
+
let order;
|
|
68
|
+
if (levels !== undefined)
|
|
69
|
+
order = levels.map(String);
|
|
70
|
+
else {
|
|
71
|
+
const distinct = [...new Set(keep.map((i) => g[i]))];
|
|
72
|
+
order = distinct.every((l) => typeof l === "number")
|
|
73
|
+
? distinct.sort((a, b) => a - b).map(String)
|
|
74
|
+
: [...new Set(distinct.map(String))].sort(compareCodePoints);
|
|
75
|
+
}
|
|
76
|
+
const groups = order.map(() => []);
|
|
77
|
+
const index = new Map(order.map((l, k) => [l, k]));
|
|
78
|
+
for (const i of keep) {
|
|
79
|
+
const k = index.get(String(g[i]));
|
|
80
|
+
if (k === undefined)
|
|
81
|
+
throw new RangeError(`the group '${String(g[i])}' is not among 'levels'`);
|
|
82
|
+
groups[k].push(xs[i]);
|
|
83
|
+
}
|
|
84
|
+
return groups;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Jonckheere-Terpstra test of equal distributions against ordered alternatives. Equivalent to DescTools'
|
|
88
|
+
* `JonckheereTerpstraTest(x, g, alternative, exact)` (with `g` an ordered factor) or, with `variant: "clinfun"`, to
|
|
89
|
+
* clinfun's `jonckheere.test(x, g, alternative)` (whose two-sided p-value is `2 min(p_increasing, p_decreasing,
|
|
90
|
+
* 0.5)`; DescTools caps it at 1 instead of 0.5). Pairs with a missing value are dropped.
|
|
91
|
+
*
|
|
92
|
+
* @param x The values.
|
|
93
|
+
* @param g Group labels, parallel to `x`, in the order of the alternative.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* jonckheereTest([1.1, 2.0, 2.4, 3.5, 3.1, 4.6], [1, 1, 2, 2, 3, 3], { alternative: "increasing" }).statistic; // 11
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export function jonckheereTest(x, g, options = {}) {
|
|
101
|
+
const alternative = matchChoice(options.alternative, ["two.sided", "increasing", "decreasing"]);
|
|
102
|
+
const variant = options.variant ?? "DescTools";
|
|
103
|
+
if (variant !== "DescTools" && variant !== "clinfun")
|
|
104
|
+
throw new RangeError(`invalid variant "${String(variant)}"`);
|
|
105
|
+
if (variant === "clinfun" && options.exact !== undefined)
|
|
106
|
+
throw new TypeError("clinfun's jonckheere.test has no option 'exact'");
|
|
107
|
+
const groups = orderedGroups(x, g, options.levels);
|
|
108
|
+
const all = groups.flat();
|
|
109
|
+
const nTotal = all.length;
|
|
110
|
+
const ties = new Set(all).size < nTotal;
|
|
111
|
+
// JT, and the sizes n_i and m_i (observations after group i)
|
|
112
|
+
let jt = 0;
|
|
113
|
+
const sizes = [];
|
|
114
|
+
let after = nTotal;
|
|
115
|
+
groups.forEach((gi, i) => {
|
|
116
|
+
after -= gi.length;
|
|
117
|
+
sizes.push([gi.length, after]);
|
|
118
|
+
for (let j = i + 1; j < groups.length; j++) {
|
|
119
|
+
for (const a of gi)
|
|
120
|
+
for (const b of groups[j])
|
|
121
|
+
jt += a < b ? 1 : a === b ? 0.5 : 0;
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
const warnings = [];
|
|
125
|
+
let exact = !ties && nTotal <= 100;
|
|
126
|
+
if (variant === "DescTools" && options.exact !== undefined) {
|
|
127
|
+
exact = options.exact;
|
|
128
|
+
if (exact && ties) {
|
|
129
|
+
exact = false;
|
|
130
|
+
warnings.push(TIES);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (!exact)
|
|
134
|
+
warnings.push(NOT_EXACT);
|
|
135
|
+
let lower;
|
|
136
|
+
let upper;
|
|
137
|
+
if (exact) {
|
|
138
|
+
// the convolution of the Mann-Whitney distributions
|
|
139
|
+
let dist = [1];
|
|
140
|
+
for (const [ni, mi] of sizes) {
|
|
141
|
+
if (ni === 0 || mi === 0)
|
|
142
|
+
continue;
|
|
143
|
+
const mw = Array.from({ length: ni * mi + 1 }, (_, v) => dwilcox1(v, ni, mi, false));
|
|
144
|
+
const next = new Array(dist.length + mw.length - 1).fill(0);
|
|
145
|
+
dist.forEach((a, u) => mw.forEach((b, v) => (next[u + v] += a * b)));
|
|
146
|
+
dist = next;
|
|
147
|
+
}
|
|
148
|
+
lower = 0;
|
|
149
|
+
upper = 0;
|
|
150
|
+
dist.forEach((pr, v) => {
|
|
151
|
+
if (v <= jt)
|
|
152
|
+
lower += pr;
|
|
153
|
+
if (v >= jt)
|
|
154
|
+
upper += pr;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
const mean = sizes.reduce((a, [ni, mi]) => a + ni * mi, 0) / 2;
|
|
159
|
+
const variance = sizes.reduce((a, [ni, mi]) => a + (ni * mi * (ni + mi + 1)) / 12, 0);
|
|
160
|
+
const z = (jt - mean) / Math.sqrt(variance);
|
|
161
|
+
lower = pnorm(z);
|
|
162
|
+
upper = 1 - lower;
|
|
163
|
+
}
|
|
164
|
+
const cap = variant === "clinfun" ? 0.5 : 1;
|
|
165
|
+
const pValue = alternative === "increasing" ? upper : alternative === "decreasing" ? lower : 2 * Math.min(upper, lower, cap);
|
|
166
|
+
return { method: "Jonckheere-Terpstra test", alternative, statistic: jt, statisticName: "JT", pValue, warnings };
|
|
167
|
+
}
|
package/dist/htest/varTest.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 varTest}. */
|
|
3
4
|
export interface VarTestOptions {
|
|
@@ -27,8 +28,19 @@ export interface VarTestResult extends Omit<HTest, "parameter" | "parameterName"
|
|
|
27
28
|
* ```ts
|
|
28
29
|
* varTest([1, 3, 2, 5, 4], [2, 9, 4, 12, 7, 1]).statistic; // 0.2307692
|
|
29
30
|
* ```
|
|
31
|
+
*
|
|
32
|
+
* There is also a formula form, `varTest("y ~ g", data, options)` (next overload).
|
|
30
33
|
*/
|
|
31
34
|
export declare function varTest(x: Sample, y: Sample, options?: VarTestOptions): VarTestResult;
|
|
35
|
+
/**
|
|
36
|
+
* F test of two variances from a formula and a data frame. Equivalent to R's `var.test(formula, data)`: `"y ~ g"`
|
|
37
|
+
* compares the two levels of `g` (the first level is `x`). Only the variables of the formula count: the rows where
|
|
38
|
+
* 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
|
+
*/
|
|
43
|
+
export declare function varTest(formula: string, data: DataFrame, options?: VarTestOptions): VarTestResult;
|
|
32
44
|
/** Result of {@link bartlettTest}. Has no `alternative`, as R's `bartlett.test()` returns none. */
|
|
33
45
|
export interface BartlettTestResult extends Omit<HTest, "alternative"> {
|
|
34
46
|
/** Degrees of freedom (`parameterName` is `"df"`). */
|
|
@@ -44,5 +56,16 @@ export interface BartlettTestResult extends Omit<HTest, "alternative"> {
|
|
|
44
56
|
* ```ts
|
|
45
57
|
* bartlettTest([[1, 3, 2, 5], [2, 9, 4, 12, 7], [3, 3.5, 4, 2]]).statistic;
|
|
46
58
|
* ```
|
|
59
|
+
*
|
|
60
|
+
* There is also a formula form, `bartlettTest("y ~ g", data)` (next overload).
|
|
47
61
|
*/
|
|
48
62
|
export declare function bartlettTest(groups: readonly Sample[]): BartlettTestResult;
|
|
63
|
+
/**
|
|
64
|
+
* Bartlett's test from a formula and a data frame. Equivalent to R's `bartlett.test(formula, data)`: `"y ~ g"`, one
|
|
65
|
+
* group per level of `g`, in the order of the levels. Only the variables of the formula count: the rows where one of
|
|
66
|
+
* them is missing are dropped.
|
|
67
|
+
*
|
|
68
|
+
* @param formula `"y ~ g"` (one variable on each side).
|
|
69
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
70
|
+
*/
|
|
71
|
+
export declare function bartlettTest(formula: string, data: DataFrame): BartlettTestResult;
|
package/dist/htest/varTest.js
CHANGED
|
@@ -8,17 +8,13 @@
|
|
|
8
8
|
import { pf1, qf1 } from "../distributions/f.js";
|
|
9
9
|
import { pgamma1 } from "../distributions/gamma.js";
|
|
10
10
|
import { variance } from "../internal/summary.js";
|
|
11
|
+
import { kSampleData, samplesByLevel, twoGroupData } from "./testFormula.js";
|
|
11
12
|
import { checkConfLevelOpen, matchAlternative } from "./types.js";
|
|
12
|
-
/**
|
|
13
|
-
* F test that the variances of two normal samples have a given ratio. Equivalent to R's `var.test()`. `NaN` values
|
|
14
|
-
* are dropped.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* varTest([1, 3, 2, 5, 4], [2, 9, 4, 12, 7, 1]).statistic; // 0.2307692
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
13
|
export function varTest(x, y, options = {}) {
|
|
14
|
+
if (typeof x === "string") {
|
|
15
|
+
const d = twoGroupData(x, y);
|
|
16
|
+
return varTest(d.x, d.y, options);
|
|
17
|
+
}
|
|
22
18
|
// as in R, infinite values are dropped along with NaN
|
|
23
19
|
const xs = Array.from(x).filter(Number.isFinite);
|
|
24
20
|
const ys = Array.from(y).filter(Number.isFinite);
|
|
@@ -69,18 +65,12 @@ export function varTest(x, y, options = {}) {
|
|
|
69
65
|
nullValueName: "ratio of variances",
|
|
70
66
|
};
|
|
71
67
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
* @example
|
|
79
|
-
* ```ts
|
|
80
|
-
* bartlettTest([[1, 3, 2, 5], [2, 9, 4, 12, 7], [3, 3.5, 4, 2]]).statistic;
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export function bartlettTest(groups) {
|
|
68
|
+
export function bartlettTest(x, data) {
|
|
69
|
+
if (typeof x === "string") {
|
|
70
|
+
const d = kSampleData(x, data);
|
|
71
|
+
return bartlettTest(samplesByLevel(d.values, d.group));
|
|
72
|
+
}
|
|
73
|
+
const groups = x;
|
|
84
74
|
const k = groups.length;
|
|
85
75
|
if (k < 2)
|
|
86
76
|
throw new RangeError("'x' must be a list with at least 2 elements");
|
|
@@ -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 wilcoxTest}. */
|
|
3
4
|
export interface WilcoxTestOptions {
|
|
@@ -57,5 +58,18 @@ export interface WilcoxTestResult extends HTest {
|
|
|
57
58
|
* r.statistic; // 40 (V)
|
|
58
59
|
* r.pValue; // 0.01953125
|
|
59
60
|
* ```
|
|
61
|
+
*
|
|
62
|
+
* There is also a formula form, `wilcoxTest("y ~ g", data, options)` (next overload).
|
|
60
63
|
*/
|
|
61
64
|
export declare function wilcoxTest(x: Sample, y?: Sample | WilcoxTestOptions, options?: WilcoxTestOptions): WilcoxTestResult;
|
|
65
|
+
/**
|
|
66
|
+
* Wilcoxon tests from a formula and a data frame. Equivalent to R's `wilcox.test(formula, data)`: `"y ~ g"` is the
|
|
67
|
+
* rank sum test of the two levels of `g` (the first level is `x`), `"y ~ 1"` the signed rank test and
|
|
68
|
+
* `"Pair(a, b) ~ 1"` the paired test of `a` and `b` (the option `paired` is not allowed). Only the variables of the
|
|
69
|
+
* formula count: the rows where one of them is missing are dropped.
|
|
70
|
+
*
|
|
71
|
+
* @param formula `"y ~ g"`, `"y ~ 1"` or `"Pair(a, b) ~ 1"`.
|
|
72
|
+
* @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
|
|
73
|
+
* @param options See {@link WilcoxTestOptions}.
|
|
74
|
+
*/
|
|
75
|
+
export declare function wilcoxTest(formula: string, data: DataFrame, options?: WilcoxTestOptions): WilcoxTestResult;
|