1
$\begingroup$

I'm trying to write a two sided permutation test to test the alternative hypothesis there is a difference in the medians of 2 independent samples. My question is this: am I calculating the p-value correctly? Thanks SO much!

sample1 <- groundwater$West[!is.na(groundwater$West)]
sample2 <- groundwater$East
ts <- median(sample1) - median(sample2)
> ts
[1] 0.105
R <- 9999
all <- c(sample1, sample2)
k <- 1:length(all)
reps <- numeric(R)
for (i in 1:R) {
    m <- sample(k, size=length(sample1), replace=FALSE)
    permsample1 <- all[m]
    permsample2 <- all[-m]
    reps[i] <- median(permsample1) - median(permsample2)
}
pvalue <- sum(c(ts, reps) <= ts)/9999
> pvalue
[1] 0.9223922
$\endgroup$
1
$\begingroup$

What you've written is a one-sided test. A two sided test would be

pvalue<-mean(abs(reps)>=ts)
| cite | improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.