Run an Independent-Samples t-Test with Automated Narrative Reporting
Source:R/easy_ttest.R
easy_ttest.RdExecutes a two-sample (or one-sample) t-test using stats::t.test(),
extracts key metrics via broom, and generates a plain-language
narrative via the Narrative Generator Module.
Usage
easy_ttest(
x,
y = NULL,
data = NULL,
mu = 0,
var.equal = TRUE,
conf.level = 0.95,
alpha = 0.05
)Arguments
- x
A numeric vector, OR a formula of the form
outcome ~ groupwhendatais provided.- y
A numeric vector (second group) when
xis not a formula. Ignored whenxis a formula.- data
A data frame. Required when
xis a formula.- mu
Null hypothesis value for the mean (one-sample test). Default
0.- var.equal
Logical; assume equal variances? Default
TRUE(classical pooled-variance t-test). Set toFALSEto use Welch's t-test instead.- conf.level
Confidence level. Default
0.95.- alpha
Significance threshold for narrative. Default
0.05.
Value
An object of class "easystat_result" with:
test_typeCharacter:
"ttest"formula_strDescription of the comparison
raw_modelThe raw
htestobjectcoefficients_tableGroup means and confidence interval
model_fit_tablet-statistic, df, and p-value
explanationPlain-language narrative string
Examples
result <- easy_ttest(mpg ~ am, data = mtcars)
print(result)
#>
#> ================================================================================
#> EasyStat Result :: TTEST
#> ================================================================================
#>
#> TABLE 1 — MAIN RESULTS
#> --------------------------------------------------------------------------------
#> Metric Value
#> Mean — 0 17.1474
#> Mean — 1 24.3923
#> Mean Difference (larger − smaller) 7.2449
#> 95% CI (lower) 3.6415
#> 95% CI (upper) 10.8484
#> n — 0 19.0000
#> n — 1 13.0000
#>
#> TABLE 2 — MODEL FIT / SUMMARY
#> --------------------------------------------------------------------------------
#> Metric Value
#> |t-statistic| 4.1061
#> Degrees of Freedom (used) 30
#> Degrees of Freedom (equal variance, n1+n2-2) 30
#> Variance Assumption Equal variances assumed
#> p-value 0.0003
#>
#> ================================================================================
#> PLAIN-LANGUAGE INTERPRETATION
#> ================================================================================
#>
#> INDEPENDENT-SAMPLES t-TEST Comparison: mpg ~ am
#>
#> An independent-samples t-test revealed a highly statistically significant (p
#> = 0.0003) difference between the two groups (t(30) = -4.106). The mean for
#> '0' was 17.1474 and the mean for '1' was 24.3923. The 95% confidence
#> interval for the difference in means ranged from 3.6415 to 10.8484. These
#> results provide statistically significant evidence that '0' and '1' differ
#> meaningfully on the measured variable.
#>
#> ================================================================================
#>