R/metric.r

Defines functions metric

#@title Function that computes M^(-1/2)
#@export
#@param dt a data.frame containing  covariates
metric <- function(dt){
  dt <- as.data.frame(dt)
  mdt <- stats::model.frame(dt,data=dt)
  nm <- names(mdt)
  out <- lapply(1:ncol(mdt),function(k){
    x <- model.matrix(as.formula(paste0("~",nm[k])),data=mdt)[,-1,drop=FALSE]
    x <- scale(x[,,drop=FALSE],TRUE,FALSE)
    x <- crossprod(x)/nrow(x)
  })
  out <- bdiag(out)
  out <- svd(out)
  if(length(out$d)==1){
    out <- out$u%*%as.matrix(1/sqrt(out$d),1,1)%*%t(out$v)
  }else{
    out <- out$u%*%diag(1/sqrt(out$d))%*%t(out$v)
  }
  return(out)
}

Try the SCGLR package in your browser

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

SCGLR documentation built on May 1, 2019, 8 p.m.