R/02_algebrahelpers_spectralshift.R

Defines functions spectralshift

spectralshift <- function(x){
# warning("'spectralshift' should be removed!")
  # # Make matrix:
  # if (!is.matrix(x)){
  #   x <- as.matrix(x)
  # }
  # 
  # # Make symmetric:
  # if (!all(x == t(x))){
  #   x <- 0.5 * (x + t(x))
  # }
  # 
  # If anything NA, just return an identity matrix:
  if (any(!is.finite(x))){
    return(Diagonal(n=nrow(x)))
  }
  
  # If all eigenvalues are good, stop:
  if (!any(Re(eigen(x)$values) < 0)){
    return(as.matrix(x))
  }
  
  # Spectral shift and return:
  x <- x - (Diagonal(n=nrow(x)) * (min(Re(eigen(x)$values)))-0.001)
  return(as.matrix(x))
}

Try the psychonetrics package in your browser

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

psychonetrics documentation built on Oct. 3, 2023, 5:09 p.m.