#********************************************************************** # File name: chisq.q * # For: This program is only useful for data analysis of Wayne Osgood * # data set since it specifically uses the coding of the * # covariate which takes values from 4-18. It can be modifeid * # for any kind of discrete covariates. * # Usage: In the current directory, enter Splus. Then store the * # response variable and covariate in vectors "x" and "y". * # They should be in a one-way layout regardless of the original* # design and only one index is used to keep track. They should * # also be sorted according to the categories. Then store the * # corresponding sample sizes into the vector "sizes". Ex. for a* # 2X3 design with sample sizes: * # A\B 1 2 3 * # 1 10 15 20 * # 2 25 30 35 * # "y" is arranged like (y_11(1),...,y_11(10),...,y_13(1),..., * # y_13(20),y_21(1),...,y_21(25),...,y_23(1),...,y_23(35)) and * # so is "x". "sizes" is a vector like (10,15,20,25,30,35). * # Finally, define the contrast matrix in "ctr.mat" then exit * # Splus and run "Splus BATCH anal.q anal". Using "more anal" * # in Unix, you see the P_value with the chisquare statistics. * # Programmer: Yunling Du * # Date: 4/16/98 * #********************************************************************** weight.f<-function(x){ k <- length(x) an <- 14.8/k^0.2 x.mat <- (matrix(4:21, 18, 18, byrow = T) - 4:21)/an x.mat <- replace(x.mat, abs(x.mat) > 1, 1) k.mat <- (15/16) * (1 - x.mat^2)^2 ct <- count.f(x) wt <- k.mat * ct tot <- apply(wt, 2, sum) tot <- replace(tot, tot == 0, 1) w.mat <- k.mat/tot w.mat } cal1.f<-function(mat,count){ apply(mat * count, 2, sum)/sum(count) } count.f<-function(x){ tabulate(x, 21)[ - (1:3)] } n <- sum(sizes) k <- length(sizes) set.seed(368) group <- rep(1:k,sizes) #group x<-wayne[,2] y<-wayne[,1] #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# # Calculate the test statistic # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# midrank <- rank(y) #The midranks of the responses w.arr<-lapply(split(x,group),weight.f) ct<-count.f(x) wm.arr<-lapply(w.arr,cal1.f,count=ct) wm.v<-unlist(wm.arr,use.names=F) ct1<-unlist(lapply(split(x,group),count.f),use.names=F) wm.v<-rep(wm.v,ct1) midrank<-midrank[order(group,x)] vmr.v<-(1/n)*wm.v*midrank int <- rowsum(vmr.v,group) #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# #calculate \hat \delta_{1,i}^2 and \hat \delta_{1,i_1,i_2},i=1,...,k # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# ww.mat<-matrix(unlist(w.arr,use.names=F),18,18*k) ww.mat<-apply(ww.mat,1,rep,times=ct1) ww.mat<-ww.mat*midrank/n wt2.mat<-rowsum(ww.mat,group) wt3.mat<-t(wt2.mat) wt.mat<-apply(wt3.mat,2,rep,times=ct) v1.mat<-(n-1)/n*var(wt.mat) #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# #calculate \hat \delta_{2,i}^2, i=1,...,kc # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# wt1.mat<-ww.mat*midrank/n wt1.mat<-rowsum(wt1.mat,group) wt.mat<-wt1.mat-(wt2.mat)^2 vu.mat<-rep(as.vector(t(wt.mat)),times=ct1)*wm.v^2 vu.mat<-rowsum(vu.mat,group) v2.mat<-diag(vu.mat)*n #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# #calculate the estimate of the covariance matrixc # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# v.mat <- v1.mat + v2.mat #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# #calculate the statisticc # #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-# ta <- ctr.mat%*%int cvc.mat <- solve(ctr.mat%*%v.mat%*%t(ctr.mat)) n*t(ta)%*%cvc.mat%*%ta