Function calculates common center subtracted group centers of spectroscopic data. In other words, function calculates centers (i.e., means, medians) for each indicated group. Then common center (e.g., a center of all data, a balanced center of all data, a center of certain group or a known spectrum) is subtracted from group centers.

center_subtracted_centers(
  sp,
  by = stop("Argument 'by' is missing."),
  FUN = median,
  Center = NULL,
  balanced = FALSE,
  balance.FUN = mean,
  show.all = FALSE,
  show.balanced = FALSE
)

Arguments

sp

hyperSpec object.

by

Name of grouping variable.

FUN

Function (default is median), that calculates center tendencies (CT, e.g., means, medians or similar) for each group.
This function calculates CT for each group and CT that is common for whole dataset.

Center

One of the following options to select the common center tendency (CCT) that will be subtracted from other CTs:
- NULL (default) or '.All' to use CT that is common for whole dataset (e.g. a median of whole dataset);
- numerical vector of length nwl(sp) to be uses as known/calculated spectrum;
- string with one of the levels of grouping variable (available levels: levels(sp$..[,by])) to use CT of that level.

If balanced == TRUE, values of Center are ignored and bananced CT is used.

balanced

Logical. If TRUE balanced center tendency will be used as a common CT. "Balanced" means that CCT is a CT (e.g. mean) of group CTs (e.g. "balanced" may be a mean of group means, a mean of group medians, a median of group medians, etc.)

show.all

logical. If TRUE, row ".All" with CT common for all data will be added. Default is FALSE.

show.balanced

logical. If TRUE, row ".All (balanced)" with balanced central tendency will be added. Default is FALSE.

Value

hyperSpec object with common center tendency subtracted center tendencies (i.e. spactra) of each group (e.g. mean-subtracted group means, median-subtracted medians etc.).

Details

In context of this function, a center is a mean, a median or similar statistic calculated at each wavelangth.

Author

Vilmantas Gėgžna

Examples

library(spHelper)


# === Common center of all spectra as the subtracted center ================

CSCs <- center_subtracted_centers(sp = Spectra2, by = "gr")

# ggplot2 type plot --------------------------------------------------------
qplot_sp(CSCs, names.in = "gr") + ggtitle("CSCs - center subtracted centers")
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.


# R base type plot ---------------------------------------------------------
names <- CSCs$gr
plot(CSCs, col = names)
legend("topright", lty = 1, col = names, legend = names, bty = "n")
title("CSCs - center subtracted centers")



# === Center of a certain group as the subtracted center ===================

center_subtracted_centers(Spectra2, "gr", Center = "A")  %>%
    qplot_sp(names.in = "gr") +
    ggtitle("Subtraced center is center of group 'A'")
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.


center_subtracted_centers(Spectra2, "gr", Center = "C")  %>%
    qplot_sp(names.in = "gr")+
    ggtitle("Subtraced center is center of group 'C'")
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.




# === Balanced center as the subtracted center =============================

center_subtracted_centers(Spectra2, "gr", balanced = TRUE)  %>%
    qplot_sp(names.in = "gr")+
    ggtitle(subt("Balanced center subtraced centers (BCSCs)",
                 "Balanced = a mean of all group centers\n" %++%
                     "Balanced center is mean"))
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.



center_subtracted_centers(Spectra2, "gr",
                          balanced = TRUE,
                          balance.FUN = median)  %>%
    qplot_sp(names.in = "gr") +
    ggtitle(subt("Balanced center subtraced centers (BCSCs)",
                 "Balanced center is median"))
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.



# === Scaled data ==========================================================


MED <- apply(Spectra2,2,median)
MAD <- apply(Spectra2,2,mad)   # median absolute deviation
scale(Spectra2,center = MED, scale = MAD)  %>%
    center_subtracted_centers(by = "gr")  %>%
    qplot_sp(names.in = "gr") +
    ggtitle(subt("CSCs of scaled data","Scaling: x = (x-median)/MAD"))
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.



scale(Spectra2,center = MED, scale = MAD)  %>%
    center_subtracted_centers(by = "gr",
                              balanced = TRUE,
                              balance.FUN = median)  %>%
    qplot_sp(names.in = "gr") +
    ggtitle(subt("Balanced median SCs of scaled data",
                 "Scaling: x = (x-median)/MAD"))
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.


# === Add curves of common & balanced central tendencies =================

center_subtracted_centers(Spectra2, "gr",
                          show.balanced = TRUE,
                          show.all = TRUE)  %>%
    qplot_sp(names.in = "gr") +
    ggtitle(subt("Curves of common & balanced centers added",
                 "Imbalanced center subtracted centers"))
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.



center_subtracted_centers(Spectra2, "gr",
                          balanced = TRUE,
                          show.balanced = TRUE,
                          show.all = TRUE)  %>%
    qplot_sp(names.in = "gr")+
    ggtitle(subt("Curves of common & balanced centers added",
                 "Balanced center subtracted centers"))
#> Error in rbind(deparse.level, ...): numbers of columns of arguments do not match


center_subtracted_centers(Spectra2, "gr", Center = "C",
                          show.balanced = TRUE,
                          show.all = TRUE)  %>%
    qplot_sp(names.in = "gr")+
    ggtitle(subt("Curves of common & balanced centers added",
                 "Group 'C' center subtracted centers"))
#> Warning: Function 'hyperSpec::chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.