###Brown-Mood中位数检验(精确检验,正态近似,连续性修正后的正态近似)
BM.test=function(x,y,alt) #alt:备择假设形式
{
xy=c(x,y)
md.xy=median(xy)
t=sum(xy>md.xy)
lx=length(x[x!=md.xy])
ly=length(y[y!=md.xy])
lxy=lx+ly
A=sum(x>md.xy)#检验统计量A
z=(A-lx*t)/(lx+ly)/(lx*ly*t*(lx+ly-t)/(lx+ly)^3)^0.5#正态近似时的标准化统计量
if(A>(min(lx,t)/2)){
z1=(A+0.5-lx*t)/(lx+ly)/(lx*ly*t*(lx+ly-t)/(lx+ly)^3)^0.5#连续性修正后正态近似时的标准化统计量
}
else{z1=(A-0.5-lx*t)/(lx+ly)/(lx*ly*t*(lx+ly-t)/(lx+ly)^3)^0.5}
if(alt=="greater"){
pv1=1-phyper(A,lx,ly,t)#精确p值
pv2=1-pnorm(z)#正态近似p值
pv3=1-pnorm(z1)#连续性修正后正态近似p值
}
if(alt=="less"){
pv1=phyper(A,lx,ly,t)
pv2=pnorm(z)
pv3=pnorm(z1)
}
if(alt=="two.sided"){
pv1=2*min(1-phyper(A,lx,ly,t),phyper(A,lx,ly,t))
pv2=2*min(1-pnorm(z),pnorm(z))
pv3=2*min(1-pnorm(z1),pnorm(z1))
}
conting.table=matrix(c(A,lx-A,lx,t-A,ly-(t-A),ly,t,lxy-t,lxy),3,3)#计数表
col.name=c("X","Y","X+Y")
row.name=c(">MXY","<MXY","TOTAL")
dimnames(conting.table)=list(row.name,col.name)
list(contingency.table=conting.table,p.value=pv1,pvnorm=pv2,pvnr=pv3)
}
暂无数据