@numero-uno-jpn/rstats 1.1.0 → 1.2.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 +61 -0
- package/README.ja.md +11 -10
- package/README.md +11 -10
- package/dist/chunks/{chunk-X4J6UJHN.js → chunk-2NNBUYHJ.js} +2 -2
- package/dist/chunks/{chunk-Y4YWLSWU.js → chunk-47IIPUFN.js} +2 -2
- package/dist/chunks/{chunk-7KJ24LYK.js → chunk-4AOTAT3O.js} +2 -2
- package/dist/chunks/{chunk-GRUQRNX7.js → chunk-5SGTWSCB.js} +3 -3
- package/dist/chunks/{chunk-YN5MLKDL.js → chunk-6FZHTD5G.js} +2 -2
- package/dist/chunks/{chunk-N5BDSMXS.js → chunk-73LNRWU3.js} +1 -1
- package/dist/chunks/{chunk-7IJCUSA5.js → chunk-7INBXTVV.js} +2 -2
- package/dist/chunks/chunk-7WG7X2UI.js +2 -0
- package/dist/chunks/{chunk-DTJG2IL2.js → chunk-A66M3ZEA.js} +2 -2
- package/dist/chunks/{chunk-EJYCDE4Z.js → chunk-BI7NYJWP.js} +1 -1
- package/dist/chunks/{chunk-JNWWLNE3.js → chunk-BKV3FINA.js} +2 -2
- package/dist/chunks/{chunk-UMLMY4XS.js → chunk-CQB4BJBR.js} +2 -2
- package/dist/chunks/{chunk-DR4GQ7PW.js → chunk-EEU7D4RU.js} +1 -1
- package/dist/chunks/{chunk-Z2HGLFZN.js → chunk-F3EQLL4F.js} +2 -2
- package/dist/chunks/{chunk-4VRMAYPY.js → chunk-HBHMEPGY.js} +2 -2
- package/dist/chunks/{chunk-GJEKNJP7.js → chunk-IAWXF73N.js} +2 -2
- package/dist/chunks/{chunk-OG7UORIX.js → chunk-IMPRIQE5.js} +1 -1
- package/dist/chunks/{chunk-GYGQUYEP.js → chunk-JWBSEL3V.js} +2 -2
- package/dist/chunks/{chunk-U7BRXOUS.js → chunk-L23RSC2N.js} +2 -2
- package/dist/chunks/{chunk-QUTJEYDX.js → chunk-NSU6U3ET.js} +2 -2
- package/dist/chunks/{chunk-4KCSBTR5.js → chunk-ORE5ZRCY.js} +2 -2
- package/dist/chunks/{chunk-GGJA3JRO.js → chunk-RRO4LMCB.js} +2 -2
- package/dist/chunks/{chunk-HZLSNJQM.js → chunk-T5DRIITM.js} +2 -2
- package/dist/chunks/{chunk-HUWOSLKE.js → chunk-TNETWBF5.js} +1 -1
- package/dist/chunks/{chunk-HRA7PCQT.js → chunk-VERBXW6E.js} +2 -2
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/tests.d.ts +5 -0
- package/dist/entries/tests.js +5 -0
- package/dist/htest/binomCI.d.ts +116 -0
- package/dist/htest/binomCI.js +252 -0
- package/dist/htest/boschlooTest.d.ts +65 -0
- package/dist/htest/boschlooTest.js +394 -0
- package/dist/htest/chisqTest.d.ts +19 -0
- package/dist/htest/chisqTest.js +30 -2
- package/dist/htest/core/twoProportions.d.ts +48 -0
- package/dist/htest/core/twoProportions.js +476 -0
- package/dist/htest/equivalence.d.ts +172 -0
- package/dist/htest/equivalence.js +189 -0
- package/dist/htest/fisherTest.d.ts +49 -0
- package/dist/htest/fisherTest.js +136 -13
- package/dist/htest/tableTests.d.ts +216 -0
- package/dist/htest/tableTests.js +650 -0
- package/dist/htest/twoByTwo.d.ts +203 -0
- package/dist/htest/twoByTwo.js +385 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/rstats.js +16 -16
- package/dist/special/igam.js +140 -23
- package/dist/special/loader.js +8 -0
- package/dist/special.js +2 -2
- package/dist/tests.js +13 -13
- package/llms.txt +8 -0
- package/package.json +1 -1
- package/dist/chunks/chunk-NDTJ23A6.js +0 -2
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
// Confidence intervals for one binomial proportion (DescTools' BinomCI), for one case per call.
|
|
2
|
+
//
|
|
3
|
+
// Written for rStats.js from the published intervals and DescTools' documented behaviour (no R or package code): the
|
|
4
|
+
// level of a one-sided interval ("left": [lower, 1], "right": [0, upper]) is that of the two-sided interval at
|
|
5
|
+
// 1 - 2(1 - confLevel); kappa = qnorm(1 - alpha/2).
|
|
6
|
+
// - wald: p ∓ kappa sqrt(p q / n)
|
|
7
|
+
// - wilson (Wilson, JASA 22, 1927): (x + kappa^2/2)/(n + kappa^2) ∓ kappa sqrt(n)/(n + kappa^2) sqrt(p q + kappa^2/(4n))
|
|
8
|
+
// - wilsoncc: Wilson's interval with continuity correction (Newcombe, Statistics in Medicine 17, 1998, method 4)
|
|
9
|
+
// - agresti-coull (Agresti and Coull, The American Statistician 52, 1998): the Wald interval of
|
|
10
|
+
// (x + kappa^2/2)/(n + kappa^2) with n + kappa^2 trials
|
|
11
|
+
// - jeffreys: the equal-tailed interval of the Beta(x + 1/2, n - x + 1/2) posterior (Brown, Cai and DasGupta,
|
|
12
|
+
// Statistical Science 16, 2001), 0 at x = 0 and 1 at x = n
|
|
13
|
+
// - clopper-pearson (Clopper and Pearson, Biometrika 26, 1934): beta quantiles of the exact binomial tails
|
|
14
|
+
// - midp: the p with 1/2 P(X = x) + P(X > x) = alpha/2 (lower) and 1/2 P(X = x) + P(X < x) = alpha/2 (upper) (Lancaster,
|
|
15
|
+
// JASA 56, 1961; Agresti and Gottard, Statistical Science 20, 2005), solved on the log scale of p to the precision of
|
|
16
|
+
// the doubles; closed forms alpha^(1/n) and 1 - alpha^(1/n) at x = n and x = 0. DescTools solves with uniroot's
|
|
17
|
+
// tolerance 1.22e-4 (absolute), which leaves its limits 1e-6 to 1e-4 off and those below 1e-4 grossly wrong.
|
|
18
|
+
// Limits are clipped to [0, 1]. Counts must be integers with 0 <= x <= n, n > 0; the level must lie in (0, 1) and,
|
|
19
|
+
// one-sided, give a two-sided level 1 - 2(1 - confLevel) >= 0.
|
|
20
|
+
import { dbinom1, pbinom1 } from "../distributions/binomial.js";
|
|
21
|
+
import { qbeta1 } from "../distributions/beta.js";
|
|
22
|
+
import { qchisq } from "../distributions/chisq.js";
|
|
23
|
+
import { qnorm } from "../distributions/normal.js";
|
|
24
|
+
import { uniroot } from "../internal/uniroot.js";
|
|
25
|
+
import { reported } from "../internal/warnings.js";
|
|
26
|
+
import { matchChoice } from "./power.js";
|
|
27
|
+
import { isCount } from "./types.js";
|
|
28
|
+
// Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
|
|
29
|
+
import { checkOptions } from "../internal/checkOptions.js";
|
|
30
|
+
const BINOM_CI_KEYS = ["method", "confLevel", "sides"];
|
|
31
|
+
const BINOM_CI_METHODS = ["wilson", "wald", "wilsoncc", "agresti-coull", "jeffreys", "clopper-pearson", "midp"];
|
|
32
|
+
const SIDES = ["two.sided", "left", "right"];
|
|
33
|
+
/** Checks the level and sides, and the two-sided level that a one-sided limit is computed at (as DescTools) */
|
|
34
|
+
export function intervalLevel(confLevel, sides) {
|
|
35
|
+
const s = matchChoice(sides, SIDES);
|
|
36
|
+
const c = confLevel ?? 0.95;
|
|
37
|
+
if (!(typeof c === "number" && c > 0 && c < 1))
|
|
38
|
+
throw new RangeError("'confLevel' must be a single number in (0, 1)");
|
|
39
|
+
const level = s === "two.sided" ? c : 1 - 2 * (1 - c);
|
|
40
|
+
if (!(level >= 0))
|
|
41
|
+
throw new RangeError("a one-sided 'confLevel' must be at least 0.5");
|
|
42
|
+
// one-sided with alpha/2 below 1e-8 (rule 10), alpha from c (1 - c is exact for c >= 1/2), not from the rounded
|
|
43
|
+
// level; otherwise 1 - level as DescTools
|
|
44
|
+
const alpha = s !== "two.sided" && 1 - c < 1e-8 ? 2 * (1 - c) : 1 - level;
|
|
45
|
+
return { ...levelOfAlpha(alpha, level), sides: s };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The two-sided interval level of a given alpha, for callers that hold alpha itself (a test at 1 - 2 alpha: alpha
|
|
49
|
+
* taken back as 1 - level is about eps / alpha relative off). `level` is 1 - alpha unless given
|
|
50
|
+
*/
|
|
51
|
+
export function levelOfAlpha(alpha, level = 1 - alpha) {
|
|
52
|
+
// (rule 10: for alpha/2 below 1e-8 the upper tails themselves, as 1 - alpha/2 and the level have lost their digits)
|
|
53
|
+
const small = alpha / 2 < 1e-8;
|
|
54
|
+
const kappa = small ? qnorm(alpha / 2, { lowerTail: false }) : qnorm(1 - alpha / 2);
|
|
55
|
+
const chisq = small ? qchisq(alpha, 1, { lowerTail: false }) : qchisq(level, 1);
|
|
56
|
+
return { sides: "two.sided", level, alpha, kappa, chisq };
|
|
57
|
+
}
|
|
58
|
+
/** A count: a nonnegative integer (within the slack of isCount), returned rounded */
|
|
59
|
+
export function checkCount(v, name) {
|
|
60
|
+
if (typeof v !== "number")
|
|
61
|
+
throw new TypeError(`'${name}' must be a number`);
|
|
62
|
+
if (!isCount(v))
|
|
63
|
+
throw new RangeError(`'${name}' must be a nonnegative integer`);
|
|
64
|
+
// (+ 0 turns -0 into 0, which would otherwise reach an estimate or a limit as -0)
|
|
65
|
+
return Math.round(v) + 0;
|
|
66
|
+
}
|
|
67
|
+
/** Successes and trials: integers with 0 <= x <= n and n > 0 */
|
|
68
|
+
export function checkTrials(x, n, xName, nName) {
|
|
69
|
+
const xs = checkCount(x, xName);
|
|
70
|
+
const ns = checkCount(n, nName);
|
|
71
|
+
if (ns === 0)
|
|
72
|
+
throw new RangeError(`'${nName}' must be positive`);
|
|
73
|
+
if (ns > 2 ** 53)
|
|
74
|
+
throw new RangeError(`'${nName}' must be at most 2^53`);
|
|
75
|
+
if (xs > ns)
|
|
76
|
+
throw new RangeError(`'${xName}' must not be greater than '${nName}'`);
|
|
77
|
+
return [xs, ns];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Wilson's score limits of x/n at kappa. At x = 0 the lower limit and at x = n the upper are exactly 0 and 1 (the
|
|
81
|
+
* formula gives them up to rounding: DescTools' upper limit of 10/10 at the 50% level is 0.99999999999999989).
|
|
82
|
+
* The limits are the roots of (n + kappa^2) p^2 - (2x + kappa^2) p + x^2/n = 0. Where centre - half cancels (more than
|
|
83
|
+
* half of the centre, small x or a level close to 1) the lower limit is taken from the product of the roots,
|
|
84
|
+
* x^2 / (n (n + kappa^2)), divided by the upper: centre - half loses digits in proportion to centre / lower (350 eps
|
|
85
|
+
* at 1/1e15 and the level 1 - 2^-53, 28 eps for wilsoncc at 1/1000 and 95%; DescTools has the same digits)
|
|
86
|
+
*/
|
|
87
|
+
export function wilsonLimits(x, n, kappa) {
|
|
88
|
+
const p = x / n;
|
|
89
|
+
const q = 1 - p;
|
|
90
|
+
const k2 = kappa * kappa;
|
|
91
|
+
const centre = (x + k2 / 2) / (n + k2);
|
|
92
|
+
const half = ((kappa * Math.sqrt(n)) / (n + k2)) * Math.sqrt(p * q + k2 / (4 * n));
|
|
93
|
+
const up = centre + half;
|
|
94
|
+
let lower = centre - half;
|
|
95
|
+
if (x === 0)
|
|
96
|
+
lower = 0;
|
|
97
|
+
else if (lower < centre / 2)
|
|
98
|
+
lower = (p * (x / (n + k2))) / up;
|
|
99
|
+
return [lower, x === n ? 1 : up];
|
|
100
|
+
}
|
|
101
|
+
/** qbeta at alpha/2 in the lower or the upper tail (from the upper tail itself where alpha/2 < 1e-8, rule 10) */
|
|
102
|
+
function betaLimit(alpha, a, b, upper) {
|
|
103
|
+
if (!upper)
|
|
104
|
+
return qbeta1(alpha / 2, a, b, true, false);
|
|
105
|
+
return alpha / 2 < 1e-8 ? qbeta1(alpha / 2, a, b, false, false) : qbeta1(1 - alpha / 2, a, b, true, false);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The mid-p limit of x/n (0 < x < n): for the lower limit the p with 1/2 P(X = x) + P(X > x) = alpha/2 (increasing in
|
|
109
|
+
* p), for the upper the p with 1/2 P(X = x) + P(X < x) = alpha/2 (decreasing). Solved in u = log p by Brent's method,
|
|
110
|
+
* from a bracket widened from the estimate x/n
|
|
111
|
+
*/
|
|
112
|
+
function midpLimit(x, n, alpha, upper) {
|
|
113
|
+
const half = alpha / 2;
|
|
114
|
+
const g = upper
|
|
115
|
+
? (u) => {
|
|
116
|
+
const p = Math.exp(u);
|
|
117
|
+
return 0.5 * dbinom1(x, n, p, false) + pbinom1(x - 1, n, p, true, false) - half;
|
|
118
|
+
}
|
|
119
|
+
: (u) => {
|
|
120
|
+
const p = Math.exp(u);
|
|
121
|
+
return 0.5 * dbinom1(x, n, p, false) + pbinom1(x, n, p, false, false) - half;
|
|
122
|
+
};
|
|
123
|
+
// g increases in u for the lower limit and decreases for the upper; sign s makes it increasing
|
|
124
|
+
const s = upper ? -1 : 1;
|
|
125
|
+
const u0 = Math.log(x / n);
|
|
126
|
+
const g0 = s * g(u0);
|
|
127
|
+
if (g0 === 0)
|
|
128
|
+
return x / n;
|
|
129
|
+
if (g0 > 0) {
|
|
130
|
+
// the root lies below u0: widen downwards
|
|
131
|
+
let hi = u0;
|
|
132
|
+
let gHi = g0;
|
|
133
|
+
let step = 1;
|
|
134
|
+
for (;;) {
|
|
135
|
+
const lo = u0 - step;
|
|
136
|
+
if (lo < -745)
|
|
137
|
+
return 0;
|
|
138
|
+
const gLo = s * g(lo);
|
|
139
|
+
if (gLo <= 0) {
|
|
140
|
+
if (gLo === 0)
|
|
141
|
+
return Math.exp(lo);
|
|
142
|
+
return Math.exp(uniroot((u) => s * g(u), lo, hi, { tol: 2 ** -60, fLower: gLo, fUpper: gHi }));
|
|
143
|
+
}
|
|
144
|
+
hi = lo;
|
|
145
|
+
gHi = gLo;
|
|
146
|
+
step *= 2;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// the root lies between u0 and 0 (p = 1, where s g > 0 for 0 < x < n)
|
|
150
|
+
const g1 = s * g(0);
|
|
151
|
+
if (g1 <= 0)
|
|
152
|
+
return 1;
|
|
153
|
+
return Math.exp(uniroot((u) => s * g(u), u0, 0, { tol: 2 ** -60, fLower: g0, fUpper: g1 }));
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Confidence interval for a binomial proportion x/n. Equivalent to `DescTools::BinomCI()` in R.
|
|
157
|
+
*
|
|
158
|
+
* Methods (`method`): `"wilson"` (the default, Wilson's score interval), `"wald"`, `"wilsoncc"` (with continuity
|
|
159
|
+
* correction), `"agresti-coull"`, `"jeffreys"`, `"clopper-pearson"` (the exact interval) and `"midp"` (the mid-p exact
|
|
160
|
+
* interval). With `sides: "left"` or `"right"` only the lower or the upper limit is kept, at the one-sided level
|
|
161
|
+
* `confLevel` (the two-sided interval at `1 - 2 (1 - confLevel)`), and the other end is 0 or 1. Limits are clipped to
|
|
162
|
+
* [0, 1].
|
|
163
|
+
*
|
|
164
|
+
* The mid-p limits are the roots of their equations to the precision of the doubles; DescTools stops its search at
|
|
165
|
+
* an absolute 1.22e-4, which leaves its limits up to 1e-4 off and gives 0 for lower limits below that. Unlike
|
|
166
|
+
* DescTools, which returns NaN or numbers, x > n, a negative or non-integer count, n = 0 and a level outside (0, 1)
|
|
167
|
+
* are errors.
|
|
168
|
+
*
|
|
169
|
+
* @param x Number of successes.
|
|
170
|
+
* @param n Number of trials.
|
|
171
|
+
* @param options See {@link BinomCIOptions}.
|
|
172
|
+
* @returns The estimate x/n and the interval; see {@link BinomCIResult}.
|
|
173
|
+
* @throws RangeError for counts that are not integers with 0 <= x <= n, n > 0, a level outside (0, 1) (at least 0.5
|
|
174
|
+
* one-sided), or an unknown method or sides.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* binomCI(37, 100).confInt; // [0.28182360534324535, 0.4677947041905709]
|
|
179
|
+
* binomCI(37, 100, { method: "clopper-pearson" }).confInt; // [0.27556657961455155, 0.4723516405516831]
|
|
180
|
+
* binomCI(1, 1000, { method: "midp", sides: "right" }).confInt; // [0, 0.004106205924199768]
|
|
181
|
+
* ```
|
|
182
|
+
*
|
|
183
|
+
* @category Hypothesis tests
|
|
184
|
+
*/
|
|
185
|
+
export function binomCI(x, n, options = {}) {
|
|
186
|
+
checkOptions(options, BINOM_CI_KEYS, "binomCI"); // generated: option-keys.mjs
|
|
187
|
+
const method = matchChoice(options.method, BINOM_CI_METHODS);
|
|
188
|
+
const lv = intervalLevel(options.confLevel, options.sides);
|
|
189
|
+
const [xs, ns] = checkTrials(x, n, "x", "n");
|
|
190
|
+
const { alpha, kappa } = lv;
|
|
191
|
+
const p = xs / ns;
|
|
192
|
+
const q = 1 - p;
|
|
193
|
+
let lower;
|
|
194
|
+
let upper;
|
|
195
|
+
switch (method) {
|
|
196
|
+
case "wald": {
|
|
197
|
+
const half = kappa * Math.sqrt((p * q) / ns);
|
|
198
|
+
[lower, upper] = [p - half, p + half];
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case "wilson":
|
|
202
|
+
[lower, upper] = wilsonLimits(xs, ns, kappa);
|
|
203
|
+
break;
|
|
204
|
+
case "wilsoncc": {
|
|
205
|
+
const k2 = kappa * kappa;
|
|
206
|
+
const den = 2 * (ns + k2);
|
|
207
|
+
// the lower limit is the smaller root of (n + kappa^2) L^2 - (2x + kappa^2 - 1) L + (2x - 1)^2 / (4n) = 0;
|
|
208
|
+
// where the difference cancels (more than half of the larger term) it is taken from the product of the roots
|
|
209
|
+
// as (2x - 1)^2 / (2n (2x + kappa^2 - 1 + kappa sqrt(...))), without cancellation (as wilsonLimits)
|
|
210
|
+
const bl = 2 * xs + k2 - 1;
|
|
211
|
+
const rl = kappa * Math.sqrt(k2 - 2 - 1 / ns + 4 * p * (ns * q + 1));
|
|
212
|
+
lower = p === 0 ? 0 : bl - rl < bl / 2 ? (((2 * xs - 1) / (2 * ns)) * (2 * xs - 1)) / (bl + rl) : (bl - rl) / den;
|
|
213
|
+
upper = p === 1 ? 1 : (2 * xs + k2 + 1 + kappa * Math.sqrt(k2 + 2 - 1 / ns + 4 * p * (ns * q - 1))) / den;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case "agresti-coull": {
|
|
217
|
+
const k2 = kappa * kappa;
|
|
218
|
+
const nt = ns + k2;
|
|
219
|
+
const pt = (xs + k2 / 2) / nt;
|
|
220
|
+
const half = kappa * Math.sqrt((pt * (1 - pt)) / nt);
|
|
221
|
+
[lower, upper] = [pt - half, pt + half];
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
case "jeffreys":
|
|
225
|
+
lower = xs === 0 ? 0 : betaLimit(alpha, xs + 0.5, ns - xs + 0.5, false);
|
|
226
|
+
upper = xs === ns ? 1 : betaLimit(alpha, xs + 0.5, ns - xs + 0.5, true);
|
|
227
|
+
break;
|
|
228
|
+
case "clopper-pearson":
|
|
229
|
+
lower = xs === 0 ? 0 : betaLimit(alpha, xs, ns - xs + 1, false);
|
|
230
|
+
upper = xs === ns ? 1 : betaLimit(alpha, xs + 1, ns - xs, true);
|
|
231
|
+
break;
|
|
232
|
+
case "midp":
|
|
233
|
+
// closed forms at the ends: 1/2 p^n = alpha/2 and 1/2 (1 - p)^n = alpha/2
|
|
234
|
+
lower = xs === 0 ? 0 : xs === ns ? Math.exp(Math.log(alpha) / ns) : midpLimit(xs, ns, alpha, false);
|
|
235
|
+
upper = xs === ns ? 1 : xs === 0 ? -Math.expm1(Math.log(alpha) / ns) : midpLimit(xs, ns, alpha, true);
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
lower = Math.max(0, lower);
|
|
239
|
+
upper = Math.min(1, upper);
|
|
240
|
+
if (lv.sides === "left")
|
|
241
|
+
upper = 1;
|
|
242
|
+
else if (lv.sides === "right")
|
|
243
|
+
lower = 0;
|
|
244
|
+
return reported("binomCI", {
|
|
245
|
+
estimate: p,
|
|
246
|
+
confInt: [lower, upper],
|
|
247
|
+
confLevel: options.confLevel ?? 0.95,
|
|
248
|
+
method,
|
|
249
|
+
sides: lv.sides,
|
|
250
|
+
warnings: [],
|
|
251
|
+
});
|
|
252
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type Alternative, type Counts, type HTest } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Two-sided method of {@link boschlooTest}: `"square"` (Boschloo's original: Fisher's two-sided p-value as the
|
|
4
|
+
* statistic) or `"central"` (twice the one-sided p-value in the direction of the data).
|
|
5
|
+
*
|
|
6
|
+
* @category Hypothesis tests
|
|
7
|
+
*/
|
|
8
|
+
export type BoschlooTsMethod = "square" | "central";
|
|
9
|
+
/**
|
|
10
|
+
* Options for {@link boschlooTest}.
|
|
11
|
+
*
|
|
12
|
+
* @category Hypothesis tests
|
|
13
|
+
*/
|
|
14
|
+
export interface BoschlooTestOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Alternative hypothesis, in Exact's orientation: `"less"` means pi1 < pi2 (the proportion of row 1 below that of
|
|
17
|
+
* row 2). Default `"two.sided"`. Unambiguous abbreviations are accepted.
|
|
18
|
+
*/
|
|
19
|
+
alternative?: Alternative | "two" | "l" | "g";
|
|
20
|
+
/** Two-sided method: `"square"` (the default, as Exact) or `"central"`. Ignored for one-sided tests. */
|
|
21
|
+
tsmethod?: BoschlooTsMethod | (string & {});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Result of {@link boschlooTest}.
|
|
25
|
+
*
|
|
26
|
+
* @category Hypothesis tests
|
|
27
|
+
*/
|
|
28
|
+
export interface BoschlooTestResult extends HTest {
|
|
29
|
+
/** The common proportion pi under the null hypothesis at which the supremum (the p-value) is attained. */
|
|
30
|
+
nuisanceParameter: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Boschloo's exact unconditional test (`method = "boschloo"`) for a 2 x 2 table whose rows are two independent
|
|
34
|
+
* binomial samples. Equivalent to `Exact::exact.test()` in R.
|
|
35
|
+
*
|
|
36
|
+
* The rows are the groups and column 1 counts the events: `[[x1, n1 - x1], [x2, n2 - x2]]`. The statistic is Fisher's
|
|
37
|
+
* exact p-value of each possible table (one-sided, or two-sided for `tsmethod: "square"`), and the p-value is the
|
|
38
|
+
* largest probability, over the common proportion pi under the null hypothesis, of the tables whose statistic is at
|
|
39
|
+
* most the observed one (Boschloo 1970). `alternative: "less"` means pi1 < pi2 (Exact's orientation; exact2x2's
|
|
40
|
+
* `boschloo()` uses the opposite one). `"central"` doubles the one-sided p-value in the direction of the data.
|
|
41
|
+
*
|
|
42
|
+
* The p-value is the true supremum, from a fine grid and a refinement of every candidate maximum; Exact maximises
|
|
43
|
+
* over 100 points refined by `optimise` and rounds to 12 digits (about 1e-9 relative off), and its two-sided
|
|
44
|
+
* `"square"` region can contain tables that do not belong there (docs/differences-from-r.md). `statistic` is the
|
|
45
|
+
* observed Fisher p-value (one-sided in the direction tested, or two-sided for `"square"`), `estimate` the difference
|
|
46
|
+
* of the proportions x1/n1 - x2/n2, and `nuisanceParameter` the pi at which the supremum is attained.
|
|
47
|
+
*
|
|
48
|
+
* A table with more than 2^24 possible outcomes ((n1 + 1)(n2 + 1)) is a RangeError, as is one whose grid would take
|
|
49
|
+
* more than about 10 seconds.
|
|
50
|
+
*
|
|
51
|
+
* @param table A 2 x 2 table of counts: rows are the groups, column 1 the events.
|
|
52
|
+
* @param options See {@link BoschlooTestOptions}.
|
|
53
|
+
* @returns The test result; see {@link BoschlooTestResult}.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const r = boschlooTest([[3, 7], [8, 2]], { alternative: "less" });
|
|
58
|
+
* r.pValue; // 0.020694732666015625 (Exact: 0.020694732666)
|
|
59
|
+
* r.statistic; // 0.034889259347463675 (Fisher's one-sided p-value)
|
|
60
|
+
* r.estimate; // -0.5
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @category Hypothesis tests
|
|
64
|
+
*/
|
|
65
|
+
export declare function boschlooTest(table: readonly Counts[], options?: BoschlooTestOptions): BoschlooTestResult;
|