@numero-uno-jpn/rstats 0.5.0 → 0.7.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 (179) hide show
  1. package/CHANGELOG.md +125 -0
  2. package/README.ja.md +36 -20
  3. package/README.md +34 -18
  4. package/THIRD_PARTY_NOTICES.md +25 -5
  5. package/dist/chunks/chunk-6R7YDHA2.js +2 -0
  6. package/dist/chunks/chunk-AFKYWGKN.js +2 -0
  7. package/dist/chunks/chunk-ANYGLFT4.js +2 -0
  8. package/dist/chunks/chunk-BI5RSUZ2.js +2 -0
  9. package/dist/chunks/chunk-J3SNI7KY.js +2 -0
  10. package/dist/chunks/chunk-JCZAB6C5.js +2 -0
  11. package/dist/chunks/chunk-MVG5LUZU.js +2 -0
  12. package/dist/chunks/chunk-PTPPP4TI.js +2 -0
  13. package/dist/chunks/chunk-QNQJA6KZ.js +2 -0
  14. package/dist/chunks/chunk-SXSJXDD3.js +2 -0
  15. package/dist/chunks/chunk-UIOAWLAN.js +2 -0
  16. package/dist/chunks/chunk-XYXHHF5L.js +2 -0
  17. package/dist/chunks/chunk-YKN6OGJL.js +2 -0
  18. package/dist/chunks/chunk-ZW52Z3GY.js +2 -0
  19. package/dist/chunks/chunk-ZYA4ISAT.js +2 -0
  20. package/dist/descriptive/basic.d.ts +73 -0
  21. package/dist/descriptive/basic.js +256 -0
  22. package/dist/descriptive/cov.d.ts +39 -0
  23. package/dist/descriptive/cov.js +105 -0
  24. package/dist/descriptive/hist.d.ts +64 -0
  25. package/dist/descriptive/hist.js +304 -0
  26. package/dist/descriptive/moments.d.ts +63 -0
  27. package/dist/descriptive/moments.js +158 -0
  28. package/dist/descriptive/quantile.d.ts +50 -0
  29. package/dist/descriptive/quantile.js +131 -0
  30. package/dist/descriptive/rank.d.ts +23 -0
  31. package/dist/descriptive/rank.js +72 -0
  32. package/dist/descriptive/table.d.ts +54 -0
  33. package/dist/descriptive/table.js +160 -0
  34. package/dist/descriptive.js +2 -0
  35. package/dist/distributions/arcsine.d.ts +24 -0
  36. package/dist/distributions/arcsine.js +73 -0
  37. package/dist/distributions/invgamma.d.ts +29 -0
  38. package/dist/distributions/invgamma.js +85 -0
  39. package/dist/distributions/kumaraswamy.d.ts +17 -0
  40. package/dist/distributions/kumaraswamy.js +96 -0
  41. package/dist/distributions/laplace.d.ts +23 -0
  42. package/dist/distributions/laplace.js +69 -0
  43. package/dist/distributions/multinomial.d.ts +24 -0
  44. package/dist/distributions/multinomial.js +70 -0
  45. package/dist/distributions/nbeta.js +28 -10
  46. package/dist/distributions/nchisq.js +20 -3
  47. package/dist/distributions/pareto.d.ts +23 -0
  48. package/dist/distributions/pareto.js +76 -0
  49. package/dist/distributions/summary.d.ts +25 -0
  50. package/dist/distributions/summary.js +289 -0
  51. package/dist/distributions/triangular.d.ts +25 -0
  52. package/dist/distributions/triangular.js +87 -0
  53. package/dist/distributions.js +2 -0
  54. package/dist/entries/descriptive.d.ts +7 -0
  55. package/dist/entries/descriptive.js +8 -0
  56. package/dist/entries/distributions.d.ts +42 -0
  57. package/dist/entries/distributions.js +42 -0
  58. package/dist/entries/linalg.d.ts +11 -0
  59. package/dist/entries/linalg.js +11 -0
  60. package/dist/entries/numerics.d.ts +8 -0
  61. package/dist/entries/numerics.js +6 -0
  62. package/dist/entries/special.d.ts +10 -0
  63. package/dist/entries/special.js +10 -0
  64. package/dist/entries/tests.d.ts +21 -0
  65. package/dist/entries/tests.js +20 -0
  66. package/dist/htest/ansariTest.d.ts +30 -0
  67. package/dist/htest/ansariTest.js +253 -0
  68. package/dist/htest/aov.d.ts +85 -0
  69. package/dist/htest/aov.js +178 -0
  70. package/dist/htest/binomTest.d.ts +36 -0
  71. package/dist/htest/binomTest.js +121 -0
  72. package/dist/htest/chisqTest.d.ts +34 -2
  73. package/dist/htest/chisqTest.js +140 -37
  74. package/dist/htest/cor.d.ts +14 -0
  75. package/dist/htest/cor.js +4 -3
  76. package/dist/htest/core/ansari.d.ts +25 -0
  77. package/dist/htest/core/ansari.js +186 -0
  78. package/dist/htest/core/kolmogorov.d.ts +26 -0
  79. package/dist/htest/core/kolmogorov.js +221 -0
  80. package/dist/htest/core/mantelhaen.d.ts +26 -0
  81. package/dist/htest/core/mantelhaen.js +145 -0
  82. package/dist/htest/core/shapiro.d.ts +24 -0
  83. package/dist/htest/core/shapiro.js +142 -0
  84. package/dist/htest/core/simulate.d.ts +2 -0
  85. package/dist/htest/core/simulate.js +16 -0
  86. package/dist/htest/core/smirnov.d.ts +20 -0
  87. package/dist/htest/core/smirnov.js +75 -0
  88. package/dist/htest/fisherTest.d.ts +12 -1
  89. package/dist/htest/fisherTest.js +187 -127
  90. package/dist/htest/groups.d.ts +20 -0
  91. package/dist/htest/groups.js +62 -0
  92. package/dist/htest/ksTest.d.ts +31 -0
  93. package/dist/htest/ksTest.js +106 -0
  94. package/dist/htest/mantelhaenTest.d.ts +26 -0
  95. package/dist/htest/mantelhaenTest.js +196 -0
  96. package/dist/htest/mcnemarTest.d.ts +26 -0
  97. package/dist/htest/mcnemarTest.js +99 -0
  98. package/dist/htest/onewayTest.d.ts +26 -0
  99. package/dist/htest/onewayTest.js +97 -0
  100. package/dist/htest/pairwise.d.ts +64 -0
  101. package/dist/htest/pairwise.js +152 -0
  102. package/dist/htest/poissonTest.d.ts +35 -0
  103. package/dist/htest/poissonTest.js +170 -0
  104. package/dist/htest/propTest.d.ts +37 -0
  105. package/dist/htest/propTest.js +164 -0
  106. package/dist/htest/rankTests.d.ts +52 -0
  107. package/dist/htest/rankTests.js +234 -0
  108. package/dist/htest/types.d.ts +2 -0
  109. package/dist/htest/types.js +6 -0
  110. package/dist/htest/varTest.d.ts +48 -0
  111. package/dist/htest/varTest.js +118 -0
  112. package/dist/htest/wilcoxTest.js +136 -174
  113. package/dist/index.d.ts +6 -50
  114. package/dist/index.js +8 -50
  115. package/dist/integrate/quadpack.d.ts +29 -0
  116. package/dist/integrate/quadpack.js +525 -0
  117. package/dist/internal/seq.d.ts +5 -0
  118. package/dist/internal/seq.js +14 -0
  119. package/dist/internal/uniroot.js +10 -71
  120. package/dist/linalg/lu.d.ts +45 -0
  121. package/dist/linalg/lu.js +88 -0
  122. package/dist/linalg/ops.d.ts +90 -0
  123. package/dist/linalg/ops.js +320 -0
  124. package/dist/linalg.js +2 -0
  125. package/dist/models/lm.d.ts +98 -0
  126. package/dist/models/lm.js +187 -0
  127. package/dist/numerics/deriv.d.ts +37 -0
  128. package/dist/numerics/deriv.js +150 -0
  129. package/dist/numerics/integrate.d.ts +91 -0
  130. package/dist/numerics/integrate.js +264 -0
  131. package/dist/numerics/interp.d.ts +103 -0
  132. package/dist/numerics/interp.js +581 -0
  133. package/dist/numerics/ode.d.ts +23 -0
  134. package/dist/numerics/ode.js +59 -0
  135. package/dist/numerics.js +2 -0
  136. package/dist/optim/localmin.d.ts +16 -0
  137. package/dist/optim/localmin.js +97 -0
  138. package/dist/random/beta.js +1 -1
  139. package/dist/random/binomial.js +1 -1
  140. package/dist/random/continuous.js +1 -1
  141. package/dist/random/extra.d.ts +33 -0
  142. package/dist/random/extra.js +62 -0
  143. package/dist/random/gamma.js +1 -1
  144. package/dist/random/hyper.js +1 -1
  145. package/dist/random/mt.js +0 -1
  146. package/dist/random/poisson.js +0 -1
  147. package/dist/random/r2dtable.d.ts +26 -0
  148. package/dist/random/r2dtable.js +153 -0
  149. package/dist/random/rank.js +1 -1
  150. package/dist/random/rng.js +1 -1
  151. package/dist/random/sample.js +1 -1
  152. package/dist/rstats.js +3 -2
  153. package/dist/special/besselIK.d.ts +38 -0
  154. package/dist/special/besselIK.js +315 -0
  155. package/dist/special/besselJY.d.ts +32 -0
  156. package/dist/special/besselJY.js +508 -0
  157. package/dist/special/besselSeries.d.ts +8 -0
  158. package/dist/special/besselSeries.js +24 -0
  159. package/dist/special/beta.js +22 -5
  160. package/dist/special/choose.d.ts +75 -0
  161. package/dist/special/choose.js +233 -0
  162. package/dist/special/erf.d.ts +28 -2
  163. package/dist/special/erf.js +37 -10
  164. package/dist/special/extra.d.ts +47 -0
  165. package/dist/special/extra.js +220 -0
  166. package/dist/special/igam.d.ts +4 -0
  167. package/dist/special/igam.js +57 -2
  168. package/dist/special/incbet.js +77 -22
  169. package/dist/special/lgammaDiff.d.ts +7 -0
  170. package/dist/special/lgammaDiff.js +16 -3
  171. package/dist/special/log1p.d.ts +53 -0
  172. package/dist/special/log1p.js +93 -0
  173. package/dist/special/psigamma.d.ts +48 -0
  174. package/dist/special/psigamma.js +318 -0
  175. package/dist/special/rCompatible.d.ts +8 -0
  176. package/dist/special/rCompatible.js +1 -0
  177. package/dist/special.js +2 -0
  178. package/dist/tests.js +3 -0
  179. package/package.json +26 -1
@@ -0,0 +1,142 @@
1
+ // Shapiro-Wilk W statistic and its p-value, for shapiro.test.
2
+ //
3
+ // Written for rStats.js from the published approximations (no ported code): Royston, "Approximating the
4
+ // Shapiro-Wilk W-test for non-normality", Statistics and Computing 2, 1992, and Royston, "Remark AS R94: A remark on
5
+ // Algorithm AS 181: The W-test for normality", Applied Statistics 44, 1995.
6
+ //
7
+ // Coefficients: m_i = qnorm((i - 3/8) / (n + 1/4)) and, with u = 1/sqrt(n), polynomial corrections for the two
8
+ // largest coefficients; the others are m_i / sqrt(phi) so that sum a_i^2 = 1.
9
+ // p-value: n = 3 exactly; 4 <= n <= 11: -log(gamma - log(1 - W)) is normal; n >= 12: log(1 - W) is normal, with
10
+ // means and standard deviations given as polynomials in n or log(n).
11
+ // Beyond the range of Royston's approximation for 4 <= n <= 11 the p-value is 1e-99: 1e-99 is the value R reports
12
+ // here (observed behaviour).
13
+ import { pnorm, qnorm } from "../../distributions/normal.js";
14
+ /** Polynomial c[0] + c[1] x + c[2] x^2 + ... */
15
+ function poly(c, x) {
16
+ let s = 0;
17
+ for (let i = c.length - 1; i >= 0; i--)
18
+ s = s * x + c[i];
19
+ return s;
20
+ }
21
+ const C1 = [0, 0.221157, -0.147981, -2.07119, 4.434685, -2.706056];
22
+ const C2 = [0, 0.042981, -0.293762, -1.752461, 5.682633, -3.582633];
23
+ const C3 = [0.544, -0.39978, 0.025054, -6.714e-4];
24
+ const C4 = [1.3822, -0.77857, 0.062767, -0.0020322];
25
+ const C5 = [-1.5861, -0.31082, -0.083751, 0.0038915];
26
+ const C6 = [-0.4803, -0.082676, 0.0030302];
27
+ const G = [-2.273, 0.459];
28
+ /**
29
+ * The Shapiro-Wilk coefficients a_1..a_n for a sample of size n (3 <= n <= 5000), antisymmetric
30
+ * (a_i = -a_(n+1-i)) with sum a_i^2 = 1.
31
+ */
32
+ export function swilkCoefficients(n) {
33
+ if (!(n >= 3) || n !== Math.floor(n))
34
+ throw new RangeError("n must be an integer of at least 3");
35
+ const a = new Float64Array(n);
36
+ if (n === 3) {
37
+ a[0] = -Math.SQRT1_2;
38
+ a[2] = Math.SQRT1_2;
39
+ return a;
40
+ }
41
+ const half = Math.floor(n / 2);
42
+ const m = new Float64Array(half); // m for the upper half, m[0] the largest
43
+ let summ2 = 0;
44
+ for (let i = 0; i < half; i++) {
45
+ // upper half: i-th largest expected normal order statistic
46
+ m[i] = -qnorm((i + 1 - 0.375) / (n + 0.25));
47
+ summ2 += m[i] * m[i];
48
+ }
49
+ summ2 *= 2;
50
+ const ssumm2 = Math.sqrt(summ2);
51
+ const rsn = 1 / Math.sqrt(n);
52
+ // the two largest coefficients a_n and a_(n-1), corrected by polynomials in u = 1/sqrt(n)
53
+ const an = m[0] / ssumm2 + poly(C1, rsn);
54
+ const up = new Float64Array(half);
55
+ up[0] = an;
56
+ let first;
57
+ let fac;
58
+ if (n > 5) {
59
+ const an1 = m[1] / ssumm2 + poly(C2, rsn);
60
+ up[1] = an1;
61
+ first = 2;
62
+ fac = Math.sqrt((summ2 - 2 * m[0] * m[0] - 2 * m[1] * m[1]) / (1 - 2 * an * an - 2 * an1 * an1));
63
+ }
64
+ else {
65
+ first = 1;
66
+ fac = Math.sqrt((summ2 - 2 * m[0] * m[0]) / (1 - 2 * an * an));
67
+ }
68
+ for (let i = first; i < half; i++)
69
+ up[i] = m[i] / fac;
70
+ // a_n, a_(n-1), ... positive for the largest observations
71
+ for (let i = 0; i < half; i++) {
72
+ a[n - 1 - i] = up[i];
73
+ a[i] = -up[i];
74
+ }
75
+ return a;
76
+ }
77
+ /**
78
+ * 1 - W for a sample sorted in increasing order, formed as (r + c)(r - c) / r^2 from c = sum a_i (x_i - mean) and
79
+ * r^2 = sum a_i^2 sum (x_i - mean)^2, so that it keeps its relative accuracy when W is close to 1 (the p-value
80
+ * depends on log(1 - W)). The data are scaled by their range first.
81
+ */
82
+ export function swilkOneMinusW(sorted) {
83
+ const n = sorted.length;
84
+ const a = swilkCoefficients(n);
85
+ const range = sorted[n - 1] - sorted[0];
86
+ let mean = 0;
87
+ for (const v of sorted)
88
+ mean += v / range;
89
+ mean /= n;
90
+ let ssa = 0;
91
+ let ssx = 0;
92
+ let sax = 0;
93
+ for (let i = 0; i < n; i++) {
94
+ const d = sorted[i] / range - mean;
95
+ ssa += a[i] * a[i];
96
+ ssx += d * d;
97
+ sax += a[i] * d;
98
+ }
99
+ const r = Math.sqrt(ssa * ssx);
100
+ return ((r - sax) * (r + sax)) / (ssa * ssx);
101
+ }
102
+ /**
103
+ * The W statistic of a sample sorted in increasing order:
104
+ * W = (sum a_i x_(i))^2 / sum (x_i - mean)^2.
105
+ */
106
+ export function swilkW(sorted) {
107
+ return 1 - swilkOneMinusW(sorted);
108
+ }
109
+ /**
110
+ * The p-value P(W' <= W) of the Shapiro-Wilk test for a sample of size n (Royston's approximations), from W.
111
+ */
112
+ export function swilkPValue(w, n) {
113
+ return swilkPValueFromOneMinusW(1 - w, n);
114
+ }
115
+ /** 6/pi and asin(sqrt(3/4)) to 15 digits, the values R's swilk uses for n = 3 */
116
+ const PI6 = 1.90985931710274;
117
+ const STQR = 1.0471975511966;
118
+ /**
119
+ * The p-value P(W' <= W) of the Shapiro-Wilk test for a sample of size n, from w1 = 1 - W (see swilkOneMinusW).
120
+ */
121
+ export function swilkPValueFromOneMinusW(w1, n) {
122
+ if (Number.isNaN(w1) || !(n >= 3))
123
+ return NaN;
124
+ if (n === 3) {
125
+ // exact: 6/pi (asin(sqrt(W)) - asin(sqrt(3/4))), W >= 3/4
126
+ const p = PI6 * (Math.asin(Math.sqrt(1 - w1)) - STQR);
127
+ return Math.max(p, 0);
128
+ }
129
+ const y = Math.log(w1);
130
+ if (n <= 11) {
131
+ const gamma = poly(G, n);
132
+ if (y >= gamma)
133
+ return 1e-99; // beyond the range of the approximation; 1e-99 is the value R reports here
134
+ const mu = poly(C3, n);
135
+ const sigma = Math.exp(poly(C4, n));
136
+ return pnorm(-Math.log(gamma - y), { mean: mu, sd: sigma, lowerTail: false });
137
+ }
138
+ const xx = Math.log(n);
139
+ const mu = poly(C5, xx);
140
+ const sigma = Math.exp(poly(C6, xx));
141
+ return pnorm(y, { mean: mu, sd: sigma, lowerTail: false });
142
+ }
@@ -0,0 +1,2 @@
1
+ /** R's method suffix for a simulated p-value; B is printed as R's paste() prints a number */
2
+ export declare function simulatedMethod(B: number): string;
@@ -0,0 +1,16 @@
1
+ // Shared pieces of the Monte Carlo p-values of chisqTest and fisherTest.
2
+ /** R's method suffix for a simulated p-value; B is printed as R's paste() prints a number */
3
+ export function simulatedMethod(B) {
4
+ return ` with simulated p-value\n\t (based on ${formatR(B)} replicates)`;
5
+ }
6
+ /** A number as R's as.character prints it (15 significant digits, fixed or scientific, whichever is narrower) */
7
+ function formatR(v) {
8
+ if (!Number.isFinite(v) || v === 0)
9
+ return String(v);
10
+ const sci = v.toExponential(14).replace(/\.?0+e/, "e");
11
+ const [mant, exp] = sci.split("e");
12
+ const e = Number(exp);
13
+ const sciStr = `${mant}e${e < 0 ? "-" : "+"}${String(Math.abs(e)).padStart(2, "0")}`;
14
+ const fixed = String(Number(v.toPrecision(15)));
15
+ return fixed.length <= sciStr.length ? fixed : sciStr;
16
+ }
@@ -0,0 +1,20 @@
1
+ export type SmirnovAlternative = "two.sided" | "less" | "greater";
2
+ export interface SmirnovOptions {
3
+ /** Which statistic: max |F_x - F_y| ("two.sided", default), max (F_x - F_y) ("greater"), max (F_y - F_x) ("less"). */
4
+ alternative?: SmirnovAlternative;
5
+ /**
6
+ * The pooled sample (length m + n). With ties, the statistic is only observed where the sorted values change.
7
+ * Omit for continuous data without ties.
8
+ */
9
+ z?: readonly number[];
10
+ }
11
+ /**
12
+ * P(D >= d) for the two-sample Smirnov statistic with sample sizes m and n, exactly.
13
+ * The comparison D >= d is done on the integer scale |i n - j m| >= d m n with a relative fuzz of 1e-10, so that a
14
+ * statistic computed in floating point from the data is not lost to rounding.
15
+ *
16
+ * @param d - Value of the statistic.
17
+ * @param m - Size of the first sample (x).
18
+ * @param n - Size of the second sample (y).
19
+ */
20
+ export declare function psmirnovUpper(d: number, m: number, n: number, options?: SmirnovOptions): number;
@@ -0,0 +1,75 @@
1
+ // Exact distribution of the two-sample Kolmogorov-Smirnov (Smirnov) statistic, for ks.test.
2
+ //
3
+ // Written for rStats.js from the published method (no ported code). Under the null hypothesis every arrangement of
4
+ // the m + n pooled observations is equally likely, i.e. every lattice path from (0, 0) to (m, n) (i steps for x,
5
+ // j steps for y). The statistic is the largest |i/m - j/n| (two-sided) or i/m - j/n ("greater") or j/n - i/m ("less")
6
+ // over the points where it can be observed: after every observation without ties, and only where the pooled sorted
7
+ // sample changes value when there are ties (Schröer and Trenkler, Computational Statistics & Data Analysis 20, 1995).
8
+ //
9
+ // P(D >= d) is summed over the points where a path first reaches the boundary: with v(i, j) the fraction of paths
10
+ // to (i, j) that stay inside,
11
+ // v(i, j) = i/(i+j) v(i-1, j) + j/(i+j) v(i, j-1),
12
+ // and a path through a boundary point is counted with the probability dhyper(i; m, n, i + j) of passing it. All
13
+ // terms are positive, so small p-values keep their relative accuracy (1 - P(D < d) would lose them).
14
+ import { dhyper1 } from "../../distributions/hypergeometric.js";
15
+ /**
16
+ * P(D >= d) for the two-sample Smirnov statistic with sample sizes m and n, exactly.
17
+ * The comparison D >= d is done on the integer scale |i n - j m| >= d m n with a relative fuzz of 1e-10, so that a
18
+ * statistic computed in floating point from the data is not lost to rounding.
19
+ *
20
+ * @param d - Value of the statistic.
21
+ * @param m - Size of the first sample (x).
22
+ * @param n - Size of the second sample (y).
23
+ */
24
+ export function psmirnovUpper(d, m, n, options = {}) {
25
+ const alternative = options.alternative ?? "two.sided";
26
+ if (Number.isNaN(d) || !(m >= 1) || !(n >= 1) || m !== Math.floor(m) || n !== Math.floor(n))
27
+ return NaN;
28
+ if (d <= 0)
29
+ return 1;
30
+ if (d > 1)
31
+ return 0;
32
+ // where the statistic is observed: after step s = i + j (1..m+n-1), or only where z changes
33
+ const N = m + n;
34
+ let observed;
35
+ if (options.z !== undefined) {
36
+ if (options.z.length !== N)
37
+ throw new RangeError("z must have length m + n");
38
+ const zs = [...options.z].sort((a, b) => a - b);
39
+ observed = new Array(N + 1).fill(false);
40
+ for (let s = 1; s < N; s++)
41
+ observed[s] = zs[s] !== zs[s - 1];
42
+ }
43
+ const threshold = d * m * n * (1 - 1e-10);
44
+ const crosses = (i, j) => {
45
+ const diff = i * n - j * m;
46
+ const v = alternative === "two.sided" ? Math.abs(diff) : alternative === "greater" ? diff : -diff;
47
+ return v >= threshold;
48
+ };
49
+ // v over j for the current i (rolling row)
50
+ let prev = new Float64Array(n + 1);
51
+ let cur = new Float64Array(n + 1);
52
+ let p = 0;
53
+ for (let i = 0; i <= m; i++) {
54
+ for (let j = 0; j <= n; j++) {
55
+ if (i === 0 && j === 0) {
56
+ cur[0] = 1;
57
+ continue;
58
+ }
59
+ const s = i + j;
60
+ const reach = (i > 0 ? (i / s) * prev[j] : 0) + (j > 0 ? (j / s) * cur[j - 1] : 0);
61
+ const obs = observed === undefined ? s < N : observed[s];
62
+ if (obs && crosses(i, j)) {
63
+ if (reach > 0)
64
+ p += reach * dhyper1(i, m, n, s, false);
65
+ cur[j] = 0;
66
+ }
67
+ else {
68
+ cur[j] = reach;
69
+ }
70
+ }
71
+ [prev, cur] = [cur, prev];
72
+ cur.fill(0);
73
+ }
74
+ return Math.min(p, 1);
75
+ }
@@ -12,6 +12,14 @@ export interface FisherTestOptions {
12
12
  confLevel?: number;
13
13
  /** Odds ratio under the null hypothesis (2 x 2 tables only). Default `1`. */
14
14
  or?: number;
15
+ /**
16
+ * Compute the p-value of an r x c table (larger than 2 x 2) by Monte Carlo simulation with `B` replicates (R's
17
+ * `simulate.p.value`), using the random stream of {@link setSeed} as R does. Ignored for 2 x 2 tables. Default
18
+ * `false`.
19
+ */
20
+ simulatePValue?: boolean;
21
+ /** Number of replicates for `simulatePValue`. Default `2000`. */
22
+ B?: number;
15
23
  }
16
24
  /**
17
25
  * Result of {@link fisherTest}. Has no `statistic` or `statisticName`, because R's
@@ -31,7 +39,10 @@ export interface FisherTestResult extends Omit<HTest, "statistic" | "statisticNa
31
39
  * tolerance `1e-7`, as in R). Non-integer counts are rounded to the nearest integer
32
40
  * (ties to even), where R warns and rounds.
33
41
  *
34
- * Not implemented: R's `hybrid` and `simulate.p.value` options.
42
+ * With `simulatePValue: true`, the p-value of a table larger than 2 x 2 is computed from `B` random tables with the
43
+ * observed margins, drawn from the random stream of {@link setSeed} exactly as in R.
44
+ *
45
+ * Not implemented: R's `hybrid` option.
35
46
  *
36
47
  * @param x Contingency table as an array of rows (at least 2 x 2).
37
48
  * @param options See {@link FisherTestOptions}.