Patient <- c(rep(1:8,2))
Hours <- c(3.2,1.6,5.7,2.8,5.5,1.2,6.1,2.9,3.8,1.0,8.4,3.6,5.0,3.5,7.3,4.8)
Treatment <- c(rep("A",8),rep("B",8))
analgesic <- data.frame(Patient,Hours,Treatment)Paired t-Tests
Background
This is a set of data from Statistical Methods in Biology, 2nd Edition N.J.T Bailey, Hodder and Stoughton, London, 1981 on the properties of two analgesics. The data is paired for 8 patients showing the number of hours of relief with each analgesic.
The first step is to create the data.
It is always a good idea to summarise your data graphically before you do any analysis.
library(ggpubr)Loading required package: ggplot2
ggboxplot(analgesic, x="Treatment" ,y= "Hours",
color = "Treatment", palette= c("#4B0082","#5c3317"),
order = c("A","B"),
ylab = "Hours of relief", xlab = "Treatment")There is not a strong apparent different between the two groups based on a summary of the data but the paired t-test might find a difference if the effect size is smaller than the population variability. To check that the t-test is valid we can check the normality of the differences with a Shapiro-Wilk test.
d <- with(analgesic, Hours[Treatment == "A"]- Hours[Treatment =="B"])
shapiro.test(d)
Shapiro-Wilk normality test
data: d
W = 0.94176, p-value = 0.6284
results <- t.test(Hours ~ Treatment, data=analgesic, paired = TRUE)
results
Paired t-test
data: Hours by Treatment
t = -2.4342, df = 7, p-value = 0.04514
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-2.07000426 -0.02999574
sample estimates:
mean difference
-1.05
Carrying out a pairwise data plot would have been more revealing than the boxplots in this case.
A <- subset(analgesic, Treatment == "A", Hours)
B <- subset(analgesic, Treatment == "B", Hours)
library(PairedData)Loading required package: MASS
Loading required package: gld
Loading required package: mvtnorm
Loading required package: lattice
Attaching package: 'PairedData'
The following object is masked from 'package:base':
summary
pd <- paired(A,B)
plot(pd, type = "profile") + theme_cleveland()Most patients see an upward movement between A and B however 2 see slight falls and there are large differences in the slope of the connections.