@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
@@ -1,5 +1,9 @@
1
1
  import type { Sample } from "../htest/types.js";
2
- /** Options of the summaries that can drop missing values. */
2
+ /**
3
+ * Options of the summaries that can drop missing values.
4
+ *
5
+ * @category Descriptive statistics
6
+ */
3
7
  export interface NaRmOptions {
4
8
  /** Drop `NaN` (R's `NA`) before computing. Default `false`: a `NaN` gives `NaN`. */
5
9
  naRm?: boolean;
@@ -7,19 +11,32 @@ export interface NaRmOptions {
7
11
  /** The values as an array, NaN dropped when naRm; undefined when a NaN remains (the result is then NaN) */
8
12
  export declare function values(x: Sample, naRm: boolean | undefined): number[] | undefined;
9
13
  /**
10
- * Sum of the values. Equivalent to R's `sum()` for a numeric vector (0 for an empty one).
14
+ * Sum of the values. Equivalent to R's `sum()`. For a numeric vector (0 for an empty one).
11
15
  *
12
16
  * @example
13
17
  * ```ts
14
- * sum([0.1, 0.2, 0.3]); // 0.6000000000000001 in plain JS addition, 0.6 here (correctly rounded)
18
+ * sum([0.1, 0.2, 0.3]); // 0.6 (plain JS addition gives 0.6000000000000001)
15
19
  * ```
20
+ *
21
+ * @category Descriptive statistics
16
22
  */
17
23
  export declare function sum(x: Sample, options?: NaRmOptions): number;
18
24
  /**
19
- * Product of the values. Equivalent to R's `prod()` (1 for an empty vector).
25
+ * Product of the values. Equivalent to R's `prod()`. 1 for an empty vector.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * prod([1, 2, 3, 4]); // 24
30
+ * ```
31
+ *
32
+ * @category Descriptive statistics
20
33
  */
21
34
  export declare function prod(x: Sample, options?: NaRmOptions): number;
22
- /** Options for {@link mean}. */
35
+ /**
36
+ * Options for {@link mean}.
37
+ *
38
+ * @category Descriptive statistics
39
+ */
23
40
  export interface MeanOptions extends NaRmOptions {
24
41
  /** Fraction (0 to 0.5) of values trimmed from each end before the mean. Default `0`. */
25
42
  trim?: number;
@@ -27,39 +44,127 @@ export interface MeanOptions extends NaRmOptions {
27
44
  /**
28
45
  * Arithmetic mean, optionally trimmed. Equivalent to R's `mean()`. Accurate to the last bit: the sum is taken in
29
46
  * double-double and corrected by the mean deviation, as R corrects its long double mean.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * mean([1, 2, 3, 10]); // 4
51
+ * mean([1, 2, 3, 10], { trim: 0.25 }); // 2.5
52
+ * ```
53
+ *
54
+ * @category Descriptive statistics
30
55
  */
31
56
  export declare function mean(x: Sample, options?: MeanOptions): number;
32
57
  /**
33
- * Weighted mean `sum(x w) / sum(w)`. Equivalent to R's `weighted.mean()`: values with weight 0 do not enter the
58
+ * Weighted mean `sum(x w) / sum(w)`. Equivalent to R's `weighted.mean()`. Values with weight 0 do not enter the
34
59
  * numerator (even if infinite or NaN), and `naRm` drops missing values of `x` with their weights.
60
+ *
61
+ * @example
62
+ * ```ts
63
+ * weightedMean([1, 2, 3], [3, 1, 1]); // 1.6
64
+ * ```
65
+ *
66
+ * @category Descriptive statistics
35
67
  */
36
68
  export declare function weightedMean(x: Sample, w: Sample, options?: NaRmOptions): number;
37
69
  /**
38
- * Sample variance (denominator n - 1). Equivalent to R's `var()` for a vector (`NaN` for fewer than 2 values); named
70
+ * Sample variance (denominator n - 1). Equivalent to R's `var()`. For a vector (`NaN` for fewer than 2 values); named
39
71
  * `variance` because `var` cannot be a name in JavaScript. For matrices use `cov`.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * variance([1, 2, 3, 4]); // 1.6666666666666667
76
+ * ```
77
+ *
78
+ * @category Descriptive statistics
40
79
  */
41
80
  export declare function variance(x: Sample, options?: NaRmOptions): number;
42
- /** Sample standard deviation (denominator n - 1). Equivalent to R's `sd()`. */
81
+ /**
82
+ * Sample standard deviation (denominator n - 1). Equivalent to R's `sd()`.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * sd([2, 4, 4, 4, 5, 5, 7, 9]); // 2.138
87
+ * ```
88
+ *
89
+ * @category Descriptive statistics
90
+ */
43
91
  export declare function sd(x: Sample, options?: NaRmOptions): number;
44
- /** Median of the values (the mean of the two middle ones for an even count). Equivalent to R's `median()`. */
92
+ /**
93
+ * Median of the values (the mean of the two middle ones for an even count). Equivalent to R's `median()`.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * median([3, 1, 2]); // 2
98
+ * median([4, 1, 3, 2]); // 2.5
99
+ * ```
100
+ *
101
+ * @category Descriptive statistics
102
+ */
45
103
  export declare function median(x: Sample, options?: NaRmOptions): number;
46
104
  /**
47
- * `[min, max]` of the values. Equivalent to R's `range()` (`[NaN, NaN]` with a missing value unless `naRm`;
48
- * `[Infinity, -Infinity]` for an empty vector).
105
+ * `[min, max]` of the values. Equivalent to R's `range()`. `[NaN, NaN]` with a missing value unless `naRm`;
106
+ * `[Infinity, -Infinity]` for an empty vector.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * range([3, 1, 4, 1, 5]); // [1, 5]
111
+ * range([3, NaN, 4], { naRm: true }); // [3, 4]
112
+ * ```
113
+ *
114
+ * @category Descriptive statistics
49
115
  */
50
116
  export declare function range(x: Sample, options?: NaRmOptions): [number, number];
51
- /** Running sums. Equivalent to R's `cumsum()`: each partial sum is rounded from an exact accumulator; `NaN` from the first `NaN` on. */
117
+ /**
118
+ * Running sums. Equivalent to R's `cumsum()`. Each partial sum is rounded from an exact accumulator; `NaN` from the
119
+ * first `NaN` on.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * cumsum([1, 2, 3, 4]); // [1, 3, 6, 10]
124
+ * ```
125
+ *
126
+ * @category Descriptive statistics
127
+ */
52
128
  export declare function cumsum(x: Sample): number[];
53
129
  /**
54
- * Running products. Equivalent to R's `cumprod()`: products beyond the double range show as ±Infinity with the right
130
+ * Running products. Equivalent to R's `cumprod()`. Products beyond the double range show as ±Infinity with the right
55
131
  * sign, as R's long double accumulator does.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * cumprod([1, 2, 3, 4]); // [1, 2, 6, 24]
136
+ * ```
137
+ *
138
+ * @category Descriptive statistics
56
139
  */
57
140
  export declare function cumprod(x: Sample): number[];
58
- /** Running maxima. Equivalent to R's `cummax()`: `NaN` from the first `NaN` on. */
141
+ /**
142
+ * Running maxima. Equivalent to R's `cummax()`. `NaN` from the first `NaN` on.
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * cummax([1, 3, 2, 5, 4]); // [1, 3, 3, 5, 5]
147
+ * ```
148
+ *
149
+ * @category Descriptive statistics
150
+ */
59
151
  export declare function cummax(x: Sample): number[];
60
- /** Running minima. Equivalent to R's `cummin()`. */
152
+ /**
153
+ * Running minima. Equivalent to R's `cummin()`.
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * cummin([5, 3, 4, 1, 2]); // [5, 3, 3, 1, 1]
158
+ * ```
159
+ *
160
+ * @category Descriptive statistics
161
+ */
61
162
  export declare function cummin(x: Sample): number[];
62
- /** Options for {@link diff}. */
163
+ /**
164
+ * Options for {@link diff}.
165
+ *
166
+ * @category Descriptive statistics
167
+ */
63
168
  export interface DiffOptions {
64
169
  /** Distance between the values subtracted. Default `1`. */
65
170
  lag?: number;
@@ -67,7 +172,15 @@ export interface DiffOptions {
67
172
  differences?: number;
68
173
  }
69
174
  /**
70
- * Lagged and iterated differences. Equivalent to R's `diff()` for a vector (empty when `lag * differences` is at
175
+ * Lagged and iterated differences. Equivalent to R's `diff()`. For a vector (empty when `lag * differences` is at
71
176
  * least the length).
177
+ *
178
+ * @example
179
+ * ```ts
180
+ * diff([1, 4, 9, 16]); // [3, 5, 7]
181
+ * diff([1, 4, 9, 16], { differences: 2 }); // [2, 2]
182
+ * ```
183
+ *
184
+ * @category Descriptive statistics
72
185
  */
73
186
  export declare function diff(x: Sample, options?: DiffOptions): number[];
@@ -25,19 +25,28 @@ function sumOf(xs) {
25
25
  return s[0] + s[1];
26
26
  }
27
27
  /**
28
- * Sum of the values. Equivalent to R's `sum()` for a numeric vector (0 for an empty one).
28
+ * Sum of the values. Equivalent to R's `sum()`. For a numeric vector (0 for an empty one).
29
29
  *
30
30
  * @example
31
31
  * ```ts
32
- * sum([0.1, 0.2, 0.3]); // 0.6000000000000001 in plain JS addition, 0.6 here (correctly rounded)
32
+ * sum([0.1, 0.2, 0.3]); // 0.6 (plain JS addition gives 0.6000000000000001)
33
33
  * ```
34
+ *
35
+ * @category Descriptive statistics
34
36
  */
35
37
  export function sum(x, options = {}) {
36
38
  const xs = values(x, options.naRm);
37
39
  return xs === undefined ? NaN : sumOf(xs);
38
40
  }
39
41
  /**
40
- * Product of the values. Equivalent to R's `prod()` (1 for an empty vector).
42
+ * Product of the values. Equivalent to R's `prod()`. 1 for an empty vector.
43
+ *
44
+ * @example
45
+ * ```ts
46
+ * prod([1, 2, 3, 4]); // 24
47
+ * ```
48
+ *
49
+ * @category Descriptive statistics
41
50
  */
42
51
  export function prod(x, options = {}) {
43
52
  const xs = values(x, options.naRm);
@@ -62,6 +71,14 @@ export function prod(x, options = {}) {
62
71
  /**
63
72
  * Arithmetic mean, optionally trimmed. Equivalent to R's `mean()`. Accurate to the last bit: the sum is taken in
64
73
  * double-double and corrected by the mean deviation, as R corrects its long double mean.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * mean([1, 2, 3, 10]); // 4
78
+ * mean([1, 2, 3, 10], { trim: 0.25 }); // 2.5
79
+ * ```
80
+ *
81
+ * @category Descriptive statistics
65
82
  */
66
83
  export function mean(x, options = {}) {
67
84
  const xs = values(x, options.naRm);
@@ -84,8 +101,15 @@ export function mean(x, options = {}) {
84
101
  return meanOf(xs);
85
102
  }
86
103
  /**
87
- * Weighted mean `sum(x w) / sum(w)`. Equivalent to R's `weighted.mean()`: values with weight 0 do not enter the
104
+ * Weighted mean `sum(x w) / sum(w)`. Equivalent to R's `weighted.mean()`. Values with weight 0 do not enter the
88
105
  * numerator (even if infinite or NaN), and `naRm` drops missing values of `x` with their weights.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * weightedMean([1, 2, 3], [3, 1, 1]); // 1.6
110
+ * ```
111
+ *
112
+ * @category Descriptive statistics
89
113
  */
90
114
  export function weightedMean(x, w, options = {}) {
91
115
  let xs = Array.from(x);
@@ -104,8 +128,15 @@ export function weightedMean(x, w, options = {}) {
104
128
  return sumOf(prods) / sumOf(ws);
105
129
  }
106
130
  /**
107
- * Sample variance (denominator n - 1). Equivalent to R's `var()` for a vector (`NaN` for fewer than 2 values); named
131
+ * Sample variance (denominator n - 1). Equivalent to R's `var()`. For a vector (`NaN` for fewer than 2 values); named
108
132
  * `variance` because `var` cannot be a name in JavaScript. For matrices use `cov`.
133
+ *
134
+ * @example
135
+ * ```ts
136
+ * variance([1, 2, 3, 4]); // 1.6666666666666667
137
+ * ```
138
+ *
139
+ * @category Descriptive statistics
109
140
  */
110
141
  export function variance(x, options = {}) {
111
142
  const xs = values(x, options.naRm);
@@ -113,11 +144,30 @@ export function variance(x, options = {}) {
113
144
  return NaN;
114
145
  return varianceOf(xs);
115
146
  }
116
- /** Sample standard deviation (denominator n - 1). Equivalent to R's `sd()`. */
147
+ /**
148
+ * Sample standard deviation (denominator n - 1). Equivalent to R's `sd()`.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * sd([2, 4, 4, 4, 5, 5, 7, 9]); // 2.138
153
+ * ```
154
+ *
155
+ * @category Descriptive statistics
156
+ */
117
157
  export function sd(x, options = {}) {
118
158
  return Math.sqrt(variance(x, options));
119
159
  }
120
- /** Median of the values (the mean of the two middle ones for an even count). Equivalent to R's `median()`. */
160
+ /**
161
+ * Median of the values (the mean of the two middle ones for an even count). Equivalent to R's `median()`.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * median([3, 1, 2]); // 2
166
+ * median([4, 1, 3, 2]); // 2.5
167
+ * ```
168
+ *
169
+ * @category Descriptive statistics
170
+ */
121
171
  export function median(x, options = {}) {
122
172
  const xs = values(x, options.naRm);
123
173
  if (xs === undefined)
@@ -130,8 +180,16 @@ export function median(x, options = {}) {
130
180
  return n % 2 === 1 ? s[h] : meanDD([s[h - 1], s[h]])[0];
131
181
  }
132
182
  /**
133
- * `[min, max]` of the values. Equivalent to R's `range()` (`[NaN, NaN]` with a missing value unless `naRm`;
134
- * `[Infinity, -Infinity]` for an empty vector).
183
+ * `[min, max]` of the values. Equivalent to R's `range()`. `[NaN, NaN]` with a missing value unless `naRm`;
184
+ * `[Infinity, -Infinity]` for an empty vector.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * range([3, 1, 4, 1, 5]); // [1, 5]
189
+ * range([3, NaN, 4], { naRm: true }); // [3, 4]
190
+ * ```
191
+ *
192
+ * @category Descriptive statistics
135
193
  */
136
194
  export function range(x, options = {}) {
137
195
  const xs = values(x, options.naRm);
@@ -147,7 +205,17 @@ export function range(x, options = {}) {
147
205
  }
148
206
  return [lo, hi];
149
207
  }
150
- /** Running sums. Equivalent to R's `cumsum()`: each partial sum is rounded from an exact accumulator; `NaN` from the first `NaN` on. */
208
+ /**
209
+ * Running sums. Equivalent to R's `cumsum()`. Each partial sum is rounded from an exact accumulator; `NaN` from the
210
+ * first `NaN` on.
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * cumsum([1, 2, 3, 4]); // [1, 3, 6, 10]
215
+ * ```
216
+ *
217
+ * @category Descriptive statistics
218
+ */
151
219
  export function cumsum(x) {
152
220
  const out = [];
153
221
  let s = [0, 0];
@@ -167,8 +235,15 @@ export function cumsum(x) {
167
235
  return out;
168
236
  }
169
237
  /**
170
- * Running products. Equivalent to R's `cumprod()`: products beyond the double range show as ±Infinity with the right
238
+ * Running products. Equivalent to R's `cumprod()`. Products beyond the double range show as ±Infinity with the right
171
239
  * sign, as R's long double accumulator does.
240
+ *
241
+ * @example
242
+ * ```ts
243
+ * cumprod([1, 2, 3, 4]); // [1, 2, 6, 24]
244
+ * ```
245
+ *
246
+ * @category Descriptive statistics
172
247
  */
173
248
  export function cumprod(x) {
174
249
  const out = [];
@@ -225,17 +300,43 @@ function cumExtreme(x, better) {
225
300
  }
226
301
  return out;
227
302
  }
228
- /** Running maxima. Equivalent to R's `cummax()`: `NaN` from the first `NaN` on. */
303
+ /**
304
+ * Running maxima. Equivalent to R's `cummax()`. `NaN` from the first `NaN` on.
305
+ *
306
+ * @example
307
+ * ```ts
308
+ * cummax([1, 3, 2, 5, 4]); // [1, 3, 3, 5, 5]
309
+ * ```
310
+ *
311
+ * @category Descriptive statistics
312
+ */
229
313
  export function cummax(x) {
230
314
  return cumExtreme(x, (a, b) => a > b);
231
315
  }
232
- /** Running minima. Equivalent to R's `cummin()`. */
316
+ /**
317
+ * Running minima. Equivalent to R's `cummin()`.
318
+ *
319
+ * @example
320
+ * ```ts
321
+ * cummin([5, 3, 4, 1, 2]); // [5, 3, 3, 1, 1]
322
+ * ```
323
+ *
324
+ * @category Descriptive statistics
325
+ */
233
326
  export function cummin(x) {
234
327
  return cumExtreme(x, (a, b) => a < b);
235
328
  }
236
329
  /**
237
- * Lagged and iterated differences. Equivalent to R's `diff()` for a vector (empty when `lag * differences` is at
330
+ * Lagged and iterated differences. Equivalent to R's `diff()`. For a vector (empty when `lag * differences` is at
238
331
  * least the length).
332
+ *
333
+ * @example
334
+ * ```ts
335
+ * diff([1, 4, 9, 16]); // [3, 5, 7]
336
+ * diff([1, 4, 9, 16], { differences: 2 }); // [2, 2]
337
+ * ```
338
+ *
339
+ * @category Descriptive statistics
239
340
  */
240
341
  export function diff(x, options = {}) {
241
342
  const lag = options.lag ?? 1;
@@ -1,28 +1,37 @@
1
+ import { type CorMethod } from "../htest/cor.js";
1
2
  import type { Sample } from "../htest/types.js";
2
- /** Correlation or covariance method, as R's `method`. */
3
- export type CorMethod = "pearson" | "kendall" | "spearman";
4
- /** Handling of missing values, as R's `use`. */
3
+ export type { CorMethod };
4
+ /**
5
+ * Handling of missing values, as R's `use`.
6
+ *
7
+ * @category Descriptive statistics
8
+ */
5
9
  export type CorUse = "everything" | "all.obs" | "complete.obs" | "na.or.complete" | "pairwise.complete.obs";
6
- /** Options for {@link cov} and {@link cor}. */
10
+ /**
11
+ * Options for {@link cov} and {@link cor}.
12
+ *
13
+ * @category Descriptive statistics
14
+ */
7
15
  export interface CovOptions {
8
16
  /** Default `"pearson"`. Unambiguous abbreviations are accepted. */
9
17
  method?: CorMethod | (string & {});
10
18
  /** Default `"everything"`. Unambiguous abbreviations are accepted. */
11
19
  use?: CorUse | (string & {});
12
20
  }
13
- type Input = Sample | readonly (readonly number[])[];
14
21
  /**
15
22
  * Covariance of two vectors, or covariance matrix of the columns of a matrix (or between the columns of `x` and `y`).
16
23
  * Equivalent to R's `cov()`.
17
24
  *
18
25
  * @example
19
26
  * ```ts
20
- * cov([1, 2, 3, 4], [2, 4, 5, 9]); // 3.6666666666666665
21
- * cov([[1, 2], [2, 4], [3, 5], [4, 9]]); // 2 x 2 matrix
27
+ * cov([1, 2, 3, 4], [2, 4, 5, 9]); // 3.6666666666666665
28
+ * cov([[1, 2], [2, 4], [3, 5], [4, 9]]); // [[1.6666666666666667, 3.6666666666666665], [3.6666666666666665, 8.666666666666666]]
22
29
  * ```
30
+ *
31
+ * @category Descriptive statistics
23
32
  */
24
33
  export declare function cov(x: Sample, y: Sample, options?: CovOptions): number;
25
- export declare function cov(x: Input, y?: Input, options?: CovOptions): number | number[][];
34
+ export declare function cov(x: Sample | readonly (readonly number[])[], y?: Sample | readonly (readonly number[])[], options?: CovOptions): number | number[][];
26
35
  /**
27
36
  * Correlation of two vectors, or correlation matrix of the columns of a matrix (or between the columns of `x` and
28
37
  * `y`). Equivalent to R's `cor()`. Pearson correlations are computed in double-double, so they can be slightly more
@@ -30,10 +39,11 @@ export declare function cov(x: Input, y?: Input, options?: CovOptions): number |
30
39
  *
31
40
  * @example
32
41
  * ```ts
33
- * cor([1, 2, 3, 4], [2, 4, 5, 9]); // 0.9561828874675149
34
- * cor([1, 2, 3, 4], [2, 4, 5, 9], { method: "spearman" }); // 1
42
+ * cor([1, 2, 3, 4], [2, 4, 5, 9]); // 0.9647638212377322
43
+ * cor([1, 2, 3, 4], [2, 4, 5, 9], { method: "spearman" }); // 1
35
44
  * ```
45
+ *
46
+ * @category Descriptive statistics
36
47
  */
37
48
  export declare function cor(x: Sample, y: Sample, options?: CovOptions): number;
38
- export declare function cor(x: Input, y?: Input, options?: CovOptions): number | number[][];
39
- export {};
49
+ export declare function cor(x: Sample | readonly (readonly number[])[], y?: Sample | readonly (readonly number[])[], options?: CovOptions): number | number[][];
@@ -0,0 +1,138 @@
1
+ import { type DataFrame } from "../models/modelFrame.js";
2
+ /**
3
+ * One value of a data frame column (`null`: missing).
4
+ *
5
+ * @category Data frames
6
+ */
7
+ export type DataValue = number | string | boolean | null;
8
+ /**
9
+ * A row of a data frame, as passed to the condition of {@link subset}: column name to value.
10
+ *
11
+ * @category Data frames
12
+ */
13
+ export type DataRow = Record<string, DataValue>;
14
+ /** One column of a data frame. */
15
+ export type Column = DataFrame[string];
16
+ /** The number of rows of a data frame (0 without columns); all columns must have the same length */
17
+ export declare function rowCount(frame: DataFrame): number;
18
+ export declare const isMissing: (v: DataValue | undefined) => boolean;
19
+ /** A copy of a column restricted to the given rows */
20
+ export declare function pick(col: Column, rows: readonly number[]): Column;
21
+ /** Adds a column to a result under construction; names must be unique */
22
+ export declare function addColumn(out: DataFrame, name: string, col: Column): void;
23
+ /** Comparison of two values for sorting: numbers as numbers, text by code point, FALSE before TRUE, missing last */
24
+ export declare function compareValues(a: DataValue, b: DataValue): number;
25
+ /**
26
+ * Options for {@link merge}.
27
+ *
28
+ * @category Data frames
29
+ */
30
+ export interface MergeOptions {
31
+ /** The key columns, in both frames. Default: the column names common to both, in x's order. */
32
+ by?: string | readonly string[];
33
+ /** The key columns of x. Default `by`. */
34
+ byX?: string | readonly string[];
35
+ /** The key columns of y (as many as `byX`, matched in order). Default `by`. */
36
+ byY?: string | readonly string[];
37
+ /** Keep the rows of both frames that have no match. Default `false`. */
38
+ all?: boolean;
39
+ /** Keep the rows of x that have no match in y (y's columns missing). Default `all`. */
40
+ allX?: boolean;
41
+ /** Keep the rows of y that have no match in x (x's columns missing). Default `all`. */
42
+ allY?: boolean;
43
+ /** Order the rows by the key. Default `true`. */
44
+ sort?: boolean;
45
+ /** Appended to the names of the non-key columns that occur in both frames. Default `[".x", ".y"]`. */
46
+ suffixes?: readonly [string, string];
47
+ }
48
+ /**
49
+ * Join two data frames on key columns. Equivalent to R's `merge()`. For data frames: every pair of rows of x and y
50
+ * with equal keys (missing keys match each other), and with `allX` / `allY` the rows without a match. Columns: the keys
51
+ * (named as in x), x's other columns, y's other columns; a non-key name present in both gets the suffixes. Rows are
52
+ * ordered by the key unless `sort` is false (several keys: as text joined with "\r" unless `allX` or `allY`, as R
53
+ * does); without key columns, the cross join (x varying fastest).
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * const x = { id: [3, 1, 2], v: [10, 20, 30] };
58
+ * const y = { id: [1, 2, 2, 4], w: [0.5, 1.5, 2.5, 3.5] };
59
+ * merge(x, y); // { id: [1, 2, 2], v: [20, 30, 30], w: [0.5, 1.5, 2.5] }
60
+ * merge(x, y, { all: true }); // also id 3 (w missing) and id 4 (v missing)
61
+ * ```
62
+ *
63
+ * @category Data frames
64
+ */
65
+ export declare function merge(x: DataFrame, y: DataFrame, options?: MergeOptions): DataFrame;
66
+ /**
67
+ * Options for {@link subset}.
68
+ *
69
+ * @category Data frames
70
+ */
71
+ export interface SubsetOptions {
72
+ /** The columns to keep, in this order. Default: all, in their order. */
73
+ select?: readonly string[];
74
+ }
75
+ /**
76
+ * The rows of a data frame that meet a condition, and optionally some of its columns. Equivalent to R's `subset()`.
77
+ * For a data frame: `keep` is a boolean per row or a function of the row; a missing (`null`) or non-boolean result
78
+ * counts as false, as R drops the rows whose condition is NA.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * const d = { y: [5, NaN, 7, 2], g: ["a", "b", "a", "b"] };
83
+ * subset(d, (row) => (row.y as number) > 4); // { y: [5, 7], g: ["a", "a"] }
84
+ * subset(d, [true, true, false, false], { select: ["g"] }); // { g: ["a", "b"] }
85
+ * ```
86
+ *
87
+ * @category Data frames
88
+ */
89
+ export declare function subset(data: DataFrame, keep: readonly (boolean | null)[] | ((row: DataRow) => boolean | null | undefined), options?: SubsetOptions): DataFrame;
90
+ /**
91
+ * Stacks data frames with the same column names (matched by name, in the first frame's order). The data frame form
92
+ * of R's `rbind()`; see {@link rbind}.
93
+ */
94
+ export declare function rbindFrames(frames: readonly DataFrame[]): DataFrame;
95
+ /**
96
+ * Puts data frames with the same number of rows side by side. The data frame form of R's `cbind()`; see
97
+ * {@link cbind}. A column name that occurs twice is an error (an object cannot hold two columns of the same name).
98
+ */
99
+ export declare function cbindFrames(frames: readonly DataFrame[]): DataFrame;
100
+ /**
101
+ * Options of {@link fromRows}.
102
+ *
103
+ * @category Data frames
104
+ */
105
+ export interface FromRowsOptions {
106
+ /**
107
+ * The columns to take, in this order. Default: every key that occurs in some row, in the order of first
108
+ * appearance.
109
+ */
110
+ columns?: readonly string[];
111
+ }
112
+ /**
113
+ * A data frame (an object of columns) from an array of row objects, the usual shape of JSON and of parsed CSV:
114
+ * `[{ age: 30, score: 5 }, { age: 41, score: 7 }]` becomes `{ age: [30, 41], score: [5, 7] }`. A key that a row
115
+ * lacks, and `undefined`, become `null` (a missing value). Values must be numbers, strings, booleans or `null`, and
116
+ * one column may not mix numbers, strings and booleans. Not an R function. R reads data as data frames directly.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * fromRows([{ id: "a", score: 5 }, { id: "b" }]); // { id: ["a", "b"], score: [5, null] }
121
+ * ```
122
+ *
123
+ * @category Data frames
124
+ */
125
+ export declare function fromRows(rows: readonly Readonly<Record<string, unknown>>[], options?: FromRowsOptions): DataFrame;
126
+ /**
127
+ * The rows of a data frame as an array of objects, the inverse of {@link fromRows}: `{ age: [30, 41], score: [5, 7]
128
+ * }` becomes `[{ age: 30, score: 5 }, { age: 41, score: 7 }]`. Missing values stay as they are (`null`, or `NaN` in a
129
+ * numeric column). Not an R function. R keeps data in data frames and has no need for row objects.
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * toRows({ id: ["a", "b"], score: [5, null] }); // [{ id: "a", score: 5 }, { id: "b", score: null }]
134
+ * ```
135
+ *
136
+ * @category Data frames
137
+ */
138
+ export declare function toRows(data: DataFrame): DataRow[];