Whitlock and Shulter Chapter 12 example 12.2

Add data by hand

before.implant<-c(4.65,3.91,4.91,4.5,4.8,4.88,4.88,4.78,4.98,4.87,4.75,4.7,4.93)

after.implant <-c(4.44,4.3,4.98,4.45,5,5,5.01,4.96,5.02,4.73,4.77,4.6,5.01)


male.ID<-c(1,4,5,6,9,10,15,16,17,19,20,23,24)

before <- rep("before",length(male.ID))
after <- rep("after",length(male.ID))

rwbb.stacked <- data.frame(male.ID = male.ID,
                           antibody = c(before.implant,
                                        after.implant),
                           treatment = c(before,
                                         after)
                           )
rwbb.stacked$treatment <- factor(rwbb.stacked$treatment,
                                 levels = c("before","after"))

Set working directory

setwd("C:/Users/lisanjie2/Desktop/TEACHING/1_STATS_CalU/1_STAT_CalU_2016_by_NLB/Lecture/Lecture15_comparing_2_means")

Load red-winged black bird data

rwbb <- read.csv("Chapter12_comparing_2_means.csv")
library(ggplot2)

means <- tapply(rwbb.stacked$antibody,
                rwbb.stacked$treatment,
                FUN = mean)

pd <- (0.5)
qplot(y = antibody,
      x = treatment,
      color = treatment,
      group = male.ID,
      shape = treatment,
      geom = c("point"),
      data = rwbb.stacked) + 
  geom_point(size = 3)  +
  stat_summary(fun.y = "mean",
               fun.ymax = function(x) mean(x) - sd(x),
               fun.ymin = function(x) mean(x) - sd(x),
               geom = "pointrange",
               size = 1,
               aes(group = treatment),
      color = "black",
      position = position_nudge(x = 0.1)) +
  theme_bw() +
  xlab("Implant treatment")

Plot individual response

library(ggplot2)

qplot(y = antibody,
      x = treatment,
      color = treatment,
      group = male.ID,
      shape = treatment,
      geom = c("point","line"),
      data = rwbb.stacked) + 
  geom_point(size = 3) +
  geom_line(color = "black") +
  theme_bw() +
  xlab("Implant treatment")

Distribution of the differences

par(mfrow = c(1,1), mar = c(4,3,3,3))
hist(rwbb$difference, main = "Before vs. After implant",
     xlab = "",
     ylim = c(0,5))
mtext(side = 1,
      line = 2.25,
      text = "Difference in antibody production", 
      cex =1.75)
abline(v = mean(rwbb$difference),
       lwd = 4,lty = 1, col = 3)
text(x=mean(rwbb$difference),y = 4.95, "Mean difference")

t.test(rwbb$difference)
## 
##  One Sample t-test
## 
## data:  rwbb$difference
## t = 1.2714, df = 12, p-value = 0.2277
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.04007695  0.15238464
## sample estimates:
##  mean of x 
## 0.05615385
t.test(rwbb$before.implant,
       rwbb$after.implant,paired = T)
## 
##  Paired t-test
## 
## data:  rwbb$before.implant and rwbb$after.implant
## t = -1.2714, df = 12, p-value = 0.2277
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.15238464  0.04007695
## sample estimates:
## mean of the differences 
##             -0.05615385