R的两均值⽐较检验(⾮参数检验)
1.两独样本参数的⾮参数检验
1.1.Welcoxon秩和检验
先将两样本看成是单⼀样本(混合样本)然后由⼩到⼤排列观察值统⼀编秩。如果原假设两个独⽴样本来⾃相同的总体为真,那么秩将⼤约均匀分布在两个样本中,即⼩的、中等的、⼤的秩值应该⼤约被均匀分在两个样本中。如果备选假设两个独⽴样本来⾃不相同的总体为真,那么其中⼀个样本将会有更多的⼩秩值,这样就会得到⼀个较⼩的秩和;另⼀个样本将会有更多的⼤秩值,因此就会得到⼀个较⼤的秩和。
R:st
>>>###独⽴样本的曼-惠特尼U检验
Forest<-read.table(file="",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")
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")
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="",header=TRUE,sep=" ")
ReportCard<-na.omit(ReportCard)
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="",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$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="",header=TRUE,sep=" ")
Tmp<-ReportCard[complete.cases(ReportCard),]
#是让你的模拟能够可重复出现,因为很多时候我们需要取随机数,但这段代码再跑⼀次的时候,结果就不⼀样
#了,如果需要重复出现的模拟结果的话,就可以⽤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(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="",header=TRUE,sep=" ")
ReportCard<-na.omit(ReportCard)
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
bootstrap检验方法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函数实现⾃举法检验

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论