Computes the variance of a spectrum in an ir object in a given region
Source: R/ir_variance_region.R
ir_variance_region.Rdir_variance_region takes a spectrum x and, depending on the
arguments computes the following summary:
- if
subtract_smoothed = FALSE it computes the variance of the intensity values for each spectrum in
x. If in additionrangeis notNULL, it computes the variance only for the region(s) represented byrange.- if
subtract_smoothed = TRUE it smoothes
x, subtracts the smoothedxfrom the unsmoothedxand computes the variance of the difference intensity values. If in additionrangeis notNULL, it computes the variance only for the region(s) represented byrange.
Usage
ir_variance_region(
x,
subtract_smoothed = FALSE,
do_normalize = FALSE,
normalize_method,
...,
range = NULL
)Arguments
- x
An object of class
ir. These are the spectra for which to compute the variance.- subtract_smoothed
A logical value. If
subtract_smoothed = TRUE,xis copied, the copy smoothed usingir_smoothwithmethod = "sg"and subtracted fromxbefore the variance of the intensity values fromxis computed. This allows e.g. to estimate the noise level in a specific region of spectra. Ifsubtract_smoothed = FALSE(the default), nothing is subtracted fromxbefore computing the variance of the intensity values.- do_normalize
A logical value. If set to
TRUE, the spectra inxare normalized after subtraction of a smoothed version, else no normalization is performed.- normalize_method
See
ir_normalize().- ...
Arguments passed to
ir_smooth()(except formethodwhich is always set to"sg"ifsubtract_smoothedisTRUE). Ifsubtract_smoothed = FALSE, these arguments will be ignored.- range
See
ir_clip(). This is the range for which the variance of the intensity values will be computed.
Value
x with two additional columns:
- variance
A numeric vector with the computed variances of the intensity values for the respective spectra.
- n_variance
An integer vector with the number of intensity values used during computing the variance.
Examples
# Whole spectra variance
x1 <-
ir::ir_sample_data |>
ir::ir_variance_region(
subtract_smoothed = FALSE,
do_normalize = TRUE,
normalize_method = "area",
range = NULL
)
# Spectra variance, but only from a specific region
range <- data.frame(start = 2700, end = 2800)
x2 <-
ir::ir_sample_data |>
ir::ir_normalize(method = "area") |>
ir::ir_variance_region(
subtract_smoothed = FALSE,
do_normalize = TRUE,
normalize_method = "area",
range = range
)
# Spectra variance after subtracting a smoothed version of the spectra and
# only from a specific region
x3 <-
ir::ir_sample_data %>%
ir::ir_variance_region(
subtract_smoothed = TRUE,
do_normalize = FALSE,
range = range,
p = 3, n = 31, ts = 1, m = 0
)