## Section 6.1 examples # Example 1: observ <- c(0.203, 0.329, 0.382, 0.477, 0.480, 0.503, 0.554, 0.581, 0.621, 0.710) ks.test(observ, "punif", 0, 1, alternative = "two.sided", exact=T) # Example 2: tumor.counts <- c(2,2,4,1,3,1,4,0,2,2,1,1,0,2,2,3,3,3) mean(tumor.counts) ks.test(tumor.counts,"ppois",1.75,alternative="less",exact=T) ###### Confidence band for the true c.d.f.: my.ecdf <- ecdf(observ) plot(my.ecdf) # w_0.95 value from Table A13 is 0.409: myseq <- seq(0,1,by=.001) # The 'by' value should be small for a nice-looking plot lines(myseq,pmax(0,my.ecdf(myseq)-.409),col='red') lines(myseq,pmin(1,my.ecdf(myseq)+.409),col='red') ################################################################ # Section 6.2 examples #### Lilliefors Test for Normality: ks.test.stat <- function(x){ mu.hat<-mean(x); sigma.hat <- sd(x); out <- ks.test(x, "pnorm", mean = mu.hat, sd = sigma.hat)$statistic return(out) } lilliefors.normal.pval <- function(x, nsimul=10000){ mu.hat<-mean(x); sigma.hat <- sd(x); n <- length(x) T1 <- ks.test.stat((x-mu.hat)/sigma.hat) mymat<-matrix(rnorm(nsimul*n),nrow=nsimul, ncol=n) T1distn <- apply(mymat,1,ks.test.stat) pval <- mean(T1 < T1distn) out <- list(test.stat=T1, p.value=pval) return(out) } # Trying it on the page 445 data: my.data.445 <- c(23,23,24,27,29,31,32,33,33,35,36,37,40,42,43,43,44,45,48,48,54,54,56,57,57,58,58,58,58,59,61,61,62,63,64,65,66,68,68,70,73,73,74,75,77,81,87,89,93,97) lilliefors.normal.pval(my.data.445) # Trying it on some simulated data sets: my.data.t <- rt(30,df=4) my.data.norm <- rnorm(30, mean=30, sd=3) my.data.exp <- rexp(30, rate=1) lilliefors.normal.pval(my.data.t) lilliefors.normal.pval(my.data.norm) lilliefors.normal.pval(my.data.exp) #### Lilliefors Test for Exponential Distribution: lilliefors.expon.ts <- function(x){ z<- x/mean(x) ecdf.zs<-ecdf(z); out <- max(c(ecdf.zs(z)-pexp(z), pexp(z)-ecdf.zs(z-.001))) return(out) } wait.times <- faithful$waiting length(wait.times) lilliefors.expon.ts(wait.times) ## Shapiro-Wilk Test for Normality: shapiro.test(my.data.445) ###################################################### # Section 6.3 Examples ## Example 1 (p. 460 data) x460 <- c(7.6, 8.4, 8.6, 8.7, 9.3, 9.9, 10.1, 10.6, 11.2) y460 <- c(5.2, 5.7, 5.9, 6.5, 6.8, 8.2, 9.1, 9.8, 10.8, 11.3, 11.5, 12.3, 12.5, 13.4, 14.6) ks.test(x460, y460, alternative="two.sided", exact=T) ## Example 2 unleaded <- c(21.7, 21.4, 23.3, 22.8) premium <- c(23.1, 23.5, 22.9, 23.4) ks.test(unleaded, premium, alternative="greater", exact=T)