library(knitr) knitr::opts_chunk$set( fig.width = 7, fig.height = 7, warning = FALSE, message = FALSE ) knitr::opts_knit$set(root.dir = tempdir()) pkgs <- c("effectsize", "methods", "flextable", "broom", "report") successfully_loaded <- vapply(pkgs, requireNamespace, FUN.VALUE = logical(1L), quietly = TRUE) can_evaluate <- all(successfully_loaded) if (can_evaluate) { knitr::opts_chunk$set(eval = TRUE) vapply(pkgs, require, FUN.VALUE = logical(1L), quietly = TRUE, character.only = TRUE) } else { knitr::opts_chunk$set(eval = FALSE) }
This function makes it really easy to get all all your t-test results in one simple, publication-ready table.
Let's first load the demo data. This data set comes with base R
(meaning you have it too and can directly type this command into your R
console).
head(mtcars)
Load the rempsyc
package:
library(rempsyc)
Note: If you haven't installed this package yet, you will need to install it via the following command:
install.packages("rempsyc")
. Furthermore, you may be asked to install the following packages if you haven't installed them already (you may decide to install them all now to avoid interrupting your workflow if you wish to follow this tutorial from beginning to end):
pkgs <- c("effectsize", "flextable", "broom", "report") install_if_not_installed(pkgs)
nice_t_test( data = mtcars, response = "mpg", group = "am", warning = FALSE )
Note: This function relies on the base R
t.test
function, which uses the Welch t-test per default (see why here: https://daniellakens.blogspot.com/2015/01/always-use-welchs-t-test-instead-of.html). To use the Student t-test, simply add the following argument:var.equal = TRUE
.
Now the best thing about this function is that you can put all your dependent variables of interest in the function call and it will output a sweet, pre-formatted table for your convenience.
t.test.results <- nice_t_test( data = mtcars, response = names(mtcars)[1:6], group = "am", warning = FALSE ) t.test.results
If we want it to look nice
my_table <- nice_table(t.test.results) my_table
Note: The d is Cohen's d, and the 95% CI is the confidence interval of the effect size (Cohen's d). p is the p-value, df is degrees of freedom, and t is the t-value.
Let's open (or save) it to word for use in a publication (optional).
# Open in Word print(my_table, preview = "docx") # Save in Word flextable::save_as_docx(my_table, path = "t-tests.docx")
The function can be passed some of the regular arguments of the base t.test()
function. For example:
nice_t_test( data = mtcars, response = "mpg", group = "am", var.equal = TRUE ) |> nice_table()
nice_t_test( data = mtcars, response = "mpg", group = "am", alternative = "less", warning = FALSE ) |> nice_table()
nice_t_test( data = mtcars, response = "mpg", mu = 17, warning = FALSE ) |> nice_table()
Note that for paired t tests, you need to use paired = TRUE
, and you also need data in "long" format rather than wide format (like for the ToothGrowth
data set). In this case, the group
argument refers to the participant ID for example, so the same group/participant is measured several times, and thus has several rows.
nice_t_test( data = ToothGrowth, response = "len", group = "supp", paired = TRUE ) |> nice_table()
Note that R >= 4.4.0 has stopped supporting the paired
argument for the formula method used internally in nice_t_test()
, but since version 0.1.7.8
, we use a workaround for backward compatibility.
It is also possible to correct for multiple comparisons. Note that only a Bonferroni correction is supported at this time (which simply multiplies the p-value by the number of tests). Bonferroni will automatically correct for the number of tests.
nice_t_test( data = mtcars, response = names(mtcars)[1:6], group = "am", correction = "bonferroni", warning = FALSE ) |> nice_table()
There are other ways to do t-tests and format the results properly, should you wish—for example through the broom
and report
packages. Examples below.
model <- t.test(mpg ~ am, data = mtcars)
broom
tablelibrary(broom) (stats.table <- tidy(model, conf.int = TRUE)) nice_table(stats.table, broom = "t.test")
report
tablelibrary(report) (stats.table <- as.data.frame(report(model))) nice_table(stats.table, report = "t.test")
The report
package provides quite comprehensive tables, so one may request an abbreviated table with the short
argument.
nice_table(stats.table, report = "t.test", short = TRUE)
And there you go!
Make sure to check out this page again if you use the code after a time or if you encounter errors, as I periodically update or improve the code. Feel free to contact me for comments, questions, or requests to improve this function at https://github.com/rempsyc/rempsyc/issues. See all tutorials here: https://remi-theriault.com/tutorials.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.