@numero-uno-jpn/rstats 0.9.0 → 0.10.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.
Files changed (112) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/README.ja.md +60 -11
  3. package/README.md +59 -10
  4. package/THIRD_PARTY_NOTICES.md +7 -2
  5. package/dist/chunks/{chunk-H2AP6TCR.js → chunk-2XKG3DX4.js} +1 -1
  6. package/dist/chunks/{chunk-FC2TLHZC.js → chunk-4NM37MGA.js} +2 -2
  7. package/dist/chunks/chunk-7HPVUVZB.js +2 -0
  8. package/dist/chunks/{chunk-YC4JWUKG.js → chunk-AECGWZHD.js} +2 -2
  9. package/dist/chunks/{chunk-LE7TBHNI.js → chunk-ENL43FNO.js} +2 -2
  10. package/dist/chunks/chunk-EWC6I75B.js +2 -0
  11. package/dist/chunks/chunk-IT354SHV.js +3 -0
  12. package/dist/chunks/{chunk-BGVMSDVJ.js → chunk-LIMZKONM.js} +2 -2
  13. package/dist/chunks/chunk-O6PT7IVU.js +2 -0
  14. package/dist/chunks/{chunk-TOVTN5Y2.js → chunk-QFULZNHL.js} +1 -1
  15. package/dist/chunks/{chunk-5K4PS7IV.js → chunk-QJJWGNJP.js} +2 -2
  16. package/dist/chunks/{chunk-LWUSYIQ4.js → chunk-QULPY6YM.js} +1 -1
  17. package/dist/chunks/chunk-XWTZ4RTR.js +2 -0
  18. package/dist/chunks/chunk-ZRIM4R4A.js +2 -0
  19. package/dist/descriptive/factanal.d.ts +56 -0
  20. package/dist/descriptive/factanal.js +186 -0
  21. package/dist/descriptive/omega.d.ts +56 -0
  22. package/dist/descriptive/omega.js +323 -0
  23. package/dist/descriptive.js +2 -2
  24. package/dist/distributions/bvnorm.d.ts +15 -0
  25. package/dist/distributions/bvnorm.js +131 -0
  26. package/dist/distributions/nbeta.js +34 -1
  27. package/dist/distributions/nchisq.d.ts +10 -0
  28. package/dist/distributions/nchisq.js +46 -0
  29. package/dist/distributions.js +2 -2
  30. package/dist/entries/distributions.d.ts +1 -0
  31. package/dist/entries/distributions.js +1 -0
  32. package/dist/entries/linalg.d.ts +2 -0
  33. package/dist/entries/linalg.js +2 -0
  34. package/dist/entries/numerics.d.ts +2 -0
  35. package/dist/entries/numerics.js +1 -0
  36. package/dist/entries/tests.d.ts +22 -1
  37. package/dist/entries/tests.js +19 -1
  38. package/dist/htest/agreement.d.ts +141 -0
  39. package/dist/htest/agreement.js +274 -0
  40. package/dist/htest/aov.d.ts +19 -0
  41. package/dist/htest/aov.js +22 -15
  42. package/dist/htest/core/simulate.d.ts +2 -0
  43. package/dist/htest/core/simulate.js +1 -1
  44. package/dist/htest/dunnett.d.ts +5 -0
  45. package/dist/htest/dunnett.js +89 -0
  46. package/dist/htest/effectSize.d.ts +112 -0
  47. package/dist/htest/effectSize.js +385 -0
  48. package/dist/htest/leveneTest.d.ts +30 -0
  49. package/dist/htest/leveneTest.js +41 -0
  50. package/dist/htest/multipleComparisons.d.ts +147 -0
  51. package/dist/htest/multipleComparisons.js +369 -0
  52. package/dist/htest/normalityTests.d.ts +44 -0
  53. package/dist/htest/normalityTests.js +164 -0
  54. package/dist/htest/polycor.d.ts +117 -0
  55. package/dist/htest/polycor.js +468 -0
  56. package/dist/htest/power.d.ts +132 -0
  57. package/dist/htest/power.js +227 -0
  58. package/dist/htest/pwr.d.ts +263 -0
  59. package/dist/htest/pwr.js +440 -0
  60. package/dist/htest/rankTests.d.ts +7 -0
  61. package/dist/htest/rankTests.js +11 -1
  62. package/dist/htest/trendTests.d.ts +57 -0
  63. package/dist/htest/trendTests.js +167 -0
  64. package/dist/linalg/eigen.d.ts +8 -0
  65. package/dist/linalg/eigen.js +5 -4
  66. package/dist/linalg/expm.d.ts +48 -0
  67. package/dist/linalg/expm.js +472 -0
  68. package/dist/linalg/schur.d.ts +48 -0
  69. package/dist/linalg/schur.js +225 -0
  70. package/dist/linalg.js +2 -2
  71. package/dist/models/anovaSequential.d.ts +58 -0
  72. package/dist/models/anovaSequential.js +138 -0
  73. package/dist/models/aovEz.d.ts +64 -0
  74. package/dist/models/aovEz.js +332 -0
  75. package/dist/models/aovStrata.d.ts +32 -0
  76. package/dist/models/aovStrata.js +104 -0
  77. package/dist/models/carAnova.d.ts +30 -0
  78. package/dist/models/carAnova.js +121 -0
  79. package/dist/models/coxph.d.ts +101 -0
  80. package/dist/models/coxph.js +502 -0
  81. package/dist/models/ddLeastSquares.d.ts +30 -0
  82. package/dist/models/ddLeastSquares.js +90 -0
  83. package/dist/models/ddMatrix.d.ts +10 -0
  84. package/dist/models/ddMatrix.js +91 -0
  85. package/dist/models/formula.d.ts +83 -0
  86. package/dist/models/formula.js +333 -0
  87. package/dist/models/lm.d.ts +84 -3
  88. package/dist/models/lm.js +141 -44
  89. package/dist/models/mauchly.d.ts +48 -0
  90. package/dist/models/mauchly.js +123 -0
  91. package/dist/models/modelFrame.d.ts +40 -0
  92. package/dist/models/modelFrame.js +131 -0
  93. package/dist/models/modelMatrix.d.ts +88 -0
  94. package/dist/models/modelMatrix.js +170 -0
  95. package/dist/models/survival.d.ts +87 -0
  96. package/dist/models/survival.js +238 -0
  97. package/dist/numerics.js +2 -2
  98. package/dist/optim/lbfgsb.d.ts +20 -0
  99. package/dist/optim/lbfgsb.js +1240 -0
  100. package/dist/optim/optim.d.ts +85 -0
  101. package/dist/optim/optim.js +610 -0
  102. package/dist/rstats.js +19 -4
  103. package/dist/special.js +2 -2
  104. package/dist/tests.js +18 -4
  105. package/llms.txt +102 -0
  106. package/package.json +3 -2
  107. package/dist/chunks/chunk-24U4DKRY.js +0 -2
  108. package/dist/chunks/chunk-IVOBWYLV.js +0 -2
  109. package/dist/chunks/chunk-JLSTEMSN.js +0 -2
  110. package/dist/chunks/chunk-MF4VBCHQ.js +0 -2
  111. package/dist/chunks/chunk-MSAUU6PY.js +0 -2
  112. package/dist/chunks/chunk-ZRO446BP.js +0 -2
@@ -0,0 +1,440 @@
1
+ // Power calculations of the pwr package (Cohen's effect sizes): pwr.t.test, pwr.t2n.test, pwr.2p.test,
2
+ // pwr.2p2n.test, pwr.p.test, pwr.norm.test, pwr.r.test, pwr.chisq.test, pwr.anova.test, pwr.f2.test, and the
3
+ // effect-size helpers ES.h, ES.w1, ES.w2 and cohen.ES.
4
+ //
5
+ // Written for rStats.js from oracle/effectsize-behaviour.md (no ported code; pwr's source was not read). The quantity
6
+ // given as null is solved for with uniroot (src/numerics/integrate.ts) on pwr's intervals with uniroot's default
7
+ // tolerance and no extension of the interval, so that the tolerance-limited root is pwr's.
8
+ import { pchisq, qchisq } from "../distributions/chisq.js";
9
+ import { pf, qf } from "../distributions/f.js";
10
+ import { pnorm, qnorm } from "../distributions/normal.js";
11
+ import { qt } from "../distributions/t.js";
12
+ import { checkExactlyOne, isUnknown, matchChoice, ptNcp, solveFor } from "./power.js";
13
+ const COHEN = {
14
+ p: [0.2, 0.5, 0.8],
15
+ t: [0.2, 0.5, 0.8],
16
+ r: [0.1, 0.3, 0.5],
17
+ anov: [0.1, 0.25, 0.4],
18
+ chisq: [0.1, 0.3, 0.5],
19
+ f2: [0.02, 0.15, 0.35],
20
+ };
21
+ /**
22
+ * Cohen's conventional effect sizes. Equivalent to pwr's `cohen.ES(test, size)`.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * cohenES("anov", "medium").effectSize; // 0.25
27
+ * ```
28
+ */
29
+ export function cohenES(test = "p", size = "small") {
30
+ const t = matchChoice(test, ["p", "t", "r", "anov", "chisq", "f2"]);
31
+ const s = matchChoice(size, ["small", "medium", "large"]);
32
+ return { test: t, size: s, effectSize: COHEN[t][["small", "medium", "large"].indexOf(s)], method: "Conventional effect size from Cohen (1982)" };
33
+ }
34
+ /** Effect size h of two proportions: `2 asin(sqrt(p1)) - 2 asin(sqrt(p2))`. Equivalent to pwr's `ES.h`. */
35
+ export function esH(p1, p2) {
36
+ return 2 * Math.asin(Math.sqrt(p1)) - 2 * Math.asin(Math.sqrt(p2));
37
+ }
38
+ /** Effect size w of a goodness-of-fit test, `sqrt(sum((p1 - p0)^2 / p0))`. Equivalent to pwr's `ES.w1`. */
39
+ export function esW1(p0, p1) {
40
+ if (p0.length !== p1.length)
41
+ throw new RangeError("'P0' and 'P1' must have the same length");
42
+ let s = 0;
43
+ for (let i = 0; i < p0.length; i++)
44
+ s += (p1[i] - p0[i]) ** 2 / p0[i];
45
+ return Math.sqrt(s);
46
+ }
47
+ /**
48
+ * Effect size w of a test of association, for a matrix of cell probabilities (an array of rows): with the row sums
49
+ * r and column sums c, `p0 = r c'` and `sqrt(sum((p - p0)^2 / p0))`. Equivalent to pwr's `ES.w2`.
50
+ */
51
+ export function esW2(p) {
52
+ const rows = p.map((r) => r.reduce((a, b) => a + b, 0));
53
+ const nc = p[0]?.length ?? 0;
54
+ const cols = Array.from({ length: nc }, (_, j) => p.reduce((a, r) => a + r[j], 0));
55
+ let s = 0;
56
+ // column-major order, as R sums a matrix
57
+ for (let j = 0; j < nc; j++) {
58
+ for (let i = 0; i < p.length; i++) {
59
+ const p0 = rows[i] * cols[j];
60
+ s += (p[i][j] - p0) ** 2 / p0;
61
+ }
62
+ }
63
+ return Math.sqrt(s);
64
+ }
65
+ const SIG_MESSAGE = "‘sig.level’ must be numeric in [0, 1]";
66
+ const POWER_MESSAGE = "‘power’ must be numeric in [0, 1]";
67
+ function checkProbabilities(sigLevel, power) {
68
+ const bad = (v) => !isUnknown(v) && !(typeof v === "number" && v >= 0 && v <= 1);
69
+ if (bad(sigLevel))
70
+ throw new RangeError(SIG_MESSAGE);
71
+ if (bad(power))
72
+ throw new RangeError(POWER_MESSAGE);
73
+ }
74
+ /** A string effect size replaced by Cohen's value for the test */
75
+ function effect(es, test) {
76
+ return typeof es === "string" ? cohenES(test, es).effectSize : es;
77
+ }
78
+ const ALTERNATIVES = ["two.sided", "less", "greater"];
79
+ /** The interval of an effect size: two-sided, less, greater */
80
+ function esInterval(alt, two) {
81
+ return alt === "two.sided" ? two : alt === "less" ? [-10, 5] : [-5, 10];
82
+ }
83
+ /** The power of a t test with nu degrees of freedom and noncentrality ncp, by pwr's rejection regions */
84
+ function tPower(nu, ncp, sig, alt) {
85
+ if (alt === "less")
86
+ return ptNcp(qt(sig, nu), nu, ncp, true);
87
+ if (alt === "greater")
88
+ return ptNcp(qt(sig, nu, { lowerTail: false }), nu, ncp, false);
89
+ const q = qt(sig / 2, nu, { lowerTail: false });
90
+ return ptNcp(q, nu, ncp, false) + ptNcp(-q, nu, ncp, true);
91
+ }
92
+ /** The power of a normal test with shift `es * scale`, by pwr's rejection regions */
93
+ function normalPower(shift, sig, alt) {
94
+ if (alt === "greater")
95
+ return pnorm(qnorm(1 - sig) - shift, { lowerTail: false });
96
+ if (alt === "less")
97
+ return pnorm(qnorm(sig) - shift);
98
+ return (pnorm(qnorm(1 - sig / 2) - shift, { lowerTail: false }) + pnorm(qnorm(sig / 2) - shift));
99
+ }
100
+ /**
101
+ * Power of a t test of means, or the sample size, effect size or significance level for a given power. Equivalent
102
+ * to pwr's `pwr.t.test()`.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * pwrTTest({ n: 20, d: 0.5 }).power; // 0.3379390289250411
107
+ * pwrTTest({ d: 0.5, power: 0.8 }).n; // 63.76561044415068
108
+ * ```
109
+ */
110
+ export function pwrTTest(options = {}) {
111
+ let { n, power } = options;
112
+ let d = effect(options.d, "t");
113
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
114
+ checkExactlyOne([n, d, power, sigLevel], "exactly one of n, d, power, and sig.level must be NULL");
115
+ checkProbabilities(sigLevel, power);
116
+ const type = matchChoice(options.type, ["two.sample", "one.sample", "paired"]);
117
+ const alt = matchChoice(options.alternative, ALTERNATIVES);
118
+ const ts = type === "two.sample" ? 2 : 1;
119
+ if (alt === "two.sided" && !isUnknown(d))
120
+ d = Math.abs(d);
121
+ const powerOf = (n, d, sig) => tPower((n - 1) * ts, Math.sqrt(n / ts) * d, sig, alt);
122
+ if (isUnknown(power))
123
+ power = powerOf(n, d, sigLevel);
124
+ else {
125
+ const target = power;
126
+ if (isUnknown(n))
127
+ n = solveFor((x) => powerOf(x, d, sigLevel) - target, 2 + 1e-10, 1e9);
128
+ else if (isUnknown(d))
129
+ d = solveFor((x) => powerOf(n, x, sigLevel) - target, ...esInterval(alt, [1e-7, 10]));
130
+ else
131
+ sigLevel = solveFor((x) => powerOf(n, d, x) - target, 1e-10, 1 - 1e-10);
132
+ }
133
+ const note = type === "paired" ? "n is number of *pairs*" : type === "two.sample" ? "n is number in *each* group" : undefined;
134
+ const method = { "two.sample": "Two-sample", "one.sample": "One-sample", paired: "Paired" }[type] + " t test power calculation";
135
+ return { n: n, d: d, sigLevel: sigLevel, power, alternative: alt, ...(note === undefined ? {} : { note }), method };
136
+ }
137
+ /**
138
+ * Power of a two-sample t test with unequal sample sizes, or a sample size, the effect size or the significance
139
+ * level for a given power. Equivalent to pwr's `pwr.t2n.test()`.
140
+ */
141
+ export function pwrT2nTest(options = {}) {
142
+ let { n1, n2, power } = options;
143
+ let d = effect(options.d, "t");
144
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
145
+ checkExactlyOne([n1, n2, d, power, sigLevel], "exactly one of n1, n2, d, power, and sig.level must be NULL");
146
+ checkProbabilities(sigLevel, power);
147
+ if (!isUnknown(n1) && n1 < 2)
148
+ throw new RangeError("number of observations in the first group must be at least 2");
149
+ if (!isUnknown(n2) && n2 < 2)
150
+ throw new RangeError("number of observations in the second group must be at least 2");
151
+ const alt = matchChoice(options.alternative, ALTERNATIVES);
152
+ if (alt === "two.sided" && !isUnknown(d))
153
+ d = Math.abs(d);
154
+ const powerOf = (n1, n2, d, sig) => tPower(n1 + n2 - 2, d / Math.sqrt(1 / n1 + 1 / n2), sig, alt);
155
+ if (isUnknown(power))
156
+ power = powerOf(n1, n2, d, sigLevel);
157
+ else {
158
+ const target = power;
159
+ if (isUnknown(n1))
160
+ n1 = solveFor((x) => powerOf(x, n2, d, sigLevel) - target, 2 + 1e-10, 1e9);
161
+ else if (isUnknown(n2))
162
+ n2 = solveFor((x) => powerOf(n1, x, d, sigLevel) - target, 2 + 1e-10, 1e9);
163
+ else if (isUnknown(d))
164
+ d = solveFor((x) => powerOf(n1, n2, x, sigLevel) - target, ...esInterval(alt, [1e-7, 10]));
165
+ else
166
+ sigLevel = solveFor((x) => powerOf(n1, n2, d, x) - target, 1e-10, 1 - 1e-10);
167
+ }
168
+ return { n1: n1, n2: n2, d: d, sigLevel: sigLevel, power, alternative: alt, method: "t test power calculation" };
169
+ }
170
+ /** The normal-approximation tests: pwr.2p.test (scale sqrt(n/2)), pwr.p.test and pwr.norm.test (sqrt(n)) */
171
+ function normalTest(es0, options, name, scale, nLower, esLower) {
172
+ let { n, power } = options;
173
+ let es = effect(es0, name === "h" ? "p" : "t");
174
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
175
+ checkExactlyOne([es, n, power, sigLevel], `exactly one of ${name}, n, power, and sig.level must be NULL`);
176
+ checkProbabilities(sigLevel, power);
177
+ if (!isUnknown(n) && n < 1)
178
+ throw new RangeError("number of observations in each group must be at least 1");
179
+ const alt = matchChoice(options.alternative, ALTERNATIVES);
180
+ if (alt === "two.sided" && !isUnknown(es))
181
+ es = Math.abs(es);
182
+ const powerOf = (es, n, sig) => normalPower(es * scale(n), sig, alt);
183
+ if (isUnknown(power))
184
+ power = powerOf(es, n, sigLevel);
185
+ else {
186
+ const target = power;
187
+ if (isUnknown(es))
188
+ es = solveFor((x) => powerOf(x, n, sigLevel) - target, ...esInterval(alt, [esLower, 10]));
189
+ else if (isUnknown(n))
190
+ n = solveFor((x) => powerOf(es, x, sigLevel) - target, nLower, 1e9);
191
+ else
192
+ sigLevel = solveFor((x) => powerOf(es, n, x) - target, 1e-10, 1 - 1e-10);
193
+ }
194
+ return { es: es, n: n, sigLevel: sigLevel, power, alternative: alt };
195
+ }
196
+ /**
197
+ * Power of the test of two proportions with equal sample sizes (arcsine transformation), or the effect size, sample
198
+ * size or significance level for a given power. Equivalent to pwr's `pwr.2p.test()`.
199
+ */
200
+ export function pwr2pTest(options = {}) {
201
+ const r = normalTest(options.h, options, "h", (n) => Math.sqrt(n / 2), 2 + 1e-10, 1e-10);
202
+ return {
203
+ h: r.es,
204
+ n: r.n,
205
+ sigLevel: r.sigLevel,
206
+ power: r.power,
207
+ alternative: r.alternative,
208
+ method: "Difference of proportion power calculation for binomial distribution (arcsine transformation)",
209
+ note: "same sample sizes",
210
+ };
211
+ }
212
+ /**
213
+ * Power of the test of one proportion (arcsine transformation), or the effect size, sample size or significance
214
+ * level for a given power. Equivalent to pwr's `pwr.p.test()`.
215
+ */
216
+ export function pwrPTest(options = {}) {
217
+ const r = normalTest(options.h, options, "h", (n) => Math.sqrt(n), 2 + 1e-10, 1e-10);
218
+ return {
219
+ h: r.es,
220
+ n: r.n,
221
+ sigLevel: r.sigLevel,
222
+ power: r.power,
223
+ alternative: r.alternative,
224
+ method: "proportion power calculation for binomial distribution (arcsine transformation)",
225
+ };
226
+ }
227
+ /**
228
+ * Power of the test of a mean of a normal distribution with known variance, or the effect size, sample size or
229
+ * significance level for a given power. Equivalent to pwr's `pwr.norm.test()`.
230
+ */
231
+ export function pwrNormTest(options = {}) {
232
+ const r = normalTest(options.d, options, "d", (n) => Math.sqrt(n), 1 + 1e-10, 1e-10);
233
+ return {
234
+ d: r.es,
235
+ n: r.n,
236
+ sigLevel: r.sigLevel,
237
+ power: r.power,
238
+ alternative: r.alternative,
239
+ method: "Mean power calculation for normal distribution with known variance",
240
+ };
241
+ }
242
+ /**
243
+ * Power of the test of two proportions with unequal sample sizes (arcsine transformation), or the effect size, a
244
+ * sample size or the significance level for a given power. Equivalent to pwr's `pwr.2p2n.test()`.
245
+ */
246
+ export function pwr2p2nTest(options = {}) {
247
+ let { n1, n2, power } = options;
248
+ let h = effect(options.h, "p");
249
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
250
+ checkExactlyOne([h, n1, n2, power, sigLevel], "exactly one of h, n1, n2, power, and sig.level must be NULL");
251
+ checkProbabilities(sigLevel, power);
252
+ if (!isUnknown(n1) && n1 < 2)
253
+ throw new RangeError("number of observations in the first group must be at least 2");
254
+ if (!isUnknown(n2) && n2 < 2)
255
+ throw new RangeError("number of observations in the second group must be at least 2");
256
+ const alt = matchChoice(options.alternative, ALTERNATIVES);
257
+ if (alt === "two.sided" && !isUnknown(h))
258
+ h = Math.abs(h);
259
+ const powerOf = (h, n1, n2, sig) => normalPower(h * Math.sqrt((n1 * n2) / (n1 + n2)), sig, alt);
260
+ if (isUnknown(power))
261
+ power = powerOf(h, n1, n2, sigLevel);
262
+ else {
263
+ const target = power;
264
+ if (isUnknown(h))
265
+ h = solveFor((x) => powerOf(x, n1, n2, sigLevel) - target, ...esInterval(alt, [1e-10, 10]));
266
+ else if (isUnknown(n1))
267
+ n1 = solveFor((x) => powerOf(h, x, n2, sigLevel) - target, 2 + 1e-10, 1e9);
268
+ else if (isUnknown(n2))
269
+ n2 = solveFor((x) => powerOf(h, n1, x, sigLevel) - target, 2 + 1e-10, 1e9);
270
+ else
271
+ sigLevel = solveFor((x) => powerOf(h, n1, n2, x) - target, 1e-10, 1 - 1e-10);
272
+ }
273
+ return {
274
+ h: h,
275
+ n1: n1,
276
+ n2: n2,
277
+ sigLevel: sigLevel,
278
+ power,
279
+ alternative: alt,
280
+ method: "difference of proportion power calculation for binomial distribution (arcsine transformation)",
281
+ note: "different sample sizes",
282
+ };
283
+ }
284
+ /**
285
+ * Power of the test of a correlation (Fisher's z transformation), or the sample size, correlation or significance
286
+ * level for a given power. Equivalent to pwr's `pwr.r.test()`. As in pwr, the power computed for `"less"` reports
287
+ * `r` negated.
288
+ */
289
+ export function pwrRTest(options = {}) {
290
+ let { n, power } = options;
291
+ let r = effect(options.r, "r");
292
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
293
+ checkExactlyOne([n, r, power, sigLevel], "exactly one of n, r, power, and sig.level must be NULL");
294
+ checkProbabilities(sigLevel, power);
295
+ if (!isUnknown(n) && n < 4)
296
+ throw new RangeError("number of observations must be at least 4");
297
+ const alt = matchChoice(options.alternative, ALTERNATIVES);
298
+ if (alt === "two.sided" && !isUnknown(r))
299
+ r = Math.abs(r);
300
+ const upper = (n, r, sig, twoSided) => {
301
+ const t = qt(twoSided ? sig / 2 : sig, n - 2, { lowerTail: false });
302
+ const rc = Math.sqrt(t ** 2 / (t ** 2 + n - 2));
303
+ const zr = Math.atanh(r) + r / (2 * (n - 1));
304
+ const zrc = Math.atanh(rc);
305
+ const p = pnorm((zr - zrc) * Math.sqrt(n - 3));
306
+ return twoSided ? p + pnorm((-zr - zrc) * Math.sqrt(n - 3)) : p;
307
+ };
308
+ const powerOf = (n, r, sig) => alt === "two.sided" ? upper(n, r, sig, true) : upper(n, alt === "less" ? -r : r, sig, false);
309
+ if (isUnknown(power)) {
310
+ power = powerOf(n, r, sigLevel);
311
+ if (alt === "less")
312
+ r = -r;
313
+ }
314
+ else {
315
+ const target = power;
316
+ if (isUnknown(n))
317
+ n = solveFor((x) => powerOf(x, r, sigLevel) - target, 4 + 1e-10, 1e9);
318
+ else if (isUnknown(r)) {
319
+ const [lo, hi] = alt === "two.sided" ? [1e-10, 1 - 1e-10] : [-1 + 1e-10, 1 - 1e-10];
320
+ r = solveFor((x) => powerOf(n, x, sigLevel) - target, lo, hi);
321
+ }
322
+ else
323
+ sigLevel = solveFor((x) => powerOf(n, r, x) - target, 1e-10, 1 - 1e-10);
324
+ }
325
+ return {
326
+ n: n,
327
+ r: r,
328
+ sigLevel: sigLevel,
329
+ power,
330
+ alternative: alt,
331
+ method: "approximate correlation power calculation (arctangh transformation)",
332
+ };
333
+ }
334
+ /**
335
+ * Power of a chi-squared test, or the effect size, number of observations or significance level for a given power.
336
+ * Equivalent to pwr's `pwr.chisq.test()`.
337
+ */
338
+ export function pwrChisqTest(options) {
339
+ let { N, power } = options;
340
+ const { df } = options;
341
+ let w = effect(options.w, "chisq");
342
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
343
+ checkExactlyOne([w, N, power, sigLevel], "exactly one of w, N, power, and sig.level must be NULL");
344
+ if (typeof df !== "number")
345
+ throw new TypeError('argument "df" is missing, with no default');
346
+ checkProbabilities(sigLevel, power);
347
+ if (!isUnknown(w) && w < 0)
348
+ throw new RangeError("w must be positive");
349
+ if (!isUnknown(N) && N < 1)
350
+ throw new RangeError("number of observations must be at least 1");
351
+ const powerOf = (w, N, sig) => pchisq(qchisq(sig, df, { lowerTail: false }), df, { ncp: N * w ** 2, lowerTail: false });
352
+ if (isUnknown(power))
353
+ power = powerOf(w, N, sigLevel);
354
+ else {
355
+ const target = power;
356
+ if (isUnknown(w))
357
+ w = solveFor((x) => powerOf(x, N, sigLevel) - target, 1e-10, 1e9);
358
+ else if (isUnknown(N))
359
+ N = solveFor((x) => powerOf(w, x, sigLevel) - target, 1 + 1e-10, 1e9);
360
+ else
361
+ sigLevel = solveFor((x) => powerOf(w, N, x) - target, 1e-10, 1 - 1e-10);
362
+ }
363
+ return { w: w, N: N, df, sigLevel: sigLevel, power, method: "Chi squared power calculation", note: "N is the number of observations" };
364
+ }
365
+ /**
366
+ * Power of the F test of a balanced one-way analysis of variance, or the number of groups, sample size, effect size
367
+ * or significance level for a given power. Equivalent to pwr's `pwr.anova.test()`.
368
+ */
369
+ export function pwrAnovaTest(options = {}) {
370
+ let { k, n, power } = options;
371
+ let f = effect(options.f, "anov");
372
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
373
+ checkExactlyOne([k, n, f, power, sigLevel], "exactly one of k, n, f, power, and sig.level must be NULL");
374
+ checkProbabilities(sigLevel, power);
375
+ if (!isUnknown(f) && f < 0)
376
+ throw new RangeError("f must be positive");
377
+ if (!isUnknown(k) && k < 2)
378
+ throw new RangeError("number of groups must be at least 2");
379
+ if (!isUnknown(n) && n < 2)
380
+ throw new RangeError("number of observations in each group must be at least 2");
381
+ const powerOf = (k, n, f, sig) => {
382
+ const df1 = k - 1;
383
+ const df2 = (n - 1) * k;
384
+ return pf(qf(sig, df1, df2, { lowerTail: false }), df1, df2, { ncp: k * n * f ** 2, lowerTail: false });
385
+ };
386
+ if (isUnknown(power))
387
+ power = powerOf(k, n, f, sigLevel);
388
+ else {
389
+ const target = power;
390
+ if (isUnknown(k))
391
+ k = solveFor((x) => powerOf(x, n, f, sigLevel) - target, 2 + 1e-10, 100);
392
+ else if (isUnknown(n))
393
+ n = solveFor((x) => powerOf(k, x, f, sigLevel) - target, 2 + 1e-10, 1e9);
394
+ else if (isUnknown(f))
395
+ f = solveFor((x) => powerOf(k, n, x, sigLevel) - target, 1e-7, 1e7);
396
+ else
397
+ sigLevel = solveFor((x) => powerOf(k, n, f, x) - target, 1e-10, 1 - 1e-10);
398
+ }
399
+ return {
400
+ k: k,
401
+ n: n,
402
+ f: f,
403
+ sigLevel: sigLevel,
404
+ power,
405
+ note: "n is number in each group",
406
+ method: "Balanced one-way analysis of variance power calculation",
407
+ };
408
+ }
409
+ /**
410
+ * Power of the F test of a general linear model, or the degrees of freedom, effect size or significance level for a
411
+ * given power. Equivalent to pwr's `pwr.f2.test()`.
412
+ */
413
+ export function pwrF2Test(options = {}) {
414
+ let { u, v, power } = options;
415
+ let f2 = effect(options.f2, "f2");
416
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
417
+ checkExactlyOne([u, v, f2, power, sigLevel], "exactly one of u, v, f2, power, and sig.level must be NULL");
418
+ checkProbabilities(sigLevel, power);
419
+ if (!isUnknown(f2) && f2 < 0)
420
+ throw new RangeError("f2 must be positive");
421
+ if (!isUnknown(u) && u < 1)
422
+ throw new RangeError("degree of freedom u for numerator must be at least 1");
423
+ if (!isUnknown(v) && v < 1)
424
+ throw new RangeError("degree of freedom v for denominator must be at least 1");
425
+ const powerOf = (u, v, f2, sig) => pf(qf(sig, u, v, { lowerTail: false }), u, v, { ncp: f2 * (u + v + 1), lowerTail: false });
426
+ if (isUnknown(power))
427
+ power = powerOf(u, v, f2, sigLevel);
428
+ else {
429
+ const target = power;
430
+ if (isUnknown(u))
431
+ u = solveFor((x) => powerOf(x, v, f2, sigLevel) - target, 1 + 1e-10, 100);
432
+ else if (isUnknown(v))
433
+ v = solveFor((x) => powerOf(u, x, f2, sigLevel) - target, 1 + 1e-10, 1e9);
434
+ else if (isUnknown(f2))
435
+ f2 = solveFor((x) => powerOf(u, v, x, sigLevel) - target, 1e-7, 1e7);
436
+ else
437
+ sigLevel = solveFor((x) => powerOf(u, v, f2, x) - target, 1e-10, 1 - 1e-10);
438
+ }
439
+ return { u: u, v: v, f2: f2, sigLevel: sigLevel, power, method: "Multiple regression power calculation" };
440
+ }
@@ -20,6 +20,13 @@ export declare function kruskalTest(x: Sample | readonly Sample[], g?: readonly
20
20
  * @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
21
21
  */
22
22
  export declare function friedmanTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
23
+ /**
24
+ * Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to DescTools'
25
+ * `CochranQTest()` with a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
26
+ *
27
+ * @param y Blocks as rows, treatments as columns, values 0 or 1. Rows with a missing value are dropped.
28
+ */
29
+ export declare function cochranQTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
23
30
  /** Result of {@link quadeTest}: an F statistic with two degrees of freedom. */
24
31
  export interface QuadeTestResult extends Omit<HTest, "alternative" | "parameter" | "parameterName"> {
25
32
  /** `[num df, denom df]`. */
@@ -1,4 +1,5 @@
1
- // Rank-based k-sample and two-sample tests: R's kruskal.test, friedman.test, quade.test, fligner.test and mood.test.
1
+ // Rank-based k-sample and two-sample tests: R's kruskal.test, friedman.test, quade.test, fligner.test and mood.test,
2
+ // and DescTools' CochranQTest (the Friedman test of a 0/1 matrix, from oracle/rm-tests-behaviour.md).
2
3
  //
3
4
  // Written for rStats.js from the published statistics (no ported code), with ties handled by average ranks:
4
5
  // - Kruskal and Wallis (JASA 47, 1952): H = (12 / (N (N + 1)) sum R_i^2 / n_i - 3 (N + 1)) / (1 - sum (t^3 - t) /
@@ -91,6 +92,15 @@ export function friedmanTest(y) {
91
92
  const stat = (12 * ss) / (n * k * (k + 1) - ties / (k - 1));
92
93
  return chisqResult("Friedman rank sum test", "Friedman chi-squared", stat, k - 1);
93
94
  }
95
+ /**
96
+ * Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to DescTools'
97
+ * `CochranQTest()` with a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
98
+ *
99
+ * @param y Blocks as rows, treatments as columns, values 0 or 1. Rows with a missing value are dropped.
100
+ */
101
+ export function cochranQTest(y) {
102
+ return { ...friedmanTest(y), method: "Cochran's Q test", statisticName: "Q" };
103
+ }
94
104
  /**
95
105
  * Quade test for an unreplicated complete block design. Equivalent to R's `quade.test()` with a matrix.
96
106
  *
@@ -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;