I have a covariance matrix that is not positive semi-definite matrix and I need it to be via some sort of adjustment. Is there any R routine or package to help me do this? Thanks, Roger [[alternative HTML version deleted]]
On 7/21/2006 8:59 AM, roger bos wrote:> I have a covariance matrix that is not positive semi-definite matrix and I > need it to be via some sort of adjustment. Is there any R routine or > package to help me do this?I think you need to be more specific about what have and what you want, but if the matrix is symmetric and nearly positive semi-definite (but not exactly because of rounding error), you might try something like fixit <- function(A) { eig <- eigen(A, symmetric = TRUE) eig$values <- pmax(0, eig$values) return(eig$vectors %*% diag(eig$values) %*% t(eig$vectors)) } Rounding error means this is not guaranteed to be positive semi-definite, but it will be very close. Duncan Murdoch
>>>>> "Duncan" == Duncan Murdoch <murdoch at stats.uwo.ca> >>>>> on Fri, 21 Jul 2006 09:44:42 -0400 writes:Duncan> On 7/21/2006 8:59 AM, roger bos wrote: >> I have a covariance matrix that is not positive semi-definite matrix and I >> need it to be via some sort of adjustment. Is there any R routine or >> package to help me do this? Duncan> I think you need to be more specific about what have and what you want, Duncan> but if the matrix is symmetric and nearly positive semi-definite (but Duncan> not exactly because of rounding error), you might try something like Duncan> fixit <- function(A) { Duncan> eig <- eigen(A, symmetric = TRUE) Duncan> eig$values <- pmax(0, eig$values) Duncan> return(eig$vectors %*% diag(eig$values) %*% t(eig$vectors)) Duncan> } Duncan> Rounding error means this is not guaranteed to be positive Duncan> semi-definite, but it will be very close. A slightly more general and stable version of the above is available via sfsmisc::posdefify(.) : install.packages("sfsmisc") ?posdefify Martin Maechler, ETH Zurich
Take a look at make.positive.definite in the corpcor package. The implementation is very similar to what Duncan suggested. Regards, Francisco Dr. Francisco J. Zagmutt College of Veterinary Medicine and Biomedical Sciences Colorado State University>From: Duncan Murdoch <murdoch at stats.uwo.ca> >To: roger bos <roger.bos at gmail.com> >CC: RHelp <r-help at stat.math.ethz.ch> >Subject: Re: [R] positive semi-definite matrix >Date: Fri, 21 Jul 2006 09:44:42 -0400 > >On 7/21/2006 8:59 AM, roger bos wrote: > > I have a covariance matrix that is not positive semi-definite matrix and >I > > need it to be via some sort of adjustment. Is there any R routine or > > package to help me do this? > >I think you need to be more specific about what have and what you want, >but if the matrix is symmetric and nearly positive semi-definite (but >not exactly because of rounding error), you might try something like > >fixit <- function(A) { > eig <- eigen(A, symmetric = TRUE) > eig$values <- pmax(0, eig$values) > return(eig$vectors %*% diag(eig$values) %*% t(eig$vectors)) >} > >Rounding error means this is not guaranteed to be positive >semi-definite, but it will be very close. > >Duncan Murdoch > >______________________________________________ >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 >and provide commented, minimal, self-contained, reproducible code.