貳、數據檢定之t檢定(以excel、Google sheet和R語言示範)(高中向) 作者:shi wood yán( 關於作者 )(部分內容由AI輔助)
前言 本篇介紹了三種方式(Excel、Google sheet、R)做t檢定,可以根據自身條件選擇,但其實Excel、Google sheet,算出來的結果其實都有些微的錯誤,兩者錯誤好像都是因為自由度的計算錯誤,而學界上,基本還是以spss與r語言為主,但我認為以高中生來講,Excel與Google sheet也夠了。
一、t檢定(t-test)簡介
使用情況
比較兩組數據的平均數,是否有明顯差異 ,以我所做的實驗舉例,校定必修-生物實驗的生活中的留言終結者—學校飲水機的水比馬桶水還髒,我們分別記錄了五組廁所之菌落數與五組飲水機之菌落數(下圖),在這種情況下就適合用t檢定。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
Independent vs Paired vs One Sample
另外,t-檢定也分為三種,獨立樣本t檢定(Independent sample t-test)、成對樣本t檢定(Paired Sample t-test)與單樣本 t 檢定(One Sample t-test)。
獨立樣本t檢定:
當二樣本組,不互相干涉,則用於獨立樣本t檢定,如:我舉例的實驗數據。
成對樣本t檢定:
若當二樣本組,互相干涉,像是樣本的前後對照,就要用到成對樣本t檢定,如:同一組病人治療前後身體數據(像是血液的白血球濃度)的差異。
單樣本 t 檢定:
使用單樣本 t 檢定的時機,通常都會有個目標值(如:理論值),用一個樣本組做鑑定,看是否有符合目標值。
本次以獨立樣本t檢定作為主要示範對象。
二、執行t檢定簡介
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
上圖為執行t檢定的參考步驟圖,在執行t檢定之前,通常會進行變異數同質性檢測來確保兩組數據的變異數是否相等,因為變異數是否相等將會決定t檢定的計算公式
同質性檢測:
在執行t檢定前,要先檢定兩組數據的變異數是否相同,這裡的意思不是直接算出變異數比較。而是透過F檢定或Levene檢定算出F值與
值(這裡的p值為變異數同質性檢測結果的p值,為防止與t檢定的p值搞混,故以
表示),並判斷變異數是否相等。
t檢定之變異數是否相等:
根據Levene檢定/F檢定的結果,可以選擇合適的t檢定方法,並算出單尾與雙尾的p值
值 > 0.05:變異數相等
值 < 0.05:變異數不相等
選擇單尾與雙尾數值:
根據實驗假設選擇紀錄單尾與雙尾的p值。
三、實際執行t檢定-以excel示範 事前設定-開啟資料分析 在 Excel 中載入分析工具箱
完成上述設定,會在資料當中出現資料分析選項。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
例子:比較兩種不同培養基(A和B)對細菌生長的影響。 (以下非實際實驗,僅作為例子示範)
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
假設與顯著性水平(𝛼): 雙尾檢定
:培養基A和B對細菌生長的影響相同(即平均CFU/mL相等)
:培養基A和B對細菌生長的影響不同(即平均CFU/mL不相等)
𝛼=0.05
STEP1:同質性檢測 我們點選資料分析,它會跳出以下畫面。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
選擇F檢定
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
接者會跳出設定頁面,設定範圍完後,按確定。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
接者便會輸出結果,得到p約為0.11(順帶一題,F值為兩變數的變異數之比值)
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
藉由上述操作我們知道p值大於0.05,結果為「變異數相等」 STEP2:做t檢定 我們再次點選資料分析,根據前一步驟選擇t檢定的類型,由於我們的結果為「變異數相等」,故選「t 檢定:兩個母體平均數差的檢定,假設變異數相等」,若及上一步結果為「變異數不相等」,則選擇「t 檢定:兩個母體平均數差的檢定,假設變異數不相等」
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
一樣設定好範圍
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
輸出結果,得知雙尾p值為2.19206E-06
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
STRP3:分析結果 根據STEP2的結果,p值為2.19206E-06,也就是約為0.000002192(
)小於𝛼值0.05,我們有理由懷疑拒絕虛無假設,並拒絕虛無假設,在統計學上,培養基A和B對細菌生長具有顯著差異。
四、實際執行t檢定-以Google sheet示範 選擇擴充功能→外掛程式→取得外掛程式,接者就會跳出Google Workspace Marketplace 視窗。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
搜尋XLMiner Analysis ToolPak,並下載之。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
接著變會看在擴充功能看到它出現,選擇並按下start,就會跳出分析界面。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
分析界面
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
例子:比較兩種不同培養基(A和B)對細菌生長的影響。 (以下非實際實驗,僅作為例子示範)
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
假設與顯著性水平(𝛼): 雙尾檢定
:培養基A和B對細菌生長的影響相同(即平均CFU/mL相等)
:培養基A和B對細菌生長的影響不同(即平均CFU/mL不相等)
𝛼=0.05
STEP1:同質性檢測 我們點選「F-Test Two-Sample for Variances」,並設定好範圍,按ok輸出。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
結果為下圖,p值約為0.137,大於0.05,結果為「變異數相等」。(對,與excel不太一樣,兩者在自由度的計算都有點錯誤)
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
STEP2:做t檢定 我們再點選「t-Test: Two-Sample Assuming Equal Variances」,設定好範圍,Hypothesized Mean Difference則設置「0」就好,按ok輸出。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
結果為下圖, one-tail是單尾的意思,two-tail就是雙尾,根據實驗假設,我們看雙尾的p值約為0.000002192。
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
STRP3:分析結果 根據STEP2的結果,p值約為0.000002192,小於𝛼值0.05,我們有理由懷疑拒絕虛無假設,並拒絕虛無假設,也就是說,在統計學上,培養基A和B對細菌生長具有顯著差異。
五、實際執行t檢定-以R語言示範 事前設定-下載R語言 本篇適合了解基本R語言操作的人,想了解的,網路上相關文章很多,而且也很詳細。
在用R做t檢定前,電腦要先安裝R語言(幹話),這網路上有需多資源,查"下載R語言"就很多了,推薦的編輯器有RGui或RStudio,Visual Studio Code應該也可以,但要設定一下。
本篇以RGui示範
例子:比較兩種不同培養基(A和B)對細菌生長的影響。 (以下非實際實驗,僅作為例子示範)
Image Not Showing
Possible Reasons
The image was uploaded to a note which you don't have access to The note which the image was originally uploaded to has been deleted
Learn More →
假設與顯著性水平(𝛼): 雙尾檢定
:培養基A和B對細菌生長的影響相同(即平均CFU/mL相等)
:培養基A和B對細菌生長的影響不同(即平均CFU/mL不相等)
𝛼=0.05
STEP1:同質性檢測 在執行Levene檢定前,R語言要下載套件,並將資料輸入成向量格式,並創建一個資料框以符合作Levene檢定的格式。實際程式碼如下:
install.packages("car")
library(car)
CFU_A <- c(150, 160, 145, 155, 165, 150, 158, 162, 148, 152)
CFU_B <- c(140, 135, 138, 142, 130, 145, 133, 137, 139, 136)
data <- data.frame(
CFU = c(CFU_A, CFU_B),
Group = factor(rep(c("A", "B"), each = 10))
)
data
leveneTest(CFU ~ Group, data = data)
執行結果:
Levene
Df F value Pr(>F)
group 1 2.764 0.1137
18
p值約為0.1137,大於0.05,結果為「變異數相等」
STEP2:做t檢定 根據STEP1的結果,我們要做t檢定-假設變異數相等,故程式碼為 var.equal=TRUE
,若變異數不相等,則打 var.equal=FALSE
,實際如下:
# 數據輸入(如果上一步有保留,就不用再執行一次)
CFU_A <- c(150 , 160 , 145 , 155 , 165 , 150 , 158 , 162 , 148 , 152 )
CFU_B <- c(140 , 135 , 138 , 142 , 130 , 145 , 133 , 137 , 139 , 136 )
# 創建一個數據框(如果上一步有保留,就不用再執行一次)
data <- data.frame(
CFU = c(CFU_A , CFU_B ),
Group = factor(rep(c("A" , "B" ), each = 10 ))
)
t.test(data$CFU ~ data$Group , var .equal= TRUE )
執行結果:
Two Sample t-test
data: data$CFU by data$Group
t = 6.8213 , df = 18 , p-value = 2.192e-06
alternative hypothesis: true difference in means between group A and group B is not equal to 0
95 percent confidence interval:
11.76406 22.23594
sample estimates:
mean in group A mean in group B
154.5 137.5
STEP3:分析結果 根據STEP2的結果,p值為2.192e-06,也就是約為0.000002192(
)小於𝛼值0.05,我們有理由懷疑拒絕虛無假設,並拒絕虛無假設,在統計學上,培養基A和B對細菌生長具有顯著差異。
相關筆記