@numero-uno-jpn/rstats 1.17.0 → 1.18.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.
- package/CHANGELOG.md +25 -0
- package/README.ja.md +6 -6
- package/README.md +6 -6
- package/dist/causal/mediationProcedures.js +18 -1
- package/dist/causal/moderation.js +11 -2
- package/dist/causal.js +2 -2
- package/dist/chunks/{chunk-NPWWGIFG.js → chunk-2JB7F646.js} +2 -2
- package/dist/chunks/{chunk-JG4TWDQ4.js → chunk-3JFRDXN7.js} +2 -2
- package/dist/chunks/chunk-53WQDB3C.js +2 -0
- package/dist/chunks/{chunk-QCPGV7XH.js → chunk-5CTWYFMB.js} +2 -2
- package/dist/chunks/chunk-6I23C2YJ.js +2 -0
- package/dist/chunks/{chunk-6MLDKHTY.js → chunk-CTZUYKVL.js} +2 -2
- package/dist/chunks/{chunk-GOYTZ5RB.js → chunk-EC77KAAM.js} +2 -2
- package/dist/chunks/{chunk-VZSQAGL2.js → chunk-FLFNMSHI.js} +2 -2
- package/dist/chunks/{chunk-XEJX5HBJ.js → chunk-GEHMND3C.js} +2 -2
- package/dist/chunks/{chunk-KY53MB3J.js → chunk-GIK34P23.js} +2 -2
- package/dist/chunks/chunk-H6Z7JVSD.js +2 -0
- package/dist/chunks/{chunk-YMI33F7W.js → chunk-HG4LJBNV.js} +2 -2
- package/dist/chunks/chunk-HGAEVPVS.js +2 -0
- package/dist/chunks/{chunk-EIUK6TKF.js → chunk-HMLYEFQN.js} +2 -2
- package/dist/chunks/chunk-HRRCC2BD.js +2 -0
- package/dist/chunks/chunk-IBHMM2SL.js +2 -0
- package/dist/chunks/chunk-ICDW3I2F.js +2 -0
- package/dist/chunks/chunk-IKMW63BE.js +2 -0
- package/dist/chunks/{chunk-O4D43LWM.js → chunk-IOJVFYHW.js} +2 -2
- package/dist/chunks/chunk-IW4S634O.js +2 -0
- package/dist/chunks/{chunk-GIIURG5X.js → chunk-JF4PXFJE.js} +2 -2
- package/dist/chunks/chunk-JPWYGTUN.js +2 -0
- package/dist/chunks/{chunk-I2KJMF4G.js → chunk-KJMLQHCY.js} +1 -1
- package/dist/chunks/chunk-KVJBCT3D.js +2 -0
- package/dist/chunks/chunk-LCIX76FR.js +2 -0
- package/dist/chunks/chunk-LY5XGFPU.js +2 -0
- package/dist/chunks/{chunk-NCDPZSI2.js → chunk-MKZH65DK.js} +2 -2
- package/dist/chunks/{chunk-GTSK4IN4.js → chunk-MVPHISOV.js} +1 -1
- package/dist/chunks/chunk-MXZ64QME.js +2 -0
- package/dist/chunks/{chunk-NCRL33PQ.js → chunk-N7BUE54L.js} +2 -2
- package/dist/chunks/{chunk-L77QFL3C.js → chunk-NDZVVNBB.js} +2 -2
- package/dist/chunks/{chunk-EB45CDFA.js → chunk-NOTJ45G3.js} +2 -2
- package/dist/chunks/{chunk-ZRKXHVTJ.js → chunk-QUD3374N.js} +2 -2
- package/dist/chunks/{chunk-XR7Y4ZV6.js → chunk-RXYZQ3UX.js} +2 -2
- package/dist/chunks/{chunk-V54MBEJM.js → chunk-SCFFHEDZ.js} +2 -2
- package/dist/chunks/{chunk-RNGDVSTS.js → chunk-V6FMGGUV.js} +2 -2
- package/dist/chunks/{chunk-TTOJDLIQ.js → chunk-VATAUY7N.js} +2 -2
- package/dist/chunks/chunk-W5M3R4NH.js +2 -0
- package/dist/chunks/{chunk-DD2GG4EC.js → chunk-WYVUCQO3.js} +1 -1
- package/dist/chunks/{chunk-JHABMYNH.js → chunk-XQCHC3SH.js} +1 -1
- package/dist/chunks/chunk-XUMVTGMM.js +2 -0
- package/dist/chunks/{chunk-72I7GDWL.js → chunk-YXMHWS5R.js} +2 -2
- package/dist/chunks/{chunk-LKKUUREA.js → chunk-Z33Q545U.js} +2 -2
- package/dist/descriptive/qcc.d.ts +320 -0
- package/dist/descriptive/qcc.js +524 -0
- package/dist/descriptive/qccTools.d.ts +301 -0
- package/dist/descriptive/qccTools.js +390 -0
- package/dist/descriptive.js +2 -2
- package/dist/distributions.js +2 -2
- package/dist/entries/descriptive.d.ts +2 -0
- package/dist/entries/descriptive.js +2 -0
- package/dist/entries/regression.d.ts +1 -0
- package/dist/entries/regression.js +2 -0
- package/dist/linalg.js +2 -2
- package/dist/mixed.js +2 -2
- package/dist/models/betareg.js +14 -2
- package/dist/models/bradleyterry.d.ts +188 -0
- package/dist/models/bradleyterry.js +533 -0
- package/dist/models/survreg.d.ts +5 -0
- package/dist/models/survreg.js +23 -3
- package/dist/models/tobit.js +16 -5
- package/dist/multivariate.js +2 -2
- package/dist/numerics.js +2 -2
- package/dist/regression.js +3 -3
- package/dist/rstats.js +17 -17
- package/dist/special.js +2 -2
- package/dist/tests.js +2 -2
- package/dist/timeseries.js +2 -2
- package/package.json +1 -1
- package/dist/chunks/chunk-23QCIPET.js +0 -2
- package/dist/chunks/chunk-3UEYA6VG.js +0 -2
- package/dist/chunks/chunk-3Y4DNK4O.js +0 -2
- package/dist/chunks/chunk-4BGGWQWS.js +0 -2
- package/dist/chunks/chunk-5UFVACSV.js +0 -2
- package/dist/chunks/chunk-5W2OZAHQ.js +0 -2
- package/dist/chunks/chunk-BWDXJL7T.js +0 -2
- package/dist/chunks/chunk-GLABB425.js +0 -2
- package/dist/chunks/chunk-JV3POOET.js +0 -2
- package/dist/chunks/chunk-K3YSXDTU.js +0 -2
- package/dist/chunks/chunk-LY2UZGUL.js +0 -2
- package/dist/chunks/chunk-PSQ6MLJM.js +0 -2
- package/dist/chunks/chunk-RM5D7Q7W.js +0 -2
- package/dist/chunks/chunk-SA2E5GVO.js +0 -2
- package/dist/chunks/chunk-T5ZALLTJ.js +0 -2
- package/dist/chunks/chunk-XPYZOG5H.js +0 -2
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,31 @@ 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.18.0 - 2026-11-26
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
|
|
14
|
+
- **Control charts** in `/descriptive` (after the qcc package, values only, no plots): `qcc` (xbar, R, S, xbar.one, p, np,
|
|
15
|
+
c, u and g charts; sigma by UWAVE-R, MVLUE-R, UWAVE-SD, MVLUE-SD, RMSDF, moving ranges or SD; nsigmas or probability
|
|
16
|
+
limits; newdata; violations of the limits and of runs), `shewhartRules`, `cusum`, `ewma`, `ewmaSmooth`, `ocCurves`,
|
|
17
|
+
`processCapability` (Cp, Cp_l, Cp_u, Cp_k, Cpm with confidence limits), `mqcc` (Hotelling's T2 for subgroups and for
|
|
18
|
+
individual observations, phase I and prediction limits), `qccOverdispersionTest`, `paretoChart` (the table) and
|
|
19
|
+
`qccGroups`; and `medianChart`, the median (Me-R) chart with the JIS factors (not in R).
|
|
20
|
+
- **Paired comparisons** in `/regression`: `btm` (after `BradleyTerry2::BTm`: abilities with a reference player,
|
|
21
|
+
contest-level variables such as home advantage, logit, probit and cauchit links, and player covariates with random
|
|
22
|
+
player effects fitted by PQL to the REML fixed point), `btAbilities`, `countsToBinomial`, and quasi variances
|
|
23
|
+
`qvcalc` and `worstErrors` (after the qvcalc package).
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- `tobit` and `survreg` with a covariate that has a large common value (x + 1e6 and beyond) gave NaN coefficients; they
|
|
28
|
+
now give R's fit. Their standard errors, and `tobitSummary`'s Wald test, stay exact for covariates near 1e±300.
|
|
29
|
+
- `betaregConfint` and `betaregSummary`: standard errors exact for covariates near 1e±300 (they were off or Infinity).
|
|
30
|
+
- `johnsonNeyman`: bounds exact for an outcome near 1e±150 (they were Infinity or 31% off).
|
|
31
|
+
- `sobelTest`: the standard error by `hypot`; `sobelTest`, `mediationMonteCarlo` and `moderatedMediation` stop with an
|
|
32
|
+
error, as `mediate` does, where the coefficient variances leave the double range (about 1e±150 and beyond), instead
|
|
33
|
+
of giving Infinity, 0 or a wrong interval.
|
|
34
|
+
|
|
10
35
|
## 1.17.0 - 2026-11-25
|
|
11
36
|
|
|
12
37
|
### 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.
|
|
19
|
+
状態: **1.18.0。** R の stats パッケージの数値計算の中心
|
|
20
20
|
(分布・記述統計・検定・線形モデル・特殊関数・数値計算・線形代数の基本) を、R と同じ結果でカバーすることを目指しています。残っている作業は
|
|
21
21
|
[ROADMAP.ja.md](ROADMAP.ja.md)、変更履歴は [CHANGELOG.md](CHANGELOG.md) (英語) にあります。
|
|
22
22
|
|
|
@@ -220,14 +220,14 @@ eigen([[2, 1], [1, 3]]).values; // ≈ [3.618033989, 1.381966011]
|
|
|
220
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
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
|
-
| 記述統計 | `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` |
|
|
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`。管理図 (qcc): `qcc` (xbar・R・S・個別値・p・np・c・u・g)、`shewhartRules`、`cusum`、`ewma`、`ewmaSmooth`、`ocCurves`、`processCapability`、`mqcc` (Hotelling の T2)、`qccOverdispersionTest`、`paretoChart`、`qccGroups`、メディアン管理図 `medianChart` |
|
|
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`) |
|
|
226
226
|
| 効果量と検出力 | `cohensD`, `hedgesG` (effectsize。信頼区間は厳密な限界), `etaSquared`, `omegaSquared`, `epsilonSquared` (偏・一般化。`anova` と `Anova` の表から), `powerTTest`, `powerPropTest`, `powerAnovaTest` (`power.*.test`), `pwrTTest`, `pwrT2nTest`, `pwr2pTest`, `pwr2p2nTest`, `pwrPTest`, `pwrNormTest`, `pwrRTest`, `pwrChisqTest`, `pwrAnovaTest`, `pwrF2Test`, `esH`, `esW1`, `esW2`, `cohenES` (pwr) |
|
|
227
227
|
| 試験デザインの標本サイズと検出力 | `epiSsequb`・`epiSsequc`・`epiSsninfb`・`epiSsninfc`・`epiSssupb`・`epiSssupc` (epiR: 比率と平均の同等性・非劣性・優越性)、`powerPaired2x2` (対応のある 2 × 2 表の正確な McNemar 検定の検出力)、`ssizeCT`・`powerCT`・`ssizeEpi`・`powerEpi`・`numDEpi` (生存時間の標本サイズと検出力)、`ph2simon` (Simon の 2 段階デザイン) |
|
|
228
228
|
| メタアナリシス | `escalc` (効果量と標本分散)、`rma` (等効果・ランダム効果のメタアナリシスとメタ回帰)、`confintRma`・`predictRma`・`regtest` (metafor) |
|
|
229
229
|
| 線形モデル | `lm` (formula とデータフレーム、または行列で渡す。`summary` の値も返す), `modelMatrix` (`model.matrix`), `confint`, `predict`。`glm` (formula とデータフレーム、または行列で渡す。R の `stats` のすべての族とリンク) と `glmSummary` (`summary.glm`)、`svyglm` (逆確率重みに対する `survey::svyglm`: デザインに基づく標準誤差)、`glmResiduals` (4 種類)、`glmHatvalues`、`glmRstandard`、`glmCooksDistance`、`glmPredict` (標準誤差つき)、`glmConfint` (プロファイル尤度、厳密な限界)、`glmAnova` (`anova.glm`)。`vcovHC`、`vcovCL` (sandwich: HC0〜HC5、一元と多元のクラスター)、`coeftest`、`coefci` (lmtest)。`step`、`stepAIC` (lm と glm の AIC・BIC による段階的な変数選択)、`drop1`、`add1`、`extractAIC` |
|
|
230
|
-
| 回帰 | `tobit`・`tobitSummary` (AER: `survreg` の上の Tobit 回帰、下限・上限、survreg のどの分布でも)、`betareg`・`betaregSummary`・`betaregConfint`・`betaregPredict`・`betaregResiduals` (betareg: 精度が定数か回帰のベータ回帰、平均のリンク 6 つ、最尤・バイアス補正・バイアス減少)、`loglin`・`loglm`・`loglmResiduals`・`loglmAnova` (反復比例当てはめによる対数線形モデル)、`multinom`・`multinomSummary`・`multinomConfint`・`multinomPredict`・`multinomAnova`・`multinomVcov` (nnet: 重みと decay つきの多項ロジスティック回帰)、`polr`・`polrSummary`・`polrVcov`・`polrPredict`・`polrConfint`・`polrAnova` (MASS: logistic・probit・cloglog・loglog・cauchit のリンクによる順序ロジスティック回帰)、`rlm`・`rlmSummary`・`rlmVcov`・`psiHuber`・`psiHampel`・`psiBisquare` (MASS: M 推定と MM 推定によるロバスト回帰)、`nls`・`nlsSummary`・`nlsVcov`・`nlsPredict`・`nlsConfint`・`nlsAnova` (非線形最小二乗法: 既定・`plinear`・`port` のアルゴリズムと 10 個の自己開始モデル)、`rq`・`rqSummary`・`rqPredict`・`rqPredictInterval`・`rqAnova` (分位点回帰)、`geeglm`・`geeSummary`・`geeVcov`・`geePredict`・`geeAnova`・`geeQIC` (一般化推定方程式)、`glmnet`・`glmnetCoef`・`glmnetPredict`・`cvGlmnet` (Gaussian・binomial・Poisson・multinomial の正則化回帰)。`@numero-uno-jpn/rstats/regression` から import します |
|
|
230
|
+
| 回帰 | `tobit`・`tobitSummary` (AER: `survreg` の上の Tobit 回帰、下限・上限、survreg のどの分布でも)、`betareg`・`betaregSummary`・`betaregConfint`・`betaregPredict`・`betaregResiduals` (betareg: 精度が定数か回帰のベータ回帰、平均のリンク 6 つ、最尤・バイアス補正・バイアス減少)、`loglin`・`loglm`・`loglmResiduals`・`loglmAnova` (反復比例当てはめによる対数線形モデル)、`multinom`・`multinomSummary`・`multinomConfint`・`multinomPredict`・`multinomAnova`・`multinomVcov` (nnet: 重みと decay つきの多項ロジスティック回帰)、`polr`・`polrSummary`・`polrVcov`・`polrPredict`・`polrConfint`・`polrAnova` (MASS: logistic・probit・cloglog・loglog・cauchit のリンクによる順序ロジスティック回帰)、`rlm`・`rlmSummary`・`rlmVcov`・`psiHuber`・`psiHampel`・`psiBisquare` (MASS: M 推定と MM 推定によるロバスト回帰)、`nls`・`nlsSummary`・`nlsVcov`・`nlsPredict`・`nlsConfint`・`nlsAnova` (非線形最小二乗法: 既定・`plinear`・`port` のアルゴリズムと 10 個の自己開始モデル)、`rq`・`rqSummary`・`rqPredict`・`rqPredictInterval`・`rqAnova` (分位点回帰)、`geeglm`・`geeSummary`・`geeVcov`・`geePredict`・`geeAnova`・`geeQIC` (一般化推定方程式)、`glmnet`・`glmnetCoef`・`glmnetPredict`・`cvGlmnet` (Gaussian・binomial・Poisson・multinomial の正則化回帰)、`btm`・`btAbilities`・`countsToBinomial` (BradleyTerry2: 一対比較、対象の共変量と変量効果の PQL も)、`qvcalc`・`worstErrors` (準分散)。`@numero-uno-jpn/rstats/regression` から import します |
|
|
231
231
|
| 多変量解析 | `manova`・`manovaSummary` (逐次 MANOVA: Pillai・Wilks・Hotelling-Lawley・Roy)、`lda`・`ldaPredict` (線形判別分析)、`cancor` (正準相関)、`dist`・`asDist`・`hclust`・`cutree`・`kmeans` (クラスタ分析)、`cmdscale` (古典的多次元尺度法)、`ca`・`caSummary`・`caCoord` (対応分析)、`mjca` (多重対応分析)、`sammon`・`isoMDS` (非計量多次元尺度法)。`@numero-uno-jpn/rstats/multivariate` から import します |
|
|
232
232
|
| 混合モデル | `lmer` (lme4: REML と最尤法、相関のある変量効果と相関のない変量効果、交差と入れ子の群。`fixef`、`vcov`、`VarCorr`、`ranef`、`logLik`、`AIC`、`BIC`、`REMLcrit`、`isSingular`)、`lmerSummary`・`lmerContest`・`lmerAnova` (lmerTest: Satterthwaite の t 検定と F 検定、Type I/II/III の分散分析表。当てはめどうしの尤度比検定)、`glmer` (lme4: glm のすべての族、ラプラス近似と適応的 Gauss-Hermite 求積)、`glmerSummary`・`glmerAnova`・`glmerPredict`・`lmerPredict`・`confintLmer`・`confintGlmer`・`simulateLmer`・`simulateGlmer`。
|
|
233
233
|
`@numero-uno-jpn/rstats/mixed` から import する |
|
|
@@ -302,7 +302,7 @@ double-double 演算で計算しているためです。詳しくは [docs/ja/ac
|
|
|
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
304
|
| `polr` / `rlm` / `nls` (`/regression` から) | 25.0 / 14.6 / 8.0 KB |
|
|
305
|
-
| すべて (`/mixed` と `/regression` を除く) |
|
|
305
|
+
| すべて (`/mixed` と `/regression` を除く) | 340.7 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 で
|
|
315
|
+
minify 済みの ES モジュール、gzip で 341 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.
|
|
320
|
+
import { pnorm } from "https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.18.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.
|
|
17
|
+
Status: **1.18.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
|
|
|
@@ -218,14 +218,14 @@ eigen([[2, 1], [1, 3]]).values; // ≈ [3.618033989, 1.381966011]
|
|
|
218
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
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
|
-
| 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` |
|
|
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`; control charts (qcc): `qcc` (xbar, R, S, individual values, p, np, c, u, g), `shewhartRules`, `cusum`, `ewma`, `ewmaSmooth`, `ocCurves`, `processCapability`, `mqcc` (Hotelling's T2), `qccOverdispersionTest`, `paretoChart`, `qccGroups`, and the median chart `medianChart` |
|
|
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`) |
|
|
224
224
|
| Effect sizes and power | `cohensD`, `hedgesG` (effectsize, with exact confidence limits), `etaSquared`, `omegaSquared`, `epsilonSquared` (partial, generalized, from `anova` and `Anova` tables); `powerTTest`, `powerPropTest`, `powerAnovaTest` (`power.*.test`); `pwrTTest`, `pwrT2nTest`, `pwr2pTest`, `pwr2p2nTest`, `pwrPTest`, `pwrNormTest`, `pwrRTest`, `pwrChisqTest`, `pwrAnovaTest`, `pwrF2Test`, `esH`, `esW1`, `esW2`, `cohenES` (pwr) |
|
|
225
225
|
| Sample size and power for trial designs | `epiSsequb`, `epiSsequc`, `epiSsninfb`, `epiSsninfc`, `epiSssupb`, `epiSssupc` (epiR: equivalence, non-inferiority and superiority for proportions and means), `powerPaired2x2` (exact paired McNemar power), `ssizeCT`, `powerCT`, `ssizeEpi`, `powerEpi`, `numDEpi` (survival sample size and power), `ph2simon` (Simon two-stage designs) |
|
|
226
226
|
| Meta-analysis | `escalc` (effect sizes and sampling variances), `rma` (equal- and random-effects meta-analysis and meta-regression), `confintRma`, `predictRma`, `regtest` (metafor) |
|
|
227
227
|
| Linear models | `lm` (a formula and a data frame, or a matrix, with the `summary` quantities), `modelMatrix` (`model.matrix`), `confint`, `predict`; `glm` (a formula and a data frame, or a matrix; every family and link of R's `stats`) with `glmSummary` (`summary.glm`), `svyglm` (`survey::svyglm` for inverse probability weights: design-based standard errors), `glmResiduals` (the four kinds), `glmHatvalues`, `glmRstandard`, `glmCooksDistance`, `glmPredict` (with standard errors), `glmConfint` (profile likelihood, exact limits), `glmAnova` (`anova.glm`); `vcovHC`, `vcovCL` (sandwich: HC0 to HC5, one-way and multi-way clustered), `coeftest`, `coefci` (lmtest); `step`, `stepAIC` (stepwise selection by AIC or BIC for lm and glm), `drop1`, `add1`, `extractAIC` |
|
|
228
|
-
| Regression | `tobit`, `tobitSummary` (AER: tobit regression on `survreg`, left and right limits, any survreg distribution); `betareg`, `betaregSummary`, `betaregConfint`, `betaregPredict`, `betaregResiduals` (betareg: beta regression with a constant or modelled precision, six mean links, maximum likelihood, bias-corrected and bias-reduced); `loglin`, `loglm`, `loglmResiduals`, `loglmAnova` (log-linear models by iterative proportional fitting); `multinom`, `multinomSummary`, `multinomConfint`, `multinomPredict`, `multinomAnova`, `multinomVcov` (nnet: multinomial logistic regression with weights and decay), `polr`, `polrSummary`, `polrVcov`, `polrPredict`, `polrConfint`, `polrAnova` (MASS: ordinal logistic regression with logistic, probit, cloglog, loglog and cauchit links); `rlm`, `rlmSummary`, `rlmVcov`, `psiHuber`, `psiHampel`, `psiBisquare` (MASS: robust M and MM regression); `nls`, `nlsSummary`, `nlsVcov`, `nlsPredict`, `nlsConfint`, `nlsAnova` (nonlinear least squares: default, `plinear` and `port` algorithms, and 10 self-starting models); `rq`, `rqSummary`, `rqPredict`, `rqPredictInterval`, `rqAnova` (quantile regression); `geeglm`, `geeSummary`, `geeVcov`, `geePredict`, `geeAnova`, `geeQIC` (generalized estimating equations); `glmnet`, `glmnetCoef`, `glmnetPredict`, `cvGlmnet` (penalized Gaussian, binomial, Poisson and multinomial regression), from `@numero-uno-jpn/rstats/regression` |
|
|
228
|
+
| Regression | `tobit`, `tobitSummary` (AER: tobit regression on `survreg`, left and right limits, any survreg distribution); `betareg`, `betaregSummary`, `betaregConfint`, `betaregPredict`, `betaregResiduals` (betareg: beta regression with a constant or modelled precision, six mean links, maximum likelihood, bias-corrected and bias-reduced); `loglin`, `loglm`, `loglmResiduals`, `loglmAnova` (log-linear models by iterative proportional fitting); `multinom`, `multinomSummary`, `multinomConfint`, `multinomPredict`, `multinomAnova`, `multinomVcov` (nnet: multinomial logistic regression with weights and decay), `polr`, `polrSummary`, `polrVcov`, `polrPredict`, `polrConfint`, `polrAnova` (MASS: ordinal logistic regression with logistic, probit, cloglog, loglog and cauchit links); `rlm`, `rlmSummary`, `rlmVcov`, `psiHuber`, `psiHampel`, `psiBisquare` (MASS: robust M and MM regression); `nls`, `nlsSummary`, `nlsVcov`, `nlsPredict`, `nlsConfint`, `nlsAnova` (nonlinear least squares: default, `plinear` and `port` algorithms, and 10 self-starting models); `rq`, `rqSummary`, `rqPredict`, `rqPredictInterval`, `rqAnova` (quantile regression); `geeglm`, `geeSummary`, `geeVcov`, `geePredict`, `geeAnova`, `geeQIC` (generalized estimating equations); `glmnet`, `glmnetCoef`, `glmnetPredict`, `cvGlmnet` (penalized Gaussian, binomial, Poisson and multinomial regression); `btm`, `btAbilities`, `countsToBinomial` (BradleyTerry2: paired comparisons, with player covariates and random effects by PQL), `qvcalc`, `worstErrors` (quasi variances), from `@numero-uno-jpn/rstats/regression` |
|
|
229
229
|
| Multivariate analysis | `manova`, `manovaSummary` (sequential MANOVA: Pillai, Wilks, Hotelling-Lawley and Roy); `lda`, `ldaPredict` (linear discriminant analysis); `cancor` (canonical correlation); `dist`, `asDist`, `hclust`, `cutree`, `kmeans` (cluster analysis); `cmdscale` (classical multidimensional scaling); `ca`, `caSummary`, `caCoord` (correspondence analysis); `mjca` (multiple correspondence analysis); `sammon` and `isoMDS` (non-metric multidimensional scaling), from `@numero-uno-jpn/rstats/multivariate` |
|
|
230
230
|
| Mixed models | `lmer` (lme4: REML and ML, correlated and uncorrelated random effects, crossed and nested grouping factors; `fixef`, `vcov`, `VarCorr`, `ranef`, `logLik`, `AIC`, `BIC`, `REMLcrit`, `isSingular`), `lmerSummary`, `lmerContest` and `lmerAnova` (lmerTest: Satterthwaite's t- and F-tests, Type I/II/III ANOVA tables; likelihood-ratio comparison of fits), `glmer` (lme4: all glm families, Laplace approximation and adaptive Gauss-Hermite quadrature), `glmerSummary`, `glmerAnova`, `glmerPredict`, `lmerPredict`, `confintLmer`, `confintGlmer`, `simulateLmer` and
|
|
231
231
|
`simulateGlmer`, from `@numero-uno-jpn/rstats/mixed` |
|
|
@@ -301,7 +301,7 @@ removed. Approximate sizes after minification and gzip:
|
|
|
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
303
|
| `polr` / `rlm` / `nls` (from `/regression`) | 25.0 / 14.6 / 8.0 KB |
|
|
304
|
-
| everything (except `/mixed` and `/regression`) |
|
|
304
|
+
| everything (except `/mixed` and `/regression`) | 340.7 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`,
|
|
315
|
+
function except those of `/mixed` and `/regression`, 341 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.
|
|
319
|
+
import { pnorm } from "https://cdn.jsdelivr.net/npm/@numero-uno-jpn/rstats@1.18.0/dist/rstats.js";
|
|
320
320
|
console.log(pnorm(1.96)); // ≈ 0.9750021049
|
|
321
321
|
</script>
|
|
322
322
|
```
|
|
@@ -25,6 +25,16 @@ const MODERATED_MEDIATION_KEYS = /* @__PURE__ */ new Set([
|
|
|
25
25
|
"treat", "mediator",
|
|
26
26
|
]);
|
|
27
27
|
/** A coefficient and its variance, by name */
|
|
28
|
+
/**
|
|
29
|
+
* Variances outside the normal doubles (variables on scales beyond about 1e+-150 give coefficient variances of 1e+-300
|
|
30
|
+
* and more, which overflow or underflow): an error, as mediate's, where the formulas would give Infinity, 0 or NaN
|
|
31
|
+
* silently. R's lm reports such variances as NA or 0 (experiments/qccbt118-old-extreme.R).
|
|
32
|
+
*/
|
|
33
|
+
function checkRange(V, role) {
|
|
34
|
+
const MIN = 2 ** -1022, MAX = Number.MAX_VALUE;
|
|
35
|
+
if (V.some((r, i) => r.some((v) => !Number.isFinite(v)) || !(r[i] >= MIN && r[i] <= MAX)))
|
|
36
|
+
throw new RangeError(`the ${role} model's coefficient covariance is out of the double range (rescale the variables)`);
|
|
37
|
+
}
|
|
28
38
|
function coefficient(fit, name, role) {
|
|
29
39
|
const cov = modelCovariance(fit);
|
|
30
40
|
const k = cov.names.indexOf(name);
|
|
@@ -62,8 +72,11 @@ function coefficient(fit, name, role) {
|
|
|
62
72
|
export function sobelTest(modelM, modelY, options) {
|
|
63
73
|
checkOptions(options, SOBEL_TEST_KEYS, "sobelTest"); // generated: option-keys.mjs
|
|
64
74
|
const ca = coefficient(modelM, options.treat, "mediator"), cb = coefficient(modelY, options.mediator, "outcome");
|
|
75
|
+
checkRange([[ca.v]], "mediator");
|
|
76
|
+
checkRange([[cb.v]], "outcome");
|
|
65
77
|
const a = ca.value, b = cb.value, sa = Math.sqrt(ca.v), sb = Math.sqrt(cb.v);
|
|
66
|
-
|
|
78
|
+
// hypot: b^2 sa^2 + a^2 sb^2 overflows or underflows for variables far from 1 in size
|
|
79
|
+
const ab = a * b, se = Math.hypot(b * sa, a * sb), z = ab / se;
|
|
67
80
|
return { a, sa, b, sb, ab, se, z, p: 2 * pnorm(Math.abs(z), { lowerTail: false }) };
|
|
68
81
|
}
|
|
69
82
|
const q7 = (v, level) => {
|
|
@@ -110,6 +123,8 @@ export function mediationMonteCarlo(modelM, modelY, options) {
|
|
|
110
123
|
const ca = coefficient(modelM, options.treat, "mediator");
|
|
111
124
|
const cb = coefficient(modelY, options.mediator, "outcome"), cc = coefficient(modelY, options.treat, "outcome");
|
|
112
125
|
const iM = cb.names.indexOf(options.mediator), iX = cb.names.indexOf(options.treat);
|
|
126
|
+
checkRange([[ca.v]], "mediator");
|
|
127
|
+
checkRange([[cb.vcov[iM][iM], cb.vcov[iM][iX]], [cb.vcov[iX][iM], cb.vcov[iX][iX]]], "outcome");
|
|
113
128
|
const aDraws = rnorm(nmc, { mean: ca.value, sd: Math.sqrt(ca.v) });
|
|
114
129
|
const bc = mvrnorm(nmc, [cb.value, cc.value], [[cb.vcov[iM][iM], cb.vcov[iM][iX]], [cb.vcov[iX][iM], cb.vcov[iX][iX]]]);
|
|
115
130
|
const ind = aDraws.map((a, j) => a * bc[j][0]), dir = bc.map((r) => r[1]), tot = ind.map((v, j) => v + dir[j]);
|
|
@@ -212,6 +227,8 @@ export function moderatedMediation(modelM, modelY, data, options) {
|
|
|
212
227
|
const all = "family" in fit ? fit.model.columnNames : fit.coefNames;
|
|
213
228
|
return fit.coefficients[all.indexOf(nm)];
|
|
214
229
|
});
|
|
230
|
+
checkRange(covM.vcov, "mediator");
|
|
231
|
+
checkRange(covY.vcov, "outcome");
|
|
215
232
|
const bm = mvrnorm(nmc, coefOf(modelM, covM.names), covM.vcov), bo = mvrnorm(nmc, coefOf(modelY, covY.names), covY.vcov);
|
|
216
233
|
const cm = coefOf(modelM, covM.names), co = coefOf(modelY, covY.names);
|
|
217
234
|
const find = (names, a, b) => {
|
|
@@ -52,7 +52,16 @@ function jn(q, alpha) {
|
|
|
52
52
|
throw new RangeError("alpha must be in (0, 1)");
|
|
53
53
|
const crit = qt(1 - alpha / 2, q.df);
|
|
54
54
|
const c2 = crit * crit;
|
|
55
|
-
|
|
55
|
+
// The quadratic on rescaled coefficients: the outcome's unit k (a power of 2 near sqrt(v11)) and the moderator's unit m
|
|
56
|
+
// (a power of 2 near sqrt(v11 / v33)), so that its coefficients are near 1; the bounds are scaled back by m exactly.
|
|
57
|
+
// Without this, an outcome near 1e+-150 or a moderator near 1e+-300 overflows or underflows in B^2 - 4 A C.
|
|
58
|
+
const MIN = 2 ** -1022, MAX = Number.MAX_VALUE;
|
|
59
|
+
if (![q.v11, q.v33].every((v) => v >= MIN && v <= MAX) || !Number.isFinite(q.v13)) {
|
|
60
|
+
throw new RangeError("the model's coefficient covariance is out of the double range (rescale the variables)");
|
|
61
|
+
}
|
|
62
|
+
const k = 2 ** Math.round(Math.log2(q.v11) / 2), m = 2 ** Math.round((Math.log2(q.v11) - Math.log2(q.v33)) / 2);
|
|
63
|
+
const b1 = q.b1 / k, b3 = q.b3 / k * m, v11 = q.v11 / k / k, v13 = q.v13 / k / k * m, v33 = q.v33 / k / k * m * m;
|
|
64
|
+
const A = b3 * b3 - c2 * v33, B = 2 * (b1 * b3 - c2 * v13), C = b1 * b1 - c2 * v11;
|
|
56
65
|
const disc = B * B - 4 * A * C;
|
|
57
66
|
const lo = Math.min(...q.w), hi = Math.max(...q.w);
|
|
58
67
|
const modrange = [lo, hi];
|
|
@@ -66,7 +75,7 @@ function jn(q, alpha) {
|
|
|
66
75
|
// the root without cancellation, then the other from the product of the roots
|
|
67
76
|
const r1 = (-B - Math.sign(B || 1) * s) / (2 * A);
|
|
68
77
|
const r2 = r1 === 0 ? (-B + Math.sign(B || 1) * s) / (2 * A) : C / (A * r1);
|
|
69
|
-
bounds = r1 <= r2 ? [r1, r2] : [r2, r1];
|
|
78
|
+
bounds = r1 <= r2 ? [r1 * m, r2 * m] : [r2 * m, r1 * m];
|
|
70
79
|
}
|
|
71
80
|
// interactions reports inside = FALSE when the interval could not be found
|
|
72
81
|
const inside = !failed && A < 0;
|
package/dist/causal.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{a as Ie,b as Ae}from"./chunks/chunk-JG4TWDQ4.js";import{p as Pe,x as ne,y as re}from"./chunks/chunk-EIUK6TKF.js";import{a as Ve}from"./chunks/chunk-GOYTZ5RB.js";import{a as ae}from"./chunks/chunk-TTOJDLIQ.js";import{h as Ce}from"./chunks/chunk-ZRKXHVTJ.js";import{a as pe}from"./chunks/chunk-GLABB425.js";import{b as Be}from"./chunks/chunk-JV3POOET.js";import"./chunks/chunk-3Y4DNK4O.js";import{f as Ge,h as be,j as Ue}from"./chunks/chunk-O4D43LWM.js";import"./chunks/chunk-LKKUUREA.js";import"./chunks/chunk-DD2GG4EC.js";import"./chunks/chunk-6MLDKHTY.js";import"./chunks/chunk-EB45CDFA.js";import{a as Ke}from"./chunks/chunk-XR7Y4ZV6.js";import{v as te}from"./chunks/chunk-4BGGWQWS.js";import"./chunks/chunk-SA2E5GVO.js";import"./chunks/chunk-XPYZOG5H.js";import"./chunks/chunk-K3YSXDTU.js";import{a as We}from"./chunks/chunk-KY53MB3J.js";import{e as qe,f as xe,k as ue}from"./chunks/chunk-BWDXJL7T.js";import"./chunks/chunk-23QCIPET.js";import{a as Ne}from"./chunks/chunk-RM5D7Q7W.js";import{e as He,f as De,g as ze,m as Fe,n as ge,o as we}from"./chunks/chunk-NCRL33PQ.js";import"./chunks/chunk-JHABMYNH.js";import"./chunks/chunk-I2KJMF4G.js";import"./chunks/chunk-72I7GDWL.js";import"./chunks/chunk-5UFVACSV.js";import"./chunks/chunk-LY2UZGUL.js";import{r as _e,s as Z,t as fe}from"./chunks/chunk-XEJX5HBJ.js";import{h as ee,k as B,l as Te}from"./chunks/chunk-YMI33F7W.js";import{l as K}from"./chunks/chunk-GTSK4IN4.js";function Ye(e){let s=0;for(let n of e)Math.abs(n)>s&&(s=Math.abs(n));return s>0&&Number.isFinite(s)?Math.floor(Math.log2(s)):0}function ie(e){let s=Ye(e),n=e.map(o=>ee(o,-s)),t=n[0]??0,r=n.map(o=>o-t),l=Ye(r);return{values:r.map(o=>ee(o,-l)),unitExp:s+l}}var yt=["estimand","M","caliper"];function Et(e,s,n={}){K(n,yt,"match");let{estimand:t="ATT",M:r=1,caliper:l}=n,o=e.length;if(s.length!==o)throw new RangeError("length(Tr) != nrow(X)");if(t!=="ATT"&&t!=="ATE"&&t!=="ATC")throw new RangeError(`invalid 'estimand': ${String(t)}`);if(!Number.isInteger(r)||r<1)throw new RangeError("'M' must be a positive integer");if(l!==void 0&&!(l>0&&Number.isFinite(l)))throw new RangeError("'caliper' must be a positive number");let m=[];for(let g=0;g<o;g++){let A=e[g];if(A===!0||A===1)m.push(!0);else if(A===!1||A===0)m.push(!1);else throw typeof A=="number"&&Number.isNaN(A)?new RangeError("Match(): input includes NAs"):new RangeError("Treatment indicator ('Tr') must be a logical variable---i.e., TRUE (1) or FALSE (0)");if(!Number.isFinite(s[g]))throw new RangeError("Match(): input includes NAs")}if(!m.includes(!0)||!m.includes(!1))throw new RangeError("Treatment indicator ('Tr') must contain both treatment and control observations");let i=0;for(let g of s)i=Math.max(i,Math.abs(g));let c=i>0?Math.floor(Math.log2(i)):0,a=s.map(g=>ee(g,-c)),u=null,E=1/0;if(l!==void 0){let{values:g,unitExp:A}=ie(a),x=0;for(let S of g)x+=S;x/=o;let k=0;for(let S of g)k+=(S-x)*(S-x);let $=ee(Math.sqrt(k/o),A);E=l*$,u=ee(E,c)}let v=[new Array(o).fill(!1),new Array(o).fill(!1)],f=[],d=[],w=[],M=[],y=0,h=0;for(let g=0;g<o;g++){let A=m[g];if(A?t==="ATC":t==="ATT")continue;let x=v[A?0:1],k=[];for(let O=0;O<o;O++){if(m[O]===A)continue;let F=Math.abs(a[g]-a[O]);F>E||x[O]||k.push({j:O,d:F})}if(k.length<r){h++,w.push(g+1);continue}let S=k.map(O=>O.d).sort((O,F)=>O-F)[r-1],_=k.filter(O=>O.d<=S).map(O=>O.j),z;if(_.length===r)z=_;else{M.push(_.length),z=[],_=_.slice();for(let O=0;O<r;O++){let F=_.length,D=Math.min(F-1,Math.floor(F*(1-ze())));z.push(_[D]),_.splice(D,1)}z.sort((O,F)=>O-F)}for(let O of z)x[O]=!0,f.push((A?g:O)+1),d.push((A?O:g)+1);y++}let p=new Set([...f,...d]),N=u===null?[]:w.filter(g=>!p.has(g)),b=[];return f.length===0&&b.push("'Match' object contains no valid matches (probably because of the caliper or the exact option)."),{indexTreated:f,indexControl:d,weights:f.map(()=>1/r),indexDropped:N.length>0?N:null,caliper:l??null,ecaliper:u,nobs:o,wnobs:y,ndrops:h,estimand:t,M:r,tieSizes:M,warnings:b}}var Mt=["match","weightit","weights","estimand","levels","binary"],Tt=["factor"];function Je(e){let s=new Array(e.nobs).fill(0);return e.indexTreated.forEach((n,t)=>{s[n-1]+=e.weights[t],s[e.indexControl[t]-1]+=e.weights[t]}),s}function At(e,s){if(e===!0||e===1)return!0;if(e===!1||e===0)return!1;throw new RangeError(`'tr' must be 0 or 1 (element ${s+1})`)}function de(e,s,n,t){let r=0,l=0,o=0;for(let c=0;c<e.length;c++){if(n[c]!==t)continue;let a=s===null?1:s[c];r+=a,l+=a*a,o+=a*e[c]}let m=o/r,i=0;for(let c=0;c<e.length;c++){if(n[c]!==t)continue;let a=s===null?1:s[c];i+=a*(e[c]-m)*(e[c]-m)}return[m,i/(r-l/r)]}function Nt(e,s,n={}){K(n,Mt,"balTab");let{match:t,weightit:r,binary:l="raw",levels:o={}}=n;if(t!==void 0&&r!==void 0)throw new RangeError("give 'match' or 'weightit', not both");let m=t!==void 0?Je(t):r!==void 0?r.weights:n.weights,i=t?.estimand??r?.estimand??n.estimand??"ATT",c=new Set(t?.indexDropped??[]),a=t!==void 0&&t.caliper!==null,u=e.length,E=e.map(At);if(!E.includes(!0)||!E.includes(!1))throw new RangeError("'tr' must contain both groups");if(m!==void 0&&m.length!==u)throw new RangeError("'weights' must have the length of 'tr'");if(i==="ATO"&&m===void 0)throw new RangeError("estimand 'ATO' needs the weights");let v=[];r?.ps&&v.push(["prop.score",r.ps,"Distance"]);for(let[M,y]of Object.entries(s)){if(y.length!==u)throw new RangeError(`covariate '${M}' must have the length of 'tr'`);if(y.every(p=>typeof p=="number")){v.push([M,y,null]);continue}if(!y.every(p=>typeof p=="string"))throw new TypeError(`covariate '${M}' must be all numbers or all strings`);let h=o[M]??[...new Set(y)].sort(Ne);if(y.some(p=>!h.includes(p)))throw new RangeError(`covariate '${M}' has values not among its levels`);for(let p of h.length===2?h.slice(1):h)v.push([`${M}_${p}`,y.map(N=>N===p?1:0),null])}let f=E.map(()=>!0),d=r!==void 0,w={rows:v.map(([M])=>M),type:[],diffUn:[],diffAdj:[],vRatioUn:[],vRatioAdj:[],observations:{rows:d?["Unadjusted","Adjusted"]:["All (ESS)","All (Unweighted)","Matched (ESS)","Matched (Unweighted)","Unmatched",...a?["Discarded"]:[]],control:[],treated:[]}};for(let[M,y,h]of v){if(y.some(D=>!Number.isFinite(D)))throw new RangeError(`covariate '${M}' has missing or infinite values`);let p=h===null&&new Set(y).size<=2&&y.every(D=>D===0||D===1),N=p?null:ie(y),b=N===null?y:N.values,[g,A]=de(b,null,E,!0),[x,k]=de(b,null,E,!1),$;if(i==="ATO"){let[D,Q]=de(b,m,f,!0);$=Math.sqrt(p?D*(1-D):Q)}else{let D=p?[g*(1-g),x*(1-x)]:[A,k];if($=Math.sqrt(i==="ATT"?D[0]:i==="ATC"?D[1]:(D[0]+D[1])/2),ee($,N?.unitExp??0)<Math.sqrt(Number.EPSILON)){let[Q,V]=de(b,null,f,!0);$=Math.sqrt(p?Q*(1-Q):V)}}let S=p&&l==="raw"?1:$;if(w.type.push(h??(p?"Binary":"Contin.")),w.diffUn.push(g===x?0:(g-x)/S),w.vRatioUn.push(p||k===0?NaN:A/k),m===void 0){w.diffAdj.push(NaN),w.vRatioAdj.push(NaN);continue}let[_,z]=de(b,m,E,!0),[O,F]=de(b,m,E,!1);w.diffAdj.push(_===O?0:(_-O)/S),w.vRatioAdj.push(p||F===0?NaN:z/F)}for(let M of[!1,!0]){let y=0,h=0,p=0,N=0,b=0;for(let x=0;x<u;x++){if(E[x]!==M||(y++,c.has(x+1)&&b++,m===void 0))continue;let k=m[x];h+=k,p+=k*k,k>0&&N++}let g=M?w.observations.treated:w.observations.control;if(d){g.push(y,h*h/p);continue}let A=m===void 0?[NaN,NaN,NaN]:[h*h/p,N,y-N-b];a&&A.push(b),g.push(y,y,...A)}return w}function Rt(e,s,n={}){K(n,Tt,"tableOneSmd");let{factor:t=!1}=n;if(e.length!==s.length)throw new RangeError("'x' and 'group' must have the same length");let r=[...new Set(s)].sort((f,d)=>f<d?-1:f>d?1:0);if(r.length!==2)throw new RangeError("'group' must have exactly two levels");let l=f=>f!==null&&!(typeof f=="number"&&Number.isNaN(f)),o=r.map(f=>e.filter((d,w)=>s[w]===f&&l(d)));if(!t){if(o.flat().some(h=>typeof h!="number"))throw new TypeError("'x' must be numeric unless factor: true");let f=ie(o.flat()).values,w=[f.slice(0,o[0].length),f.slice(o[0].length)].map(h=>{let p=h.reduce((N,b)=>N+b,0)/h.length;return[p,h.reduce((N,b)=>N+(b-p)*(b-p),0)/(h.length-1)]}),M=w[1][0]-w[0][0],y=Math.sqrt((w[0][1]+w[1][1])/2);return M===0?0:Math.abs(M)/y}let m=[...new Set(o.flat())].sort((f,d)=>typeof f=="number"&&typeof d=="number"?f-d:String(f)<String(d)?-1:String(f)>String(d)?1:0),i=o.map(f=>m.slice(1).map(d=>f.filter(w=>w===d).length/f.length)),c=i[1].map((f,d)=>f-i[0][d]);if(c.every(f=>f===0))return 0;let a=c.length,u=Array.from({length:a},(f,d)=>Array.from({length:a},(w,M)=>{let y=h=>d===M?h[d]*(1-h[d]):-h[d]*h[M];return(y(i[0])+y(i[1]))/2}));if(u.every(f=>f.every(d=>d===0)))return NaN;let E=Be(u),v=0;for(let f=0;f<a;f++)for(let d=0;d<a;d++)v+=c[f]*E[f][d]*c[d];return Math.sqrt(v)}var St=["method","link","estimand","stabilize"],Ot=["at","lower","treat"],oe=Number.EPSILON;function xt(e){return e.map((s,n)=>{if(s===!0||s===1)return 1;if(s===!1||s===0)return 0;throw new RangeError(`'tr' must be 0 or 1 (element ${n+1})`)})}function Ct(e,s){let n=[],t=[],r={};for(let[l,o]of Object.entries(e)){if(o.length!==s)throw new RangeError(`covariate '${l}' must have the length of 'tr'`);if(o.every(c=>typeof c=="number")){let c=o;if(c.some(a=>!Number.isFinite(a)))throw new RangeError(`covariate '${l}' has missing or infinite values`);n.push(l),t.push(c.slice());continue}if(!o.every(c=>typeof c=="string"))throw new TypeError(`covariate '${l}' must be all numbers or all strings`);let m=o,i=[...new Set(m)].sort(Ne);r[l]=i;for(let c of i.slice(1))n.push(l+c),t.push(m.map(a=>a===c?1:0))}return{names:n,columns:t,factors:r}}function Xe(e){return e.map(s=>{let n=s.reduce((r,l)=>r+l,0)/s.length,t=s.reduce((r,l)=>Math.max(r,Math.abs(l-n)),0);return s.map(r=>t>0?(r-n)/t:0)})}function $t(e){let s=e[0]?.length??0,n=[new Array(s).fill(1/Math.sqrt(s))],t=[];for(let r of e){let l=Math.sqrt(r.reduce((i,c)=>i+c*c,0)),o=r.slice();for(let i=0;i<2;i++)for(let c of n){let a=o.reduce((u,E,v)=>u+E*c[v],0);for(let u=0;u<s;u++)o[u]-=a*c[u]}let m=Math.sqrt(o.reduce((i,c)=>i+c*c,0));m>1e-7*l&&(n.push(o.map(i=>i/m)),t.push(r))}return t}function rt(e,s){let n=s.length,t=e.map((o,m)=>[...o,s[m]]),r=0;for(let o of e)for(let m of o)r=Math.max(r,Math.abs(m));for(let o=0;o<n;o++){let m=o;for(let i=o+1;i<n;i++)Math.abs(t[i][o])>Math.abs(t[m][o])&&(m=i);if(!(Math.abs(t[m][o])>1e-13*r))return null;[t[o],t[m]]=[t[m],t[o]];for(let i=o+1;i<n;i++){let c=t[i][o]/t[o][o];if(c!==0)for(let a=o;a<=n;a++)t[i][a]-=c*t[o][a]}}let l=new Array(n).fill(0);for(let o=n-1;o>=0;o--){let m=t[o][n];for(let i=o+1;i<n;i++)m-=t[o][i]*l[i];l[o]=m/t[o][o]}return l}function $e(e,s){return Array.from({length:s},(n,t)=>[1,...e.map(r=>r[t])])}function Qe(e,s,n){let t=n.length,r=new Array(t).fill(0);r[0]=Math.log(n[0]/s.length);let l=c=>{let a=0;for(let u of s)a+=Math.exp(e[u].reduce((E,v,f)=>E+v*c[f],0));return a-n.reduce((u,E,v)=>u+E*c[v],0)},o=l(r),m=!1,i=1e-13*Math.max(1,Math.abs(n[0]));for(let c=0;c<200;c++){let a=n.map(h=>-h),u=Array.from({length:t},()=>new Array(t).fill(0));for(let h of s){let p=e[h],N=Math.exp(p.reduce((b,g,A)=>b+g*r[A],0));for(let b=0;b<t;b++){a[b]+=N*p[b];for(let g=0;g<=b;g++)u[b][g]+=N*p[b]*p[g]}}for(let h=0;h<t;h++)for(let p=h+1;p<t;p++)u[h][p]=u[p][h];let E=Math.max(...a.map(Math.abs));if(E<=i){m=!0;break}let v=rt(u,a.map(h=>-h));if(v===null||v.some(h=>!Number.isFinite(h)))break;let f=1e-12*(Math.abs(o)+1),d=1,w=r.map((h,p)=>h+v[p]),M=l(w);for(;!(M<=o+f)&&d>1e-10;)d/=2,w=r.map((h,p)=>h+d*v[p]),M=l(w);if(!(M<=o+f))break;let y=v.some((h,p)=>Math.abs(d*h)>1e-15*Math.max(1,Math.abs(r[p])));if(r.splice(0,t,...w),o=M,!y){m=E<=1e-9*Math.max(1,Math.abs(n[0]));break}}return{eta:e.map(c=>c.reduce((a,u,E)=>a+u*r[E],0)),converged:m}}var kt={logit:{p:e=>te(e),q:e=>te(-e),d:e=>te(e)*te(-e)},probit:{p:e=>Z(e),q:e=>Z(-e),d:e=>_e(e)}};function jt(e,s,n,t,r){let l=s.length,o=r.length,m=r.slice(),i=d=>{let w=new Array(o).fill(0),M=Array.from({length:o},()=>new Array(o).fill(0));for(let y=0;y<l;y++){let h=e[y],p=h.reduce(($,S,_)=>$+S*d[_],0),N=t.p(p),b=t.q(p),g=t.d(p),A=s[y],x,k;n==="ATE"?(x=A?1/N:-1/b,k=A?-g/(N*N):-g/(b*b)):n==="ATT"?(x=A?1:-N/b,k=A?0:-g/(b*b)):n==="ATC"?(x=A?b/N:-1,k=A?-g/(N*N):0):(x=A?b:-N,k=-g);for(let $=0;$<o;$++){w[$]+=x*h[$];for(let S=0;S<o;S++)M[$][S]+=k*h[$]*h[S]}}return{g:w,J:M}},c=d=>d.reduce((w,M)=>w+M*M,0),{g:a,J:u}=i(m),E=c(a),v=!1,f=1e-12*l;for(let d=0;d<200;d++){if(Math.max(...a.map(Math.abs))<=f){v=!0;break}let w=rt(u,a.map(N=>-N));if(w===null||w.some(N=>!Number.isFinite(N)))break;let M=1,y=m.map((N,b)=>N+w[b]),h=i(y),p=c(h.g);for(;!(p<E)&&M>1e-10;)M/=2,y=m.map((N,b)=>N+M*w[b]),h=i(y),p=c(h.g);if(!(p<E)){v=Math.max(...a.map(Math.abs))<=1e-8*l;break}m=y,a=h.g,u=h.J,E=p}return{eta:e.map(d=>d.reduce((w,M,y)=>w+M*m[y],0)),converged:v}}function Ze(e,s,n,t){switch(e){case"ATE":return s?1/n:1/t;case"ATT":return s?1:n/t;case"ATC":return s?t/n:1;default:return s?t:n}}var Lt={glm:"glm.fit: algorithm did not converge",cbps:"The covariate balancing conditions could not be solved, so the covariates will not be exactly balanced.",ipt:"The optimization failed to converge; consider using fewer covariates or a different link function.",ebal:"The estimated weights do not balance the covariates, indicating the optimization arrived at a degenerate solution. Try decreasing the number of variables supplied to the optimization."},et={cbps:"covariate balancing propensity score weighting",ipt:"inverse probability tilting",ebal:"entropy balancing"};function _t(e,s,n={}){K(n,St,"weightit");let{method:t="glm",estimand:r="ATE",stabilize:l=!1}=n,o=t==="ps"?"glm":t;if(!["glm","cbps","ipt","ebal"].includes(o))throw new RangeError(`unknown method '${String(t)}'`);if(!["ATE","ATT","ATC","ATO"].includes(r))throw new RangeError(`unknown estimand '${String(r)}'`);let m=o==="ebal"?null:n.link??"logit";if(m!==null&&!{glm:["logit","probit","cauchit","cloglog"],cbps:["logit","probit"],ipt:["logit"]}[o].includes(m))throw new RangeError(`link '${m}' is not available for method '${o}'`);if((o==="ipt"||o==="ebal")&&r==="ATO")throw new RangeError(`"ATO" is not an allowable estimand for ${et[o]} with a binary treatment. Only "ATE", "ATT", and "ATC" are allowed.`);let c=[];if(l&&o==="glm"&&r!=="ATE")throw new RangeError('`stabilize` can only be supplied when `estimand = "ATE"`.');l&&o!=="glm"&&c.push(`\`stabilize\` cannot be used with ${et[o]} and will be ignored.`);let a=xt(e),u=a.length;if(!a.includes(0)||!a.includes(1))throw new RangeError("The treatment must have at least two unique values.");let{names:E,columns:v}=Ct(s,u);if(v.length===0)throw new RangeError("'covariates' must have at least one column");let f=$t(v.map(b=>ie(b).values)),d=a.flatMap((b,g)=>b?[g]:[]),w=a.flatMap((b,g)=>b?[]:[g]),M=a.map((b,g)=>g),y=r==="ATT"?1:r==="ATC"?0:null,h=null,p,N=!0;if(o==="glm"){let b=we(()=>be($e(f,u).map(g=>g.slice(1)),a,{family:"binomial",link:m}));N=b.converged,h=b.fittedValues.map((g,A)=>m==="logit"&&Math.abs(b.linearPredictors[A])>30?b.linearPredictors[A]>0?1-oe:oe:Math.min(Math.max(g,oe),1-oe)),p=a.map((g,A)=>Ze(r,g,h[A],1-h[A])),h.some(g=>g<=10*oe||g>=1-10*oe)&&c.push("Propensity scores numerically equal to 0 or 1 were estimated, indicating perfect separation. These may yield problems with inference. See `?method_glm` (`?WeightIt::method_glm()`) for details.")}else if(o==="cbps"){let b=Xe(f),g=$e(b,u),A=we(()=>be(g.map(S=>S.slice(1)),a,{family:"binomial",link:m})).coefficients.map(S=>Number.isFinite(S)?S:0),x=kt[m],k=jt(g,a,r,x,A);N=k.converged;let $=S=>Math.min(Math.max(S,oe),1-oe);h=k.eta.map(S=>$(x.p(S))),p=a.map((S,_)=>Ze(r,S,h[_],$(x.q(k.eta[_]))))}else{let b=Xe(f),g=$e(b,u),A=_=>g[0].map((z,O)=>_.reduce((F,D)=>F+g[D][O],0)),x={C:r!=="ATC",T:r!=="ATT"},k=o==="ebal"&&r==="ATE"?{C:A(M),T:A(M)}:{C:A(d),T:A(w)},$=x.C?Qe(g,w,k.C):null,S=x.T?Qe(g,d,k.T):null;if(N=($?.converged??!0)&&(S?.converged??!0),o==="ebal")p=a.map((_,z)=>_?S?Math.exp(S.eta[z]):1:$?Math.exp($.eta[z]):1);else{let _=r==="ATE"?1:0;p=a.map((z,O)=>z?S?_+Math.exp(S.eta[O]):1:$?_+Math.exp($.eta[O]):1),h=a.map((z,O)=>r==="ATT"?te($.eta[O]):r==="ATC"?te(-S.eta[O]):z?te(-S.eta[O]):te($.eta[O]))}}if(N||c.push(Lt[o]),l&&o==="glm"){let b=[w.length/u,d.length/u];p=p.map((g,A)=>g*b[a[A]])}return p.every(b=>Math.abs(b-1)<Math.sqrt(oe))&&c.push("All weights are 1, possibly indicating an estimation failure."),ge("weightit",{weights:p,ps:h,treat:a,estimand:r,method:o,link:m,stabilized:l&&o==="glm",focal:y,columnNames:E,converged:N,warnings:c})}function tt(e,s,n,t){let r=e.slice();if(s.length===0)return r;let l=s.map(i=>e[i]),o,m;if(n<1){let i=n<.5?1-n:n;o=ae(l,i,{type:3}),m=ae(l,1-i,{type:3})}else{if(!Number.isInteger(n))throw new RangeError("'at' must be below 1 or a whole number");if(n>=l.length)return null;let i=n>l.length/2?l.length-n:n,c=l.slice().sort((a,u)=>a-u);o=c[c.length-1-i],m=c[i]}for(let i of s)e[i]>o&&(r[i]=o),t&&e[i]<m&&(r[i]=m);return r}var nt=e=>`\`at\` must be less than ${e}, the number of units. Weights will not be trimmed.`;function qt(e,s){K(s,Ot,"trimWeights");let{at:n,lower:t=!1}=s;if(!(n>0)||!Number.isFinite(n))throw new RangeError("'at' must be a positive number");if(Array.isArray(e)||ArrayBuffer.isView(e)){let m=Array.from(e);if(s.treat!==void 0&&s.treat.length!==m.length)throw new RangeError("'treat' must have the length of 'w'");let i=tt(m,m.map((c,a)=>a),n,t);return i===null?(Fe("trimWeights",[nt(m.length)]),m):i}let r=e,l=r.treat.flatMap((m,i)=>r.focal===null||m!==r.focal?[i]:[]),o=tt(r.weights,l,n,t);return o===null?ge("trimWeights",{...r,weights:r.weights.slice(),warnings:[nt(l.length)]}):{...r,weights:o,warnings:r.warnings.slice()}}function ot(e,s){let n=s.map(a=>e[a]),t=n.length,r=0,l=0;for(let a of n)r+=a,l+=a*a;let o=r/t,m=n.reduce((a,u)=>a+(u-o)*(u-o),0),i=n.map(a=>o>0?a/o:NaN),c=n.map((a,u)=>u).sort((a,u)=>n[u]-n[a]||a-u).slice(0,5).reverse();return{range:[Math.min(...n),Math.max(...n)],top:{index:c.map(a=>s[a]+1),weights:c.map(a=>n[a])},mean:o,coefOfVar:Math.sqrt(m/(t-1))/o,scaledMad:i.reduce((a,u)=>a+Math.abs(u-1),0)/t,negativeEntropy:i.reduce((a,u)=>a+(Number.isNaN(u)?NaN:u>0?u*Math.log(u):0),0)/t,numZeros:n.filter(a=>Math.abs(a)<1e-10).length,n:t,ess:r*r/l}}function Ht(e){let s=e.treat.flatMap((t,r)=>t?[r]:[]),n=e.treat.flatMap((t,r)=>t?[]:[r]);return{treated:ot(e.weights,s),control:ot(e.weights,n),stabilized:e.stabilized}}var Dt=new Set(["treat","mediator","treatValue","controlValue","covariates","sims","boot","bootCiType","confLevel","robustSE"]);function st(e,s,n){let t=Ge(e);if(t===void 0||typeof e.formula!="string")throw new TypeError(`the ${n} model must be a formula fit of lm or glm`);if(t.weights!==void 0||t.offset!==void 0)throw new RangeError(`weights and offsets are not supported (the ${n} model)`);let r="family"in e,l=a=>a;if(r){let a=e;if(a.family==="binomial"&&(a.link==="logit"||a.link==="probit"))l=Ue(a).linkinv;else if(!(a.family==="gaussian"&&a.link==="identity"))throw new RangeError(`the ${n} model's family is not supported (binomial with logit or probit, or gaussian)`)}if(e.coefficients.some(a=>Number.isNaN(a)))throw new RangeError(`the ${n} model has aliased coefficients`);let o=e.formula,m=Ke(We(o,!1,!1,!0),s,{...t.contrasts===void 0?{}:{contrasts:t.contrasts},...t.levels===void 0?{}:{levels:t.levels},dropUnusedLevels:!0,anyResponse:!0}),i=r?e.model.rows:e.rows,c=r?e.model.columnNames:e.coefNames;if(m.frame.rows.length!==i.length||m.frame.rows.some((a,u)=>a!==i[u])||m.colNames.join("\0")!==c.join("\0"))throw new RangeError(`the data do not give the ${n} model's rows and columns (pass the data it was fitted on)`);return{fit:e,isGlm:r,formula:o,options:t,coef:e.coefficients.slice(),names:c,linkinv:l,d:m,rows:i}}function ve(e,s,n,t){let r=e.d.terms.variables.indexOf(s);if(r<0){if(t)throw new RangeError(`'${s}' is not a variable of the ${n} model`);return-1}if(e.d.frame.variables[r].kind!=="numeric")throw new RangeError(`'${s}' must be numeric in the ${n} model`);return r}function ft(e,s,n){let t=e.d.frame.variables.map((r,l)=>{let o=s.get(l);return o===void 0?r:{kind:"numeric",values:typeof o=="number"?new Array(n).fill(o):o.slice()}});return e.d.rowsOf(t,n)}var ce=(e,s)=>{let n=0;for(let t=0;t<e.length;t++)n+=e[t]*s[t];return n};function at(e,s,n,t){let r=e.length,l=2**-1022,o=Number.MAX_VALUE;if(s.some((a,u)=>a.some(E=>!Number.isFinite(E))||!(a[u]>=l&&a[u]<=o)))throw new RangeError(`the ${t} model's coefficient covariance is out of the double range (rescale the variables, or use boot: true)`);let m=Ie(s),i=ue(n*r),c=[];for(let a=0;a<n;a++){let u=[];for(let E=0;E<r;E++){let v=0;for(let f=0;f<r;f++)v+=i[a*r+f]*m[f][E];u.push(e[E]+v)}c.push(u)}return c}function Se(e,s){let n=new Map(e.mCov);return n.set(e.mTreat,s),ft(e.m,n,e.n)}function Ee(e,s,n){let t=new Map(e.yCov);return t.set(e.yTreat,s),t.set(e.yMed,n),ft(e.y,t,e.n)}function Re(e,s,n,t){return Ee(e,s,n).map(r=>e.y.linkinv(ce(r,t)))}var ye=(e,s)=>B(e.map((n,t)=>n-s[t]));function ut(e,s,n,t){if(Oe(e.y)){let i=Ee(e,e.t0,s),c=Ee(e,e.t0,n),a=Ee(e,e.t1,s),u=Ee(e,e.t1,n),E=(v,f)=>B(v.map((d,w)=>{let M=0;for(let y=0;y<d.length;y++)M+=(d[y]-f[w][y])*t[y];return M}));return[E(i,c),E(a,u),E(u,c),E(a,i),E(a,c)]}let r=Re(e,e.t0,s,t),l=Re(e,e.t0,n,t),o=Re(e,e.t1,s,t),m=Re(e,e.t1,n,t);return[ye(r,l),ye(o,m),ye(m,l),ye(o,r),ye(o,l)]}function ke(e,s,n,t){let r=e.n,l=Se(e,e.t1).map(c=>ce(c,s)),o=Se(e,e.t0).map(c=>ce(c,s)),m,i;if(Oe(e.m)){let c=ue(r,{sd:n});m=l.map((a,u)=>a+c[u]),i=o.map((a,u)=>a+c[u])}else m=l.map(c=>pe(1,e.m.linkinv(c))),i=o.map(c=>pe(1,e.m.linkinv(c)));return ut(e,m,i,t)}var Oe=e=>!e.isGlm||e.fit.family==="gaussian",je=e=>e.isGlm?Math.sqrt(zt(e.fit)):e.fit.sigma;function zt(e){let s=0;for(let n=0;n<e.residuals.length;n++)s+=e.weights[n]*e.residuals[n]**2;return s/e.dfResidual}var it=(e,s,n)=>{let t=ae(e,[s,n],{type:7});return[t[0],t[1]]};function Le(e,s,n,t){K(t,Dt,"mediate");let{treat:r,mediator:l}=t;if(typeof r!="string"||typeof l!="string")throw new TypeError("'treat' and 'mediator' are required");let o=t.treatValue??1,m=t.controlValue??0;if(!Number.isFinite(o)||!Number.isFinite(m))throw new RangeError("treatValue and controlValue must be finite");let i=Math.trunc(t.sims??1e3),c=t.boot??!1,a=t.bootCiType??"perc";if(a!=="perc"&&a!=="bca")throw new RangeError('bootCiType must be "perc" or "bca"');let u=t.confLevel??.95;if(!(u>0&&u<1))throw new RangeError("confLevel must be in (0, 1)");let E=t.robustSE??!1;if(!(i>=2))throw new RangeError("sims must be at least 2");let v=[];c&&E&&v.push("'robustSE' is ignored for nonparametric bootstrap");let f=st(e,n,"mediator"),d=st(s,n,"outcome");if(!Oe(f)&&e.family!=="binomial")throw new RangeError("unsupported mediator model");if(f.d.terms.variables[0]!==l)throw new RangeError(`the mediator model's response must be '${l}'`);if(f.rows.length!==d.rows.length||f.rows.some((T,C)=>T!==d.rows[C]))throw new RangeError("number of observations do not match between mediator and outcome models");let w=f.rows.length,M=Object.entries(t.covariates??{});for(let[,T]of M)if(!Number.isFinite(T))throw new RangeError("covariate values must be finite");let y={m:f,y:d,treat:r,mediator:l,t1:o,t0:m,n:w,mTreat:ve(f,r,"mediator",!0),yTreat:ve(d,r,"outcome",!0),yMed:ve(d,l,"outcome",!0),mCov:M.map(([T,C])=>[ve(f,T,"mediator",!1),C]).filter(([T])=>T>=0),yCov:M.map(([T,C])=>[ve(d,T,"outcome",!1),C]).filter(([T])=>T>=0)};if(M.some(([T])=>!f.d.terms.variables.includes(T)&&!d.d.terms.variables.includes(T)))throw new RangeError("covariates must be variables of the models");let h=Oe(f),p,N;if(c){let T=Ve(w,{size:w*i,replace:!0}),C=[...new Set([...f.d.terms.variables,...d.d.terms.variables])],I=h?je(f):NaN;ke(y,f.coef,I,d.coef),p=[];for(let U=0;U<i;U++){let G=Array.from({length:w},(H,j)=>f.rows[T[U+j*i]]),Y=Object.fromEntries(C.map(H=>{let j=n[H];return[H,G.map(R=>j[R])]})),J=ct(f,Y),X=ct(d,Y);p.push(ke(y,mt(J,f,v),h?je({...f,fit:J,isGlm:"family"in J}):NaN,mt(X,d,v)))}N=ke(y,f.coef,I,d.coef)}else{let T=E?re(e,{type:"HC3"}).vcov:ne(e).vcov,C=E?re(s,{type:"HC3"}).vcov:ne(s).vcov,I=at(f.coef,T,i,"mediator"),U=at(d.coef,C,i,"outcome"),G=Se(y,o),Y=Se(y,m),J=Array.from({length:i},()=>new Array(w)),X=Array.from({length:i},()=>new Array(w));if(h){let H=ue(i*w,{sd:je(f)});for(let j=0;j<w;j++)for(let R=0;R<i;R++)J[R][j]=ce(G[j],I[R])+H[j*i+R],X[R][j]=ce(Y[j],I[R])+H[j*i+R]}else{for(let H=0;H<w;H++)for(let j=0;j<i;j++)J[j][H]=pe(1,f.linkinv(ce(G[H],I[j])));for(let H=0;H<w;H++)for(let j=0;j<i;j++)X[j][H]=pe(1,f.linkinv(ce(Y[H],I[j])))}p=I.map((H,j)=>ut(y,J[j],X[j],U[j]))}let b=T=>p.map(C=>C[T]),g=b(0),A=b(1),x=b(2),k=b(3),$=b(4),S=g.map((T,C)=>T/$[C]),_=A.map((T,C)=>T/$[C]),z=g.map((T,C)=>(T+A[C])/2),O=x.map((T,C)=>(T+k[C])/2),F=S.map((T,C)=>(T+_[C])/2),D=(1-u)/2,Q=(1+u)/2,V=(T,C)=>{let I;if(c&&a==="bca"){let Y=B(T),J=0,X=0,H=0;for(let q of T){let W=Y-q;J+=W*W,X+=W*W*W,q<Y&&H++}let j=X/(6*J**1.5),R=fe(H/T.length),L=q=>Z(R+(R+q)/(1-j*(R+q)));I=it(T,L(fe(D)),L(fe(Q)))}else I=it(T,D,Q);let U=0,G=0;for(let Y of T)Y>0?U++:Y<0&&G++;return{estimate:C,ci:I,p:2*Math.min(U/T.length,G/T.length),sims:T}},P;if(N===void 0){let T=Ce(S),C=Ce(_);P=[B(g),B(A),B(x),B(k),B($),T,C,B(z),B(O),(T+C)/2]}else{let[T,C,I,U,G]=N;P=[T,C,I,U,G,T/G,C/G,(T+C)/2,(I+U)/2,(T/G+C/G)/2]}let le=d.d.terms.termLabels.some(T=>{let C=T.split(":");return C.length>1&&C.includes(r)&&C.includes(l)});return ge("mediate",{d0:V(g,P[0]),d1:V(A,P[1]),z0:V(x,P[2]),z1:V(k,P[3]),tau:V($,P[4]),n0:V(S,P[5]),n1:V(_,P[6]),dAvg:V(z,P[7]),zAvg:V(O,P[8]),nAvg:V(F,P[9]),treatValue:o,controlValue:m,nobs:w,sims:i,boot:c,bootCiType:a,confLevel:u,robustSE:E&&!c,INT:le,warnings:v})}function ct(e,s){let n={...e.options};return we(()=>e.isGlm?be(e.formula,s,n):Pe(e.formula,s,n))}var lt='prediction from rank-deficient fit; attr(*, "non-estim") has doubtful cases';function mt(e,s,n){let t="family"in e?e.model.columnNames:e.coefNames;return s.names.map(r=>{let l=t.indexOf(r);return l<0||Number.isNaN(e.coefficients[l])?(n.includes(lt)||n.push(lt),0):e.coefficients[l]})}var Vt=["pred","modx","modxValues","johnsonNeyman","jnAlpha","ciWidth","robust"],It=["pred","modx","alpha","robust"];function dt(e,s,n,t,r){if(typeof n!="string"||typeof t!="string")throw new TypeError("'pred' and 'modx' are required");if(r!==!1&&r!=="HC3")throw new RangeError('robust must be false or "HC3"');let l="family"in e,o=l?e.model.columnNames:e.coefNames;if(o===void 0)throw new TypeError("the model must be a formula fit of lm or glm");if(l&&e.family!=="binomial"&&e.family!=="gaussian")throw new RangeError("the model's family is not supported (binomial or gaussian)");let m=[`${n}:${t}`,`${t}:${n}`].find(d=>o.includes(d));if(!o.includes(n)||m===void 0)throw new RangeError(`the model has no coefficients '${n}' and '${n}:${t}'`);let i=r==="HC3"?re(e,{type:"HC3"}):ne(e),c=i.names.indexOf(n),a=i.names.indexOf(m);if(c<0||a<0)throw new RangeError("the predictor's or the interaction's coefficient is aliased");let u=e.coefficients,E=l?e.model.rows:e.rows,v=s[t];if(v===void 0)throw new RangeError(`object '${t}' not found`);let f=E.map(d=>Number(v[d]));if(f.some(d=>!Number.isFinite(d)))throw new RangeError(`'${t}' must be numeric`);return{b1:u[o.indexOf(n)],b3:u[o.indexOf(m)],v11:i.vcov[c][c],v13:i.vcov[c][a],v33:i.vcov[a][a],df:e.dfResidual,isGlm:l&&e.family==="binomial",w:f}}function ht(e,s){if(!(s>0&&s<1))throw new RangeError("alpha must be in (0, 1)");let n=xe(1-s/2,e.df),t=n*n,r=e.b3*e.b3-t*e.v33,l=2*(e.b1*e.b3-t*e.v13),o=e.b1*e.b1-t*e.v11,m=l*l-4*r*o,i=Math.min(...e.w),c=Math.max(...e.w),a=[i,c],u,E=!1;if(!(m>=0))u=[-1/0,1/0],E=!0;else{let d=Math.sqrt(m),w=(-l-Math.sign(l||1)*d)/(2*r),M=w===0?(-l+Math.sign(l||1)*d)/(2*r):o/(r*w);u=w<=M?[w,M]:[M,w]}let v=!E&&r<0,f=!(u[0]>i&&u[0]<c)&&!(u[1]>i&&u[1]<c);return{bounds:u,inside:v,failed:E,allSig:f,modrange:a,alpha:s}}function Ft(e,s,n){return K(n,It,"johnsonNeyman"),ht(dt(e,s,n.pred,n.modx,n.robust??!1),n.alpha??.05)}function Wt(e,s,n){K(n,Vt,"simSlopes");let t=dt(e,s,n.pred,n.modx,n.robust??!1),r=n.ciWidth??.95;if(!(r>0&&r<1))throw new RangeError("ciWidth must be in (0, 1)");let l=n.modxValues??"mean-plus-minus",o;if(l==="mean-plus-minus"){let v=B(t.w),f=Math.sqrt(Te(t.w));o=[v-f,v,v+f]}else if(Array.isArray(l)&&l.every(v=>Number.isFinite(v)))o=[...l];else throw new RangeError('modxValues must be "mean-plus-minus" or finite numbers');let m=t.isGlm?fe((1+r)/2):xe((1+r)/2,t.df),i=o.map(v=>t.b1+t.b3*v),c=o.map(v=>Math.sqrt(t.v11+2*v*t.v13+v*v*t.v33)),a=i.map((v,f)=>v/c[f]),u=a.map(v=>2*(t.isGlm?Z(-Math.abs(v)):qe(-Math.abs(v),t.df))),E={values:o,estimate:i,se:c,lower:i.map((v,f)=>v-m*c[f]),upper:i.map((v,f)=>v+m*c[f]),statistic:a,statisticName:t.isGlm?"z":"t",p:u};return(n.johnsonNeyman??!0)&&(E.jn=ht(t,n.jnAlpha??.05)),E}var Kt=["treat","mediator"],Gt=["nmc","confLevel","treat","mediator"],Pt=new Set(["moderator","treatValue","controlValue","wLow","wHigh","method","nmc","sims","confLevel","robustSE","treat","mediator"]);function Me(e,s,n){let t=ne(e),r=t.names.indexOf(s);if(r<0)throw new RangeError(`the ${n} model has no coefficient '${s}'`);let l="family"in e?e.model.columnNames:e.coefNames;return{value:e.coefficients[l.indexOf(s)],v:t.vcov[r][r],names:t.names,vcov:t.vcov}}function Ut(e,s,n){K(n,Kt,"sobelTest");let t=Me(e,n.treat,"mediator"),r=Me(s,n.mediator,"outcome"),l=t.value,o=r.value,m=Math.sqrt(t.v),i=Math.sqrt(r.v),c=l*o,a=Math.sqrt(o*o*m*m+l*l*i*i),u=c/a;return{a:l,sa:m,b:o,sb:i,ab:c,se:a,z:u,p:2*Z(Math.abs(u),{lowerTail:!1})}}var wt=(e,s)=>{let n=1-s,t=ae(e,[n/2,1-n/2],{type:7});return[t[0],t[1]]};function bt(e){if(!(e>0&&e<1))throw new RangeError("confLevel must be in (0, 1)")}function Bt(e,s,n){K(n,Gt,"mediationMonteCarlo");let t=Math.trunc(n.nmc??2e4),r=n.confLevel??.95;if(bt(r),!(t>=2))throw new RangeError("nmc must be at least 2");let l=Me(e,n.treat,"mediator"),o=Me(s,n.mediator,"outcome"),m=Me(s,n.treat,"outcome"),i=o.names.indexOf(n.mediator),c=o.names.indexOf(n.treat),a=ue(t,{mean:l.value,sd:Math.sqrt(l.v)}),u=Ae(t,[o.value,m.value],[[o.vcov[i][i],o.vcov[i][c]],[o.vcov[c][i],o.vcov[c][c]]]),E=a.map((h,p)=>h*u[p][0]),v=u.map(h=>h[1]),f=E.map((h,p)=>h+v[p]),d=E.map((h,p)=>h/f[p]),w=l.value*o.value,M=m.value,y=(h,p)=>({estimate:h,ci:wt(p,r),sims:p});return{indirect:y(w,E),direct:y(M,v),total:y(w+M,f),proportion:y(w/(w+M),d)}}function pt(e,s,n){let t=e[n];if(t===void 0)throw new RangeError(`object '${n}' not found`);let r=s.map(i=>Number(t[i]));if(r.some(i=>!Number.isFinite(i)))throw new RangeError(`'${n}' must be numeric`);let l=[...new Set(r)].sort((i,c)=>i-c);if(l.length===2)return[l[0],l[1]];let o=B(r),m=Math.sqrt(Te(r));return[o-m,o+m]}var gt=e=>!("family"in e)||e.family==="gaussian",Yt=e=>"family"in e?e.model.rows:e.rows;function Jt(e,s,n,t){K(t,Pt,"moderatedMediation");let{treat:r,mediator:l,moderator:o}=t;if(typeof r!="string"||typeof l!="string"||typeof o!="string")throw new TypeError("'treat', 'mediator' and 'moderator' are required");let m=t.confLevel??.95;bt(m);let i=Yt(e),[c,a]=pt(n,i,r),[u,E]=pt(n,i,o),v=t.treatValue??a,f=t.controlValue??c,d=t.wLow??u,w=t.wHigh??E,M=t.method??(gt(e)&>(s)?"monteCarlo":"mediate"),y=(R,L)=>({estimate:R,ci:wt(L,m),sims:L}),h={method:M,wLow:d,wHigh:w,treatValue:v,controlValue:f};if(M==="mediate"){let R=He(),L=se=>Le(e,s,n,{treat:r,mediator:l,treatValue:v,controlValue:f,covariates:{[o]:se},sims:t.sims??1e3,confLevel:m,robustSE:t.robustSE??!1}),q=L(d);De(R);let W=L(w),he=W.dAvg.sims.map((se,vt)=>se-q.dAvg.sims[vt]),me=se=>({estimate:se.estimate,ci:se.ci,sims:se.sims});return{...h,indirectLow:me(q.dAvg),indirectHigh:me(W.dAvg),directLow:me(q.zAvg),directHigh:me(W.zAvg),totalLow:me(q.tau),totalHigh:me(W.tau),index:y(W.dAvg.estimate-q.dAvg.estimate,he),low:q,high:W}}if(M!=="monteCarlo")throw new RangeError('method must be "monteCarlo" or "mediate"');let p=Math.trunc(t.nmc??2e4);if(!(p>=2))throw new RangeError("nmc must be at least 2");let N=t.robustSE?re(e,{type:"HC3"}):ne(e),b=t.robustSE?re(s,{type:"HC3"}):ne(s),g=(R,L)=>L.map(q=>{let W="family"in R?R.model.columnNames:R.coefNames;return R.coefficients[W.indexOf(q)]}),A=Ae(p,g(e,N.names),N.vcov),x=Ae(p,g(s,b.names),b.vcov),k=g(e,N.names),$=g(s,b.names),S=(R,L,q)=>{let W=q===void 0?R.indexOf(L):[`${L}:${q}`,`${q}:${L}`].map(he=>R.indexOf(he)).find(he=>he>=0)??-1;if(q===void 0&&W<0)throw new RangeError(`a model has no coefficient '${L}'`);return W},_=S(N.names,r),z=S(N.names,r,o),O=S(b.names,l),F=S(b.names,l,o),D=S(b.names,r),Q=S(b.names,r,o),V=(R,L)=>L<0?0:R[L],P=v-f,le=(R,L,q)=>P*(V(R,_)+V(R,z)*q)*(V(L,O)+V(L,F)*q),T=(R,L)=>P*(V(R,D)+V(R,Q)*L),C=R=>Array.from({length:p},(L,q)=>R(q)),I=C(R=>le(A[R],x[R],d)),U=C(R=>le(A[R],x[R],w)),G=C(R=>T(x[R],d)),Y=C(R=>T(x[R],w)),J=le(k,$,d),X=le(k,$,w),H=T($,d),j=T($,w);return{...h,indirectLow:y(J,I),indirectHigh:y(X,U),directLow:y(H,G),directHigh:y(j,Y),totalLow:y(J+H,I.map((R,L)=>R+G[L])),totalHigh:y(X+j,U.map((R,L)=>R+Y[L])),index:y(X-J,U.map((R,L)=>R-I[L]))}}export{Nt as balTab,Ft as johnsonNeyman,Et as match,Je as matchWeights,Le as mediate,Bt as mediationMonteCarlo,Jt as moderatedMediation,Wt as simSlopes,Ut as sobelTest,Rt as tableOneSmd,qt as trimWeights,Ht as weightSummary,_t as weightit};
|
|
1
|
+
/*! rStats.js 1.18.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as Fe,b as Ne}from"./chunks/chunk-3JFRDXN7.js";import{p as Pe,x as ne,y as re}from"./chunks/chunk-HMLYEFQN.js";import{a as Ie}from"./chunks/chunk-EC77KAAM.js";import{a as ae}from"./chunks/chunk-VATAUY7N.js";import{h as ke}from"./chunks/chunk-QUD3374N.js";import{a as ge}from"./chunks/chunk-KVJBCT3D.js";import{b as Xe}from"./chunks/chunk-XUMVTGMM.js";import{f as Ue,h as ve,j as Be}from"./chunks/chunk-IOJVFYHW.js";import"./chunks/chunk-Z33Q545U.js";import"./chunks/chunk-WYVUCQO3.js";import"./chunks/chunk-CTZUYKVL.js";import"./chunks/chunk-NOTJ45G3.js";import{a as Ge}from"./chunks/chunk-RXYZQ3UX.js";import{d as te}from"./chunks/chunk-H6Z7JVSD.js";import"./chunks/chunk-JPWYGTUN.js";import"./chunks/chunk-IBHMM2SL.js";import{a as Ke}from"./chunks/chunk-GIK34P23.js";import{C as ue,w as He,x as Ce}from"./chunks/chunk-IW4S634O.js";import"./chunks/chunk-LY5XGFPU.js";import"./chunks/chunk-IKMW63BE.js";import{a as Re}from"./chunks/chunk-MXZ64QME.js";import{e as De,f as Ve,g as ze,m as We,n as we,o as be}from"./chunks/chunk-N7BUE54L.js";import"./chunks/chunk-XQCHC3SH.js";import"./chunks/chunk-KJMLQHCY.js";import"./chunks/chunk-YXMHWS5R.js";import"./chunks/chunk-53WQDB3C.js";import{r as qe,s as Z,t as fe}from"./chunks/chunk-GEHMND3C.js";import{h as ee,k as B,l as Te}from"./chunks/chunk-HG4LJBNV.js";import{l as K}from"./chunks/chunk-MVPHISOV.js";function Ye(e){let s=0;for(let n of e)Math.abs(n)>s&&(s=Math.abs(n));return s>0&&Number.isFinite(s)?Math.floor(Math.log2(s)):0}function ie(e){let s=Ye(e),n=e.map(o=>ee(o,-s)),t=n[0]??0,r=n.map(o=>o-t),m=Ye(r);return{values:r.map(o=>ee(o,-m)),unitExp:s+m}}var Et=["estimand","M","caliper"];function Mt(e,s,n={}){K(n,Et,"match");let{estimand:t="ATT",M:r=1,caliper:m}=n,o=e.length;if(s.length!==o)throw new RangeError("length(Tr) != nrow(X)");if(t!=="ATT"&&t!=="ATE"&&t!=="ATC")throw new RangeError(`invalid 'estimand': ${String(t)}`);if(!Number.isInteger(r)||r<1)throw new RangeError("'M' must be a positive integer");if(m!==void 0&&!(m>0&&Number.isFinite(m)))throw new RangeError("'caliper' must be a positive number");let l=[];for(let g=0;g<o;g++){let A=e[g];if(A===!0||A===1)l.push(!0);else if(A===!1||A===0)l.push(!1);else throw typeof A=="number"&&Number.isNaN(A)?new RangeError("Match(): input includes NAs"):new RangeError("Treatment indicator ('Tr') must be a logical variable---i.e., TRUE (1) or FALSE (0)");if(!Number.isFinite(s[g]))throw new RangeError("Match(): input includes NAs")}if(!l.includes(!0)||!l.includes(!1))throw new RangeError("Treatment indicator ('Tr') must contain both treatment and control observations");let i=0;for(let g of s)i=Math.max(i,Math.abs(g));let c=i>0?Math.floor(Math.log2(i)):0,a=s.map(g=>ee(g,-c)),u=null,E=1/0;if(m!==void 0){let{values:g,unitExp:A}=ie(a),S=0;for(let O of g)S+=O;S/=o;let C=0;for(let O of g)C+=(O-S)*(O-S);let $=ee(Math.sqrt(C/o),A);E=m*$,u=ee(E,c)}let b=[new Array(o).fill(!1),new Array(o).fill(!1)],f=[],d=[],y=[],M=[],v=0,p=0;for(let g=0;g<o;g++){let A=l[g];if(A?t==="ATC":t==="ATT")continue;let S=b[A?0:1],C=[];for(let x=0;x<o;x++){if(l[x]===A)continue;let F=Math.abs(a[g]-a[x]);F>E||S[x]||C.push({j:x,d:F})}if(C.length<r){p++,y.push(g+1);continue}let O=C.map(x=>x.d).sort((x,F)=>x-F)[r-1],j=C.filter(x=>x.d<=O).map(x=>x.j),V;if(j.length===r)V=j;else{M.push(j.length),V=[],j=j.slice();for(let x=0;x<r;x++){let F=j.length,D=Math.min(F-1,Math.floor(F*(1-ze())));V.push(j[D]),j.splice(D,1)}V.sort((x,F)=>x-F)}for(let x of V)S[x]=!0,f.push((A?g:x)+1),d.push((A?x:g)+1);v++}let h=new Set([...f,...d]),T=u===null?[]:y.filter(g=>!h.has(g)),w=[];return f.length===0&&w.push("'Match' object contains no valid matches (probably because of the caliper or the exact option)."),{indexTreated:f,indexControl:d,weights:f.map(()=>1/r),indexDropped:T.length>0?T:null,caliper:m??null,ecaliper:u,nobs:o,wnobs:v,ndrops:p,estimand:t,M:r,tieSizes:M,warnings:w}}var At=["match","weightit","weights","estimand","levels","binary"],Tt=["factor"];function Je(e){let s=new Array(e.nobs).fill(0);return e.indexTreated.forEach((n,t)=>{s[n-1]+=e.weights[t],s[e.indexControl[t]-1]+=e.weights[t]}),s}function Nt(e,s){if(e===!0||e===1)return!0;if(e===!1||e===0)return!1;throw new RangeError(`'tr' must be 0 or 1 (element ${s+1})`)}function he(e,s,n,t){let r=0,m=0,o=0;for(let c=0;c<e.length;c++){if(n[c]!==t)continue;let a=s===null?1:s[c];r+=a,m+=a*a,o+=a*e[c]}let l=o/r,i=0;for(let c=0;c<e.length;c++){if(n[c]!==t)continue;let a=s===null?1:s[c];i+=a*(e[c]-l)*(e[c]-l)}return[l,i/(r-m/r)]}function Rt(e,s,n={}){K(n,At,"balTab");let{match:t,weightit:r,binary:m="raw",levels:o={}}=n;if(t!==void 0&&r!==void 0)throw new RangeError("give 'match' or 'weightit', not both");let l=t!==void 0?Je(t):r!==void 0?r.weights:n.weights,i=t?.estimand??r?.estimand??n.estimand??"ATT",c=new Set(t?.indexDropped??[]),a=t!==void 0&&t.caliper!==null,u=e.length,E=e.map(Nt);if(!E.includes(!0)||!E.includes(!1))throw new RangeError("'tr' must contain both groups");if(l!==void 0&&l.length!==u)throw new RangeError("'weights' must have the length of 'tr'");if(i==="ATO"&&l===void 0)throw new RangeError("estimand 'ATO' needs the weights");let b=[];r?.ps&&b.push(["prop.score",r.ps,"Distance"]);for(let[M,v]of Object.entries(s)){if(v.length!==u)throw new RangeError(`covariate '${M}' must have the length of 'tr'`);if(v.every(h=>typeof h=="number")){b.push([M,v,null]);continue}if(!v.every(h=>typeof h=="string"))throw new TypeError(`covariate '${M}' must be all numbers or all strings`);let p=o[M]??[...new Set(v)].sort(Re);if(v.some(h=>!p.includes(h)))throw new RangeError(`covariate '${M}' has values not among its levels`);for(let h of p.length===2?p.slice(1):p)b.push([`${M}_${h}`,v.map(T=>T===h?1:0),null])}let f=E.map(()=>!0),d=r!==void 0,y={rows:b.map(([M])=>M),type:[],diffUn:[],diffAdj:[],vRatioUn:[],vRatioAdj:[],observations:{rows:d?["Unadjusted","Adjusted"]:["All (ESS)","All (Unweighted)","Matched (ESS)","Matched (Unweighted)","Unmatched",...a?["Discarded"]:[]],control:[],treated:[]}};for(let[M,v,p]of b){if(v.some(D=>!Number.isFinite(D)))throw new RangeError(`covariate '${M}' has missing or infinite values`);let h=p===null&&new Set(v).size<=2&&v.every(D=>D===0||D===1),T=h?null:ie(v),w=T===null?v:T.values,[g,A]=he(w,null,E,!0),[S,C]=he(w,null,E,!1),$;if(i==="ATO"){let[D,Q]=he(w,l,f,!0);$=Math.sqrt(h?D*(1-D):Q)}else{let D=h?[g*(1-g),S*(1-S)]:[A,C];if($=Math.sqrt(i==="ATT"?D[0]:i==="ATC"?D[1]:(D[0]+D[1])/2),ee($,T?.unitExp??0)<Math.sqrt(Number.EPSILON)){let[Q,z]=he(w,null,f,!0);$=Math.sqrt(h?Q*(1-Q):z)}}let O=h&&m==="raw"?1:$;if(y.type.push(p??(h?"Binary":"Contin.")),y.diffUn.push(g===S?0:(g-S)/O),y.vRatioUn.push(h||C===0?NaN:A/C),l===void 0){y.diffAdj.push(NaN),y.vRatioAdj.push(NaN);continue}let[j,V]=he(w,l,E,!0),[x,F]=he(w,l,E,!1);y.diffAdj.push(j===x?0:(j-x)/O),y.vRatioAdj.push(h||F===0?NaN:V/F)}for(let M of[!1,!0]){let v=0,p=0,h=0,T=0,w=0;for(let S=0;S<u;S++){if(E[S]!==M||(v++,c.has(S+1)&&w++,l===void 0))continue;let C=l[S];p+=C,h+=C*C,C>0&&T++}let g=M?y.observations.treated:y.observations.control;if(d){g.push(v,p*p/h);continue}let A=l===void 0?[NaN,NaN,NaN]:[p*p/h,T,v-T-w];a&&A.push(w),g.push(v,v,...A)}return y}function St(e,s,n={}){K(n,Tt,"tableOneSmd");let{factor:t=!1}=n;if(e.length!==s.length)throw new RangeError("'x' and 'group' must have the same length");let r=[...new Set(s)].sort((f,d)=>f<d?-1:f>d?1:0);if(r.length!==2)throw new RangeError("'group' must have exactly two levels");let m=f=>f!==null&&!(typeof f=="number"&&Number.isNaN(f)),o=r.map(f=>e.filter((d,y)=>s[y]===f&&m(d)));if(!t){if(o.flat().some(p=>typeof p!="number"))throw new TypeError("'x' must be numeric unless factor: true");let f=ie(o.flat()).values,y=[f.slice(0,o[0].length),f.slice(o[0].length)].map(p=>{let h=p.reduce((T,w)=>T+w,0)/p.length;return[h,p.reduce((T,w)=>T+(w-h)*(w-h),0)/(p.length-1)]}),M=y[1][0]-y[0][0],v=Math.sqrt((y[0][1]+y[1][1])/2);return M===0?0:Math.abs(M)/v}let l=[...new Set(o.flat())].sort((f,d)=>typeof f=="number"&&typeof d=="number"?f-d:String(f)<String(d)?-1:String(f)>String(d)?1:0),i=o.map(f=>l.slice(1).map(d=>f.filter(y=>y===d).length/f.length)),c=i[1].map((f,d)=>f-i[0][d]);if(c.every(f=>f===0))return 0;let a=c.length,u=Array.from({length:a},(f,d)=>Array.from({length:a},(y,M)=>{let v=p=>d===M?p[d]*(1-p[d]):-p[d]*p[M];return(v(i[0])+v(i[1]))/2}));if(u.every(f=>f.every(d=>d===0)))return NaN;let E=Xe(u),b=0;for(let f=0;f<a;f++)for(let d=0;d<a;d++)b+=c[f]*E[f][d]*c[d];return Math.sqrt(b)}var Ot=["method","link","estimand","stabilize"],xt=["at","lower","treat"],oe=Number.EPSILON;function Ct(e){return e.map((s,n)=>{if(s===!0||s===1)return 1;if(s===!1||s===0)return 0;throw new RangeError(`'tr' must be 0 or 1 (element ${n+1})`)})}function kt(e,s){let n=[],t=[],r={};for(let[m,o]of Object.entries(e)){if(o.length!==s)throw new RangeError(`covariate '${m}' must have the length of 'tr'`);if(o.every(c=>typeof c=="number")){let c=o;if(c.some(a=>!Number.isFinite(a)))throw new RangeError(`covariate '${m}' has missing or infinite values`);n.push(m),t.push(c.slice());continue}if(!o.every(c=>typeof c=="string"))throw new TypeError(`covariate '${m}' must be all numbers or all strings`);let l=o,i=[...new Set(l)].sort(Re);r[m]=i;for(let c of i.slice(1))n.push(m+c),t.push(l.map(a=>a===c?1:0))}return{names:n,columns:t,factors:r}}function Qe(e){return e.map(s=>{let n=s.reduce((r,m)=>r+m,0)/s.length,t=s.reduce((r,m)=>Math.max(r,Math.abs(m-n)),0);return s.map(r=>t>0?(r-n)/t:0)})}function $t(e){let s=e[0]?.length??0,n=[new Array(s).fill(1/Math.sqrt(s))],t=[];for(let r of e){let m=Math.sqrt(r.reduce((i,c)=>i+c*c,0)),o=r.slice();for(let i=0;i<2;i++)for(let c of n){let a=o.reduce((u,E,b)=>u+E*c[b],0);for(let u=0;u<s;u++)o[u]-=a*c[u]}let l=Math.sqrt(o.reduce((i,c)=>i+c*c,0));l>1e-7*m&&(n.push(o.map(i=>i/l)),t.push(r))}return t}function st(e,s){let n=s.length,t=e.map((o,l)=>[...o,s[l]]),r=0;for(let o of e)for(let l of o)r=Math.max(r,Math.abs(l));for(let o=0;o<n;o++){let l=o;for(let i=o+1;i<n;i++)Math.abs(t[i][o])>Math.abs(t[l][o])&&(l=i);if(!(Math.abs(t[l][o])>1e-13*r))return null;[t[o],t[l]]=[t[l],t[o]];for(let i=o+1;i<n;i++){let c=t[i][o]/t[o][o];if(c!==0)for(let a=o;a<=n;a++)t[i][a]-=c*t[o][a]}}let m=new Array(n).fill(0);for(let o=n-1;o>=0;o--){let l=t[o][n];for(let i=o+1;i<n;i++)l-=t[o][i]*m[i];m[o]=l/t[o][o]}return m}function $e(e,s){return Array.from({length:s},(n,t)=>[1,...e.map(r=>r[t])])}function Ze(e,s,n){let t=n.length,r=new Array(t).fill(0);r[0]=Math.log(n[0]/s.length);let m=c=>{let a=0;for(let u of s)a+=Math.exp(e[u].reduce((E,b,f)=>E+b*c[f],0));return a-n.reduce((u,E,b)=>u+E*c[b],0)},o=m(r),l=!1,i=1e-13*Math.max(1,Math.abs(n[0]));for(let c=0;c<200;c++){let a=n.map(p=>-p),u=Array.from({length:t},()=>new Array(t).fill(0));for(let p of s){let h=e[p],T=Math.exp(h.reduce((w,g,A)=>w+g*r[A],0));for(let w=0;w<t;w++){a[w]+=T*h[w];for(let g=0;g<=w;g++)u[w][g]+=T*h[w]*h[g]}}for(let p=0;p<t;p++)for(let h=p+1;h<t;h++)u[p][h]=u[h][p];let E=Math.max(...a.map(Math.abs));if(E<=i){l=!0;break}let b=st(u,a.map(p=>-p));if(b===null||b.some(p=>!Number.isFinite(p)))break;let f=1e-12*(Math.abs(o)+1),d=1,y=r.map((p,h)=>p+b[h]),M=m(y);for(;!(M<=o+f)&&d>1e-10;)d/=2,y=r.map((p,h)=>p+d*b[h]),M=m(y);if(!(M<=o+f))break;let v=b.some((p,h)=>Math.abs(d*p)>1e-15*Math.max(1,Math.abs(r[h])));if(r.splice(0,t,...y),o=M,!v){l=E<=1e-9*Math.max(1,Math.abs(n[0]));break}}return{eta:e.map(c=>c.reduce((a,u,E)=>a+u*r[E],0)),converged:l}}var Lt={logit:{p:e=>te(e),q:e=>te(-e),d:e=>te(e)*te(-e)},probit:{p:e=>Z(e),q:e=>Z(-e),d:e=>qe(e)}};function _t(e,s,n,t,r){let m=s.length,o=r.length,l=r.slice(),i=d=>{let y=new Array(o).fill(0),M=Array.from({length:o},()=>new Array(o).fill(0));for(let v=0;v<m;v++){let p=e[v],h=p.reduce(($,O,j)=>$+O*d[j],0),T=t.p(h),w=t.q(h),g=t.d(h),A=s[v],S,C;n==="ATE"?(S=A?1/T:-1/w,C=A?-g/(T*T):-g/(w*w)):n==="ATT"?(S=A?1:-T/w,C=A?0:-g/(w*w)):n==="ATC"?(S=A?w/T:-1,C=A?-g/(T*T):0):(S=A?w:-T,C=-g);for(let $=0;$<o;$++){y[$]+=S*p[$];for(let O=0;O<o;O++)M[$][O]+=C*p[$]*p[O]}}return{g:y,J:M}},c=d=>d.reduce((y,M)=>y+M*M,0),{g:a,J:u}=i(l),E=c(a),b=!1,f=1e-12*m;for(let d=0;d<200;d++){if(Math.max(...a.map(Math.abs))<=f){b=!0;break}let y=st(u,a.map(T=>-T));if(y===null||y.some(T=>!Number.isFinite(T)))break;let M=1,v=l.map((T,w)=>T+y[w]),p=i(v),h=c(p.g);for(;!(h<E)&&M>1e-10;)M/=2,v=l.map((T,w)=>T+M*y[w]),p=i(v),h=c(p.g);if(!(h<E)){b=Math.max(...a.map(Math.abs))<=1e-8*m;break}l=v,a=p.g,u=p.J,E=h}return{eta:e.map(d=>d.reduce((y,M,v)=>y+M*l[v],0)),converged:b}}function et(e,s,n,t){switch(e){case"ATE":return s?1/n:1/t;case"ATT":return s?1:n/t;case"ATC":return s?t/n:1;default:return s?t:n}}var jt={glm:"glm.fit: algorithm did not converge",cbps:"The covariate balancing conditions could not be solved, so the covariates will not be exactly balanced.",ipt:"The optimization failed to converge; consider using fewer covariates or a different link function.",ebal:"The estimated weights do not balance the covariates, indicating the optimization arrived at a degenerate solution. Try decreasing the number of variables supplied to the optimization."},tt={cbps:"covariate balancing propensity score weighting",ipt:"inverse probability tilting",ebal:"entropy balancing"};function qt(e,s,n={}){K(n,Ot,"weightit");let{method:t="glm",estimand:r="ATE",stabilize:m=!1}=n,o=t==="ps"?"glm":t;if(!["glm","cbps","ipt","ebal"].includes(o))throw new RangeError(`unknown method '${String(t)}'`);if(!["ATE","ATT","ATC","ATO"].includes(r))throw new RangeError(`unknown estimand '${String(r)}'`);let l=o==="ebal"?null:n.link??"logit";if(l!==null&&!{glm:["logit","probit","cauchit","cloglog"],cbps:["logit","probit"],ipt:["logit"]}[o].includes(l))throw new RangeError(`link '${l}' is not available for method '${o}'`);if((o==="ipt"||o==="ebal")&&r==="ATO")throw new RangeError(`"ATO" is not an allowable estimand for ${tt[o]} with a binary treatment. Only "ATE", "ATT", and "ATC" are allowed.`);let c=[];if(m&&o==="glm"&&r!=="ATE")throw new RangeError('`stabilize` can only be supplied when `estimand = "ATE"`.');m&&o!=="glm"&&c.push(`\`stabilize\` cannot be used with ${tt[o]} and will be ignored.`);let a=Ct(e),u=a.length;if(!a.includes(0)||!a.includes(1))throw new RangeError("The treatment must have at least two unique values.");let{names:E,columns:b}=kt(s,u);if(b.length===0)throw new RangeError("'covariates' must have at least one column");let f=$t(b.map(w=>ie(w).values)),d=a.flatMap((w,g)=>w?[g]:[]),y=a.flatMap((w,g)=>w?[]:[g]),M=a.map((w,g)=>g),v=r==="ATT"?1:r==="ATC"?0:null,p=null,h,T=!0;if(o==="glm"){let w=be(()=>ve($e(f,u).map(g=>g.slice(1)),a,{family:"binomial",link:l}));T=w.converged,p=w.fittedValues.map((g,A)=>l==="logit"&&Math.abs(w.linearPredictors[A])>30?w.linearPredictors[A]>0?1-oe:oe:Math.min(Math.max(g,oe),1-oe)),h=a.map((g,A)=>et(r,g,p[A],1-p[A])),p.some(g=>g<=10*oe||g>=1-10*oe)&&c.push("Propensity scores numerically equal to 0 or 1 were estimated, indicating perfect separation. These may yield problems with inference. See `?method_glm` (`?WeightIt::method_glm()`) for details.")}else if(o==="cbps"){let w=Qe(f),g=$e(w,u),A=be(()=>ve(g.map(O=>O.slice(1)),a,{family:"binomial",link:l})).coefficients.map(O=>Number.isFinite(O)?O:0),S=Lt[l],C=_t(g,a,r,S,A);T=C.converged;let $=O=>Math.min(Math.max(O,oe),1-oe);p=C.eta.map(O=>$(S.p(O))),h=a.map((O,j)=>et(r,O,p[j],$(S.q(C.eta[j]))))}else{let w=Qe(f),g=$e(w,u),A=j=>g[0].map((V,x)=>j.reduce((F,D)=>F+g[D][x],0)),S={C:r!=="ATC",T:r!=="ATT"},C=o==="ebal"&&r==="ATE"?{C:A(M),T:A(M)}:{C:A(d),T:A(y)},$=S.C?Ze(g,y,C.C):null,O=S.T?Ze(g,d,C.T):null;if(T=($?.converged??!0)&&(O?.converged??!0),o==="ebal")h=a.map((j,V)=>j?O?Math.exp(O.eta[V]):1:$?Math.exp($.eta[V]):1);else{let j=r==="ATE"?1:0;h=a.map((V,x)=>V?O?j+Math.exp(O.eta[x]):1:$?j+Math.exp($.eta[x]):1),p=a.map((V,x)=>r==="ATT"?te($.eta[x]):r==="ATC"?te(-O.eta[x]):V?te(-O.eta[x]):te($.eta[x]))}}if(T||c.push(jt[o]),m&&o==="glm"){let w=[y.length/u,d.length/u];h=h.map((g,A)=>g*w[a[A]])}return h.every(w=>Math.abs(w-1)<Math.sqrt(oe))&&c.push("All weights are 1, possibly indicating an estimation failure."),we("weightit",{weights:h,ps:p,treat:a,estimand:r,method:o,link:l,stabilized:m&&o==="glm",focal:v,columnNames:E,converged:T,warnings:c})}function nt(e,s,n,t){let r=e.slice();if(s.length===0)return r;let m=s.map(i=>e[i]),o,l;if(n<1){let i=n<.5?1-n:n;o=ae(m,i,{type:3}),l=ae(m,1-i,{type:3})}else{if(!Number.isInteger(n))throw new RangeError("'at' must be below 1 or a whole number");if(n>=m.length)return null;let i=n>m.length/2?m.length-n:n,c=m.slice().sort((a,u)=>a-u);o=c[c.length-1-i],l=c[i]}for(let i of s)e[i]>o&&(r[i]=o),t&&e[i]<l&&(r[i]=l);return r}var ot=e=>`\`at\` must be less than ${e}, the number of units. Weights will not be trimmed.`;function Ht(e,s){K(s,xt,"trimWeights");let{at:n,lower:t=!1}=s;if(!(n>0)||!Number.isFinite(n))throw new RangeError("'at' must be a positive number");if(Array.isArray(e)||ArrayBuffer.isView(e)){let l=Array.from(e);if(s.treat!==void 0&&s.treat.length!==l.length)throw new RangeError("'treat' must have the length of 'w'");let i=nt(l,l.map((c,a)=>a),n,t);return i===null?(We("trimWeights",[ot(l.length)]),l):i}let r=e,m=r.treat.flatMap((l,i)=>r.focal===null||l!==r.focal?[i]:[]),o=nt(r.weights,m,n,t);return o===null?we("trimWeights",{...r,weights:r.weights.slice(),warnings:[ot(m.length)]}):{...r,weights:o,warnings:r.warnings.slice()}}function rt(e,s){let n=s.map(a=>e[a]),t=n.length,r=0,m=0;for(let a of n)r+=a,m+=a*a;let o=r/t,l=n.reduce((a,u)=>a+(u-o)*(u-o),0),i=n.map(a=>o>0?a/o:NaN),c=n.map((a,u)=>u).sort((a,u)=>n[u]-n[a]||a-u).slice(0,5).reverse();return{range:[Math.min(...n),Math.max(...n)],top:{index:c.map(a=>s[a]+1),weights:c.map(a=>n[a])},mean:o,coefOfVar:Math.sqrt(l/(t-1))/o,scaledMad:i.reduce((a,u)=>a+Math.abs(u-1),0)/t,negativeEntropy:i.reduce((a,u)=>a+(Number.isNaN(u)?NaN:u>0?u*Math.log(u):0),0)/t,numZeros:n.filter(a=>Math.abs(a)<1e-10).length,n:t,ess:r*r/m}}function Dt(e){let s=e.treat.flatMap((t,r)=>t?[r]:[]),n=e.treat.flatMap((t,r)=>t?[]:[r]);return{treated:rt(e.weights,s),control:rt(e.weights,n),stabilized:e.stabilized}}var Vt=new Set(["treat","mediator","treatValue","controlValue","covariates","sims","boot","bootCiType","confLevel","robustSE"]);function at(e,s,n){let t=Ue(e);if(t===void 0||typeof e.formula!="string")throw new TypeError(`the ${n} model must be a formula fit of lm or glm`);if(t.weights!==void 0||t.offset!==void 0)throw new RangeError(`weights and offsets are not supported (the ${n} model)`);let r="family"in e,m=a=>a;if(r){let a=e;if(a.family==="binomial"&&(a.link==="logit"||a.link==="probit"))m=Be(a).linkinv;else if(!(a.family==="gaussian"&&a.link==="identity"))throw new RangeError(`the ${n} model's family is not supported (binomial with logit or probit, or gaussian)`)}if(e.coefficients.some(a=>Number.isNaN(a)))throw new RangeError(`the ${n} model has aliased coefficients`);let o=e.formula,l=Ge(Ke(o,!1,!1,!0),s,{...t.contrasts===void 0?{}:{contrasts:t.contrasts},...t.levels===void 0?{}:{levels:t.levels},dropUnusedLevels:!0,anyResponse:!0}),i=r?e.model.rows:e.rows,c=r?e.model.columnNames:e.coefNames;if(l.frame.rows.length!==i.length||l.frame.rows.some((a,u)=>a!==i[u])||l.colNames.join("\0")!==c.join("\0"))throw new RangeError(`the data do not give the ${n} model's rows and columns (pass the data it was fitted on)`);return{fit:e,isGlm:r,formula:o,options:t,coef:e.coefficients.slice(),names:c,linkinv:m,d:l,rows:i}}function ye(e,s,n,t){let r=e.d.terms.variables.indexOf(s);if(r<0){if(t)throw new RangeError(`'${s}' is not a variable of the ${n} model`);return-1}if(e.d.frame.variables[r].kind!=="numeric")throw new RangeError(`'${s}' must be numeric in the ${n} model`);return r}function ut(e,s,n){let t=e.d.frame.variables.map((r,m)=>{let o=s.get(m);return o===void 0?r:{kind:"numeric",values:typeof o=="number"?new Array(n).fill(o):o.slice()}});return e.d.rowsOf(t,n)}var ce=(e,s)=>{let n=0;for(let t=0;t<e.length;t++)n+=e[t]*s[t];return n};function it(e,s,n,t){let r=e.length,m=2**-1022,o=Number.MAX_VALUE;if(s.some((a,u)=>a.some(E=>!Number.isFinite(E))||!(a[u]>=m&&a[u]<=o)))throw new RangeError(`the ${t} model's coefficient covariance is out of the double range (rescale the variables, or use boot: true)`);let l=Fe(s),i=ue(n*r),c=[];for(let a=0;a<n;a++){let u=[];for(let E=0;E<r;E++){let b=0;for(let f=0;f<r;f++)b+=i[a*r+f]*l[f][E];u.push(e[E]+b)}c.push(u)}return c}function Oe(e,s){let n=new Map(e.mCov);return n.set(e.mTreat,s),ut(e.m,n,e.n)}function Me(e,s,n){let t=new Map(e.yCov);return t.set(e.yTreat,s),t.set(e.yMed,n),ut(e.y,t,e.n)}function Se(e,s,n,t){return Me(e,s,n).map(r=>e.y.linkinv(ce(r,t)))}var Ee=(e,s)=>B(e.map((n,t)=>n-s[t]));function ht(e,s,n,t){if(xe(e.y)){let i=Me(e,e.t0,s),c=Me(e,e.t0,n),a=Me(e,e.t1,s),u=Me(e,e.t1,n),E=(b,f)=>B(b.map((d,y)=>{let M=0;for(let v=0;v<d.length;v++)M+=(d[v]-f[y][v])*t[v];return M}));return[E(i,c),E(a,u),E(u,c),E(a,i),E(a,c)]}let r=Se(e,e.t0,s,t),m=Se(e,e.t0,n,t),o=Se(e,e.t1,s,t),l=Se(e,e.t1,n,t);return[Ee(r,m),Ee(o,l),Ee(l,m),Ee(o,r),Ee(o,m)]}function Le(e,s,n,t){let r=e.n,m=Oe(e,e.t1).map(c=>ce(c,s)),o=Oe(e,e.t0).map(c=>ce(c,s)),l,i;if(xe(e.m)){let c=ue(r,{sd:n});l=m.map((a,u)=>a+c[u]),i=o.map((a,u)=>a+c[u])}else l=m.map(c=>ge(1,e.m.linkinv(c))),i=o.map(c=>ge(1,e.m.linkinv(c)));return ht(e,l,i,t)}var xe=e=>!e.isGlm||e.fit.family==="gaussian",_e=e=>e.isGlm?Math.sqrt(zt(e.fit)):e.fit.sigma;function zt(e){let s=0;for(let n=0;n<e.residuals.length;n++)s+=e.weights[n]*e.residuals[n]**2;return s/e.dfResidual}var ct=(e,s,n)=>{let t=ae(e,[s,n],{type:7});return[t[0],t[1]]};function je(e,s,n,t){K(t,Vt,"mediate");let{treat:r,mediator:m}=t;if(typeof r!="string"||typeof m!="string")throw new TypeError("'treat' and 'mediator' are required");let o=t.treatValue??1,l=t.controlValue??0;if(!Number.isFinite(o)||!Number.isFinite(l))throw new RangeError("treatValue and controlValue must be finite");let i=Math.trunc(t.sims??1e3),c=t.boot??!1,a=t.bootCiType??"perc";if(a!=="perc"&&a!=="bca")throw new RangeError('bootCiType must be "perc" or "bca"');let u=t.confLevel??.95;if(!(u>0&&u<1))throw new RangeError("confLevel must be in (0, 1)");let E=t.robustSE??!1;if(!(i>=2))throw new RangeError("sims must be at least 2");let b=[];c&&E&&b.push("'robustSE' is ignored for nonparametric bootstrap");let f=at(e,n,"mediator"),d=at(s,n,"outcome");if(!xe(f)&&e.family!=="binomial")throw new RangeError("unsupported mediator model");if(f.d.terms.variables[0]!==m)throw new RangeError(`the mediator model's response must be '${m}'`);if(f.rows.length!==d.rows.length||f.rows.some((N,k)=>N!==d.rows[k]))throw new RangeError("number of observations do not match between mediator and outcome models");let y=f.rows.length,M=Object.entries(t.covariates??{});for(let[,N]of M)if(!Number.isFinite(N))throw new RangeError("covariate values must be finite");let v={m:f,y:d,treat:r,mediator:m,t1:o,t0:l,n:y,mTreat:ye(f,r,"mediator",!0),yTreat:ye(d,r,"outcome",!0),yMed:ye(d,m,"outcome",!0),mCov:M.map(([N,k])=>[ye(f,N,"mediator",!1),k]).filter(([N])=>N>=0),yCov:M.map(([N,k])=>[ye(d,N,"outcome",!1),k]).filter(([N])=>N>=0)};if(M.some(([N])=>!f.d.terms.variables.includes(N)&&!d.d.terms.variables.includes(N)))throw new RangeError("covariates must be variables of the models");let p=xe(f),h,T;if(c){let N=Ie(y,{size:y*i,replace:!0}),k=[...new Set([...f.d.terms.variables,...d.d.terms.variables])],I=p?_e(f):NaN;Le(v,f.coef,I,d.coef),h=[];for(let P=0;P<i;P++){let G=Array.from({length:y},(H,L)=>f.rows[N[P+L*i]]),X=Object.fromEntries(k.map(H=>{let L=n[H];return[H,G.map(R=>L[R])]})),Y=lt(f,X),J=lt(d,X);h.push(Le(v,ft(Y,f,b),p?_e({...f,fit:Y,isGlm:"family"in Y}):NaN,ft(J,d,b)))}T=Le(v,f.coef,I,d.coef)}else{let N=E?re(e,{type:"HC3"}).vcov:ne(e).vcov,k=E?re(s,{type:"HC3"}).vcov:ne(s).vcov,I=it(f.coef,N,i,"mediator"),P=it(d.coef,k,i,"outcome"),G=Oe(v,o),X=Oe(v,l),Y=Array.from({length:i},()=>new Array(y)),J=Array.from({length:i},()=>new Array(y));if(p){let H=ue(i*y,{sd:_e(f)});for(let L=0;L<y;L++)for(let R=0;R<i;R++)Y[R][L]=ce(G[L],I[R])+H[L*i+R],J[R][L]=ce(X[L],I[R])+H[L*i+R]}else{for(let H=0;H<y;H++)for(let L=0;L<i;L++)Y[L][H]=ge(1,f.linkinv(ce(G[H],I[L])));for(let H=0;H<y;H++)for(let L=0;L<i;L++)J[L][H]=ge(1,f.linkinv(ce(X[H],I[L])))}h=I.map((H,L)=>ht(v,Y[L],J[L],P[L]))}let w=N=>h.map(k=>k[N]),g=w(0),A=w(1),S=w(2),C=w(3),$=w(4),O=g.map((N,k)=>N/$[k]),j=A.map((N,k)=>N/$[k]),V=g.map((N,k)=>(N+A[k])/2),x=S.map((N,k)=>(N+C[k])/2),F=O.map((N,k)=>(N+j[k])/2),D=(1-u)/2,Q=(1+u)/2,z=(N,k)=>{let I;if(c&&a==="bca"){let X=B(N),Y=0,J=0,H=0;for(let q of N){let W=X-q;Y+=W*W,J+=W*W*W,q<X&&H++}let L=J/(6*Y**1.5),R=fe(H/N.length),_=q=>Z(R+(R+q)/(1-L*(R+q)));I=ct(N,_(fe(D)),_(fe(Q)))}else I=ct(N,D,Q);let P=0,G=0;for(let X of N)X>0?P++:X<0&&G++;return{estimate:k,ci:I,p:2*Math.min(P/N.length,G/N.length),sims:N}},U;if(T===void 0){let N=ke(O),k=ke(j);U=[B(g),B(A),B(S),B(C),B($),N,k,B(V),B(x),(N+k)/2]}else{let[N,k,I,P,G]=T;U=[N,k,I,P,G,N/G,k/G,(N+k)/2,(I+P)/2,(N/G+k/G)/2]}let le=d.d.terms.termLabels.some(N=>{let k=N.split(":");return k.length>1&&k.includes(r)&&k.includes(m)});return we("mediate",{d0:z(g,U[0]),d1:z(A,U[1]),z0:z(S,U[2]),z1:z(C,U[3]),tau:z($,U[4]),n0:z(O,U[5]),n1:z(j,U[6]),dAvg:z(V,U[7]),zAvg:z(x,U[8]),nAvg:z(F,U[9]),treatValue:o,controlValue:l,nobs:y,sims:i,boot:c,bootCiType:a,confLevel:u,robustSE:E&&!c,INT:le,warnings:b})}function lt(e,s){let n={...e.options};return be(()=>e.isGlm?ve(e.formula,s,n):Pe(e.formula,s,n))}var mt='prediction from rank-deficient fit; attr(*, "non-estim") has doubtful cases';function ft(e,s,n){let t="family"in e?e.model.columnNames:e.coefNames;return s.names.map(r=>{let m=t.indexOf(r);return m<0||Number.isNaN(e.coefficients[m])?(n.includes(mt)||n.push(mt),0):e.coefficients[m]})}var It=["pred","modx","modxValues","johnsonNeyman","jnAlpha","ciWidth","robust"],Ft=["pred","modx","alpha","robust"];function dt(e,s,n,t,r){if(typeof n!="string"||typeof t!="string")throw new TypeError("'pred' and 'modx' are required");if(r!==!1&&r!=="HC3")throw new RangeError('robust must be false or "HC3"');let m="family"in e,o=m?e.model.columnNames:e.coefNames;if(o===void 0)throw new TypeError("the model must be a formula fit of lm or glm");if(m&&e.family!=="binomial"&&e.family!=="gaussian")throw new RangeError("the model's family is not supported (binomial or gaussian)");let l=[`${n}:${t}`,`${t}:${n}`].find(d=>o.includes(d));if(!o.includes(n)||l===void 0)throw new RangeError(`the model has no coefficients '${n}' and '${n}:${t}'`);let i=r==="HC3"?re(e,{type:"HC3"}):ne(e),c=i.names.indexOf(n),a=i.names.indexOf(l);if(c<0||a<0)throw new RangeError("the predictor's or the interaction's coefficient is aliased");let u=e.coefficients,E=m?e.model.rows:e.rows,b=s[t];if(b===void 0)throw new RangeError(`object '${t}' not found`);let f=E.map(d=>Number(b[d]));if(f.some(d=>!Number.isFinite(d)))throw new RangeError(`'${t}' must be numeric`);return{b1:u[o.indexOf(n)],b3:u[o.indexOf(l)],v11:i.vcov[c][c],v13:i.vcov[c][a],v33:i.vcov[a][a],df:e.dfResidual,isGlm:m&&e.family==="binomial",w:f}}function pt(e,s){if(!(s>0&&s<1))throw new RangeError("alpha must be in (0, 1)");let n=Ce(1-s/2,e.df),t=n*n,r=2**-1022,m=Number.MAX_VALUE;if(![e.v11,e.v33].every(A=>A>=r&&A<=m)||!Number.isFinite(e.v13))throw new RangeError("the model's coefficient covariance is out of the double range (rescale the variables)");let o=2**Math.round(Math.log2(e.v11)/2),l=2**Math.round((Math.log2(e.v11)-Math.log2(e.v33))/2),i=e.b1/o,c=e.b3/o*l,a=e.v11/o/o,u=e.v13/o/o*l,E=e.v33/o/o*l*l,b=c*c-t*E,f=2*(i*c-t*u),d=i*i-t*a,y=f*f-4*b*d,M=Math.min(...e.w),v=Math.max(...e.w),p=[M,v],h,T=!1;if(!(y>=0))h=[-1/0,1/0],T=!0;else{let A=Math.sqrt(y),S=(-f-Math.sign(f||1)*A)/(2*b),C=S===0?(-f+Math.sign(f||1)*A)/(2*b):d/(b*S);h=S<=C?[S*l,C*l]:[C*l,S*l]}let w=!T&&b<0,g=!(h[0]>M&&h[0]<v)&&!(h[1]>M&&h[1]<v);return{bounds:h,inside:w,failed:T,allSig:g,modrange:p,alpha:s}}function Wt(e,s,n){return K(n,Ft,"johnsonNeyman"),pt(dt(e,s,n.pred,n.modx,n.robust??!1),n.alpha??.05)}function Kt(e,s,n){K(n,It,"simSlopes");let t=dt(e,s,n.pred,n.modx,n.robust??!1),r=n.ciWidth??.95;if(!(r>0&&r<1))throw new RangeError("ciWidth must be in (0, 1)");let m=n.modxValues??"mean-plus-minus",o;if(m==="mean-plus-minus"){let b=B(t.w),f=Math.sqrt(Te(t.w));o=[b-f,b,b+f]}else if(Array.isArray(m)&&m.every(b=>Number.isFinite(b)))o=[...m];else throw new RangeError('modxValues must be "mean-plus-minus" or finite numbers');let l=t.isGlm?fe((1+r)/2):Ce((1+r)/2,t.df),i=o.map(b=>t.b1+t.b3*b),c=o.map(b=>Math.sqrt(t.v11+2*b*t.v13+b*b*t.v33)),a=i.map((b,f)=>b/c[f]),u=a.map(b=>2*(t.isGlm?Z(-Math.abs(b)):He(-Math.abs(b),t.df))),E={values:o,estimate:i,se:c,lower:i.map((b,f)=>b-l*c[f]),upper:i.map((b,f)=>b+l*c[f]),statistic:a,statisticName:t.isGlm?"z":"t",p:u};return(n.johnsonNeyman??!0)&&(E.jn=pt(t,n.jnAlpha??.05)),E}var Gt=["treat","mediator"],Ut=["nmc","confLevel","treat","mediator"],Pt=new Set(["moderator","treatValue","controlValue","wLow","wHigh","method","nmc","sims","confLevel","robustSE","treat","mediator"]);function de(e,s){let n=22250738585072014e-324,t=Number.MAX_VALUE;if(e.some((r,m)=>r.some(o=>!Number.isFinite(o))||!(r[m]>=n&&r[m]<=t)))throw new RangeError(`the ${s} model's coefficient covariance is out of the double range (rescale the variables)`)}function Ae(e,s,n){let t=ne(e),r=t.names.indexOf(s);if(r<0)throw new RangeError(`the ${n} model has no coefficient '${s}'`);let m="family"in e?e.model.columnNames:e.coefNames;return{value:e.coefficients[m.indexOf(s)],v:t.vcov[r][r],names:t.names,vcov:t.vcov}}function Bt(e,s,n){K(n,Gt,"sobelTest");let t=Ae(e,n.treat,"mediator"),r=Ae(s,n.mediator,"outcome");de([[t.v]],"mediator"),de([[r.v]],"outcome");let m=t.value,o=r.value,l=Math.sqrt(t.v),i=Math.sqrt(r.v),c=m*o,a=Math.hypot(o*l,m*i),u=c/a;return{a:m,sa:l,b:o,sb:i,ab:c,se:a,z:u,p:2*Z(Math.abs(u),{lowerTail:!1})}}var bt=(e,s)=>{let n=1-s,t=ae(e,[n/2,1-n/2],{type:7});return[t[0],t[1]]};function vt(e){if(!(e>0&&e<1))throw new RangeError("confLevel must be in (0, 1)")}function Xt(e,s,n){K(n,Ut,"mediationMonteCarlo");let t=Math.trunc(n.nmc??2e4),r=n.confLevel??.95;if(vt(r),!(t>=2))throw new RangeError("nmc must be at least 2");let m=Ae(e,n.treat,"mediator"),o=Ae(s,n.mediator,"outcome"),l=Ae(s,n.treat,"outcome"),i=o.names.indexOf(n.mediator),c=o.names.indexOf(n.treat);de([[m.v]],"mediator"),de([[o.vcov[i][i],o.vcov[i][c]],[o.vcov[c][i],o.vcov[c][c]]],"outcome");let a=ue(t,{mean:m.value,sd:Math.sqrt(m.v)}),u=Ne(t,[o.value,l.value],[[o.vcov[i][i],o.vcov[i][c]],[o.vcov[c][i],o.vcov[c][c]]]),E=a.map((p,h)=>p*u[h][0]),b=u.map(p=>p[1]),f=E.map((p,h)=>p+b[h]),d=E.map((p,h)=>p/f[h]),y=m.value*o.value,M=l.value,v=(p,h)=>({estimate:p,ci:bt(h,r),sims:h});return{indirect:v(y,E),direct:v(M,b),total:v(y+M,f),proportion:v(y/(y+M),d)}}function gt(e,s,n){let t=e[n];if(t===void 0)throw new RangeError(`object '${n}' not found`);let r=s.map(i=>Number(t[i]));if(r.some(i=>!Number.isFinite(i)))throw new RangeError(`'${n}' must be numeric`);let m=[...new Set(r)].sort((i,c)=>i-c);if(m.length===2)return[m[0],m[1]];let o=B(r),l=Math.sqrt(Te(r));return[o-l,o+l]}var wt=e=>!("family"in e)||e.family==="gaussian",Yt=e=>"family"in e?e.model.rows:e.rows;function Jt(e,s,n,t){K(t,Pt,"moderatedMediation");let{treat:r,mediator:m,moderator:o}=t;if(typeof r!="string"||typeof m!="string"||typeof o!="string")throw new TypeError("'treat', 'mediator' and 'moderator' are required");let l=t.confLevel??.95;vt(l);let i=Yt(e),[c,a]=gt(n,i,r),[u,E]=gt(n,i,o),b=t.treatValue??a,f=t.controlValue??c,d=t.wLow??u,y=t.wHigh??E,M=t.method??(wt(e)&&wt(s)?"monteCarlo":"mediate"),v=(R,_)=>({estimate:R,ci:bt(_,l),sims:_}),p={method:M,wLow:d,wHigh:y,treatValue:b,controlValue:f};if(M==="mediate"){let R=De(),_=se=>je(e,s,n,{treat:r,mediator:m,treatValue:b,controlValue:f,covariates:{[o]:se},sims:t.sims??1e3,confLevel:l,robustSE:t.robustSE??!1}),q=_(d);Ve(R);let W=_(y),pe=W.dAvg.sims.map((se,yt)=>se-q.dAvg.sims[yt]),me=se=>({estimate:se.estimate,ci:se.ci,sims:se.sims});return{...p,indirectLow:me(q.dAvg),indirectHigh:me(W.dAvg),directLow:me(q.zAvg),directHigh:me(W.zAvg),totalLow:me(q.tau),totalHigh:me(W.tau),index:v(W.dAvg.estimate-q.dAvg.estimate,pe),low:q,high:W}}if(M!=="monteCarlo")throw new RangeError('method must be "monteCarlo" or "mediate"');let h=Math.trunc(t.nmc??2e4);if(!(h>=2))throw new RangeError("nmc must be at least 2");let T=t.robustSE?re(e,{type:"HC3"}):ne(e),w=t.robustSE?re(s,{type:"HC3"}):ne(s),g=(R,_)=>_.map(q=>{let W="family"in R?R.model.columnNames:R.coefNames;return R.coefficients[W.indexOf(q)]});de(T.vcov,"mediator"),de(w.vcov,"outcome");let A=Ne(h,g(e,T.names),T.vcov),S=Ne(h,g(s,w.names),w.vcov),C=g(e,T.names),$=g(s,w.names),O=(R,_,q)=>{let W=q===void 0?R.indexOf(_):[`${_}:${q}`,`${q}:${_}`].map(pe=>R.indexOf(pe)).find(pe=>pe>=0)??-1;if(q===void 0&&W<0)throw new RangeError(`a model has no coefficient '${_}'`);return W},j=O(T.names,r),V=O(T.names,r,o),x=O(w.names,m),F=O(w.names,m,o),D=O(w.names,r),Q=O(w.names,r,o),z=(R,_)=>_<0?0:R[_],U=b-f,le=(R,_,q)=>U*(z(R,j)+z(R,V)*q)*(z(_,x)+z(_,F)*q),N=(R,_)=>U*(z(R,D)+z(R,Q)*_),k=R=>Array.from({length:h},(_,q)=>R(q)),I=k(R=>le(A[R],S[R],d)),P=k(R=>le(A[R],S[R],y)),G=k(R=>N(S[R],d)),X=k(R=>N(S[R],y)),Y=le(C,$,d),J=le(C,$,y),H=N($,d),L=N($,y);return{...p,indirectLow:v(Y,I),indirectHigh:v(J,P),directLow:v(H,G),directHigh:v(L,X),totalLow:v(Y+H,I.map((R,_)=>R+G[_])),totalHigh:v(J+L,P.map((R,_)=>R+X[_])),index:v(J-Y,P.map((R,_)=>R-I[_]))}}export{Rt as balTab,Wt as johnsonNeyman,Mt as match,Je as matchWeights,je as mediate,Xt as mediationMonteCarlo,Jt as moderatedMediation,Kt as simSlopes,Bt as sobelTest,St as tableOneSmd,Ht as trimWeights,Dt as weightSummary,qt as weightit};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{b as d,c as p}from"./chunk-
|
|
1
|
+
/*! rStats.js 1.18.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{b as d,c as p}from"./chunk-CTZUYKVL.js";import{d as g}from"./chunk-XQCHC3SH.js";import{a as N,c as b}from"./chunk-KJMLQHCY.js";import{l as h}from"./chunk-MVPHISOV.js";function w(f,n,o){let r=Math.ceil(o);if(!Number.isFinite(o)||r<0)throw new RangeError("'length.out' must be a non-negative number");if(r===1)return[f];let s;if(Number.isFinite(n-f)||!Number.isFinite(f)||!Number.isFinite(n)){let a=(n-f)/(r-1);s=Array.from({length:r},(e,t)=>f+t*a)}else{let a=f/2,e=(n/2-a)/(r-1);s=Array.from({length:r},(t,i)=>2*(a+i*e))}return r>0&&(s[r-1]=n),s}var E=["nbins"],A=["useNA"],R=["right","includeLowest"],x=["rightmostClosed","allInside","leftOpen"];function q(f,...n){h(p(n),E,"tabulate");let{nbins:o}=d(["nbins"],n),r=Array.from(f,e=>{let t=Math.trunc(e);return t>=-2147483647&&t<=2147483647?t:NaN});if(o!==void 0){if(Number.isNaN(o))throw new RangeError("missing value where TRUE/FALSE needed");if(Math.trunc(o)<0)throw new RangeError("invalid 'nbin' argument");if(Math.trunc(o)>=2**31)throw new RangeError("attempt to make a table with >= 2^31 elements")}let s=o===void 0?Math.max(1,N(r.filter(e=>!Number.isNaN(e)))):Math.trunc(o),a=new Array(Math.max(0,s)).fill(0);for(let e of r)e>=1&&e<=s&&a[e-1]++;return a}function C(f,n={}){h(n,A,"table");let o=n.useNA??"no";if(o!=="no"&&o!=="ifany"&&o!=="always")throw new RangeError("'arg' should be one of \u201Cno\u201D, \u201Cifany\u201D, \u201Calways\u201D");let r=g(Array.from(f)),s=new Map,a=0;for(let i of r)Number.isNaN(i)?a++:s.set(i,(s.get(i)??0)+1);let e=Array.from(s.keys()).sort((i,u)=>i-u),t=e.map(i=>s.get(i));return(o==="always"||o==="ifany"&&a>0)&&(e.push(NaN),t.push(a)),{values:e,counts:t}}function K(f){let n=new Set,o=[];for(let r of Array.from(f)){let s=r===0?0:r;n.has(s)||(n.add(s),o.push(r))}return o}function U(f,n,o={}){h(o,R,"cut");let r=o.right??!0,s=o.includeLowest??!1,a=Array.from(f),e;if(typeof n=="number"||n.length===1){let t=typeof n=="number"?n:n[0];if(!(t>=2))throw new RangeError("invalid number of intervals");let i=Math.trunc(t+1),u=a.filter(y=>!Number.isNaN(y)),l=b(u),m=N(u);if(u.length>0&&l===m&&!Number.isFinite(l))throw new RangeError("missing value where TRUE/FALSE needed");if(u.length===0||l===-1/0)throw new RangeError("'from' must be a finite number");if(m===1/0)throw new RangeError("'to' must be a finite number");let c=m-l;c===0?(c=l!==0?Math.abs(l):1,e=w(l-c/1e3,m+c/1e3,i)):(e=w(l,m,i),e[0]=l-c/1e3,e[i-1]=m+c/1e3)}else if(e=n.filter(t=>!Number.isNaN(t)).sort((t,i)=>t-i),e.length<2)throw new RangeError("invalid number of intervals");if(new Set(e).size<e.length)throw new RangeError("'breaks' are not unique");return M(a,e,r,s)}function M(f,n,o,r){let s=n.length;return f.map(a=>{if(Number.isNaN(a))return NaN;let e=0,t=s;for(;e<t;){let u=Math.floor((e+t)/2);(o?n[u]<a:n[u]<=a)?e=u+1:t=u}let i=e;return r&&(o&&a===n[0]&&(i=1),!o&&a===n[s-1]&&(i=s-1)),i>=1&&i<=s-1?i:NaN})}function Y(f,n,o={}){h(o,x,"findInterval");let r=n.length;for(let t=1;t<r;t++)if(!(n[t]>=n[t-1]))throw new RangeError("'vec' must be sorted non-decreasingly and not contain NAs");if(n.some(Number.isNaN))throw new RangeError("'vec' must be sorted non-decreasingly and not contain NAs");let{rightmostClosed:s=!1,allInside:a=!1,leftOpen:e=!1}=o;return Array.from(f).map(t=>{if(Number.isNaN(t))return NaN;if(r===0)return 0;if(e?t<=n[0]:t<n[0])return a||s&&e&&t===n[0]?1:0;if(e?t>n[r-1]:t>=n[r-1])return a||s&&!e&&t===n[r-1]?r-1:r;let i=0,u=r;for(;i<u;){let l=Math.floor((i+u)/2);(e?n[l]<t:n[l]<=t)?i=l+1:u=l}return i})}export{w as a,q as b,C as c,K as d,U as e,Y as f};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{
|
|
1
|
+
/*! rStats.js 1.18.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{C as R}from"./chunk-IW4S634O.js";import{l as E}from"./chunk-MVPHISOV.js";var j=["tol"];function k(s,e,r){let h=s+r,u=s-r,f=Math.abs(u),m=e+e,o=Math.abs(m),[a,n]=Math.abs(s)>Math.abs(r)?[s,r]:[r,s],t=f>o?f*Math.sqrt(1+(o/f)**2):f<o?o*Math.sqrt(1+(f/o)**2):o*Math.SQRT2,c,w,i;h<0?(c=.5*(h-t),i=-1,w=a/c*n-e/c*e):h>0?(c=.5*(h+t),i=1,w=a/c*n-e/c*e):(c=.5*t,w=-.5*t,i=1);let M,g;u>=0?(M=u+t,g=1):(M=u-t,g=-1);let b,l;if(Math.abs(M)>o){let v=-m/M;l=1/Math.sqrt(1+v*v),b=v*l}else if(o===0)b=1,l=0;else{let v=-M/m;b=1/Math.sqrt(1+v*v),l=v*b}if(i===g){let v=b;b=-l,l=v}return{rt1:c,rt2:w,cs1:b,sn1:l}}function d(s){let e=s.length,r=Array.from({length:e},(o,a)=>Array.from({length:e},(n,t)=>a>=t?s[a][t]:s[t][a])),h=Array.from({length:e},(o,a)=>Array.from({length:e},(n,t)=>a===t?1:0)),u=2**-53;for(let o=0;o<100;o++){let a=!1;for(let n=0;n<e-1;n++)for(let t=n+1;t<e;t++){let c=r[n][t];if(c===0)continue;let w=r[n][n],i=r[t][t];if(Math.abs(c)<=u*Math.sqrt(Math.abs(w))*Math.sqrt(Math.abs(i))){r[n][t]=r[t][n]=0;continue}a=!0;let M=(i-w)/(2*c),g=Math.abs(M)>1e150?1/(2*M):Math.sign(M||1)/(Math.abs(M)+Math.sqrt(M*M+1)),b=1/Math.sqrt(1+g*g),l=g*b,v=l/(1+b);r[n][n]=w-g*c,r[t][t]=i+g*c,r[n][t]=r[t][n]=0;for(let p=0;p<e;p++){if(p!==n&&p!==t){let x=r[p][n],A=r[p][t];r[p][n]=r[n][p]=x-l*(A+v*x),r[p][t]=r[t][p]=A+l*(x-v*A)}let q=h[p][n],y=h[p][t];h[p][n]=q-l*(y+v*q),h[p][t]=y+l*(q-v*y)}}if(!a)break}let f=Array.from({length:e},(o,a)=>a).sort((o,a)=>r[a][a]-r[o][o]),m=Array.from({length:e},()=>new Array(e));return f.forEach((o,a)=>{let n=0;for(let c=1;c<e;c++)Math.abs(h[c][o])>Math.abs(h[n][o])&&(n=c);let t=h[n][o]<0?-1:1;for(let c=0;c<e;c++)m[c][a]=t*h[c][o]}),{values:f.map(o=>r[o][o]),vectors:m}}function _(s){let e=s.length;if(e===1)return{values:[s[0][0]],vectors:[[1]]};if(e===2){let{rt1:r,rt2:h,cs1:u,sn1:f}=k(s[0][0],s[1][0],s[1][1]);return r>=h?{values:[r,h],vectors:[[u,-f],[f,u]]}:{values:[h,r],vectors:[[-f,u],[u,f]]}}return d(s)}function z(s){let e=s.length;if(e===1)return[[Math.sqrt(Math.max(s[0][0],0))]];let{values:r,vectors:h}=d(s),u=r.map(m=>Math.sqrt(Math.max(m,0))),f=[];for(let m=0;m<e;m++){let o=[];for(let a=0;a<e;a++){let n=0;for(let t=0;t<e;t++)n+=h[m][t]*u[t]*h[a][t];o.push(n)}f.push(o)}return f}function N(s,e){if(s.length!==e||s.some(r=>r.length!==e))throw new RangeError("incompatible arguments");for(let r of s)for(let h of r)if(!Number.isFinite(h))throw new RangeError("'Sigma' must be finite")}function C(s,e,r,h={}){E(h,j,"mvrnorm");let u=h.tol??1e-6,f=Array.from(r,i=>Array.from(i));if(f.some(i=>i.length!==f[0].length))throw new TypeError("all rows of the matrix must have the same length");let m=e.length;N(f,m);let o=Math.trunc(s);if(!(o>=1))throw new RangeError(o===0?"non-conformable arguments":"invalid arguments");let{values:a,vectors:n}=_(f);if(!a.every(i=>i>=-u*Math.abs(a[0])))throw new RangeError("'Sigma' is not positive definite");let t=a.map(i=>Math.sqrt(Math.max(i,0))),c=R(o*m),w=[];for(let i=0;i<o;i++){let M=[];for(let g=0;g<m;g++){let b=0;for(let l=0;l<m;l++)b+=n[g][l]*t[l]*c[l*o+i];M.push(e[g]+b)}w.push(M)}return w}export{z as a,C as b};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! rStats.js 1.18.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as V,b as F3,e as R3}from"./chunk-GEHMND3C.js";import{a as L3,b as T,c as C0,d as m,e as U1,f as L,g as O,h as w,i as p1,j as q0,k as b,l as B0}from"./chunk-MVPHISOV.js";var D=11102230246251565e-32;var J=171.6243769563027,F1=.9189385332046728,k3=1.1447298858494002;var L1=.5772156649015329;var Ge=[.00016011952247675185,.0011913514700658638,.010421379756176158,.04763678004571372,.20744822764843598,.4942148268014971,1],Te=[-23158187332412014e-21,.0005396055804933034,-.004456419138517973,.011813978522206043,.035823639860549865,-.23459179571824335,.0714304917030273,1];function O3(e){let t=1;e<0&&(e=-e,t=-1);let n=e%2;return n<.5?t*Math.sin(Math.PI*n):n>1.5?t*Math.sin(Math.PI*(n-2)):-t*Math.sin(Math.PI*(n-1))}function j0(e){if(e>J)return 1/0;let t=e-1,n=m(w(b([t,0]),t),[-t+Xe(t),0]);return Math.sqrt(2*Math.PI*t)*Math.exp(n[0])*(1+n[1])}function t1(e){if(!Number.isFinite(e))return e>0?e:NaN;if(e===0)return NaN;let t=Math.abs(e);if(t>33){if(e<0){let o=Math.floor(t);if(o===t)return NaN;let s=o%2===0?-1:1,r=t-o;if(r>.5&&(o+=1,r=t-o),r=t*O3(r),r===0)return s*(1/0);let i=Math.abs(r)*j0(t);if(Number.isFinite(i))return s*Math.PI/i;let c=1;for(let u=1;u<=10;u++)c*=t-u;return s*Math.PI/(Math.abs(r)*j0(t-10))/c}return j0(e)}let n=1;for(;e>=3;)e-=1,n*=e;for(;e<0;){if(e>-1e-9)return G3(e,n);n/=e,e+=1}for(;e<2;){if(e<1e-9)return G3(e,n);n/=e,e+=1}return e===2?n:(e-=2,n*V(e,Ge)/V(e,Te))}function G3(e,t){return e===0?NaN:t/((1+L1*e)*e)}var Oe=[.0008116141674705085,-.0005950619042843014,.0007936503404577169,-.002777777777300997,.08333333333333319],Ce=[-1378.2515256912086,-38801.631513463784,-331612.9927388712,-1162370974927623e-9,-1.7217370082083966e6,-853555.6642457654],qe=[-351.81570143652345,-17064.210665188115,-220528.59055385445,-1.1393344436798252e6,-2.5325230717758294e6,-2.0188914143353277e6],Be=2556348e299;function je(e){let t=(e-.5)*Math.log(e)-e+F1;if(e>1e8)return t;let n=1/(e*e);return t+((.0007936507936507937*n-.002777777777777778)*n+.08333333333333333)/e}function K(e){if(!Number.isFinite(e))return{value:e===-1/0?1/0:e,sign:1};if(e<-34){let o=-e,s=K(o).value,r=Math.floor(o);if(r===o)return{value:1/0,sign:1};let i=r%2===0?-1:1,c=o-r;return c>.5&&(r+=1,c=r-o),c=o*O3(c),c===0?{value:1/0,sign:1}:{value:k3-Math.log(c)-s,sign:i}}if(e!==0&&Math.abs(e)<1e-300)return{value:-Math.log(Math.abs(e)),sign:e>0?1:-1};if(e<13){let o=1,s=0,r=e;for(;r>=3;)s-=1,r=e+s,o*=r;for(;r<2;){if(r===0)return{value:1/0,sign:1};o/=r,s+=1,r=e+s}let i=1;if(o<0&&(i=-1,o=-o),r===2)return{value:Math.log(o),sign:i};s-=2;let c=e+s;return{value:Math.log(o)+c*V(c,Ce)/F3(c,qe),sign:i}}if(e>Be)return{value:1/0,sign:1};if(e>=1e3)return{value:je(e),sign:1};let t=(e-.5)*Math.log(e)-e+F1,n=1/(e*e);return{value:t+V(n,Oe)/e,sign:1}}function W(e){return Math.abs(e-1)<=.2?e1(e-1):Math.abs(e-2)<=.2?e1(e-2)+Math.log1p(e-2):K(e).value}var ze=[1.6449340668482264,1.2020569031595942,1.0823232337111381,1.03692775514337,1.0173430619844492,1.008349277381923,1.0040773561979444,1.0020083928260821,1.000994575127818,1.0004941886041194,1.000246086553308,1.0001227133475785,1.0000612481350588,1.000030588236307,1.0000152822594086,1.0000076371976379,1.000003817293265,1.0000019082127165,1.0000009539620338,1.0000004769329869,1.0000002384505027,1.000000119219926,1.000000059608189,1.0000000298035034,1.0000000149015549,1.0000000074507118,1.000000003725334,1.0000000018626598,1.0000000009313275,1.0000000004656628,1.000000000232831,1.0000000001164155,1.0000000000582077,1.0000000000291038,1.000000000014552,1.000000000007276,1.000000000003638,1.000000000001819,1.0000000000009095,1.0000000000004547];function T3(e){if(e===0)return 0;let t=-L1*e,n=-e;for(let o=2;o<42;o++){n*=-e;let s=ze[o-2]*n/o;if(t+=s,Math.abs(s)<D*Math.abs(t))break}return t}function e1(e){return Math.abs(e)<=.2?T3(e):Math.abs(e-1)<=.2?Math.log(e)+T3(e-1):W(e+1)}var He=[1/12,-1/360,1/1260,-1/1680,1/1188,-691/360360,1/156];function Xe(e){let t=1/(e*e),n=0,o=1/e;for(let s of He)n+=s*o,o*=t;return n}function c6(e){return Math.abs(e-Z1(e))>1e-9*Math.max(1,Math.abs(e))}function i6(e){let t=Z1(e);return Math.abs(e-t)<=Math.min(1e-7,1e-9*Math.max(1,Math.abs(e)))?t:Math.floor(e)}function Z1(e){if(!Number.isFinite(e))return e;let t=Math.round(e);return t-e===.5&&t%2!==0?t-1:t}var Ye=["rCompatible"],Ue=["rCompatible"],Ze=[.08333333333333333,-.021092796092796094,.007575757575757576,-.004166666666666667,.003968253968253968,-.008333333333333333,.08333333333333333],Ke=.9955816268920898,Qe=1569415565/1073741824,Ve=381566830/1073741824/1073741824,Je=9016312093258695e-35,We=[-.002071332116774595,-.04525132144873906,-.28919126444774784,-.6503185377089651,-.3255503118680449,.25479851061131553],$e=[-5578984132167551e-22,.0021284987017821146,.054151797245674226,.43593529692665967,1.4606242909763516,2.076711702373047,1];function xe(e){let t=e-Qe;t-=Ve,t-=Je;let n=V(e-1,We)/V(e-1,$e);return t*Ke+t*n}function et(e){let t=0;if(e<1e17){let n=1/(e*e);t=n*V(n,Ze)}return Math.log(e)-.5/e-t}function tt(e){let t=0;if(Number.isNaN(e)||e===1/0)return e;if(e===-1/0)return NaN;if(e===0)return NaN;if(e<0){let n=e-Math.trunc(e);if(n===0)return NaN;t=-Math.PI/Math.tan(Math.PI*n),e=1-e}if(e<=10&&e===Math.floor(e)){let n=e;for(let o=1;o<n;o++)t+=1/o;return t-L1}if(e<1)t-=1/e,e+=1;else if(e<10)for(;e>2;)e-=1,t+=1/e;return e>=1&&e<=2?t+xe(e):t+et(e)}var H0=-.5040830082644554,nt=7289763902976895e-32,C3;function ot(e){C3??=Array.from({length:100},(s,r)=>rt(r+2,H0));let t=nt,n=-1,o=e-H0;for(let s=1;s<100;s++){n*=-o;let r=n*C3[s-1];if(t+=r,Math.abs(r)<Number.EPSILON*Math.abs(t))break}return t}var B3=[12,-720,30240,-1209600,47900160,-18924375803183792e-7,74724249600,-2950130727918164e-3,11646782814350067e-2,-4597978722407473,0x284e46fd8153bc0,-7166165256175667e3];function st(e,t){let n=1,o=t,s=1,r=0;for(;t<=1e8&&(r<9||o<=9||o<e);)if(r+=1,o+=1,s=Math.exp(-e*Math.log1p(r/t)),n+=s,Math.abs(s/n)<D)return n;let i=o;n+=s*i/(e-1),n-=.5*s;let c=1,u=0;for(let a=0;a<12;a++){c*=e+u,s/=i;let f=c*s/B3[a];if(n+=f,Math.abs(f/n)<D)break;u+=1,c*=e+u,s/=i,u+=1}return n}function rt(e,t){let n=Math.pow(t,-e),o=t,s=0,r=0;for(;r<9||o<=9||o<e;)if(r+=1,o+=1,s=Math.pow(o,-e),n+=s,Math.abs(s/n)<D)return n;let i=o;n+=s*i/(e-1),n-=.5*s;let c=1,u=0;for(let a=0;a<12;a++){c*=e+u,s/=i;let f=c*s/B3[a];if(n+=f,Math.abs(f/n)<D)break;u+=1,c*=e+u,s/=i,u+=1}return n}function q3(e,t){if(t===1/0)return e%2===0?-0:0;let n=1;for(let s=1;s<=e;s++)n*=s/t;let o=n*(st(e+1,t)/t);return e%2===0?-o:o}var z0=[];function ct(e){if(z0[e]!==void 0)return z0[e];let t=[0,1];for(let s=0;s<e;s++){let r=t.slice(1).map((c,u)=>c*(u+1)),i=new Array(r.length+2).fill(0);r.forEach((c,u)=>{i[u]=i[u]-c,i[u+2]=i[u+2]-c}),t=i}let n=(e+1)%2,o=[];for(let s=n;s<t.length;s+=2)o.push(t[s]);return z0[e]=o,o}function it(e,t){let n=ct(e),o=t*t,s=n[n.length-1];for(let r=n.length-2;r>=0;r--)s=s*o+n[r];return(e+1)%2===1?s*t:s}function ut(e){let t=e-Math.round(e);return Math.sin(Math.PI*(.5-Math.abs(t)))/Math.sin(Math.PI*t)}function j3(e,t,n){if(Number.isNaN(t))return NaN;if(t===-1/0)return n&&e%2===1?1/0:NaN;if(t<=0&&t===Math.floor(t))return e%2===1?1/0:NaN;if(t>0)return q3(e,t);let o=q3(e,1-t);return(e%2===0?o:-o)-Math.pow(Math.PI,e+1)*it(e,ut(t))}function at(e){return Math.abs(e-H0)<.3?ot(e):tt(e)}function M6(e,t){return B0(t,Ye,"trigamma"),j3(1,e,t?.rCompatible??!0)}function m6(e,t=0,n){B0(n,Ue,"psigamma");let o=Z1(t);return Number.isNaN(o)||o<0||o>100?NaN:o===0?at(e):j3(o,e,n?.rCompatible??!0)}var z3=[1/12,-1/360,1/1260,-1/1680,1/1188,-691/360360,1/156];function H3(e,t){let n=X0(e,t);return n[0]+n[1]}function X0(e,t){if(e===0)return[0,0];let n=0,o=t;for(;o<=15;)n-=Math.log1p(e/o),o+=1;let s=Math.log1p(e/o),r=e*Math.log(o+e),i=Math.abs(r)>2&&Number.isFinite(r)&&e<1e300?w(b(T(o,e)),e):[r,0],c=(o-.5)*s-e,u=1/o,a=1/(o*o);for(let f=0;f<z3.length;f++){let l=2*f+1;c+=z3[f]*u*Math.expm1(-l*s),u*=a}return m(i,[n+c,0])}var X3=22250738585072014e-324,J0=1.8378770664093456,ft=[.15342640972002736,.08106146679532726,.05481412105191765,.0413406959554093,.03316287351993629,.02767792568499834,.023746163656297496,.020790672103765093,.018488450532673187,.016644691189821193,.015134973221917378,.013876128823070748,.012810465242920227,.01189670994589177,.011104559758206917,.010411265261972096,.009799416126158804,.009255462182712733,.008768700134139386,.00833056343336287,.00793411456431402,.007573675487951841,.007244554301320383,.00694284010720953,.006665247032707682,.006408994188004207,.006171712263039458,.0059513701127588475,.0057462165130101155,.005554733551962801],K1=1/12,Q1=1/360,Y0=1/1260,Y3=1/1680,lt=1/1188,ht=691/360360,Mt=1/156;function U3(e){let t=1/(e*e);return e>500?(K1-Q1*t)/e:e>80?(K1-(Q1-Y0*t)*t)/e:e>35?(K1-(Q1-(Y0-Y3*t)*t)*t)/e:(K1-(Q1-(Y0-(Y3-(lt-(ht-Mt*t)*t)*t)*t)*t)*t)/e}function k(e){if(e>15)return U3(e);let t=2*e;if(t===Math.floor(t)&&t>=1)return ft[t-1];if(e<1)return e1(e)-(e+.5)*Math.log(e)+e-F1;let n=0,o=e;for(;o<=15;)n+=(o+.5)*Math.log1p(1/o)-1,o+=1;return n+U3(o)}var mt=.12;function b1(e,t){if(e===0)return t;if(Math.max(e,t[0])>1e299&&Math.min(e,t[0])>1e-250)return $1(b1(e*2**-64,[t[0]*2**-64,t[1]*2**-64]));if(!L3())return pt(e,t);let n=-t[0],o=-t[1],s=e+n,r=s-e,i=e-(s-r)+(n-r),c=0+o,u=c-0,a=0-(c-u)+(o-u),f=i+c,l=s+f,I=f-(l-s)+a,S=l+I,E=I-(S-l),M=S,N=E;if(Math.abs(M)<.1*(e+t[0])){let q=e+t[0],z=q-e,g=e-(q-z)+(t[0]-z),P=0+t[1],A=P-0,F=0-(P-A)+(t[1]-A),_=g+P,y=q+_,o1=_-(y-q)+F,v=y+o1,X=o1-(v-y),G=M/v,s1=v*G,g1=134217729*v,$=g1-(g1-v),d1=v-$,R1=134217729*G,t0=R1-(R1-G),n2=G-t0,o2=$*t0-s1+$*n2+d1*t0+d1*n2+X*G,s2=s1+o2,le=o2-(s2-s1),r2=-s2,c2=-le,k1=M+r2,i2=k1-M,he=M-(k1-i2)+(r2-i2),n0=N+c2,u2=n0-N,Me=N-(n0-u2)+(c2-u2),a2=he+n0,o0=k1+a2,f2=a2-(o0-k1)+Me,I1=o0+f2,s0=f2-(I1-o0),r1=I1/v,r0=v*r1,l2=134217729*v,c0=l2-(l2-v),h2=v-c0,M2=134217729*r1,i0=M2-(M2-r1),m2=r1-i0,p2=c0*i0-r0+c0*m2+h2*i0+h2*m2+X*r1,b2=r0+p2,me=p2-(b2-r0),N2=-b2,g2=-me,G1=I1+N2,d2=G1-I1,pe=I1-(G1-d2)+(N2-d2),u0=s0+g2,I2=u0-s0,be=s0-(u0-I2)+(g2-I2),A2=pe+u0,a0=G1+A2,y2=A2-(a0-G1)+be,v2=a0+y2,Ht=y2-(v2-a0),S2=v2/v,T1=G+r1,f0=r1-(T1-G),O1=T1+S2,P2=O1-T1,Ne=T1-(O1-P2)+(S2-P2),l0=f0+0,_2=l0-f0,ge=f0-(l0-_2)+(0-_2),w2=Ne+l0,h0=O1+w2,D2=w2-(h0-O1)+ge,E2=h0+D2,de=D2-(E2-h0),p=E2,C1=de,M0=M*p,F2=134217729*M,m0=F2-(F2-M),L2=M-m0,R2=134217729*p,p0=R2-(R2-p),k2=p-p0,G2=m0*p0-M0+m0*k2+L2*p0+L2*k2+(M*C1+N*p),T2=M0+G2,Ie=G2-(T2-M0),Y=T2,Q=Ie;if(Math.abs(e*p*p*p)<=mt){let f1=p*p,c1=2*e*p*f1,l1=c1/3;for(let M1=2;M1<1e3;M1++){c1*=f1;let m1=c1/(2*M1+1);if(l1+=m1,Math.abs(m1)<=2**-60*Y)break}let x=Y+l1,S1=x-Y,q1=Y-(x-S1)+(l1-S1),i1=Q+0,P1=i1-Q,Q2=Q-(i1-P1)+(0-P1),_1=q1+i1,h1=x+_1,R=_1-(h1-x)+Q2,Z=h1+R,w1=R-(Z-h1);return[Z,w1]}let A1=2*e,b0=p*A1,O2=134217729*p,N0=O2-(O2-p),C2=p-N0,q2=134217729*A1,g0=q2-(q2-A1),B2=A1-g0,j2=N0*g0-b0+N0*B2+C2*g0+C2*B2+C1*A1,z2=b0+j2,Ae=j2-(z2-b0),U=z2,y1=Ae,d0=p*p,H2=134217729*p,I0=H2-(H2-p),X2=p-I0,Y2=134217729*p,A0=Y2-(Y2-p),U2=p-A0,Z2=I0*A0-d0+I0*U2+X2*A0+X2*U2+(p*C1+C1*p),K2=d0+Z2,ye=Z2-(K2-d0),v1=K2,ve=ye;for(let f1=1;f1<1e3;f1++){let c1=U*v1,l1=134217729*U,x=l1-(l1-U),S1=U-x,q1=134217729*v1,i1=q1-(q1-v1),P1=v1-i1,_1=x*i1-c1+x*P1+S1*i1+S1*P1+(U*ve+y1*v1),h1=c1+_1,V2=_1-(h1-c1);U=h1,y1=V2;let R=2*f1+1,Z=U/R,w1=R*Z,M1=134217729*R,m1=M1-(M1-R),J2=R-m1,W2=134217729*Z,y0=W2-(W2-Z),$2=Z-y0,x2=m1*y0-w1+m1*$2+J2*y0+J2*$2+0*Z,e3=w1+x2,Se=x2-(e3-w1),t3=-e3,n3=-Se,B1=U+t3,o3=B1-U,Pe=U-(B1-o3)+(t3-o3),v0=y1+n3,s3=v0-y1,_e=y1-(v0-s3)+(n3-s3),r3=Pe+v0,S0=B1+r3,c3=r3-(S0-B1)+_e,D1=S0+c3,P0=c3-(D1-S0),u1=D1/R,_0=R*u1,i3=134217729*R,w0=i3-(i3-R),u3=R-w0,a3=134217729*u1,D0=a3-(a3-u1),f3=u1-D0,l3=w0*D0-_0+w0*f3+u3*D0+u3*f3+0*u1,h3=_0+l3,we=l3-(h3-_0),M3=-h3,m3=-we,j1=D1+M3,p3=j1-D1,De=D1-(j1-p3)+(M3-p3),E0=P0+m3,b3=E0-P0,Ee=P0-(E0-b3)+(m3-b3),N3=De+E0,F0=j1+N3,g3=N3-(F0-j1)+Ee,d3=F0+g3,Wt=g3-(d3-F0),I3=d3/R,z1=Z+u1,L0=u1-(z1-Z),H1=z1+I3,A3=H1-z1,Fe=z1-(H1-A3)+(I3-A3),R0=L0+0,y3=R0-L0,Le=L0-(R0-y3)+(0-y3),v3=Fe+R0,k0=H1+v3,S3=v3-(k0-H1)+Le,X1=k0+S3,P3=S3-(X1-k0),Y1=Y+X1,_3=Y1-Y,Re=Y-(Y1-_3)+(X1-_3),G0=Q+P3,w3=G0-Q,ke=Q-(G0-w3)+(P3-w3),D3=Re+G0,T0=Y1+D3,E3=D3-(T0-Y1)+ke,E1=T0+E3,O0=E3-(E1-T0);if(Math.abs(X1)<=1e-33*Math.abs(E1)||E1===Y&&O0===Q)return[E1,O0];Y=E1,Q=O0}return[Y,Q]}let B=e/t[0],j=B>1e-290&&B<1e290&&Math.max(e,t[0])<1e300?b(p1([e,0],t)):L(b([e,0]),b(t));return Z3(j,e,[M,N])}function pt(e,t){let n=L([e,0],t);if(Math.abs(n[0])<.1*(e+t[0])){let r=p1(n,m([e,0],t)),i=O(n,r),c=w(r,2*e),u=O(r,r);for(let a=1;a<1e3;a++){c=O(c,u);let f=q0(c,2*a+1),l=m(i,f);if(Math.abs(f[0])<=1e-33*Math.abs(l[0])||l[0]===i[0]&&l[1]===i[1])return l;i=l}return i}let o=e/t[0],s=o>1e-290&&o<1e290&&Math.max(e,t[0])<1e300?b(p1([e,0],t)):L(b([e,0]),b(t));return Z3(s,e,n)}function Z3(e,t,n){return t<1e299?m(w(e,t),U1(n)):$1(m(w(e,t*2**-64),[-n[0]*2**-64,-n[1]*2**-64]))}function $1(e){let t=e[0]*18446744073709552e3;return Number.isFinite(t)?[t,e[1]*2**64]:[t,0]}function W1(e,t,n,o=0){let s=t[0]>1e299?5421010862427522e-35:1,r;if(o===0){let c=O([t[0]*s,t[1]*s],n);r=[c[0]/s,c[1]/s]}else r=Q0(t,n,o);if(r[0]>=1e-290)return b1(e,r);let i=L(b([e,0]),m(b(t),U0(n,o)));return m(w(i,e),T(o===0?t[0]*n[0]:r[0],-e))}var V1=(e,t)=>e*2**Math.trunc(t/2)*2**(t-Math.trunc(t/2)),bt=[.6931471805599453,23190468138462996e-33],U0=(e,t)=>t===0?b(e):L(b(e),w(bt,t));function A6(e,t){if(!Number.isFinite(e)||!Number.isFinite(t)||t===0)return NaN;if(e===0)return t;let n=b1(e,[t,0]);return n[0]+n[1]}function Z0(e,t,n,o){if(t)return-e[0]-e[1]-.5*n();if(e[0]<700)return Math.exp(-e[0])*(1-e[1])/Math.sqrt(o());if(e[0]>2e3)return 0;let s=o();return!Number.isFinite(s)||s===0?Math.exp(-e[0]-e[1]-.5*n()):Math.exp(700-e[0])*(1-e[1])/Math.sqrt(s)*Math.exp(-700)}function K0(e,t){return e[0]>1e299?$1(O([e[0]*2**-64,e[1]*2**-64],t)):O(e,t)}var Nt=[0,0];function Q0(e,t,n){if(n===0)return K0(e,t);let o=Math.floor(Math.log2(e[0])),s=O([V1(e[0],-o),V1(e[1],-o)],t);return[V1(s[0],o-n),V1(s[1],o-n)]}var J1=e=>e[0]<-2e3?0:Math.exp(e[0])*(1+e[1]);function y6(e,t,n,o,s,r,i=Nt){let[c,u]=i,a=s?-1/0:0,f=s?0:1,l=T(e,t),h=l[0];if(n[0]===0)return e===0?f:a;if(o[0]===0)return t===0?f:a;if(e===0){if(h===0)return f;if(c>0||n[0]<.1){let N=m(W1(h,l,o,u),Q0(l,n,c));return s?-N[0]-N[1]:J1([-N[0],-N[1]])}let M=K0(l,U0(o,u));return s?M[0]+M[1]:J1(M)}if(t===0){if(u>0||o[0]<.1){let N=m(W1(h,l,n,c),Q0(l,o,u));return s?-N[0]-N[1]:J1([-N[0],-N[1]])}let M=K0(l,U0(n,c));return s?M[0]+M[1]:J1(M)}if(e<0||t<0)return a;let I=r!==void 0&&(l[0]!==r||l[1]!==0)?gt(e,r,n,o):W0(e,t,l,n,o,c,u),S=e*(t/h)>2e307,E=Z0(I,s,()=>J0+Math.log(e)+Math.log(t)-Math.log(h),S?()=>Math.PI/8*(e*(t/h)):()=>2*Math.PI*e*(t/h));return S&&!s?E/4:E}function v6(e,t,n,o){let s=T(e,t),r=s[0];return{e:W0(e,t,s,n,o),logDenom:J0+Math.log(e)+Math.log(t)-Math.log(r)}}function gt(e,t,n,o){let s=[e,0],r=T(t,-e),i=l=>{if(t<1e299)return C0(t,l);let h=C0(t*2**-64,l);return[h[0]*2**64,h[1]*2**64]},c,u;if(n[1]===0)c=L(s,i(n[0])),u=U1(c);else if(o[1]===0)u=L(r,i(o[0])),c=U1(u);else return W0(e,r[0],T(e,r[0]),n,o);let a=V0(s,L(s,c),c),f=V0(r,L(r,u),u);return Number.isFinite(a[0]+f[0])?m(m(a,f),T(k(e)+k(r[0]),-k(t))):[1/0,0]}function V0(e,t,n){if(Math.max(e[0],t[0])>1e299){let r=5421010862427522e-35;return $1(V0([e[0]*r,e[1]*r],[t[0]*r,t[1]*r],[n[0]*r,n[1]*r]))}if(Math.abs(n[0])<.1*(e[0]+t[0])){let r=p1(n,m(e,t)),i=O(n,r),c=O(r,[2*e[0],2*e[1]]),u=O(r,r);for(let a=1;a<1e3;a++){c=O(c,u);let f=q0(c,2*a+1),l=m(i,f);if(Math.abs(f[0])<=1e-33*Math.abs(l[0])||l[0]===i[0]&&l[1]===i[1])return l;i=l}return i}let o=e[0]/t[0],s=o>1e-290&&o<1e290?b(p1(e,t)):L(b(e),b(t));return L(O(s,e),n)}function W0(e,t,n,o,s,r=0,i=0){let c=W1(e,n,o,r),u=W1(t,n,s,i);return Number.isFinite(c[0]+u[0])?m(m(c,u),T(k(e)+k(t),-k(n[0]))):[1/0,0]}function a1(e,t,n){let o=K3(e,t,n,!n);return n?o[0]:o[1]}function $0(e,t){return K3(e,t,!0,!0)}function K3(e,t,n,o){if(t===0)return e===0?[0,1]:[-1/0,0];if(!Number.isFinite(t)||e<0)return[-1/0,0];if(e<=t*X3)return[-t,o?Math.exp(-t):NaN];if(t<e*X3){if(!Number.isFinite(e))return[-1/0,0];let a=e1(e),f=a===1/0?-1/0:-t+e*Math.log(t)-a;return[f,o?Math.exp(f):NaN]}if(e<.01&&t<700){let a=e*Math.log(t)-e1(e);return[-t+a,o?Math.exp(-t)*Math.exp(a):NaN]}let s=b1(e,[t,0]),r=s[0]===1/0?s:m(s,[k(e),0]),i=()=>J0+Math.log(e),c=e>2e307,u=c?()=>Math.PI/8*e:()=>2*Math.PI*e;return[n?Z0(r,!0,i,u):NaN,o?Z0(r,!1,i,u)/(c?4:1):NaN]}var x1=1e6;function Q3(e,t){let{value:n,sign:o}=K(t),s=n;return s-=t*Math.log(e),s+=t*(1-t)/(2*e),s+=t*(1-t)*(1-2*t)/(12*e*e),s+=-t*t*(1-t)*(1-t)/(12*e*e*e),{value:s,sign:o}}function F6(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(e<0||t<0)return NaN;if(e===1/0&&t>0||t===1/0&&e>0)return 0;if(e>0&&t>0&&Math.max(e,t)>15){let i=Math.min(e,t),c=Math.max(e,t);if(i<1){let a=X0(i,c);return t1(i)*Math.exp(-a[0])*(1-a[1])}let u=c>=J3?W3(c,i):$3(e,t);return Math.exp(u[0])*(1+u[1])}let n=1;if(e===0||t===0||1/Math.min(e,t)===1/0)return 1/0;if(Math.abs(e)<Math.abs(t)&&([e,t]=[t,e]),Math.abs(e)>x1*Math.abs(t)&&e>x1){let i=Q3(e,t);return i.sign*Math.exp(i.value)}let o=e+t;if(Math.abs(o)>J||Math.abs(e)>J||Math.abs(t)>J){let i=K(o);n*=i.sign;let c=K(t);n*=c.sign;let u=K(e);n*=u.sign;let a=u.value+(c.value-i.value);return n*Math.exp(a)}o=1/t1(o);let s=t1(e),r=t1(t);return Number.isFinite(o)?Math.abs(Math.abs(s*o)-1)>Math.abs(Math.abs(r*o)-1)?r*o*s:s*o*r:n*(1/0)}function L6(e,t){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(e<0||t<0)return NaN;if(e===1/0&&t>0||t===1/0&&e>0)return-1/0;if(e>0&&t>0&&Number.isFinite(e)&&Number.isFinite(t)&&Math.max(e,t)>15){let i=Math.min(e,t),c=Math.max(e,t);if(i<1)return W(i)-H3(i,c);let u=c>=J3?W3(c,i):$3(e,t);return u[0]+u[1]}if(e===0||t===0)return 1/0;if(Math.min(e,t)<1e-300)return W(e)+W(t)-W(e+t);if(Math.abs(e)<Math.abs(t)&&([e,t]=[t,e]),Math.abs(e)>x1*Math.abs(t)&&e>x1)return Q3(e,t).value;let n=e+t;if(Math.abs(n)>J||Math.abs(e)>J||Math.abs(t)>J)return K(e).value+(K(t).value-K(n).value);n=1/t1(n);let o=t1(e),s=t1(t);if(!Number.isFinite(n))return 1/0;let r=Math.abs(Math.abs(o*n)-1)>Math.abs(Math.abs(s*n)-1)?s*n*o:o*n*s;return Math.log(Math.abs(r))}var V3=[.9189385332046728,-38782941580672414e-33],J3=2**52;function W3(e,t){let n=w(L(b([t,0]),b([e,0])),t),o=Math.log1p(t/e),s=Number.isFinite(e+t)?(e+t-.5)*o:e*o+(t-.5)*o,r=-.5*Math.log(t)-s+(k(e)+k(t)-k(e+t));return m(m(n,V3),[r,0])}function $3(e,t){let n=T(e,t),o=w(b([e,0]),e-.5),s=w(b([t,0]),t-.5),r=dt(b(n),m(n,[-.5,0])),i=L(m(o,s),r);return i=m(i,V3),m(i,T(k(e)+k(t),-k(n[0])))}function dt(e,t){return m(w(e,t[0]),w(e,t[1]))}function x0(e){if(Math.abs(e)<.5){if(e*e===0)return 0;let t=e,n=0;for(let o=2;o<500;o++){t*=-e;let s=t/o;if(n+=s,Math.abs(s)<D*Math.abs(n))break}return n}return Math.log1p(e)-e}var x3=[[-.3333333333333333,.08333333333333333,-.014814814814814815,.0011574074074074073,.0003527336860670194,-.0001787551440329218,3919263178522438e-20,-21854485106799924e-22,-185406221071516e-20,8296711340953087e-22,-17665952736826078e-23,6707853543401498e-24,10261809784240309e-24,-4382036018453353e-24,914769958223679e-24,-2551419399494625e-26,-5830772132550426e-26,24361948020667415e-27,-50276692801141755e-28,11004392031956135e-29,33717632624009856e-29,-1392388722418162e-28,28534893807047445e-30,-5139111834242572e-31,-19752288294349442e-31],[-.001851851851851852,-.003472222222222222,.0026455026455026454,-.0009902263374485596,.00020576131687242798,-4018775720164609e-22,-18098550334489977e-21,764916091608111e-20,-16120900894563446e-22,4647127802807434e-24,1378633446915721e-22,-5752545603517705e-23,11951628599778148e-24,-17543241719747647e-27,-10091543710600413e-25,4162792991842583e-25,-856390702649298e-25,6067215101604758e-29,71624989648114856e-28,-2933186643771437e-27,5996696365683689e-28,-21671786527323313e-32,-4978339972369262e-29,20291628823713425e-30,-413125571381061e-29],[.004133597883597883,-.0026813271604938273,.0007716049382716049,20093878600823047e-22,-.00010736653226365161,52923448829120125e-21,-12760635188618728e-21,3423578734096138e-23,13721957309062932e-22,-6298992138380055e-22,14280614206064242e-23,-20477098421990866e-26,-1409252991086752e-23,6228974084922022e-24,-13670488396617112e-25,9428356159014678e-28,12872252400089318e-26,-55645956134363323e-27,1197593554636698e-26,-41689782251838634e-31,-10940640427884595e-28,4662239946390136e-28,-9905105763906907e-29,18931876768373515e-33,8859221872591127e-30],[.0006494341563786008,.00022947209362139917,-.0004691894943952557,.00026772063206283885,-7561801671883977e-20,-2396505113867297e-22,11082654115347302e-21,-56749528269915965e-22,14230900732435883e-22,-27861080291528143e-27,-16958404091930278e-23,8099464905388083e-23,-19111168485973655e-24,23928620439808118e-28,20620131815488797e-25,-9460496661855133e-25,21541049775774907e-26,-1388823336813903e-29,-21894761681963938e-27,9790998951171684e-27,-2178219188018096e-27,6208819573407901e-32,2126978363279737e-28,-9344688791517433e-29,2045367122678285e-29],[-.0008618882909167117,.0007840392217200666,-.0002990724803031902,-14638452578843418e-22,6641498215465122e-20,-3968365047179435e-20,11375726970678419e-21,2507497226237533e-25,-16954149536558305e-22,8907507532205309e-22,-2292934834000805e-22,2956794137544049e-26,28865829742708783e-24,-14189739437803219e-24,34463580499464896e-25,-23024517174528067e-29,-39409233028046403e-26,186023389685045e-24,-4356323005056618e-26,1278600101629623e-30,467927502665792e-26,-2149246470613483e-27,4908815614809652e-28,-633859148489156e-32,-5045332069080094e-29],[-.00033679855336635813,-6972813758365858e-20,.0002772753244959392,-.00019932570516188847,6797780477937208e-20,1419062920643967e-22,-13594048189768693e-21,8018470256334202e-21,-2291481176508095e-21,-3252473551298454e-25,34652846491085265e-23,-18447187191171344e-23,48240967037894184e-24,-17989466721743514e-30,-6306194500013523e-24,3162417628774568e-24,-7840924253697429e-25,5192679165254041e-30,9358944242306784e-26,-4513426216163278e-26,10799129993116828e-27,-3661886712685252e-32,-1210902069055155e-27,5680743584990564e-28,-13249659916340829e-29],[.0005313079364639922,-.0005921664373536939,.0002708782096718045,7902353232660328e-22,-8153969367561969e-20,561168275310625e-19,-18329116582843375e-21,-30796134506033047e-25,3465155368803609e-21,-20291327396058603e-22,5788792863149004e-22,2338630673826657e-28,-8828600746330484e-23,47435958880408125e-24,-12545415020710382e-24,8649648858010293e-29,16846058979264062e-25,-8575492823577594e-25,21598224929232125e-26,-7613230520476153e-31,-26639822008536144e-27,13065700536611057e-27,-31799163902367977e-28,4710976121367431e-33,36902800842763465e-29],[.00034436760689237765,5171790908260592e-20,-.00033493161081142234,.0002812695154763237,-.00010976582244684731,-12741009095484485e-23,27744451511563645e-21,-18263488805711332e-21,57876949497350525e-22,493875893393627e-24,-10595367014026043e-22,6166714376110408e-22,-17562973359060463e-23,-1297447328701544e-27,2695423606288966e-23,-14578352908731272e-24,3887645959386175e-24,-3881002251019412e-32,-5327994173877286e-25,27437977643314844e-26,-6995796092070568e-26,2589986387486848e-32,8856689099669639e-27,-4403168815871311e-27,10865561947091654e-28],[-.0006526239185953094,.0008394987206720873,-.000438297098541721,-6969091458420552e-22,.00016644846642067547,-.00012783517679769218,4629953263691304e-20,4557909867922708e-24,-10595271125805195e-21,6783342904865167e-21,-21075476666258803e-22,-17213731432817144e-27,3773587741611098e-22,-21867506700122867e-23,6220228804018927e-23,6597703826733e-28,-9590386497425686e-24,5213214492280807e-24,-13991589583935709e-25,5382058999060575e-31,19484714275467745e-26,-10127287556389682e-26,26077347197254926e-27,-5090418699993299e-33,-33721464474854593e-28],[-.0005967612901927463,-7204895416020011e-20,.0006782308837667328,-.0006401475260262758,.00027750107634328704,1819700838046515e-22,-8479507117068503e-20,6105192082501531e-20,-21073920183404862e-21,-8858589014125599e-25,45284535953805374e-22,-28427815022504407e-22,8708234177864641e-22,36886101871706966e-28,-1534469519070206e-22,8862466778790695e-23,-25184812301826817e-24,-10225912098215092e-30,3896947075815478e-24,-21267304792235634e-25,5737013552805138e-25,-1887749850169741e-34,-8093153869465787e-26,423827232834492e-25,-11002224534207727e-27],[.0013324454494800656,-.0019144384985654776,.0011089369134596636,99324041226423e-20,-.0005087450129309319,.00042735056665392886,-.000168588537679108,-81301893922785e-22,45284402370562144e-21,-3127053674781734e-20,1044986828530338e-20,48435226265680926e-27,-2148256587345626e-21,1329369701097492e-21,-4029569309210103e-22,-1756787766632329e-28,7014504316366825e-23,-4040787734999483e-23,11474026743371962e-24,39642746853563326e-34,-17804938269892715e-25,9748026254873165e-25,-26405338676507616e-26,5794875163403742e-33,3764774955354384e-26],[.001579727660730835,.00016251626278391583,-.0020633421035543276,.00213896861856891,-.0010108559391263003,-399127055299192e-21,.0003623502508476469,-.00028143901463712157,.00010449513336495887,212114184918303e-23,-25779417251947842e-21,17281818956040464e-21,-5641377387290428e-21,-11024320105776174e-27,11223224418895176e-22,-6869339637952674e-22,20653236975414888e-23,46714772409838506e-30,-35609886164949055e-24,20470855345905963e-24,-5809173863328336e-24,-1332821287582869e-31,9035460439133513e-25,-4959878251733084e-25,13481607129399748e-26],[-.004072512119514016,.00640336283380807,-.004041016108167662,-2183732802866233e-21,.002174044180125464,-.001970044051841889,.0008359546974796246,19445447567109655e-24,-.000257793871204217,.00019009987368139304,-6769649993743896e-20,-14440629666426571e-26,15712512518742267e-21,-10304008744776892e-21,3304517767401387e-21,7982976024232571e-28,-64097794149313e-20,38894624761300054e-23,-1161834764494887e-22,-2816808630596451e-30,19878012911297094e-24,-11407719956357511e-24,32355857064185554e-25,4175946829345594e-35,-5042311271810582e-25],[-.0059475779383993,-.0005401647678926045,.00879104135507679,-.009857631558785612,.005013469503102154,12807521786221875e-22,-.0020626019342754685,.0017109128573523059,-.000676953127141338,-6901154567656214e-24,.00018855128143995903,-.0001339521566349197,4626318303352804e-20,4003423061332135e-26,-10255652921494033e-21,6612086372797651e-21,-20913022027253007e-22,-20951775649603836e-29,3975602904199325e-22,-2395621197881589e-22,7118288338214586e-23,8925574873053455e-31,-12101547235064675e-24,6935061824833439e-24,-196614644538561e-23],[.01740202778752271,-.029527880945699123,.020045875571402798,70289515966903405e-22,-.012375421071343148,.011976293444235253,-.0054156038466518525,-6329089339641862e-23,.0018855118129005065,-.001473473274825001,.0005551581009770838,5240683441255066e-25,-.00014357913535784837,991812932249433e-19,-3346083474947831e-20,-3575583729109899e-27,71560851960630075e-22,-4551680262815553e-21,14236576649271474e-22,18803149082089665e-30,-2662340389892921e-22,15950642189595716e-23,-471875146738411e-22,-6510787295875518e-32,7979509102674624e-24],[.03024912416090589,.0024817436002649977,-.049939134373457025,.05991564300930787,-.03248320760162339,-5721296865210344e-21,.015085251778569354,-.013261324005088445,.0055515262632426145,3026318225703001e-23,-.0017229548406756724,.0012893570099929638,-.00046845138348319875,-1830259937893045e-25,.00011449739014822654,-7737856522124447e-20,25625836246985202e-21,10766165333192815e-28,-5324680928242262e-21,3349634863064464e-21,-10381253128684019e-22,-5608909920621128e-30,19150821930676592e-23,-11418365800203487e-23,33654425209171787e-24],[-.09905102088015905,.17954011706123485,-.12989606383463778,-31478872752284355e-21,.09051063527684813,-.0928288244111844,.04441211283987781,27779236316835886e-23,-.017229543805449696,.014182925050891573,-.005621416163374734,-239598509186381e-23,.0016029634366079909,-.0011606784674435774,.00041001337768153875,1836580075409066e-26,-958442565636559e-19,6364306233776471e-20,-2076250624489065e-20,-11806020912804483e-29,4213180823912065e-21,-2626224133701247e-21,8077062049493066e-22,6012591212363273e-31,-1472973737401884e-22],[-.19994542198219728,-.015056113040026424,.36470239469348487,-.4643519231173355,.26640934719197895,3403826602714719e-20,-.13784338709329624,.1276467178337056,-.056213828755200985,-1753150885483011e-22,.019235592956768112,-.015088821281095314,.005740185445135012,10622382710310225e-25,-.0015335082692563998,.0010819320643228215,-.0003737251019394566,-6617090972903199e-27,8426361738090962e-20,-5515070682748348e-20,1776953644834807e-20,3882792321020553e-29,-353513697488768e-20,2186583213004527e-21,-668128494476256e-21],[.7243860850402943,-1.3918010932653375,1.0654143352413967,.0001876173868950258,-.827055011761527,.8935243334782842,-.44971003995291337,-16107401567546651e-22,.1923559016527109,-.1659770216004261,.06888222268181433,13910091724608687e-24,-.02146911561508663,.016228980898865892,-.005979601617258426,-11287469112826745e-26,.0015167451119784856,-.00104786342935539,.0003553907288912642,8170432211180152e-28,-777730134424524e-19,5029141389700772e-20,-16035083867000518e-21,12469354315487606e-30,31369106244517616e-22],[1.6668949727276812,.1165462765994632,-3.3288393225018904,4.469232548286404,-2.6977693045875806,-.0002600667859891061,1.5389017615694538,-1.4937962361134611,.6888196463323315,13077482004552384e-22,-.2576296332559629,.21097676102125448,-.08371440835921988,-7792042888135476e-24,.0242679230648336,-.017813678334552312,.006397033038890006,4943080709048052e-26,-.0015554602758465635,.0010561196919903215,-.000352771844604729,9300233464502246e-29,7528585502655717e-20,-4818651556915635e-20,15227271505597605e-21],[-6.618829886137293,13.397985455142589,-10.789350606845145,-.0014352254537875018,9.23336945961898,-10.456552819547769,5.510552602903347,12024439690716742e-21,-2.5762961164755818,2.320744274538718,-1.0045728797216285,-10207833290021913e-23,.3397509217116947,-.2672051745075747,.10235252851562705,8432973048487163e-25,-.027998284958442594,.020066274144976814,-.007055436891508624,19402238183698188e-28,.0016562888105449611,-.0011082898580743682,.0003654545161310169,-5129003202697179e-26,-7634010369686903e-20],[-17.112706061976095,-1.1208044642899115,37.131966511885445,-52.29827102534896,33.058589696624615,.002479129897620022,-20.61089403411526,20.88672775145582,-10.045703956517752,-12238783449063012e-21,4.077013427422114,-3.473667358470195,1.4329352617312006,7135991441187971e-23,-.4479725715911561,.3411266608064446,-.12699786326594922,-2895367726908153e-25,.03312577627825986,-.023274087021036102,.008039999350364889,-1177805216235265e-24,-.001832162489107167,.0012108282933588664,-.0003947994124682252],[73.89033153567425,-156.80141270402274,132.2177542759164,.013692876877324546,-123.66496885920151,146.2068939106273,-80.36558772486535,-.00011259851148881298,40.77013219617994,-38.21034001327303,17.19522294277362,9351970795516835e-22,-6.271615990774704,5.116899907185264,-2.0319658112299095,-4950721558276154e-24,.596263972943326,-.44220765337238094,.16079998700166273,-24733786203223403e-24,-.040307574759979765,.02784905074709787,-.009475185899205422,6419922235909132e-21,.002125018077469946],[212.1683709838252,13.107863022633868,-496.9828593287175,731.215952669692,-482.1382172089085,-.028817248692894887,326.167203029471,-343.8934028008712,171.95193870816232,.00014038077378096157,-75.2594195897599,66.51969984520935,-28.447519748152462,-7613702615875392e-22,9.540223710530437,-7.517530111331138,2.894399756887196,-466121949995382e-21,-.8061514959879409,.5848300657063102,-.20845408972964957,.00014765818959305816,.05100043386375302,-.03306625214188366,.015109265210467774],[-989.5964309832236,2192.555536090523,-1928.3586782723355,-.15925738122215252,1956.9985945919857,-2407.2514765081555,1375.6149959336497,.0012920735237496668,-752.5941715948055,731.7166874220871,-341.37023466220063,-9985739026060805e-21,133.56313181291574,-112.76295161252794,46.31039609820446,-7923738713361476e-21,-14.510726927018647,11.111771248100563,-4.169081794527089,.0031008219800117806,1.1220095449981469,-.7605237992614992,.36262236505085255,.2216867741940747,.48683443692930506]];var N1=1e7,At=20,t2=200,yt=.3,vt=4.5,oe=4503599627370496,C=2220446049250313e-31,e0=2**-1022;function te(e,t,n){let o=se(e,t);if(n)return a1(e,t,!0)+Math.log(o);let s=a1(e,t,!1);return s>=e0?s*o:Math.exp(a1(e,t,!0)+Math.log(o))}function ne(e,t){let n=se(e,t),o=$0(e,t),s=o[0]+Math.log(n);return[s,o[1]>=e0?o[1]*n:Math.exp(s)]}function se(e,t){if(e>1e3&&t>.97*e)return Pt(e,t);let n=e,o=1,s=1;for(let r=0;r<N1&&(n+=1,o*=t/n,s+=o,!(o<=D*s));r++);return s}var St=2**-64;function Pt(e,t){let n=1,o=0,s=1,r=0;for(let i=1;i<N1;i++){let c=e+i,u=c-e,a=e-(c-u)+(i-u),f=t/c,l=134217729*f,h=l-(l-f),I=f-h;l=134217729*c;let S=l-(l-c),E=c-S,M=f*c,N=h*S-M+h*E+I*S+I*E,B=(t-M-N-f*a)/c;l=134217729*n;let j=l-(l-n),q=n-j,z=n*f,g=j*h-z+j*I+q*h+q*I+(n*B+o*f);n=z+g,o=g-(n-z);let P=s+n,A=P-s,F=s-(P-A)+(n-A)+(r+o);if(s=P+F,r=F-(s-P),n*f<=St*s*(1-f))break}return s+r}function _t(e,t,n){let o=re(e,t);if(n)return Math.log(e)+a1(e,t,!0)+Math.log(o);let s=a1(e,t,!1);return s>=e0?e*s*o:Math.exp(Math.log(e)+a1(e,t,!0)+Math.log(o))}function wt(e,t){let n=re(e,t),o=$0(e,t),s=Math.log(e)+o[0]+Math.log(n);return[s,o[1]>=e0?e*o[1]*n:Math.exp(s)]}function re(e,t){if(t>1e20)return Lt(e,t);if(e<Dt&&t<Et)return Ft(e,t);let n=1-e,o=t+n+1,s=0,r=1,i=t,c=t+1,u=o*t,a=c/u;for(let f=0;f<N1;f++){s+=1,n+=1,o+=2;let l=n*s,h=c*o-r*l,I=u*o-i*l,S;if(I!==0){let E=h/I;S=Math.abs((a-E)/E),a=E}else S=1;if(r=c,c=h,i=u,u=I,Math.abs(h)>oe&&(r*=C,c*=C,i*=C,u*=C),S<=D)break}return a}var Dt=4,Et=64;function Ft(e,t){let n=0,o=0,s=(g,P,A,F)=>{let _=g*A,y=134217729*g,H=y-(y-g),o1=g-H;y=134217729*A;let v=y-(y-A),X=A-v,G=H*v-_+H*X+o1*v+o1*X+(g*F+P*A);n=_+G,o=G-(n-_)},r=(g,P,A,F)=>{let _=g-A,y=_-g,H=g-(_-y)-(A+y)+(P-F);n=_+H,o=H-(n-_)},i=(g,P,A)=>{let F=g+A,_=F-g,y=g-(F-_)+(A-_)+P;n=F+y,o=y-(n-F)};r(1,0,e,0);let c=n,u=o;i(c,u,1),r(n,o,-t,0);let a=n,f=o,l=0,h=1,I=0,S=t,E=0;i(t,0,1);let M=n,N=o;s(a,f,t,0);let B=n,j=o,q=M/B,z=0;for(let g=0;g<N1;g++){l+=1,i(c,u,1),c=n,u=o,i(a,f,2),a=n,f=o,s(c,u,l,0);let P=n,A=o;s(M,N,a,f);let F=n,_=o;s(h,I,P,A),r(F,_,n,o);let y=n,H=o;s(B,j,a,f);let o1=n,v=o;s(S,E,P,A),r(o1,v,n,o);let X=n,G=o,s1=y/X;s(s1,0,X,G),r(y,H,n,o);let g1=(n+o)/X,$=s1+g1,d1=g1-($-s1),R1=q-$+(z-d1);if(q=$,z=d1,h=M,I=N,M=y,N=H,S=B,E=j,B=X,j=G,Math.abs(M)>oe&&(h*=C,I*=C,M*=C,N*=C,S*=C,E*=C,B*=C,j*=C),Math.abs(R1)<=2**-58*Math.abs($))break}return q+z}function Lt(e,t){let o=t+1-e,s=1/1e-300,r=1/o,i=r;for(let c=1;c<N1;c++){let u=-c*(c-e);o+=2,r=u*r+o,Math.abs(r)<1e-300&&(r=1e-300),s=o+u/s,Math.abs(s)<1e-300&&(s=1e-300),r=1/r;let a=r*s;if(i*=a,Math.abs(a-1)<=D)break}return i}function ce(e,t){let n=1,o=0;for(let c=1;c<N1;c++){n*=-t/c;let u=n/(e+c);if(o+=u,Math.abs(u)<=D*Math.abs(o))break}let s=Math.log(t),r=e*s-e1(e);return-Math.expm1(r)-e*Math.exp(r)*o}function Rt(e,t){let n=t/e,o=(t-e)/e;return n>1?Math.sqrt(-2*x0(o)):n<1?-Math.sqrt(-2*x0(o)):0}function kt(e,t){let n=new Array(25).fill(0);n[0]=1;let o=0,s=0,r=1,i=1/0;for(let c=0;c<25;c++){let u=x3[c],a=u[0];for(let h=1;h<25;h++){h>o&&(n[h]=t*n[h-1],o+=1);let I=u[h]*n[h];if(a+=I,Math.abs(I)<D*Math.abs(a))break}let f=a*r,l=Math.abs(f);if(l>i||(s+=f,l<D*Math.abs(s)))break;i=l,r/=e}return s}function ie(e,t){return e>At&&e<t2&&t<yt||e>t2&&t<vt/Math.sqrt(e)}var ue=1e299,ae=(e,t)=>e>t2&&(t<.001||e>ue);function Gt(e,t){let n=b1(e,[t,0]),o=t>e?1:-1,s=Rt(e,t),r=e>ue&&Math.abs(s)>.1?1/((t-e)/e)-1/s:kt(e,s),i=.5*R3(Math.sqrt(n[0]+n[1]))+o*r/Math.sqrt(2*Math.PI*e);return[-n[0]-n[1]+Math.log(i),n[0]>2e3?0:Math.exp(-n[0])*(1-n[1])*i]}function fe(e,t,n){let[o,s]=Gt(e,t);return t>e!==n?[o,s]:[Math.log1p(-s),1-s]}function e2(e,t,n){if(t>1.1)return t<e?{lower:!0,value:te(e,t,n)}:{lower:!1,value:_t(e,t,n)};if(t<=.5?-.4/Math.log(t)<e:t*1.1<e)return{lower:!0,value:te(e,t,n)};let s=ce(e,t);return{lower:!1,value:n?Math.log(s):s}}function Tt(e,t){if(t>1.1){let[s,r]=t<e?ne(e,t):wt(e,t);return{lower:t<e,log:s,lin:r}}if(t<=.5?-.4/Math.log(t)<e:t*1.1<e){let[s,r]=ne(e,t);return{lower:!0,log:s,lin:r}}let o=ce(e,t);return{lower:!1,log:Math.log(o),lin:o}}var n1=1e-300;function Ot(e,t,n){if(Number.isNaN(e)||Number.isNaN(t))return[NaN,NaN];if(t<0||e<0)return[NaN,NaN];if(e===0)return t>0?n?[0,1]:[-1/0,0]:[NaN,NaN];if(t===0)return n?[-1/0,0]:[0,1];if(e===1/0)return t===1/0?[NaN,NaN]:n?[-1/0,0]:[0,1];if(t===1/0)return n?[0,1]:[-1/0,0];if(t>1e15*Math.max(e,1)){let r=(e-1)*Math.log(t)-t-W(e)+Math.log1p((e-1)/t);return n?[Math.log1p(-Math.exp(r)),-Math.expm1(r)]:[r,Math.exp(r)]}if(!n&&e<n1){let[r,i]=Ot(n1,t,!1);return[r+Math.log(e/n1),i*(e/n1)]}let o=Math.abs(t-e)/e;if(ie(e,o)||ae(e,o))return fe(e,t,n);let s=Tt(e,t);return s.lower===n?[s.log,s.lin]:[s.lin>0?Math.log1p(-s.lin):-Math.exp(s.log),1-s.lin]}function Ct(e,t,n,o){if(Number.isNaN(e)||Number.isNaN(t))return NaN;if(t<0||e<0)return NaN;let s=o?-1/0:0,r=o?0:1;if(e===0)return t>0?n?r:s:NaN;if(t===0)return n?s:r;if(e===1/0)return t===1/0?NaN:n?s:r;if(t===1/0)return n?r:s;if(t>1e15*Math.max(e,1)){let u=(e-1)*Math.log(t)-t-W(e)+Math.log1p((e-1)/t);return n?o?Math.log1p(-Math.exp(u)):-Math.expm1(u):o?u:Math.exp(u)}if(!n&&e<n1){let u=Ct(n1,t,!1,o);return o?u+Math.log(e/n1):u*(e/n1)}let i=Math.abs(t-e)/e;if(ie(e,i)||ae(e,i))return fe(e,t,n)[o?0:1];if(o){let u=e2(e,t,!0);if(u.lower===n)return u.value;let a=e2(e,t,!1).value;return a>0?Math.log1p(-a):-Math.exp(u.value)}let c=e2(e,t,!1);return c.lower===n?c.value:1-c.value}export{D as a,L1 as b,O3 as c,t1 as d,K as e,W as f,e1 as g,k as h,b1 as i,A6 as j,y6 as k,v6 as l,a1 as m,x0 as n,se as o,Pt as p,re as q,Ot as r,Ct as s,c6 as t,i6 as u,Z1 as v,at as w,M6 as x,m6 as y,H3 as z,X0 as A,F6 as B,L6 as C,$3 as D};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! rStats.js 1.
|
|
2
|
-
import{a as b}from"./chunk-
|
|
1
|
+
/*! rStats.js 1.18.0 | MIT License | includes code ported from SciPy, Boost.Math, statsmodels, LAPACK and others: see THIRD_PARTY_NOTICES.md at https://github.com/numero-uno-jpn/rStats.js */
|
|
2
|
+
import{a as b}from"./chunk-MXZ64QME.js";import{d as h,e as f}from"./chunk-XQCHC3SH.js";import{b as c}from"./chunk-KJMLQHCY.js";function w(t){let r=h(t);return r.some(e=>typeof e=="string")?r.map(e=>typeof e=="number"&&!Number.isNaN(e)?f(e):e):r}function N(t,r){return typeof t=="number"&&typeof r=="number"?t-r:b(String(t),String(r))}function d(t,r,e="drop"){if(r===void 0){let n=t;if(n.length>0&&typeof n[0]=="number")throw new TypeError("'g' is required when 'x' is a single sample");let u=n.map(i=>Array.from(i).filter(p=>!Number.isNaN(p)));if(e==="error"&&u.some(i=>i.length===0))throw new RangeError("all groups must contain data");let s=[],a=[];return u.forEach((i,p)=>{i.length!==0&&(s.push(String(p+1)),a.push(i))}),{labels:s,groups:a}}let o=Array.from(t);if(o.length!==r.length)throw new RangeError("'x' and 'g' must have the same length");let l=w(r),m=new Map;for(let n=0;n<o.length;n++){let u=o[n],s=l[n];if(Number.isNaN(u)||typeof s=="number"&&Number.isNaN(s))continue;let a=m.get(s);a===void 0&&m.set(s,a=[]),a.push(u)}let g=Array.from(m.keys()).sort(N);return{labels:g.map(f),groups:g.map(n=>m.get(n))}}function E(t,r,e="drop"){return d(t,r,e).groups}function R(t){let r=[...t].sort((o,l)=>o-l),e=r.length;return e%2===1?r[(e-1)/2]:c(r[e/2-1],r[e/2])}function A(t){if(t.length===0)throw new RangeError("'y' must have at least one row");let r=t[0].length;if(t.some(e=>e.length!==r))throw new RangeError("all rows of 'y' must have the same length");return t.filter(e=>e.every(o=>!Number.isNaN(o))).map(e=>Array.from(e))}export{w as a,N as b,d as c,E as d,R as e,A as f};
|