T-test example

Whitlock & Shulter Analysis of Biological Data 2nd Ed

Chapter 12, Question 20



The data: electric fish in Amazon basin

Data as sperate vectors

upstream <- c(14,11,8,5,10,5,23,29,19,16,25,10)
downstream <-c(19,18,8,7,16,6,24,30,16,20,21,12)



Paried t-test on sperate vectors

t.test(upstream, downstream, paired = T)
## 
##  Paired t-test
## 
## data:  upstream and downstream
## t = -1.9096, df = 11, p-value = 0.0826
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.9464134  0.2797467
## sample estimates:
## mean of the differences 
##               -1.833333

Note: this it is equivalent to reverse the order, it just changes the negative values to positive and hte positive values to negative (each the first version has a negative t statistics, the versions that follows has a positive t)

t.test(downstream,upstream, paired = T)
## 
##  Paired t-test
## 
## data:  downstream and upstream
## t = 1.9096, df = 11, p-value = 0.0826
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.2797467  3.9464134
## sample estimates:
## mean of the differences 
##                1.833333



Check assumption- are differences normal

Calcualte the difference between the pairs

d <- upstream-downstream



Plot histogram of differences. They are reasonably normal.

par(mfrow = c(1,1), mar = c(4,4,4,4))
hist(d, main = "Distribution of differences (d)")



Paired t-test on differences

This formulates that code as a 1-sample t-test with the null hypothesis the the differences = 0.

Note ** mu = 0 **, and the fact that there is no need to indicate that this is a paired test.

t.test(d, mu = 0)
## 
##  One Sample t-test
## 
## data:  d
## t = -1.9096, df = 11, p-value = 0.0826
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -3.9464134  0.2797467
## sample estimates:
## mean of x 
## -1.833333



Paired t-test on dataframe - vs1

The data could also be set up as a dataframe where all the numeric data occur in a single column

Make dataframe

n <- length(downstream)
elec.df1 <- data.frame(species = c(upstream,
                                  downstream),
                      location = c(rep("upstream",n),
                                   rep("downstream",n)))

head(elec.df1)
##   species location
## 1      14 upstream
## 2      11 upstream
## 3       8 upstream
## 4       5 upstream
## 5      10 upstream
## 6       5 upstream
tail(elec.df1)
##    species   location
## 19      24 downstream
## 20      30 downstream
## 21      16 downstream
## 22      20 downstream
## 23      21 downstream
## 24      12 downstream



T-test on dataframe. Note “paired=T”.

t.test(species ~ location, 
       data = elec.df1,
       paired =T)
## 
##  Paired t-test
## 
## data:  species by location
## t = 1.9096, df = 11, p-value = 0.0826
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.2797467  3.9464134
## sample estimates:
## mean of the differences 
##                1.833333

Paired t-test on dataframe - vs2

The data could also appear as a dataframe with two columns, one for upstream, one for down streams

elec.df2 <- data.frame(upstream = upstream,
                       downstream = downstream)

head(elec.df2)
##   upstream downstream
## 1       14         19
## 2       11         18
## 3        8          8
## 4        5          7
## 5       10         16
## 6        5          6



Use data = …

t.test(upstream , downstream, data = elec.df2, paired = T)

Could also do it like this

t.test(elec.df2$upstream , elec.df2$downstream, , paired = T)

Or like this

t.test(elec.df2[, "upstream"], elec.df2[,"downstream"], , paired = T)

Or why not like this

t.test(elec.df2[, 1], elec.df2[,2], , paired = T)



All equivalent

t.test(downstream,upstream, paired = T)
t.test(upstream , downstream, data = elec.df2, paired = T)
t.test(elec.df2$upstream , elec.df2$downstream, , paired = T)
t.test(elec.df2[, "upstream"], elec.df2[,"downstream"], , paired = T)
t.test(elec.df2[, 1], elec.df2[,2], , paired = T)