轉自個人微信公眾號【Memo_Cleon】的統計學習筆記:R筆記:兩配對樣本的顯著性檢驗。
跟兩獨立樣本相對應的是兩配對樣本,生物醫學中常見的案例是治療前後的比較,兩種檢測方法的比較(同一樣本接受不同的檢驗方法)、配對的對象接受不同的處理。根據不同適用條件,常用的統計方法有:兩配對樣本的t檢驗、兩配對樣本的非參數檢驗(如Wilcoxon符號秩檢驗)、配對卡方檢驗(McNemar)。
【1】兩配對樣本的t檢驗
其本質是兩配對樣本的差值與0的比較。跟兩獨立樣本的t檢驗一樣,兩配對樣本的t檢驗用到的函數也是t.test {stats}。t.test {stats}默認的是兩獨立樣本的t檢驗,在進行兩配對樣本的t檢驗時,只需要將默認的paired = FALSE修改為paired = TRUE即可。
t.test {stats}:Student's t-Test,Performs one and two sample t-tests on vectors of data.
t.test(x, y = NULL,alternative = c("two.sided", "less", "greater"),mu = 0, paired = FALSE, var.equal = FALSE,conf.level = 0.95, ...)
配對t檢驗也要求正態分布,但與兩獨立樣本的t檢驗不同,配對樣本的t檢驗要求的兩配對樣本的差值呈正態分布。
李斌,賀佳等.醫學統計學(第6版).北京:人民衛生出版社,2013.
①數據導入
library(openxlsx)
pttest<-read.xlsx("D:/Temp/pddata.xlsx",1)
②正態性檢驗
d=pttest$before-pttest$after
shapiro.test(d)
③配對t檢驗
t.test(pttest$before,pttest$after,paired = TRUE)
t.test(pttest[1:12,2],pttest[1:12,3],paired =TRUE)
Shapiro-Wilk檢驗表明,差值呈正態分布(W=0.88,P=0.08>0.05),可以採用配對t檢驗進行差異性檢驗;配對t檢驗結果顯示:t=3.74,P=0.003<0.05,可以認為飲用咖啡前後運動者的心肌血流量存在差異。
【2】兩配對樣本的非參數檢驗:Wilcoxon符號秩檢驗
當配對樣本的差值不滿足正態分布時,需要改用非參數檢驗。差值正態的數據也可以使用非參,只是檢驗效率低一點而已。跟兩獨立樣本的非參數檢驗一樣,兩相關樣本的非參數檢驗常用的函數也是wilcox.test {stats}.,但需要將默認的paired = FALSE修改為paired = TRUE。
wilcox.test {stats}:Wilcoxon Rank Sum and Signed Rank Tests,Performs one- and two-sample Wilcoxon tests on vectors of data; the latter is also known as 『Mann-Whitney』 test.
wilcox.test(x, y = NULL,alternative = c("two.sided", "less", "greater"), mu = 0, paired = FALSE, exact = NULL, correct = TRUE,http://conf.int = FALSE, conf.level = 0.95, ...)
顏虹等.醫學統計學(第2版).北京:人民衛生出版社,2010.8.
①數據導入
library(openxlsx)
nptest<-read.xlsx("D:/Temp/pddata.xlsx",2)
②正態性檢驗
d=nptest$before-nptest$after
shapiro.test(d)
③Wilcoxon符號秩檢驗
wilcox.test(nptest$before,nptest$after,paired = TRUE)
wilcox.test(nptest[1:12,2],nptest[1:12,3],paired =TRUE)
Shapiro-Wilk檢驗表明,差值不呈正態分布(W=0.82,P=0.026<0.05),採用配對t檢驗是不合適的,需要採用兩相關樣本的非參數檢驗,結果顯示P=0.0041<0.05,可以認為溶脲脲原體會影響到家兔精子的密度。警示信息的出現主要是由於數據中有重複數據(打結現象)。
【3】配對四表格設計的卡方分析
配對卡方檢驗我們用的McNemar's Chi-squared Test,使用函數mcnemar.test {stats}:Performs McNemar's chi-squared test for symmetry of rows and columns in a two-dimensional contingency table.
mcnemar.test(x, y = NULL, correct = TRUE)
x:either a two-dimensional contingency table in matrix form, or a factor object. y:a factor object; ignored if x is a matrix. correct:a logical indicating whether to apply continuity correction when computing the test statistic.
顏虹等.醫學統計學(第2版).北京:人民衛生出版社,2010.8.
①數據導入
library(openxlsx)
mctest<-read.xlsx("D:/Temp/pddata.xlsx",3)
②生成配對四表格矩陣
pd<-matrix(c(mctest$頻數),nrow = 2,dimnames = list("甲法" = c("陽性", "陰性"),"乙法" = c("陽性", "陰性")))
③McNemar's Chi-squared Test
mcnemar.test(pd)
結果顯示chi2=0.196,P=0.658>0.05,尚不能拒絕兩種檢測方法的檢出率沒有差異的假設。
轉自個人微信公眾號【Memo_Cleon】的統計學習筆記:R筆記:兩配對樣本的顯著性檢驗。
END