# Paired t-test in R Exercises

September 21, 2016
By

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

The paired samples t test is used to check if there are any differences in the mean of the same sample at two different time points. For example a medical researcher collects data on the same patients before and after a therapy. A paired t test will show if the therapy improves patient outcomes.

There are several assumptions that need to be satisfied so that results of a paired t test are valid. They are listed below

• The measured variable is continuous
• The differences between the two groups are approximately normally distributed
• We should not have any outliers in our data
• An adequate sample size is required

For this exercise we will use the anorexia data set available in package MASS. The data set contains weights of girls before and after anorexia treatment. Our interest is to know if the treatment caused any change in weight.

Solutions to these exercises can be found here

Exercise 1

Load the data and inspect its structure

Exercise 2

Generate descriptive statistics on weight before treatment

Exercise 3

Generate descriptive statistics on weight after treatment

Exercise 4

Create a new variable that contains the differences in weight before and after treatment

Exercise 5

Create a boxplot to identify any outliers

Exercise 6

Create a histogram with a normal curve to visually inspect normality

Exercise 7

Perform a normality test on the differences

Exercise 8

Perform a power analysis to assess sample adequacy

Exercise 9

Perform a paired t test

Exercise 10

Interpret the results

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...