HAKAN DEMIRTAS
2010-Oct-21 22:50 UTC
[R] how do I make a correlation matrix positive definite?
Hi, If a matrix is not positive definite, make.positive.definite() function in corpcor library finds the nearest positive definite matrix by the method proposed by Higham (1988). However, when I deal with correlation matrices whose diagonals have to be 1 by definition, how do I do it? The above-mentioned function seem to mess up the diagonal entries. [I haven't seen this complication, but obviously all entries must remain in (-1,1) range after conversion.] Any R tools to handle this? I'd appreciate any help. Hakan Demirtas [[alternative HTML version deleted]]
Jeremy Miles
2010-Oct-21 22:54 UTC
[R] how do I make a correlation matrix positive definite?
You could use cov2cor() to convert from covariance matrix to correlation matrix. If the correlation is >1, the matrix won't be positive definite, so you can restandardize the matrix to get a pos def correlation matrix. Jeremy On 21 October 2010 15:50, HAKAN DEMIRTAS <demirtas at uic.edu> wrote:> Hi, > > If a matrix is not positive definite, make.positive.definite() function in corpcor library finds the nearest positive definite matrix by the method proposed by Higham (1988). > > However, when I deal with correlation matrices whose diagonals have to be 1 by definition, how do I do it? The above-mentioned function seem to mess up the diagonal entries. [I haven't seen this complication, but obviously all entries must remain in (-1,1) range after conversion.] > > Any R tools to handle this? > > I'd appreciate any help. > > Hakan Demirtas > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jeremy Miles Psychology Research Methods Wiki: www.researchmethodsinpsychology.com
Peter Langfelder
2010-Oct-22 00:03 UTC
[R] how do I make a correlation matrix positive definite?
On Thu, Oct 21, 2010 at 3:50 PM, HAKAN DEMIRTAS <demirtas at uic.edu> wrote:> Hi, > > If a matrix is not positive definite, make.positive.definite() function in corpcor library finds the nearest positive definite matrix by the method proposed by Higham (1988). > > However, when I deal with correlation matrices whose diagonals have to be 1 by definition, how do I do it? The above-mentioned function seem to mess up the diagonal entries. [I haven't seen this complication, but obviously all entries must remain in (-1,1) range after conversion.] > > Any R tools to handle this? > > I'd appreciate any help.Well, I can't provide immediate help, but I'm curious where the correlation matrices are coming from. Autocorrelation matrices (i.e., cor(x)) are always positive semi-definite (unless you have missing data and you specify use = "pairwise.complete.observations", in which case you may get some negative eigenvalues). The correlation matrix you provided seems to be inconsistent in the sense that cor (x[, 1], x[, 2]) is -0.95, cor(x[, 2], x[, 3]) is -0.81, but cor(x[, 1], x[, 3]) is only -0.25. Using basic geometry arguments one can show that given the 1,2 and 2,3 correlations, the 1,3 correlation must be at least 0.58. Again, you may get such an apparently inconsistent correlation matrix if you use "pairwise.complete.observations" and you have enough missing data. So one question is whether you do use "pairwise.complete.observations" when you calculate the matrix. If yes, you may want to try specifying "complete.observations" which will remove more observations but may solve your problem. Peter
beyza doganay
2010-Oct-22 10:14 UTC
[R] how do I make a correlation matrix positive definite?
Hi, If a matrix is not positive definite, make.positive.definite() function in corpcor library finds the nearest positive definite matrix by the method proposed by Higham (1988). However, when I deal with correlation matrices whose diagonals have to be 1 by definition, how do I do it? The above-mentioned function seem to mess up the diagonal entries. [I haven't seen this complication, but obviously all entries must remain in (-1,1) range after conversion.] Any R tools to handle this? I'd appreciate any help. -- Beyza Doganay [[alternative HTML version deleted]]
Ravi Varadhan
2010-Oct-22 17:36 UTC
[R] how do I make a correlation matrix positive definite?
I responded to another question that asked the exact same question. So, I will repeat my answer here: Nick Higham (2002) discusses algorithms for this. One of the algorithms discussed in the paper is implemented in the "Matrix" package as `nearPD' function. library(Matrix) ?nearPD Ravi. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of HAKAN DEMIRTAS Sent: Thursday, October 21, 2010 6:50 PM To: r-help at r-project.org Subject: [R] how do I make a correlation matrix positive definite? Hi, If a matrix is not positive definite, make.positive.definite() function in corpcor library finds the nearest positive definite matrix by the method proposed by Higham (1988). However, when I deal with correlation matrices whose diagonals have to be 1 by definition, how do I do it? The above-mentioned function seem to mess up the diagonal entries. [I haven't seen this complication, but obviously all entries must remain in (-1,1) range after conversion.] Any R tools to handle this? I'd appreciate any help. Hakan Demirtas [[alternative HTML version deleted]] ______________________________________________ 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.