| Title: | 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>. |
| Authors: | Martin Nyamu [aut, cre] |
| Maintainer: | Martin Nyamu <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-10-28 11:34:09 UTC |
| Source: | https://github.com/cran/enhancedTtest |
Calculate Effect Size for Any Test
calculate_effect_size(result)calculate_effect_size(result)
result |
Test result object |
Effect size with interpretation
Complete Assumption Checking for All Test Types
check_complete_assumptions(data, response_var, group_var, test_type)check_complete_assumptions(data, response_var, group_var, test_type)
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" |
List with assumption check results
Check Homogeneity of Variance
check_homogeneity( data, response_var, group_var, method = "levene", alpha = 0.05 )check_homogeneity( data, response_var, group_var, method = "levene", alpha = 0.05 )
data |
Data frame |
response_var |
Name of response variable |
group_var |
Name of grouping variable |
method |
Character, "levene" or "bartlett" |
alpha |
Significance level |
List with variance homogeneity results
Check Normality with Multiple Methods
check_normality(x, method = "shapiro", alpha = 0.05)check_normality(x, method = "shapiro", alpha = 0.05)
x |
Numeric vector |
method |
Character, "shapiro", "anderson", or "both" |
alpha |
Significance level |
List with normality check results
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.
complete_ttest( data, formula, paired = FALSE, alternative = "two.sided", mu = 0, auto_select = TRUE, apa_table = TRUE, export_word = FALSE, filename = "ttest_results.docx" )complete_ttest( data, formula, paired = FALSE, alternative = "two.sided", mu = 0, auto_select = TRUE, apa_table = TRUE, export_word = FALSE, filename = "ttest_results.docx" )
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 |
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.test or wilcox.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; TRUE if a parametric test was
used, FALSE if 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 NULL for 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, SE for
parametric tests; or group, N, Median,
IQR for 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, plus
warnings and recommendations character vectors.
apa_tableA flextable object containing the
publication-ready APA-formatted table, or NULL if
apa_table = FALSE or 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.
create_complete_apa_table for building APA tables,
export_apa_to_word for Word export,
format_p_value for formatting p-values.
# 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# 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
Alias for create_complete_apa_table.
create_apa_table(result)create_apa_table(result)
result |
A |
A flextable object
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.
create_complete_apa_table(result)create_complete_apa_table(result)
result |
A |
A flextable object, or NULL if descriptive
statistics are not available
result <- complete_ttest( iris[iris$Species != "virginica", ], Sepal.Length ~ Species ) create_complete_apa_table(result)result <- complete_ttest( iris[iris$Species != "virginica", ], Sepal.Length ~ Species ) create_complete_apa_table(result)
Generates a comprehensive Word document containing descriptive statistics, inferential tests, and interpretations for multiple outcome variables.
create_complete_report( data, formula_list, group_var, paired = FALSE, title = "Statistical Analysis Report", filename = "apa_report.docx", include_descriptives = TRUE, include_interpretations = TRUE )create_complete_report( data, formula_list, group_var, paired = FALSE, title = "Statistical Analysis Report", filename = "apa_report.docx", include_descriptives = TRUE, include_interpretations = TRUE )
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 |
Invisibly returns the path to the created file
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 )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 )
Exports an APA-formatted flextable to an HTML file.
export_apa_to_html( ft, filename = "ttest_results.html", include_interpretation = TRUE, interpretation_text = NULL )export_apa_to_html( ft, filename = "ttest_results.html", include_interpretation = TRUE, interpretation_text = NULL )
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 |
Invisibly returns the path to the created file
result <- complete_ttest( iris[iris$Species != "virginica", ], Sepal.Length ~ Species ) tmp <- tempfile(fileext = ".html") export_apa_to_html(result$apa_table, tmp)result <- complete_ttest( iris[iris$Species != "virginica", ], Sepal.Length ~ Species ) tmp <- tempfile(fileext = ".html") export_apa_to_html(result$apa_table, tmp)
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.
export_apa_to_word( apa_table, filename = "ttest_results.docx", title = "T-Test Results" )export_apa_to_word( apa_table, filename = "ttest_results.docx", title = "T-Test Results" )
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 |
Invisibly returns the path to the created file
result <- complete_ttest( iris[iris$Species != "virginica", ], Sepal.Length ~ Species ) tmp <- tempfile(fileext = ".docx") export_apa_to_word(result$apa_table, tmp)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
format_p_value(p, include_stars = TRUE)format_p_value(p, include_stars = TRUE)
p |
Numeric p-value |
include_stars |
Logical, include significance stars (default TRUE) |
Character string with formatted p-value
format_p_value(0.0001) format_p_value(0.03) format_p_value(0.5)format_p_value(0.0001) format_p_value(0.03) format_p_value(0.5)
Generate random test data
generate_test_data( n = 50, mean = 10, sd = 2, group1_name = "A", group2_name = "B", seed = 123 )generate_test_data( n = 50, mean = 10, sd = 2, group1_name = "A", group2_name = "B", seed = 123 )
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) |
A data frame with columns value and group
df <- generate_test_data()df <- generate_test_data()
Interpret Cohen's d
interpret_cohens_d(d)interpret_cohens_d(d)
d |
Numeric Cohen's d value |
Character string with interpretation
interpret_cohens_d(0.3)interpret_cohens_d(0.3)
Interpret r (for non-parametric tests)
interpret_r(r)interpret_r(r)
r |
Numeric r value |
Character string with interpretation
interpret_r(0.4)interpret_r(0.4)
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.
## S3 method for class 'complete_ttest' print(x, ...)## S3 method for class 'complete_ttest' print(x, ...)
x |
A |
... |
Additional arguments passed to |
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_table is not
NULL, the publication-ready table is printed inline.
Summary: a plain-language summary combining the method used, assumption results, significance, and effect size.
summary.complete_ttest for a condensed summary with an
APA-style interpretation, complete_ttest for the main
analysis function.
result <- complete_ttest(mtcars, mpg ~ 1, mu = 20) print(result)result <- complete_ttest(mtcars, mpg ~ 1, mu = 20) print(result)
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.
## S3 method for class 'complete_ttest' summary(object, ...)## S3 method for class 'complete_ttest' summary(object, ...)
object |
A |
... |
Additional arguments passed to |
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.
print.complete_ttest, complete_ttest.
result <- complete_ttest(mtcars, mpg ~ 1, mu = 20) summary(result)result <- complete_ttest(mtcars, mpg ~ 1, mu = 20) summary(result)