@numero-uno-jpn/rstats 0.8.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 (122) hide show
  1. package/CHANGELOG.md +88 -0
  2. package/README.ja.md +89 -13
  3. package/README.md +90 -13
  4. package/THIRD_PARTY_NOTICES.md +12 -2
  5. package/dist/chunks/{chunk-OHPHNE3O.js → chunk-2XKG3DX4.js} +1 -1
  6. package/dist/chunks/{chunk-3I6EKGB6.js → chunk-4NM37MGA.js} +2 -2
  7. package/dist/chunks/chunk-7HPVUVZB.js +2 -0
  8. package/dist/chunks/{chunk-I3LAZE6C.js → chunk-AECGWZHD.js} +2 -2
  9. package/dist/chunks/chunk-ENL43FNO.js +2 -0
  10. package/dist/chunks/chunk-EWC6I75B.js +2 -0
  11. package/dist/chunks/chunk-IT354SHV.js +3 -0
  12. package/dist/chunks/{chunk-VIPSPG3Y.js → chunk-LIMZKONM.js} +2 -2
  13. package/dist/chunks/chunk-O6PT7IVU.js +2 -0
  14. package/dist/chunks/{chunk-YGWPU67I.js → chunk-QFULZNHL.js} +1 -1
  15. package/dist/chunks/{chunk-XU4BGFZ6.js → chunk-QJJWGNJP.js} +2 -2
  16. package/dist/chunks/{chunk-32PRRONF.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 +25 -1
  37. package/dist/entries/tests.js +22 -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/glm.d.ts +226 -0
  88. package/dist/models/glm.js +546 -0
  89. package/dist/models/glmAnova.d.ts +31 -0
  90. package/dist/models/glmAnova.js +146 -0
  91. package/dist/models/glmConfint.d.ts +25 -0
  92. package/dist/models/glmConfint.js +139 -0
  93. package/dist/models/glmFamily.d.ts +66 -0
  94. package/dist/models/glmFamily.js +359 -0
  95. package/dist/models/lm.d.ts +84 -3
  96. package/dist/models/lm.js +141 -44
  97. package/dist/models/mauchly.d.ts +48 -0
  98. package/dist/models/mauchly.js +123 -0
  99. package/dist/models/modelFrame.d.ts +40 -0
  100. package/dist/models/modelFrame.js +131 -0
  101. package/dist/models/modelMatrix.d.ts +88 -0
  102. package/dist/models/modelMatrix.js +170 -0
  103. package/dist/models/survival.d.ts +87 -0
  104. package/dist/models/survival.js +238 -0
  105. package/dist/numerics.js +2 -2
  106. package/dist/optim/lbfgsb.d.ts +20 -0
  107. package/dist/optim/lbfgsb.js +1240 -0
  108. package/dist/optim/optim.d.ts +85 -0
  109. package/dist/optim/optim.js +610 -0
  110. package/dist/rstats.js +19 -3
  111. package/dist/special.js +2 -2
  112. package/dist/tests.js +18 -3
  113. package/llms.txt +102 -0
  114. package/package.json +3 -2
  115. package/dist/chunks/chunk-EJRCDJ76.js +0 -2
  116. package/dist/chunks/chunk-JERGQKZM.js +0 -2
  117. package/dist/chunks/chunk-JVYV4633.js +0 -2
  118. package/dist/chunks/chunk-LXMMKO26.js +0 -2
  119. package/dist/chunks/chunk-MJQLKEO5.js +0 -2
  120. package/dist/chunks/chunk-RZ7SBJEG.js +0 -2
  121. package/dist/chunks/chunk-SPQC445Q.js +0 -2
  122. package/dist/chunks/chunk-U633VFH3.js +0 -2
@@ -0,0 +1,132 @@
1
+ import { type UnirootOptions } from "../numerics/integrate.js";
2
+ /** R's match.arg: the value in full, also from an unambiguous abbreviation */
3
+ export declare function matchChoice<T extends string>(value: string | undefined, choices: readonly T[]): T;
4
+ /** Whether a quantity is to be solved for: null, or omitted when R gives it no default */
5
+ export declare const isUnknown: (v: unknown) => v is null | undefined;
6
+ /** The error when not exactly one of the quantities is unknown */
7
+ export declare function checkExactlyOne(values: readonly unknown[], message: string): void;
8
+ /** The root of f(x) = 0 by uniroot, as the power functions call it */
9
+ export declare function solveFor(f: (x: number) => number, lower: number, upper: number, options?: UnirootOptions): number;
10
+ /** pt(q, df, ncp) in one tail */
11
+ export declare const ptNcp: (q: number, df: number, ncp: number, lowerTail: boolean) => number;
12
+ /** Options for {@link powerTTest}. The quantity given as `null` (or omitted, for those without a default) is solved for. */
13
+ export interface PowerTTestOptions {
14
+ /** Number of observations (per group). */
15
+ n?: number | null;
16
+ /** True difference in means. */
17
+ delta?: number | null;
18
+ /** Standard deviation. Default `1`. */
19
+ sd?: number | null;
20
+ /** Significance level. Default `0.05`. */
21
+ sigLevel?: number | null;
22
+ /** Power of the test. */
23
+ power?: number | null;
24
+ /** `"two.sample"` (default), `"one.sample"` or `"paired"`. */
25
+ type?: "two.sample" | "one.sample" | "paired" | (string & {});
26
+ /** `"two.sided"` (default) or `"one.sided"`. */
27
+ alternative?: "two.sided" | "one.sided" | (string & {});
28
+ /** Two-sided: count the probability of rejecting in the wrong direction too. Default `false`. */
29
+ strict?: boolean;
30
+ /** Tolerance of the root finding. Default `eps^0.25`. */
31
+ tol?: number;
32
+ }
33
+ /** Result of {@link powerTTest}: R's power.htest with the solved quantity filled in. */
34
+ export interface PowerTTestResult {
35
+ n: number;
36
+ delta: number;
37
+ sd: number;
38
+ sigLevel: number;
39
+ power: number;
40
+ alternative: "two.sided" | "one.sided";
41
+ note?: string;
42
+ method: string;
43
+ }
44
+ /**
45
+ * Power of a one- or two-sample t test, or the sample size, difference, standard deviation or significance level
46
+ * for a given power. Equivalent to R's `power.t.test()`.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * powerTTest({ n: 20, delta: 1 }).power; // 0.8689528016921269
51
+ * powerTTest({ delta: 0.5, power: 0.8 }).n; // 63.765763714280546
52
+ * ```
53
+ */
54
+ export declare function powerTTest(options?: PowerTTestOptions): PowerTTestResult;
55
+ /** Options for {@link powerPropTest}. The quantity given as `null` (or omitted, for those without a default) is solved for. */
56
+ export interface PowerPropTestOptions {
57
+ /** Number of observations per group. */
58
+ n?: number | null;
59
+ /** Probability in one group. */
60
+ p1?: number | null;
61
+ /** Probability in the other group. */
62
+ p2?: number | null;
63
+ /** Significance level. Default `0.05`. */
64
+ sigLevel?: number | null;
65
+ /** Power of the test. */
66
+ power?: number | null;
67
+ /** `"two.sided"` (default) or `"one.sided"`. */
68
+ alternative?: "two.sided" | "one.sided" | (string & {});
69
+ /** Two-sided: count the probability of rejecting in the wrong direction too. Default `false`. */
70
+ strict?: boolean;
71
+ /** Tolerance of the root finding. Default `eps^0.25`. */
72
+ tol?: number;
73
+ }
74
+ /** Result of {@link powerPropTest}. */
75
+ export interface PowerPropTestResult {
76
+ n: number;
77
+ p1: number;
78
+ p2: number;
79
+ sigLevel: number;
80
+ power: number;
81
+ alternative: "two.sided" | "one.sided";
82
+ note: string;
83
+ method: string;
84
+ /** R's warnings. */
85
+ warnings: string[];
86
+ }
87
+ /**
88
+ * Power of the test of two proportions, or the sample size, a probability or the significance level for a given
89
+ * power. Equivalent to R's `power.prop.test()`.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * powerPropTest({ n: 50, p1: 0.5, p2: 0.75 }).power; // 0.7401659013313878
94
+ * ```
95
+ */
96
+ export declare function powerPropTest(options?: PowerPropTestOptions): PowerPropTestResult;
97
+ /** Options for {@link powerAnovaTest}. The quantity given as `null` (or omitted, for those without a default) is solved for. */
98
+ export interface PowerAnovaTestOptions {
99
+ /** Number of groups. */
100
+ groups?: number | null;
101
+ /** Number of observations per group. */
102
+ n?: number | null;
103
+ /** Variance between the groups' means. */
104
+ betweenVar?: number | null;
105
+ /** Variance within the groups. */
106
+ withinVar?: number | null;
107
+ /** Significance level. Default `0.05`. */
108
+ sigLevel?: number | null;
109
+ /** Power of the test. */
110
+ power?: number | null;
111
+ }
112
+ /** Result of {@link powerAnovaTest}. */
113
+ export interface PowerAnovaTestResult {
114
+ groups: number;
115
+ n: number;
116
+ betweenVar: number;
117
+ withinVar: number;
118
+ sigLevel: number;
119
+ power: number;
120
+ note: string;
121
+ method: string;
122
+ }
123
+ /**
124
+ * Power of the F test of a balanced one-way analysis of variance, or one of the other quantities for a given power.
125
+ * Equivalent to R's `power.anova.test()`.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * powerAnovaTest({ groups: 4, n: 5, betweenVar: 1, withinVar: 3 }).power; // 0.3535594237229933
130
+ * ```
131
+ */
132
+ export declare function powerAnovaTest(options?: PowerAnovaTestOptions): PowerAnovaTestResult;
@@ -0,0 +1,227 @@
1
+ // Power calculations of R's stats package: power.t.test, power.prop.test and power.anova.test.
2
+ //
3
+ // Written for rStats.js from oracle/effectsize-behaviour.md (no ported code; R's source was not read). Each function
4
+ // computes the power of a test from the other quantities, or solves for the one quantity given as null with uniroot
5
+ // (src/numerics/integrate.ts), called with R's interval, tolerance and extendInt so that the iterations, and so the
6
+ // tolerance-limited root, are R's.
7
+ import { pf, qf } from "../distributions/f.js";
8
+ import { pnorm, qnorm } from "../distributions/normal.js";
9
+ import { pt, qt } from "../distributions/t.js";
10
+ import { uniroot } from "../numerics/integrate.js";
11
+ const TOL = Number.EPSILON ** 0.25;
12
+ /** R's match.arg: the value in full, also from an unambiguous abbreviation */
13
+ export function matchChoice(value, choices) {
14
+ if (value === undefined)
15
+ return choices[0];
16
+ const exact = choices.find((c) => c === value);
17
+ if (exact !== undefined)
18
+ return exact;
19
+ const hit = choices.filter((c) => c.startsWith(value));
20
+ if (value.length === 0 || hit.length !== 1) {
21
+ throw new RangeError(`'arg' should be one of ${choices.map((c) => `“${c}”`).join(", ")}`);
22
+ }
23
+ return hit[0];
24
+ }
25
+ /** Whether a quantity is to be solved for: null, or omitted when R gives it no default */
26
+ export const isUnknown = (v) => v === null || v === undefined;
27
+ /** The error when not exactly one of the quantities is unknown */
28
+ export function checkExactlyOne(values, message) {
29
+ if (values.filter(isUnknown).length !== 1)
30
+ throw new RangeError(message);
31
+ }
32
+ /** The root of f(x) = 0 by uniroot, as the power functions call it */
33
+ export function solveFor(f, lower, upper, options = {}) {
34
+ return uniroot(f, lower, upper, options).root;
35
+ }
36
+ /** pt(q, df, ncp) in one tail */
37
+ export const ptNcp = (q, df, ncp, lowerTail) => pt(q, df, { ncp, lowerTail });
38
+ function checkSigLevel(sigLevel, message) {
39
+ if (sigLevel !== null && sigLevel !== undefined && !(typeof sigLevel === "number" && sigLevel >= 0 && sigLevel <= 1)) {
40
+ throw new RangeError(message);
41
+ }
42
+ }
43
+ const SIG_MESSAGE = "'sig.level' must be numeric in [0, 1]";
44
+ /**
45
+ * Power of a one- or two-sample t test, or the sample size, difference, standard deviation or significance level
46
+ * for a given power. Equivalent to R's `power.t.test()`.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * powerTTest({ n: 20, delta: 1 }).power; // 0.8689528016921269
51
+ * powerTTest({ delta: 0.5, power: 0.8 }).n; // 63.765763714280546
52
+ * ```
53
+ */
54
+ export function powerTTest(options = {}) {
55
+ let { n, delta, power } = options;
56
+ let sd = options.sd === undefined ? 1 : options.sd;
57
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
58
+ checkExactlyOne([n, delta, sd, power, sigLevel], "exactly one of 'n', 'delta', 'sd', 'power', and 'sig.level' must be NULL");
59
+ checkSigLevel(sigLevel, SIG_MESSAGE);
60
+ const type = matchChoice(options.type, ["two.sample", "one.sample", "paired"]);
61
+ const alternative = matchChoice(options.alternative, ["two.sided", "one.sided"]);
62
+ const strict = options.strict ?? false;
63
+ const tol = options.tol ?? TOL;
64
+ const ts = type === "two.sample" ? 2 : 1;
65
+ const sides = alternative === "two.sided" ? 2 : 1;
66
+ if (sides === 2 && !isUnknown(delta))
67
+ delta = Math.abs(delta);
68
+ const powerOf = (n, delta, sd, sig) => {
69
+ const nu = Math.max(1e-7, n - 1) * ts;
70
+ const q = qt(sig / sides, nu, { lowerTail: false });
71
+ const ncp = (Math.sqrt(n / ts) * delta) / sd;
72
+ const p = ptNcp(q, nu, ncp, false);
73
+ return strict && sides === 2 ? p + ptNcp(-q, nu, ncp, true) : p;
74
+ };
75
+ if (isUnknown(power))
76
+ power = powerOf(n, delta, sd, sigLevel);
77
+ else {
78
+ const target = power;
79
+ if (isUnknown(n))
80
+ n = solveFor((x) => powerOf(x, delta, sd, sigLevel) - target, 2, 1e7, { tol, extendInt: "upX" });
81
+ else if (isUnknown(sd)) {
82
+ sd = solveFor((x) => powerOf(n, delta, x, sigLevel) - target, delta * 1e-7, delta * 1e7, { tol, extendInt: "downX" });
83
+ }
84
+ else if (isUnknown(delta)) {
85
+ delta = solveFor((x) => powerOf(n, x, sd, sigLevel) - target, sd * 1e-7, sd * 1e7, { tol, extendInt: "upX" });
86
+ }
87
+ else {
88
+ sigLevel = solveFor((x) => powerOf(n, delta, sd, x) - target, 1e-10, 1 - 1e-10, { tol, extendInt: "yes" });
89
+ }
90
+ }
91
+ const note = type === "paired"
92
+ ? "n is number of *pairs*, sd is std.dev. of *differences* within pairs"
93
+ : type === "two.sample"
94
+ ? "n is number in *each* group"
95
+ : undefined;
96
+ const method = { "two.sample": "Two-sample", "one.sample": "One-sample", paired: "Paired" }[type] + " t test power calculation";
97
+ return {
98
+ n: n,
99
+ delta: delta,
100
+ sd: sd,
101
+ sigLevel: sigLevel,
102
+ power,
103
+ alternative,
104
+ ...(note === undefined ? {} : { note }),
105
+ method,
106
+ };
107
+ }
108
+ /**
109
+ * Power of the test of two proportions, or the sample size, a probability or the significance level for a given
110
+ * power. Equivalent to R's `power.prop.test()`.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * powerPropTest({ n: 50, p1: 0.5, p2: 0.75 }).power; // 0.7401659013313878
115
+ * ```
116
+ */
117
+ export function powerPropTest(options = {}) {
118
+ let { n, p1, p2, power } = options;
119
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
120
+ checkExactlyOne([n, p1, p2, power, sigLevel], "exactly one of 'n', 'p1', 'p2', 'power', and 'sig.level' must be NULL");
121
+ checkSigLevel(sigLevel, SIG_MESSAGE);
122
+ const alternative = matchChoice(options.alternative, ["two.sided", "one.sided"]);
123
+ const strict = options.strict ?? false;
124
+ const tol = options.tol ?? TOL;
125
+ const sides = alternative === "two.sided" ? 2 : 1;
126
+ const powerOf = (n, p1, p2, sig) => {
127
+ const z = qnorm(sig / sides, { lowerTail: false });
128
+ if (strict && sides === 2) {
129
+ const pbar = (p1 + p2) / 2;
130
+ const v1 = p1 * (1 - p1);
131
+ const v2 = p2 * (1 - p2);
132
+ const vbar = pbar * (1 - pbar);
133
+ const d = Math.abs(p1 - p2);
134
+ const s = Math.sqrt(v1 + v2);
135
+ return (pnorm((Math.sqrt(n) * d - z * Math.sqrt(2 * vbar)) / s) +
136
+ pnorm((Math.sqrt(n) * d + z * Math.sqrt(2 * vbar)) / s, { lowerTail: false }));
137
+ }
138
+ return pnorm((Math.sqrt(n) * Math.abs(p1 - p2) - z * Math.sqrt((p1 + p2) * (1 - (p1 + p2) / 2))) / Math.sqrt(p1 * (1 - p1) + p2 * (1 - p2)));
139
+ };
140
+ const warnings = [];
141
+ if (isUnknown(power))
142
+ power = powerOf(n, p1, p2, sigLevel);
143
+ else {
144
+ const target = power;
145
+ if (isUnknown(n))
146
+ n = solveFor((x) => powerOf(x, p1, p2, sigLevel) - target, 1, 1e7, { tol, extendInt: "upX" });
147
+ else if (isUnknown(p1)) {
148
+ p1 = solveFor((x) => powerOf(n, x, p2, sigLevel) - target, 0, p2, { tol, extendInt: "yes" });
149
+ if (p1 < 0)
150
+ warnings.push("No p1 in [0, p2] can be found to achieve the desired power");
151
+ }
152
+ else if (isUnknown(p2)) {
153
+ p2 = solveFor((x) => powerOf(n, p1, x, sigLevel) - target, p1, 1, { tol, extendInt: "yes" });
154
+ if (p2 > 1)
155
+ warnings.push("No p2 in [p1, 1] can be found to achieve the desired power");
156
+ }
157
+ else {
158
+ sigLevel = solveFor((x) => powerOf(n, p1, p2, x) - target, 1e-10, 1 - 1e-10, { tol, extendInt: "upX" });
159
+ if (sigLevel < 0 || sigLevel > 1)
160
+ warnings.push("No significance level [0, 1] can be found to achieve the desired power");
161
+ }
162
+ }
163
+ return {
164
+ n: n,
165
+ p1: p1,
166
+ p2: p2,
167
+ sigLevel: sigLevel,
168
+ power,
169
+ alternative,
170
+ note: "n is number in *each* group",
171
+ method: "Two-sample comparison of proportions power calculation",
172
+ warnings,
173
+ };
174
+ }
175
+ /**
176
+ * Power of the F test of a balanced one-way analysis of variance, or one of the other quantities for a given power.
177
+ * Equivalent to R's `power.anova.test()`.
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * powerAnovaTest({ groups: 4, n: 5, betweenVar: 1, withinVar: 3 }).power; // 0.3535594237229933
182
+ * ```
183
+ */
184
+ export function powerAnovaTest(options = {}) {
185
+ let { groups, n, betweenVar, withinVar, power } = options;
186
+ let sigLevel = options.sigLevel === undefined ? 0.05 : options.sigLevel;
187
+ checkExactlyOne([groups, n, betweenVar, withinVar, power, sigLevel], "exactly one of 'groups', 'n', 'between.var', 'within.var', 'power', and 'sig.level' must be NULL");
188
+ if (!isUnknown(groups) && groups < 2)
189
+ throw new RangeError("number of groups must be at least 2");
190
+ if (!isUnknown(n) && n < 2)
191
+ throw new RangeError("number of observations in each group must be at least 2");
192
+ checkSigLevel(sigLevel, SIG_MESSAGE);
193
+ const powerOf = (groups, n, between, within, sig) => {
194
+ const lambda = ((groups - 1) * n * between) / within;
195
+ const df1 = groups - 1;
196
+ const df2 = (n - 1) * groups;
197
+ return pf(qf(sig, df1, df2, { lowerTail: false }), df1, df2, { ncp: lambda, lowerTail: false });
198
+ };
199
+ if (isUnknown(power))
200
+ power = powerOf(groups, n, betweenVar, withinVar, sigLevel);
201
+ else {
202
+ const target = power;
203
+ if (isUnknown(groups))
204
+ groups = solveFor((x) => powerOf(x, n, betweenVar, withinVar, sigLevel) - target, 2, 100);
205
+ else if (isUnknown(n))
206
+ n = solveFor((x) => powerOf(groups, x, betweenVar, withinVar, sigLevel) - target, 2, 1e5);
207
+ else if (isUnknown(withinVar)) {
208
+ withinVar = solveFor((x) => powerOf(groups, n, betweenVar, x, sigLevel) - target, betweenVar * 1e-7, betweenVar * 1e7);
209
+ }
210
+ else if (isUnknown(betweenVar)) {
211
+ betweenVar = solveFor((x) => powerOf(groups, n, x, withinVar, sigLevel) - target, withinVar * 1e-7, withinVar * 1e7);
212
+ }
213
+ else {
214
+ sigLevel = solveFor((x) => powerOf(groups, n, betweenVar, withinVar, x) - target, 1e-10, 1 - 1e-10);
215
+ }
216
+ }
217
+ return {
218
+ groups: groups,
219
+ n: n,
220
+ betweenVar: betweenVar,
221
+ withinVar: withinVar,
222
+ sigLevel: sigLevel,
223
+ power,
224
+ note: "n is number in each group",
225
+ method: "Balanced one-way analysis of variance power calculation",
226
+ };
227
+ }
@@ -0,0 +1,263 @@
1
+ import type { Alternative } from "./types.js";
2
+ /** An effect size: a number, or Cohen's conventional `"small"`, `"medium"` or `"large"` */
3
+ export type EffectSize = number | "small" | "medium" | "large";
4
+ /** The tests of {@link cohenES} */
5
+ export type CohenTest = "p" | "t" | "r" | "anov" | "chisq" | "f2";
6
+ /** Result of {@link cohenES}. */
7
+ export interface CohenESResult {
8
+ test: CohenTest;
9
+ size: "small" | "medium" | "large";
10
+ effectSize: number;
11
+ method: string;
12
+ }
13
+ /**
14
+ * Cohen's conventional effect sizes. Equivalent to pwr's `cohen.ES(test, size)`.
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * cohenES("anov", "medium").effectSize; // 0.25
19
+ * ```
20
+ */
21
+ export declare function cohenES(test?: CohenTest | (string & {}), size?: "small" | "medium" | "large" | (string & {})): CohenESResult;
22
+ /** Effect size h of two proportions: `2 asin(sqrt(p1)) - 2 asin(sqrt(p2))`. Equivalent to pwr's `ES.h`. */
23
+ export declare function esH(p1: number, p2: number): number;
24
+ /** Effect size w of a goodness-of-fit test, `sqrt(sum((p1 - p0)^2 / p0))`. Equivalent to pwr's `ES.w1`. */
25
+ export declare function esW1(p0: readonly number[], p1: readonly number[]): number;
26
+ /**
27
+ * Effect size w of a test of association, for a matrix of cell probabilities (an array of rows): with the row sums
28
+ * r and column sums c, `p0 = r c'` and `sqrt(sum((p - p0)^2 / p0))`. Equivalent to pwr's `ES.w2`.
29
+ */
30
+ export declare function esW2(p: readonly (readonly number[])[]): number;
31
+ /** Common options of the pwr functions */
32
+ interface PwrCommon {
33
+ /** Significance level. Default `0.05`. */
34
+ sigLevel?: number | null;
35
+ /** Power of the test. */
36
+ power?: number | null;
37
+ }
38
+ /** Common options of the pwr functions with an alternative */
39
+ interface PwrAlternative extends PwrCommon {
40
+ /** `"two.sided"` (default), `"less"` or `"greater"`; unambiguous abbreviations are accepted. */
41
+ alternative?: Alternative | (string & {});
42
+ }
43
+ /** Options for {@link pwrTTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
44
+ export interface PwrTTestOptions extends PwrAlternative {
45
+ /** Number of observations (per group, or of pairs). */
46
+ n?: number | null;
47
+ /** Effect size d (Cohen's d), or `"small"`, `"medium"`, `"large"`. */
48
+ d?: EffectSize | null;
49
+ /** `"two.sample"` (default), `"one.sample"` or `"paired"`. */
50
+ type?: "two.sample" | "one.sample" | "paired" | (string & {});
51
+ }
52
+ /** Result of {@link pwrTTest}. */
53
+ export interface PwrTTestResult {
54
+ n: number;
55
+ d: number;
56
+ sigLevel: number;
57
+ power: number;
58
+ alternative: Alternative;
59
+ note?: string;
60
+ method: string;
61
+ }
62
+ /**
63
+ * Power of a t test of means, or the sample size, effect size or significance level for a given power. Equivalent
64
+ * to pwr's `pwr.t.test()`.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * pwrTTest({ n: 20, d: 0.5 }).power; // 0.3379390289250411
69
+ * pwrTTest({ d: 0.5, power: 0.8 }).n; // 63.76561044415068
70
+ * ```
71
+ */
72
+ export declare function pwrTTest(options?: PwrTTestOptions): PwrTTestResult;
73
+ /** Options for {@link pwrT2nTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
74
+ export interface PwrT2nTestOptions extends PwrAlternative {
75
+ /** Number of observations in the first group. */
76
+ n1?: number | null;
77
+ /** Number of observations in the second group. */
78
+ n2?: number | null;
79
+ /** Effect size d, or `"small"`, `"medium"`, `"large"`. */
80
+ d?: EffectSize | null;
81
+ }
82
+ /** Result of {@link pwrT2nTest}. */
83
+ export interface PwrT2nTestResult {
84
+ n1: number;
85
+ n2: number;
86
+ d: number;
87
+ sigLevel: number;
88
+ power: number;
89
+ alternative: Alternative;
90
+ method: string;
91
+ }
92
+ /**
93
+ * Power of a two-sample t test with unequal sample sizes, or a sample size, the effect size or the significance
94
+ * level for a given power. Equivalent to pwr's `pwr.t2n.test()`.
95
+ */
96
+ export declare function pwrT2nTest(options?: PwrT2nTestOptions): PwrT2nTestResult;
97
+ /** Options for {@link pwr2pTest} and {@link pwrPTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
98
+ export interface Pwr2pTestOptions extends PwrAlternative {
99
+ /** Effect size h ({@link esH}), or `"small"`, `"medium"`, `"large"`. */
100
+ h?: EffectSize | null;
101
+ /** Number of observations (per group for {@link pwr2pTest}). */
102
+ n?: number | null;
103
+ }
104
+ /** Result of {@link pwr2pTest} and {@link pwrPTest}. */
105
+ export interface Pwr2pTestResult {
106
+ h: number;
107
+ n: number;
108
+ sigLevel: number;
109
+ power: number;
110
+ alternative: Alternative;
111
+ method: string;
112
+ note?: string;
113
+ }
114
+ /**
115
+ * Power of the test of two proportions with equal sample sizes (arcsine transformation), or the effect size, sample
116
+ * size or significance level for a given power. Equivalent to pwr's `pwr.2p.test()`.
117
+ */
118
+ export declare function pwr2pTest(options?: Pwr2pTestOptions): Pwr2pTestResult;
119
+ /**
120
+ * Power of the test of one proportion (arcsine transformation), or the effect size, sample size or significance
121
+ * level for a given power. Equivalent to pwr's `pwr.p.test()`.
122
+ */
123
+ export declare function pwrPTest(options?: Pwr2pTestOptions): Pwr2pTestResult;
124
+ /** Options for {@link pwrNormTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
125
+ export interface PwrNormTestOptions extends PwrAlternative {
126
+ /** Effect size d (difference of means over the known standard deviation), or `"small"`, `"medium"`, `"large"`. */
127
+ d?: EffectSize | null;
128
+ /** Number of observations. */
129
+ n?: number | null;
130
+ }
131
+ /** Result of {@link pwrNormTest}. */
132
+ export interface PwrNormTestResult {
133
+ d: number;
134
+ n: number;
135
+ sigLevel: number;
136
+ power: number;
137
+ alternative: Alternative;
138
+ method: string;
139
+ }
140
+ /**
141
+ * Power of the test of a mean of a normal distribution with known variance, or the effect size, sample size or
142
+ * significance level for a given power. Equivalent to pwr's `pwr.norm.test()`.
143
+ */
144
+ export declare function pwrNormTest(options?: PwrNormTestOptions): PwrNormTestResult;
145
+ /** Options for {@link pwr2p2nTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
146
+ export interface Pwr2p2nTestOptions extends PwrAlternative {
147
+ /** Effect size h ({@link esH}), or `"small"`, `"medium"`, `"large"`. */
148
+ h?: EffectSize | null;
149
+ /** Number of observations in the first group. */
150
+ n1?: number | null;
151
+ /** Number of observations in the second group. */
152
+ n2?: number | null;
153
+ }
154
+ /** Result of {@link pwr2p2nTest}. */
155
+ export interface Pwr2p2nTestResult {
156
+ h: number;
157
+ n1: number;
158
+ n2: number;
159
+ sigLevel: number;
160
+ power: number;
161
+ alternative: Alternative;
162
+ method: string;
163
+ note: string;
164
+ }
165
+ /**
166
+ * Power of the test of two proportions with unequal sample sizes (arcsine transformation), or the effect size, a
167
+ * sample size or the significance level for a given power. Equivalent to pwr's `pwr.2p2n.test()`.
168
+ */
169
+ export declare function pwr2p2nTest(options?: Pwr2p2nTestOptions): Pwr2p2nTestResult;
170
+ /** Options for {@link pwrRTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
171
+ export interface PwrRTestOptions extends PwrAlternative {
172
+ /** Number of observations. */
173
+ n?: number | null;
174
+ /** Correlation, or `"small"`, `"medium"`, `"large"`. */
175
+ r?: EffectSize | null;
176
+ }
177
+ /** Result of {@link pwrRTest}. */
178
+ export interface PwrRTestResult {
179
+ n: number;
180
+ r: number;
181
+ sigLevel: number;
182
+ power: number;
183
+ alternative: Alternative;
184
+ method: string;
185
+ }
186
+ /**
187
+ * Power of the test of a correlation (Fisher's z transformation), or the sample size, correlation or significance
188
+ * level for a given power. Equivalent to pwr's `pwr.r.test()`. As in pwr, the power computed for `"less"` reports
189
+ * `r` negated.
190
+ */
191
+ export declare function pwrRTest(options?: PwrRTestOptions): PwrRTestResult;
192
+ /** Options for {@link pwrChisqTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
193
+ export interface PwrChisqTestOptions extends PwrCommon {
194
+ /** Effect size w ({@link esW1}, {@link esW2}), or `"small"`, `"medium"`, `"large"`. */
195
+ w?: EffectSize | null;
196
+ /** Total number of observations. */
197
+ N?: number | null;
198
+ /** Degrees of freedom. */
199
+ df: number;
200
+ }
201
+ /** Result of {@link pwrChisqTest}. */
202
+ export interface PwrChisqTestResult {
203
+ w: number;
204
+ N: number;
205
+ df: number;
206
+ sigLevel: number;
207
+ power: number;
208
+ method: string;
209
+ note: string;
210
+ }
211
+ /**
212
+ * Power of a chi-squared test, or the effect size, number of observations or significance level for a given power.
213
+ * Equivalent to pwr's `pwr.chisq.test()`.
214
+ */
215
+ export declare function pwrChisqTest(options: PwrChisqTestOptions): PwrChisqTestResult;
216
+ /** Options for {@link pwrAnovaTest}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
217
+ export interface PwrAnovaTestOptions extends PwrCommon {
218
+ /** Number of groups. */
219
+ k?: number | null;
220
+ /** Number of observations per group. */
221
+ n?: number | null;
222
+ /** Effect size f, or `"small"`, `"medium"`, `"large"`. */
223
+ f?: EffectSize | null;
224
+ }
225
+ /** Result of {@link pwrAnovaTest}. */
226
+ export interface PwrAnovaTestResult {
227
+ k: number;
228
+ n: number;
229
+ f: number;
230
+ sigLevel: number;
231
+ power: number;
232
+ note: string;
233
+ method: string;
234
+ }
235
+ /**
236
+ * Power of the F test of a balanced one-way analysis of variance, or the number of groups, sample size, effect size
237
+ * or significance level for a given power. Equivalent to pwr's `pwr.anova.test()`.
238
+ */
239
+ export declare function pwrAnovaTest(options?: PwrAnovaTestOptions): PwrAnovaTestResult;
240
+ /** Options for {@link pwrF2Test}. The quantity given as `null` (or omitted, except `sigLevel`) is solved for. */
241
+ export interface PwrF2TestOptions extends PwrCommon {
242
+ /** Degrees of freedom of the numerator. */
243
+ u?: number | null;
244
+ /** Degrees of freedom of the denominator. */
245
+ v?: number | null;
246
+ /** Effect size f², or `"small"`, `"medium"`, `"large"`. */
247
+ f2?: EffectSize | null;
248
+ }
249
+ /** Result of {@link pwrF2Test}. */
250
+ export interface PwrF2TestResult {
251
+ u: number;
252
+ v: number;
253
+ f2: number;
254
+ sigLevel: number;
255
+ power: number;
256
+ method: string;
257
+ }
258
+ /**
259
+ * Power of the F test of a general linear model, or the degrees of freedom, effect size or significance level for a
260
+ * given power. Equivalent to pwr's `pwr.f2.test()`.
261
+ */
262
+ export declare function pwrF2Test(options?: PwrF2TestOptions): PwrF2TestResult;
263
+ export {};