data(sleep)

直接使用原本數據的變項進行檢定

t.test(extra ~ group, data = sleep)
## 
##  Welch Two Sample t-test
## 
## data:  extra by group
## t = -1.8608, df = 17.776, p-value = 0.07939
## alternative hypothesis: true difference in means between group 1 and group 2 is not equal to 0
## 95 percent confidence interval:
##  -3.3654832  0.2054832
## sample estimates:
## mean in group 1 mean in group 2 
##            0.75            2.33
boxplot(extra ~ group, sleep, col = "skyblue", border = "purple", xlab="group", ylab="extra sleeping time")

把控制組和實驗組獨立出來檢定

control <- sleep[c(1:10), ]
treatment <- sleep[c(11:20), ]

要指定跑兩組各自extra這個變項的檢定

t.test(control$extra , treatment$extra)
## 
##  Welch Two Sample t-test
## 
## data:  control$extra and treatment$extra
## t = -1.8608, df = 17.776, p-value = 0.07939
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -3.3654832  0.2054832
## sample estimates:
## mean of x mean of y 
##      0.75      2.33
boxplot(control$extra , treatment$extra , col = "skyblue", border = "purple", xlab="group", ylab="extra sleeping time")

H0:使用藥物不會對睡眠造成影響(實驗組與控制組無顯著差異) H1:使用藥物會對睡眠造成影響(實驗組與控制組有顯著差異)

由T檢定可知實驗組與控制組並無顯著差異(p-value = 0.07939,沒有小於0.05,且95%信賴區間包含0),則在信心水準95%的情況下,無法推翻虛無假設。 雖然由圖表可看見平均數有一些差異,但從檢定來看卻沒有,可能原因是樣本數太少,所以沒辦法呈現顯著差異。