Skip to content
Prev 95766 / 398500 Next

A possible too old question on significant test of correlation matrix

On Mon, 2006-07-10 at 13:27 +0800, Guo Wei-Wei wrote:
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)
}