---
title: "Publication-ready t-tests in R"
author: "Rémi Thériault"
date: "February 3, 2022"
output:
rmarkdown::html_vignette:
toc: true
vignette: >
%\VignetteIndexEntry{Publication-ready t-tests in R}
%\VignetteEngine{knitr::rmarkdown}
---
```{r global_options, include=FALSE}
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)
}
```
## Getting started
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).
```{r}
head(mtcars)
```
Load the `rempsyc` package:
```{r}
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):
```{r}
pkgs <- c("effectsize", "flextable", "broom", "report")
install_if_not_installed(pkgs)
```
---
```{r}
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.
```{r}
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
```{r}
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.
### Save table to Word
Let's open (or save) it to word for use in a publication (optional).
```{r, eval = FALSE}
# Open in Word
print(my_table, preview = "docx")
# Save in Word
flextable::save_as_docx(my_table, path = "t-tests.docx")
```
## Special cases
The function can be passed some of the regular arguments of the base `t.test()` function. For example:
### Student t-test (instead of Welch)
```{r}
nice_t_test(
data = mtcars,
response = "mpg",
group = "am",
var.equal = TRUE
) |>
nice_table()
```
### One-sided (instead of two-sided)
```{r}
nice_t_test(
data = mtcars,
response = "mpg",
group = "am",
alternative = "less",
warning = FALSE
) |>
nice_table()
```
### One-sample (instead of two-sample)
```{r}
nice_t_test(
data = mtcars,
response = "mpg",
mu = 17,
warning = FALSE
) |>
nice_table()
```
### Paired *t* test (instead of independent samples)
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.
```{r, eval = FALSE}
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.
### Multiple comparison corrections
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.
```{r}
nice_t_test(
data = mtcars,
response = names(mtcars)[1:6],
group = "am",
correction = "bonferroni",
warning = FALSE
) |>
nice_table()
```
## Integrations
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.
```{r}
model <- t.test(mpg ~ am, data = mtcars)
```
### `broom` table
```{r}
library(broom)
(stats.table <- tidy(model, conf.int = TRUE))
nice_table(stats.table, broom = "t.test")
```
### `report` table
```{r}
library(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.
```{r}
nice_table(stats.table, report = "t.test", short = TRUE)
```
And there you go!
### Thanks for checking in
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.