@numero-uno-jpn/rstats 1.15.0 → 1.16.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 (83) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.ja.md +7 -7
  3. package/README.md +7 -7
  4. package/dist/causal/mediate.d.ts +146 -0
  5. package/dist/causal/mediate.js +399 -0
  6. package/dist/causal/mediationProcedures.d.ts +231 -0
  7. package/dist/causal/mediationProcedures.js +240 -0
  8. package/dist/causal/moderation.d.ts +148 -0
  9. package/dist/causal/moderation.js +165 -0
  10. package/dist/causal.js +2 -2
  11. package/dist/chunks/{chunk-AGA7JCRF.js → chunk-244M5C2B.js} +2 -2
  12. package/dist/chunks/chunk-37YHUVXL.js +3 -0
  13. package/dist/chunks/chunk-3L333CTJ.js +2 -0
  14. package/dist/chunks/{chunk-YYLBBEWZ.js → chunk-3RO5FWJ5.js} +2 -2
  15. package/dist/chunks/chunk-3VKN6K3P.js +2 -0
  16. package/dist/chunks/{chunk-PZRABNMZ.js → chunk-57BGTHS7.js} +2 -2
  17. package/dist/chunks/{chunk-KQGH6SEW.js → chunk-6DP2U23T.js} +2 -2
  18. package/dist/chunks/{chunk-XVRRYHLM.js → chunk-6MHLWTBF.js} +2 -2
  19. package/dist/chunks/{chunk-VHMFOILD.js → chunk-7IU5OE5I.js} +2 -2
  20. package/dist/chunks/{chunk-UFJFVNC6.js → chunk-7MFMRNMT.js} +1 -1
  21. package/dist/chunks/{chunk-CIBAGTO2.js → chunk-AC22FPFB.js} +2 -2
  22. package/dist/chunks/{chunk-4EH3O466.js → chunk-AIII5VRA.js} +2 -2
  23. package/dist/chunks/{chunk-HF4TT233.js → chunk-B5QE2FDM.js} +1 -1
  24. package/dist/chunks/{chunk-IMSXMJ43.js → chunk-BHNDCNWM.js} +2 -2
  25. package/dist/chunks/{chunk-M3HFLMXL.js → chunk-DP6TE35N.js} +2 -2
  26. package/dist/chunks/{chunk-NULJXDAR.js → chunk-FCE6N6PI.js} +2 -2
  27. package/dist/chunks/{chunk-GBNGGBU3.js → chunk-FHHI3PYN.js} +2 -2
  28. package/dist/chunks/{chunk-L7HJGM52.js → chunk-GKDA5W53.js} +2 -2
  29. package/dist/chunks/{chunk-VO3E4Q6H.js → chunk-H6WBOIFZ.js} +1 -1
  30. package/dist/chunks/{chunk-JGZSGGMD.js → chunk-HOH5KGSU.js} +2 -2
  31. package/dist/chunks/{chunk-AILN2NGE.js → chunk-IPMC3VOC.js} +2 -2
  32. package/dist/chunks/chunk-ISOLPXQV.js +2 -0
  33. package/dist/chunks/{chunk-5BCXHJPF.js → chunk-IVJTWTGV.js} +2 -2
  34. package/dist/chunks/{chunk-RHNGF7R7.js → chunk-J74CELYL.js} +2 -2
  35. package/dist/chunks/chunk-JYE3T7FS.js +2 -0
  36. package/dist/chunks/chunk-K23MXTLC.js +2 -0
  37. package/dist/chunks/chunk-K5IP4ZJ5.js +2 -0
  38. package/dist/chunks/chunk-KSN6A4OK.js +2 -0
  39. package/dist/chunks/{chunk-FV4ZQPRI.js → chunk-LHMG6Q5C.js} +2 -2
  40. package/dist/chunks/{chunk-MKCPS5QQ.js → chunk-MNPZUMPL.js} +2 -2
  41. package/dist/chunks/chunk-NCVEJDRI.js +2 -0
  42. package/dist/chunks/{chunk-APOF7BLH.js → chunk-NQWFURFV.js} +2 -2
  43. package/dist/chunks/chunk-P3Q6IJMR.js +2 -0
  44. package/dist/chunks/{chunk-WL3JGBLZ.js → chunk-PIWXBWMD.js} +2 -2
  45. package/dist/chunks/{chunk-3K47QLCY.js → chunk-QBY63DGM.js} +2 -2
  46. package/dist/chunks/{chunk-JAXYZJV7.js → chunk-RV4SUKXV.js} +2 -2
  47. package/dist/chunks/chunk-SALMA4XX.js +2 -0
  48. package/dist/chunks/{chunk-OO22LZ7Q.js → chunk-UMXY4R4G.js} +2 -2
  49. package/dist/chunks/{chunk-R74QJUYU.js → chunk-W5D6CIZM.js} +2 -2
  50. package/dist/chunks/{chunk-6SZLUVSL.js → chunk-XAMWBGTP.js} +2 -2
  51. package/dist/chunks/{chunk-SNQABDAT.js → chunk-XMA2NWYX.js} +1 -1
  52. package/dist/chunks/{chunk-FD375XBW.js → chunk-ZTZX2UYP.js} +2 -2
  53. package/dist/chunks/chunk-ZZ6KYMBW.js +2 -0
  54. package/dist/descriptive.js +2 -2
  55. package/dist/distributions.js +2 -2
  56. package/dist/entries/causal.d.ts +3 -0
  57. package/dist/entries/causal.js +3 -0
  58. package/dist/entries/distributions.d.ts +1 -0
  59. package/dist/entries/distributions.js +1 -0
  60. package/dist/linalg.js +2 -2
  61. package/dist/mixed.js +2 -2
  62. package/dist/models/fitOptions.js +6 -4
  63. package/dist/multivariate.js +2 -2
  64. package/dist/numerics.js +2 -2
  65. package/dist/random/mvrnorm.d.ts +60 -0
  66. package/dist/random/mvrnorm.js +237 -0
  67. package/dist/regression.js +2 -2
  68. package/dist/rstats.js +16 -16
  69. package/dist/special.js +2 -2
  70. package/dist/tests.js +15 -15
  71. package/dist/timeseries.js +2 -2
  72. package/package.json +1 -1
  73. package/dist/chunks/chunk-5JKKIVYL.js +0 -3
  74. package/dist/chunks/chunk-5Q5YVM7S.js +0 -2
  75. package/dist/chunks/chunk-HSVVCFEN.js +0 -2
  76. package/dist/chunks/chunk-OBTCIG5D.js +0 -2
  77. package/dist/chunks/chunk-P4TKSL4W.js +0 -2
  78. package/dist/chunks/chunk-RV4JZHTE.js +0 -2
  79. package/dist/chunks/chunk-SMA62635.js +0 -2
  80. package/dist/chunks/chunk-U5UOVXKD.js +0 -2
  81. package/dist/chunks/chunk-VHBIWPB4.js +0 -2
  82. package/dist/chunks/chunk-ZDUVFNFH.js +0 -2
  83. package/dist/chunks/chunk-ZUTVNRML.js +0 -2
package/CHANGELOG.md CHANGED
@@ -7,6 +7,19 @@ While the version is 0.x, a minor release (0.y.0) may contain breaking changes;
7
7
  they are listed under **Breaking** for each release. From 1.0.0 the project follows
8
8
  [Semantic Versioning](https://semver.org/).
9
9
 
10
+ ## 1.16.0 - 2026-11-25
11
+
12
+ ### Added
13
+
14
+ - **Causal mediation analysis**: `mediate` (after `mediation::mediate`) for `lm` and binomial (logit, probit) `glm` mediator and outcome models: the average causal mediation effects, direct effects, total effect and proportions mediated with their averages, by the quasi-Bayesian approximation or the nonparametric bootstrap (percentile and BCa intervals), with `treatValue` / `controlValue`, `covariates` and HC3 covariances. The same draws, estimates, intervals and p-values as R under the same seed ([the differences](docs/differences-from-r.md#mediation-and-moderation-1160)).
15
+ - **Moderation**: `simSlopes` and `johnsonNeyman` (after `interactions::sim_slopes` and `johnson_neyman`): simple slopes at the moderator's mean ± SD or given values, with HC3, and the Johnson-Neyman interval.
16
+ - **ReactiveStat's procedures**: `sobelTest`, `mediationMonteCarlo` (the Monte Carlo method on the coefficients) and `moderatedMediation` (effects at a low and a high moderator value and the index of moderated mediation).
17
+ - **`mvrnorm`** (after `MASS::mvrnorm`) in the random numbers: R's values in 1 and 2 dimensions, R's distribution in more.
18
+
19
+ ### Changed
20
+
21
+ - The options a formula fit of `lm` or `glm` remembers (for `step`, `drop1`, `add1` and `mediate`) are kept on the result under a registered symbol, so a fit made with one CDN bundle (`dist/rstats.js`) is accepted by another (`dist/causal.js`).
22
+
10
23
  ## 1.15.0 - 2026-11-25
11
24
 
12
25
  ### Added
package/README.ja.md CHANGED
@@ -16,7 +16,7 @@ JavaScript とブラウザで使える、R と互換の統計関数のライブ
16
16
  - **どこでも動きます。** ESM のみで、依存パッケージはなく、Node 専用の API も使っていません。
17
17
  tree-shaking が効きます (`pnorm` だけなら gzip で 1.6 KB)。
18
18
 
19
- 状態: **1.15.0。** R の stats パッケージの数値計算の中心
19
+ 状態: **1.16.0。** R の stats パッケージの数値計算の中心
20
20
  (分布・記述統計・検定・線形モデル・特殊関数・数値計算・線形代数の基本) を、R と同じ結果でカバーすることを目指しています。残っている作業は
21
21
  [ROADMAP.ja.md](ROADMAP.ja.md)、変更履歴は [CHANGELOG.md](CHANGELOG.md) (英語) にあります。
22
22
 
@@ -217,9 +217,9 @@ eigen([[2, 1], [1, 3]]).values; // ≈ [3.618033989, 1.381966011]
217
217
  |---|---|
218
218
  | 特殊関数 | `gamma`, `lgamma`, `beta`, `lbeta`, `choose`, `lchoose`, `factorial`, `lfactorial`, `digamma`, `trigamma`, `psigamma`, `besselI`, `besselK`, `besselJ`, `besselY`。R にはない `erf`, `erfc`, `erfinv`, `erfcinv`, `gammainc`, `perm` と、R の C API (Rmath.h) の `log1pmx`, `lgamma1p`, `log1mexp`, `log1pexp` も |
219
219
  | 分布 (d/p/q) | 正規、t、カイ二乗、F、ベータ、ガンマ、二項、ポアソン、一様、指数、対数正規、ロジスティック、コーシー、ワイブル、幾何、負の二項、超幾何、Wilcoxon の符号付き順位と順位和、スチューデント化された範囲 (`ptukey`, `qtukey`)、非心カイ二乗・t・F・ベータ (`ncp`)。多項分布の確率 (`dmultinom`)。逆ガンマ、Kumaraswamy、Pareto、三角、Laplace (extraDistr) と逆正弦。`distSummary` (平均・分散・標準偏差・中央値・最頻値・歪度・尖度) |
220
- | 乱数 | `setSeed` (`set.seed`), `RNGkind` (Mersenne-Twister と L'Ecuyer-CMRG、`sample.kind` の Rejection と Rounding), `getRandomSeed`/`setRandomSeed` (`.Random.seed`), `nextRNGStream`/`nextRNGSubStream` (`parallel`), 26 個の `r*` 関数, `sample`, `sampleInt` (`sample.int`), `r2dtable` |
220
+ | 乱数 | `setSeed` (`set.seed`), `RNGkind` (Mersenne-Twister と L'Ecuyer-CMRG、`sample.kind` の Rejection と Rounding), `getRandomSeed`/`setRandomSeed` (`.Random.seed`), `nextRNGStream`/`nextRNGSubStream` (`parallel`), 26 個の `r*` 関数, `sample`, `sampleInt` (`sample.int`), `r2dtable`, `mvrnorm` (`MASS::mvrnorm`) |
221
221
  | 時系列 | `acf`、`pacf`、`ccf` (`stats::acf`、`stats::pacf`、`stats::ccf`)、`@numero-uno-jpn/rstats/timeseries` |
222
- | 因果推論 | `match` (傾向スコアのマッチング: `ties = FALSE`・`replace = FALSE` の `Matching::Match`。ATT・ATE・ATC、1:M、キャリパー)、`matchWeights` (`cobalt::get.w`)、`weightit` (逆確率重み: glm・cbps・ipt・ebal の `WeightIt::weightit`。ATE・ATT・ATC・ATO)、`trimWeights`・`weightSummary`、`balTab` (マッチング・重み付けの前後の標準化平均差と分散比: `cobalt::bal.tab`)、`tableOneSmd` (`tableone::ExtractSmd`)、`@numero-uno-jpn/rstats/causal` |
222
+ | 因果推論 | `match` (傾向スコアのマッチング: `ties = FALSE`・`replace = FALSE` の `Matching::Match`。ATT・ATE・ATC、1:M、キャリパー)、`matchWeights` (`cobalt::get.w`)、`weightit` (逆確率重み: glm・cbps・ipt・ebal の `WeightIt::weightit`。ATE・ATT・ATC・ATO)、`trimWeights`・`weightSummary`、`balTab` (マッチング・重み付けの前後の標準化平均差と分散比: `cobalt::bal.tab`)、`tableOneSmd` (`tableone::ExtractSmd`)、`mediate` (因果媒介分析: `mediation::mediate`、準ベイズ近似とブートストラップ)、`simSlopes`・`johnsonNeyman` (`interactions::sim_slopes`)、`sobelTest`・`mediationMonteCarlo`・`moderatedMediation` (index of moderated mediation)、`@numero-uno-jpn/rstats/causal` |
223
223
  | 記述統計 | `sum`, `prod`, `mean`, `weightedMean` (`weighted.mean`), `variance` (`var`), `sd`, `median`, `range`, `cumsum`, `cumprod`, `cummax`, `cummin`, `diff`, `quantile`, `IQR`, `fivenum`, `mad`, `rank`, `cov`, `cor`, `tabulate`, `table`, `unique`, `cut`, `findInterval`, `pretty`, `hist`, `ecdf`, `scale`, `skewness`, `kurtosis`, `geometricMean`, `modes`, `meanAD`, `cv` |
224
224
  | 検定 | `tTest`, `chisqTest`, `fisherTest`, `wilcoxTest`, `corTest`, `binomTest`, `poissonTest`, `propTest`, `mcnemarTest`, `varTest`, `bartlettTest`, `kruskalTest`, `friedmanTest`, `quadeTest`, `flignerTest`, `moodTest`, `onewayTest`, `shapiroTest`, `mantelhaenTest`, `ansariTest`, `ksTest`, `pairwiseTTest`, `pairwiseWilcoxTest`, `pairwisePropTest`, `aov` (一元配置。表は当てはめの `aovSummary`), `TukeyHSD`, `zTest`, `brunnerMunzelTest`, `tamhaneT2Test` (Tamhane の T2 と T2')、`pageTest`、`grubbsTest`・`pgrubbs`・`qgrubbs`、`pAdjust` (`p.adjust`) |
225
225
  | 分散分析 | formula で呼ぶ `aov` と `aovSummary` (多元、交互作用、共変量、`Error()` の層), `anova` (`anova.lm`。逐次の表とモデルの比較), `Anova` (car の Type II と III), `aovEz` (afex。反復測定と混合計画、Greenhouse-Geisser と Huynh-Feldt の補正、一般化 η² と偏 η²), `mauchlyTest` (`mauchly.test`) |
@@ -301,8 +301,8 @@ double-double 演算で計算しているためです。詳しくは [docs/ja/ac
301
301
  | `glm` と `glmSummary` / `glm` とそのすべての関数 | 40.6 / 56.9 KB |
302
302
  | `lmer` (`/mixed` から) / `lmerSummary`・`lmerContest`・`lmerAnova` も | 19.4 / 70.0 KB |
303
303
  | `glmer` (`/mixed` から) / `glmerSummary`・`glmerAnova`・`glmerPredict` も | 47.2 / 66.1 KB |
304
- | `polr` / `rlm` / `nls` (`/regression` から) | 24.6 / 14.6 / 7.9 KB |
305
- | すべて (`/mixed` と `/regression` を除く) | 332.9 KB |
304
+ | `polr` / `rlm` / `nls` (`/regression` から) | 25.0 / 14.6 / 8.0 KB |
305
+ | すべて (`/mixed` と `/regression` を除く) | 333.9 KB |
306
306
 
307
307
  (`npm run size` で測れます。)
308
308
 
@@ -312,12 +312,12 @@ double-double 演算で計算しているためです。詳しくは [docs/ja/ac
312
312
  `/regression` の `polr`・`rlm`・`nls` とその関数は、この方法でしか import できません。メインの入口には含めていません)。
313
313
 
314
314
  バンドラを使わない場合は、1 ファイルにまとめたビルド `dist/rstats.js` (`/mixed` と `/regression` 以外のすべての関数を含む
315
- minify 済みの ES モジュール、gzip で 333 KB) を jsDelivr から読み込めます。jsDelivr は、npm のリリースを
315
+ minify 済みの ES モジュール、gzip で 334 KB) を jsDelivr から読み込めます。jsDelivr は、npm のリリースを
316
316
  そのまま配信します。
317
317
 
318
318
  ```html
319
319
  <script type="module">
320
- import { pnorm } from "https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.15.0/dist/rstats.js";
320
+ import { pnorm } from "https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.16.0/dist/rstats.js";
321
321
  console.log(pnorm(1.96)); // ≈ 0.9750021049
322
322
  </script>
323
323
  ```
package/README.md CHANGED
@@ -14,7 +14,7 @@ R-compatible statistical functions for JavaScript and the browser, written in Ty
14
14
  - **Runs anywhere.** ESM only, no dependencies, no Node-specific APIs, tree-shakable
15
15
  (`pnorm` alone is 1.6 KB gzipped).
16
16
 
17
- Status: **1.15.0.** The goal is to cover the numerical core of R's
17
+ Status: **1.16.0.** The goal is to cover the numerical core of R's
18
18
  `stats` package (distributions, descriptive statistics, hypothesis tests, linear models, special
19
19
  functions, numerical methods and the basics of linear algebra) with R-compatible results; see [ROADMAP.md](ROADMAP.md) for what remains. See [CHANGELOG.md](CHANGELOG.md).
20
20
 
@@ -215,9 +215,9 @@ eigen([[2, 1], [1, 3]]).values; // ≈ [3.618033989, 1.381966011]
215
215
  |---|---|
216
216
  | Special functions | `gamma`, `lgamma`, `beta`, `lbeta`, `choose`, `lchoose`, `factorial`, `lfactorial`, `digamma`, `trigamma`, `psigamma`, `besselI`, `besselK`, `besselJ`, `besselY`; also `erf`, `erfc`, `erfinv`, `erfcinv`, `gammainc` and `perm`, which R lacks, and `log1pmx`, `lgamma1p`, `log1mexp`, `log1pexp` from R's C API (Rmath.h) |
217
217
  | 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`); the multinomial probability (`dmultinom`); inverse gamma, Kumaraswamy, Pareto, triangular, Laplace (extraDistr) and arcsine; `distSummary` (mean, variance, sd, median, mode, skewness, kurtosis) |
218
- | Random numbers | `setSeed` (`set.seed`), `RNGkind` (Mersenne-Twister and L'Ecuyer-CMRG, `sample.kind` Rejection and Rounding), `getRandomSeed`/`setRandomSeed` (`.Random.seed`), `nextRNGStream`/`nextRNGSubStream` (`parallel`), 26 `r*` functions, `sample`, `sampleInt` (`sample.int`), `r2dtable` |
218
+ | Random numbers | `setSeed` (`set.seed`), `RNGkind` (Mersenne-Twister and L'Ecuyer-CMRG, `sample.kind` Rejection and Rounding), `getRandomSeed`/`setRandomSeed` (`.Random.seed`), `nextRNGStream`/`nextRNGSubStream` (`parallel`), 26 `r*` functions, `sample`, `sampleInt` (`sample.int`), `r2dtable`, `mvrnorm` (`MASS::mvrnorm`) |
219
219
  | Time series | `acf`, `pacf`, `ccf` (`stats::acf`, `stats::pacf`, `stats::ccf`) from `@numero-uno-jpn/rstats/timeseries` |
220
- | Causal inference | `match` (propensity score matching: `Matching::Match` with `ties = FALSE`, `replace = FALSE`; ATT, ATE and ATC, 1:M, caliper), `matchWeights` (`cobalt::get.w`), `weightit` (inverse probability weights: `WeightIt::weightit` with glm, cbps, ipt and ebal; ATE, ATT, ATC and ATO), `trimWeights` and `weightSummary`, `balTab` (standardized mean differences and variance ratios before and after: `cobalt::bal.tab`) and `tableOneSmd` (`tableone::ExtractSmd`), from `@numero-uno-jpn/rstats/causal` |
220
+ | Causal inference | `match` (propensity score matching: `Matching::Match` with `ties = FALSE`, `replace = FALSE`; ATT, ATE and ATC, 1:M, caliper), `matchWeights` (`cobalt::get.w`), `weightit` (inverse probability weights: `WeightIt::weightit` with glm, cbps, ipt and ebal; ATE, ATT, ATC and ATO), `trimWeights` and `weightSummary`, `balTab` (standardized mean differences and variance ratios before and after: `cobalt::bal.tab`) and `tableOneSmd` (`tableone::ExtractSmd`); `mediate` (causal mediation analysis: `mediation::mediate`, quasi-Bayesian and bootstrap), `simSlopes` and `johnsonNeyman` (`interactions::sim_slopes`), `sobelTest`, `mediationMonteCarlo` and `moderatedMediation` (the index of moderated mediation), from `@numero-uno-jpn/rstats/causal` |
221
221
  | Descriptive statistics | `sum`, `prod`, `mean`, `weightedMean` (`weighted.mean`), `variance` (`var`), `sd`, `median`, `range`, `cumsum`, `cumprod`, `cummax`, `cummin`, `diff`, `quantile`, `IQR`, `fivenum`, `mad`, `rank`, `cov`, `cor`, `tabulate`, `table`, `unique`, `cut`, `findInterval`, `pretty`, `hist`, `ecdf`, `scale`, `skewness`, `kurtosis`, `geometricMean`, `modes`, `meanAD`, `cv` |
222
222
  | Hypothesis tests | `tTest`, `chisqTest`, `fisherTest`, `wilcoxTest`, `corTest`, `binomTest`, `poissonTest`, `propTest`, `mcnemarTest`, `varTest`, `bartlettTest`, `kruskalTest`, `friedmanTest`, `quadeTest`, `flignerTest`, `moodTest`, `onewayTest`, `shapiroTest`, `mantelhaenTest`, `ansariTest`, `ksTest`, `pairwiseTTest`, `pairwiseWilcoxTest`, `pairwisePropTest`, `aov` (one way, a fit whose table is `aovSummary`), `TukeyHSD`, `zTest`, `brunnerMunzelTest`, `tamhaneT2Test` (Tamhane's T2/T2'), `pageTest`, `grubbsTest`, `pgrubbs`, `qgrubbs`, `pAdjust` (`p.adjust`) |
223
223
  | Analysis of variance | `aov` and `aovSummary` with a formula (several factors, interactions, covariates, `Error()` strata), `anova` (`anova.lm`, sequential and model comparison), `Anova` (car: types II and III), `aovEz` (afex: repeated-measures and mixed designs, Greenhouse-Geisser and Huynh-Feldt corrections, generalized and partial eta squared), `mauchlyTest` (`mauchly.test`) |
@@ -300,8 +300,8 @@ removed. Approximate sizes after minification and gzip:
300
300
  | `glm` and `glmSummary` / `glm` with all its methods | 40.6 / 56.9 KB |
301
301
  | `lmer` (from `/mixed`) / with `lmerSummary`, `lmerContest`, `lmerAnova` | 19.4 / 70.0 KB |
302
302
  | `glmer` (from `/mixed`) / with `glmerSummary`, `glmerAnova`, `glmerPredict` | 47.2 / 66.1 KB |
303
- | `polr` / `rlm` / `nls` (from `/regression`) | 24.6 / 14.6 / 7.9 KB |
304
- | everything (except `/mixed` and `/regression`) | 332.9 KB |
303
+ | `polr` / `rlm` / `nls` (from `/regression`) | 25.0 / 14.6 / 8.0 KB |
304
+ | everything (except `/mixed` and `/regression`) | 333.9 KB |
305
305
 
306
306
  (`npm run size` measures these.)
307
307
 
@@ -312,11 +312,11 @@ Each area is also a subpath export, for importing one area without the rest:
312
312
  areas are only available this way, not from the main entry point.
313
313
 
314
314
  Without a bundler, load the single-file build `dist/rstats.js` (one minified ES module with every
315
- function except those of `/mixed` and `/regression`, 333 KB gzipped) from jsDelivr, which serves each npm release:
315
+ function except those of `/mixed` and `/regression`, 334 KB gzipped) from jsDelivr, which serves each npm release:
316
316
 
317
317
  ```html
318
318
  <script type="module">
319
- import { pnorm } from "https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.15.0/dist/rstats.js";
319
+ import { pnorm } from "https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.16.0/dist/rstats.js";
320
320
  console.log(pnorm(1.96)); // ≈ 0.9750021049
321
321
  </script>
322
322
  ```
@@ -0,0 +1,146 @@
1
+ import { type LmFormulaResult } from "../models/lm.js";
2
+ import { type GlmFormulaResult } from "../models/glm.js";
3
+ import type { DataFrame } from "../models/modelFrame.js";
4
+ /**
5
+ * A model for {@link mediate}: a formula fit of {@link lm}, or of {@link glm} with the binomial family and the logit or
6
+ * probit link.
7
+ *
8
+ * @category Causal inference
9
+ */
10
+ export type MediationModel = LmFormulaResult | GlmFormulaResult;
11
+ /**
12
+ * Options for {@link mediate}.
13
+ *
14
+ * @category Causal inference
15
+ */
16
+ export interface MediateOptions {
17
+ /** The treatment: a numeric variable of both models. */
18
+ treat: string;
19
+ /** The mediator: the mediator model's response and a numeric variable of the outcome model. */
20
+ mediator: string;
21
+ /** The treatment's value for the treated condition (default 1). */
22
+ treatValue?: number;
23
+ /** The treatment's value for the control condition (default 0). */
24
+ controlValue?: number;
25
+ /** Covariates held at the given values in every observation's prediction (R's `covariates`); numeric variables. */
26
+ covariates?: Record<string, number>;
27
+ /** Number of simulations or bootstrap replicates (default 1000). */
28
+ sims?: number;
29
+ /** Nonparametric bootstrap instead of the quasi-Bayesian approximation (default `false`). */
30
+ boot?: boolean;
31
+ /** The bootstrap intervals: `"perc"` (percentile, the default) or `"bca"` (bias-corrected and accelerated). */
32
+ bootCiType?: "perc" | "bca";
33
+ /** Confidence level of the intervals (default 0.95). */
34
+ confLevel?: number;
35
+ /** Heteroskedasticity-consistent (HC3) covariances for the quasi-Bayesian draws (default `false`). */
36
+ robustSE?: boolean;
37
+ }
38
+ /**
39
+ * One effect of {@link mediate}: its estimate, interval, p-value and draws.
40
+ *
41
+ * @category Causal inference
42
+ */
43
+ export interface MediateEffect {
44
+ /** The estimate (R's `d0`, `z0`, `tau.coef`, ...). */
45
+ estimate: number;
46
+ /** The confidence interval (R's `d0.ci`, ...). */
47
+ ci: [number, number];
48
+ /** The two-sided p-value, `2 min(mean(draws > 0), mean(draws < 0))` (R's `d0.p`, ...). */
49
+ p: number;
50
+ /** The simulation or bootstrap draws (R's `d0.sims`, ...). */
51
+ sims: number[];
52
+ }
53
+ /**
54
+ * Result of {@link mediate}. `d` are the average causal mediation effects (ACME), `z` the average direct effects (ADE),
55
+ * `n` the proportions mediated, `0` under control and `1` under treatment.
56
+ *
57
+ * @category Causal inference
58
+ */
59
+ export interface MediateResult {
60
+ /** ACME under control: the mean of Y(t0, M(t1)) − Y(t0, M(t0)). */
61
+ d0: MediateEffect;
62
+ /** ACME under treatment: the mean of Y(t1, M(t1)) − Y(t1, M(t0)). */
63
+ d1: MediateEffect;
64
+ /** ADE under control: the mean of Y(t1, M(t0)) − Y(t0, M(t0)). */
65
+ z0: MediateEffect;
66
+ /** ADE under treatment: the mean of Y(t1, M(t1)) − Y(t0, M(t1)). */
67
+ z1: MediateEffect;
68
+ /** The total effect: the mean of Y(t1, M(t1)) − Y(t0, M(t0)). */
69
+ tau: MediateEffect;
70
+ /** Proportion mediated under control, d0 / tau. */
71
+ n0: MediateEffect;
72
+ /** Proportion mediated under treatment, d1 / tau. */
73
+ n1: MediateEffect;
74
+ /** The average ACME, (d0 + d1) / 2. */
75
+ dAvg: MediateEffect;
76
+ /** The average ADE, (z0 + z1) / 2. */
77
+ zAvg: MediateEffect;
78
+ /** The average proportion mediated, (n0 + n1) / 2. */
79
+ nAvg: MediateEffect;
80
+ /** The treated value used. */
81
+ treatValue: number;
82
+ /** The control value used. */
83
+ controlValue: number;
84
+ /** Number of observations. */
85
+ nobs: number;
86
+ /** Number of simulations or replicates. */
87
+ sims: number;
88
+ /** Whether the bootstrap was used. */
89
+ boot: boolean;
90
+ /** The bootstrap interval type. */
91
+ bootCiType: "perc" | "bca";
92
+ /** The confidence level. */
93
+ confLevel: number;
94
+ /** Whether HC3 covariances were used (false with the bootstrap, which ignores them). */
95
+ robustSE: boolean;
96
+ /** Whether the outcome model has an interaction of the treatment and the mediator (R's `INT`). */
97
+ INT: boolean;
98
+ /** R's warnings. */
99
+ warnings: string[];
100
+ }
101
+ /**
102
+ * Causal mediation analysis for linear and binomial (logit or probit) mediator and outcome models. Equivalent to
103
+ * `mediation::mediate()` in R.
104
+ *
105
+ * The average causal mediation effects (ACME, `d0` and `d1`), the average direct effects (ADE, `z0` and `z1`), the total
106
+ * effect (`tau`) and the proportions mediated (`n0`, `n1`) with their averages, by the quasi-Bayesian Monte Carlo
107
+ * approximation (the default) or the nonparametric bootstrap (`boot: true`), as in Imai, Keele and Tingley (2010).
108
+ * With the same {@link setSeed} the draws, estimates, intervals and p-values equal R's: the parameters are drawn with
109
+ * the symmetric square root of their covariance, then the mediator for every simulation and observation (normal errors
110
+ * with the residual standard error for a linear mediator, binomial draws for a binary one).
111
+ *
112
+ * Quasi-Bayesian estimates are the means of the draws (the proportions mediated: their medians; `nAvg` the average of
113
+ * the two medians); bootstrap estimates come from a final draw on the original data, after the replicates, as in R.
114
+ * Intervals are quantiles of type 7 of the draws (with `bootCiType: "bca"`: the bias-corrected and accelerated levels,
115
+ * the bias measured against the draws' mean and the acceleration from their skewness, as R).
116
+ *
117
+ * The models must be formula fits of {@link lm} or {@link glm} (binomial with logit or probit, or gaussian) on `data`,
118
+ * on the same rows, without weights or offsets; the treatment and the mediator are numeric variables.
119
+ *
120
+ * @param modelM The mediator model (its response is the mediator).
121
+ * @param modelY The outcome model (it includes the treatment and the mediator).
122
+ * @param data The data both models were fitted on.
123
+ * @param options See {@link MediateOptions}; `treat` and `mediator` are required.
124
+ * @returns See {@link MediateResult}.
125
+ * @throws TypeError or RangeError for unsupported models, data that do not reproduce the fits, a treatment or mediator
126
+ * that is not a numeric variable of the models, models on different rows, `sims` below 2, or
127
+ * `confLevel` outside (0, 1).
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const data = {
132
+ * X: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], C: [1.2, -0.4, 0.3, 0.8, -1.1, 0.5, -0.2, 1.4, -0.7, 0.1],
133
+ * M: [1.1, 0.4, 0.9, 1.6, 0.2, 1.9, 1.3, 2.6, 1.0, 1.8], Y: [2.0, 1.1, 1.5, 2.4, 0.8, 3.1, 2.2, 3.9, 1.9, 2.8],
134
+ * W: [3.1, 2.2, 4.0, 2.8, 3.5, 2.9, 3.8, 2.4, 3.3, 4.2],
135
+ * };
136
+ * const m = lm("M ~ X + C", data), y = lm("Y ~ X + M + C", data);
137
+ * setSeed(1234);
138
+ * const r = mediate(m, y, data, { treat: "X", mediator: "M", sims: 500 });
139
+ * r.d0.estimate; // 0.8851 (R: set.seed(1234); mediate(m, y, treat = "X", mediator = "M", sims = 500)$d0)
140
+ * r.d0.ci; // [0.5261, 1.3011]
141
+ * r.n0.estimate; // 0.7432
142
+ * ```
143
+ *
144
+ * @category Causal inference
145
+ */
146
+ export declare function mediate(modelM: MediationModel, modelY: MediationModel, data: DataFrame, options: MediateOptions): MediateResult;