Solved – Permutation tests in R for correlations

bonferronicorrelationmultiple-comparisonspermutation-testr

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.

Best Answer

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

library(plyr)

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()) )

hist(out)
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.