books <- read.csv("https://raw.githubusercontent.com/roualdes/data/master/books.csv")two sample t-test
Paired Data
A dataset about book prices at two different stores, the UCLA bookstore and on Amazon. The prices at the two stores are paired on each book (specifically the ISBN number).
For paired data, first create a new variable of the difference. Note which way the subtraction goes.
books$diff <- books$uclaNew - books$amazNewWith the new variable, we just perform a one sample t-test.
t.test(books$diff, mu = 0, conf.level = 0.9)
    One Sample t-test
data:  books$diff
t = 7.6488, df = 72, p-value = 6.928e-11
alternative hypothesis: true mean is not equal to 0
90 percent confidence interval:
  9.981505 15.541783
sample estimates:
mean of x 
 12.76164 Two Sample t-test
Next, we’ll compare two numeric variables which are not paired.
suppressMessages(library(ape))
data(carnivora)The variable longevity (LY) is measured in months.
t.test(LY ~ SuperFamily, data = carnivora, conf.level = 0.95)
    Welch Two Sample t-test
data:  LY by SuperFamily
t = 1.0243, df = 37.394, p-value = 0.3123
alternative hypothesis: true difference in means between group Caniformia and group Feliformia is not equal to 0
95 percent confidence interval:
 -20.03687  61.03354
sample estimates:
mean in group Caniformia mean in group Feliformia 
                192.4583                 171.9600