Goto Lab: | #0 | #1 | #2 | #3 |
For the most part, this assignment is homework problems out of the text. In addition, there's additional Splus computations that go with Exercise 18.
> ypray_c(20,15,19,14,17,19) > ycont_c(19,14,17,13,15,22)
> diff0_mean(ypray) - mean(ycont) > diff0 [1] 0.6666667
> ally_c(ypray,ycont) > ally [1] 20 15 19 14 17 19 19 14 17 13 15 22
> yshuf_sample(ally,size=12,replace=F) > yshuf [1] 19 19 15 20 14 15 17 14 19 13 17 22
> mean(yshuf[1:6]) - mean(yshuf[7:12]) [1] 0so the observed difference was larger; let's repeat this random permuting of labels and calculating the mean difference 1000 times.
> diff_rep(NA,1000)
> for(i in 1:1000){ yshuf_sample(ally,size=12,replace=F) diff[i]_mean(yshuf[1:6]) - mean(yshuf[7:12]) }...a quick hint here. Try it out with 10 or 1 iterations. Once you get that to work, then go for 1000.
> hist(diff)and mark the value of the actual statistic
> points(diff0,0,pch=16,cex=3)
> larger_ifelse(diff >= diff0,1,0)Now larger is a vector of 1's and 0's; 1 if the permutation test statistic was larger; 0 if not. The proportion of permutation statistics that are larger is the mean of this vector of 0's and 1's.
> mean(larger) [1] 0.373*Note this is a "one sided" p-value since we expected the treatment (prayer) group to have a larger mean a priori. If we didn't have a preconception, a two-sided p-value would have been more appropriate. In this case, we look for the chance of seeing anything more extreme that what was observed.
> extremer_ifelse(abs(diff) >= abs(diff0),1,0) > mean(extremer)*A second note: The test statistic here was the difference between means. Instead we could just as easily have taken the difference between medians, average ranks, variances, or whatever.
> diff0_median(ypray) - median(ycont) > for(i in 1:1000){ yshuf_sample(ally,size=12,replace=F) diff[i]_median(yshuf[1:6]) - median(yshuf[7:12]) }