@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,476 @@
|
|
|
1
|
+
// Score statistics of two independent binomial proportions x1/n1 and x2/n2 under a restriction (a given difference,
|
|
2
|
+
// ratio or odds ratio), and the roots that make their confidence limits. Shared by binomDiffCI, binomRatioCI and
|
|
3
|
+
// oddsRatio (src/htest/twoByTwo.ts); the statistics at a given difference are what a test of equivalence uses.
|
|
4
|
+
//
|
|
5
|
+
// Written for rStats.js from the papers:
|
|
6
|
+
// - Difference: the maximum likelihood estimate under p1 - p2 = delta, the root of the score equation of Farrington
|
|
7
|
+
// and Manning (Statistics in Medicine 9, 1990, 1447-1454) started from the closed (trigonometric) form of their
|
|
8
|
+
// cubic, and the statistic
|
|
9
|
+
// (p1 - p2 - delta)^2 / var(delta) with var = p1~(1 - p1~)/n1 + p2~(1 - p2~)/n2: Miettinen and Nurminen (Statistics
|
|
10
|
+
// in Medicine 4, 1985, 213-226) multiply var by N/(N - 1); Mee (Biometrics 40, 1984) and Farrington-Manning do not.
|
|
11
|
+
// - Ratio: Koopman (Biometrics 40, 1984, 513-517): Pearson's statistic at the MLE under p1 = phi p2, whose p1~ is the
|
|
12
|
+
// smaller root of a quadratic (Nam, Biometrics 51, 1995, gives the same limits in closed form).
|
|
13
|
+
// - Odds ratio: the score statistic at the MLE under (p1/(1 - p1)) / (p2/(1 - p2)) = theta (the root of a quadratic
|
|
14
|
+
// in p2~; Miettinen and Nurminen 1985 without the factor N/(N - 1), i.e. Cornfield's uncorrected chi-squared).
|
|
15
|
+
// The limits are the roots of statistic = bound on either side of the estimate, found by Brent's method to the
|
|
16
|
+
// precision of the doubles (on the log scale for the ratio and the odds ratio); the statistic enters through
|
|
17
|
+
// T/(1 + T), which is finite also where T is infinite (a variance of 0 at the end of the range).
|
|
18
|
+
import { logScaleRoot, uniroot } from "../../internal/uniroot.js";
|
|
19
|
+
import { ddAdd, ddDivNum, ddMul, ddMulNum, ddSub, twoProd, twoSum } from "../../special/dd.js";
|
|
20
|
+
/** T / (1 + T) for T >= 0, 1 for T = Infinity: a bounded, increasing function of a statistic */
|
|
21
|
+
function bounded(t) {
|
|
22
|
+
return t === Infinity ? 1 : t / (1 + t);
|
|
23
|
+
}
|
|
24
|
+
/** a^2 / b where 0 / 0 is 0 (a term of a statistic whose cell is fitted exactly at the end of the range) */
|
|
25
|
+
function term(num, den) {
|
|
26
|
+
if (num === 0)
|
|
27
|
+
return 0;
|
|
28
|
+
return den > 0 ? (num * num) / den : Infinity;
|
|
29
|
+
}
|
|
30
|
+
// ---- difference ----
|
|
31
|
+
/**
|
|
32
|
+
* Whether the counts are taken as failures (x -> n - x, delta -> -delta, each proportion -> 1 - p): where
|
|
33
|
+
* p1 + p2 > 1, so that the proportions worked with are the smaller ones. The statistics are the same in either
|
|
34
|
+
* orientation, but x / n near 1 holds only the absolute precision eps (1 - (1e15 - 1)/1e15 is 1e-15 off by 5%), and so
|
|
35
|
+
* does 1 - p~ formed from a p~ near 1
|
|
36
|
+
*/
|
|
37
|
+
function failures(x1, n1, x2, n2) {
|
|
38
|
+
return x1 / n1 + x2 / n2 > 1;
|
|
39
|
+
}
|
|
40
|
+
/** p1 - p2, rounded once (diffDeviation at 0) */
|
|
41
|
+
export function diffEstimate(x1, n1, x2, n2) {
|
|
42
|
+
return diffDeviation(x1, n1, x2, n2, 0);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The maximum likelihood estimate of two proportions under p1 - p2 = delta (-1 <= delta <= 1) with the complements:
|
|
46
|
+
* [p1~, 1 - p1~, p2~, 1 - p2~], each to nearly the precision of the doubles.
|
|
47
|
+
*
|
|
48
|
+
* The estimate is the root of the score equation (Farrington and Manning 1990). With s the smaller of p1~ and p2~
|
|
49
|
+
* (group s, xs of ns) and D = |delta|, the other is s + D (group l) and
|
|
50
|
+
* f(s) = xs/s - (ns - xs)/(1 - s) + xl/(s + D) - (nl - xl)/(1 - D - s) = 0, 0 <= s <= 1 - D,
|
|
51
|
+
* f decreasing; s = 0 where f(0) <= 0 and s = 1 - D where f(1 - D) >= 0. Solved by Newton's method kept inside the
|
|
52
|
+
* bracket by bisection (geometric where the bracket spans more than a factor 2), from the trigonometric solution of
|
|
53
|
+
* Farrington and Manning's cubic. That closed form alone has only the absolute precision eps: 4.337e-9 for 2.713e-9
|
|
54
|
+
* at 5/1e9 against 2/1e9 and delta = -1e-8, and the upper limit of 1/1e8 against 0/1e8 was 4e-3 relative off. Working
|
|
55
|
+
* with s and D (not p1~ - delta, nor 1 - p near 1: see `failures`) keeps the relative precision of small proportions
|
|
56
|
+
*/
|
|
57
|
+
function restrictedMleDiffFull(x1, n1, x2, n2, delta) {
|
|
58
|
+
if (failures(x1, n1, x2, n2)) {
|
|
59
|
+
const [q1, r1, q2, r2] = restrictedMleDiffFull(n1 - x1, n1, n2 - x2, n2, -delta);
|
|
60
|
+
return [r1, q1, r2, q2];
|
|
61
|
+
}
|
|
62
|
+
// group s has the smaller restricted proportion: group 1 where delta <= 0 (p1~ = p2~ + delta)
|
|
63
|
+
const oneSmaller = delta <= 0;
|
|
64
|
+
const [xs, ns, xl, nl] = oneSmaller ? [x1, n1, x2, n2] : [x2, n2, x1, n1];
|
|
65
|
+
const D = Math.abs(delta);
|
|
66
|
+
// 1 - D: exact for D >= 1/2, within half an ulp of a value above 1/2 otherwise
|
|
67
|
+
const top = 1 - D;
|
|
68
|
+
let s = 0;
|
|
69
|
+
if (top > 0) {
|
|
70
|
+
const f = (v) => {
|
|
71
|
+
let r = 0;
|
|
72
|
+
if (xs > 0)
|
|
73
|
+
r += xs / v;
|
|
74
|
+
if (xs < ns)
|
|
75
|
+
r -= (ns - xs) / (1 - v);
|
|
76
|
+
if (xl > 0)
|
|
77
|
+
r += xl / (v + D);
|
|
78
|
+
if (xl < nl)
|
|
79
|
+
r -= (nl - xl) / (top - v);
|
|
80
|
+
return r;
|
|
81
|
+
};
|
|
82
|
+
const df = (v) => {
|
|
83
|
+
let r = 0;
|
|
84
|
+
if (xs > 0)
|
|
85
|
+
r -= xs / (v * v);
|
|
86
|
+
if (xs < ns)
|
|
87
|
+
r -= (ns - xs) / ((1 - v) * (1 - v));
|
|
88
|
+
if (xl > 0)
|
|
89
|
+
r -= xl / ((v + D) * (v + D));
|
|
90
|
+
if (xl < nl)
|
|
91
|
+
r -= (nl - xl) / ((top - v) * (top - v));
|
|
92
|
+
return r;
|
|
93
|
+
};
|
|
94
|
+
// at the ends f is finite or +Infinity at 0 and -Infinity at 1 - D (only positive terms divide by 0 at 0, and only
|
|
95
|
+
// negative ones at 1 - D)
|
|
96
|
+
const f0 = f(0);
|
|
97
|
+
if (f0 <= 0)
|
|
98
|
+
s = 0;
|
|
99
|
+
else if (f(top) >= 0)
|
|
100
|
+
s = top;
|
|
101
|
+
else {
|
|
102
|
+
let a = 0;
|
|
103
|
+
let b = top;
|
|
104
|
+
const guess = closedFormMleDiff(x1, n1, x2, n2, delta);
|
|
105
|
+
s = oneSmaller ? guess : guess - delta;
|
|
106
|
+
if (!(s > a && s < b))
|
|
107
|
+
s = b / 2;
|
|
108
|
+
for (let i = 0; i < 2000; i++) {
|
|
109
|
+
const fs = f(s);
|
|
110
|
+
if (fs === 0)
|
|
111
|
+
break;
|
|
112
|
+
if (fs > 0)
|
|
113
|
+
a = s;
|
|
114
|
+
else
|
|
115
|
+
b = s;
|
|
116
|
+
let next = s - fs / df(s);
|
|
117
|
+
if (!(next > a && next < b))
|
|
118
|
+
next = a === 0 ? b / 2 : b > 2 * a ? Math.sqrt(a * b) : a + (b - a) / 2;
|
|
119
|
+
if (!(next > a && next < b))
|
|
120
|
+
break;
|
|
121
|
+
const step = Math.abs(next - s);
|
|
122
|
+
s = next;
|
|
123
|
+
if (step <= 2 ** -53 * s)
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const sl = [s, 1 - s, Math.min(1, s + D), Math.max(0, top - s)];
|
|
129
|
+
return oneSmaller ? sl : [sl[2], sl[3], sl[0], sl[1]];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The trigonometric solution of Farrington and Manning's cubic for p1~ under p1 - p2 = delta, with t = n2 / n1:
|
|
133
|
+
* a = 1 + t, b = -(1 + t + p1 + t p2 + delta (t + 2)), c = delta^2 + delta (2 p1 + t + 1) + p1 + t p2,
|
|
134
|
+
* d = -p1 delta (1 + delta); v = (b/(3a))^3 - bc/(6a^2) + d/(2a), u = sign(v) sqrt((b/(3a))^2 - c/(3a)),
|
|
135
|
+
* w = (pi + acos(v/u^3))/3, p1~ = 2u cos(w) - b/(3a). Accurate to about eps absolute: the start of the iteration
|
|
136
|
+
*/
|
|
137
|
+
function closedFormMleDiff(x1, n1, x2, n2, delta) {
|
|
138
|
+
const p1 = x1 / n1;
|
|
139
|
+
const p2 = x2 / n2;
|
|
140
|
+
const t = n2 / n1;
|
|
141
|
+
const a = 1 + t;
|
|
142
|
+
const b = -(1 + t + p1 + t * p2 + delta * (t + 2));
|
|
143
|
+
const c = delta * delta + delta * (2 * p1 + t + 1) + p1 + t * p2;
|
|
144
|
+
const d = -p1 * delta * (1 + delta);
|
|
145
|
+
const b3a = b / (3 * a);
|
|
146
|
+
const v = b3a * b3a * b3a - (b * c) / (6 * a * a) + d / (2 * a);
|
|
147
|
+
const u = Math.sign(v) * Math.sqrt(Math.max(0, b3a * b3a - c / (3 * a)));
|
|
148
|
+
if (u === 0)
|
|
149
|
+
return -b3a;
|
|
150
|
+
const w = (Math.PI + Math.acos(Math.max(-1, Math.min(1, v / (u * u * u))))) / 3;
|
|
151
|
+
return 2 * u * Math.cos(w) - b3a;
|
|
152
|
+
}
|
|
153
|
+
/** The maximum likelihood estimate (p1~, p2~) of two proportions under p1 - p2 = delta (see restrictedMleDiffFull) */
|
|
154
|
+
export function restrictedMleDiff(x1, n1, x2, n2, delta) {
|
|
155
|
+
const [q1, , q2] = restrictedMleDiffFull(x1, n1, x2, n2, delta);
|
|
156
|
+
return [q1, q2];
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The variance of p1 - p2 used by the score statistic at the difference delta: at the restricted MLE times N/(N - 1)
|
|
160
|
+
* (Miettinen-Nurminen, "mn"), at the restricted MLE ("mee", Farrington-Manning), or at the observed proportions
|
|
161
|
+
* ("wald"); each 1 - p without cancellation
|
|
162
|
+
*/
|
|
163
|
+
export function diffVariance(x1, n1, x2, n2, delta, kind) {
|
|
164
|
+
if (kind === "wald")
|
|
165
|
+
return ((x1 / n1) * ((n1 - x1) / n1)) / n1 + ((x2 / n2) * ((n2 - x2) / n2)) / n2;
|
|
166
|
+
const [q1, r1, q2, r2] = restrictedMleDiffFull(x1, n1, x2, n2, delta);
|
|
167
|
+
const v = (q1 * r1) / n1 + (q2 * r2) / n2;
|
|
168
|
+
if (kind === "mee")
|
|
169
|
+
return v;
|
|
170
|
+
const N = n1 + n2;
|
|
171
|
+
return (v * N) / (N - 1);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* (p1 - p2 - delta)^2 - bound var(delta) in double-double (the score methods): its sign tells statistic > bound
|
|
175
|
+
* without the roundings of the statistic (a few ulps of it, each moving a limit by about an ulp)
|
|
176
|
+
*/
|
|
177
|
+
function diffExcess(x1, n1, x2, n2, delta, kind, bound) {
|
|
178
|
+
const dev = ddSub(ddSub(ddDivNum([x1, 0], n1), ddDivNum([x2, 0], n2)), [delta, 0]);
|
|
179
|
+
const [q1, r1, q2, r2] = restrictedMleDiffFull(x1, n1, x2, n2, delta);
|
|
180
|
+
let v = ddAdd(ddDivNum(twoProd(q1, r1), n1), ddDivNum(twoProd(q2, r2), n2));
|
|
181
|
+
if (kind === "mn")
|
|
182
|
+
v = ddDivNum(ddMulNum(v, n1 + n2), n1 + n2 - 1);
|
|
183
|
+
return ddSub(ddMul(dev, dev), ddMulNum(v, bound))[0];
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* p1 - p2 - delta from the counts in double-double, rounded once: x1/n1 - x2/n2 in double cancels where the
|
|
187
|
+
* proportions are close (each holds only its absolute precision: 1 - (1e15 - 1)/1e15 is 1e-15 off by 5%, and
|
|
188
|
+
* 0.22 - 0.2 is 1e-17 off, an ulp of the limit 0.1155 of 80/100 against 78/100 at 90%)
|
|
189
|
+
*/
|
|
190
|
+
export function diffDeviation(x1, n1, x2, n2, delta) {
|
|
191
|
+
return ddSub(ddSub(ddDivNum([x1, 0], n1), ddDivNum([x2, 0], n2)), [delta, 0])[0];
|
|
192
|
+
}
|
|
193
|
+
/** The squared score statistic (p1 - p2 - delta)^2 / var(delta); 0 at delta = p1 - p2, Infinity where var is 0 */
|
|
194
|
+
export function diffStatistic(x1, n1, x2, n2, delta, kind) {
|
|
195
|
+
return term(diffDeviation(x1, n1, x2, n2, delta), diffVariance(x1, n1, x2, n2, delta, kind));
|
|
196
|
+
}
|
|
197
|
+
const f64 = new Float64Array(1);
|
|
198
|
+
const i64 = new BigInt64Array(f64.buffer);
|
|
199
|
+
/** An integer key of a finite double that orders as the doubles do (consecutive doubles have consecutive keys) */
|
|
200
|
+
function keyOf(x) {
|
|
201
|
+
f64[0] = Math.abs(x);
|
|
202
|
+
return x < 0 ? -i64[0] : i64[0];
|
|
203
|
+
}
|
|
204
|
+
function fromKey(k) {
|
|
205
|
+
i64[0] = k < 0n ? -k : k;
|
|
206
|
+
return k < 0n ? -f64[0] : f64[0];
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The double at the sign change of g (increasing where `increasing`) within [lo, hi], from a root r found to a few ulps
|
|
210
|
+
* or a few eps |log r|: Brent stops once its bracket is within 4 eps |r|, which left a limit of the difference up to
|
|
211
|
+
* 4 ulps from the change (0.11553638647686573 for 0.11553638647686568), and on the log scale within 4 eps |log r|
|
|
212
|
+
* (13 eps at the ratio 2e-17). Searches from r in steps of 1, 2, 4, ... ulps for the change, bisects the doubles
|
|
213
|
+
* between, and returns the one of the two neighbours at the change with the smaller |g|
|
|
214
|
+
*/
|
|
215
|
+
function polishRoot(g, r, lo, hi, increasing) {
|
|
216
|
+
const gr = g(r);
|
|
217
|
+
if (gr === 0 || Number.isNaN(gr))
|
|
218
|
+
return r;
|
|
219
|
+
// g(r) > 0 on the increasing side means the change lies below r
|
|
220
|
+
const up = (gr < 0) === increasing;
|
|
221
|
+
const end = keyOf(up ? hi : lo);
|
|
222
|
+
let a = keyOf(r);
|
|
223
|
+
let ga = gr;
|
|
224
|
+
let b = a;
|
|
225
|
+
let gb = gr;
|
|
226
|
+
let step = 1n;
|
|
227
|
+
for (;;) {
|
|
228
|
+
b = up ? a + step : a - step;
|
|
229
|
+
if (up ? b >= end : b <= end)
|
|
230
|
+
b = end;
|
|
231
|
+
gb = g(fromKey(b));
|
|
232
|
+
if (gb === 0)
|
|
233
|
+
return fromKey(b);
|
|
234
|
+
if (gb < 0 !== ga < 0 || b === end)
|
|
235
|
+
break;
|
|
236
|
+
a = b;
|
|
237
|
+
ga = gb;
|
|
238
|
+
step *= 2n;
|
|
239
|
+
if (step > 1n << 40n)
|
|
240
|
+
return fromKey(a);
|
|
241
|
+
}
|
|
242
|
+
if (gb < 0 === ga < 0 || Number.isNaN(gb))
|
|
243
|
+
return fromKey(a);
|
|
244
|
+
// a and b on either side of the change: bisect the keys until they are neighbours
|
|
245
|
+
while (b - a > 1n || a - b > 1n) {
|
|
246
|
+
const m = (a + b) / 2n;
|
|
247
|
+
const gm = g(fromKey(m));
|
|
248
|
+
if (gm === 0)
|
|
249
|
+
return fromKey(m);
|
|
250
|
+
if (gm < 0 === ga < 0) {
|
|
251
|
+
a = m;
|
|
252
|
+
ga = gm;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
b = m;
|
|
256
|
+
gb = gm;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return Math.abs(gb) < Math.abs(ga) ? fromKey(b) : fromKey(a);
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The limits [lower, upper] of {delta in [-1, 1] : statistic(delta) <= bound} around est = p1 - p2: the roots of
|
|
263
|
+
* statistic = bound below and above est (-1 or 1 where est is at the end)
|
|
264
|
+
*/
|
|
265
|
+
export function diffScoreLimits(x1, n1, x2, n2, bound, kind) {
|
|
266
|
+
const est = diffEstimate(x1, n1, x2, n2);
|
|
267
|
+
if (bound === 0)
|
|
268
|
+
return [est, est];
|
|
269
|
+
const target = bounded(bound);
|
|
270
|
+
const g = (delta) => (delta === est ? 0 : bounded(diffStatistic(x1, n1, x2, n2, delta, kind))) - target;
|
|
271
|
+
const root = (lo, hi, gLo, gHi) => {
|
|
272
|
+
if (gLo === 0)
|
|
273
|
+
return lo;
|
|
274
|
+
if (gHi === 0)
|
|
275
|
+
return hi;
|
|
276
|
+
const r = uniroot(g, lo, hi, { tol: 1e-300, fLower: gLo, fUpper: gHi });
|
|
277
|
+
return kind === "wald" ? r : polishRoot((d) => diffExcess(x1, n1, x2, n2, d, kind, bound), r, lo, hi, gLo < 0);
|
|
278
|
+
};
|
|
279
|
+
const gm = g(-1);
|
|
280
|
+
const gp = g(1);
|
|
281
|
+
const lower = est <= -1 || gm <= 0 ? -1 : root(-1, est, gm, -target);
|
|
282
|
+
const upper = est >= 1 || gp <= 0 ? 1 : root(est, 1, -target, gp);
|
|
283
|
+
return [lower, upper];
|
|
284
|
+
}
|
|
285
|
+
// ---- roots on the log scale ----
|
|
286
|
+
/**
|
|
287
|
+
* The root of g(u) = h(exp(u)) - target, h increasing in |u - u0| from h(exp(u0)) <= target, on the side of u0 given by
|
|
288
|
+
* dir: the bracket is widened from u0 by steps that double, and the root solved by Brent's method; 0 (dir -1) or
|
|
289
|
+
* Infinity (dir 1) when no root lies within the doubles
|
|
290
|
+
*/
|
|
291
|
+
function logSideRoot(g, u0, dir) {
|
|
292
|
+
let a = u0;
|
|
293
|
+
let ga = g(u0);
|
|
294
|
+
if (ga >= 0)
|
|
295
|
+
return Math.exp(u0);
|
|
296
|
+
let step = 1;
|
|
297
|
+
for (;;) {
|
|
298
|
+
const b = u0 + dir * step;
|
|
299
|
+
if (Math.abs(b) > 745)
|
|
300
|
+
return dir < 0 ? 0 : Infinity;
|
|
301
|
+
const gb = g(b);
|
|
302
|
+
if (gb >= 0) {
|
|
303
|
+
if (gb === 0)
|
|
304
|
+
return Math.exp(b);
|
|
305
|
+
const r = dir > 0 ? uniroot(g, a, b, { tol: 2 ** -60, fLower: ga, fUpper: gb }) : uniroot(g, b, a, { tol: 2 ** -60, fLower: gb, fUpper: ga });
|
|
306
|
+
return Math.exp(r);
|
|
307
|
+
}
|
|
308
|
+
a = b;
|
|
309
|
+
ga = gb;
|
|
310
|
+
step *= 2;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
// ---- ratio ----
|
|
314
|
+
/**
|
|
315
|
+
* The MLE p1~ under p1 = phi p2 (Koopman 1984): the smaller root of
|
|
316
|
+
* (n1 + n2) p^2 - (phi (n1 + x2) + x1 + n2) p + phi (x1 + x2) = 0, capped at min(1, phi); p2~ = p1~ / phi. Accurate to
|
|
317
|
+
* about eps absolute (the discriminant cancels where the roots are close): the start of restrictedMleRatioFull
|
|
318
|
+
*/
|
|
319
|
+
export function restrictedMleRatio(x1, n1, x2, n2, phi) {
|
|
320
|
+
const A = n1 + n2;
|
|
321
|
+
const B = phi * (n1 + x2) + x1 + n2;
|
|
322
|
+
const C = phi * (x1 + x2);
|
|
323
|
+
// the smaller root (B - sqrt(B^2 - 4AC)) / (2A), in the form without cancellation
|
|
324
|
+
const disc = Math.sqrt(Math.max(0, B * B - 4 * A * C));
|
|
325
|
+
const q1 = Math.min(1, phi, C === 0 ? 0 : (2 * C) / (B + disc));
|
|
326
|
+
return [q1, Math.min(1, q1 / phi)];
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* The MLE under p1 = phi p2 with the complements, [p1~, 1 - p1~, p2~, 1 - p2~], each to nearly the precision of the
|
|
330
|
+
* doubles. The closed form (restrictedMleRatio) holds about eps absolute, so 1 - p~ near 0 lost its digits (the upper
|
|
331
|
+
* limit of 1/2^53 against 2^53/2^53 was 4.4409e-16 for 6.2893e-16, the lower of 1e15/1e15 against (1e15 - 1)/1e15
|
|
332
|
+
* was 1). The root of the score in p2~, S = m/p2 - phi (n1 - x1)/(1 - p1) - (n2 - x2)/(1 - p2) (m = x1 + x2,
|
|
333
|
+
* decreasing), is solved by Newton's method kept in its bracket, in p2~, 1 - p2~ or 1 - p1~ (chosen at the closed
|
|
334
|
+
* form's estimate), from which the others are formed in double-double
|
|
335
|
+
*/
|
|
336
|
+
function restrictedMleRatioFull(x1, n1, x2, n2, phi) {
|
|
337
|
+
const [c1, c2] = restrictedMleRatio(x1, n1, x2, n2, phi);
|
|
338
|
+
if (!(phi > 0 && phi < Infinity))
|
|
339
|
+
return [c1, 1 - c1, c2, 1 - c2];
|
|
340
|
+
// the variable: 0 = p2~, 2 = 1 - p2~, 3 = 1 - p1~; p2~ as a function of it, in double-double. The rounding of v
|
|
341
|
+
// (eps v) moves each other quantity by at most about eps times itself: v = p2~ where both are at most 1/2 (1 - p1~
|
|
342
|
+
// moves by eps p1~), otherwise v = 1 - p2~ where phi (1 - p2~) <= 1 - p1~ (1 - p1~ = 1 - phi + phi v moves by
|
|
343
|
+
// eps phi v) and v = 1 - p1~ elsewhere. (With v = p1~, 3/1e15 against (1e15 - 1)/1e15 failed: 1 - p2~ = 1 - v/phi
|
|
344
|
+
// moves by eps p2~, 10% of its 1e-15)
|
|
345
|
+
const mode = c1 <= 0.5 && c2 <= 0.5 ? 0 : phi * (1 - c2) <= 1 - c1 ? 2 : 3;
|
|
346
|
+
const start = mode === 0 ? c2 : mode === 2 ? 1 - c2 : 1 - c1;
|
|
347
|
+
const p2Of = (v) => (mode === 0 ? [v, 0] : mode === 2 ? twoSum(1, -v) : ddDivNum(twoSum(1, -v), phi));
|
|
348
|
+
// |d p2~ / dv| and the range of v (p2~ in [0, min(1, 1/phi)], p1~ = phi p2~ in [0, min(1, phi)])
|
|
349
|
+
const slope = mode === 3 ? 1 / phi : 1;
|
|
350
|
+
const vMax = mode === 0 ? Math.min(1, 1 / phi) : 1;
|
|
351
|
+
const vMin = mode === 2 ? (phi > 1 ? ddDivNum(twoSum(phi, -1), phi)[0] : 0) : mode === 3 ? (phi < 1 ? twoSum(1, -phi)[0] : 0) : 0;
|
|
352
|
+
const parts = (v) => {
|
|
353
|
+
const p2 = p2Of(v);
|
|
354
|
+
const p1 = ddMulNum(p2, phi);
|
|
355
|
+
return [p1[0], ddSub([1, 0], p1)[0], p2[0], ddSub([1, 0], p2)[0]];
|
|
356
|
+
};
|
|
357
|
+
const m = x1 + x2;
|
|
358
|
+
// h(v) = S as a function of v, made decreasing (p2~ decreases in v for modes 2 and 3), and h'(v)
|
|
359
|
+
const sign = mode < 2 ? 1 : -1;
|
|
360
|
+
// (a count over a value that has rounded to 0 or below at the end of the range counts as infinite)
|
|
361
|
+
const h = (v) => {
|
|
362
|
+
const [, r1, q2, r2] = parts(v);
|
|
363
|
+
let s = 0;
|
|
364
|
+
let ds = 0;
|
|
365
|
+
if (m > 0) {
|
|
366
|
+
s += q2 > 0 ? m / q2 : Infinity;
|
|
367
|
+
ds -= m / (q2 * q2);
|
|
368
|
+
}
|
|
369
|
+
if (x1 < n1) {
|
|
370
|
+
s -= r1 > 0 ? (phi * (n1 - x1)) / r1 : Infinity;
|
|
371
|
+
ds -= ((phi * (n1 - x1)) / r1) * (phi / r1);
|
|
372
|
+
}
|
|
373
|
+
if (x2 < n2) {
|
|
374
|
+
s -= r2 > 0 ? (n2 - x2) / r2 : Infinity;
|
|
375
|
+
ds -= (n2 - x2) / (r2 * r2);
|
|
376
|
+
}
|
|
377
|
+
return [sign * s, ds * slope];
|
|
378
|
+
};
|
|
379
|
+
let v;
|
|
380
|
+
if (!(vMax > vMin))
|
|
381
|
+
v = vMin;
|
|
382
|
+
else if (!(h(vMin)[0] > 0))
|
|
383
|
+
v = vMin;
|
|
384
|
+
else if (!(h(vMax)[0] < 0))
|
|
385
|
+
v = vMax;
|
|
386
|
+
else {
|
|
387
|
+
let a = vMin;
|
|
388
|
+
let b = vMax;
|
|
389
|
+
v = start;
|
|
390
|
+
if (!(v > a && v < b))
|
|
391
|
+
v = a + (b - a) / 2;
|
|
392
|
+
for (let i = 0; i < 2000; i++) {
|
|
393
|
+
const [hv, dh] = h(v);
|
|
394
|
+
if (hv === 0)
|
|
395
|
+
break;
|
|
396
|
+
if (hv > 0)
|
|
397
|
+
a = v;
|
|
398
|
+
else
|
|
399
|
+
b = v;
|
|
400
|
+
let next = v - hv / dh;
|
|
401
|
+
if (!(next > a && next < b))
|
|
402
|
+
next = a <= 0 ? b / 2 : b > 2 * a ? Math.sqrt(a * b) : a + (b - a) / 2;
|
|
403
|
+
if (!(next > a && next < b))
|
|
404
|
+
break;
|
|
405
|
+
const step = Math.abs(next - v);
|
|
406
|
+
v = next;
|
|
407
|
+
if (step <= 2 ** -53 * v)
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const [q1, r1, q2, r2] = parts(v);
|
|
412
|
+
return [Math.min(1, Math.max(0, q1)), Math.min(1, Math.max(0, r1)), Math.min(1, Math.max(0, q2)), Math.min(1, Math.max(0, r2))];
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Koopman's statistic: Pearson's chi-squared of the two groups at the MLE under p1 = phi p2 (a 0/0 term counts 0);
|
|
416
|
+
* x - n p~ is formed as n (1 - p~) - (n - x) where p~ > 1/2, with 1 - p~ from restrictedMleRatioFull
|
|
417
|
+
*/
|
|
418
|
+
export function ratioStatistic(x1, n1, x2, n2, phi) {
|
|
419
|
+
const [q1, r1, q2, r2] = restrictedMleRatioFull(x1, n1, x2, n2, phi);
|
|
420
|
+
const dev = (x, n, q, r) => (q > 0.5 ? n * r - (n - x) : x - n * q);
|
|
421
|
+
return term(dev(x1, n1, q1, r1), n1 * q1 * r1) + term(dev(x2, n2, q2, r2), n2 * q2 * r2);
|
|
422
|
+
}
|
|
423
|
+
/** Koopman's interval {phi : statistic(phi) <= bound} around est = p1 / p2 (not both counts 0) */
|
|
424
|
+
export function ratioScoreLimits(x1, n1, x2, n2, bound) {
|
|
425
|
+
const est = x1 / n1 / (x2 / n2);
|
|
426
|
+
return logScoreLimits(est, bound, (phi) => ratioStatistic(x1, n1, x2, n2, phi));
|
|
427
|
+
}
|
|
428
|
+
// ---- odds ratio ----
|
|
429
|
+
/**
|
|
430
|
+
* The MLE (p1~, p2~) under (p1/(1 - p1)) / (p2/(1 - p2)) = theta: p2~ is the root in [0, 1] of A p^2 + B p + C = 0 with
|
|
431
|
+
* A = n2 (theta - 1), B = n1 theta + n2 - (x1 + x2)(theta - 1), C = -(x1 + x2), written 2(x1 + x2) / (B + sqrt(B^2 - 4AC))
|
|
432
|
+
* (no cancellation, and (x1 + x2) / (n1 + n2) at theta = 1); p1~ = p2~ theta / (1 + p2~ (theta - 1))
|
|
433
|
+
*/
|
|
434
|
+
export function restrictedMleOddsRatio(x1, n1, x2, n2, theta) {
|
|
435
|
+
const m = x1 + x2;
|
|
436
|
+
const A = n2 * (theta - 1);
|
|
437
|
+
const B = n1 * theta + n2 - m * (theta - 1);
|
|
438
|
+
const q2 = m === 0 ? 0 : Math.min(1, (2 * m) / (B + Math.sqrt(Math.max(0, B * B + 4 * A * m))));
|
|
439
|
+
const q1 = theta === Infinity ? (q2 > 0 ? 1 : 0) : Math.min(1, (q2 * theta) / (1 + q2 * (theta - 1)));
|
|
440
|
+
return [q1, q2];
|
|
441
|
+
}
|
|
442
|
+
/** The score statistic of the odds ratio at theta: (x1 - n1 p1~)^2 (1/(n1 p1~ q1~) + 1/(n2 p2~ q2~)) */
|
|
443
|
+
export function oddsRatioStatistic(x1, n1, x2, n2, theta) {
|
|
444
|
+
const [q1, q2] = restrictedMleOddsRatio(x1, n1, x2, n2, theta);
|
|
445
|
+
const dev = x1 - n1 * q1;
|
|
446
|
+
return term(dev, n1 * q1 * (1 - q1)) + term(dev, n2 * q2 * (1 - q2));
|
|
447
|
+
}
|
|
448
|
+
/** The score interval of the odds ratio around est = x1 (n2 - x2) / ((n1 - x1) x2) (est 0 or Infinity, not NaN) */
|
|
449
|
+
export function oddsRatioScoreLimits(x1, n1, x2, n2, bound) {
|
|
450
|
+
const est = (x1 * (n2 - x2)) / ((n1 - x1) * x2);
|
|
451
|
+
return logScoreLimits(est, bound, (theta) => oddsRatioStatistic(x1, n1, x2, n2, theta));
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* The roots of stat = bound on both sides of est (where stat is 0), solved on the log scale. Where est is 0 or
|
|
455
|
+
* Infinity (a zero count), that limit is est and the other is solved from a bracket around 1 (logScaleRoot). The roots
|
|
456
|
+
* are then moved to the sign change of stat - bound among the doubles (polishRoot)
|
|
457
|
+
*/
|
|
458
|
+
function logScoreLimits(est, bound, stat) {
|
|
459
|
+
if (bound === 0)
|
|
460
|
+
return [est, est];
|
|
461
|
+
const target = bounded(bound);
|
|
462
|
+
// the raw comparison for the polish (stat increases away from est)
|
|
463
|
+
const h = (v) => stat(v) - bound;
|
|
464
|
+
const below = (r) => (r > 0 && r < est ? polishRoot(h, r, Number.MIN_VALUE, est, false) : r);
|
|
465
|
+
const above = (r) => (r > est && r < Infinity ? polishRoot(h, r, est, Number.MAX_VALUE, true) : r);
|
|
466
|
+
if (est === 0 || est === Infinity) {
|
|
467
|
+
// stat increases in v from est = 0, and decreases in v towards est = Infinity
|
|
468
|
+
const f = (v) => bounded(stat(v)) - target;
|
|
469
|
+
const f1 = f(1);
|
|
470
|
+
const root = f1 === 0 ? 1 : logScaleRoot(f, est === 0 ? f1 > 0 : f1 < 0);
|
|
471
|
+
return est === 0 ? [0, above(root)] : [below(root), Infinity];
|
|
472
|
+
}
|
|
473
|
+
const u0 = Math.log(est);
|
|
474
|
+
const g = (u) => (u === u0 ? 0 : bounded(stat(Math.exp(u)))) - target;
|
|
475
|
+
return [below(logSideRoot(g, u0, -1)), above(logSideRoot(g, u0, 1))];
|
|
476
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { type Sample } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Options for {@link tost}.
|
|
4
|
+
*
|
|
5
|
+
* @category Hypothesis tests
|
|
6
|
+
*/
|
|
7
|
+
export interface TostOptions {
|
|
8
|
+
/** The equivalence margin: equivalence means |mean difference| < epsilon. Positive. Default `1` (as R). */
|
|
9
|
+
epsilon?: number;
|
|
10
|
+
/** Paired TOST on `x - y` (pairs with `NaN` are dropped). Default `false`. */
|
|
11
|
+
paired?: boolean;
|
|
12
|
+
/** Pooled variance (Student's t) instead of Welch's. Default `false`. */
|
|
13
|
+
varEqual?: boolean;
|
|
14
|
+
/** One minus the level of each one-sided test, in (0.5, 1); the interval is a 1 - 2(1 - confLevel) interval.
|
|
15
|
+
* Default `0.95`. */
|
|
16
|
+
confLevel?: number;
|
|
17
|
+
/** The level of each one-sided test, in (0, 0.5); overrides `confLevel` (which becomes `1 - alpha`). */
|
|
18
|
+
alpha?: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Result of {@link tost}.
|
|
22
|
+
*
|
|
23
|
+
* @category Hypothesis tests
|
|
24
|
+
*/
|
|
25
|
+
export interface TostResult {
|
|
26
|
+
/** `"Welch Two Sample TOST"`, `" Two Sample TOST"` (pooled; the leading space is R's) or `"Paired TOST"`. */
|
|
27
|
+
method: string;
|
|
28
|
+
/** Degrees of freedom of the t tests (`parameterName` `"df"`). */
|
|
29
|
+
parameter: number;
|
|
30
|
+
/** `"df"`. */
|
|
31
|
+
parameterName: string;
|
|
32
|
+
/** The larger of the two one-sided p-values, `P(T > (epsilon - |d|)/se)` (R's `tost.p.value`). */
|
|
33
|
+
pValue: number;
|
|
34
|
+
/** `"rejected"` (equivalence shown: `confInt` lies inside (-epsilon, epsilon)) or `"not rejected"`, as R. */
|
|
35
|
+
result: "rejected" | "not rejected";
|
|
36
|
+
/** `d -+ qt(confLevel, df) se`: the 1 - 2(1 - confLevel) interval of the difference (R's `tost.interval`). */
|
|
37
|
+
confInt: [number, number];
|
|
38
|
+
/** `confLevel` (or `1 - alpha`), R's `conf.level` attribute of the interval. */
|
|
39
|
+
confLevel: number;
|
|
40
|
+
/** `[mean of x, mean of y]`, or the mean of the differences for a paired test. */
|
|
41
|
+
estimate: number | number[];
|
|
42
|
+
/** Names of `estimate`, as tTest's. */
|
|
43
|
+
estimateNames: string | string[];
|
|
44
|
+
/** The margin. */
|
|
45
|
+
epsilon: number;
|
|
46
|
+
/** Standard error of the difference (R's `se.diff`). */
|
|
47
|
+
seDiff: number;
|
|
48
|
+
/** Warnings (none: input without a defined result is an error). */
|
|
49
|
+
warnings: string[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Two one-sided t tests (TOST) of equivalence of two means. Equivalent to `equivalence::tost()` in R.
|
|
53
|
+
*
|
|
54
|
+
* Equivalence, |mean(x) - mean(y)| < epsilon, is shown at level alpha = 1 - confLevel when both one-sided t tests of
|
|
55
|
+
* H01: d <= -epsilon and H02: d >= epsilon reject; the p-value is the larger of their p-values and `confInt` the dual
|
|
56
|
+
* 1 - 2 alpha interval (90% for the default `confLevel` 0.95). The difference, standard error and degrees of freedom are
|
|
57
|
+
* those of {@link tTest} (Welch's by default, pooled with `varEqual`, paired with `paired`), with tTest's checks and
|
|
58
|
+
* errors. Used on 0/1 data it compares two proportions with a t approximation; for counts
|
|
59
|
+
* {@link binomDiffEquivalenceTest} is the better test.
|
|
60
|
+
*
|
|
61
|
+
* Unlike R, a level of 0.5 or less (`confLevel`) or of 0.5 or more (`alpha`) is an error (the interval would be empty),
|
|
62
|
+
* and so is a margin that is not positive.
|
|
63
|
+
*
|
|
64
|
+
* @param x Sample.
|
|
65
|
+
* @param y Second sample.
|
|
66
|
+
* @param options See {@link TostOptions}.
|
|
67
|
+
* @returns See {@link TostResult}.
|
|
68
|
+
* @throws RangeError for a margin that is not positive and finite or a level out of range; Error where tTest has
|
|
69
|
+
* too few observations or the data are essentially constant.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const r = tost([5.1, 4.9, 5.6, 5.8, 6.0, 5.4], [5.3, 5.0, 5.5, 5.7, 5.9, 5.2], { epsilon: 0.5 });
|
|
74
|
+
* r.pValue; // 0.029694342816029128
|
|
75
|
+
* r.confInt; // [-0.36394177233776226, 0.4306084390044287] (a 90% interval)
|
|
76
|
+
* r.result; // "rejected"
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* @category Hypothesis tests
|
|
80
|
+
*/
|
|
81
|
+
export declare function tost(x: Sample, y: Sample, options?: TostOptions): TostResult;
|
|
82
|
+
/**
|
|
83
|
+
* Statistics of {@link binomDiffEquivalenceTest}.
|
|
84
|
+
*
|
|
85
|
+
* @category Hypothesis tests
|
|
86
|
+
*/
|
|
87
|
+
export type BinomDiffEquivalenceMethod = "mn" | "mee" | "wald";
|
|
88
|
+
/**
|
|
89
|
+
* Options for {@link binomDiffEquivalenceTest}.
|
|
90
|
+
*
|
|
91
|
+
* @category Hypothesis tests
|
|
92
|
+
*/
|
|
93
|
+
export interface BinomDiffEquivalenceTestOptions {
|
|
94
|
+
/** The equivalence margin epsilon, in (0, 1): equivalence means -epsilon < p1 - p2 < epsilon. Required. */
|
|
95
|
+
margin?: number;
|
|
96
|
+
/** The statistic: `"mn"` (Miettinen-Nurminen score, the default), `"mee"` (Farrington-Manning score) or `"wald"`.
|
|
97
|
+
* Unambiguous abbreviations are accepted. */
|
|
98
|
+
method?: BinomDiffEquivalenceMethod | (string & {});
|
|
99
|
+
/** The level of each one-sided test, in (0, 0.5). Default `0.05`. */
|
|
100
|
+
alpha?: number;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Result of {@link binomDiffEquivalenceTest}.
|
|
104
|
+
*
|
|
105
|
+
* @category Hypothesis tests
|
|
106
|
+
*/
|
|
107
|
+
export interface BinomDiffEquivalenceTestResult {
|
|
108
|
+
/** The statistic, in full (`"mn"`, `"mee"` or `"wald"`). */
|
|
109
|
+
method: BinomDiffEquivalenceMethod;
|
|
110
|
+
/** The estimate p1 - p2. */
|
|
111
|
+
estimate: number;
|
|
112
|
+
/** The margin epsilon. */
|
|
113
|
+
margin: number;
|
|
114
|
+
/** The level of each one-sided test. */
|
|
115
|
+
alpha: number;
|
|
116
|
+
/**
|
|
117
|
+
* `[z(-margin), z(margin)]`, z(delta0) = (p1 - p2 - delta0) / sqrt(var(delta0)): the statistics of H01: p1 - p2 <=
|
|
118
|
+
* -margin (large values reject) and of H02: p1 - p2 >= margin (small values reject).
|
|
119
|
+
*/
|
|
120
|
+
statistic: [number, number];
|
|
121
|
+
/** `"z"`. */
|
|
122
|
+
statisticName: string;
|
|
123
|
+
/** `[P(Z > z(-margin)), P(Z < z(margin))]`: the one-sided p-values of H01 and H02. */
|
|
124
|
+
pValues: [number, number];
|
|
125
|
+
/** The larger of `pValues`: the p-value of the test of equivalence. */
|
|
126
|
+
pValue: number;
|
|
127
|
+
/** `"rejected"` (equivalence shown: both p-values below alpha) or `"not rejected"`, as {@link tost}'s `result`. */
|
|
128
|
+
result: "rejected" | "not rejected";
|
|
129
|
+
/** The 1 - 2 alpha interval of p1 - p2 of {@link binomDiffCI} with the same method; equivalence is shown exactly when
|
|
130
|
+
* it lies inside (-margin, margin). */
|
|
131
|
+
confInt: [number, number];
|
|
132
|
+
/** `1 - 2 alpha`. */
|
|
133
|
+
confLevel: number;
|
|
134
|
+
/** Warnings (none: input without a defined result is an error). */
|
|
135
|
+
warnings: string[];
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Test of equivalence of two independent proportions x1/n1 and x2/n2 within a margin. Two one-sided tests of
|
|
139
|
+
* H01: p1 - p2 <= -margin and H02: p1 - p2 >= margin, each at level `alpha`; equivalence is shown when both reject.
|
|
140
|
+
* Not an R function. (The R packages in scope give the dual intervals, `DescTools::BinomDiffCI`, or test one bound at
|
|
141
|
+
* a time.)
|
|
142
|
+
*
|
|
143
|
+
* The statistic at a bound delta0 is z = (p1 - p2 - delta0) / sqrt(var), with var the variance of p1 - p2 at the
|
|
144
|
+
* maximum likelihood estimate of (p1, p2) under p1 - p2 = delta0 (Farrington and Manning 1990, `"mee"`), the same times
|
|
145
|
+
* N/(N - 1) (Miettinen and Nurminen 1985, `"mn"`, the default), or at the observed proportions (`"wald"`). The
|
|
146
|
+
* p-values are normal tails. The test is dual to {@link binomDiffCI} with the same method at the level 1 - 2 alpha:
|
|
147
|
+
* equivalence is shown exactly when that interval, returned as `confInt`, lies inside (-margin, margin).
|
|
148
|
+
*
|
|
149
|
+
* The Wald variance is 0 when both proportions are 0 or 1: the statistics are then infinite and the p-values 0 or 1,
|
|
150
|
+
* as binomDiffCI's Wald interval is a point there.
|
|
151
|
+
*
|
|
152
|
+
* @param x1 Successes in group 1.
|
|
153
|
+
* @param n1 Trials in group 1.
|
|
154
|
+
* @param x2 Successes in group 2.
|
|
155
|
+
* @param n2 Trials in group 2.
|
|
156
|
+
* @param options See {@link BinomDiffEquivalenceTestOptions}; `margin` is required.
|
|
157
|
+
* @returns See {@link BinomDiffEquivalenceTestResult}.
|
|
158
|
+
* @throws RangeError for counts that are not integers with 0 <= x <= n, n > 0, a margin outside (0, 1), alpha
|
|
159
|
+
* outside (0, 0.5), or an unknown method.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* const r = binomDiffEquivalenceTest(80, 100, 78, 100, { margin: 0.15 });
|
|
164
|
+
* r.pValues; // [0.0018937433499954864, 0.012899725361675841]
|
|
165
|
+
* r.pValue; // 0.012899725361675841
|
|
166
|
+
* r.confInt; // [-0.07557697533608577, 0.11553638647686569] (binomDiffCI, method "mn", 90%)
|
|
167
|
+
* r.result; // "rejected"
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @category Hypothesis tests
|
|
171
|
+
*/
|
|
172
|
+
export declare function binomDiffEquivalenceTest(x1: number, n1: number, x2: number, n2: number, options?: BinomDiffEquivalenceTestOptions): BinomDiffEquivalenceTestResult;
|