實驗設計 Exercise 2.4

As part of a larger experiment, Dale (1992) looked at six samples of a wetland soil undergoing a simulated snowmelt. Three were randomly selected for treatment with a neutral pH snowmelt; the other three got a reduced pH snowmelt. The observed response was the number of Copepoda removed from each microcosm during the first 14 days of snowmelt.
Reduced pH Neutral pH
256 159 149 54 123 248

Using randomization methods, test the null hypothesis that the two treatments have equal average numbers of Copepoda versus a two-sided alternative.

library(ggplot2)
library(dplyr)
x1<- c(256,159,149)
x2 <- c(54,123,248)
x <- c(x1,x2)

Observation

ob <- (sum(x1) - sum(x2))/3

Simulation

n = 1; pv = NULL
while(n <= 1000){
sumd <- NULL
for(i in 1:10000){
y <- sample(x,3,F)
tmp <- sum(x) - sum(y)
sumd[i] <-  (sum(y) - tmp)/3}
p <- sum(sumd >= ob)/10000
pv <- c(pv,p)
n = n + 1}

Two tale P value

mean and standard error

P_value <- mean(pv) * 2
P_value
## [1] 0.4004862
sd(pv)
## [1] 0.003869461

Histogram

sumd <- as.data.frame(sumd)
sumd <- mutate(sumd,cc = (sumd >= ob) *1 )
sumd$cc[sumd$cc == 1] <- '>= red line'
sumd$cc[sumd$cc == 0] <- '< red line' 
sumd$cc <- sumd$cc %>% as.factor()
tmp <- ggplot(sumd,aes(x = sumd, fill=cc, color = cc))+ geom_histogram(position="identity", alpha = 0.5, binwidth = 30)
tmp+theme(plot.title = element_text(colour = "black", face = "bold", size = 20, vjust = 0.3)) + 
  geom_vline(aes(xintercept=ob),linetype="dashed",color="red")+
  scale_color_manual(values=c("skyblue", "darkblue","black"))+
  scale_fill_manual(values=c("skyblue", "darkblue","black"))+
  labs(title="Randomization ",x="Simulation of statistic", y = "Count")