Performs a Bayesian or Frequentist two-sample t-test using RTMB.
Usage
rtmb_ttest(
x,
y = NULL,
data = NULL,
r = 0.707,
paired = FALSE,
ID = NULL,
y_range = NULL,
prior = prior_flat(),
init = NULL,
fixed = NULL,
var.equal = TRUE,
missing = c("listwise", "fiml"),
WAIC = FALSE,
...
)Arguments
- x
Numeric vector of responses for group 1, a formula (e.g., `y ~ group`), or a column name (unquoted) if `data` is provided.
- y
Numeric vector of responses for group 2, or a column name (unquoted) if `data` is provided. Required if `x` is not a formula.
- data
Data frame containing the variables.
- r
Numeric; Cauchy prior scale for the effect size (delta). Default is 0.707.
- paired
Logical; whether to perform a paired t-test.
- ID
Character; name of the ID variable for paired t-tests (required for formula input with paired = TRUE).
- y_range
Theoretical minimum and maximum values of the response variable as a vector c(min, max). Required when using weakly informative priors.
- prior
An object of class `"rtmb_prior"`. Use `prior_flat()` for no prior, `prior_normal()` for default normal/exponential priors, or `prior_weak()` for weakly informative Bayesian inference. Default is `prior_flat()`.
- init
List of initial values.
- fixed
Optional named list of fixed values for specific parameters.
- var.equal
Logical; whether to assume equal variances. Default is TRUE.
- missing
Missing value handling strategy: "listwise".
- WAIC
Logical; if TRUE, add pointwise `log_lik` to the generate block for WAIC.
- ...
Additional arguments.
Details
For classic inference, heteroscedastic two-sample t-tests use the same RTMB Satterthwaite machinery as `optimize(marginal = ..., df_method = "satterthwaite")`. The result is model-based and reproducible from the printed model code. This corresponds to the Welch-type unequal-variance t-test, but the degrees of freedom are computed by the package's internal Satterthwaite procedure rather than by a separate closed-form formula. When `prior_jzs()` is combined with `var.equal = FALSE`, BayesRTMB uses a Welch-style JZS extension: the effect size `delta` is an explicit parameter with a Cauchy prior, and the group mean difference is scaled by the root-mean-square of the two group standard deviations.
Examples
# Simulate two-sample data with a true effect size
set.seed(123)
y1 <- rnorm(30, mean = 0.5, sd = 1)
y2 <- rnorm(30, mean = 0.0, sd = 1)
# Fit the Bayesian two-sample t-test model
# r = 0.707 is the standard scale for the Cauchy prior on the effect size
fit_ttest <- rtmb_ttest(y1, y2, prior = prior_jzs(r = 0.707))
#> Pre-checking model code...
#> Checking RTMB setup...
# \donttest{
# MCMC sampling (chains and iterations reduced for faster execution)
mcmc_ttest <- fit_ttest$sample(sampling = 500, warmup = 500, chains = 2)
#> Starting sequential sampling (chains = 2)...
#> chain 1 started...
#> chain 1: iter 200 warmup
#> chain 1: iter 400 warmup
#> chain 1: iter 600 sampling
#> chain 1: iter 800 sampling
#> chain 1: iter 1000 sampling
#> chain 2 started...
#> chain 2: iter 200 warmup
#> chain 2: iter 400 warmup
#> chain 2: iter 600 sampling
#> chain 2: iter 800 sampling
#> chain 2: iter 1000 sampling
#> Calculating transformed parameters...
#>
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
mcmc_ttest$summary()
#> variable mean sd map q2.5 q97.5 ess_bulk ess_tail rhat
#> lp -81.12 1.31 -80.15 -84.49 -79.64 355 407 1.00
#> diff 0.24 0.23 0.24 -0.21 0.70 536 503 1.00
#> delta 0.26 0.24 0.26 -0.23 0.73 533 492 1.00
#> total_mean 0.32 0.12 0.32 0.10 0.56 954 444 1.00
#> sd 0.92 0.09 0.89 0.77 1.11 1218 763 1.00
#> mean0 0.44 0.17 0.46 0.10 0.76 721 653 1.00
#> mean1 0.20 0.16 0.21 -0.12 0.52 718 476 1.00
# }