前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R in action读书笔记(16)第十二章 重抽样与自助法之 置换检验

R in action读书笔记(16)第十二章 重抽样与自助法之 置换检验

作者头像
统计学家
发布2019-04-10 17:07:33
9870
发布2019-04-10 17:07:33
举报

第十二章:重抽样与自助法

本章,我们将探究两种应用广泛的依据随机化思想的统计方法:置换检验和自助法

12.1 置换检验

置换检验,也称随机化检验或重随机化检验.

有两种处理条件的实验,十个受试者已经被随机分配到其中一种条件(A或B)中,相应的结果变量(score)也已经被记录。实验结果如下:

如果两种处理方式真的等价,那么分配给观测得分的标签(A处理或B处理)便是任意的。为检验两种处理方式的差异,我们可遵循如下步骤:

(1) 与参数方法类似,计算观测数据的t统计量,称为t0;

(2) 将10个得分放在一个组中;

(3) 随机分配五个得分到A处理中,并分配五个得分到B处理中;

(4) 计算并记录新观测的t统计量;

(5) 对每一种可能随机分配重复(3)~(4)步,此处有252种可能的分配组合;

(6) 将252个t统计量按升序排列,这便是基于(或以之为条件)样本数据的经验分布;

(7) 如果t0落在经验分布中间95%部分的外面,则在0.05的显著性水平下,拒绝两个处理组的

总体均值相等的零假设。

12.2 用coin 包做置换检验

对于独立性问题,coin包提供了一个进行置换检验的一般性框架。通过该包可以回答

如下问题:

响应值与组的分配独立吗?

两个数值变量独立吗?

两个类别型变量独立吗?

相对于传统检验,提供可选置换检验的coin函数:

检验

coin函数

两样本和K样本置换检验

oneway_test(y ~ A)

含一个分层(区组)因子的两样本和K样本置换检验

oneway_test(y ~ A | C)

Wilcoxon-Mann-Whitney秩和检验

wilcox_test(y ~ A)

Kruskal-Wallis检验

kruskal_test(y ~ A)

Person卡方检验

chisq_test(A ~ B)

Cochran-Mantel-Haenszel检验

cmh_test(A ~ B | C)

线性关联检验

lbl_test(D ~ E)

Spearman检验

spearman_test(y ~ x)

Friedman检验

friedman_test(y ~ A | C)

Wilcoxon符号秩检验

wilcoxsign_test(y1 ~ y2)

在coin函数中,y和x是数值变量,A和B是分类因子,C是类别型区组变量,D和E是有序因子,y1和y2是相匹配的

数值变量。

函数形式:function(formula,data,distribution=)

其中:

formula描述的是要检验变量间的关系。示例可参见表12-2;

data是一个数据框;

distribution指定经验分布在零假设条件下的形式,可能值有exact,asymptotic和

approximate。若distribution = "exact",那么在零假设条件下,分布的计算是精确的(即依据所有可能的排列组合)。当然,也可以根据它的渐进分布(distribution = "asymptotic")或蒙特卡洛重抽样(distribution ="approxiamate(B = #)")来做近似计算,其中#指所需重复的次数。distribution = "exact"当前仅可用于两样本问题。

12.2.1 独立两样本和K样本检验

虚拟数据中的t检验与单因素置换检验:

> library(coin)

> score<-c(40,57,45,55,58,57,64,55,62,65)

>treatment<-factor(c(rep("A",5),rep("B",5)))

> mydata<-data.frame(treatment,score)

> t.test(score~treatment,data=mydata,var.equal=TRUE)

Two Samplet-test

data: score bytreatment

t = -2.345, df = 8, p-value = 0.04705

alternative hypothesis: true difference in means is notequal to 0

95 percent confidence interval:

-19.0405455 -0.1594545

sample estimates:

mean in group A mean in group B

51.0 60.6

>oneway_test(score~treatment,data=mydata,distribute="exact")

Asymptotic2-Sample Permutation Test

data: score by treatment(A, B)

Z = -1.9147, p-value = 0.05553

alternative hypothesis: true mu is not equal to 0

Wilcoxon-Mann-Whitney U检验

> library(MASS)

> UScrime<-transform(UScrime,So=factor(So))

>wilcox_test(Prob~So,data=UScrime,distribute="exact")

Asymptotic WilcoxonMann-Whitney Rank Sum Test

data: Prob by So(0, 1)

Z = -3.7493, p-value = 0.0001774

alternative hypothesis: true mu is not equal to 0

近似的K样本置换检验

> library(multcomp)

> set.seed(1234)

> oneway_test(response~trt,data=cholesterol,

+ distribution=approximate(B=9999))

ApproximativeK-Sample Permutation Test

data: response by

trt (1time, 2times, 4times, drugD, drugE)

maxT = 4.7623, p-value < 2.2e-16

12.2.2 列联表中的独立性

通过chisq_test()或cmh_test()函数,我们可用置换检验判断两类别型变量的独立性。

当数据可根据第三个类别型变量进行分层时,需要使用后一个函数。若变量都是有序型,可使用

lbl_test()函数来检验是否存在线性趋势。

> library(coin)

> library(vcd)

载入需要的程辑包:grid

> Arthritis<-transform(Arthritis,

+ Improved=as.factor(as.numeric(Improved)))

> set.seed(1234)

> chisq_test(Treatment~Improved,data=Arthritis,distribution=approximate(B=9999))

ApproximativePearson's Chi-Squared Test

data: Treatment byImproved (1, 2, 3)

chi-squared = 13.055, p-value = 0.0018

需要把变量Improved从一个有序因子变成一个分类因子是因为,如果用有序因子,coin()

将会生成一个线性与线性趋势检验,而不是卡方检验。

12.2.3 数值变量间的独立性

spearman_test()函数提供了两数值变量的独立性置换检验。

> states<-as.data.frame(state.x77)

> set.seed(1234)

>spearman_test(Illiteracy~Murder,data=states,distribution=approximate(B=9999))

ApproximativeSpearman Correlation Test

data: Illiteracyby Murder

Z = 4.7065, p-value < 2.2e-16

alternative hypothesis: true mu is not equal to 0#独立性假设并不被满足。

12.2.4 两样本和K样本相关性检验

当处于不同组的观测已经被分配得当,或者使用了重复测量时,样本相关检验便可派上用场。

对于两配对组的置换检验,可使用wilcoxsign_test()函数;多于两组时,使用friedman_

test()函数。

> library(coin)

> library(MASS)

>wilcoxsign_test(U1~U2,data=UScrime,distribution="exact")

ExactWilcoxon-Signed-Rank Test

data: y by x (neg,pos)

stratified by block

Z = 5.9691, p-value = 1.421e-14

alternative hypothesis: true mu is not equal to 0#结果表明两者的失业率是不同的

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-04-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器学习与统计学 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档