Skip to contents

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).

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Load the rempsyc package:

Note: If you haven’t installed this package yet, you will need to install it via the following command: install.packages("rempsyc").

nice_t_test(data = mtcars,
            response = "mpg",
            group = "am",
            warning = FALSE)
##   Dependent Variable         t       df           p         d  CI_lower
## 1                mpg -3.767123 18.33225 0.001373638 -1.477947 -2.265973
##     CI_upper
## 1 -0.6705686

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.

nice_t_test(data = mtcars,
            response = names(mtcars)[1:6],
            group = "am",
            warning = FALSE) -> t.test.results
t.test.results
##   Dependent Variable         t       df            p          d   CI_lower
## 1                mpg -3.767123 18.33225 1.373638e-03 -1.4779471 -2.2659731
## 2                cyl  3.354114 25.85363 2.464713e-03  1.2084550  0.4315896
## 3               disp  4.197727 29.25845 2.300413e-04  1.4452210  0.6417834
## 4                 hp  1.266189 18.71541 2.209796e-01  0.4943081 -0.2260466
## 5               drat -5.646088 27.19780 5.266742e-06 -2.0030843 -2.8592770
## 6                 wt  5.493905 29.23352 6.272020e-06  1.8924060  1.0300224
##     CI_upper
## 1 -0.6705686
## 2  1.9683146
## 3  2.2295592
## 4  1.2066992
## 5 -1.1245498
## 6  2.7329218

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.

Save table to Word

Let’s save it to word for use in a publication (optional).

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)

nice_t_test(data = mtcars,
            response = "mpg",
            group = "am",
            var.equal = TRUE) |> 
  nice_table()

One-sided (instead of two-sided)

nice_t_test(data = mtcars,
            response = "mpg",
            group = "am",
            alternative = "less",
            warning = FALSE) |> 
  nice_table()

One-sample (instead of two-sample)

nice_t_test(data = mtcars,
            response = "mpg",
            mu = 17,
            warning = FALSE) |> 
  nice_table()

Paired t-test (instead of independent samples)

nice_t_test(data = ToothGrowth,
            response = "len",
            group = "supp",
            paired = TRUE) |> 
  nice_table()

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.

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.

model <- t.test(mpg ~ am, data = mtcars)

broom table

library(broom)
(stats.table <- tidy(model, conf.int = TRUE))
## # A tibble: 1 × 10
##   estim…¹ estim…² estim…³ stati…⁴ p.value param…⁵ conf.…⁶ conf.…⁷ method alter…⁸
##     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <chr>  <chr>  
## 1   -7.24    17.1    24.4   -3.77 0.00137    18.3   -11.3   -3.21 Welch… two.si…
## # … with abbreviated variable names ¹​estimate, ²​estimate1, ³​estimate2,
## #   ⁴​statistic, ⁵​parameter, ⁶​conf.low, ⁷​conf.high, ⁸​alternative
nice_table(stats.table, broom = "t.test")

report table

library(report)
(stats.table <- as.data.frame(report(model)))
## Welch Two Sample t-test
## 
## Parameter | Group | Mean_Group1 | Mean_Group2 | Difference |          95% CI | t(18.33) |     p |     d |          d  CI
## ------------------------------------------------------------------------------------------------------------------------
## mpg       |    am |       17.15 |       24.39 |      -7.24 | [-11.28, -3.21] |    -3.77 | 0.001 | -1.76 | [-2.82, -0.67]
## 
## Alternative hypothesis: two.sided
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!

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.