R/repairMatrix.R

repairMatrix <- function(C) {
    ## compute eigenvectors/-values
    E <- eigen(C, symmetric = TRUE)   
    V <- E$vectors
    D <- E$values
    
    ## replace negative eigenvalues by zero
    D <- pmax(D, 0)
    
    ## reconstruct correlation matrix
    BB <- V %*% diag(D) %*% t(V)
    
    ## rescale correlation matrix
    T <- 1/sqrt(diag(BB))
    TT <- outer(T,T)
    C <- BB * TT
    C
}

Try the NMOF package in your browser

Any scripts or data that you put into this service are public.

NMOF documentation built on May 2, 2019, 6:39 p.m.