@numero-uno-jpn/rstats 0.0.0-stage → 0.5.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 (159) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/LICENSE +21 -0
  3. package/README.ja.md +181 -0
  4. package/README.md +172 -2
  5. package/THIRD_PARTY_NOTICES.md +297 -0
  6. package/dist/distributions/beta.d.ts +62 -0
  7. package/dist/distributions/beta.js +246 -0
  8. package/dist/distributions/binomial.d.ts +41 -0
  9. package/dist/distributions/binomial.js +136 -0
  10. package/dist/distributions/cauchy.d.ts +34 -0
  11. package/dist/distributions/cauchy.js +139 -0
  12. package/dist/distributions/chisq.d.ts +42 -0
  13. package/dist/distributions/chisq.js +60 -0
  14. package/dist/distributions/exponential.d.ts +34 -0
  15. package/dist/distributions/exponential.js +93 -0
  16. package/dist/distributions/f.d.ts +47 -0
  17. package/dist/distributions/f.js +214 -0
  18. package/dist/distributions/gamma.d.ts +61 -0
  19. package/dist/distributions/gamma.js +188 -0
  20. package/dist/distributions/geometric.d.ts +38 -0
  21. package/dist/distributions/geometric.js +116 -0
  22. package/dist/distributions/hypergeometric.d.ts +38 -0
  23. package/dist/distributions/hypergeometric.js +234 -0
  24. package/dist/distributions/logistic.d.ts +36 -0
  25. package/dist/distributions/logistic.js +94 -0
  26. package/dist/distributions/lognormal.d.ts +39 -0
  27. package/dist/distributions/lognormal.js +158 -0
  28. package/dist/distributions/nbeta.d.ts +7 -0
  29. package/dist/distributions/nbeta.js +312 -0
  30. package/dist/distributions/nchisq.d.ts +3 -0
  31. package/dist/distributions/nchisq.js +217 -0
  32. package/dist/distributions/negbinomial.d.ts +54 -0
  33. package/dist/distributions/negbinomial.js +325 -0
  34. package/dist/distributions/normal.d.ts +66 -0
  35. package/dist/distributions/normal.js +184 -0
  36. package/dist/distributions/nt.d.ts +3 -0
  37. package/dist/distributions/nt.js +491 -0
  38. package/dist/distributions/poisson.d.ts +41 -0
  39. package/dist/distributions/poisson.js +104 -0
  40. package/dist/distributions/signrank.d.ts +38 -0
  41. package/dist/distributions/signrank.js +216 -0
  42. package/dist/distributions/t.d.ts +49 -0
  43. package/dist/distributions/t.js +235 -0
  44. package/dist/distributions/tukey.d.ts +43 -0
  45. package/dist/distributions/tukey.js +985 -0
  46. package/dist/distributions/uniform.d.ts +36 -0
  47. package/dist/distributions/uniform.js +90 -0
  48. package/dist/distributions/weibull.d.ts +37 -0
  49. package/dist/distributions/weibull.js +125 -0
  50. package/dist/distributions/wilcox.d.ts +47 -0
  51. package/dist/distributions/wilcox.js +270 -0
  52. package/dist/htest/chisqTest.d.ts +55 -0
  53. package/dist/htest/chisqTest.js +160 -0
  54. package/dist/htest/cor.d.ts +81 -0
  55. package/dist/htest/cor.js +436 -0
  56. package/dist/htest/fisherTest.d.ts +48 -0
  57. package/dist/htest/fisherTest.js +468 -0
  58. package/dist/htest/pAdjust.d.ts +34 -0
  59. package/dist/htest/pAdjust.js +147 -0
  60. package/dist/htest/tTest.d.ts +57 -0
  61. package/dist/htest/tTest.js +153 -0
  62. package/dist/htest/types.d.ts +49 -0
  63. package/dist/htest/types.js +20 -0
  64. package/dist/htest/wilcoxTest.d.ts +61 -0
  65. package/dist/htest/wilcoxTest.js +471 -0
  66. package/dist/index.d.ts +50 -0
  67. package/dist/index.js +50 -0
  68. package/dist/internal/ddexp.d.ts +19 -0
  69. package/dist/internal/ddexp.js +94 -0
  70. package/dist/internal/discrete.d.ts +14 -0
  71. package/dist/internal/discrete.js +85 -0
  72. package/dist/internal/dpq.d.ts +43 -0
  73. package/dist/internal/dpq.js +54 -0
  74. package/dist/internal/integer.d.ts +4 -0
  75. package/dist/internal/integer.js +17 -0
  76. package/dist/internal/invert.d.ts +36 -0
  77. package/dist/internal/invert.js +124 -0
  78. package/dist/internal/noncentral.d.ts +12 -0
  79. package/dist/internal/noncentral.js +2 -0
  80. package/dist/internal/poissonMixture.d.ts +31 -0
  81. package/dist/internal/poissonMixture.js +401 -0
  82. package/dist/internal/summary.d.ts +21 -0
  83. package/dist/internal/summary.js +125 -0
  84. package/dist/internal/uniroot.d.ts +11 -0
  85. package/dist/internal/uniroot.js +75 -0
  86. package/dist/internal/vectorize.d.ts +12 -0
  87. package/dist/internal/vectorize.js +7 -0
  88. package/dist/linalg/basic.d.ts +95 -0
  89. package/dist/linalg/basic.js +152 -0
  90. package/dist/linalg/chol.d.ts +83 -0
  91. package/dist/linalg/chol.js +200 -0
  92. package/dist/linalg/eigen.d.ts +52 -0
  93. package/dist/linalg/eigen.js +753 -0
  94. package/dist/linalg/kappa.d.ts +51 -0
  95. package/dist/linalg/kappa.js +199 -0
  96. package/dist/linalg/matrix.d.ts +29 -0
  97. package/dist/linalg/matrix.js +65 -0
  98. package/dist/linalg/norm.d.ts +32 -0
  99. package/dist/linalg/norm.js +92 -0
  100. package/dist/linalg/qr.d.ts +264 -0
  101. package/dist/linalg/qr.js +494 -0
  102. package/dist/linalg/solve.d.ts +123 -0
  103. package/dist/linalg/solve.js +380 -0
  104. package/dist/linalg/svd.d.ts +38 -0
  105. package/dist/linalg/svd.js +275 -0
  106. package/dist/random/as241.d.ts +2 -0
  107. package/dist/random/as241.js +58 -0
  108. package/dist/random/beta.d.ts +14 -0
  109. package/dist/random/beta.js +117 -0
  110. package/dist/random/binomial.d.ts +25 -0
  111. package/dist/random/binomial.js +224 -0
  112. package/dist/random/common.d.ts +4 -0
  113. package/dist/random/common.js +35 -0
  114. package/dist/random/continuous.d.ts +91 -0
  115. package/dist/random/continuous.js +138 -0
  116. package/dist/random/gamma.d.ts +52 -0
  117. package/dist/random/gamma.js +217 -0
  118. package/dist/random/hyper.d.ts +12 -0
  119. package/dist/random/hyper.js +240 -0
  120. package/dist/random/mt.d.ts +16 -0
  121. package/dist/random/mt.js +97 -0
  122. package/dist/random/poisson.d.ts +33 -0
  123. package/dist/random/poisson.js +204 -0
  124. package/dist/random/rank.d.ts +19 -0
  125. package/dist/random/rank.js +62 -0
  126. package/dist/random/rng.d.ts +46 -0
  127. package/dist/random/rng.js +148 -0
  128. package/dist/random/sample.d.ts +45 -0
  129. package/dist/random/sample.js +255 -0
  130. package/dist/rstats.js +2 -0
  131. package/dist/special/beta.d.ts +30 -0
  132. package/dist/special/beta.js +152 -0
  133. package/dist/special/constants.d.ts +16 -0
  134. package/dist/special/constants.js +19 -0
  135. package/dist/special/dd.d.ts +29 -0
  136. package/dist/special/dd.js +693 -0
  137. package/dist/special/erf.d.ts +9 -0
  138. package/dist/special/erf.js +85 -0
  139. package/dist/special/gamma.d.ts +32 -0
  140. package/dist/special/gamma.js +270 -0
  141. package/dist/special/igam.d.ts +11 -0
  142. package/dist/special/igam.js +291 -0
  143. package/dist/special/igamAsympCoeff.d.ts +4 -0
  144. package/dist/special/igamAsympCoeff.js +233 -0
  145. package/dist/special/igami.d.ts +2 -0
  146. package/dist/special/igami.js +135 -0
  147. package/dist/special/incbet.d.ts +25 -0
  148. package/dist/special/incbet.js +326 -0
  149. package/dist/special/lgammaDiff.d.ts +2 -0
  150. package/dist/special/lgammaDiff.js +34 -0
  151. package/dist/special/loader.d.ts +43 -0
  152. package/dist/special/loader.js +456 -0
  153. package/dist/special/log1pmx.d.ts +2 -0
  154. package/dist/special/log1pmx.js +20 -0
  155. package/dist/special/ndtri.d.ts +4 -0
  156. package/dist/special/ndtri.js +82 -0
  157. package/dist/special/polevl.d.ts +4 -0
  158. package/dist/special/polevl.js +17 -0
  159. package/package.json +68 -4
package/CHANGELOG.md ADDED
@@ -0,0 +1,43 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
5
+
6
+ While the version is 0.x, a minor release (0.y.0) may contain breaking changes;
7
+ they are listed under **Breaking** for each release. From 1.0.0 the project follows
8
+ [Semantic Versioning](https://semver.org/).
9
+
10
+ ## 0.5.0 - 2026-10-21
11
+
12
+ First public version.
13
+
14
+ ### Included
15
+
16
+ - Special functions: `gamma`, `lgamma`, `beta`, `lbeta`.
17
+ - d/p/q functions with `lowerTail` and `logP` for the normal, t, chi-squared, F, beta, gamma,
18
+ binomial, Poisson, uniform, exponential, log-normal, logistic, Cauchy, Weibull, geometric,
19
+ negative binomial, hypergeometric, Wilcoxon signed rank and rank sum, and studentized range
20
+ distributions, and the non-central chi-squared, t, F and beta (`ncp`).
21
+ - Random numbers that reproduce R's `set.seed()` streams: `setSeed`, `getRandomSeed`,
22
+ `setRandomSeed`, 20 `r*` functions, `sample` and `sampleInt`.
23
+ - Hypothesis tests returning R's `htest` fields as plain objects: `tTest`, `chisqTest`,
24
+ `fisherTest` (including r × c tables), `wilcoxTest` (exact and normal approximation, with
25
+ confidence intervals), `cor`, `corTest` and `pAdjust`.
26
+ - Linear algebra on matrices given as arrays of rows: `crossprod`, `tcrossprod`, `norm`,
27
+ `backsolve`, `forwardsolve`, `chol`, `chol2inv`, `qr` (R's default limited pivoting and the
28
+ LAPACK variant) with `qrQ`, `qrR`, `qrX`, `qrCoef`, `qrQy`, `qrQty`, `qrResid`, `qrFitted`,
29
+ `qrSolve`, `solve`, `det`, `determinant`, `rcond`, `kappa`, `svd`, `eigen` (symmetric and
30
+ general, including complex eigenvalues) and `ginv`.
31
+
32
+ ### Conventions
33
+
34
+ - Indices are 0-based, matching JavaScript arrays: `sampleInt(n)` returns integers in `0..n-1`
35
+ (R's `sample.int()` minus 1, from the same random stream), and the `pivot` of `qr()` and of
36
+ `chol(x, { pivot: true })` is 0-based.
37
+ - Errors are thrown with English messages, and the class tells the kind of problem: `TypeError`
38
+ for wrong types or shapes, `RangeError` for values out of range, and `Error` for numerical
39
+ failures (singular matrix, not positive definite, no convergence, data essentially constant).
40
+ - A single-file minified ES module, `dist/rstats.js`, for loading from a CDN; release tags contain
41
+ the build output so that jsDelivr can serve it from GitHub.
42
+ - ESM only, no dependencies, tree-shakable. Requires Node.js 22 or later (or another ES2022
43
+ environment).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 numero-uno-jpn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.ja.md ADDED
@@ -0,0 +1,181 @@
1
+ # rStats.js
2
+
3
+ [English](README.md) | 日本語
4
+
5
+ > この文書は [英語版](README.md) の訳です。内容が食い違うときは英語版が正です。
6
+
7
+ JavaScript とブラウザで使える、R と互換の統計関数のライブラリです。TypeScript で書いています。
8
+
9
+ - **R と同じ数値を返します。** 関数名・既定値・結果は R に合わせています。すべての関数を R 4.5.2 と
10
+ 比べてテストしています (分布関数は相対誤差 1e-14)。R 自身が不正確なところでは、高精度の参照値と
11
+ 比べています。
12
+ - **JavaScript らしい呼び出し方です。** 必須の引数は位置で渡し、省略できる引数は camelCase のキーを
13
+ 持つオブジェクトで渡します。`pt(q, df, { lowerTail: false })` は R の `pt(q, df, lower.tail = FALSE)` に
14
+ 当たります。結果は素のオブジェクトなので、そのまま `JSON.stringify` できます。
15
+ - **どこでも動きます。** ESM のみで、依存パッケージはなく、Node 専用の API も使っていません。
16
+ tree-shaking が効きます (`pnorm` だけなら gzip で 1.2 KB)。
17
+
18
+ 状態: **0.5.0。** R の stats パッケージの数値計算の中心
19
+ (分布・検定・特殊関数・線形代数の基本) を、R と同じ結果でカバーすることを目指しています。残っている作業は
20
+ [ROADMAP.md](ROADMAP.md) (英語)、変更履歴は [CHANGELOG.md](CHANGELOG.md) (英語) にあります。
21
+
22
+ ## インストール
23
+
24
+ ```sh
25
+ npm install @numero-uno-jpn/rstats
26
+ ```
27
+
28
+ ES2022 の環境が必要です (現行のブラウザ、Node.js 22 以降、Deno、Bun)。
29
+
30
+ ## 使い方の例
31
+
32
+ ### 分布
33
+
34
+ ```ts
35
+ import { pnorm, qnorm, pt, qbeta, dbinom } from "@numero-uno-jpn/rstats";
36
+
37
+ pnorm(1.96); // 0.9750021048517795
38
+ qnorm([0.025, 0.975]); // [-1.9599639845400545, 1.959963984540054]
39
+ pt(-40, 3); // 1.7190340394579263e-5
40
+ pt(2, 10, { lowerTail: false, logP: true }); // -3.3051415512324533 (log of the upper tail)
41
+ qbeta(1e-10, 2, 3); // 4.082494015808333e-6
42
+ dbinom(3, 10, 0.2); // 0.20132659200000003
43
+ ```
44
+
45
+ 最初の引数には数値・配列・`Float64Array` を渡せます。結果は同じ形で返ります。分布の母数はスカラーです。
46
+
47
+ ### `set.seed()` と同じ乱数
48
+
49
+ ```ts
50
+ import { setSeed, rnorm, sample } from "@numero-uno-jpn/rstats";
51
+
52
+ setSeed(42);
53
+ rnorm(3); // [1.3709584471466685, -0.5646981713960887, 0.3631284113373392]
54
+ // R: set.seed(42); rnorm(3) gives the same numbers (up to the last bit)
55
+ ```
56
+
57
+ R で `set.seed(42); rnorm(3)` とすると、最後のビットまで同じ数値になります。
58
+
59
+ ### 検定
60
+
61
+ ```ts
62
+ import { tTest, fisherTest, pAdjust } from "@numero-uno-jpn/rstats";
63
+
64
+ const r = tTest([5.1, 4.9, 6.2, 5.8, 6.0, 5.5], [4.1, 4.5, 5.0, 4.8, 4.3]);
65
+ r.pValue; // 0.0035365308877675647 (Welch, the default in R)
66
+ r.confInt; // [0.4423253334608047, 1.6443413332058612]
67
+ r.method; // "Welch Two Sample t-test"
68
+
69
+ fisherTest([[3, 1], [1, 3]]).pValue; // 0.4857142857142857
70
+ pAdjust([0.01, 0.02, 0.03, 0.04], { method: "BH" }); // ≈ [0.04, 0.04, 0.04, 0.04]
71
+ ```
72
+
73
+ `tTest` の既定は、R と同じ Welch の検定です。
74
+
75
+ ### 線形代数
76
+
77
+ 行列は行の配列で表します。
78
+
79
+ ```ts
80
+ import { solve, qr, qrCoef, svd, eigen } from "@numero-uno-jpn/rstats";
81
+
82
+ solve([[2, 1], [1, 3]]); // [[0.6, -0.2], [-0.2, 0.4]]
83
+ const q = qr([[1, 1], [1, 2], [1, 3]]);
84
+ qrCoef(q, [1, 3, 4]); // ≈ [-0.333, 1.5] (least squares)
85
+ svd([[1, 4], [2, 5], [3, 6]]).d; // [9.50803200069572, 0.7728696356734838]
86
+ eigen([[2, 1], [1, 3]]).values; // [3.618033988749895, 1.381966011250105]
87
+ ```
88
+
89
+ ## 含まれるもの
90
+
91
+ | 分野 | 関数 (R と名前が違うものは括弧内が R の名前) |
92
+ |---|---|
93
+ | 特殊関数 | `gamma`, `lgamma`, `beta`, `lbeta` |
94
+ | 分布 (d/p/q) | 正規、t、カイ二乗、F、ベータ、ガンマ、二項、ポアソン、一様、指数、対数正規、ロジスティック、コーシー、ワイブル、幾何、負の二項、超幾何、Wilcoxon の符号付き順位と順位和、スチューデント化された範囲 (`ptukey`, `qtukey`)、非心カイ二乗・t・F・ベータ (`ncp`) |
95
+ | 乱数 | `setSeed` (`set.seed`), `getRandomSeed`/`setRandomSeed` (`.Random.seed`), 20 個の `r*` 関数, `sample`, `sampleInt` (`sample.int`) |
96
+ | 検定 | `tTest`, `chisqTest`, `fisherTest`, `wilcoxTest`, `cor`, `corTest`, `pAdjust` (`p.adjust`) |
97
+ | 線形代数 | `crossprod`, `tcrossprod`, `norm`, `backsolve`, `forwardsolve`, `chol`, `chol2inv`, `qr` と `qrQ`, `qrR`, `qrX`, `qrCoef`, `qrQy`, `qrQty`, `qrResid`, `qrFitted`, `qrSolve` (`qr.Q` …), `solve`, `det`, `determinant`, `rcond`, `kappa`, `svd`, `eigen`, `ginv` (MASS) |
98
+
99
+ API リファレンスは `npm run docs` (TypeDoc) で作れます (英語)。
100
+
101
+ ## 精度
102
+
103
+ 正解の値は [`oracle/`](oracle) の R のスクリプトで作り、[`test/fixtures/`](test/fixtures) に
104
+ 保存してあります。そのため、テストは R がなくても動きます。各ケースは、R と一致するか、R が
105
+ 間違っているところでは Rmpfr や gmp で計算した高精度の値と一致すれば合格です。分布の裾の深い
106
+ ところや母数の大きいところでは、rStats.js のほうが R より正確なことがよくあります。指数部を
107
+ double-double 演算で計算しているためです。詳しくは [docs/ja/accuracy.md](docs/ja/accuracy.md) を
108
+ 見てください。
109
+
110
+ ## R との違い
111
+
112
+ 違いの多くは、R の精度が落ちるか、R が失敗するケースです。ふだんの使い方に関わる意図的な違いは
113
+ 次のとおりです。
114
+
115
+ - 添字は 0 から始まります。`sampleInt(n)` は `0..n-1` を返します (R の `sample.int()` から 1 を
116
+ 引いた値で、乱数の列は同じ)。`qr` と `chol` のピボットも 0 から始まります。
117
+ - 固有ベクトルと特異ベクトルの符号が R と違うことがあります (どちらも正しい答えです)。
118
+ - 検定は R の警告を出しません。数値の欄と手法の名前はすべて一致します。
119
+ - 分布の母数は、ベクトルとして使い回されません (リサイクルしない)。
120
+
121
+ すべての違いは [docs/ja/differences-from-r.md](docs/ja/differences-from-r.md) にあります。
122
+
123
+ ## ブラウザで使う
124
+
125
+ バンドラ (Vite、esbuild、webpack、Rollup) を使う場合は、関数を名前で import してください。
126
+ 使わない関数は取り除かれます。minify と gzip をした後のおおよその大きさは次のとおりです。
127
+
128
+ | import するもの | gzip |
129
+ |---|---|
130
+ | `pnorm` | 1.2 KB |
131
+ | `setSeed`, `rnorm` | 1.7 KB |
132
+ | `solve` / `svd` / `eigen` | 1.7 / 2.0 / 3.7 KB |
133
+ | `wilcoxTest` / `fisherTest` / `tTest` | 7.9 / 13.2 / 16.3 KB |
134
+ | `qbeta` / `pt` と `qt` | 17.3 / 25.0 KB |
135
+ | すべて | 72.2 KB |
136
+
137
+ (`npm run size` で測れます。)
138
+
139
+ バンドラを使わない場合は、1 ファイルにまとめたビルド `dist/rstats.js` (すべての関数を含む minify 済みの
140
+ ES モジュール、gzip で 72 KB) を jsDelivr から読み込めます。jsDelivr は、このリポジトリのリリースの
141
+ タグをそのまま配信します。
142
+
143
+ ```html
144
+ <script type="module">
145
+ import { pnorm } from "https://cdn.jsdelivr.net/gh/numero-uno-jpn/rStats.js@v0.5.0/dist/rstats.js";
146
+ console.log(pnorm(1.96)); // 0.9750021048517795
147
+ </script>
148
+ ```
149
+
150
+ ビルド結果 (`dist/`) は main ブランチにはなく、リリースのタグにだけ入っています。npm を元にした CDN も
151
+ 使えます。たとえば
152
+ `https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@0.5.0/dist/rstats.js` や
153
+ `https://esm.sh/@numero-uno-jpn/rstats` です。
154
+
155
+ ブラウザ向けのビルドは、headless Chromium でテストしています。バンドラでまとめた場合と、
156
+ `dist/rstats.js` を ES モジュールとして読み込んだ場合の両方です (`npm run test:browser`)。
157
+
158
+ **デモ。** [`demo/`](demo) は、`dist/rstats.js` を jsDelivr から読み込んで、すべての関数を試せる
159
+ 静的なページです。確率分布 (グラフ付き)・特殊関数・乱数・検定・線形代数を扱います。各フォームには、
160
+ 入力に対応する R と JavaScript のコードが表示されます。「R との比較」のタブでは、例題の値を R が
161
+ 出力した値と照らし合わせます (`source("examples.R")` で、すべての例題を R で実行できます)。CDN の
162
+ 代わりに手元のビルドを使うには、`npm run build` をしてから、リポジトリのルートを静的サーバー
163
+ (たとえば `npx serve .`) で配信し、`/demo/?src=local` を開いてください。
164
+
165
+ ## 版の番号
166
+
167
+ 版が 0.x の間は、minor の版で互換性のない変更が入ることがあります。その場合は変更履歴に
168
+ 書きます。1.0.0 からは Semantic Versioning に従います。
169
+
170
+ ## 貢献
171
+
172
+ R との答え合わせの仕組みと、ライセンスの決まり (R や GPL のパッケージのコードは使わない) は
173
+ [CONTRIBUTING.md](CONTRIBUTING.md) (英語) にあります。速さと double-double 演算については
174
+ [docs/performance.md](docs/performance.md) (英語) にあります。
175
+
176
+ ## ライセンス
177
+
178
+ MIT です。数値計算の部品は、SciPy の xsf と scipy.stats (BSD-3)、Boost.Math (BSL-1.0)、
179
+ statsmodels (BSD-3)、LAPACK の参照実装 (修正 BSD) から移植しています。パブリックドメインの
180
+ LINPACK・EISPACK・Cephes のコードも使っています。詳しくは
181
+ [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) (英語) を見てください。
package/README.md CHANGED
@@ -1,3 +1,173 @@
1
- # Temporary Holding Version
1
+ # rStats.js
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ English | [日本語](README.ja.md)
4
+
5
+ R-compatible statistical functions for JavaScript and the browser, written in TypeScript.
6
+
7
+ - **Same numbers as R.** Function names, defaults and results follow R. Every function is
8
+ tested against R 4.5.2 (relative error 1e-14 for distributions), and where R itself is
9
+ inaccurate, against high-precision reference values.
10
+ - **JavaScript-style calls.** Required arguments are positional, optional ones go in an object
11
+ with camelCase keys: `pt(q, df, { lowerTail: false })` is R's `pt(q, df, lower.tail = FALSE)`.
12
+ Results are plain objects that `JSON.stringify` cleanly.
13
+ - **Runs anywhere.** ESM only, no dependencies, no Node-specific APIs, tree-shakable
14
+ (`pnorm` alone is 1.2 KB gzipped).
15
+
16
+ Status: **0.5.0.** The goal is to cover the numerical core of R's
17
+ `stats` package (distributions, hypothesis tests, special functions and the basics of linear
18
+ algebra) with R-compatible results; see [ROADMAP.md](ROADMAP.md) for what remains. See [CHANGELOG.md](CHANGELOG.md).
19
+
20
+ ## Install
21
+
22
+ ```sh
23
+ npm install @numero-uno-jpn/rstats
24
+ ```
25
+
26
+ Requires an ES2022 environment (current browsers, Node.js 22 or later, Deno, Bun).
27
+
28
+ ## Examples
29
+
30
+ ### Distributions
31
+
32
+ ```ts
33
+ import { pnorm, qnorm, pt, qbeta, dbinom } from "@numero-uno-jpn/rstats";
34
+
35
+ pnorm(1.96); // 0.9750021048517795
36
+ qnorm([0.025, 0.975]); // [-1.9599639845400545, 1.959963984540054]
37
+ pt(-40, 3); // 1.7190340394579263e-5
38
+ pt(2, 10, { lowerTail: false, logP: true }); // -3.3051415512324533 (log of the upper tail)
39
+ qbeta(1e-10, 2, 3); // 4.082494015808333e-6
40
+ dbinom(3, 10, 0.2); // 0.20132659200000003
41
+ ```
42
+
43
+ The first argument may be a number, an array or a `Float64Array`; the result has the same shape.
44
+ Distribution parameters are scalars.
45
+
46
+ ### Random numbers that match `set.seed()`
47
+
48
+ ```ts
49
+ import { setSeed, rnorm, sample } from "@numero-uno-jpn/rstats";
50
+
51
+ setSeed(42);
52
+ rnorm(3); // [1.3709584471466685, -0.5646981713960887, 0.3631284113373392]
53
+ // R: set.seed(42); rnorm(3) gives the same numbers (up to the last bit)
54
+ ```
55
+
56
+ ### Hypothesis tests
57
+
58
+ ```ts
59
+ import { tTest, fisherTest, pAdjust } from "@numero-uno-jpn/rstats";
60
+
61
+ const r = tTest([5.1, 4.9, 6.2, 5.8, 6.0, 5.5], [4.1, 4.5, 5.0, 4.8, 4.3]);
62
+ r.pValue; // 0.0035365308877675647 (Welch, the default in R)
63
+ r.confInt; // [0.4423253334608047, 1.6443413332058612]
64
+ r.method; // "Welch Two Sample t-test"
65
+
66
+ fisherTest([[3, 1], [1, 3]]).pValue; // 0.4857142857142857
67
+ pAdjust([0.01, 0.02, 0.03, 0.04], { method: "BH" }); // ≈ [0.04, 0.04, 0.04, 0.04]
68
+ ```
69
+
70
+ ### Linear algebra
71
+
72
+ Matrices are arrays of rows.
73
+
74
+ ```ts
75
+ import { solve, qr, qrCoef, svd, eigen } from "@numero-uno-jpn/rstats";
76
+
77
+ solve([[2, 1], [1, 3]]); // [[0.6, -0.2], [-0.2, 0.4]]
78
+ const q = qr([[1, 1], [1, 2], [1, 3]]);
79
+ qrCoef(q, [1, 3, 4]); // ≈ [-0.333, 1.5] (least squares)
80
+ svd([[1, 4], [2, 5], [3, 6]]).d; // [9.50803200069572, 0.7728696356734838]
81
+ eigen([[2, 1], [1, 3]]).values; // [3.618033988749895, 1.381966011250105]
82
+ ```
83
+
84
+ ## What is included
85
+
86
+ | Area | Functions (R names in parentheses where they differ) |
87
+ |---|---|
88
+ | Special functions | `gamma`, `lgamma`, `beta`, `lbeta` |
89
+ | Distributions (d/p/q) | normal, t, chi-squared, F, beta, gamma, binomial, Poisson, uniform, exponential, log-normal, logistic, Cauchy, Weibull, geometric, negative binomial, hypergeometric, Wilcoxon signed rank and rank sum, studentized range (`ptukey`, `qtukey`), non-central chi-squared, t, F and beta (`ncp`) |
90
+ | Random numbers | `setSeed` (`set.seed`), `getRandomSeed`/`setRandomSeed` (`.Random.seed`), 20 `r*` functions, `sample`, `sampleInt` (`sample.int`) |
91
+ | Hypothesis tests | `tTest`, `chisqTest`, `fisherTest`, `wilcoxTest`, `cor`, `corTest`, `pAdjust` (`p.adjust`) |
92
+ | Linear algebra | `crossprod`, `tcrossprod`, `norm`, `backsolve`, `forwardsolve`, `chol`, `chol2inv`, `qr` with `qrQ`, `qrR`, `qrX`, `qrCoef`, `qrQy`, `qrQty`, `qrResid`, `qrFitted`, `qrSolve` (`qr.Q` …), `solve`, `det`, `determinant`, `rcond`, `kappa`, `svd`, `eigen`, `ginv` (MASS) |
93
+
94
+ An API reference can be generated with `npm run docs` (TypeDoc).
95
+
96
+ ## Accuracy
97
+
98
+ Expected values are produced by R scripts in [`oracle/`](oracle) and stored in
99
+ [`test/fixtures/`](test/fixtures), so the test suite runs without R. A case passes if it matches
100
+ R, or if R is wrong there and rStats.js matches a high-precision value computed with Rmpfr or
101
+ gmp. In the far tails and for large parameters rStats.js is often more accurate than R, because
102
+ it computes exponents in double-double arithmetic. Details: [docs/accuracy.md](docs/accuracy.md).
103
+
104
+ ## Differences from R
105
+
106
+ Most differences are cases where R loses accuracy or fails. The deliberate ones that affect
107
+ everyday use:
108
+
109
+ - Indices are 0-based: `sampleInt(n)` returns `0..n-1` (R's `sample.int()` minus 1, from the
110
+ same random stream), and pivots of `qr` and `chol` are 0-based.
111
+ - Signs of eigenvectors and singular vectors may differ from R (both are correct).
112
+ - Hypothesis tests do not emit R's warnings; all numeric fields and method names match.
113
+ - Distribution parameters are not recycled as vectors.
114
+
115
+ Full list: [docs/differences-from-r.md](docs/differences-from-r.md).
116
+
117
+ ## Using it in the browser
118
+
119
+ With a bundler (Vite, esbuild, webpack, Rollup), import named functions; unused functions are
120
+ removed. Approximate sizes after minification and gzip:
121
+
122
+ | Import | gzip |
123
+ |---|---|
124
+ | `pnorm` | 1.2 KB |
125
+ | `setSeed`, `rnorm` | 1.7 KB |
126
+ | `solve` / `svd` / `eigen` | 1.7 / 2.0 / 3.7 KB |
127
+ | `wilcoxTest` / `fisherTest` / `tTest` | 7.9 / 13.2 / 16.3 KB |
128
+ | `qbeta` / `pt` and `qt` | 17.3 / 25.0 KB |
129
+ | everything | 72.2 KB |
130
+
131
+ (`npm run size` measures these.)
132
+
133
+ Without a bundler, load the single-file build `dist/rstats.js` (one minified ES module with every
134
+ function, 72 KB gzipped) from jsDelivr, which serves each release tag of this repository:
135
+
136
+ ```html
137
+ <script type="module">
138
+ import { pnorm } from "https://cdn.jsdelivr.net/gh/numero-uno-jpn/rStats.js@v0.5.0/dist/rstats.js";
139
+ console.log(pnorm(1.96)); // 0.9750021048517795
140
+ </script>
141
+ ```
142
+
143
+ Release tags contain the build output (`dist/`), which is not on the main branch. npm-based CDNs
144
+ work as well, for example
145
+ `https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@0.5.0/dist/rstats.js` or
146
+ `https://esm.sh/@numero-uno-jpn/rstats`.
147
+
148
+ The browser build is tested in headless Chromium, both bundled and as `dist/rstats.js` loaded as
149
+ an ES module (`npm run test:browser`).
150
+
151
+ **Demo.** [`demo/`](demo) is a static page that loads `dist/rstats.js` from jsDelivr and exercises
152
+ every function: distributions with plots, special functions, random numbers, hypothesis tests and
153
+ linear algebra. Each form shows the equivalent R and JavaScript code for its inputs, and a "Compare
154
+ with R" tab checks curated examples against values printed by R (`source("examples.R")` runs them
155
+ all in R). To use your own build instead of the CDN, run `npm run build`, serve the repository root
156
+ with a static server (for example `npx serve .`) and open `/demo/?src=local`.
157
+
158
+ ## Versioning
159
+
160
+ While the version is 0.x, a minor release may contain breaking changes; they are listed in the
161
+ changelog. From 1.0.0 the project follows Semantic Versioning.
162
+
163
+ ## Contributing
164
+
165
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for how the comparison with R works and the licensing
166
+ rules (no code from R or GPL packages). Notes on speed and the double-double arithmetic are in
167
+ [docs/performance.md](docs/performance.md).
168
+
169
+ ## License
170
+
171
+ MIT. Numerical kernels are ported from SciPy's xsf and scipy.stats (BSD-3), Boost.Math
172
+ (BSL-1.0), statsmodels (BSD-3) and the LAPACK reference implementation (modified BSD), plus
173
+ public-domain LINPACK, EISPACK and Cephes code. See [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md).