分享

R笔记:两配对样本的显著性检验

 Memo_Cleon 2020-05-01

跟两独立样本相对应的是两配对样本,生物医学中常见的案例是治疗前后的比较,两种检测方法的比较(同一样本接受不同的检验方法)、配对的对象接受不同的处理。根据不同适用条件,常用的统计方法有:两配对样本的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,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,尚不能拒绝两种检测方法的检出率没有差异的假设。


END

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多