Run multiple t-tests by groups on tidy data
Source:R/t_test_all_tidy_grouped.r
t_test_all_tidy_grouped.RdA wrapper around t_test_all_tidy that performs t-tests
separately for each combination of grouping variables in a tidy dataset.
Optionally, it provides Holm's step-down family-wise error control by
returning per-hypothesis critical alpha thresholds.
Usage
t_test_all_tidy_grouped(
dataset,
n_group_col = 1,
paired = F,
var.equal = FALSE,
onesample = FALSE,
mu = 0,
ci = c("freq", "bayes_central", "bayes_hdi"),
alternative = c("two.sided", "less", "greater"),
conf.level = 0.95,
alpha = 0.05,
holm = FALSE,
pd = FALSE,
bf = FALSE,
cor = TRUE,
mean_x_EAP = FALSE,
mean_x_MAP = FALSE,
mean_x_MED = FALSE,
diff_EAP = FALSE,
diff_MAP = FALSE,
diff_MED = FALSE,
cohens_d = NULL,
cohens_d_EAP = FALSE,
cohens_d_MAP = FALSE,
cohens_d_MED = FALSE,
cohens_dz = TRUE,
cohens_dz_EAP = FALSE,
cohens_dz_MAP = FALSE,
cohens_dz_MED = FALSE,
rscale_est = Inf,
rscale_bf = "medium",
iterations = 10000,
map_density_n = 512,
show_design = TRUE,
detailed = FALSE,
fullbayes = FALSE
)Arguments
- dataset
A tidy data frame containing participant ID, one or more grouping variables, an independent variable with exactly two levels, and a numeric dependent variable (in that order).
- n_group_col
Integer. The number of grouping columns (default = 1).
- paired
Logical. If
TRUE, a paired t-test is performed (defaultFALSE).- var.equal
Logical. If
TRUE, the two-sample t-test assumes equal variances (defaultFALSE).- onesample
Logical. If
TRUE, a one-sample t-test is performed (defaultFALSE).- mu
Numeric. Null hypothesis value for the mean difference (default
0).- ci
Character vector specifying interval type(s):
"freq","bayes_central", or"bayes_hdi". Default isc("freq","bayes_central","bayes_hdi"). Passed tot_test_all.- alternative
Character. Alternative hypothesis:
"two.sided","less", or"greater". Default is"two.sided". Passed tot_test_all.- conf.level
Numeric. Confidence/credibility level (default
0.95). Passed tot_test_all.- alpha
Numeric in (0, 1). Family-wise significance level used for Holm's method when
holm = TRUE. Default is0.05.- holm
Logical. If
TRUEand the output contains apcolumn, analphacolumn is (re)computed using Holm's method viap_to_holmalphawithsig.level = alpha. This affects only the per-hypothesis critical alpha thresholds, not the p-values. Default isFALSE.- pd, bf, cor
Logical flags. Whether to compute probability of direction (pd), Bayes factors, or correlation (for paired samples). Passed to
t_test_all.- mean_x_EAP, mean_x_MAP, mean_x_MED
Logical. Report posterior summaries for group means. Passed to
t_test_all.- diff_EAP, diff_MAP, diff_MED
Logical. Report posterior summaries for the mean difference. Passed to
t_test_all.- cohens_d
Logical or character. Type/request for Cohen's d in independent samples. Passed to
t_test_all.- cohens_d_EAP, cohens_d_MAP, cohens_d_MED
Logical. Posterior summaries for Cohen's d. Passed to
t_test_all.- cohens_dz
Logical. Request Cohen's dz for paired samples. Passed to
t_test_all.- cohens_dz_EAP, cohens_dz_MAP, cohens_dz_MED
Logical. Posterior summaries for Cohen's dz. Passed to
t_test_all.- rscale_est, rscale_bf
Numeric or character. Cauchy prior scales for estimation and Bayes factors (e.g.,
"ultrawide","wide","medium", or a positive number). Defaults areInfand"medium", respectively. Passed tot_test_all.- iterations, map_density_n
Integer. MCMC iterations and grid size for MAP density. Passed to
t_test_all.- show_design
Logical. If
TRUE, show message of design (defaultTRUE).- detailed
Logical. Whether to return detailed results (
TRUE) or minimal output (FALSE, default).- fullbayes
Logical. Whether to show only Bayesian results (
TRUE) or both frequentist and Bayesian results (FALSE, default).
Value
A data frame with t-test results for each group, matching the
structure of t_test_all_tidy() and, if requested, including
Holm-based critical alpha thresholds in alpha.
Details
The function validates the dataset layout, groups by the specified
grouping variables, and applies t_test_all_tidy() within each
group. When holm = TRUE, it replaces/creates an alpha
column containing Holm step-down critical alpha values corresponding to
the reported p values (per group), controlling the family-wise
error rate at alpha. P-values themselves are not adjusted.
Examples
# Example with one grouping variable:
head(data_fict_mixed)
#> # A tibble: 6 × 4
#> participant gender condition rt
#> <dbl> <chr> <chr> <dbl>
#> 1 1 woman control 679
#> 2 1 woman experimental 900
#> 3 2 woman control 712
#> 4 2 woman experimental 1047
#> 5 3 woman control 781
#> 6 3 woman experimental 1056
t_test_all_tidy_grouped(data_fict_mixed, paired = TRUE, n_group_col = 1)
#> design: paired
#> gender diff t df p alpha sig cohens_dz
#> 1 man -34.00 -0.43 3 0.699 0.05 ns -0.213
#> 2 woman -277.25 -11.91 3 0.001 0.05 * -5.954