Assumptions | Test to use |
---|---|
independent observations | sign test |
i .i .d. observations | Wilcoxon signed rank test |
i.i.d. normal observations | t test |
The previous tests also apply to paired data. We have independent paired observations (X1,Y1,),...,(Xn,Yn). We assume that Zi=Yi-Xi has a symmetric distribution.
To do a t test, we use:
t.test(x, y=NULL, alternative="two.sided", mu=0, paired=F, var.equal=T, conf.level=.95)
This command does the z-test and the t-test. If y is not indicated, we get the one sample case. The argument alternative has the options: "greater", "less" or "two.sided".
To do a signed rank Wilcoxon test for paired data, we can do the following. Suppose that we want to test Ho:theta=thetao versus H1:theta<thetao, from (X1,Y1,),...,(Xn,Yn). We can do a sign test, a signed rank Wilcoxon test for paired data and a t-test.
************************* > x_scan() 1: 1.83 0.50 1.62 2.48 1.68 1.88 1.55 3.06 1.30 10: > y_scan() 1: 0.878 0.647 0.598 2.050 1.060 1.290 1.060 3.140 1.290 10: > z_y-x > sum(z>0) [1] 2 > pbinom(2,size=9,p=0.5) [1] 0.08984375 >sum(rank(abs(z))*(z>0)) [1] 5 > wilcox.test(x, y, alternative="greater",paired=T) Exact Wilcoxon signed-rank test data: x and y signed-rank statistic V = 40, n = 9, p-value = 0.0195 alternative hypothesis: true mu is greater than 0 > t.test(x, y, alternative="greater", mu=0, paired=T, conf.level=.95) Paired t-Test data: x and y t = 3.0354, df = 8, p-value = 0.0081 alternative hypothesis: true mean of differences is greater than 0 95 percent confidence interval: 0.1673028 NA sample estimates: mean of x - y 0.4318889 *************************The p-value of the sign test is 0.08984375. This is not enough evidence to reject Ho at the level 0.05. We got 2 positive signs in 9 observations.
The p-value of the Wilcoxon signed rank test is 0.0195. There is enough evidence to reject Ho at the level 0.05. The sum of the positive ranks is small enough: it is only 5. Even, when we get 2 positive signs. These values are small compared to the rest.
The p-value of the t-test is 0.0081. There is enough evidence to reject Ho at the level 0.05.
For more information in the command wilcox.test, do
> help(wilcox.test)The distribution of the Wilcoxon signed rank statistics is not found in Splus, you can find the Wilcoxon signed rank statistic using this program. Instead of 6, you plug, the corresponding n.
********3a********************************** n_6 nn_2**n v_rep(0,times=nn) for(i in 1:nn){ for(j in 1:n){ l_((i-1)%%(2**j))+1 m_(2**(j-1))+.5 k_0 if (l>m) k_1 v[i]_v[i]+(k*j) } } v1_sort(v) a1_rle(v1)$values a2_rle(v1)$lengths a2_a2/nn a3_cumsum(a2) a4_1-a3 a_cbind(a1,a2,a3,a4) dimnames(a)_list(c(NULL),c("value","probab.","cdf","1-cdf")) ******************************************This program find the exact distribution of the Wilcoxon signed rank statistic. However, it takes a lot of computer time. You can do estimate the distribution of the Wilcoxon signed rank statistic using simulations:
*******3b************* n_6 N_10000 alpha_.05 rm(xb) xb_c(1:N) for(i in 1:N){ xb[i]<-sum(c(1:n)*rbinom(n,1,.5)) } res<- quantile(xb,probs=c(alpha/2,1-alpha/2)) names(res)_c("t(1-alpha/2)","t(alpha/2)") xb_sort(xb) a1_rle(xb)$values a2_rle(xb)$lengths a2_a2/N a3_cumsum(a2) a4_1-a3 a_cbind(a1,a2,a3,a4) dimnames(a)_list(c(NULL),c("value","probab.","cdf","1-cdf")) *********************The following programs finds theta-hat and a 95 % confidence interval for theta using the data in Example 3.1 of the textbook.
*******3c************* x_c(1.83,.5,1.62,2.48,1.68,1.88,1.55,3.06,1.3) y_c(.878,.647,.598,2.05,1.06,1.29,1.06,3.14,1.29) z_y-x zabs_abs(z) ra_rank(zabs) si_(sign(z)+1)/2 v_sum(si*ra) n_length(x) rm(aver,con1,con2,conf) aver_c((z[1]+z[1])/2) for(j in 1:n){ for(i in 1:j){ aver_append(aver,(z[i]+z[j])/2) } } aver_aver[-1] aver_sort(aver) theta_median(aver) zalpha_qnorm(.975) calpha1_((n*(n+1)/4)-zalpha*sqrt(n*(n+1)*(2*n+1)/24)-1/2) calpha1_round(calpha1) calpha2_((n*(n+1)/2)+1-calpha1) diff_sort(differ) con1_aver[calpha1] con2_aver[calpha2] conf_c(con1,con2) *********************We run this program and get theta-hat=-0.46, theta-L=-0.806 and that-U=0.035
>source("3c") > theta [1] -0.46 > conf [1] -0.806 0.035