R/standardize.R

Defines functions sd_ std

#' @importFrom stats model.matrix
std <- function(X) {
  if (typeof(X) == 'integer') storage.mode(X) <- 'double'
  if (!inherits(X, "matrix")) {
    if (is.numeric(X)) {
      X <- matrix(as.double(X), ncol=1)
    } else {
      tmp <- try(X <- model.matrix(~0+., data=X), silent=TRUE)
      if (inherits(tmp, "try-error")) stop("X must be a matrix or able to be coerced to a matrix", call.=FALSE)
    }
  }
  
  scale <- apply(X, 2, sd_)

  sX <- scale(X, center = TRUE, scale = scale)
  dimnames(sX) <- dimnames(X)

  sX
}

sd_ <- function(y) sqrt(sum((y-mean(y))^2)/length(y)) 

Try the glmtlp package in your browser

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

glmtlp documentation built on March 18, 2022, 7:59 p.m.