Computes the variance of a spectrum in an ir
object in a given region
Source: R/ir_variance_region.R
ir_variance_region.Rd
ir_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 additionrange
is notNULL
, it computes the variance only for the region(s) represented byrange
.- if
subtract_smoothed = TRUE
it smoothes
x
, subtracts the smoothedx
from the unsmoothedx
and computes the variance of the difference intensity values. If in additionrange
is 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
,x
is copied, the copy smoothed usingir_smooth
withmethod = "sg"
and subtracted fromx
before the variance of the intensity values fromx
is computed. This allows e.g. to estimate the noise level in a specific region of spectra. Ifsubtract_smoothed = FALSE
(the default), nothing is subtracted fromx
before computing the variance of the intensity values.- do_normalize
A logical value. If set to
TRUE
, the spectra inx
are normalized after subtraction of a smoothed version, else no normalization is performed.- normalize_method
See
ir_normalize()
.- ...
Arguments passed to
ir_smooth()
(except formethod
which is always set to"sg"
ifsubtract_smoothed
isTRUE
). 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
)