Paired Samples T-test in R - Easy Guides - Wiki (2024)

  • Discussion
  • What is paired samples t-test?
  • Research questions and statistical hypotheses
  • Formula of paired samples t-test
  • Visualize your data and compute paired t-test in R
    • R function to compute paired t-test
    • Import your data into R
    • Check your data
    • Visualize your data using box plots
    • Preleminary test to check paired t-test assumptions
    • Compute paired samples t-test
    • Interpretation of the result
    • Access to the values returned by t.test() function
  • Online paired t-test calculator
  • See also
  • Infos

The paired samples t-test is used to compare the means between two related groups of samples. In this case, you have two values (i.e., pair of values) for the same samples. This article describes how to compute paired samples t-test using R software.

As an example of data, 20 mice received a treatment X during 3 months. We want to know whether the treatment X has an impact on the weight of the mice.

To answer to this question, the weight of the 20 mice has been measured before and after the treatment. This gives us 20 sets of values before treatment and 20 sets of values after treatment from measuring twice the weight of the same mice.

In such situations, paired t-test can be used to compare the mean weights before and after treatment.

Paired t-test analysis is performed as follow:

  1. Calculate the difference (\(d\)) between each pair of value
  2. Compute the mean (\(m\)) and the standard deviation (\(s\)) of \(d\)
  3. Compare the average difference to 0. If there is any significant difference between the two pairs of samples, then the mean of d (\(m\)) is expected to be far from 0.

Paired t-test can be used only when the difference \(d\) is normally distributed. This can be checked using Shapiro-Wilk test.


Paired Samples T-test in R - Easy Guides - Wiki (1)


Typical research questions are:


  1. whether the mean difference (\(m\)) is equal to 0?
  2. whether the mean difference (\(m\)) is less than 0?
  3. whether the mean difference (\(m\)) is greather than 0?

In statistics, we can define the corresponding null hypothesis (\(H_0\)) as follow:

  1. \(H_0: m = 0\)
  2. \(H_0: m \leq 0\)
  3. \(H_0: m \geq 0\)

The corresponding alternative hypotheses (\(H_a\)) are as follow:

  1. \(H_a: m \ne 0\) (different)
  2. \(H_a: m > 0\) (greater)
  3. \(H_a: m < 0\) (less)

Note that:

  • Hypotheses 1) are called two-tailed tests
  • Hypotheses 2) and 3) are called one-tailed tests

t-test statistisc value can be calculated using the following formula:

\[t = \frac{m}{s/\sqrt{n}}\]

where,

  • m is the mean differences
  • n is the sample size (i.e., size of d).
  • s is the standard deviation of d

We can compute the p-value corresponding to the absolute value of the t-test statistics (|t|) for the degrees of freedom (df): \(df = n - 1\).

If the p-value is inferior or equal to 0.05, we can conclude that the difference between the two paired samples are significantly different.

R function to compute paired t-test

To perform paired samples t-test comparing the means of two paired samples (x & y), the R function t.test() can be used as follow:

t.test(x, y, paired = TRUE, alternative = "two.sided")

  • x,y: numeric vectors
  • paired: a logical value specifying that we want to compute a paired t-test
  • alternative: the alternative hypothesis. Allowed value is one of “two.sided” (default), “greater” or “less”.

Import your data into R

  1. Prepare your data as specified here: Best practices for preparing your data set for R

  2. Save your data in an external .txt tab or .csv files

  3. Import your data into R as follow:

# If .txt tab file, use thismy_data <- read.delim(file.choose())# Or, if .csv file, use thismy_data <- read.csv(file.choose())

Here, we’ll use an example data set, which contains the weight of 10 mice before and after the treatment.

# Data in two numeric vectors# ++++++++++++++++++++++++++# Weight of the mice before treatmentbefore <-c(200.1, 190.9, 192.7, 213, 241.4, 196.9, 172.2, 185.5, 205.2, 193.7)# Weight of the mice after treatmentafter <-c(392.9, 393.2, 345.1, 393, 434, 427.9, 422, 383.9, 392.3, 352.2)# Create a data framemy_data <- data.frame( group = rep(c("before", "after"), each = 10), weight = c(before, after) )

We want to know, if there is any significant difference in the mean weights after treatment?

Check your data

# Print all dataprint(my_data)
 group weight1 before 200.12 before 190.93 before 192.74 before 213.05 before 241.46 before 196.97 before 172.28 before 185.59 before 205.210 before 193.711 after 392.912 after 393.213 after 345.114 after 393.015 after 434.016 after 427.917 after 422.018 after 383.919 after 392.320 after 352.2

Compute summary statistics (mean and sd) by groups using the dplyr package.

  • To install dplyr package, type this:
install.packages("dplyr")
  • Compute summary statistics by groups:
library("dplyr")group_by(my_data, group) %>% summarise( count = n(), mean = mean(weight, na.rm = TRUE), sd = sd(weight, na.rm = TRUE) )
Source: local data frame [2 x 4] group count mean sd (fctr) (int) (dbl) (dbl)1 after 10 393.65 29.398012 before 10 199.16 18.47354

Visualize your data using box plots

To use R base graphs read this: R base graphs. Here, we’ll use the ggpubr R package for an easy ggplot2-based data visualization.

  • Install the latest version of ggpubr from GitHub as follow (recommended):
# Installif(!require(devtools)) install.packages("devtools")devtools::install_github("kassambara/ggpubr")
  • Or, install from CRAN as follow:
install.packages("ggpubr")
  • Visualize your data:
# Plot weight by group and color by grouplibrary("ggpubr")ggboxplot(my_data, x = "group", y = "weight", color = "group", palette = c("#00AFBB", "#E7B800"), order = c("before", "after"), ylab = "Weight", xlab = "Groups")

Paired Samples T-test in R - Easy Guides - Wiki (3)

Paired Samples T-test in R

Box plots show you the increase, but lose the paired information. You can use the function plot.paired() [in pairedData package] to plot paired data (“before - after” plot).

  • Install pairedData package:
install.packages("PairedData")
  • Plot paired data:
# Subset weight data before treatmentbefore <- subset(my_data, group == "before", weight, drop = TRUE)# subset weight data after treatmentafter <- subset(my_data, group == "after", weight, drop = TRUE)# Plot paired datalibrary(PairedData)pd <- paired(before, after)plot(pd, type = "profile") + theme_bw()

Paired Samples T-test in R - Easy Guides - Wiki (4)

Paired Samples T-test in R

Preleminary test to check paired t-test assumptions

Assumption 1: Are the two samples paired?

Yes, since the data have been collected from measuring twice the weight of the same mice.

Assumption 2: Is this a large sample?

No, because n < 30. Since the sample size is not large enough (less than 30), we need to check whether the differences of the pairs follow a normal distribution.

How to check the normality?

Use Shapiro-Wilk normality test as described at: Normality Test in R.

  • Null hypothesis: the data are normally distributed
  • Alternative hypothesis: the data are not normally distributed
# compute the differenced <- with(my_data, weight[group == "before"] - weight[group == "after"])# Shapiro-Wilk normality test for the differencesshapiro.test(d) # => p-value = 0.6141

From the output, the p-value is greater than the significance level 0.05 implying that the distribution of the differences (d) are not significantly different from normal distribution. In other words, we can assume the normality.

Note that, if the data are not normally distributed, it’s recommended to use the non parametric paired two-samples Wilcoxon test.

Compute paired samples t-test

Question : Is there any significant changes in the weights of mice after treatment?

1) Compute paired t-test - Method 1: The data are saved in two different numeric vectors.

# Compute t-testres <- t.test(before, after, paired = TRUE)res
 Paired t-testdata: before and aftert = -20.883, df = 9, p-value = 6.2e-09alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: -215.5581 -173.4219sample estimates:mean of the differences -194.49 

2) Compute paired t-test - Method 2: The data are saved in a data frame.

# Compute t-testres <- t.test(weight ~ group, data = my_data, paired = TRUE)res
 Paired t-testdata: weight by groupt = 20.883, df = 9, p-value = 6.2e-09alternative hypothesis: true difference in means is not equal to 095 percent confidence interval: 173.4219 215.5581sample estimates:mean of the differences 194.49 

As you can see, the two methods give the same results.


In the result above :

  • t is the t-test statistic value (t = 20.88),
  • df is the degrees of freedom (df= 9),
  • p-value is the significance level of the t-test (p-value = 6.210^{-9}).
  • conf.int is the confidence interval (conf.int) of the mean differences at 95% is also shown (conf.int= [173.42, 215.56])
  • sample estimates is the mean differences between pairs (mean = 194.49).

Note that:

  • if you want to test whether the average weight before treatment is less than the average weight after treatment, type this:
t.test(weight ~ group, data = my_data, paired = TRUE, alternative = "less")
  • Or, if you want to test whether the average weight before treatment is greater than the average weight after treatment, type this
t.test(weight ~ group, data = my_data, paired = TRUE, alternative = "greater")

Interpretation of the result

The p-value of the test is 6.210^{-9}, which is less than the significance level alpha = 0.05. We can then reject null hypothesis and conclude that the average weight of the mice before treatment is significantly different from the average weight after treatment with a p-value = 6.210^{-9}.

Access to the values returned by t.test() function

The result of t.test() function is a list containing the following components:


  • statistic: the value of the t test statistics
  • parameter: the degrees of freedom for the t test statistics
  • p.value: the p-value for the test
  • conf.int: a confidence interval for the mean appropriate to the specified alternative hypothesis.
  • estimate: the means of the two groups being compared (in the case of independent t test) or difference in means (in the case of paired t test).

The format of the R code to use for getting these values is as follow:

# printing the p-valueres$p.value
[1] 6.200298e-09
# printing the meanres$estimate
mean of the differences 194.49 
# printing the confidence intervalres$conf.int
[1] 173.4219 215.5581attr(,"conf.level")[1] 0.95

You can perform paired-samples t-test, online, without any installation by clicking the following link:


Online paired-samples t-test calculator

Paired Samples Wilcoxon Test (non-parametric)

This analysis has been performed using R software (ver. 3.2.4).


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!

Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!



Recommended for You!


Machine Learning Essentials: Practical Guide in R
Practical Guide to Cluster Analysis in R
Practical Guide to Principal Component Methods in R
R Graphics Essentials for Great Data Visualization
Network Analysis and Visualization in R
More books on R and data science

Recommended for you

This section contains best data science and self-development resources to help you on your path.

Coursera - Online Courses and Specialization

Data science

Popular Courses Launched in 2020

Trending Courses

Books - Data Science

Our Books

Others



Want to Learn More on R Programming and Data Science?

Follow us by Email

On Social Networks:

Get involved :
Click to follow us on Facebook and Google+ :
Comment this article by clicking on "Discussion" button (top-right position of this page)

Paired Samples T-test in R - Easy Guides - Wiki (2024)

References

Top Articles
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5688

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.