#(1) x=c(5.7,5.5,4.3,5.9,5.2,5.6,5.8,5.1) y=c(5.0,4.5,4.2,5.4,4.4) m=length(x) n=length(y) N=m+n #(a) Wilcoxonuv test - presny wilcox.test(x,y,exact=TRUE,cor=FALSE) R=rank(c(x,y)) t=sum(R[(m+1):N]) #testova statistika X=combn(N,n) K=length(X[1,]) W=apply(X,2,sum) a=table(W)/K #pravdepodobnostni funkce 2*sum(a[1:7]) #presna p-hodnota #(b) Wilcoxonuv test - asymptoticky wilcox.test(x,y,exact=FALSE,cor=FALSE) W=sum(R[(m+1):N]) EW=n*(N+1)/2 DW=m*n*(N+1)/12 t=abs((W-EW)/sqrt(DW)) #testova statistika 2*(1-pnorm(t)) #asymptoticka p-hodnota #(c) van der Waerdenuv test - presny t=sum(qnorm(R[(m+1):N]/(N+1))) #testova statistika W=apply(qnorm(X/(N+1)),2,sum) a=table(W)/K #pravdepodobnostni funkce 2*sum(W<=t)/K #presna p-hodnota #(d) van der Waerdenuv test - asymptoticky library(snpar) data=c(x,y) group=c(rep(0,m),rep(1,n)) ns.test(data,group) S=sum(qnorm(R[(m+1):N]/(N+1))) DS=m*n/N/(N-1)*sum(qnorm((1:N)/(N+1))^2) t=abs(S/sqrt(DS)) #testova statistika 2*(1-pnorm(t)) #asymptoticka p-hodnota #(e) medianovy test - presny t=sum(sign(R[(m+1):N]/(N+1)-1/2)) #testova statistika W=apply(sign(X/(N+1)-1/2),2,sum) a=table(W)/K #pravdepodobnostni funkce 2*sum(a[1:3]) #presna p-hodnota #(f) medianovy test - asymptoticky R=rank(c(x,y)) S=sum(sign(R[(m+1):N]/(N+1)-1/2)) DS=m*n/N t=abs(S/sqrt(DS)) #testova statistika 2*(1-pnorm(t)) #asymptoticka p-hodnota #(g+h) Kolmogorovuv-Smirnovuv test z <- cumsum(ifelse(order(c(x,y)) <= m, 1/m, -1/n)) z #hodnoty rozdilu emp. distribucnich funkci v bodech Z_(1),...,Z_(N) D=max(abs(z)) D ks.test(x,y,exact=F) t=sqrt(m*n/(m+n))*D #testova statistika 2*exp(-2*t^2) #priblizna p-hodnota #(2) m=10 n=15 N=n+m opak=10000 zam1=0 zam2=0 zam3=0 zam4=0 zam5=0 set.seed(1234) for (i in 1:opak){ x=rnorm(m) y=rnorm(n)+0.5 data=c(x,y) group=c(rep(0,m),rep(1,n)) if (t.test(x,y)$p.val<0.05) zam1=zam1+1 if (wilcox.test(x,y)$p.val<0.05) zam2=zam2+1 if (ns.test(data,group)$p<0.05) zam3=zam3+1 R=rank(data) S=sum(sign(R[(m+1):N]/(N+1)-1/2)) T=abs(S/sqrt(m*n/(N-1))) if (abs(T)>qnorm(0.975)) zam4=zam4+1 if (ks.test(x,y)$p.val<0.05) zam5=zam5+1 } zam1/opak*100 #t-test zam2/opak*100 #Wilcoxonuv test zam3/opak*100 #van der Waerdenuv test zam4/opak*100 #medianovy test zam5/opak*100 #Kolmogorovuv-Smirnovuv test