10-29 R code
Thursday, 30 October 2008 01:03

Here's the R code from class today, covering two different kinds of nonparametric randomization tests.

 

A = c(13.2, 8.2, 10.9, 14.3, 10.7, 6.6, 9.5, 10.8, 8.8, 13.3)
B = c(14, 8.8, 11.2, 14.2, 11.8, 6.4, 9.8, 11.3, 9.3, 13.6)

Diff = A - B
Dlist = NULL

for (lc in 1:10000)
{
    flips = 2*rbinom(10, 1, 0.5) - 1
    Dlist = c(Dlist, mean(Diff*flips))
}


md = mean(Diff)

sum(Dlist<md)/10000
sum(Dlist>-md)/10000

#########

library(boot)

head(claridge)
hist(claridge$hand)

RH = mean(claridge[claridge[,2]==1, 1])
LH = mean(claridge[claridge$hand!=1, 1])

HDlist = NULL

for(i in 1:50000)
{
    hand_r = sample(claridge$hand)
    HDlist = c(HDlist, mean(claridge[hand_r == 1,1]) - mean(claridge[hand_r!=1,1]))
}

sum( (RH-LH) > HDlist)/10000
sum( -(RH-LH) < HDlist)/10000

sum( (RH-LH) > HDlist)/50000 + sum( -(RH-LH) < HDlist)/50000