Hi. I have a 925 by 925 correlation matrix corM. I want to identify all variables that have correlation greater than 0.9. Can anyone suggest an "R way" of doing this? Thank you. -- View this message in context: http://www.nabble.com/search-through-a-matrix-tp23153538p23153538.html Sent from the R help mailing list archive at Nabble.com.
onyourmark wrote:> Hi. I have a 925 by 925 correlation matrix corM. I want to identify all > variables that have correlation greater than 0.9. Can anyone suggest an "R > way" of doing this? > > Thank you.Example: # prepare example data: x <- matrix((1:25) / 25, 5, 5) rownames(x) <- letters[1:5] colnames(x) <- letters[1:5] # solution cbind(rownames(x)[row(x)[x > 0.9]], colnames(x)[col(x)[x > 0.9]]) Uwe Ligges
Uwe Ligges wrote:> > > onyourmark wrote: >> Hi. I have a 925 by 925 correlation matrix corM. I want to identify all >> variables that have correlation greater than 0.9. Can anyone suggest >> an "R >> way" of doing this? >> >> Thank you. > > Example: > > # prepare example data: > x <- matrix((1:25) / 25, 5, 5) > rownames(x) <- letters[1:5] > colnames(x) <- letters[1:5] > > # solution > cbind(rownames(x)[row(x)[x > 0.9]], colnames(x)[col(x)[x > 0.9]])or even which(x > 0.9, arr.ind = TRUE) Best, Dimitris> > Uwe Ligges > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Thanks very much. I don't really understand the row() function. I looked in the reference but I don't really get it. It says: Description Returns a matrix of integers indicating their row number in a matrix-like object, or a factor indicating the row labels. Usage row(x, as.factor = FALSE) Arguments x a matrix-like object, that is one with a two-dimensional dim. I don't understand what row() does. And in the example in the documentation it says: x <- matrix(1:12, 3, 4) # extract the diagonal of a matrix dx <- x[row(x) == col(x)] dx [1] 1 5 9 I thought the single square bracket notation accepts a pair separated by a comma but I don't see how row(x)==col(x) produces that? Thanks again. onyourmark wrote:> > Hi. I have a 925 by 925 correlation matrix corM. I want to identify all > variables that have correlation greater than 0.9. Can anyone suggest an > "R way" of doing this? > > Thank you. >-- View this message in context: http://www.nabble.com/search-through-a-matrix-tp23153538p23155104.html Sent from the R help mailing list archive at Nabble.com.
I changed the condition to: highlyCor<-cbind(rownames(x)[row(x)[((x > 0.9)|(x<(-.9)))]], colnames(x)[col(x)[((x > 0.9)|(x<(-.9)))]]) Actually what I am trying to do is run factor analysis on this 923 by 925 x matrix but many of the variables where too highly correlated and so I got some message about singular and that the factanal could not run. Is there a way to choose to remove one variable from each pair of highly correlated variables (say variables with greater than .9 or less than -.9 correlation) and receive a new smaller x matrix which I can then do factor analysis on? Thanks. Uwe Ligges-3 wrote:> > > > onyourmark wrote: >> Hi. I have a 925 by 925 correlation matrix corM. I want to identify all >> variables that have correlation greater than 0.9. Can anyone suggest an >> "R >> way" of doing this? >> >> Thank you. > > Example: > > # prepare example data: > x <- matrix((1:25) / 25, 5, 5) > rownames(x) <- letters[1:5] > colnames(x) <- letters[1:5] > > # solution > cbind(rownames(x)[row(x)[x > 0.9]], colnames(x)[col(x)[x > 0.9]]) > > > Uwe Ligges > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/search-through-a-matrix-tp23153538p23169908.html Sent from the R help mailing list archive at Nabble.com.