# Permutation test for relationship with R

I want to do a permutation test for the significance of a relationship between x and y with a manual function. And I don't want to generate all possible permutations but a sample of them. Before I can compute the p-value I have to generate resamples that are consistent with the null hypothesis of no relationship. My solution for this is the following code:

> resamples <- replicate(n, sample(y,size=length(y),replace=FALSE))


The problem is, that I am not sure, if this is correct. I read in a book on the internet:

"Choose permutation resamples from the data without replacement".

Is it false if the observations (of one sample) are chosen without replacement but not my resamples. Because my function above generates sometimes the same resamples by chance.

• It is unclear what is your question, could you clarify? What is false? What is your question about? Function sample takes a random sample from a vector of values so it can return duplicated values.
– Tim
May 15, 2016 at 6:58
• The phrase "without replacement": is it related to the observations of one resample or means "without replacement" that all resamples have to be different. May 15, 2016 at 8:11
• I think you should clarify whether this question is primarily about coding (in which case it would be off-topic here but we could migrate it to Stack Overflow for you - have a look at our help center for more details about what questions are within the scope of our site) or whether your main issue is the underlying statistical one. For what it's worth, I think there is enough statistical content in this question to justify it staying on CV. May 15, 2016 at 10:23
• There is a distinction to be made between bootstrapping and a permutation test: Bootstrapping samples with replacement. A permutation test just shuffles the data in y relative to x so as to break the pairing / relationship between them; for a permutation test, you sample with replacement. Your code is appropriate for a permutation test, but it does not implement bootstrapping. May 15, 2016 at 11:48

As you correctly noticed, in R's sample function there is parameter replace to declare if you want to sample with (TRUE), or without (FALSE) replacement.