[R] A possible too old question on significant test of correlation matrix

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Jul 10 09:39:05 CEST 2006


On Mon, 2006-07-10 at 13:27 +0800, Guo Wei-Wei wrote:
> Dear all,
> 
> I'm working on a data.frame named en.data, which has n cases and m columns.
> I generate the correlation matrix of en.data by
> 
> > cor(en.data)
> 
> I find that there is no p-value on each correlation in the correlation
> matrix. I searched in the R-help mail list and found some related
> posts, but I didn't find direct way to solve the problem. Someone said
> to use cor.test() or t.test(). The problem is that cor.test() and
> t.test() can only apply on two vectors, not on a data.frame or a
> matrix.
> 
> My solution is
> 
> for (i in 1:(ncol(en.data) -1)) {
>    cor.test(en.data[,i], en.data[, i+1])
> }
> 
> I think it is a stupid way. Is there a direct way to do so? After all,
> it is a basic function to generate significant level of a correlation
> in a correlation matrix.
> 
> Thank you in advance!
> Wei-Wei

Hi,

Bill Venables posted a solution to this on the R-Help list in Jan 2000.
I made a minor modification to add a class to the result and wrote a
print method (which could probably do with some tidying but it works).

E.g.:

# paste in the functions below, then
data(iris)
corProb(iris[,1:4])

## prints
Correlations are shown below the diagonal
P-values are shown above the diagonal

             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length  1.0000       0.1519      0.0000       0.0000
Sepal.Width  -0.1176       1.0000      0.0000       0.0000
Petal.Length  0.8718      -0.4284      1.0000       0.0000
Petal.Width   0.8179      -0.3661      0.9629       1.0000

Is this what you want?

HTH

G

# correlation function
# based on post by Bill Venables on R-Help
# Date: Tue, 04 Jan 2000 15:05:39 +1000
# https://stat.ethz.ch/pipermail/r-help/2000-January/009758.html
# modified by G L Simpson, September 2003
# version 0.2: added print.cor.prob
#              added class statement to cor.prob
# version 0.1: original function of Bill Venables
corProb <- function(X, dfr = nrow(X) - 2) {
    R <- cor(X)
    above <- row(R) < col(R)
    r2 <- R[above]^2
    Fstat <- r2 * dfr / (1 - r2)
    R[above] <- 1 - pf(Fstat, 1, dfr)
    class(R) <- "corProb"
    R
}
print.corProb <- function(x, digits = getOption("digits"), quote = FALSE, na.print = "", 
    justify = "none", ...) {
    xx <- format(unclass(round(x, digits = 4)), digits = digits, justify = justify)
    if (any(ina <- is.na(x))) 
        xx[ina] <- na.print
    cat("\nCorrelations are shown below the diagonal\n")
    cat("P-values are shown above the diagonal\n\n")
    print(xx, quote = quote, ...)
    invisible(x)
}

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 *Note new Address and Fax and Telephone numbers from 10th April 2006*
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/cv/
WC1E 6BT                          [w] http://www.ucl.ac.uk/~ucfagls/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list