R/bigstatsr_init.R

Defines functions bigstatsr_scores bigstatsr_is_installed

bigstatsr_is_installed <- function() {
  is_installed("bigstatsr")
}

bigstatsr_scores <- function(X,
                             ncol,
                             center = TRUE,
                             ret_extra = FALSE,
                             ncores = 1,
                             verbose = FALSE) {
  res <- bigstatsr::big_randomSVD(
    X = bigstatsr::as_FBM(X),
    fun.scaling = bigstatsr::big_scale(center = center, scale = FALSE),
    k = ncol,
    ncores = ncores
  )
  if (verbose) {
    totalvar <- sum(apply(X, 2, stats::var))
    lambda <- sum((res$d^2) / (nrow(X) - 1))
    varex <- lambda / totalvar
    tsmessage(
      "PCA: ",
      ncol,
      " components explained ",
      formatC(varex * 100),
      "% variance"
    )
  }
  scores <- stats::predict(res)
  if (ret_extra) {
    list(
      scores = scores,
      rotation = res$v,
      center = res$center
    )
  } else {
    scores
  }
}

Try the uwot package in your browser

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

uwot documentation built on June 22, 2024, 6:58 p.m.