Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

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
share|improve this question

migrated from stackoverflow.com Oct 23 '13 at 3:41

This question came from our site for professional and enthusiast programmers.

1 Answer 1

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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