×
中国卫生检验杂志

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

跟两独立样本相对应的是两配对样本,生物医学中常见的案例是治疗前后的比较,两种检测方法的比较(同一样本接受不同的检验方法)、配对的对象接受不同的处理。根据不同适用条件,常用的统计方法有:两配对样本的t检验、两配对样本的非参数检验(如Wilcoxon符号秩检验)、配对卡方检验(McNemar)。

【1】两配对样本的t检验

其本质是两配对样本的差值与0的比较。跟两独立样本的t检验一样,两配对样本的t检验用到的函数也是t.test {stats} {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(\"\", \"less\", \"greater\"),mu = 0, paired = FALSE, = FALSE, = 0.95, ...)

配对t检验也要求正态分布,但与两独立样本的t检验不同,配对样本的t检验要求的两配对样本的差值呈正态分布

李斌,贺佳等.医学统计学(第6版).北京:人民卫生出版社,2013.

①数据导入

library(openxlsx)

pttest<-(\"D:/Temp/\",1)

②正态性检验

d=pttest$before-pttest$after

(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符号秩检验

当配对样本的差值不满足正态分布时,需要改用非参数检验。差值正态的数据也可以使用非参,只是检验效率低一点而已。跟两独立样本的非参数检验一样,两相关样本的非参数检验常用的函数也是 {stats}.,但需要将默认的paired = FALSE修改为paired = TRUE。

{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.

(x, y = NULL,alternative = c(\"\", \"less\", \"greater\"), mu = 0, paired = FALSE, exact = NULL, correct = TRUE,conf.int = FALSE, = 0.95, ...)

颜虹等.医学统计学(第2版).北京:人民卫生出版社,2010.8.

①数据导入

library(openxlsx)

nptest<-(\"D:/Temp/\",2)

②正态性检验

d=nptest$before-nptest$after

(d)

Wilcoxon符号秩检验

(nptest$before,nptest$after,paired = TRUE)

(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,使用函数 {stats}:Performs McNemar's chi-squared test for symmetry of rows and columns in a two-dimensional contingency table.

(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<-(\"D:/Temp/\",3)

②生成配对四表格矩阵

上一篇:严密构筑疫情防控链 上海积极推进上海秋冬季疫
下一篇:没有了