t檢驗和 Z檢驗都可用於均值檢驗。
單樣本均值檢驗
當樣本容量小於30時使用t檢驗,當樣本容量大於30時使用Z檢驗
Z檢驗使用例子:
library(UsingR)x<-rnorm(50,0,5)simple.z.test(x,5)
運行結果:
[1] -2.947929 3.250022
結果說明在信賴度為95%的情況下總體的均值區間為[-2.947929 3.250022]
t檢驗使用例子:
x<-rnorm(20,0,5)t.test(x)
運行結果:
One Sample t-test
data: x
t = -0.1736, df = 19, p-value = 0.864
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-2.886276 2.444247
sample estimates:
mean of x
-0.2210147
------------------------------------------------------------------------------------
雙樣本均值檢驗
對t.test函數,R的協助文檔有很好的例子:
require(graphics)t.test(1:10, y = c(7:20)) # P = .00001855t.test(1:10, y = c(7:20, 200)) # P = .1245 -- NOT significant anymore## Classical example: Student's sleep dataplot(extra ~ group, data = sleep)## Traditional interfacewith(sleep, t.test(extra[group == 1], extra[group == 2]))## Formula interfacet.test(extra ~ group, data = sleep)
此外《統計建模與R語言》從P206頁開始有討論正太總體均值的建設檢驗,書中作者編寫了自己的均值檢驗函數mean.test1(針對單個總體)和mean.test2(針對兩個總體),也有相對應的使用t.test函數進行檢驗的例子,具體應用時可以參考。