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开发者_JAVA技巧 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)
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.
library(perm)
permTS(x,y,alternative = "less")
data: x and y
p-value = 0.3333
alternative hypothesis: true mean x - mean y is less than 0
sample estimates:
mean x - mean y
-1.693333
精彩评论