tags:

views:

193

answers:

1

Here is a simple randomized experiment.

In the following code I calculate the p-value under the null hypothesis that two different fertilizers applied to tomato plants have no effect in plants yields. The first random sample (x) comes from plants where a standard fertilizer has been used, while an "improved" one has been used in the plants where the second sample (y) comes from.

x <- c(11.4,25.3,29.9,16.5,21.1)
y <- c(23.7,26.6,28.5,14.2,17.9,24.3)
total <- c(x,y)
first <- combn(total,length(x))
second <- apply(first,2,function(z) total[is.na(pmatch(total,z))])
dif.treat <- apply(second,2,mean) - apply(first,2,mean)
# the first element of dif.treat is the one that I'm interested in 
(p.value <- length(dif.treat[dif.treat >= dif.treat[1]]) / length(dif.treat))

Do you know of any R function that performs tests like this one?

EDIT

# this is the equivalent independent t.test
t.test(x,y,alternative = "less",var.equal = T)
+4  A: 

The boot library is convenient for bootstrap and permutation tests, but it will not perform exact tests (which is OK most of the time). The coin library implements exact randomization tests as well.

Aniko
Thank you! I just found some `permtest` in {BHH2}. It seems to work in a very similar way. By coincidence the same tomato dataset is used as an example!!!
gd047
Deducer also does monte carlo permutation tests: perm.t.test(x,y,"mean","less")
Ian Fellows