@numero-uno-jpn/rstats 0.15.3 → 0.17.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 (322) hide show
  1. package/CHANGELOG.md +294 -0
  2. package/README.ja.md +68 -35
  3. package/README.md +65 -30
  4. package/dist/chunks/chunk-2C33MXSW.js +3 -0
  5. package/dist/chunks/chunk-34S5SQY6.js +2 -0
  6. package/dist/chunks/chunk-4WE3OO62.js +2 -0
  7. package/dist/chunks/chunk-5AYHCITP.js +2 -0
  8. package/dist/chunks/chunk-6JJQL6HL.js +2 -0
  9. package/dist/chunks/chunk-C7V2FH6L.js +3 -0
  10. package/dist/chunks/chunk-CZI7KUO3.js +2 -0
  11. package/dist/chunks/chunk-DXI5N2ET.js +2 -0
  12. package/dist/chunks/chunk-FY3FVOBM.js +2 -0
  13. package/dist/chunks/chunk-H3WVPZDL.js +2 -0
  14. package/dist/chunks/chunk-LHUIJQHY.js +2 -0
  15. package/dist/chunks/chunk-LV3W2F7N.js +2 -0
  16. package/dist/chunks/chunk-M4ITG7YR.js +2 -0
  17. package/dist/chunks/chunk-NSYZTS26.js +2 -0
  18. package/dist/chunks/chunk-ONY5CISH.js +2 -0
  19. package/dist/chunks/chunk-RFE7MVP7.js +2 -0
  20. package/dist/chunks/chunk-SE7V2KLO.js +2 -0
  21. package/dist/chunks/chunk-THIGHOKK.js +2 -0
  22. package/dist/chunks/chunk-TNMJPQPV.js +2 -0
  23. package/dist/chunks/chunk-WM7VFXTV.js +2 -0
  24. package/dist/chunks/chunk-WMDYDTVQ.js +2 -0
  25. package/dist/chunks/chunk-XLSFJTTK.js +2 -0
  26. package/dist/chunks/chunk-Z37NELOS.js +2 -0
  27. package/dist/chunks/chunk-ZFQWB222.js +2 -0
  28. package/dist/data/grouping.js +26 -7
  29. package/dist/descriptive/basic.js +110 -8
  30. package/dist/descriptive/cov.js +23 -4
  31. package/dist/descriptive/dataFrames.d.ts +4 -2
  32. package/dist/descriptive/dataFrames.js +69 -23
  33. package/dist/descriptive/factanal.d.ts +2 -0
  34. package/dist/descriptive/factanal.js +26 -9
  35. package/dist/descriptive/hist.d.ts +8 -5
  36. package/dist/descriptive/hist.js +77 -31
  37. package/dist/descriptive/missing.js +3 -1
  38. package/dist/descriptive/moments.js +86 -19
  39. package/dist/descriptive/omega.d.ts +1 -1
  40. package/dist/descriptive/omega.js +74 -10
  41. package/dist/descriptive/pivot.d.ts +25 -10
  42. package/dist/descriptive/pivot.js +182 -42
  43. package/dist/descriptive/quantile.js +10 -1
  44. package/dist/descriptive/rank.js +8 -1
  45. package/dist/descriptive/table.d.ts +23 -3
  46. package/dist/descriptive/table.js +36 -21
  47. package/dist/descriptive.js +2 -2
  48. package/dist/distributions/arcsine.js +27 -2
  49. package/dist/distributions/beta.js +33 -4
  50. package/dist/distributions/binomial.js +46 -14
  51. package/dist/distributions/bvnorm.js +11 -2
  52. package/dist/distributions/cauchy.js +21 -4
  53. package/dist/distributions/chisq.js +16 -3
  54. package/dist/distributions/exponential.js +14 -0
  55. package/dist/distributions/f.js +41 -17
  56. package/dist/distributions/gamma.js +35 -3
  57. package/dist/distributions/geometric.js +58 -13
  58. package/dist/distributions/hypergeometric.d.ts +5 -1
  59. package/dist/distributions/hypergeometric.js +101 -6
  60. package/dist/distributions/invgamma.js +10 -1
  61. package/dist/distributions/kumaraswamy.js +9 -0
  62. package/dist/distributions/laplace.js +36 -6
  63. package/dist/distributions/logistic.js +13 -1
  64. package/dist/distributions/lognormal.js +16 -2
  65. package/dist/distributions/multinomial.js +58 -11
  66. package/dist/distributions/nbeta.js +62 -11
  67. package/dist/distributions/nchisq.js +30 -6
  68. package/dist/distributions/negbinomial.js +102 -17
  69. package/dist/distributions/normal.js +17 -4
  70. package/dist/distributions/nt.js +392 -17
  71. package/dist/distributions/pareto.js +12 -1
  72. package/dist/distributions/poisson.js +37 -3
  73. package/dist/distributions/signrank.js +16 -4
  74. package/dist/distributions/summary.d.ts +2 -1
  75. package/dist/distributions/summary.js +481 -61
  76. package/dist/distributions/t.js +9 -0
  77. package/dist/distributions/triangular.js +19 -5
  78. package/dist/distributions/tukey.js +11 -2
  79. package/dist/distributions/uniform.js +8 -0
  80. package/dist/distributions/weibull.js +18 -0
  81. package/dist/distributions/wilcox.d.ts +9 -3
  82. package/dist/distributions/wilcox.js +164 -26
  83. package/dist/distributions.js +2 -2
  84. package/dist/entries/descriptive.d.ts +3 -1
  85. package/dist/entries/descriptive.js +1 -1
  86. package/dist/entries/distributions.d.ts +1 -0
  87. package/dist/entries/distributions.js +2 -0
  88. package/dist/entries/linalg.d.ts +3 -1
  89. package/dist/entries/mixed.d.ts +3 -0
  90. package/dist/entries/mixed.js +2 -0
  91. package/dist/entries/numerics.d.ts +2 -1
  92. package/dist/entries/tests.d.ts +8 -5
  93. package/dist/entries/tests.js +3 -1
  94. package/dist/htest/agreement.d.ts +16 -2
  95. package/dist/htest/agreement.js +122 -21
  96. package/dist/htest/ansariTest.d.ts +8 -2
  97. package/dist/htest/ansariTest.js +116 -18
  98. package/dist/htest/aov.d.ts +29 -23
  99. package/dist/htest/aov.js +90 -47
  100. package/dist/htest/binomTest.d.ts +5 -0
  101. package/dist/htest/binomTest.js +47 -17
  102. package/dist/htest/brunnerMunzelTest.d.ts +4 -3
  103. package/dist/htest/brunnerMunzelTest.js +12 -5
  104. package/dist/htest/chisqTest.d.ts +5 -2
  105. package/dist/htest/chisqTest.js +151 -46
  106. package/dist/htest/cor.d.ts +9 -2
  107. package/dist/htest/cor.js +57 -26
  108. package/dist/htest/core/ansari.d.ts +9 -0
  109. package/dist/htest/core/ansari.js +21 -1
  110. package/dist/htest/core/kolmogorov.d.ts +25 -0
  111. package/dist/htest/core/kolmogorov.js +123 -0
  112. package/dist/htest/core/mantelhaen.js +6 -4
  113. package/dist/htest/core/shapiro.d.ts +1 -1
  114. package/dist/htest/core/shapiro.js +5 -1
  115. package/dist/htest/core/simulate.d.ts +6 -0
  116. package/dist/htest/core/simulate.js +20 -0
  117. package/dist/htest/dunnett.js +14 -5
  118. package/dist/htest/effectSize.d.ts +5 -0
  119. package/dist/htest/effectSize.js +117 -35
  120. package/dist/htest/fisherTest.d.ts +4 -3
  121. package/dist/htest/fisherTest.js +88 -24
  122. package/dist/htest/groups.d.ts +7 -0
  123. package/dist/htest/groups.js +21 -4
  124. package/dist/htest/ksTest.d.ts +7 -1
  125. package/dist/htest/ksTest.js +36 -6
  126. package/dist/htest/leveneTest.d.ts +2 -0
  127. package/dist/htest/leveneTest.js +28 -6
  128. package/dist/htest/mantelhaenTest.js +132 -25
  129. package/dist/htest/mcnemarTest.js +40 -11
  130. package/dist/htest/multipleComparisons.d.ts +24 -26
  131. package/dist/htest/multipleComparisons.js +70 -64
  132. package/dist/htest/normalityTests.js +54 -14
  133. package/dist/htest/onewayTest.js +106 -5
  134. package/dist/htest/pAdjust.d.ts +3 -2
  135. package/dist/htest/pAdjust.js +42 -6
  136. package/dist/htest/pairwise.d.ts +3 -1
  137. package/dist/htest/pairwise.js +46 -21
  138. package/dist/htest/poissonTest.d.ts +1 -1
  139. package/dist/htest/poissonTest.js +117 -59
  140. package/dist/htest/polycor.js +47 -17
  141. package/dist/htest/power.d.ts +15 -1
  142. package/dist/htest/power.js +128 -10
  143. package/dist/htest/propTest.d.ts +3 -0
  144. package/dist/htest/propTest.js +44 -6
  145. package/dist/htest/pwr.d.ts +38 -1
  146. package/dist/htest/pwr.js +159 -39
  147. package/dist/htest/rankTests.js +36 -6
  148. package/dist/htest/tTest.d.ts +2 -0
  149. package/dist/htest/tTest.js +53 -21
  150. package/dist/htest/testFormula.js +11 -11
  151. package/dist/htest/trendTests.d.ts +28 -4
  152. package/dist/htest/trendTests.js +124 -35
  153. package/dist/htest/types.d.ts +7 -0
  154. package/dist/htest/types.js +7 -0
  155. package/dist/htest/varTest.js +52 -14
  156. package/dist/htest/wilcoxTest.js +164 -29
  157. package/dist/integrate/quadpack.js +27 -5
  158. package/dist/internal/checkOptions.d.ts +11 -0
  159. package/dist/internal/checkOptions.js +31 -0
  160. package/dist/internal/covmat.d.ts +5 -0
  161. package/dist/internal/covmat.js +20 -0
  162. package/dist/internal/ddexp.d.ts +15 -1
  163. package/dist/internal/ddexp.js +59 -6
  164. package/dist/internal/discrete.d.ts +14 -0
  165. package/dist/internal/discrete.js +78 -5
  166. package/dist/internal/extremes.d.ts +9 -0
  167. package/dist/internal/extremes.js +24 -0
  168. package/dist/internal/fround.js +13 -4
  169. package/dist/internal/integer.d.ts +9 -0
  170. package/dist/internal/integer.js +12 -0
  171. package/dist/internal/invert.js +15 -2
  172. package/dist/internal/optionalArgs.d.ts +12 -0
  173. package/dist/internal/optionalArgs.js +46 -0
  174. package/dist/internal/scaledInverse.d.ts +10 -0
  175. package/dist/internal/scaledInverse.js +22 -0
  176. package/dist/internal/seq.js +15 -2
  177. package/dist/internal/summary.d.ts +90 -0
  178. package/dist/internal/summary.js +458 -39
  179. package/dist/internal/uniroot.d.ts +7 -0
  180. package/dist/internal/uniroot.js +28 -3
  181. package/dist/internal/warnings.d.ts +66 -0
  182. package/dist/internal/warnings.js +122 -0
  183. package/dist/linalg/basic.d.ts +3 -1
  184. package/dist/linalg/basic.js +58 -7
  185. package/dist/linalg/chol.js +10 -1
  186. package/dist/linalg/dot.d.ts +29 -0
  187. package/dist/linalg/dot.js +132 -0
  188. package/dist/linalg/eigen.d.ts +6 -1
  189. package/dist/linalg/eigen.js +75 -14
  190. package/dist/linalg/expm.d.ts +3 -2
  191. package/dist/linalg/expm.js +113 -15
  192. package/dist/linalg/kappa.js +64 -10
  193. package/dist/linalg/lu.d.ts +2 -1
  194. package/dist/linalg/lu.js +40 -5
  195. package/dist/linalg/matrix.d.ts +14 -0
  196. package/dist/linalg/matrix.js +24 -0
  197. package/dist/linalg/norm.js +4 -0
  198. package/dist/linalg/ops.d.ts +27 -2
  199. package/dist/linalg/ops.js +74 -26
  200. package/dist/linalg/qr.d.ts +2 -1
  201. package/dist/linalg/qr.js +105 -23
  202. package/dist/linalg/schur.js +11 -3
  203. package/dist/linalg/solve.d.ts +6 -1
  204. package/dist/linalg/solve.js +156 -26
  205. package/dist/linalg/svd.d.ts +9 -2
  206. package/dist/linalg/svd.js +33 -11
  207. package/dist/linalg.js +2 -2
  208. package/dist/mixed.js +4 -4
  209. package/dist/models/anovaSequential.d.ts +32 -2
  210. package/dist/models/anovaSequential.js +170 -28
  211. package/dist/models/aovEz.js +154 -39
  212. package/dist/models/aovStrata.js +68 -24
  213. package/dist/models/carAnova.d.ts +1 -1
  214. package/dist/models/carAnova.js +78 -33
  215. package/dist/models/coxph.d.ts +6 -0
  216. package/dist/models/coxph.js +166 -69
  217. package/dist/models/estimable.d.ts +15 -0
  218. package/dist/models/estimable.js +68 -0
  219. package/dist/models/formula.d.ts +1 -1
  220. package/dist/models/formula.js +26 -8
  221. package/dist/models/glm.d.ts +57 -7
  222. package/dist/models/glm.js +427 -74
  223. package/dist/models/glmAnova.js +90 -14
  224. package/dist/models/glmConfint.d.ts +1 -1
  225. package/dist/models/glmConfint.js +121 -29
  226. package/dist/models/glmFamily.d.ts +11 -6
  227. package/dist/models/glmFamily.js +247 -62
  228. package/dist/models/lm.d.ts +12 -1
  229. package/dist/models/lm.js +246 -37
  230. package/dist/models/lmer.d.ts +31 -7
  231. package/dist/models/lmer.js +129 -42
  232. package/dist/models/lmerAnova.d.ts +3 -3
  233. package/dist/models/lmerAnova.js +47 -9
  234. package/dist/models/lmerHypotheses.d.ts +6 -2
  235. package/dist/models/lmerHypotheses.js +14 -2
  236. package/dist/models/lmerTest.d.ts +13 -6
  237. package/dist/models/lmerTest.js +82 -29
  238. package/dist/models/mauchly.d.ts +6 -1
  239. package/dist/models/mauchly.js +41 -15
  240. package/dist/models/mixedStructure.js +6 -0
  241. package/dist/models/modelFrame.js +10 -4
  242. package/dist/models/modelMatrix.d.ts +3 -1
  243. package/dist/models/modelMatrix.js +47 -17
  244. package/dist/models/scaledTables.d.ts +8 -0
  245. package/dist/models/scaledTables.js +6 -0
  246. package/dist/models/survival.d.ts +12 -1
  247. package/dist/models/survival.js +75 -28
  248. package/dist/models/tiedTimes.d.ts +2 -0
  249. package/dist/models/tiedTimes.js +34 -0
  250. package/dist/numerics/deriv.d.ts +1 -1
  251. package/dist/numerics/deriv.js +72 -8
  252. package/dist/numerics/integrate.d.ts +9 -1
  253. package/dist/numerics/integrate.js +111 -16
  254. package/dist/numerics/interp.d.ts +40 -9
  255. package/dist/numerics/interp.js +375 -55
  256. package/dist/numerics/ode.js +59 -1
  257. package/dist/numerics.js +2 -2
  258. package/dist/optim/localmin.js +4 -0
  259. package/dist/optim/optim.d.ts +3 -1
  260. package/dist/optim/optim.js +97 -10
  261. package/dist/random/beta.js +58 -4
  262. package/dist/random/binomial.d.ts +14 -3
  263. package/dist/random/binomial.js +59 -24
  264. package/dist/random/continuous.js +50 -5
  265. package/dist/random/extra.js +55 -9
  266. package/dist/random/gamma.d.ts +4 -1
  267. package/dist/random/gamma.js +47 -8
  268. package/dist/random/hyper.js +9 -6
  269. package/dist/random/mt.js +17 -2
  270. package/dist/random/poisson.js +31 -3
  271. package/dist/random/r2dtable.d.ts +8 -1
  272. package/dist/random/r2dtable.js +24 -3
  273. package/dist/random/rank.d.ts +2 -0
  274. package/dist/random/rank.js +44 -4
  275. package/dist/random/rng.js +4 -0
  276. package/dist/random/sample.d.ts +12 -2
  277. package/dist/random/sample.js +63 -20
  278. package/dist/random/streams.js +4 -2
  279. package/dist/rstats.js +17 -14
  280. package/dist/special/besselIK.d.ts +12 -0
  281. package/dist/special/besselIK.js +175 -23
  282. package/dist/special/besselJY.d.ts +11 -1
  283. package/dist/special/besselJY.js +127 -19
  284. package/dist/special/besselSeries.d.ts +27 -0
  285. package/dist/special/besselSeries.js +45 -2
  286. package/dist/special/beta.js +4 -1
  287. package/dist/special/choose.js +24 -8
  288. package/dist/special/dd.js +122 -18
  289. package/dist/special/extra.js +14 -2
  290. package/dist/special/gamma.js +11 -2
  291. package/dist/special/incbet.js +30 -4
  292. package/dist/special/incbetTemme.d.ts +7 -0
  293. package/dist/special/incbetTemme.js +45 -12
  294. package/dist/special/loader.d.ts +1 -1
  295. package/dist/special/loader.js +117 -20
  296. package/dist/special/log1pmx.js +4 -0
  297. package/dist/special/psigamma.js +7 -0
  298. package/dist/special.js +2 -2
  299. package/dist/tests.js +15 -11
  300. package/llms.txt +15 -6
  301. package/package.json +19 -10
  302. package/dist/chunks/chunk-3THFTB36.js +0 -2
  303. package/dist/chunks/chunk-3TSWYE5L.js +0 -2
  304. package/dist/chunks/chunk-3WPU6LGK.js +0 -2
  305. package/dist/chunks/chunk-5ITR67MB.js +0 -2
  306. package/dist/chunks/chunk-5NRFU6F6.js +0 -2
  307. package/dist/chunks/chunk-E2LCSVQV.js +0 -2
  308. package/dist/chunks/chunk-FCNVTIPJ.js +0 -2
  309. package/dist/chunks/chunk-FKAKALOZ.js +0 -2
  310. package/dist/chunks/chunk-HD73UKPT.js +0 -2
  311. package/dist/chunks/chunk-IUULP3LE.js +0 -2
  312. package/dist/chunks/chunk-JQG3YQOC.js +0 -2
  313. package/dist/chunks/chunk-KT66SB7R.js +0 -2
  314. package/dist/chunks/chunk-MAUAOQG3.js +0 -2
  315. package/dist/chunks/chunk-MDYE7QP2.js +0 -2
  316. package/dist/chunks/chunk-NM3Z2UBQ.js +0 -2
  317. package/dist/chunks/chunk-PMJI5QO6.js +0 -3
  318. package/dist/chunks/chunk-UF7KOTHF.js +0 -3
  319. package/dist/chunks/chunk-UOE57JGT.js +0 -2
  320. package/dist/chunks/chunk-USPEBXSR.js +0 -2
  321. package/dist/chunks/chunk-VLUL2HFW.js +0 -3
  322. package/dist/chunks/chunk-ZIX7NYQF.js +0 -2
@@ -11,6 +11,10 @@ import { formatR } from "../htest/core/simulate.js";
11
11
  import { numberLevels } from "../internal/levels.js";
12
12
  import { expandRhs, formulaVariables, parseFormula } from "../models/formula.js";
13
13
  import { compareCodePoints } from "../models/modelFrame.js";
14
+ import { addColumn } from "../descriptive/dataFrames.js";
15
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
16
+ import { checkOptions } from "../internal/checkOptions.js";
17
+ const SPLIT_KEYS = ["drop", "sep"];
14
18
  const isMissing = (v) => v === null || (typeof v === "number" && Number.isNaN(v));
15
19
  const levelOrder = (a, b) => typeof a === "number" ? a - b : typeof a === "string" ? compareCodePoints(a, b) : Number(a) - Number(b);
16
20
  /** A level as R names it: numbers as as.character (15 significant digits), booleans as TRUE and FALSE */
@@ -48,6 +52,10 @@ function groupingOf(lists, names, n) {
48
52
  });
49
53
  const factors = lists.map((l, j) => factorOf(names[j], l));
50
54
  const size = factors.reduce((s, f) => s * f.levels.length, 1);
55
+ // R's tapply limit, before the per-cell arrays: 2^32 cells failed with V8's "Invalid array length", and from 2^31
56
+ // up the allocation was attempted (#363)
57
+ if (size >= 2 ** 31)
58
+ throw new RangeError("total number of levels >= 2^31");
51
59
  const cells = new Array(n);
52
60
  for (let i = 0; i < n; i++) {
53
61
  let cell = 0;
@@ -114,7 +122,8 @@ export function tapply(x, index, fun) {
114
122
  function rowsOf(data, rows) {
115
123
  const out = {};
116
124
  for (const [name, col] of Object.entries(data)) {
117
- out[name] = col instanceof Float64Array ? Float64Array.from(rows, (i) => col[i]) : rows.map((i) => col[i]);
125
+ // (addColumn: a column named "__proto__" stays an own column; issue #35)
126
+ addColumn(out, name, col instanceof Float64Array ? Float64Array.from(rows, (i) => col[i]) : rows.map((i) => col[i]));
118
127
  }
119
128
  return out;
120
129
  }
@@ -130,6 +139,7 @@ function rowCount(data) {
130
139
  return n;
131
140
  }
132
141
  export function split(x, f, options = {}) {
142
+ checkOptions(options, SPLIT_KEYS, "split"); // generated: option-keys.mjs
133
143
  const sep = options.sep ?? ".";
134
144
  const lists = labelLists(f);
135
145
  const frame = !isLabelArray(x);
@@ -143,6 +153,9 @@ export function split(x, f, options = {}) {
143
153
  if (rows.length === 0 && (options.drop || g.factors.length === 1))
144
154
  return;
145
155
  const name = cellLevels(g, cell).map(levelName).join(sep);
156
+ // Two different combinations of levels whose names joined with sep coincide would overwrite one another
157
+ if (out.has(name))
158
+ throw new RangeError(`the group names joined with '${sep}' are not unique ('${name}'); use another sep`);
146
159
  out.set(name, frame ? rowsOf(x, rows) : rows.map((i) => xs[i]));
147
160
  });
148
161
  return out;
@@ -150,7 +163,7 @@ export function split(x, f, options = {}) {
150
163
  /** A summarised column as numbers (null is NaN) */
151
164
  function numericColumn(name, col) {
152
165
  if (col === undefined)
153
- throw new Error(`object '${name}' not found`);
166
+ throw new RangeError(`object '${name}' not found`);
154
167
  if (!isLabelArray(col))
155
168
  throw new TypeError(`column '${name}' must be an array`);
156
169
  return Array.from(col, (v) => {
@@ -164,7 +177,7 @@ function numericColumn(name, col) {
164
177
  function labelColumn(data, name) {
165
178
  const col = Object.prototype.hasOwnProperty.call(data, name) ? data[name] : undefined;
166
179
  if (col === undefined)
167
- throw new Error(`object '${name}' not found`);
180
+ throw new RangeError(`object '${name}' not found`);
168
181
  if (!isLabelArray(col))
169
182
  throw new TypeError(`column '${name}' must be an array`);
170
183
  return col;
@@ -214,11 +227,17 @@ function aggregateRows(valueNames, valueCols, groupNames, groupCols, keep, fun)
214
227
  // The labels of each group's first row (-0 as 0), as R keeps them
215
228
  groupNames.forEach((name, j) => {
216
229
  const col = groupCols[j];
217
- out[name] = cells.map((c) => {
230
+ addColumn(out, name, cells.map((c) => {
218
231
  const v = col[c.rows[0]];
219
232
  return v === 0 ? 0 : v;
220
- });
233
+ }));
221
234
  });
235
+ // A result column whose name is already taken (by a grouping column or another result) would overwrite it
236
+ const setColumn = (name, v) => {
237
+ if (Object.hasOwn(out, name))
238
+ throw new RangeError(`duplicate column name '${name}' in the result`);
239
+ addColumn(out, name, v);
240
+ };
222
241
  valueNames.forEach((name, j) => {
223
242
  const col = valueCols[j];
224
243
  const results = cells.map((c) => fun(c.rows.map((i) => col[i])));
@@ -234,12 +253,12 @@ function aggregateRows(valueNames, valueCols, groupNames, groupCols, keep, fun)
234
253
  if (flat.some((r) => r.values.length !== k))
235
254
  throw new RangeError("fun must return values of the same length for every group");
236
255
  if (k === 1 && flat[0]?.names === undefined) {
237
- out[name] = flat.map((r) => r.values[0]);
256
+ setColumn(name, flat.map((r) => r.values[0]));
238
257
  return;
239
258
  }
240
259
  for (let m = 0; m < k; m++) {
241
260
  const suffix = flat[0].names?.[m] ?? String(m + 1);
242
- out[`${name}.${suffix}`] = flat.map((r) => r.values[m]);
261
+ setColumn(`${name}.${suffix}`, flat.map((r) => r.values[m]));
243
262
  }
244
263
  });
245
264
  return out;
@@ -8,8 +8,20 @@
8
8
  // overflow (src/internal/summary.ts), and products keep a separate power-of-two exponent, with factors far from 1
9
9
  // scaled before they are multiplied.
10
10
  // As everywhere in rStats.js, NaN stands for R's NA: with `naRm` it is dropped, otherwise it propagates.
11
- import { ldexp, mean as meanOf, meanDD, sum as sumOf, variance as varianceOf, wideSum, wideSumAdd, wideSumValue, } from "../internal/summary.js";
11
+ import { ldexp, scaleToUnit, mean as meanOf, meanDD, sum as sumOf, standardDeviation, variance as varianceOf, wideSum, wideSumAdd, wideSumValue, } from "../internal/summary.js";
12
12
  import { ddMul } from "../special/dd.js";
13
+ import { roundScaled, splitDouble } from "../linalg/dot.js";
14
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
15
+ import { checkOptions } from "../internal/checkOptions.js";
16
+ const SUM_KEYS = ["naRm"];
17
+ const PROD_KEYS = ["naRm"];
18
+ const MEAN_KEYS = ["trim", "naRm"];
19
+ const WEIGHTED_MEAN_KEYS = ["naRm"];
20
+ const VARIANCE_KEYS = ["naRm"];
21
+ const SD_KEYS = ["naRm"];
22
+ const MEDIAN_KEYS = ["naRm"];
23
+ const RANGE_KEYS = ["naRm"];
24
+ const DIFF_KEYS = ["lag", "differences"];
13
25
  /** The values as an array, NaN dropped when naRm; undefined when a NaN remains (the result is then NaN) */
14
26
  export function values(x, naRm) {
15
27
  const xs = Array.from(x);
@@ -28,6 +40,7 @@ export function values(x, naRm) {
28
40
  * @category Descriptive statistics
29
41
  */
30
42
  export function sum(x, options = {}) {
43
+ checkOptions(options, SUM_KEYS, "sum"); // generated: option-keys.mjs
31
44
  const xs = values(x, options.naRm);
32
45
  return xs === undefined ? NaN : sumOf(xs);
33
46
  }
@@ -42,6 +55,7 @@ export function sum(x, options = {}) {
42
55
  * @category Descriptive statistics
43
56
  */
44
57
  export function prod(x, options = {}) {
58
+ checkOptions(options, PROD_KEYS, "prod"); // generated: option-keys.mjs
45
59
  const xs = values(x, options.naRm);
46
60
  if (xs === undefined)
47
61
  return NaN;
@@ -93,15 +107,19 @@ function prodSign(acc) {
93
107
  * @category Descriptive statistics
94
108
  */
95
109
  export function mean(x, options = {}) {
110
+ checkOptions(options, MEAN_KEYS, "mean"); // generated: option-keys.mjs
96
111
  const xs = values(x, options.naRm);
112
+ let trim = options.trim ?? 0;
113
+ // R tests `trim > 0 && n` with n the length after na.rm, before looking at missing values: a missing value in x
114
+ // (n >= 1) is an error too, only an empty x escapes (mean(c(1, NA), trim = NaN) stops in R)
115
+ if (Number.isNaN(trim) && (xs === undefined || xs.length > 0)) {
116
+ throw new RangeError("'trim' must be numeric of length one");
117
+ }
97
118
  if (xs === undefined)
98
119
  return NaN;
99
120
  const n = xs.length;
100
121
  if (n === 0)
101
122
  return NaN;
102
- let trim = options.trim ?? 0;
103
- if (Number.isNaN(trim))
104
- throw new RangeError("'trim' must be numeric of length one");
105
123
  if (trim > 0 && n > 0) {
106
124
  if (trim >= 0.5)
107
125
  return median(xs);
@@ -124,6 +142,7 @@ export function mean(x, options = {}) {
124
142
  * @category Descriptive statistics
125
143
  */
126
144
  export function weightedMean(x, w, options = {}) {
145
+ checkOptions(options, WEIGHTED_MEAN_KEYS, "weightedMean"); // generated: option-keys.mjs
127
146
  let xs = Array.from(x);
128
147
  let ws = Array.from(w);
129
148
  if (xs.length !== ws.length)
@@ -134,10 +153,84 @@ export function weightedMean(x, w, options = {}) {
134
153
  ws = ws.filter((_, i) => keep[i]);
135
154
  }
136
155
  const prods = [];
137
- for (let i = 0; i < xs.length; i++)
156
+ // a product of nonzero factors below the normal range has lost bits (1e-200 * 1e-200 is 0, #364)
157
+ let lost = false;
158
+ for (let i = 0; i < xs.length; i++) {
159
+ if (ws[i] === 0)
160
+ continue;
161
+ const p = xs[i] * ws[i];
162
+ if (xs[i] !== 0 && Math.abs(p) < 2 ** -1022)
163
+ lost = true;
164
+ prods.push(p);
165
+ }
166
+ const sw = sumOf(ws);
167
+ const m = sumOf(prods) / sw;
168
+ if ((Number.isFinite(m) && Number.isFinite(sw) && !lost) || !xs.every(Number.isFinite) || !ws.every(Number.isFinite)) {
169
+ return m;
170
+ }
171
+ // Exact quotient where a product underflowed, or where the scaling below would lose a weight: signed weights whose
172
+ // sum cancels to far below the largest weight (1e-308 next to 1e308 and -1e308 became 0 when divided by 2^1024, so
173
+ // the mean was 0 / 0, #334)
174
+ const exact = exactWeightedMean(xs, ws);
175
+ if (exact !== undefined)
176
+ return exact;
177
+ // Finite values whose products or sums overflow (R returns Inf, NaN, or 0 where only the sum of the weights
178
+ // overflows, #201): the values of nonzero weight and the weights divided by powers of 2 that bring their largest
179
+ // absolute values into [1, 2), so that neither the products nor the sums overflow, and small values are not lost to
180
+ // the division (dividing by the largest values themselves turned 1e-308 / 1e308 into 0)
181
+ const used = xs.filter((_, i) => ws[i] !== 0);
182
+ const { groups: [sx], k } = scaleToUnit([used]);
183
+ const { groups: [sws] } = scaleToUnit([ws]);
184
+ const scaled = [];
185
+ for (let i = 0, j = 0; i < xs.length; i++)
138
186
  if (ws[i] !== 0)
139
- prods.push(xs[i] * ws[i]);
140
- return sumOf(prods) / sumOf(ws);
187
+ scaled.push(sx[j++] * sws[i]);
188
+ return ldexp(sumOf(scaled) / sumOf(sws), k);
189
+ }
190
+ /**
191
+ * sum(x w) / sum(w) for finite x and w, each sum exact in BigInt (the products as integer mantissas times powers of
192
+ * 2) and the quotient rounded once. undefined where the sum of the weights is exactly 0 (no mean)
193
+ */
194
+ function exactWeightedMean(xs, ws) {
195
+ const num = [];
196
+ const den = [];
197
+ for (let i = 0; i < xs.length; i++) {
198
+ if (ws[i] === 0)
199
+ continue;
200
+ const [mw, ew] = splitDouble(ws[i]);
201
+ den.push([mw, ew]);
202
+ if (xs[i] === 0)
203
+ continue;
204
+ const [mx, ex] = splitDouble(xs[i]);
205
+ num.push([mx * mw, ex + ew]);
206
+ }
207
+ const exactSum = (terms) => {
208
+ let low = 0;
209
+ for (const [, e] of terms)
210
+ low = Math.min(low, e);
211
+ let s = 0n;
212
+ for (const [m, e] of terms)
213
+ s += m << BigInt(e - low);
214
+ return [s, low];
215
+ };
216
+ let [n, ln] = exactSum(num);
217
+ let [d, ld] = exactSum(den);
218
+ if (d === 0n)
219
+ return undefined;
220
+ if (n === 0n)
221
+ return 0;
222
+ if (d < 0n) {
223
+ n = -n;
224
+ d = -d;
225
+ }
226
+ // n / d 2^(ln - ld): the integer quotient with at least 66 bits, its last bit set where it is not exact
227
+ const negative = n < 0n;
228
+ const a = negative ? -n : n;
229
+ const shift = Math.max(0, d.toString(2).length - a.toString(2).length + 66);
230
+ let q = (a << BigInt(shift)) / d;
231
+ if (q * d !== a << BigInt(shift))
232
+ q |= 1n;
233
+ return roundScaled(negative ? -q : q, ln - ld - shift);
141
234
  }
142
235
  /**
143
236
  * Sample variance (denominator n - 1). Equivalent to R's `var()`. For a vector (`NaN` for fewer than 2 values); named
@@ -151,6 +244,7 @@ export function weightedMean(x, w, options = {}) {
151
244
  * @category Descriptive statistics
152
245
  */
153
246
  export function variance(x, options = {}) {
247
+ checkOptions(options, VARIANCE_KEYS, "variance"); // generated: option-keys.mjs
154
248
  const xs = values(x, options.naRm);
155
249
  if (xs === undefined)
156
250
  return NaN;
@@ -167,7 +261,12 @@ export function variance(x, options = {}) {
167
261
  * @category Descriptive statistics
168
262
  */
169
263
  export function sd(x, options = {}) {
170
- return Math.sqrt(variance(x, options));
264
+ checkOptions(options, SD_KEYS, "sd"); // generated: option-keys.mjs
265
+ // not sqrt(variance): the variance can overflow or underflow where the standard deviation is in range
266
+ const xs = values(x, options.naRm);
267
+ if (xs === undefined)
268
+ return NaN;
269
+ return standardDeviation(xs);
171
270
  }
172
271
  /**
173
272
  * Median of the values (the mean of the two middle ones for an even count). Equivalent to R's `median()`.
@@ -181,6 +280,7 @@ export function sd(x, options = {}) {
181
280
  * @category Descriptive statistics
182
281
  */
183
282
  export function median(x, options = {}) {
283
+ checkOptions(options, MEDIAN_KEYS, "median"); // generated: option-keys.mjs
184
284
  const xs = values(x, options.naRm);
185
285
  if (xs === undefined)
186
286
  return NaN;
@@ -204,6 +304,7 @@ export function median(x, options = {}) {
204
304
  * @category Descriptive statistics
205
305
  */
206
306
  export function range(x, options = {}) {
307
+ checkOptions(options, RANGE_KEYS, "range"); // generated: option-keys.mjs
207
308
  const xs = values(x, options.naRm);
208
309
  if (xs === undefined)
209
310
  return [NaN, NaN];
@@ -347,6 +448,7 @@ export function cummin(x) {
347
448
  * @category Descriptive statistics
348
449
  */
349
450
  export function diff(x, options = {}) {
451
+ checkOptions(options, DIFF_KEYS, "diff"); // generated: option-keys.mjs
350
452
  const lag = options.lag ?? 1;
351
453
  const differences = options.differences ?? 1;
352
454
  if (!(Number.isInteger(lag) && lag >= 1 && Number.isInteger(differences) && differences >= 1)) {
@@ -10,6 +10,11 @@
10
10
  import { kendall, pearsonDD } from "../htest/cor.js";
11
11
  import { covariance } from "../internal/summary.js";
12
12
  import { rank } from "./rank.js";
13
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
14
+ import { checkOptions } from "../internal/checkOptions.js";
15
+ import { isOptionsObject } from "../internal/optionalArgs.js";
16
+ const COV_KEYS = ["method", "use"];
17
+ const COR_KEYS = ["method", "use"];
13
18
  const METHODS = ["pearson", "kendall", "spearman"];
14
19
  const USES = ["all.obs", "complete.obs", "pairwise.complete.obs", "everything", "na.or.complete"];
15
20
  function match(v, all, dflt, what) {
@@ -30,6 +35,8 @@ function columns(x) {
30
35
  if (!isMatrix(x))
31
36
  return [Array.from(x)];
32
37
  const ncol = x[0].length;
38
+ if (x.some((r) => !Array.isArray(r) || r.length !== ncol))
39
+ throw new TypeError("all rows of the matrix must have the same length");
33
40
  return Array.from({ length: ncol }, (_, j) => x.map((r) => r[j]));
34
41
  }
35
42
  function compute(x, y, options, correlation) {
@@ -91,19 +98,31 @@ function compute(x, y, options, correlation) {
91
98
  return covariance(u, v);
92
99
  };
93
100
  const out = px.map((a) => py.map((b) => entry(a, b)));
94
- // as in R, the correlation matrix of x alone has 1 on the diagonal, whatever the column holds, unless fewer than 2
95
- // observations are used (then it is all NaN)
101
+ // the correlation matrix of x alone has 1 on the diagonal (a computed 0.9999999999999998 made 1, as in R) where the
102
+ // correlation of a column with itself is defined; it stays NaN for a constant column (0/0) and, with "everything", for
103
+ // a column with a missing value (R puts 1 there too, whatever the column holds). With fewer than 2 observations
104
+ // used, all is NaN
96
105
  const used = keepRows === undefined ? n : keepRows.filter(Boolean).length;
97
- if (correlation && y === undefined && use !== "pairwise.complete.obs" && used >= 2)
98
- out.forEach((r, i) => (r[i] = 1));
106
+ if (correlation && y === undefined && use !== "pairwise.complete.obs" && used >= 2) {
107
+ out.forEach((r, i) => {
108
+ if (!Number.isNaN(r[i]))
109
+ r[i] = 1;
110
+ });
111
+ }
99
112
  return (y === undefined ? false : !isMatrix(x) && !isMatrix(y)) ? out[0][0] : out;
100
113
  }
101
114
  export function cov(x, y, options = {}) {
115
+ if (isOptionsObject(y))
116
+ checkOptions(y, COV_KEYS, "cov"); // generated: option-keys.mjs
117
+ checkOptions(options, COV_KEYS, "cov"); // generated: option-keys.mjs
102
118
  if (y !== undefined && !Array.isArray(y) && !ArrayBuffer.isView(y))
103
119
  return compute(x, undefined, y, false);
104
120
  return compute(x, y, options, false);
105
121
  }
106
122
  export function cor(x, y, options = {}) {
123
+ if (isOptionsObject(y))
124
+ checkOptions(y, COR_KEYS, "cor"); // generated: option-keys.mjs
125
+ checkOptions(options, COR_KEYS, "cor"); // generated: option-keys.mjs
107
126
  if (y !== undefined && !Array.isArray(y) && !ArrayBuffer.isView(y))
108
127
  return compute(x, undefined, y, true);
109
128
  return compute(x, y, options, true);
@@ -47,8 +47,10 @@ export interface MergeOptions {
47
47
  }
48
48
  /**
49
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
50
+ * with equal keys (missing keys match each other; key columns of different types are compared in their common type,
51
+ * as R's match: text, or numbers with TRUE as 1), and with `allX` / `allY` the rows without a match. Columns: the
52
+ * keys (named as in x; in the common type when rows only in y are kept), x's other columns, y's other columns; a
53
+ * non-key name present in both gets the suffixes. Rows are
52
54
  * ordered by the key unless `sort` is false (several keys: as text joined with "\r" unless `allX` or `allY`, as R
53
55
  * does); without key columns, the cross join (x varying fastest).
54
56
  *
@@ -7,6 +7,11 @@
7
7
  // null). merge reproduces R's row order exactly, including the order that R's (unstable) Shell sort of the row
8
8
  // indices gives to rows that share a key.
9
9
  import { compareCodePoints } from "../models/modelFrame.js";
10
+ // Generated by `node scripts/option-keys.mjs --write` from the option types; do not edit (begin)
11
+ import { checkOptions } from "../internal/checkOptions.js";
12
+ const MERGE_KEYS = ["by", "byX", "byY", "all", "allX", "allY", "sort", "suffixes"];
13
+ const SUBSET_KEYS = ["select"];
14
+ const FROM_ROWS_KEYS = ["columns"];
10
15
  /** The number of rows of a data frame (0 without columns); all columns must have the same length */
11
16
  export function rowCount(frame) {
12
17
  const cols = Object.values(frame);
@@ -36,8 +41,9 @@ export function pick(col, rows) {
36
41
  /** Adds a column to a result under construction; names must be unique */
37
42
  export function addColumn(out, name, col) {
38
43
  if (Object.hasOwn(out, name))
39
- throw new Error(`duplicate column name '${name}'`);
40
- out[name] = col;
44
+ throw new RangeError(`duplicate column name '${name}'`);
45
+ // defineProperty, so that a column named "__proto__" is an own column rather than setting the prototype
46
+ Object.defineProperty(out, name, { value: col, enumerable: true, writable: true, configurable: true });
41
47
  }
42
48
  /**
43
49
  * A number as R's as.character writes it: up to 15 significant digits, in fixed notation unless the scientific one
@@ -145,14 +151,16 @@ const nameList = (v) => (typeof v === "string" ? [v] : [...v]);
145
151
  function keyColumns(frame, names) {
146
152
  return names.map((n) => {
147
153
  if (!Object.hasOwn(frame, n))
148
- throw new Error("'by' must specify a uniquely valid column");
154
+ throw new RangeError("'by' must specify a uniquely valid column");
149
155
  return frame[n];
150
156
  });
151
157
  }
152
158
  /**
153
159
  * Join two data frames on key columns. Equivalent to R's `merge()`. For data frames: every pair of rows of x and y
154
- * with equal keys (missing keys match each other), and with `allX` / `allY` the rows without a match. Columns: the keys
155
- * (named as in x), x's other columns, y's other columns; a non-key name present in both gets the suffixes. Rows are
160
+ * with equal keys (missing keys match each other; key columns of different types are compared in their common type,
161
+ * as R's match: text, or numbers with TRUE as 1), and with `allX` / `allY` the rows without a match. Columns: the
162
+ * keys (named as in x; in the common type when rows only in y are kept), x's other columns, y's other columns; a
163
+ * non-key name present in both gets the suffixes. Rows are
156
164
  * ordered by the key unless `sort` is false (several keys: as text joined with "\r" unless `allX` or `allY`, as R
157
165
  * does); without key columns, the cross join (x varying fastest).
158
166
  *
@@ -167,6 +175,7 @@ function keyColumns(frame, names) {
167
175
  * @category Data frames
168
176
  */
169
177
  export function merge(x, y, options = {}) {
178
+ checkOptions(options, MERGE_KEYS, "merge"); // generated: option-keys.mjs
170
179
  const xNames = Object.keys(x);
171
180
  const yNames = Object.keys(y);
172
181
  const nx = rowCount(x);
@@ -177,7 +186,7 @@ export function merge(x, y, options = {}) {
177
186
  const keysX = keyColumns(x, byX);
178
187
  const keysY = keyColumns(y, byY);
179
188
  if (byX.length !== byY.length)
180
- throw new Error("'by.x' and 'by.y' specify different numbers of columns");
189
+ throw new TypeError("'by.x' and 'by.y' specify different numbers of columns");
181
190
  const allX = options.allX ?? options.all ?? false;
182
191
  const allY = options.allY ?? options.all ?? false;
183
192
  const sort = options.sort ?? true;
@@ -194,12 +203,28 @@ export function merge(x, y, options = {}) {
194
203
  pairs.push([i, j]);
195
204
  }
196
205
  else {
206
+ // Each key column of x and y compared in their common type, as R's match: text if either holds text, numbers
207
+ // (TRUE as 1) if numbers meet logicals (merge({ id: [1] }, { id: ["1"] }) matched nothing, #82)
208
+ const common = keysX.map((cx, k) => {
209
+ const kinds = new Set([...cx, ...keysY[k]].filter((v) => !isMissing(v)).map((v) => typeof v));
210
+ if (kinds.has("string"))
211
+ return (v) => (typeof v === "string" ? v : valueText(v));
212
+ if (kinds.has("number") && kinds.has("boolean"))
213
+ return (v) => (typeof v === "boolean" ? Number(v) : v);
214
+ return (v) => v;
215
+ });
216
+ const commonAt = (cols, k, i) => {
217
+ const v = valueAt(cols[k], i);
218
+ return isMissing(v) ? MISSING_KEY : common[k](v);
219
+ };
197
220
  const keyOf = byX.length === 1
198
- ? (cols, i) => {
199
- const v = valueAt(cols[0], i);
200
- return isMissing(v) ? MISSING_KEY : v;
201
- }
202
- : (cols, i) => cols.map((c) => valueText(valueAt(c, i))).join("\r");
221
+ ? (cols, i) => commonAt(cols, 0, i)
222
+ : // Matched on the values' text, as R, but encoded unambiguously: R pastes them with "\r", so that a key
223
+ // containing "\r", or "NA" against a missing value, matches a different key
224
+ (cols, i) => JSON.stringify(cols.map((_, k) => {
225
+ const v = commonAt(cols, k, i);
226
+ return v === MISSING_KEY ? null : valueText(v);
227
+ }));
203
228
  const kx = Array.from({ length: nx }, (_, i) => keyOf(keysX, i));
204
229
  const ky = Array.from({ length: ny }, (_, j) => keyOf(keysY, j));
205
230
  const inY = new Set(ky);
@@ -228,13 +253,21 @@ export function merge(x, y, options = {}) {
228
253
  for (const j of sy.alone)
229
254
  pairs.push([-1, j]);
230
255
  }
256
+ // The key columns of the result (in the order of pairs): x's values, and y's for the rows only in y; those rows
257
+ // bring the columns to their common type, as R's rbind of the two sides does (x's type otherwise)
258
+ const yOnly = pairs.some(([i]) => i < 0);
259
+ const fromX = pairs.some(([i]) => i >= 0);
260
+ const keyCols = keysX.map((cx, k) => {
261
+ const cy = keysY[k];
262
+ // a Float64Array when the columns the values come from are (x's alone without rows only in y, #449)
263
+ const sources = fromX || !yOnly ? (yOnly ? [cx, cy] : [cx]) : [cy];
264
+ const col = gather(sources, pairs.map(([i, j]) => (i >= 0 ? [cx, i] : [cy, j])));
265
+ return yOnly ? commonType(col) : col;
266
+ });
231
267
  // Rows sorted stably by the key
232
268
  let order = pairs.map((_, r) => r);
233
269
  if (sort && byX.length > 0) {
234
- const keyValue = (r, k) => {
235
- const [i, j] = pairs[r];
236
- return i >= 0 ? valueAt(keysX[k], i) : valueAt(keysY[k], j);
237
- };
270
+ const keyValue = (r, k) => valueAt(keyCols[k], r);
238
271
  if (byX.length > 1 && !allX && !allY) {
239
272
  const text = pairs.map((_, r) => byX.map((_, k) => valueText(keyValue(r, k))).join("\r"));
240
273
  order.sort((a, b) => compareCodePoints(text[a], text[b]));
@@ -252,11 +285,7 @@ export function merge(x, y, options = {}) {
252
285
  }
253
286
  const sorted = order.map((r) => pairs[r]);
254
287
  const out = {};
255
- byX.forEach((n, k) => {
256
- const cx = keysX[k];
257
- const cy = keysY[k];
258
- addColumn(out, n, gather([cx, cy], sorted.map(([i, j]) => (i >= 0 ? [cx, i] : [cy, j]))));
259
- });
288
+ byX.forEach((n, k) => addColumn(out, n, pick(keyCols[k], order)));
260
289
  for (const n of restX) {
261
290
  const c = x[n];
262
291
  addColumn(out, nameX(n), gather([c], sorted.map(([i]) => (i >= 0 ? [c, i] : null))));
@@ -282,6 +311,7 @@ export function merge(x, y, options = {}) {
282
311
  * @category Data frames
283
312
  */
284
313
  export function subset(data, keep, options = {}) {
314
+ checkOptions(options, SUBSET_KEYS, "subset"); // generated: option-keys.mjs
285
315
  const names = Object.keys(data);
286
316
  const n = rowCount(data);
287
317
  if (typeof keep !== "function" && keep.length !== n) {
@@ -296,7 +326,7 @@ export function subset(data, keep, options = {}) {
296
326
  const out = {};
297
327
  for (const c of select) {
298
328
  if (!Object.hasOwn(data, c))
299
- throw new Error("undefined columns selected");
329
+ throw new RangeError("undefined columns selected");
300
330
  addColumn(out, c, pick(data[c], rows));
301
331
  }
302
332
  return out;
@@ -313,17 +343,32 @@ export function rbindFrames(frames) {
313
343
  rowCount(f);
314
344
  const other = Object.keys(f);
315
345
  if (other.length !== names.length || !names.every((n) => Object.hasOwn(f, n))) {
316
- throw new Error("names do not match previous names");
346
+ throw new TypeError("names do not match previous names");
317
347
  }
318
348
  }
319
349
  const out = {};
320
350
  for (const n of names) {
321
351
  const cols = frames.map((f) => f[n]);
322
352
  const rows = cols.flatMap((c) => Array.from({ length: c.length }, (_, i) => [c, i]));
323
- out[n] = gather(cols, rows);
353
+ addColumn(out, n, commonType(gather(cols, rows)));
324
354
  }
325
355
  return out;
326
356
  }
357
+ /**
358
+ * A column of mixed value types converted to their common type, as R's rbind does: text if any value is text
359
+ * (numbers as as.character writes them, logicals as "TRUE" / "FALSE"), otherwise numbers (TRUE as 1) when numbers
360
+ * and logicals are mixed
361
+ */
362
+ function commonType(col) {
363
+ if (col instanceof Float64Array)
364
+ return col;
365
+ const kinds = new Set(col.filter((v) => !isMissing(v)).map((v) => typeof v));
366
+ if (kinds.size < 2)
367
+ return col;
368
+ if (kinds.has("string"))
369
+ return col.map((v) => (isMissing(v) ? null : typeof v === "string" ? v : valueText(v)));
370
+ return col.map((v) => (isMissing(v) ? null : typeof v === "boolean" ? (v ? 1 : 0) : v));
371
+ }
327
372
  /**
328
373
  * Puts data frames with the same number of rows side by side. The data frame form of R's `cbind()`; see
329
374
  * {@link cbind}. A column name that occurs twice is an error (an object cannot hold two columns of the same name).
@@ -354,6 +399,7 @@ export function cbindFrames(frames) {
354
399
  * @category Data frames
355
400
  */
356
401
  export function fromRows(rows, options = {}) {
402
+ checkOptions(options, FROM_ROWS_KEYS, "fromRows"); // generated: option-keys.mjs
357
403
  let names = options.columns;
358
404
  if (names === undefined) {
359
405
  const seen = new Set();
@@ -63,6 +63,8 @@ export interface FactanalResult {
63
63
  objective: number;
64
64
  /** optim's counts of function and gradient evaluations */
65
65
  counts: [number, number];
66
+ /** R's warnings, each once, in R's order (an empty array when there are none). */
67
+ warnings: string[];
66
68
  }
67
69
  /** Sum rounded once (R's long double accumulation, here double-double); also used by omega */
68
70
  export declare function sumOnce(xs: readonly number[]): number;