### R examples for nonparametric statistics ### Source: Hollander M, Wolfe D, Nonparametric statistical methods, 2nd edition, Wiley ### Course: APMA168-Nonparametric Statistics, Brown University ### Instructor: Konstantinos Spiliopoulos #boots.tau is an R program that computes an asymptotically #distribution-free confidence interval for Kendall's tau #based on Efron's bootstrap method. See Section 8.4. One must specify, #using the paired data Z_1=(X_1,Y_1), ..., (X_n,Y_n), the vector #x=(X_1,...,X_n), the vector y=(Y_1,...,Y_n), alpha, where 1-alpha is the #confidence coefficient, and B, the number of bootstrap #replications. The output contains the list of the ordered bootstrap replications as #well as the lower and upper limits of the confidence interval. boots.tau<-function(x,y,alpha,B) { alpha<-(alpha/2) n<-length(x) #(x,y) is the sample values # 1-alpha is the confidence coefficient #B is the size of the bootstrap sample, e.g. B=1000 xnew<-numeric() ynew<-numeric() taunew<-numeric() #Main loop that generates the bootsrtap sample for(i in 1:B) { a<-ceiling(10*runif(n,0,n/10)) for(j in 1:n) { xnew[j]<-x[a[j]] ynew[j]<-y[a[j]] } # calculating the tau's for the samples (xnew,ynew) using the function "tau" taunew[i]<-tau(xnew,ynew) } taunew taunew<-sort(taunew) #Getting the quantiles L<-taunew[trunc((alpha)*(B+1))] b<-trunc((alpha)*(B+1)) U<-taunew[(B+1-b)] #Create a list of objects containing the vector "taunew" # and the quantiles L and U #To extract them use sum.stat[[]] sum.stat<-list(taunew,L,U) sum.stat } tau<-function(x,y) { n<-length(x) s<-0 p<-0 for(i in 1:(n-1)) { for(j in (i+1):n) { a<-(x[i]-x[j])*(y[i]-y[j]) if(a>0) s<-s+1 else if(a==0) s<-s else s<-s-1 } } p<-(2*s)/(n*(n-1)) p } ### Application to canned Tuna data of table 8.1 Hunter <- c(44.4, 45.9, 41.9, 53.3, 44.7, 44.1, 50.7, 45.2, 60.1) Panel <- c( 2.6, 3.1, 2.5, 5.0, 3.6, 4.0, 5.2, 2.8, 3.8) example<-boots.tau(Hunter,Panel, 0.1,1000) hist(example[[1]]) ###The code is from the book of Hollander-Wolfe.