Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A matrix version of cor.test()

Tags:

r

correlation

Cor.test() takes vectors x and y as arguments, but I have an entire matrix of data that I want to test, pairwise. Cor() takes this matrix as an argument just fine, and I'm hoping to find a way to do the same for cor.test().

The common advice from other folks seems to be to use cor.prob():

https://stat.ethz.ch/pipermail/r-help/2001-November/016201.html

But these p-values are not the same as those generated by cor.test()!!! Cor.test() also seems better equipped to handle pairwise deletion (I have quite a bit of missing data in my data set) than cor.prob().

Does anybody have any alternatives to cor.prob()? If the solution involves nested for loops, so be it (I'm new enough to R for even this to be problematic for me).

like image 460
Atticus29 Avatar asked Oct 28 '12 19:10

Atticus29


People also ask

What is the COR test?

Use the function cor. test(x,y) to analyze the correlation coefficient between two variables and to get significance level of the correlation. Three possible correlation methods using the function cor.test(x,y): pearson, kendall, spearman.

What does Cor test tell you in R?

The test statistic in a correlation test is called a correlation coefficient and is represented by the letter r. The coefficient can range from -1 to +1, with -1 meaning a strong negative relationship, and +1 meaning a strong positive relationship.

What is a correlation matrix?

A correlation matrix is simply a table which displays the correlation coefficients for different variables. The matrix depicts the correlation between all the possible pairs of values in a table. It is a powerful tool to summarize a large dataset and to identify and visualize patterns in the given data.


2 Answers

corr.test in the psych package is designed to do this:

library("psych") data(sat.act) corr.test(sat.act) 

As noted in the comments, to replicate the p-values from the base cor.test() function over the entire matrix, then you need to turn off adjustment of the p-values for multiple comparisons (the default is to use Holm's method of adjustment):

 corr.test(sat.act, adjust = "none") 

[But be careful when interpreting those results!]

like image 146
Sacha Epskamp Avatar answered Oct 04 '22 08:10

Sacha Epskamp


If you're strictly after the pvalues in a matrix format from cor.test here's a solution shamelessly stolen from Vincent (LINK):

cor.test.p <- function(x){     FUN <- function(x, y) cor.test(x, y)[["p.value"]]     z <- outer(       colnames(x),        colnames(x),        Vectorize(function(i,j) FUN(x[,i], x[,j]))     )     dimnames(z) <- list(colnames(x), colnames(x))     z }  cor.test.p(mtcars) 

Note: Tommy also provides a faster solution though less easy to impliment. Oh and no for loops :)

Edit I have a function v_outer in my qdapTools package that makes this task pretty easy:

library(qdapTools) (out <- v_outer(mtcars, function(x, y) cor.test(x, y)[["p.value"]])) print(out, digits=4)  # for more digits 
like image 36
Tyler Rinker Avatar answered Oct 04 '22 07:10

Tyler Rinker