CFWP / rags2ridges

Get ridge or die trying - 2 cents
8 stars 2 forks source link

alternative version of covML function #14

Open CFWP opened 9 years ago

CFWP commented 9 years ago

The old covML version did not recognize standardized/scaled data. Could then be the case that, when inputting scaled data, the output is not a correlation matrix.

So have made an alternative covML function (just called covML2 for now) to replace the old covML function. This alternative version returns the ML estimator of the covariance matrix when the data are not scaled, and the correlation matrix when the data are indeed scaled. Code:

covML2 <- function (Y){
  if (!is.matrix(Y)) {
    stop("Input (Y) should be a matrix")
  }
  else {
    if (all(round(diag(var(Y)), 10) == 1)){
      Sml <- cor(Y)
    } else {
      Ys <- scale(Y, center = TRUE, scale = FALSE)
      Sml <- (t(Ys) %*% Ys)/nrow(Ys)
    }
    return(Sml)
  }
}

Anders, what's your professional opinion on this?

AEBilgrau commented 9 years ago

Hm, I'm unsure. I don't mind the old version. Suppose you want the _cov_ariance matrix of the scaled data and not the _cor_relation matrix, then you'd have to do it yourself. But maybe that's an unreasonable scenario? With the old covML, I think that a note or warning section in the documentation describing the issue will suffice. An example also in the documentation that shows how to get the correlation matrix would then be nice.

CFWP commented 9 years ago

Does not everyone expect the covariance matrix of the scaled data to be the correlation matrix? In a sense, the old covML on scaled data gives a weighted correlation matrix. It's probably rare for users to want that. In that respect, the alternative version may be preferable. What do you think?