R/snv.R

Defines functions snv

Documented in snv

#' @title Standard Normal Variate
#'
#' @author Christian L. Goueguel
#'
#' @description
#' This function performs Standard Normal Variate (SNV) scaling on the input
#' spectral data. SNV scaling scales each row of the input data to have zero
#' mean and unit standard deviation. This is equivalent to autoscaling the
#' transpose of the input data.
#'
#' @param x A numeric matrix or data frame.
#' @param drop_na A logical value indicating whether to remove spectra (rows)
#' containing missing values. If `TRUE` (the default), such rows are removed.
#' @param drop.na `r lifecycle::badge("deprecated")` Use `drop_na`.
#'
#' @return A list with the following components:
#'   \item{`correction`}{The SNV-scaled data.}
#'   \item{`means`}{A vector of row means.}
#'   \item{`stds`}{A vector of row standard deviations.}
#'
#' @export snv
#'
#' @examples
#' data(forageLIBS)
#' spectra <- forageLIBS[-(1:14)]  # the spectral channels
#' corrected <- snv(spectra)$correction
#' corrected[1:3, 1:4]
snv <- function(x, drop_na = TRUE,
                drop.na = deprecated()) {
  drop_na <- renamed_arg(drop.na, drop_na, "snv", "drop.na", "drop_na")

  if (missing(x)) {
    stop("Missing 'x' argument.")
  }
  if (!is.logical(drop_na)) {
    stop("'drop_na' must be a logical value (TRUE or FALSE).")
  }
  x <- tryCatch(as_numeric_matrix(x, "x"), error = function(e) {
    stop("Input 'x' must be a numeric matrix or data frame.", call. = FALSE)
  })
  if (drop_na) {
    x <- x[stats::complete.cases(x), , drop = FALSE]
  }

  mns <- rowMeans(x, na.rm = drop_na)
  sds <- apply(x, 1, stats::sd, na.rm = drop_na)
  if (any(sds == 0, na.rm = TRUE)) {
    warning("Some spectra are constant (zero standard deviation); they are only centered.")
    sds[sds == 0] <- 1
  }
  x_snv <- sweep(x, 1, mns, "-") / sds

  res <- list(
    "correction" = as_tbl(x_snv, colnames(x)),
    "means" = mns,
    "stds" = sds
    )

  return(res)
}

Try the specProc package in your browser

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

specProc documentation built on Nov. 20, 2026, 5:11 p.m.