@numero-uno-jpn/rstats 0.10.0 → 0.11.1

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 (280) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.ja.md +13 -9
  3. package/README.md +13 -8
  4. package/dist/chunks/{chunk-QJJWGNJP.js → chunk-2X7JYQBI.js} +2 -2
  5. package/dist/chunks/{chunk-O6PT7IVU.js → chunk-36GVER3O.js} +2 -2
  6. package/dist/chunks/{chunk-EWC6I75B.js → chunk-3WUXRSM6.js} +1 -1
  7. package/dist/chunks/{chunk-4NM37MGA.js → chunk-6I6QKTFK.js} +2 -2
  8. package/dist/chunks/{chunk-2XKG3DX4.js → chunk-ETOEZTGL.js} +1 -1
  9. package/dist/chunks/{chunk-IT354SHV.js → chunk-FVPDVRJQ.js} +2 -2
  10. package/dist/chunks/{chunk-QULPY6YM.js → chunk-GG4BJMJW.js} +1 -1
  11. package/dist/chunks/{chunk-7HPVUVZB.js → chunk-HITD7CX6.js} +1 -1
  12. package/dist/chunks/{chunk-LIMZKONM.js → chunk-KK4GE65D.js} +2 -2
  13. package/dist/chunks/chunk-LAUYSGV4.js +3 -0
  14. package/dist/chunks/chunk-M6GDXKKS.js +2 -0
  15. package/dist/chunks/{chunk-QFULZNHL.js → chunk-PHHUHR7T.js} +1 -1
  16. package/dist/chunks/{chunk-ENL43FNO.js → chunk-WQPFDDIY.js} +2 -2
  17. package/dist/chunks/chunk-X2AWQSVS.js +2 -0
  18. package/dist/chunks/{chunk-ZRIM4R4A.js → chunk-ZRJQD7VS.js} +2 -2
  19. package/dist/data/grouping.d.ts +100 -0
  20. package/dist/data/grouping.js +264 -0
  21. package/dist/descriptive/basic.d.ts +130 -17
  22. package/dist/descriptive/basic.js +115 -14
  23. package/dist/descriptive/cov.d.ts +22 -12
  24. package/dist/descriptive/dataFrames.d.ts +138 -0
  25. package/dist/descriptive/dataFrames.js +400 -0
  26. package/dist/descriptive/factanal.d.ts +26 -6
  27. package/dist/descriptive/factanal.js +7 -3
  28. package/dist/descriptive/hist.d.ts +27 -6
  29. package/dist/descriptive/hist.js +9 -3
  30. package/dist/descriptive/missing.d.ts +42 -0
  31. package/dist/descriptive/missing.js +52 -0
  32. package/dist/descriptive/moments.d.ts +91 -13
  33. package/dist/descriptive/moments.js +66 -9
  34. package/dist/descriptive/omega.d.ts +28 -6
  35. package/dist/descriptive/omega.js +16 -4
  36. package/dist/descriptive/pivot.d.ts +105 -0
  37. package/dist/descriptive/pivot.js +347 -0
  38. package/dist/descriptive/quantile.d.ts +33 -5
  39. package/dist/descriptive/quantile.js +20 -2
  40. package/dist/descriptive/rank.d.ts +12 -2
  41. package/dist/descriptive/rank.js +2 -0
  42. package/dist/descriptive/table.d.ts +55 -7
  43. package/dist/descriptive/table.js +38 -4
  44. package/dist/descriptive.js +2 -2
  45. package/dist/distributions/arcsine.d.ts +40 -5
  46. package/dist/distributions/arcsine.js +35 -4
  47. package/dist/distributions/beta.d.ts +12 -0
  48. package/dist/distributions/beta.js +12 -0
  49. package/dist/distributions/binomial.d.ts +18 -0
  50. package/dist/distributions/binomial.js +18 -0
  51. package/dist/distributions/bvnorm.d.ts +4 -1
  52. package/dist/distributions/bvnorm.js +4 -1
  53. package/dist/distributions/cauchy.d.ts +24 -0
  54. package/dist/distributions/cauchy.js +24 -0
  55. package/dist/distributions/chisq.d.ts +18 -0
  56. package/dist/distributions/chisq.js +18 -0
  57. package/dist/distributions/exponential.d.ts +29 -1
  58. package/dist/distributions/exponential.js +24 -0
  59. package/dist/distributions/f.d.ts +18 -0
  60. package/dist/distributions/f.js +18 -0
  61. package/dist/distributions/gamma.d.ts +20 -0
  62. package/dist/distributions/gamma.js +18 -0
  63. package/dist/distributions/geometric.d.ts +24 -0
  64. package/dist/distributions/geometric.js +24 -0
  65. package/dist/distributions/hypergeometric.d.ts +24 -0
  66. package/dist/distributions/hypergeometric.js +24 -0
  67. package/dist/distributions/invgamma.d.ts +32 -4
  68. package/dist/distributions/invgamma.js +27 -3
  69. package/dist/distributions/kumaraswamy.d.ts +31 -3
  70. package/dist/distributions/kumaraswamy.js +31 -3
  71. package/dist/distributions/laplace.d.ts +36 -4
  72. package/dist/distributions/laplace.js +31 -3
  73. package/dist/distributions/logistic.d.ts +29 -1
  74. package/dist/distributions/logistic.js +24 -0
  75. package/dist/distributions/lognormal.d.ts +29 -1
  76. package/dist/distributions/lognormal.js +24 -0
  77. package/dist/distributions/multinomial.d.ts +7 -1
  78. package/dist/distributions/multinomial.js +2 -0
  79. package/dist/distributions/negbinomial.d.ts +29 -1
  80. package/dist/distributions/negbinomial.js +24 -0
  81. package/dist/distributions/normal.d.ts +11 -1
  82. package/dist/distributions/normal.js +6 -0
  83. package/dist/distributions/pareto.d.ts +36 -4
  84. package/dist/distributions/pareto.js +31 -3
  85. package/dist/distributions/poisson.d.ts +18 -0
  86. package/dist/distributions/poisson.js +18 -0
  87. package/dist/distributions/signrank.d.ts +24 -0
  88. package/dist/distributions/signrank.js +24 -0
  89. package/dist/distributions/summary.d.ts +20 -2
  90. package/dist/distributions/summary.js +4 -0
  91. package/dist/distributions/t.d.ts +18 -0
  92. package/dist/distributions/t.js +18 -0
  93. package/dist/distributions/triangular.d.ts +36 -4
  94. package/dist/distributions/triangular.js +31 -3
  95. package/dist/distributions/tukey.d.ts +15 -1
  96. package/dist/distributions/tukey.js +10 -0
  97. package/dist/distributions/uniform.d.ts +29 -1
  98. package/dist/distributions/uniform.js +24 -0
  99. package/dist/distributions/weibull.d.ts +29 -1
  100. package/dist/distributions/weibull.js +24 -0
  101. package/dist/distributions/wilcox.d.ts +24 -0
  102. package/dist/distributions/wilcox.js +24 -0
  103. package/dist/distributions.js +2 -2
  104. package/dist/entries/descriptive.d.ts +5 -1
  105. package/dist/entries/descriptive.js +4 -0
  106. package/dist/entries/numerics.d.ts +1 -1
  107. package/dist/entries/tests.js +1 -1
  108. package/dist/htest/agreement.d.ts +61 -23
  109. package/dist/htest/agreement.js +19 -9
  110. package/dist/htest/ansariTest.d.ts +19 -1
  111. package/dist/htest/ansariTest.js +5 -17
  112. package/dist/htest/aov.d.ts +43 -10
  113. package/dist/htest/aov.js +17 -3
  114. package/dist/htest/binomTest.d.ts +12 -2
  115. package/dist/htest/binomTest.js +2 -0
  116. package/dist/htest/brunnerMunzelTest.d.ts +9 -3
  117. package/dist/htest/brunnerMunzelTest.js +4 -2
  118. package/dist/htest/chisqTest.d.ts +19 -3
  119. package/dist/htest/cor.d.ts +31 -3
  120. package/dist/htest/cor.js +9 -27
  121. package/dist/htest/effectSize.d.ts +70 -12
  122. package/dist/htest/effectSize.js +40 -7
  123. package/dist/htest/fisherTest.d.ts +9 -1
  124. package/dist/htest/fisherTest.js +2 -0
  125. package/dist/htest/groups.d.ts +5 -1
  126. package/dist/htest/ksTest.d.ts +32 -2
  127. package/dist/htest/ksTest.js +5 -8
  128. package/dist/htest/leveneTest.d.ts +15 -4
  129. package/dist/htest/leveneTest.js +5 -2
  130. package/dist/htest/mantelhaenTest.d.ts +25 -3
  131. package/dist/htest/mantelhaenTest.js +15 -1
  132. package/dist/htest/mcnemarTest.d.ts +14 -4
  133. package/dist/htest/mcnemarTest.js +4 -2
  134. package/dist/htest/multipleComparisons.d.ts +173 -29
  135. package/dist/htest/multipleComparisons.js +123 -19
  136. package/dist/htest/normalityTests.d.ts +56 -9
  137. package/dist/htest/normalityTests.js +40 -6
  138. package/dist/htest/onewayTest.d.ts +45 -2
  139. package/dist/htest/onewayTest.js +15 -7
  140. package/dist/htest/pAdjust.d.ts +9 -1
  141. package/dist/htest/pAdjust.js +2 -0
  142. package/dist/htest/pairwise.d.ts +50 -5
  143. package/dist/htest/pairwise.js +25 -0
  144. package/dist/htest/poissonTest.d.ts +12 -2
  145. package/dist/htest/poissonTest.js +2 -0
  146. package/dist/htest/polycor.d.ts +29 -10
  147. package/dist/htest/power.d.ts +60 -6
  148. package/dist/htest/power.js +6 -0
  149. package/dist/htest/propTest.d.ts +13 -2
  150. package/dist/htest/propTest.js +2 -0
  151. package/dist/htest/pwr.d.ts +286 -36
  152. package/dist/htest/pwr.js +116 -15
  153. package/dist/htest/rankTests.d.ts +132 -7
  154. package/dist/htest/rankTests.js +30 -33
  155. package/dist/htest/tTest.d.ts +32 -2
  156. package/dist/htest/tTest.js +19 -24
  157. package/dist/htest/testFormula.d.ts +57 -0
  158. package/dist/htest/testFormula.js +171 -0
  159. package/dist/htest/trendTests.d.ts +29 -8
  160. package/dist/htest/trendTests.js +8 -4
  161. package/dist/htest/types.d.ts +14 -2
  162. package/dist/htest/varTest.d.ts +46 -6
  163. package/dist/htest/varTest.js +11 -21
  164. package/dist/htest/wilcoxTest.d.ts +23 -1
  165. package/dist/htest/wilcoxTest.js +9 -24
  166. package/dist/internal/dpq.d.ts +4 -0
  167. package/dist/internal/noncentral.d.ts +2 -0
  168. package/dist/internal/vectorize.d.ts +4 -0
  169. package/dist/linalg/basic.d.ts +39 -5
  170. package/dist/linalg/basic.js +15 -0
  171. package/dist/linalg/chol.d.ts +26 -5
  172. package/dist/linalg/chol.js +7 -0
  173. package/dist/linalg/eigen.d.ts +16 -7
  174. package/dist/linalg/eigen.js +6 -5
  175. package/dist/linalg/expm.d.ts +15 -5
  176. package/dist/linalg/expm.js +5 -3
  177. package/dist/linalg/kappa.d.ts +21 -1
  178. package/dist/linalg/kappa.js +16 -0
  179. package/dist/linalg/lu.d.ts +30 -6
  180. package/dist/linalg/lu.js +15 -3
  181. package/dist/linalg/matrix.d.ts +4 -0
  182. package/dist/linalg/norm.d.ts +16 -1
  183. package/dist/linalg/norm.js +9 -0
  184. package/dist/linalg/ops.d.ts +168 -36
  185. package/dist/linalg/ops.js +116 -20
  186. package/dist/linalg/qr.d.ts +87 -12
  187. package/dist/linalg/qr.js +31 -0
  188. package/dist/linalg/schur.d.ts +8 -2
  189. package/dist/linalg/schur.js +3 -1
  190. package/dist/linalg/solve.d.ts +42 -6
  191. package/dist/linalg/solve.js +22 -0
  192. package/dist/linalg/svd.d.ts +12 -2
  193. package/dist/linalg/svd.js +2 -0
  194. package/dist/linalg.js +2 -2
  195. package/dist/models/anovaSequential.d.ts +26 -4
  196. package/dist/models/anovaSequential.js +3 -1
  197. package/dist/models/aovEz.d.ts +29 -6
  198. package/dist/models/aovEz.js +5 -2
  199. package/dist/models/aovStrata.d.ts +12 -2
  200. package/dist/models/carAnova.d.ts +9 -3
  201. package/dist/models/carAnova.js +4 -2
  202. package/dist/models/coxph.d.ts +33 -7
  203. package/dist/models/coxph.js +8 -3
  204. package/dist/models/glm.d.ts +128 -21
  205. package/dist/models/glm.js +71 -14
  206. package/dist/models/glmAnova.d.ts +30 -5
  207. package/dist/models/glmAnova.js +15 -2
  208. package/dist/models/glmConfint.d.ts +16 -2
  209. package/dist/models/glmConfint.js +9 -1
  210. package/dist/models/glmFamily.d.ts +27 -5
  211. package/dist/models/lm.d.ts +75 -17
  212. package/dist/models/lm.js +22 -4
  213. package/dist/models/mauchly.d.ts +20 -5
  214. package/dist/models/mauchly.js +7 -3
  215. package/dist/models/modelFrame.d.ts +5 -1
  216. package/dist/models/modelMatrix.d.ts +18 -4
  217. package/dist/models/modelMatrix.js +3 -1
  218. package/dist/models/survival.d.ts +41 -9
  219. package/dist/models/survival.js +10 -4
  220. package/dist/numerics/deriv.d.ts +21 -6
  221. package/dist/numerics/deriv.js +16 -5
  222. package/dist/numerics/integrate.d.ts +37 -7
  223. package/dist/numerics/integrate.js +8 -2
  224. package/dist/numerics/interp.d.ts +63 -12
  225. package/dist/numerics/interp.js +30 -6
  226. package/dist/numerics/ode.d.ts +15 -5
  227. package/dist/numerics/ode.js +5 -3
  228. package/dist/numerics.js +2 -2
  229. package/dist/optim/optim.d.ts +32 -8
  230. package/dist/optim/optim.js +8 -4
  231. package/dist/random/beta.d.ts +8 -0
  232. package/dist/random/beta.js +8 -0
  233. package/dist/random/binomial.d.ts +16 -0
  234. package/dist/random/binomial.js +16 -0
  235. package/dist/random/continuous.d.ts +51 -0
  236. package/dist/random/continuous.js +51 -0
  237. package/dist/random/extra.d.ts +66 -6
  238. package/dist/random/extra.js +66 -6
  239. package/dist/random/gamma.d.ts +33 -0
  240. package/dist/random/gamma.js +33 -0
  241. package/dist/random/hyper.d.ts +8 -0
  242. package/dist/random/hyper.js +8 -0
  243. package/dist/random/poisson.d.ts +25 -0
  244. package/dist/random/poisson.js +25 -0
  245. package/dist/random/r2dtable.d.ts +2 -0
  246. package/dist/random/r2dtable.js +2 -0
  247. package/dist/random/rank.d.ts +16 -0
  248. package/dist/random/rank.js +16 -0
  249. package/dist/random/rng.d.ts +49 -6
  250. package/dist/random/rng.js +29 -2
  251. package/dist/random/sample.d.ts +10 -2
  252. package/dist/random/sample.js +5 -1
  253. package/dist/random/streams.d.ts +13 -0
  254. package/dist/random/streams.js +13 -0
  255. package/dist/rstats.js +14 -14
  256. package/dist/special/besselIK.d.ts +11 -3
  257. package/dist/special/besselIK.js +6 -2
  258. package/dist/special/besselJY.d.ts +6 -2
  259. package/dist/special/besselJY.js +6 -2
  260. package/dist/special/beta.d.ts +16 -2
  261. package/dist/special/beta.js +16 -2
  262. package/dist/special/choose.d.ts +13 -5
  263. package/dist/special/choose.js +13 -5
  264. package/dist/special/erf.d.ts +8 -3
  265. package/dist/special/erf.js +8 -3
  266. package/dist/special/extra.d.ts +30 -10
  267. package/dist/special/extra.js +25 -9
  268. package/dist/special/gamma.d.ts +18 -2
  269. package/dist/special/gamma.js +18 -2
  270. package/dist/special/log1p.d.ts +16 -7
  271. package/dist/special/log1p.js +16 -7
  272. package/dist/special/psigamma.d.ts +9 -3
  273. package/dist/special/psigamma.js +9 -3
  274. package/dist/special/rCompatible.d.ts +5 -1
  275. package/dist/special.js +2 -2
  276. package/dist/tests.js +12 -13
  277. package/llms.txt +14 -0
  278. package/package.json +1 -1
  279. package/dist/chunks/chunk-AECGWZHD.js +0 -2
  280. package/dist/chunks/chunk-XWTZ4RTR.js +0 -2
package/dist/htest/pwr.js CHANGED
@@ -19,23 +19,43 @@ const COHEN = {
19
19
  f2: [0.02, 0.15, 0.35],
20
20
  };
21
21
  /**
22
- * Cohen's conventional effect sizes. Equivalent to pwr's `cohen.ES(test, size)`.
22
+ * Cohen's conventional effect sizes. Equivalent to `pwr::cohen.ES(test, size)` in R.
23
23
  *
24
24
  * @example
25
25
  * ```ts
26
26
  * cohenES("anov", "medium").effectSize; // 0.25
27
27
  * ```
28
+ *
29
+ * @category Effect sizes and power
28
30
  */
29
31
  export function cohenES(test = "p", size = "small") {
30
32
  const t = matchChoice(test, ["p", "t", "r", "anov", "chisq", "f2"]);
31
33
  const s = matchChoice(size, ["small", "medium", "large"]);
32
34
  return { test: t, size: s, effectSize: COHEN[t][["small", "medium", "large"].indexOf(s)], method: "Conventional effect size from Cohen (1982)" };
33
35
  }
34
- /** Effect size h of two proportions: `2 asin(sqrt(p1)) - 2 asin(sqrt(p2))`. Equivalent to pwr's `ES.h`. */
36
+ /**
37
+ * Effect size h of two proportions: `2 asin(sqrt(p1)) - 2 asin(sqrt(p2))`. Equivalent to `pwr::ES.h()` in R.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * esH(0.65, 0.45); // 0.40486007517695755
42
+ * ```
43
+ *
44
+ * @category Effect sizes and power
45
+ */
35
46
  export function esH(p1, p2) {
36
47
  return 2 * Math.asin(Math.sqrt(p1)) - 2 * Math.asin(Math.sqrt(p2));
37
48
  }
38
- /** Effect size w of a goodness-of-fit test, `sqrt(sum((p1 - p0)^2 / p0))`. Equivalent to pwr's `ES.w1`. */
49
+ /**
50
+ * Effect size w of a goodness-of-fit test, `sqrt(sum((p1 - p0)^2 / p0))`. Equivalent to `pwr::ES.w1()` in R.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * esW1([0.25, 0.25, 0.25, 0.25], [0.4, 0.2, 0.2, 0.2]); // 0.34641016151377546
55
+ * ```
56
+ *
57
+ * @category Effect sizes and power
58
+ */
39
59
  export function esW1(p0, p1) {
40
60
  if (p0.length !== p1.length)
41
61
  throw new RangeError("'P0' and 'P1' must have the same length");
@@ -46,7 +66,14 @@ export function esW1(p0, p1) {
46
66
  }
47
67
  /**
48
68
  * 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`.
69
+ * r and column sums c, `p0 = r c'` and `sqrt(sum((p - p0)^2 / p0))`. Equivalent to `pwr::ES.w2()` in R.
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * esW2([[0.3, 0.2], [0.1, 0.4]]); // 0.40824829046386296
74
+ * ```
75
+ *
76
+ * @category Effect sizes and power
50
77
  */
51
78
  export function esW2(p) {
52
79
  const rows = p.map((r) => r.reduce((a, b) => a + b, 0));
@@ -98,14 +125,16 @@ function normalPower(shift, sig, alt) {
98
125
  return (pnorm(qnorm(1 - sig / 2) - shift, { lowerTail: false }) + pnorm(qnorm(sig / 2) - shift));
99
126
  }
100
127
  /**
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()`.
128
+ * Power of a t test of means, or the sample size, effect size or significance level for a given power.
129
+ * Equivalent to `pwr::pwr.t.test()` in R.
103
130
  *
104
131
  * @example
105
132
  * ```ts
106
133
  * pwrTTest({ n: 20, d: 0.5 }).power; // 0.3379390289250411
107
134
  * pwrTTest({ d: 0.5, power: 0.8 }).n; // 63.76561044415068
108
135
  * ```
136
+ *
137
+ * @category Effect sizes and power
109
138
  */
110
139
  export function pwrTTest(options = {}) {
111
140
  let { n, power } = options;
@@ -136,7 +165,15 @@ export function pwrTTest(options = {}) {
136
165
  }
137
166
  /**
138
167
  * 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()`.
168
+ * level for a given power. Equivalent to `pwr::pwr.t2n.test()` in R.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * pwrT2nTest({ n1: 20, n2: 30, d: 0.5 }).power; // 0.39656999440895696
173
+ * pwrT2nTest({ n1: 50, d: 0.5, power: 0.8 }).n2; // 87.70891347276086
174
+ * ```
175
+ *
176
+ * @category Effect sizes and power
140
177
  */
141
178
  export function pwrT2nTest(options = {}) {
142
179
  let { n1, n2, power } = options;
@@ -195,7 +232,15 @@ function normalTest(es0, options, name, scale, nLower, esLower) {
195
232
  }
196
233
  /**
197
234
  * 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()`.
235
+ * size or significance level for a given power. Equivalent to `pwr::pwr.2p.test()` in R.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * pwr2pTest({ h: 0.3, n: 80 }).power; // 0.47510087057263806
240
+ * pwr2pTest({ h: esH(0.65, 0.45), power: 0.8 }).n; // 95.76938656160651
241
+ * ```
242
+ *
243
+ * @category Effect sizes and power
199
244
  */
200
245
  export function pwr2pTest(options = {}) {
201
246
  const r = normalTest(options.h, options, "h", (n) => Math.sqrt(n / 2), 2 + 1e-10, 1e-10);
@@ -211,7 +256,15 @@ export function pwr2pTest(options = {}) {
211
256
  }
212
257
  /**
213
258
  * 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()`.
259
+ * level for a given power. Equivalent to `pwr::pwr.p.test()` in R.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * pwrPTest({ h: 0.2, n: 60 }).power; // 0.34084512406749834
264
+ * pwrPTest({ h: 0.2, power: 0.8 }).n; // 196.221512550509
265
+ * ```
266
+ *
267
+ * @category Effect sizes and power
215
268
  */
216
269
  export function pwrPTest(options = {}) {
217
270
  const r = normalTest(options.h, options, "h", (n) => Math.sqrt(n), 2 + 1e-10, 1e-10);
@@ -226,7 +279,15 @@ export function pwrPTest(options = {}) {
226
279
  }
227
280
  /**
228
281
  * 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()`.
282
+ * significance level for a given power. Equivalent to `pwr::pwr.norm.test()` in R.
283
+ *
284
+ * @example
285
+ * ```ts
286
+ * pwrNormTest({ d: 0.3, n: 50 }).power; // 0.5641160254310038
287
+ * pwrNormTest({ d: 0.3, power: 0.8 }).n; // 87.20953791564195
288
+ * ```
289
+ *
290
+ * @category Effect sizes and power
230
291
  */
231
292
  export function pwrNormTest(options = {}) {
232
293
  const r = normalTest(options.d, options, "d", (n) => Math.sqrt(n), 1 + 1e-10, 1e-10);
@@ -241,7 +302,15 @@ export function pwrNormTest(options = {}) {
241
302
  }
242
303
  /**
243
304
  * 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()`.
305
+ * sample size or the significance level for a given power. Equivalent to `pwr::pwr.2p2n.test()` in R.
306
+ *
307
+ * @example
308
+ * ```ts
309
+ * pwr2p2nTest({ h: 0.3, n1: 80, n2: 120 }).power; // 0.5471899635264242
310
+ * pwr2p2nTest({ h: 0.3, n1: 200, power: 0.8 }).n2; // 154.6399892648981
311
+ * ```
312
+ *
313
+ * @category Effect sizes and power
245
314
  */
246
315
  export function pwr2p2nTest(options = {}) {
247
316
  let { n1, n2, power } = options;
@@ -283,8 +352,16 @@ export function pwr2p2nTest(options = {}) {
283
352
  }
284
353
  /**
285
354
  * 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
355
+ * level for a given power. Equivalent to `pwr::pwr.r.test()` in R. As in pwr, the power computed for `"less"` reports
287
356
  * `r` negated.
357
+ *
358
+ * @example
359
+ * ```ts
360
+ * pwrRTest({ n: 50, r: 0.3 }).power; // 0.571555841860208
361
+ * pwrRTest({ r: 0.3, power: 0.8 }).n; // 84.07363978228769
362
+ * ```
363
+ *
364
+ * @category Effect sizes and power
288
365
  */
289
366
  export function pwrRTest(options = {}) {
290
367
  let { n, power } = options;
@@ -333,7 +410,15 @@ export function pwrRTest(options = {}) {
333
410
  }
334
411
  /**
335
412
  * 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()`.
413
+ * Equivalent to `pwr::pwr.chisq.test()` in R.
414
+ *
415
+ * @example
416
+ * ```ts
417
+ * pwrChisqTest({ w: 0.3, N: 100, df: 3 }).power; // 0.7112535997950422
418
+ * pwrChisqTest({ w: 0.3, df: 3, power: 0.8 }).N; // 121.13959310294602
419
+ * ```
420
+ *
421
+ * @category Effect sizes and power
337
422
  */
338
423
  export function pwrChisqTest(options) {
339
424
  let { N, power } = options;
@@ -364,7 +449,15 @@ export function pwrChisqTest(options) {
364
449
  }
365
450
  /**
366
451
  * 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()`.
452
+ * or significance level for a given power. Equivalent to `pwr::pwr.anova.test()` in R.
453
+ *
454
+ * @example
455
+ * ```ts
456
+ * pwrAnovaTest({ k: 4, n: 20, f: 0.25 }).power; // 0.42039009037704295
457
+ * pwrAnovaTest({ k: 4, f: 0.25, power: 0.8 }).n; // 44.59927432039623
458
+ * ```
459
+ *
460
+ * @category Effect sizes and power
368
461
  */
369
462
  export function pwrAnovaTest(options = {}) {
370
463
  let { k, n, power } = options;
@@ -408,7 +501,15 @@ export function pwrAnovaTest(options = {}) {
408
501
  }
409
502
  /**
410
503
  * 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()`.
504
+ * given power. Equivalent to `pwr::pwr.f2.test()` in R.
505
+ *
506
+ * @example
507
+ * ```ts
508
+ * pwrF2Test({ u: 3, v: 60, f2: 0.15 }).power; // 0.7109605416866717
509
+ * pwrF2Test({ u: 3, f2: 0.15, power: 0.8 }).v; // 72.70582657950871
510
+ * ```
511
+ *
512
+ * @category Effect sizes and power
412
513
  */
413
514
  export function pwrF2Test(options = {}) {
414
515
  let { u, v, power } = options;
@@ -1,6 +1,11 @@
1
+ import type { DataFrame } from "../models/modelFrame.js";
1
2
  import { type GroupLabel } from "./groups.js";
2
3
  import { type Alternative, type HTest, type Sample } from "./types.js";
3
- /** Result of a k-sample test with a chi-squared statistic. Has no `alternative`, as R returns none. */
4
+ /**
5
+ * Result of a k-sample test with a chi-squared statistic. Has no `alternative`, as R returns none.
6
+ *
7
+ * @category Hypothesis tests
8
+ */
4
9
  export interface ChisqStatTestResult extends Omit<HTest, "alternative"> {
5
10
  /** Degrees of freedom (`parameterName` is `"df"`). */
6
11
  parameter: number;
@@ -11,43 +16,143 @@ export interface ChisqStatTestResult extends Omit<HTest, "alternative"> {
11
16
  * @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
12
17
  * labels `g`.
13
18
  * @param g Group labels, parallel to `x`.
19
+ *
20
+ * There is also a formula form, `kruskalTest("y ~ g", data)` (next overload).
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const r = kruskalTest([[2.9, 3.0, 2.5, 2.6, 3.2], [3.8, 2.7, 4.0, 2.4], [2.8, 3.4, 3.7, 2.2, 2.0]]);
25
+ * r.statistic; // 0.7714285714285722
26
+ * r.pValue; // 0.6799647735788935
27
+ * ```
28
+ *
29
+ * @category Hypothesis tests
14
30
  */
15
31
  export declare function kruskalTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[]): ChisqStatTestResult;
16
32
  /**
17
- * Friedman rank sum test for an unreplicated complete block design. Equivalent to R's `friedman.test()` with a
33
+ * Kruskal-Wallis test from a formula and a data frame. As R's `kruskal.test(formula, data)`: `"y ~ g"`,
34
+ * one group per level of `g`. Only the variables of the formula count: the rows where one of them is missing are
35
+ * dropped.
36
+ *
37
+ * @param formula `"y ~ g"` (one variable on each side).
38
+ * @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
39
+ */
40
+ export declare function kruskalTest(formula: string, data: DataFrame): ChisqStatTestResult;
41
+ /**
42
+ * Friedman rank sum test for an unreplicated complete block design. Equivalent to R's `friedman.test()`. Takes a
18
43
  * matrix.
19
44
  *
20
45
  * @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
46
+ *
47
+ * There is also a formula form, `friedmanTest("y ~ w | b", data)` (next overload).
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const r = friedmanTest([[5.4, 5.5, 5.55], [5.85, 5.7, 5.75], [5.2, 5.6, 5.5], [5.55, 5.5, 5.4], [5.9, 5.85, 5.7]]);
52
+ * r.statistic; // 0.4
53
+ * r.pValue; // 0.8187307530779818
54
+ * ```
55
+ *
56
+ * @category Hypothesis tests
21
57
  */
22
58
  export declare function friedmanTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
23
59
  /**
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.
60
+ * Friedman test from a formula and a data frame. As R's `friedman.test(formula, data)`: `"y ~ w | b"`
61
+ * with the groups `w` and the blocks `b`. The rows where `y`, `w` or `b` is missing are dropped first; then every
62
+ * combination of group and block must occur exactly once (else the error `not an unreplicated complete block
63
+ * design`, so that one missing value fails, unlike the matrix form, which drops its block).
64
+ *
65
+ * @param formula `"y ~ w | b"` (one variable on each side of `|`).
66
+ * @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
67
+ */
68
+ export declare function friedmanTest(formula: string, data: DataFrame): ChisqStatTestResult;
69
+ /**
70
+ * Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to
71
+ * `DescTools::CochranQTest()` in R. Takes a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
26
72
  *
27
73
  * @param y Blocks as rows, treatments as columns, values 0 or 1. Rows with a missing value are dropped.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * const r = cochranQTest([[1, 1, 0], [1, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0], [1, 0, 1]]);
78
+ * r.statistic; // 3.5
79
+ * r.pValue; // 0.17377394345044514
80
+ * ```
81
+ *
82
+ * @category Hypothesis tests
28
83
  */
29
84
  export declare function cochranQTest(y: readonly (readonly number[])[]): ChisqStatTestResult;
30
- /** Result of {@link quadeTest}: an F statistic with two degrees of freedom. */
85
+ /**
86
+ * Result of {@link quadeTest}: an F statistic with two degrees of freedom.
87
+ *
88
+ * @category Hypothesis tests
89
+ */
31
90
  export interface QuadeTestResult extends Omit<HTest, "alternative" | "parameter" | "parameterName"> {
32
91
  /** `[num df, denom df]`. */
33
92
  parameter: [number, number];
93
+ /** Names of `parameter`: `["num df", "denom df"]`. */
34
94
  parameterName: [string, string];
35
95
  }
36
96
  /**
37
- * Quade test for an unreplicated complete block design. Equivalent to R's `quade.test()` with a matrix.
97
+ * Quade test for an unreplicated complete block design. Equivalent to R's `quade.test()`. Takes a matrix.
38
98
  *
39
99
  * @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
100
+ *
101
+ * There is also a formula form, `quadeTest("y ~ w | b", data)` (next overload).
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * const r = quadeTest([[5.4, 5.5, 5.55], [5.85, 5.7, 5.75], [5.2, 5.6, 5.5], [5.55, 5.5, 5.4], [5.9, 5.85, 5.7]]);
106
+ * r.statistic; // 0.20634920634920637
107
+ * r.parameter; // [2, 8]
108
+ * r.pValue; // 0.8177464498805584
109
+ * ```
110
+ *
111
+ * @category Hypothesis tests
40
112
  */
41
113
  export declare function quadeTest(y: readonly (readonly number[])[]): QuadeTestResult;
114
+ /**
115
+ * Quade test from a formula and a data frame. As R's `quade.test(formula, data)`: `"y ~ w | b"` with the
116
+ * groups `w` and the blocks `b`. The rows where `y`, `w` or `b` is missing are dropped first; then every combination
117
+ * of group and block must occur exactly once (else the error `not an unreplicated complete block design`).
118
+ *
119
+ * @param formula `"y ~ w | b"` (one variable on each side of `|`).
120
+ * @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
121
+ */
122
+ export declare function quadeTest(formula: string, data: DataFrame): QuadeTestResult;
42
123
  /**
43
124
  * Fligner-Killeen (median) test of homogeneity of variances. Equivalent to R's `fligner.test()`.
44
125
  *
45
126
  * @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
46
127
  * labels `g`.
47
128
  * @param g Group labels, parallel to `x`.
129
+ *
130
+ * There is also a formula form, `flignerTest("y ~ g", data)` (next overload).
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * const r = flignerTest([[2.9, 3.0, 2.5, 2.6, 3.2], [3.8, 2.7, 4.0, 2.4], [2.8, 3.4, 3.7, 2.2, 2.0]]);
135
+ * r.statistic; // 5.209183208821734
136
+ * r.pValue; // 0.07393332507668932
137
+ * ```
138
+ *
139
+ * @category Hypothesis tests
48
140
  */
49
141
  export declare function flignerTest(x: Sample | readonly Sample[], g?: readonly GroupLabel[]): ChisqStatTestResult;
50
- /** Options for {@link moodTest}. */
142
+ /**
143
+ * Fligner-Killeen test from a formula and a data frame. As R's `fligner.test(formula, data)`: `"y ~ g"`,
144
+ * one group per level of `g`. Only the variables of the formula count: the rows where one of them is missing are
145
+ * dropped.
146
+ *
147
+ * @param formula `"y ~ g"` (one variable on each side).
148
+ * @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
149
+ */
150
+ export declare function flignerTest(formula: string, data: DataFrame): ChisqStatTestResult;
151
+ /**
152
+ * Options for {@link moodTest}.
153
+ *
154
+ * @category Hypothesis tests
155
+ */
51
156
  export interface MoodTestOptions {
52
157
  /** Alternative hypothesis (unambiguous abbreviations accepted). Default `"two.sided"`. */
53
158
  alternative?: Alternative | "two" | "l" | "g";
@@ -55,5 +160,25 @@ export interface MoodTestOptions {
55
160
  /**
56
161
  * Mood's two-sample test of scale. Equivalent to R's `mood.test()`. `NaN` values are dropped; ties get the average
57
162
  * of the scores of their positions.
163
+ *
164
+ * There is also a formula form, `moodTest("y ~ g", data, options)` (next overload).
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const r = moodTest([0.8, 1.9, 2.4, 3.1, 4.6, 5.0], [2.6, 2.9, 3.0, 3.3, 3.5]);
169
+ * r.statistic; // 2.0919054414739633
170
+ * r.pValue; // 0.03644697523591335
171
+ * ```
172
+ *
173
+ * @category Hypothesis tests
58
174
  */
59
175
  export declare function moodTest(x: Sample, y: Sample, options?: MoodTestOptions): HTest;
176
+ /**
177
+ * Mood's test from a formula and a data frame. As R's `mood.test(formula, data)`: `"y ~ g"` compares the
178
+ * two levels of `g` (the first level is `x`). Only the variables of the formula count: the rows where one of them is
179
+ * missing are dropped.
180
+ *
181
+ * @param formula `"y ~ g"`.
182
+ * @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
183
+ */
184
+ export declare function moodTest(formula: string, data: DataFrame, options?: MoodTestOptions): HTest;
@@ -21,6 +21,7 @@ import { pgamma1 } from "../distributions/gamma.js";
21
21
  import { pnorm, qnorm } from "../distributions/normal.js";
22
22
  import { rank, tieCounts, variance } from "../internal/summary.js";
23
23
  import { completeRows, median, toGroups } from "./groups.js";
24
+ import { blockData, kSampleData, twoGroupData } from "./testFormula.js";
24
25
  import { matchAlternative } from "./types.js";
25
26
  function chisqResult(method, name, statistic, df) {
26
27
  return {
@@ -38,14 +39,11 @@ function sumTies(x) {
38
39
  s += t * t * t - t;
39
40
  return s;
40
41
  }
41
- /**
42
- * Kruskal-Wallis rank sum test. Equivalent to R's `kruskal.test()`.
43
- *
44
- * @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
45
- * labels `g`.
46
- * @param g Group labels, parallel to `x`.
47
- */
48
42
  export function kruskalTest(x, g) {
43
+ if (typeof x === "string") {
44
+ const d = kSampleData(x, g);
45
+ return kruskalTest(d.values, d.group.codes);
46
+ }
49
47
  const groups = toGroups(x, g, "error");
50
48
  const k = groups.length;
51
49
  if (k < 2)
@@ -65,13 +63,9 @@ export function kruskalTest(x, g) {
65
63
  stat = ((12 * stat) / (n * (n + 1)) - 3 * (n + 1)) / (1 - sumTies(all) / (n * n * n - n));
66
64
  return chisqResult("Kruskal-Wallis rank sum test", "Kruskal-Wallis chi-squared", stat, k - 1);
67
65
  }
68
- /**
69
- * Friedman rank sum test for an unreplicated complete block design. Equivalent to R's `friedman.test()` with a
70
- * matrix.
71
- *
72
- * @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
73
- */
74
- export function friedmanTest(y) {
66
+ export function friedmanTest(y, data) {
67
+ if (typeof y === "string")
68
+ return friedmanTest(blockData(y, data));
75
69
  const rows = completeRows(y);
76
70
  const n = rows.length;
77
71
  const k = y[0]?.length ?? 0;
@@ -93,20 +87,26 @@ export function friedmanTest(y) {
93
87
  return chisqResult("Friedman rank sum test", "Friedman chi-squared", stat, k - 1);
94
88
  }
95
89
  /**
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.
90
+ * Cochran's Q test for binary responses in an unreplicated complete block design. Equivalent to
91
+ * `DescTools::CochranQTest()` in R. Takes a matrix: the Friedman test of the 0/1 matrix (with ties), reported as Q.
98
92
  *
99
93
  * @param y Blocks as rows, treatments as columns, values 0 or 1. Rows with a missing value are dropped.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const r = cochranQTest([[1, 1, 0], [1, 0, 0], [1, 1, 1], [0, 0, 0], [1, 1, 0], [1, 0, 1]]);
98
+ * r.statistic; // 3.5
99
+ * r.pValue; // 0.17377394345044514
100
+ * ```
101
+ *
102
+ * @category Hypothesis tests
100
103
  */
101
104
  export function cochranQTest(y) {
102
105
  return { ...friedmanTest(y), method: "Cochran's Q test", statisticName: "Q" };
103
106
  }
104
- /**
105
- * Quade test for an unreplicated complete block design. Equivalent to R's `quade.test()` with a matrix.
106
- *
107
- * @param y Blocks as rows, treatments as columns. Rows with a missing value are dropped.
108
- */
109
- export function quadeTest(y) {
107
+ export function quadeTest(y, data) {
108
+ if (typeof y === "string")
109
+ return quadeTest(blockData(y, data));
110
110
  const rows = completeRows(y);
111
111
  // as in R, b counts all blocks, including those dropped for a missing value
112
112
  const b = y.length;
@@ -155,14 +155,11 @@ export function quadeTest(y) {
155
155
  pValue,
156
156
  };
157
157
  }
158
- /**
159
- * Fligner-Killeen (median) test of homogeneity of variances. Equivalent to R's `fligner.test()`.
160
- *
161
- * @param x An array of samples (each with at least one non-missing value, as R requires), or one sample with group
162
- * labels `g`.
163
- * @param g Group labels, parallel to `x`.
164
- */
165
158
  export function flignerTest(x, g) {
159
+ if (typeof x === "string") {
160
+ const d = kSampleData(x, g);
161
+ return flignerTest(d.values, d.group.codes);
162
+ }
166
163
  const groups = toGroups(x, g, "error");
167
164
  const k = groups.length;
168
165
  if (k < 2)
@@ -191,11 +188,11 @@ export function flignerTest(x, g) {
191
188
  stat /= variance(a);
192
189
  return chisqResult("Fligner-Killeen test of homogeneity of variances", "Fligner-Killeen:med chi-squared", stat, k - 1);
193
190
  }
194
- /**
195
- * Mood's two-sample test of scale. Equivalent to R's `mood.test()`. `NaN` values are dropped; ties get the average
196
- * of the scores of their positions.
197
- */
198
191
  export function moodTest(x, y, options = {}) {
192
+ if (typeof x === "string") {
193
+ const d = twoGroupData(x, y);
194
+ return moodTest(d.x, d.y, options);
195
+ }
199
196
  // as in R, infinite values are dropped along with NaN
200
197
  const xs = Array.from(x).filter(Number.isFinite);
201
198
  const ys = Array.from(y).filter(Number.isFinite);
@@ -1,5 +1,10 @@
1
+ import type { DataFrame } from "../models/modelFrame.js";
1
2
  import { type Alternative, type HTest, type Sample } from "./types.js";
2
- /** Options for {@link tTest}. */
3
+ /**
4
+ * Options for {@link tTest}.
5
+ *
6
+ * @category Hypothesis tests
7
+ */
3
8
  export interface TTestOptions {
4
9
  /**
5
10
  * Alternative hypothesis: `"two.sided"`, `"less"` or `"greater"`. Default `"two.sided"`.
@@ -15,7 +20,11 @@ export interface TTestOptions {
15
20
  /** Confidence level of the interval. Default `0.95`. */
16
21
  confLevel?: number;
17
22
  }
18
- /** Result of {@link tTest}. */
23
+ /**
24
+ * Result of {@link tTest}.
25
+ *
26
+ * @category Hypothesis tests
27
+ */
19
28
  export interface TTestResult extends HTest {
20
29
  /** Degrees of freedom of the t distribution (`parameterName` is `"df"`). */
21
30
  parameter: number;
@@ -53,5 +62,26 @@ export interface TTestResult extends HTest {
53
62
  *
54
63
  * tTest([1, 2, 3, 4, 5], [3, 4, 5, 6, 8]).pValue; // 0.0849560662972433 (Welch)
55
64
  * ```
65
+ *
66
+ * There is also a formula form, `tTest("y ~ g", data, options)` (next overload).
67
+ *
68
+ * @category Hypothesis tests
56
69
  */
57
70
  export declare function tTest(x: Sample, y?: Sample | TTestOptions, options?: TTestOptions): TTestResult;
71
+ /**
72
+ * Student's t-test from a formula and a data frame. As R's `t.test(formula, data)`: `"y ~ g"` compares
73
+ * the two levels of `g` (the first level is `x`; `estimateNames` and `nullValueName` name the levels, as in R),
74
+ * `"y ~ 1"` is the one-sample test and `"Pair(a, b) ~ 1"` the paired test of `a` and `b` (the option `paired` is not
75
+ * allowed). Only the variables of the formula count: the rows where one of them is missing are dropped.
76
+ *
77
+ * @param formula `"y ~ g"`, `"y ~ 1"` or `"Pair(a, b) ~ 1"`.
78
+ * @param data The data frame: an object of columns (`null` or `NaN` for a missing value).
79
+ * @param options See {@link TTestOptions}.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * tTest("y ~ g", { y: [5.1, 4.9, 5.6, 5.8, 6.0, 5.4], g: ["a", "a", "a", "b", "b", "b"] }).estimateNames;
84
+ * // ["mean in group a", "mean in group b"]
85
+ * ```
86
+ */
87
+ export declare function tTest(formula: string, data: DataFrame, options?: TTestOptions): TTestResult;
@@ -7,32 +7,11 @@
7
7
  // - If the standard error is below 10 eps times the absolute mean, the data are treated as constant and an error is raised (as in R)
8
8
  import { pt1, qt1 } from "../distributions/t.js";
9
9
  import { completePairs, dropNaN, mean, variance } from "../internal/summary.js";
10
+ import { twoSampleData } from "./testFormula.js";
10
11
  import { checkConfLevel, matchAlternative } from "./types.js";
11
- /**
12
- * Student's t-test. Equivalent to R's `t.test()`.
13
- *
14
- * Performs a one-sample, two-sample (Welch's by default, or pooled-variance with
15
- * `varEqual: true`) or paired t-test. `NaN` values are dropped. As in R, an error is
16
- * thrown if the data are essentially constant.
17
- *
18
- * @param x Sample.
19
- * @param y Second sample for a two-sample or paired test. May be omitted, and the options
20
- * passed in its place: `tTest(x, { mu: 1 })`.
21
- * @param options See {@link TTestOptions}.
22
- * @returns The test result, with `statisticName` `"t"`; see {@link TTestResult}.
23
- *
24
- * @example
25
- * ```ts
26
- * const r = tTest([5.1, 4.9, 5.6, 5.8, 6.0, 5.4], { mu: 5 });
27
- * r.statistic; // 2.7351263280759737
28
- * r.parameter; // 5
29
- * r.pValue; // 0.04102919855187924
30
- * r.confInt; // [5.028074545960439, 5.905258787372895]
31
- *
32
- * tTest([1, 2, 3, 4, 5], [3, 4, 5, 6, 8]).pValue; // 0.0849560662972433 (Welch)
33
- * ```
34
- */
35
12
  export function tTest(x, y, options = {}) {
13
+ if (typeof x === "string")
14
+ return tTestFormula(x, y, options);
36
15
  // Also accept the form tTest(x, { mu: 1 })
37
16
  let ys;
38
17
  if (y !== undefined && !ArrayBuffer.isView(y) && !Array.isArray(y))
@@ -151,3 +130,19 @@ export function tTest(x, y, options = {}) {
151
130
  stdErr,
152
131
  };
153
132
  }
133
+ /** t.test from a formula: the default form on the data the formula selects, with the estimates named after the levels */
134
+ function tTestFormula(formula, data, options) {
135
+ if (options.paired !== undefined)
136
+ throw new Error("cannot use 'paired' in formula method");
137
+ const d = twoSampleData(formula, data, true);
138
+ if (d.kind === "one")
139
+ return tTest(d.x, options);
140
+ if (d.kind === "paired")
141
+ return tTest(d.x, d.y, { ...options, paired: true });
142
+ const [first, second] = d.levels;
143
+ return {
144
+ ...tTest(d.x, d.y, options),
145
+ estimateNames: [`mean in group ${first}`, `mean in group ${second}`],
146
+ nullValueName: `difference in means between group ${first} and group ${second}`,
147
+ };
148
+ }