| Title: | Enhanced T-Test with Automatic Selection and APA Formatting |
| Version: | 0.1.0 |
| Description: | Provides enhanced t-test functionality with automatic assumption checking, non-parametric fallbacks, effect size calculation, and APA-formatted tables that can be exported to Word documents. Supports one-sample, two-sample, and paired t-tests with intelligent test selection. Effect size interpretations follow Cohen (1988) <doi:10.4324/9780203771587> and APA formatting follows the Publication Manual of the American Psychological Association, 7th edition (2020) <doi:10.1037/0000165-000>. |
| License: | GPL-3 |
| Encoding: | UTF-8 |
| Depends: | R (≥ 3.5.0) |
| Imports: | car (≥ 3.0-0), dplyr (≥ 1.0.0), flextable (≥ 0.7.0), htmltools (≥ 0.5.0), nortest (≥ 1.0-4), officer (≥ 0.4.0), rlang (≥ 1.0.0), stats, utils |
| Suggests: | ggplot2, knitr, rmarkdown, testthat (≥ 3.0.0) |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| Config/roxygen2/version: | 8.1.0 |
| NeedsCompilation: | no |
| Packaged: | 2026-10-15 12:47:34 UTC; DELL |
| Author: | Martin Nyamu [aut, cre] |
| Maintainer: | Martin Nyamu <analystnyamu79@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-10-27 15:20:07 UTC |
enhancedTtest: Enhanced T-Test with Automatic Selection and APA Formatting
Description
Provides enhanced t-test functionality with automatic assumption checking, non-parametric fallbacks, effect size calculation, and APA-formatted tables that can be exported to Word documents. Supports one-sample, two-sample, and paired t-tests with intelligent test selection. Effect size interpretations follow Cohen (1988) doi:10.4324/9780203771587 and APA formatting follows the Publication Manual of the American Psychological Association, 7th edition (2020) doi:10.1037/0000165-000.
Author(s)
Maintainer: Martin Nyamu analystnyamu79@gmail.com
Authors:
Martin Nyamu analystnyamu79@gmail.com
Calculate Effect Size for Any Test
Description
Calculate Effect Size for Any Test
Usage
calculate_effect_size(result)
Arguments
result |
Test result object |
Value
Effect size with interpretation
Complete Assumption Checking for All Test Types
Description
Complete Assumption Checking for All Test Types
Usage
check_complete_assumptions(data, response_var, group_var, test_type)
Arguments
data |
Data frame |
response_var |
Name of response variable |
group_var |
Name of grouping variable (NULL for one-sample) |
test_type |
Character, "one_sample", "two_sample", or "paired" |
Value
List with assumption check results
Check Homogeneity of Variance
Description
Check Homogeneity of Variance
Usage
check_homogeneity(
data,
response_var,
group_var,
method = "levene",
alpha = 0.05
)
Arguments
data |
Data frame |
response_var |
Name of response variable |
group_var |
Name of grouping variable |
method |
Character, "levene" or "bartlett" |
alpha |
Significance level |
Value
List with variance homogeneity results
Check Normality with Multiple Methods
Description
Check Normality with Multiple Methods
Usage
check_normality(x, method = "shapiro", alpha = 0.05)
Arguments
x |
Numeric vector |
method |
Character, "shapiro", "anderson", or "both" |
alpha |
Significance level |
Value
List with normality check results
Complete T-Test Analysis with Automatic Selection
Description
Performs one-sample, two-sample, or paired t-tests with automatic non-parametric fallbacks when assumptions are violated. Handles input validation, assumption checking, effect size calculation, and produces APA-formatted output.
Usage
complete_ttest(
data,
formula,
paired = FALSE,
alternative = "two.sided",
mu = 0,
auto_select = TRUE,
apa_table = TRUE,
export_word = FALSE,
filename = "ttest_results.docx"
)
Arguments
data |
A data frame containing the variables. |
formula |
A formula specifying the model:
|
paired |
Logical, whether to perform a paired t-test
(default |
alternative |
Character string specifying the alternative hypothesis.
Must be one of |
mu |
Numeric, the hypothesized mean for one-sample tests
(default |
auto_select |
Logical, if |
apa_table |
Logical, if |
export_word |
Logical, if |
filename |
Character string, output filename for Word export.
Only used when |
Value
An object of class complete_ttest (also inheriting from
enhanced_ttest), which is a named list containing the following
components:
rawThe raw test result object returned by
t.testorwilcox.test. Contains the test statistic, degrees of freedom, p-value, confidence interval, and estimates.methodCharacter string naming the test performed (e.g.,
"Welch's t-test","Mann-Whitney U test").parametricLogical;
TRUEif a parametric test was used,FALSEif a non-parametric alternative was selected.test_typeCharacter string indicating the test type:
"one_sample","two_sample", or"paired".response_varCharacter string with the name of the response variable from the formula.
group_varCharacter string with the name of the grouping variable from the formula, or
NULLfor one-sample tests.dataThe processed data frame used in the analysis, after removing missing values and converting the grouping variable to a factor (if applicable).
alternativeCharacter string with the alternative hypothesis used.
muNumeric value of the hypothesized mean (for one-sample tests).
descriptive_statsData frame with descriptive statistics by group (or overall for one-sample tests). Contains columns:
group,N,Mean,SD,SEfor parametric tests; orgroup,N,Median,IQRfor non-parametric tests.effect_sizeA named list describing the effect size with elements:
-
value: Numeric effect size value. -
name: Character string, either"Cohen's d"(parametric) or"r"(non-parametric). -
interpretation: Character string describing the magnitude (e.g.,"Small","Medium","Large").
-
assumptionsA named list with results of assumption checks, including
normality_passed,normality_p_value,variance_passed,variance_p_value, pluswarningsandrecommendationscharacter vectors.apa_tableA
flextableobject containing the publication-ready APA-formatted table, orNULLifapa_table = FALSEor table creation failed.recommendationCharacter string with a plain-language summary of the analysis, including the test method used, assumption check results, significance, and effect size interpretation.
callThe matched function call, for reproducibility.
The object has S3 methods print.complete_ttest and
summary.complete_ttest for displaying results.
See Also
create_complete_apa_table for building APA tables,
export_apa_to_word for Word export,
format_p_value for formatting p-values.
Examples
# One-sample t-test: test if mean mpg differs from 20
result <- complete_ttest(mtcars, mpg ~ 1, mu = 20)
print(result)
# Two-sample t-test: compare Sepal.Length between two species
iris_subset <- iris[iris$Species != "virginica", ]
iris_subset$Species <- droplevels(iris_subset$Species)
result <- complete_ttest(iris_subset, Sepal.Length ~ Species)
summary(result)
# Access components
result$method # Test name
result$raw$p.value # p-value
result$effect_size # Effect size with interpretation
result$apa_table # APA-formatted flextable
Create APA Table (Alias)
Description
Alias for create_complete_apa_table.
Usage
create_apa_table(result)
Arguments
result |
A |
Value
A flextable object
Create APA Table for complete_ttest Object
Description
Creates a publication-ready APA-formatted table from a complete_ttest
result object. Automatically selects the correct table format based on
the test type (one-sample, two-sample, or paired) and whether the test
was parametric or non-parametric.
Usage
create_complete_apa_table(result)
Arguments
result |
A |
Value
A flextable object, or NULL if descriptive
statistics are not available
Examples
result <- complete_ttest(
iris[iris$Species != "virginica", ],
Sepal.Length ~ Species
)
create_complete_apa_table(result)
Create Complete Report in Word
Description
Generates a comprehensive Word document containing descriptive statistics, inferential tests, and interpretations for multiple outcome variables.
Usage
create_complete_report(
data,
formula_list,
group_var,
paired = FALSE,
title = "Statistical Analysis Report",
filename = "apa_report.docx",
include_descriptives = TRUE,
include_interpretations = TRUE
)
Arguments
data |
A data frame containing the variables |
formula_list |
A list of formulas (e.g., |
group_var |
Character, name of the grouping variable |
paired |
Logical, whether to perform paired tests |
title |
Character, report title |
filename |
Character, output filename (should end in .docx) |
include_descriptives |
Logical, include descriptive statistics table |
include_interpretations |
Logical, include APA interpretations |
Value
Invisibly returns the path to the created file
Examples
formulas <- list(
Sepal.Length ~ Species,
Sepal.Width ~ Species
)
tmp <- tempfile(fileext = ".docx")
data_subset <- iris[iris$Species != "virginica", ]
data_subset$Species <- droplevels(data_subset$Species)
create_complete_report(
data = data_subset,
formula_list = formulas,
group_var = "Species",
filename = tmp
)
Export APA Table to HTML
Description
Exports an APA-formatted flextable to an HTML file.
Usage
export_apa_to_html(
ft,
filename = "ttest_results.html",
include_interpretation = TRUE,
interpretation_text = NULL
)
Arguments
ft |
A flextable object containing the APA-formatted table |
filename |
Character, output filename (should end in .html) |
include_interpretation |
Logical, include interpretation section |
interpretation_text |
Character, interpretation text to include |
Value
Invisibly returns the path to the created file
Examples
result <- complete_ttest(
iris[iris$Species != "virginica", ],
Sepal.Length ~ Species
)
tmp <- tempfile(fileext = ".html")
export_apa_to_html(result$apa_table, tmp)
Export APA Table to Word
Description
Exports an APA-formatted flextable to a Word document with proper
formatting. Attempts to use flextable::save_as_docx() first,
with a fallback to officer for older flextable versions.
Usage
export_apa_to_word(
apa_table,
filename = "ttest_results.docx",
title = "T-Test Results"
)
Arguments
apa_table |
A flextable object containing the APA-formatted table |
filename |
Character, output filename (should end in .docx) |
title |
Character, optional title for the document |
Value
Invisibly returns the path to the created file
Examples
result <- complete_ttest(
iris[iris$Species != "virginica", ],
Sepal.Length ~ Species
)
tmp <- tempfile(fileext = ".docx")
export_apa_to_word(result$apa_table, tmp)
Format p-value for APA style
Description
Format p-value for APA style
Usage
format_p_value(p, include_stars = TRUE)
Arguments
p |
Numeric p-value |
include_stars |
Logical, include significance stars (default TRUE) |
Value
Character string with formatted p-value
Examples
format_p_value(0.0001)
format_p_value(0.03)
format_p_value(0.5)
Generate random test data
Description
Generate random test data
Usage
generate_test_data(
n = 50,
mean = 10,
sd = 2,
group1_name = "A",
group2_name = "B",
seed = 123
)
Arguments
n |
Sample size per group (default 50) |
mean |
Mean of first group (default 10) |
sd |
Standard deviation (default 2) |
group1_name |
Name of first group (default "A") |
group2_name |
Name of second group (default "B") |
seed |
Random seed (default 123) |
Value
A data frame with columns value and group
Examples
df <- generate_test_data()
Interpret Cohen's d
Description
Interpret Cohen's d
Usage
interpret_cohens_d(d)
Arguments
d |
Numeric Cohen's d value |
Value
Character string with interpretation
Examples
interpret_cohens_d(0.3)
Interpret r (for non-parametric tests)
Description
Interpret r (for non-parametric tests)
Usage
interpret_r(r)
Arguments
r |
Numeric r value |
Value
Character string with interpretation
Examples
interpret_r(0.4)
Print method for complete_ttest objects
Description
Formats and prints a complete_ttest object to the console in a
human-readable format. The output includes the test type, method used,
assumption check results, descriptive statistics, test statistics,
effect size, and (if present) the APA-formatted table.
Usage
## S3 method for class 'complete_ttest'
print(x, ...)
Arguments
x |
A |
... |
Additional arguments passed to |
Value
Invisibly returns the input object x, unchanged. This function
is called primarily for its side effect of printing a formatted
summary of the t-test analysis to the console.
The printed output includes:
-
Test information: test type (
"one_sample","two_sample", or"paired"), test method name, whether a parametric or non-parametric test was used, and the variable names. -
Assumption checks: results of normality and homogeneity of variance tests, plus any recommendations (only shown if assumption checks were performed).
-
Descriptive statistics: sample size, mean, standard deviation, and standard error by group (or overall for one-sample tests). For non-parametric tests, median and interquartile range are shown instead.
-
Test results: test statistic, degrees of freedom, p-value, and 95\
-
Effect size: the effect size value, name (Cohen's d or r), and magnitude interpretation (e.g., "Small", "Medium", "Large").
-
APA-formatted table: if
x$apa_tableis notNULL, the publication-ready table is printed inline. -
Summary: a plain-language summary combining the method used, assumption results, significance, and effect size.
See Also
summary.complete_ttest for a condensed summary with an
APA-style interpretation, complete_ttest for the main
analysis function.
Examples
result <- complete_ttest(mtcars, mpg ~ 1, mu = 20)
print(result)
Summary method for complete_ttest objects
Description
Provides a condensed summary of a complete_ttest object,
including all the information shown by print.complete_ttest
plus a plain-language, APA-style interpretation of the results.
Usage
## S3 method for class 'complete_ttest'
summary(object, ...)
Arguments
object |
A |
... |
Additional arguments passed to |
Value
Invisibly returns the input object object, unchanged. This
function is called primarily for its side effect of printing a
detailed summary of the t-test analysis to the console.
The printed output includes everything from
print.complete_ttest plus:
-
APA-style interpretation: a complete sentence describing the test performed, the variables compared, the statistical result (with test statistic, degrees of freedom, and p-value), and the effect size with its interpretation. This text is suitable for direct inclusion in a research report or manuscript.
See Also
print.complete_ttest, complete_ttest.
Examples
result <- complete_ttest(mtcars, mpg ~ 1, mu = 20)
summary(result)