Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 53

Understanding and Applying T-Tests

Recommended resources

Welcome to our new lesson! In our previous session, we established the importance of data validation with assertthat, ensuring our data is clean and correct before we even begin analysis. Now, we move from validating our data to asking meaningful questions of it.

Your goal is to conduct statistical analysis for clinical research, a field where comparing groups is a fundamental activity. Do patients who received a new drug show a greater reduction in blood pressure than those who received a placebo? Is the average body temperature of a group of patients different from the known population average? The t-test is the statistical tool designed to answer precisely these kinds of questions.

In this lesson, we will delve into the theory and practical application of the t-test in R. You will learn to perform and interpret the three main types: the one-sample, the independent two-sample, and the paired t-test. This will equip you with a foundational method for hypothesis testing in your future research work.

1. The Logic of Comparing Means

Before we write any code, it's essential to understand the "why" behind the t-test. In research, we often want to know if an observed difference between group averages (means) is "real" or just due to random chance from sampling.

The t-test is a parametric test, meaning it makes certain assumptions about the data, most notably that the data follows a normal distribution. It is one of the most common methods used in clinical research to compare continuous variables.

Statistics: Analysis of continuous data using the t-test and ANOVA

This article from EuroIntervention, a journal focused on cardiovascular medicine, provides an excellent, high-level overview of why t-tests are relevant in a clinical context.

Start by reading the introduction to understand the context. Then, review the sections on basic statistical considerations to refresh your understanding of data distribution, means, and the logic of hypothesis testing (null hypothesis and p-values). Finally, read the section on the different types of t-tests. Don't worry about the formulas in Figure 2 for now; focus on understanding the purpose of each test type.

The article you just read outlines three key scenarios for comparing means. This image provides a helpful visual summary of when to use each type of t-test.

This diagram illustrates the core question answered by each of the three main types of t-tests: one-sample, independent samples, and paired samples.

2. A Tidyverse-Friendly Toolkit: rstatix

While R's built-in t.test() function is perfectly capable, the rstatix package offers a suite of functions that integrate seamlessly with the tidyverse workflow you've been learning. Its primary function, t_test(), uses the same formula syntax as ggplot2 and works beautifully with the pipe (%>%). For this course, we will focus on rstatix as it provides clear, well-formatted output ready for analysis and reporting.

To proceed, you'll need to install and load the necessary packages if you haven't already.




# install.packages("tidyverse")
# install.packages("rstatix")
# install.packages("ggpubr") # Used for visualization
# install.packages("datarium") # Contains example datasets

library(tidyverse)
library(rstatix)
library(ggpubr)

Now, let's explore each t-test one by one. The following resource from Datanovia will serve as our main practical guide. It provides clear, step-by-step examples for each test using the rstatix package.


3. The One-Sample T-Test

Use Case: You use a one-sample t-test when you want to compare the mean of a single sample to a known or hypothesized population mean.

  • Clinical Example: A researcher might know that the average weight of a specific lab mouse strain is 25g. They take a sample of 10 mice that have undergone a new diet and want to know if their average weight is statistically different from the known 25g standard.

The process involves stating a null hypothesis (e.g., ), checking assumptions, running the test, and interpreting the p-value.

T-test in R: The Ultimate Guide - Datanovia

This guide will walk you through performing a one-sample t-test in R.

Read the section on the One-Sample t-test. Follow the steps for the mice dataset. Pay close attention to: Checking Assumptions: The guide checks for outliers and uses the Shapiro-Wilk test for normality. This connects back to what we learned in the previous module. Computation: Note the simple formula syntax: t_test(weight ~ 1, mu = 25). The ~ 1 indicates we are testing against a single value, and mu is that hypothesized value. Interpretation: Look at the p-value (p). A small p-value (typically < 0.05) suggests that the sample mean is significantly different from the hypothesized mean.


4. The Independent Two-Sample T-Test

Use Case: This is likely the most common t-test you'll encounter. It's used to compare the means of two independent groups. The subjects in one group have no connection to the subjects in the other.

  • Clinical Example: Comparing the mean blood pressure of a group of patients receiving a new medication versus a group receiving a placebo. The patients in the medication group are different people from those in the placebo group.

A key assumption for this test is the homogeneity of variances, meaning the spread (variance) of the data in both groups should be roughly equal. If this assumption is violated, we use a variation called Welch's t-test, which is robust to unequal variances. Conveniently, rstatix::t_test() performs Welch's test by default.

T-test in R: The Ultimate Guide - Datanovia

Now, let's apply the same process to comparing two independent groups.

Read the section on the Independent samples t-test. Using the genderweight dataset, focus on: Checking Assumptions: Notice that normality is checked for each group. A new check is introduced: Levene's test for equality of variances (levene_test()). Computation: The formula is now t_test(weight ~ group). R understands that group has two levels (F and M) and performs the comparison. The guide explains that because the Levene's test was significant (p < 0.05), the variances are unequal, and Welch's t-test (the default) is appropriate. Interpretation: The conclusion is based on the p-value, just as before.


5. The Paired T-Test

Use Case: The paired t-test is used when the measurements in the two groups are related. This typically happens when you measure the same subject at two different points in time (a "before and after" scenario).

  • Clinical Example: Measuring the cholesterol levels of a group of patients before they start a new diet, and then measuring the same patients' cholesterol levels again six months after starting the diet. Each "before" measurement is paired with an "after" measurement from the same person.

Because the measurements are paired, the test is statistically more powerful than an independent t-test if the data structure is indeed paired. The key assumption is that the differences between the paired measurements are normally distributed.

T-test in R: The Ultimate Guide - Datanovia

Finally, let's examine the case of related or "before-and-after" measurements.

Read the section on the Paired samples t-test. For the mice2 dataset, observe the following: Data Structure: The guide first transforms the data from a "wide" format (with before and after columns) to a "long" format. This uses gather(), which is an older version of the pivot_longer() function you learned in Module 4. The principle is identical. Checking Assumptions: The test checks for normality on the differences between the paired values. Computation: The formula is the same as the independent test, but with a crucial addition: t_test(weight ~ group, paired = TRUE). This argument tells R to treat the data as paired.


6. Choosing the Right Test: A Summary

You've now seen all three types of t-tests. The decision of which one to use is critical and depends entirely on your research question and experimental design. This flowchart provides a simple decision-making map.

This flowchart guides you through selecting the correct t-test. The first question is always about the relationship between your samples.

7. Video Walkthrough: Seeing it in Action

To solidify your understanding, the following video from the "R Programming 101" channel provides an excellent walkthrough of performing t-tests. It uses the base R t.test() function, which is good to be aware of, but the interpretation of the p-value, confidence intervals, and hypothesis testing logic is universal and applies directly to what you've learned with rstatix.

t-test and interpreting p values using R Programming

This video provides a great conceptual revision of hypothesis testing and then demonstrates the single-sample, independent, and paired t-tests in R.

Watch the introduction to hypothesis testing from the beginning. This is a fantastic recap of the logic we've been using. See the one-sample t-test in action from this section. Notice the mu = 50 argument, which is the same as in rstatix. Next, watch the demonstration of the independent two-sample t-test from this segment. The formula lifeExp ~ continent is identical to what we used. Finally, see how the paired t-test is performed from this part. Note the paired = TRUE argument, which is also identical.


Conclusion

In this lesson, you've learned to perform and interpret one of the most fundamental statistical tests in the analyst's toolkit. You can now confidently compare means to answer critical research questions.

Key Takeaways:

  • The t-test is used to compare the means of continuous data.
  • One-Sample T-Test: Compares the mean of one group to a known value. In rstatix: t_test(variable ~ 1, mu = value).
  • Independent Two-Sample T-Test: Compares the means of two unrelated groups. In rstatix: t_test(outcome ~ group). Welch's test is used by default, which is robust to unequal variances.
  • Paired T-Test: Compares the means of two related groups (e.g., before/after). In rstatix: t_test(outcome ~ group, paired = TRUE).
  • The interpretation always comes back to the p-value: a small p-value (e.g., < 0.05) provides evidence to reject the null hypothesis and conclude that a statistically significant difference exists.

Preview of the Next Lesson:
The t-test is powerful, but it's limited to comparing one or two means of normally distributed data. What if you want to compare proportions instead of means? Or what if you have three or more groups to compare? In our next lesson, we will expand our testing toolkit to answer these questions by exploring the chi-square test and Analysis of Variance (ANOVA). We will also introduce non-parametric alternatives to use when your data doesn't meet the assumptions of the t-test.