Calculate lack_of_fit (LOF) for spectroscopic data by class

get_LOF_by_class(obj, by, FUN = median)

Arguments

obj

- hyperSpec object.

by

- string with name of factor variable in `obj`.

FUN

- function used to calculate expected values.

Value

Function returns a list of 4 objects:

data - object `obj`;

centers - hyperSpec object in which variable $spc is matrix with expected values (means, medians, etc. indicated by `FUN`) for every row;

difference - hyperSpec object in which variable $spc is matrix with differences between observed and expected values;

LOF - vector of lack-of-fit values for every row.

See also

Examples

library(spHelper)
obj0 <- get_LOF_by_class(obj = Spectra2, by = "gr")
#> Warning: Function 'chk.hy' is deprecated. 
#> Use function 'assert_hyperSpec' instead.
str(obj0, max.level = 1)
#> List of 4
#>  $ data       :Formal class 'hyperSpec' [package "hyperSpec"] with 4 slots
#>  $ centers    :Formal class 'hyperSpec' [package "hyperSpec"] with 4 slots
#>  $ differences:Formal class 'hyperSpec' [package "hyperSpec"] with 4 slots
#>  $ LOF        : num [1:150] 36.41 12.86 11.41 5.51 19.03 ...
#>  - attr(*, "class")= chr [1:2] "LOF_obj" "list"

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#  Unit tests:
#
#  require(testthat)
# test_that("classes and their centers are sorted correctly",{
# require(hyperSpec)
# set.seed(1)
# obj <- hyperSpec::sample(Spectra2, 150)
#
# LOF_obj <- get_LOF_by_class(obj, "gr")
# N_unique <- (!duplicated(
#     LOF_obj$centers[,c("gr","spc")]  %>%
#    as.wide.df)) %>%  sum
# expect_equal(N_unique, nlevels(obj$gr))
#  })
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~