What's the easiest way to run permutation tests for a bivariate Pearson correlation matrix in R?

I have tried running MPT.Corr, Multivariate Permutation Test for Correlations by Urbano Blackford et al., but I am not sure this function is applicable to the problem.

The data contains mainly Likert scale items and time measures. At least one nominal variable needs to be entered as a covariate - i.e. controlled for.


This is fairly simple to do using R code without needing a special function, here is an example:


tmpfun <- function() {
    tmp <- ddply( iris, .(Species), function(df) {
        data.frame( Petal.Width=df$Petal.Width,
                    Sepal.Width=sample( df$Sepal.Width ) ) } )
    cor(tmp$Petal.Width, tmp$Sepal.Width)

out <- c( cor(iris$Petal.Width, iris$Sepal.Width), 
    replicate(999, tmpfun()) )

abline(v=out[1], col='red')
mean( out >= out[1] )

This uses the plyr package to do the permuting within levels of Species to adjust for it.

| cite | improve this answer | |
  • $\begingroup$ Thanks. Looks good. I was not aware of plyr. Migrating from SPSS, it seems - at first sight - to be an advanced version of Data/Aggregate. $\endgroup$ – noumenal Apr 27 '12 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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