Guo Wei-Wei
2006-Jul-10 05:27 UTC
[R] A possible too old question on significant test of correlation matrix
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
Gavin Simpson
2006-Jul-10 07:39 UTC
[R] A possible too old question on significant test of correlation matrix
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-WeiHi, 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/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dimitris Rizopoulos
2006-Jul-10 07:57 UTC
[R] A possible too old question on significant test of correlationmatrix
you can use function rcor.test() from package 'ltm', e.g., help(rcor.test, package = "ltm") ### library(ltm) dat <- data.frame(matrix(rnorm(1000), 100, 10)) rcor.test(dat) rcor.test(dat, method = "kendall") rcor.test(dat, method = "spearman") I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Guo Wei-Wei" <wwguocn at gmail.com> To: <r-help at stat.math.ethz.ch> Sent: Monday, July 10, 2006 7:27 AM Subject: [R] A possible too old question on significant test of correlationmatrix> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm