1.两独样本参数的非参数检验

1.1.Welcoxon秩和检验

先将两样本看成是单一样本(混合样本)然后由小到大排列观察值统一编秩。如果原假设两个独立样本来自相同的总体为真,那么秩将大约均匀分布在两个样本中,即小的、中等的、大的秩值应该大约被均匀分在两个样本中。如果备选假设两个独立样本来自不相同的总体为真,那么其中一个样本将会有更多的小秩值,这样就会得到一个较小的秩和;另一个样本将会有更多的大秩值,因此就会得到一个较大的秩和。

R:wilcox.test

 

##################独立样本的曼-惠特尼U检验
Forest<-read.table(file="ForestData.txt",header=TRUE,sep="	")
Forest$month<-factor(Forest$month,levels=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))
Tmp<-subset(Forest,Forest$month=="jan" | Forest$month=="aug")
wilcox.test(temp~month,data=Tmp)

  

Wilcoxon rank sum test with continuity correction

data: temp by month
W = 2, p-value = 0.01653
alternative hypothesis: true location shift is not equal to 0

1.2.K-S检验

##################独立样本的K-S检验
x1<-subset(Forest,Forest$month=="jan")
x2<-subset(Forest,Forest$month=="aug")
ks.test(x1$temp,x2$temp)

  

Two-sample Kolmogorov-Smirnov test

data: x1$temp and x2$temp
D = 0.99457, p-value = 0.03992
alternative hypothesis: two-sided

1.3.两配对样本分布

###############配对样本的Wilcoxon符号秩检验
ReportCard<-read.table(file="ReportCard.txt",header=TRUE,sep=" ")
ReportCard<-na.omit(ReportCard)
wilcox.test(ReportCard$chi,ReportCard$math,paired=TRUE)

sum(outer(ReportCard$chi,ReportCard$math,"-")<0)
sum(outer(ReportCard$math,ReportCard$chi,"-")<0)

  

Wilcoxon signed rank test with continuity correction

data: ReportCard$chi and ReportCard$math
V = 1695.5, p-value = 8.021e-11
alternative hypothesis: true location shift is not equal to 0

>
> sum(outer(ReportCard$chi,ReportCard$math,"-")<0)
[1] 332
> sum(outer(ReportCard$math,ReportCard$chi,"-")<0)
[1] 3026

2.两样本均值置换检验

我们在实验中经常会因为各种问题(时间、经费、人力、物力)得到一些小样本结果,如果我们想知道这些小样本结果的总体是什么样子的,就需要用到置换检验。

Permutation test 置换检验是Fisher于20世纪30年代提出的一种基于大量计算(computationally intensive),利用样本数据的全(或随机)排列,进行统计推断的方法,因其对总体分布自由,应用较为广泛,特别适用于总体分布未知的小样本资料,以及某些难以用常规方法分析资料的假设检验问题。在具体使用上它和Bootstrap Methods类似,通过对样本进行顺序上的置换,重新计算统计检验量,构造经验分布,然后在此基础上求出P-value进行推断。

2.1.概述

参数也可以是中位数等

2.2R程序

oneway_test()

 

Forest<-read.table(file="ForestData.txt",header=TRUE,sep="	")
Forest$month<-factor(Forest$month,levels=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))
Tmp<-subset(Forest,Forest$month=="jan" | Forest$month=="aug")
t.test(temp~month,data=Tmp,paired=FALSE,var.equal=TRUE)
Tmp$month<-as.vector(Tmp$month)
Tmp$month<-as.factor(Tmp$month)
oneway_test(temp~month,data=Tmp,distribution="exact")
oneway_test(temp~month,data=Tmp,distribution="asymptotic")
oneway_test(temp~month,data=Tmp,distribution=approximate(B=1000))

  

Two Sample t-test

data: temp by month
t = -4.8063, df = 184, p-value = 3.184e-06
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-23.106033 -9.657011
sample estimates:
mean in group jan mean in group aug
5.25000 21.63152

 

 

Exact Two-Sample Fisher-Pitman Permutation Test

data: temp by month (aug, jan)
Z = 4.5426, p-value = 0.0001744
alternative hypothesis: true mu is not equal to 0

 

 

Asymptotic Two-Sample Fisher-Pitman Permutation Test

data: temp by month (aug, jan)
Z = 4.5426, p-value = 5.557e-06
alternative hypothesis: true mu is not equal to 0

 

Approximative Two-Sample Fisher-Pitman Permutation Test

data: temp by month (aug, jan)
Z = 4.5426, p-value < 2.2e-16
alternative hypothesis: true mu is not equal to 0

2.3相关系数置换检验

spearsman_test

对学生成绩,基于数学和物理成绩的spearsman相关系数进行置换检验

ReportCard<-read.table(file="ReportCard.txt",header=TRUE,sep=" ")
Tmp<-ReportCard[complete.cases(ReportCard),]
cor.test(Tmp[,5],Tmp[,7],alternative="two.side",method="spearman")
#是让你的模拟能够可重复出现,因为很多时候我们需要取随机数,但这段代码再跑一次的时候,结果就不一样
#了,如果需要重复出现的模拟结果的话,就可以用set.seed()。在调试程序或者做展示的时候,结果的可重#复性是很重要的. 12345是种子数
set.seed(12345)
spearman_test(math~phy,data=Tmp,distribution=approximate(B=1000))

  

sample estimates:
rho
0.7651233

Approximative Spearman Correlation Test

data: math by phy
Z = 5.7766, p-value < 2.2e-16
alternative hypothesis: true rho is not equal to 0

 

2.4卡方分布置换检验

对于学生的成绩,在性别和平均分等级列联表上,采用置换检验,看性别与平均分两个变量是否是独立的

Tmp<-ReportCard[complete.cases(ReportCard),]
CrossTable<-table(Tmp[,c(2,12)])  #编制性别和平均分等级的列联表
chisq.test(CrossTable,correct=FALSE)
chisq_test(sex~avScore,data=Tmp,distribution="asymptotic")
set.seed(12345)
chisq_test(sex~avScore,data=Tmp,distribution=approximate(B=1000))

 

> CrossTable
avScore
sex B C D E
F 2 13 10 3
M 2 11 12 5

Pearson's Chi-squared test

data: CrossTable
X-squared = 0.78045, df = 3, p-value = 0.8541

Asymptotic Pearson Chi-Squared Test

data: sex by avScore (B, C, D, E)
chi-squared = 0.78045, df = 3, p-value = 0.8541

 

Approximative Pearson Chi-Squared Test

data: sex by avScore (B, C, D, E)
chi-squared = 0.78045, p-value = 0.922

原假设:有关,不应拒绝原假设。

2.5两配对样本置换检验

wilcoxsign_test

ReportCard<-read.table(file="ReportCard.txt",header=TRUE,sep=" ")
ReportCard<-na.omit(ReportCard)
wilcox.test(ReportCard$chi,ReportCard$math,paired=TRUE)
wilcoxsign_test(chi~math,data=ReportCard,distribution="asymptotic")

  

Wilcoxon signed rank test with continuity correction

data: ReportCard$chi and ReportCard$math
V = 1695.5, p-value = 8.021e-11
alternative hypothesis: true location shift is not equal to 0

 

Asymptotic Wilcoxon-Pratt Signed-Rank Test

data: y by x (pos, neg)
stratified by block
Z = 6.5041, p-value = 7.817e-11
alternative hypothesis: true mu is not equal to 0

量结论一致

3.两样本均值差的自举检验

3.1概述

两样本均值的置换检验可以检验出两个总体的均值是否存在显著差异,但对总体均值差的置信区间估计比较困难。置信区间的估计,是以样本均值差的抽样分布已知且对称为前提的,若无法保证这个前提,则可采用自举发进行检验。

3.2.R实现

1.编写用户自定义函数

例如,对两样本均值的自举法检验:分别计算两个样本的均值并返回

DiffMean<-function(DataSet,indices){
 ReSample<-DataSet[indices,]#从Dataset中抽取indices决定的观测形成自举样本
 diff<-tapply(ReSample[,1],INDEX=as.factor(ReSample[,2]),FUN=mean)
#表示以自举样本第2列分组标识,分别计算自举样本第1列的均值。
 return(diff[1]-diff[2])
}
#第一列是待检验变量,第二列为观测来自总体的标识。indices包括了n个元素的随机位置向量,它是从DataSet
#中抽取观测以形成自举样本的依据。

  

2.调用boot函数实现自举法检验

library("boot")
Forest<-read.table(file="ForestData.txt",header=TRUE,sep="	")
Forest$month<-factor(Forest$month,levels=c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"))
Tmp<-subset(Forest,Forest$month=="jan" | Forest$month=="aug")
Tmp<-cbind(Tmp$temp,Tmp$month)
set.seed(12345)
BootObject<-boot(data=Tmp,statistic=DiffMean,R=20)
#调用自定义函数,自举重复次数20。

 

Call:
boot(data = Tmp, statistic = DiffMean, R = 20)


Bootstrap Statistics :
original bias std. error
t1* -16.38152 -0.07459533 0.2012279

BootObject:t是从自举样本中得到的M个统计量。

 

3.获得计算结果

BootObject$t0
mean(BootObject$t,na.rm=TRUE)
print(BootObject)
plot(BootObject)
boot.ci(BootObject,conf=0.95,type=c("norm","perc"))

  

CALL :
boot.ci(boot.out = BootObject, conf = 0.95, type = c("norm",
"perc"))

Intervals :
Level Normal Percentile
95% (-16.70, -15.91 ) (-16.85, -16.06 )
Calculations and Intervals on Original Scale

基于自举样本的样本均值差不服从正态分布,因此不适合采纳根据正态分布确定的置信区间。

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程