t檢驗算法及其在R語言中的實現

定義及分類

在進行 t 檢驗之前讓我們先看看它的定義:t 檢驗法就是在假設檢驗時利用 t 分布進行概率計算的檢驗方法。那問題來了,什么是 t 分布呢?

在概率論和統計學中,學生?t- 分布(Student's?t-distribution)可簡稱為?t?分布,用于根據小樣本來估計呈正態分布且方差未知的總體的均值。如果總體方差已知(例如在樣本數量足夠多時),則應該用正態分布來估計總體均值[1]。

所以我們在進行 t 檢驗之前,應該對數據進行正態性檢驗以及方差齊性檢驗

?t?檢驗可以分為單樣本?t?檢驗和雙樣本?t?檢驗(見下圖)。

t 檢驗分類 by小藍哥

單樣本 t 檢驗

步驟及算法

單樣本?t?檢驗步驟如下。

1. 提出假設

by 小藍哥

2. 計算 t?

by 小藍哥

3. 統計推斷

在R中的實現

#單樣本T檢驗

data <- c(4.33,4.62,3.89,4.14,4.78,4.64,4.52,4.55,4.48,4.26)

shapiro.test(data) #p>0.05,符合正態分布

t.test(data,mu=4.5) #mu表示的是平均數

看看R的結果:

①正態性檢驗結果

> shapiro.test(data) #p>0.05,符合正態分布

Shapiro-Wilk normality test

data:? data

W = 0.95054, p-value = 0.6749

②?t?檢驗結果

> t.test(data,mu=4.5)

One Sample t-test

data:? data

t = -0.93574, df = 9, p-value = 0.3738

alternative hypothesis: true mean is not equal to 4.5

95 percent confidence interval:

?4.230016 4.611984

sample estimates:

mean of x?

? ? 4.421?

p=0.3738>0.05,所以拒絕Ho,接受HA。


方差齊的非配對的雙樣本 t?檢驗

步驟及算法

1.?提出假設

by 小藍哥

2.?計算?t

by 小藍哥

其中:

by 小藍哥

3.?統計推斷

在R中的實現

#非配對兩樣本T檢驗

high<-c(134,146,106,119,124,161,107,83,113,129,97,123)

low<-c(70,118,101,85,107,132,94)

x <- c(high,low)

group <- c(rep("high",12),rep("low",7))

shapiro.test(high) #正態性檢驗

shapiro.test(low) #正態性檢驗

bartlett.test(x~group)#方差齊性檢驗

t.test(high,low,paired = FALSE,var.equal = T) #非配對: paired = FALSE? ? ?方差齊: var.equal = T ? ?

①方差齊性檢驗結果

> bartlett.test(x~group)#方差齊性檢驗

Bartlett test of homogeneity of variances

data:? x by group

Bartlett's K-squared = 0.0066764, df = 1, p-value = 0.9349 #接近1表明方差齊

②?t?檢驗結果

> t.test(high,low,paired = FALSE,var.equal = T)

Two Sample t-test

data:? high and low

t = 1.9157, df = 17, p-value = 0.07238

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

-1.942543 40.275876

sample estimates:

mean of x mean of y

120.1667? 101.0000

p-value = 0.07238>0.05,所以不能否定Ho。


方差不齊的非配對的 t 檢驗

步驟及算法?

1. 提出假設

by 小藍哥

2.?計算 t’

by 小藍哥

其中:

by 小藍哥


by 小藍哥


by 小藍哥

3.?統計推斷

在R中的實現

#非配對兩樣本T檢驗

high<-c(134,146,106,119,124,161,107,83,113,129,97,123)

low<-c(70,118,101,85,107,132,94)

x <- c(high,low)

group <- c(rep("high",12),rep("low",7))

shapiro.test(high)

shapiro.test(low)

bartlett.test(x~group)#方差齊性檢驗

t.test(high,low,paired = FALSE,var.equal = F)

t?檢驗結果

> t.test(high,low,paired = FALSE,var.equal = F)

Welch Two Sample t-test

data:? high and low

t = 1.9319, df = 13.016, p-value = 0.07543

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

?-2.263671 40.597005

sample estimates:

mean of x mean of y?

?120.1667? 101.0000?

p-value = 0.07238>0.05,所以不能否定Ho。


配對 雙樣本 t?檢驗

步驟及算法

1.提出假設

by 小藍哥

2.計算t

by 小藍哥

其中

by 小藍哥

by 小藍哥


3. 統計推斷

在R中的實現

#配對兩樣本T檢驗

ds <- c(82.5,85.2,87.6,89.9,89.4,90.1,87.8,87.0,88.5,92.4)

cs <- c(91.7,94.2,93.3,97.0,96.4,91.5,97.2,96.2,98.5,95.8)

library(carData)

library(car)

leveneTest(ds,cs)

d <- ds-cs

shapiro.test(d) #方差齊性檢驗

t.test(ds,cs,paired = T,alternative = "two.sided",cond.lvel=0.95)

t檢驗結果:

> t.test(ds,cs,paired = T,alternative = "two.sided",cond.lvel=0.95)

Paired t-test

data:? ds and cs

t = -7.8601, df = 9, p-value = 2.548e-05

alternative hypothesis: true difference in means is not equal to 0

95 percent confidence interval:

?-9.1949 -5.0851

sample estimates:

mean of the differences?

? ? ? ? ? ? ? ? ? -7.14?

p-value = 2.548e-05 < 0.01,所以否定Ho,接受HA。

參考文獻:

[1].學生 t - 分布(Wikipedia)

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容