@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.
Files changed (59) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/README.ja.md +11 -10
  3. package/README.md +11 -10
  4. package/dist/chunks/{chunk-X4J6UJHN.js → chunk-2NNBUYHJ.js} +2 -2
  5. package/dist/chunks/{chunk-Y4YWLSWU.js → chunk-47IIPUFN.js} +2 -2
  6. package/dist/chunks/{chunk-7KJ24LYK.js → chunk-4AOTAT3O.js} +2 -2
  7. package/dist/chunks/{chunk-GRUQRNX7.js → chunk-5SGTWSCB.js} +3 -3
  8. package/dist/chunks/{chunk-YN5MLKDL.js → chunk-6FZHTD5G.js} +2 -2
  9. package/dist/chunks/{chunk-N5BDSMXS.js → chunk-73LNRWU3.js} +1 -1
  10. package/dist/chunks/{chunk-7IJCUSA5.js → chunk-7INBXTVV.js} +2 -2
  11. package/dist/chunks/chunk-7WG7X2UI.js +2 -0
  12. package/dist/chunks/{chunk-DTJG2IL2.js → chunk-A66M3ZEA.js} +2 -2
  13. package/dist/chunks/{chunk-EJYCDE4Z.js → chunk-BI7NYJWP.js} +1 -1
  14. package/dist/chunks/{chunk-JNWWLNE3.js → chunk-BKV3FINA.js} +2 -2
  15. package/dist/chunks/{chunk-UMLMY4XS.js → chunk-CQB4BJBR.js} +2 -2
  16. package/dist/chunks/{chunk-DR4GQ7PW.js → chunk-EEU7D4RU.js} +1 -1
  17. package/dist/chunks/{chunk-Z2HGLFZN.js → chunk-F3EQLL4F.js} +2 -2
  18. package/dist/chunks/{chunk-4VRMAYPY.js → chunk-HBHMEPGY.js} +2 -2
  19. package/dist/chunks/{chunk-GJEKNJP7.js → chunk-IAWXF73N.js} +2 -2
  20. package/dist/chunks/{chunk-OG7UORIX.js → chunk-IMPRIQE5.js} +1 -1
  21. package/dist/chunks/{chunk-GYGQUYEP.js → chunk-JWBSEL3V.js} +2 -2
  22. package/dist/chunks/{chunk-U7BRXOUS.js → chunk-L23RSC2N.js} +2 -2
  23. package/dist/chunks/{chunk-QUTJEYDX.js → chunk-NSU6U3ET.js} +2 -2
  24. package/dist/chunks/{chunk-4KCSBTR5.js → chunk-ORE5ZRCY.js} +2 -2
  25. package/dist/chunks/{chunk-GGJA3JRO.js → chunk-RRO4LMCB.js} +2 -2
  26. package/dist/chunks/{chunk-HZLSNJQM.js → chunk-T5DRIITM.js} +2 -2
  27. package/dist/chunks/{chunk-HUWOSLKE.js → chunk-TNETWBF5.js} +1 -1
  28. package/dist/chunks/{chunk-HRA7PCQT.js → chunk-VERBXW6E.js} +2 -2
  29. package/dist/descriptive.js +2 -2
  30. package/dist/distributions.js +2 -2
  31. package/dist/entries/tests.d.ts +5 -0
  32. package/dist/entries/tests.js +5 -0
  33. package/dist/htest/binomCI.d.ts +116 -0
  34. package/dist/htest/binomCI.js +252 -0
  35. package/dist/htest/boschlooTest.d.ts +65 -0
  36. package/dist/htest/boschlooTest.js +394 -0
  37. package/dist/htest/chisqTest.d.ts +19 -0
  38. package/dist/htest/chisqTest.js +30 -2
  39. package/dist/htest/core/twoProportions.d.ts +48 -0
  40. package/dist/htest/core/twoProportions.js +476 -0
  41. package/dist/htest/equivalence.d.ts +172 -0
  42. package/dist/htest/equivalence.js +189 -0
  43. package/dist/htest/fisherTest.d.ts +49 -0
  44. package/dist/htest/fisherTest.js +136 -13
  45. package/dist/htest/tableTests.d.ts +216 -0
  46. package/dist/htest/tableTests.js +650 -0
  47. package/dist/htest/twoByTwo.d.ts +203 -0
  48. package/dist/htest/twoByTwo.js +385 -0
  49. package/dist/linalg.js +2 -2
  50. package/dist/mixed.js +2 -2
  51. package/dist/numerics.js +2 -2
  52. package/dist/rstats.js +16 -16
  53. package/dist/special/igam.js +140 -23
  54. package/dist/special/loader.js +8 -0
  55. package/dist/special.js +2 -2
  56. package/dist/tests.js +13 -13
  57. package/llms.txt +8 -0
  58. package/package.json +1 -1
  59. package/dist/chunks/chunk-NDTJ23A6.js +0 -2
@@ -0,0 +1,216 @@
1
+ import { type Alternative, type Counts, type HTest } from "./types.js";
2
+ /**
3
+ * Freeman-Tukey deviate sqrt(O) + sqrt(O + 1) - sqrt(4E + 1) (Bishop, Fienberg and Holland 1975, 14.7). The plain
4
+ * formula cancels for large counts (sqrt(O) + sqrt(O + 1) against sqrt(4E + 1), losing eps sqrt(O) absolute); here it
5
+ * is (a^2 - b^2) / (a + b) with a = sqrt(O) + sqrt(O + 1), b = sqrt(4E + 1) and
6
+ * a^2 - b^2 = 2(O - E) + 2 ((O - E)(O + E) + O) / (sqrt(O (O + 1)) + E), whose rounding is a few eps absolute
7
+ */
8
+ export declare function freemanTukeyDeviate(O: number, E: number): number;
9
+ /** Freeman-Tukey deviates of a vector or a table (rows) */
10
+ export declare function freemanTukeyResiduals(O: readonly number[], E: readonly number[]): number[];
11
+ export declare function freemanTukeyResiduals(O: readonly (readonly number[])[], E: readonly (readonly number[])[]): number[][];
12
+ /**
13
+ * Corrections of {@link gTest}.
14
+ *
15
+ * @category Hypothesis tests
16
+ */
17
+ export type GTestCorrection = "none" | "williams" | "yates";
18
+ /**
19
+ * Options for {@link gTest}.
20
+ *
21
+ * @category Hypothesis tests
22
+ */
23
+ export interface GTestOptions {
24
+ /**
25
+ * `"none"` (default), `"williams"` (Williams' correction q of the statistic) or `"yates"` (Yates' correction of the
26
+ * counts by 0.5, 2 x 2 tables and two-category goodness of fit only). Unambiguous abbreviations are accepted.
27
+ */
28
+ correct?: GTestCorrection | (string & {});
29
+ /** Expected probabilities for the goodness-of-fit test. Default: equal probabilities. Ignored for a table. */
30
+ p?: readonly number[];
31
+ /** Rescale `p` to sum to 1. Default `false`, in which case `p` must sum to 1 (within sqrt(eps)). */
32
+ rescaleP?: boolean;
33
+ }
34
+ /**
35
+ * Result of {@link gTest}. Has no `alternative`, as DescTools' `GTest` returns none.
36
+ *
37
+ * @category Hypothesis tests
38
+ */
39
+ export interface GTestResult extends Omit<HTest, "alternative"> {
40
+ /** Degrees of freedom (`parameterName` is `"X-squared df"`, as DescTools). */
41
+ parameter: number;
42
+ /** The counts, after Yates' correction where it is applied (as DescTools); an array of rows for a table. */
43
+ observed: number[] | number[][];
44
+ /** Expected counts under the null hypothesis. */
45
+ expected: number[] | number[][];
46
+ /**
47
+ * Freeman-Tukey deviates `sqrt(O) + sqrt(O + 1) - sqrt(4E + 1)` of the counts given (without Yates' correction),
48
+ * approximately standard normal under the null hypothesis.
49
+ */
50
+ ftResiduals: number[] | number[][];
51
+ }
52
+ /**
53
+ * Log-likelihood ratio (G) test of independence in a contingency table, or of goodness of fit of counts to given
54
+ * probabilities. Equivalent to `DescTools::GTest()` in R.
55
+ *
56
+ * For a table (an array of rows), `G = 2 sum x log(x / E) / q` with `E` the products of the margins over the total and
57
+ * `(r - 1)(c - 1)` degrees of freedom, counting empty rows and columns as DescTools; for a vector (or a table with one
58
+ * row or column), `E = n p` and `k - 1` degrees of freedom. `correct: "williams"` divides by Williams' q
59
+ * (`1 + (n sum 1/R_i - 1)(n sum 1/C_j - 1) / (6 n (r - 1)(c - 1))` for a table, which is not defined with an empty
60
+ * row or column: an error, where DescTools gives G = 0 and p = 1; and `1 + (k + 1) / (6n)` for a vector). `correct: "yates"` moves the counts of a 2 x 2 table by 0.5 toward
61
+ * independence as DescTools does: the off-diagonal cells up and the diagonal down when `x11 x22 - x12 x21 > 0`, and
62
+ * the other way otherwise, also when it is 0 (so that a table of exact independence gets a positive G), and without
63
+ * stopping at 0 (a cell that becomes negative makes G NaN, with the warning "NaNs produced"). For two counts it moves
64
+ * them by 0.5 toward `E` when they differ from it by more than 0.25.
65
+ *
66
+ * The p-value is the upper tail of chi-squared (DescTools computes `1 - pchisq` for a table, which is 0 below about
67
+ * 1e-16). G is summed as a deviance, without the cancellation of the plain sum. The result also has the Freeman-Tukey
68
+ * deviates `ftResiduals`.
69
+ *
70
+ * @param x Counts (nonnegative, finite; not all 0), or a contingency table as an array of rows.
71
+ * @param options See {@link GTestOptions}.
72
+ * @returns The test result, with `statisticName` `"G"`; see {@link GTestResult}.
73
+ * @throws RangeError for negative or non-finite counts, a total of 0, fewer than 2 counts, `p` of the wrong length,
74
+ * negative or not summing to 1, `"yates"` for anything but a 2 x 2 table or two counts, and `"williams"` for a
75
+ * table with an empty row or column.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * gTest([[12, 5], [7, 9]]).statistic; // 2.4600430077307167
80
+ * gTest([[12, 5], [7, 9]], { correct: "williams" }).pValue; // 0.12530270793206313
81
+ * gTest([10, 20, 30], { p: [0.2, 0.3, 0.5] }).pValue; // 0.7527706003419717
82
+ * ```
83
+ *
84
+ * @category Hypothesis tests
85
+ */
86
+ export declare function gTest(x: Counts | readonly Counts[], options?: GTestOptions): GTestResult;
87
+ /**
88
+ * Result of {@link stuartMaxwellTest}. Has no `alternative`, as DescTools' `StuartMaxwellTest` returns none.
89
+ *
90
+ * @category Hypothesis tests
91
+ */
92
+ export interface StuartMaxwellTestResult extends Omit<HTest, "alternative"> {
93
+ /** Degrees of freedom: the number of categories left, less 1 (`parameterName` is `"df"`). */
94
+ parameter: number;
95
+ /** The total of the table without the categories removed. */
96
+ N: number;
97
+ }
98
+ /**
99
+ * Stuart-Maxwell test of marginal homogeneity of a square table (paired classifications into the same categories).
100
+ * Equivalent to `DescTools::StuartMaxwellTest()` in R. The degrees of freedom differ where categories are removed.
101
+ *
102
+ * Categories with no counts off the diagonal (in their row and column) are removed. With the K categories left, `d`
103
+ * the differences of the row and column sums of the first K - 1 and `S` their covariance (`S_ii = r_i + c_i - 2 x_ii`,
104
+ * `S_ij = -(x_ij + x_ji)`), the statistic `d' S^-1 d` is chi-squared with K - 1 degrees of freedom (DescTools keeps the
105
+ * original number of categories less 1). For a 2 x 2 table it is McNemar's statistic without correction.
106
+ *
107
+ * @param table A square table of counts as an array of rows.
108
+ * @returns The test result, with `statisticName` `"chi-squared"`; see {@link StuartMaxwellTestResult}.
109
+ * @throws RangeError for a table that is not square with at least two rows, negative or non-finite counts, fewer
110
+ * than 2 categories left ("Too many equal marginals, cannot compute") and categories whose off-diagonal counts fall
111
+ * into unconnected groups (S is then singular).
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * stuartMaxwellTest([[20, 3, 2], [4, 15, 6], [1, 5, 30]]).statistic; // 0.30534351145038163
116
+ * ```
117
+ *
118
+ * @category Hypothesis tests
119
+ */
120
+ export declare function stuartMaxwellTest(table: readonly Counts[]): StuartMaxwellTestResult;
121
+ /**
122
+ * Options for {@link goodmanKruskalGamma}.
123
+ *
124
+ * @category Hypothesis tests
125
+ */
126
+ export interface GoodmanKruskalGammaOptions {
127
+ /** Confidence level of `confInt`, in (0, 1). Default `0.95`. */
128
+ confLevel?: number;
129
+ }
130
+ /**
131
+ * Result of {@link goodmanKruskalGamma}.
132
+ *
133
+ * @category Hypothesis tests
134
+ */
135
+ export interface GoodmanKruskalGammaResult {
136
+ /** Gamma, `(C - D) / (C + D)` with C and D the numbers of concordant and discordant pairs (NaN when both are 0). */
137
+ estimate: number;
138
+ /** The Wald interval `gamma -/+ z ase`, clipped to [-1, 1]. */
139
+ confInt: [number, number];
140
+ /** The confidence level of `confInt`. */
141
+ confLevel: number;
142
+ /** The asymptotic standard error ASE1 (not under the null hypothesis). */
143
+ ase: number;
144
+ /** Warnings (none: DescTools gives none either). */
145
+ warnings: string[];
146
+ }
147
+ /**
148
+ * Goodman and Kruskal's gamma of a table of two ordered classifications, with its asymptotic standard error and a
149
+ * Wald confidence interval. Equivalent to `DescTools::GoodmanKruskalGamma()` in R.
150
+ *
151
+ * `gamma = (C - D) / (C + D)` with C and D the numbers of concordant and discordant pairs. The standard error is the
152
+ * non-null ASE1 (Agresti 2010, section 7.1): `sqrt(sum x psi^2)` with `psi_ij = 2 (D Pc_ij - C Pd_ij) / (C + D)^2`,
153
+ * `Pc_ij` the counts above-left and below-right of cell (i, j) and `Pd_ij` those above-right and below-left. The
154
+ * interval is `[max(-1, gamma - z ase), min(1, gamma + z ase)]`, z the normal quantile of `1 - (1 - confLevel) / 2`.
155
+ * Without concordant or discordant pairs (a table with one row, for example) gamma and the interval are NaN, as
156
+ * DescTools. No test of gamma = 0 is given (it would need the null standard error).
157
+ *
158
+ * @param table A table of counts (nonnegative, finite) as an array of rows; rows and columns in their order.
159
+ * @param options See {@link GoodmanKruskalGammaOptions}.
160
+ * @returns See {@link GoodmanKruskalGammaResult}.
161
+ * @throws RangeError for negative or non-finite counts, or a level outside (0, 1).
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * const g = goodmanKruskalGamma([[10, 5, 2], [3, 8, 6], [1, 4, 9]]);
166
+ * g.estimate; // 0.6703296703296703
167
+ * g.confInt; // [0.4241347346132843, 0.9165246060460562]
168
+ * ```
169
+ *
170
+ * @category Hypothesis tests
171
+ */
172
+ export declare function goodmanKruskalGamma(table: readonly Counts[], options?: GoodmanKruskalGammaOptions): GoodmanKruskalGammaResult;
173
+ /**
174
+ * Options for {@link lblTest}.
175
+ *
176
+ * @category Hypothesis tests
177
+ */
178
+ export interface LblTestOptions {
179
+ /** `"two.sided"` (default), `"less"` (negative association) or `"greater"` (positive association). */
180
+ alternative?: Alternative | (string & {});
181
+ /** Scores of the rows. Default `1, 2, ..., r`. */
182
+ rowScores?: readonly number[];
183
+ /** Scores of the columns. Default `1, 2, ..., c`. */
184
+ colScores?: readonly number[];
185
+ }
186
+ /**
187
+ * Result of {@link lblTest}.
188
+ *
189
+ * @category Hypothesis tests
190
+ */
191
+ export type LblTestResult = HTest;
192
+ /**
193
+ * Asymptotic linear-by-linear association test of a table of two ordered classifications (the Mantel test of trend).
194
+ * Equivalent to `coin::lbl_test()` in R. As coin's asymptotic test, except for tables with a two-level factor.
195
+ *
196
+ * `Z = sqrt(N - 1) r`, r the Pearson correlation of the row and column scores over the N observations: the linear
197
+ * statistic `sum x_ij u_i v_j` standardised by its permutation (conditional) mean and variance. The scores are 1..k
198
+ * by default for every factor (the level index; empty rows and columns keep their place). coin leaves a two-level
199
+ * factor unordered and codes it by an indicator of its first level, which turns the sign of Z, and refuses 2 x 2
200
+ * tables; here two-level factors are scored like the others and a 2 x 2 table is tested (Z^2 is then the 'N - 1'
201
+ * chi-squared statistic). p-values from the normal distribution: `"greater"` is positive association.
202
+ *
203
+ * @param table A table of counts (nonnegative, finite) as an array of rows.
204
+ * @param options See {@link LblTestOptions}.
205
+ * @returns The test result, with `statisticName` `"Z"`; see {@link LblTestResult}.
206
+ * @throws RangeError for negative or non-finite counts, a total of at most 1, scores of the wrong length or not
207
+ * finite, and tables whose row or column scores do not vary over the observations (Z undefined).
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * lblTest([[10, 5, 2], [3, 8, 6], [1, 4, 9]]).statistic; // 3.601652214003794
212
+ * ```
213
+ *
214
+ * @category Hypothesis tests
215
+ */
216
+ export declare function lblTest(table: readonly Counts[], options?: LblTestOptions): LblTestResult;