# S-PLUS hints for assignment due on March 25 # # Problem 1, Exercise 9.2.10 # (a) The menu options Statistics:Compare Samples:One Sample:t test # opens a dialog box to fill in to do a t test. # (b) To do a sign test for variable conc in data frame prob1 # with the null hypothesis that mu = 2.6 # versus the alternative that mu > 2.6, you want to count the number # of observations greater than 2.6 and find the sum of binomial probabilities # with n equal to the sample size and p = 0.5 greater than or equal to this # count. A sequence of S-PLUS commands that do this as follows. # this allows you to refer to variables in prob1 by name attach(prob1) # create a vector of signs x <- sign(conc - 2.6) # count the positive signs count <- sum(x == 1) # find the sum of the binomial probabilities n <- length(x) sum(dbinom(count:n,n,0.5)) # or, alternatively 1 - pbinom(count-1,n,0.5) # A short command to do all of this in one command is 1 - pbinom(sum(sign(prob1\$conc-2.6)==1)-1,length(prob1\$conc),0.5) # (c) The menu options Statistics:Compare Samples:One Sample:Wilcoxon signed rank test # opens a dialog box to fill in to do a nonparametric Wilcoxon test. # You could mimic this in steps by doing this. attach(prob1) s <- sign(conc - 2.6) r <- rank(abs(conc - 2.6)) w <- sum(s*r) wilcox.test(conc,mu=2.6,alternative="greater",exact=T,correct=T) # Problem 2, data from Exercise 9.4.6 # (a) The menu options Statistics:Compare Samples:Two Samples:t test # opens a dialog box to fill in to do a t test. # For this data, there is a grouping variable (esp). # (c) The menu options Statistics:Compare Samples:Two Samples:Wilcoxon rank test # opens a dialog box to fill in to do a nonparametric Wilcoxon test. # You could compute the test statistic directly like this. attach(prob2) # CORRECTED ON MARCH 24! #x <- score[esp="high"] is wrong #y <- score[esp="low"] is wrong x <- score[esp=="high"] y <- score[esp=="low"] w <- sum(rank(c(x,y))[seq(along=x)]) # Problem 3, data from Exercise 9.4.12 # (a) The menu options Statistics:Compare Samples:Two Samples:t test # opens a dialog box to fill in to do a t test. # In this case, game1 and game2 are the two variables, and it is a paired t-test. # (b) To do a permutation test, you need to find the distribution of the test # statistic when the labels are randomly assigned to each pair. This code # will estimate a p-value by simulation. attach(prob3) d <- game1 - game2 # create a matrix of size 5000 by length(d) with i.i.d. equally probable +/- 1's. # # rs <- matrix(sample(c(-1,1),5000*length(d),replace=T)) THIS WAS WRONG! # # CORRECTED ON MARCH 24, 1999 rs <- matrix(sample(c(-1,1),5000*length(d),replace=T),5000,length(d)) # create an array of simulated test statistics by taking the dot product # of each row of the random signs with the array d, via matrix multiplication x <- as.vector(rs %*% d) # find a p-value for the one-sided test that mu(d) < 0 p <- sum(x <= sum(d))/5000