R/calc_pcareduceres.R

Defines functions calc_pcareduceres

calc_pcareduceres <- function(obj, reduce) {
  if (!reduce) {
    return(t(obj))
  }
  df <- data.frame(x = 1:20)
  df$sdev <- prcomp(t(obj), scale = TRUE)$sdev[1:20]
  optpoint <- which.min(
    vapply(
      2:10,
      function(i) {
        data$x2 <- pmax(0, data$x - i)
        sum(lm(sdev ~ x + x2, df)$residuals^2)
      },
      0
    )
  )
  pcadim <- optpoint + 1
  tmpdata <- t(apply(obj, 1, scale))
  colnames(tmpdata) <- colnames(obj)
  tmppc <- prcomp(t(tmpdata), scale = TRUE)
  return(t(tmpdata) %*% tmppc$rotation[, 1:pcadim])
}

Try the DIscBIO package in your browser

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

DIscBIO documentation built on Nov. 6, 2023, 5:08 p.m.