Function finds spectra that represent mean +/- n MADs and adds column .name.

median_Nmad(
  sp,
  n = 2,
  plus.minus = TRUE,
  var_fun = mad,
  center_fun = median,
  center_name = as.character(match.call()$center_fun),
  var_name = as.character(match.call()$var_fun)
)

Arguments

sp

hyperSpec object.

n

Number of MADs.

plus.minus

Logical. If TRUE, two spectra representing 1 n are calculated. Otherwise only one spectrum representing n is calculated.

var_fun

Function that calculates variability measure. Default is var_fun = mad.

center_fun

Function that calculates center tendency. Default is center_fun = median.

center_name

A string with the name for measure of center tendency (will be used to create a row name in .name).

var_name

A string with the name for measure of variability (will be used to create a row name in .name).

Value

A hyperSpec object with spectra at median(sp) 1 n*MAD(sp).

Author

Vilmantas Gegzna

Examples


data(Spectra2)
Margins  <- median_Nmad(Spectra2)
#> NULL
#> Error in .expand(e1, dim(e2)[c(1, 3)]): Dimension mismatch.
Margins3 <- median_Nmad(Spectra2, n = 3)
#> NULL
#> Error in .expand(e1, dim(e2)[c(1, 3)]): Dimension mismatch.

plotspc(Margins)
#> Warning: Function 'plotspc' is deprecated. 
#> Use function 'plot_spc' instead.
#> Error in is_hyperSpec(object): object 'Margins' not found


# Plot data and margins =======================================

Marg2 <- as.long.df(Margins,  rownames = TRUE, na.rm = FALSE)
#> Error in is_hyperSpec(object): object 'Margins' not found
Marg3 <- as.long.df(Margins3, rownames = TRUE, na.rm = FALSE)
#> Error in is_hyperSpec(object): object 'Margins3' not found

Spectra2$.name <- factor("Spectra")

qplotspc(Spectra2, spc.nmax = nrow(Spectra2),
         mapping = aes(x = .wavelength, y = spc, group = .rownames, color = .name),
         alpha = .25) + theme_light() +
    geom_line(data = Marg2, aes(color = .name), lwd = 1) +
    geom_line(data = Marg3, aes(color = .name), lwd = 1) +
    scale_color_manual(" ", values = c("darkgreen","blue", "red")) +
    scale_alpha_manual(" ", values = ".2", guide = FALSE)
#> Warning: Function 'qplotspc' is deprecated. 
#> Use function 'qplotspc' from package 'hySpc.ggplot2' instead.
#> https://r-hyperspec.github.io/hySpc.ggplot2
#> Error in fortify(data): object 'Marg2' not found