Skip to contents

Calibrate independent models (iteratively) for multiple properties with optimization of both the pre-processing recipe (based on a list of different recipes) and the regression method. This function uses calibrate to construct such list of models.

Usage

calibrate_models(formulas,
                 data, group = NULL,
                 preprocess_recipes,
                 methods,
                 control = calibration_control(seed = 1),
                 metadata_list = NULL,
                 skip_indices_list = NULL,
                 return_inputs = TRUE,
                 ...,
                 na_action = na.pass,
                 verbose = TRUE,
                 save_all = FALSE)

# S3 method for class 'spectral_multimodel'
predict(object, newdata, verbose = TRUE, ...)

Arguments

formulas

a list containing one or more objects of class formula where each of them represents the model to be calibrated.

data

a data.frame containing the data of the variables in the model (as in the calibrate function).

group

an optional factor (or character vector that can be coerced to factor by as.factor) that assigns a group/class label to each observation in X (e.g. groups can be given by spectra collected from the same batch of measurements, from the same observation, from observations with very similar origin, etc). This is taken into account for cross-validation for pls tuning (factor optimization) to avoid pseudo- replication. When one observation is selected for cross-validation, all observations of the same group are removed and assigned to validation. The length of the vector must be equal to the number of observations in X.

preprocess_recipes

a list with one or more objects of class preprocess_recipe that are to be tested for finding the optimal one for each model in the list passed to formulas.

methods

a list containing one or more objects of class fit_constructor as returned by fit_plsr or fit_xlsr, indicating what type of regression method to use along with its parameters.

control

a calibration_control object as returned by the calibration_control function, indicating how some aspects of the calibration process must be conducted (e.g. cross-validation and outlier detection). Default is calibration_control(seed = 1). See details.

metadata_list

a list containing the specifications for the metadata of each model in formulas given in the same order. Each element in the list should be defined as in the metadata argument of calibrate using the add_model_metadata function. Defaults to NULL.

skip_indices_list

a list of vectors of integers for the indices in the input data to be skipped for the computation of each of the models in formulas. The vectors in this list must be provided in the same order as their corresponding counterparts in formulas. Defaults to NULL. In case a list is passed, the list components must be filled with numeric() for those formulas where there is no indices to be skipped.

return_inputs

a logical. For calibrate methods, indicates if the input data should be attached to the returned object. Note that this data is crucial for creating an application file.

...

arguments to be passed to the calibrate method. Not currently used for the predict.spectral_multimodel method.

na_action

a function to specify the action to be taken if NAs are found in the object passed in data. Default is na.pass.

verbose

a logical indicating whether or not to print a progress bar for the iterations of the validation along with messages of the execution of the cross-validation. For the predict method, messages about the progress are printed. Default is TRUE. Note: In case parallel processing is used, these progress bars are not printed.

save_all

a logical indicating if all the models tested (with the different pre-processing recipes) are to be saved. Default is FALSE.

object

an object of class spectral_multimodel.

newdata

a data.frame containing the new spectral data of the variables in the model, of similar form as data. Alternatively, can also be a matrix of spectra.

Value

A list of class "spectral_multimodel" containing the following objects:

  • results_grid: a data.frame with the validation results of the best models found for each pre-processing recipe with the best regression method applied on the spectral data of the model built for each formula.

  • all_models: if save_all, a list with the spectral_model objects corresponding to all the models tested.

  • final_models: a list containing only the spectral_model objects corresponding to the best models found for each formula. This list can be used/passed later to the proximate_write_nax function to produce an application file (in that case it might be convenient to add some metadata to the resulting models in the list using the add_model_metadata function).

For predict(), a list with the following elements:

  • predictions: A matrix with the predictions of the response variable using the new spectral data (newdata), based on the provided models (object). Contains only the predictions of the optimal number of components (ncomp).

  • model_information: A list, containing information on the models inputs in object. Each component in the list contains the following information:

    • target_var: A character, indicating the name of the target variable.

    • preprocess_recipe: A character, indicating the spectral preprocessing recipe and its order.

    • model_grid: A matrix, containing the grid of the model object, such as the coefficient of determination and the RMSE of the validation for the requested number of components.

    • unit: A character, indicating the units of the model.

    • opt_comp: An integer, signifying the optimal number of components as computed by the validation process of the model.

Details

The object passed to the control argument should indicate a seed for the random number generator (RNG). This allows the function to use the same cross-validation validation groups (for leave group-out cross-validation, see calibration_control) across the same formula with different recipes. This enables proper model comparisons.

Parallel cross-validation

The cross-validation loop inside each call to calibrate is implemented with foreach, so it can be parallelised transparently by registering a parallel backend before calling calibrate_models. Set allow_parallel = TRUE in calibration_control (the default) and register a backend, for example:


cl <- parallel::makeCluster(parallel::detectCores() - 1L)
doParallel::registerDoParallel(cl)

result <- calibrate_models(...)

parallel::stopCluster(cl)

When no parallel backend is registered, foreach falls back silently to sequential execution regardless of the allow_parallel setting. Note that progress bars are suppressed during parallel execution.

Author

Leonardo Ramirez-Lopez and Claudio Orellano

Examples

# \donttest{
data("NIRcannabis")
# the list of formulas for the models to be built
app_formulas <- list(THC ~ spc, THCA ~ spc, CBD ~ spc, CBDA ~ spc)

# the list of pre-processing recipes to be tested
precipes <- list(
  recipe_1 = preprocess_recipe(
    prep_resample(grid = c(1001, 1700, 2)),
    prep_snv(),
    prep_derivative(m = 1, w = 9, p = 7, algorithm = "nwp"),
    device = "proximate"
  ),
  recipe_2 = preprocess_recipe(
    prep_resample(grid = c(1001, 1700, 2)),
    prep_snv(),
    prep_derivative(m = 2, w = 11, p = 9, algorithm = "nwp"),
    device = "proximate"
  )
)

optimized_app <- calibrate_models(
  formulas = app_formulas,
  data = NIRcannabis,
  preprocess_recipes = precipes,
  methods = list(fit_plsr(15, type = "nwp")),
  return_inputs = TRUE,
  save_all = FALSE
)
#> --- Finding model for THC ~ spc ----
#>  + testing preprocessing recipe index 1
#>  + testing preprocessing recipe index 2
#> --- Finding model for THCA ~ spc ----
#>  + testing preprocessing recipe index 1
#>  + testing preprocessing recipe index 2
#> --- Finding model for CBD ~ spc ----
#>  + testing preprocessing recipe index 1
#>  + testing preprocessing recipe index 2
#> --- Finding model for CBDA ~ spc ----
#>  + testing preprocessing recipe index 1
#>  + testing preprocessing recipe index 2
#> --- Fitting final model for THC ~ spc ----
#> Fitting model...
#> Cross-validating...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |-                                                                     |   1%
  |                                                                            
  |-                                                                     |   2%
  |                                                                            
  |--                                                                    |   3%
  |                                                                            
  |---                                                                   |   4%
  |                                                                            
  |----                                                                  |   5%
  |                                                                            
  |----                                                                  |   6%
  |                                                                            
  |-----                                                                 |   7%
  |                                                                            
  |------                                                                |   8%
  |                                                                            
  |------                                                                |   9%
  |                                                                            
  |-------                                                               |  10%
  |                                                                            
  |--------                                                              |  11%
  |                                                                            
  |--------                                                              |  12%
  |                                                                            
  |---------                                                             |  13%
  |                                                                            
  |----------                                                            |  14%
  |                                                                            
  |----------                                                            |  15%
  |                                                                            
  |-----------                                                           |  16%
  |                                                                            
  |------------                                                          |  17%
  |                                                                            
  |-------------                                                         |  18%
  |                                                                            
  |-------------                                                         |  19%
  |                                                                            
  |--------------                                                        |  20%
  |                                                                            
  |---------------                                                       |  21%
  |                                                                            
  |---------------                                                       |  22%
  |                                                                            
  |----------------                                                      |  23%
  |                                                                            
  |-----------------                                                     |  24%
  |                                                                            
  |------------------                                                    |  25%
  |                                                                            
  |------------------                                                    |  26%
  |                                                                            
  |-------------------                                                   |  27%
  |                                                                            
  |--------------------                                                  |  28%
  |                                                                            
  |--------------------                                                  |  29%
  |                                                                            
  |---------------------                                                 |  30%
  |                                                                            
  |----------------------                                                |  31%
  |                                                                            
  |----------------------                                                |  32%
  |                                                                            
  |-----------------------                                               |  33%
  |                                                                            
  |------------------------                                              |  34%
  |                                                                            
  |------------------------                                              |  35%
  |                                                                            
  |-------------------------                                             |  36%
  |                                                                            
  |--------------------------                                            |  37%
  |                                                                            
  |---------------------------                                           |  38%
  |                                                                            
  |---------------------------                                           |  39%
  |                                                                            
  |----------------------------                                          |  40%
  |                                                                            
  |-----------------------------                                         |  41%
  |                                                                            
  |-----------------------------                                         |  42%
  |                                                                            
  |------------------------------                                        |  43%
  |                                                                            
  |-------------------------------                                       |  44%
  |                                                                            
  |--------------------------------                                      |  45%
  |                                                                            
  |--------------------------------                                      |  46%
  |                                                                            
  |---------------------------------                                     |  47%
  |                                                                            
  |----------------------------------                                    |  48%
  |                                                                            
  |----------------------------------                                    |  49%
  |                                                                            
  |-----------------------------------                                   |  50%
  |                                                                            
  |------------------------------------                                  |  51%
  |                                                                            
  |------------------------------------                                  |  52%
  |                                                                            
  |-------------------------------------                                 |  53%
  |                                                                            
  |--------------------------------------                                |  54%
  |                                                                            
  |--------------------------------------                                |  55%
  |                                                                            
  |---------------------------------------                               |  56%
  |                                                                            
  |----------------------------------------                              |  57%
  |                                                                            
  |-----------------------------------------                             |  58%
  |                                                                            
  |-----------------------------------------                             |  59%
  |                                                                            
  |------------------------------------------                            |  60%
  |                                                                            
  |-------------------------------------------                           |  61%
  |                                                                            
  |-------------------------------------------                           |  62%
  |                                                                            
  |--------------------------------------------                          |  63%
  |                                                                            
  |---------------------------------------------                         |  64%
  |                                                                            
  |----------------------------------------------                        |  65%
  |                                                                            
  |----------------------------------------------                        |  66%
  |                                                                            
  |-----------------------------------------------                       |  67%
  |                                                                            
  |------------------------------------------------                      |  68%
  |                                                                            
  |------------------------------------------------                      |  69%
  |                                                                            
  |-------------------------------------------------                     |  70%
  |                                                                            
  |--------------------------------------------------                    |  71%
  |                                                                            
  |--------------------------------------------------                    |  72%
  |                                                                            
  |---------------------------------------------------                   |  73%
  |                                                                            
  |----------------------------------------------------                  |  74%
  |                                                                            
  |----------------------------------------------------                  |  75%
  |                                                                            
  |-----------------------------------------------------                 |  76%
  |                                                                            
  |------------------------------------------------------                |  77%
  |                                                                            
  |-------------------------------------------------------               |  78%
  |                                                                            
  |-------------------------------------------------------               |  79%
  |                                                                            
  |--------------------------------------------------------              |  80%
  |                                                                            
  |---------------------------------------------------------             |  81%
  |                                                                            
  |---------------------------------------------------------             |  82%
  |                                                                            
  |----------------------------------------------------------            |  83%
  |                                                                            
  |-----------------------------------------------------------           |  84%
  |                                                                            
  |------------------------------------------------------------          |  85%
  |                                                                            
  |------------------------------------------------------------          |  86%
  |                                                                            
  |-------------------------------------------------------------         |  87%
  |                                                                            
  |--------------------------------------------------------------        |  88%
  |                                                                            
  |--------------------------------------------------------------        |  89%
  |                                                                            
  |---------------------------------------------------------------       |  90%
  |                                                                            
  |----------------------------------------------------------------      |  91%
  |                                                                            
  |----------------------------------------------------------------      |  92%
  |                                                                            
  |-----------------------------------------------------------------     |  93%
  |                                                                            
  |------------------------------------------------------------------    |  94%
  |                                                                            
  |------------------------------------------------------------------    |  95%
  |                                                                            
  |-------------------------------------------------------------------   |  96%
  |                                                                            
  |--------------------------------------------------------------------  |  97%
  |                                                                            
  |--------------------------------------------------------------------- |  98%
  |                                                                            
  |--------------------------------------------------------------------- |  99%
  |                                                                            
  |----------------------------------------------------------------------| 100%
#> --- Fitting final model for THCA ~ spc ----
#> Fitting model...
#> Cross-validating...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |-                                                                     |   1%
  |                                                                            
  |-                                                                     |   2%
  |                                                                            
  |--                                                                    |   3%
  |                                                                            
  |---                                                                   |   4%
  |                                                                            
  |----                                                                  |   5%
  |                                                                            
  |----                                                                  |   6%
  |                                                                            
  |-----                                                                 |   7%
  |                                                                            
  |------                                                                |   8%
  |                                                                            
  |------                                                                |   9%
  |                                                                            
  |-------                                                               |  10%
  |                                                                            
  |--------                                                              |  11%
  |                                                                            
  |--------                                                              |  12%
  |                                                                            
  |---------                                                             |  13%
  |                                                                            
  |----------                                                            |  14%
  |                                                                            
  |----------                                                            |  15%
  |                                                                            
  |-----------                                                           |  16%
  |                                                                            
  |------------                                                          |  17%
  |                                                                            
  |-------------                                                         |  18%
  |                                                                            
  |-------------                                                         |  19%
  |                                                                            
  |--------------                                                        |  20%
  |                                                                            
  |---------------                                                       |  21%
  |                                                                            
  |---------------                                                       |  22%
  |                                                                            
  |----------------                                                      |  23%
  |                                                                            
  |-----------------                                                     |  24%
  |                                                                            
  |------------------                                                    |  25%
  |                                                                            
  |------------------                                                    |  26%
  |                                                                            
  |-------------------                                                   |  27%
  |                                                                            
  |--------------------                                                  |  28%
  |                                                                            
  |--------------------                                                  |  29%
  |                                                                            
  |---------------------                                                 |  30%
  |                                                                            
  |----------------------                                                |  31%
  |                                                                            
  |----------------------                                                |  32%
  |                                                                            
  |-----------------------                                               |  33%
  |                                                                            
  |------------------------                                              |  34%
  |                                                                            
  |------------------------                                              |  35%
  |                                                                            
  |-------------------------                                             |  36%
  |                                                                            
  |--------------------------                                            |  37%
  |                                                                            
  |---------------------------                                           |  38%
  |                                                                            
  |---------------------------                                           |  39%
  |                                                                            
  |----------------------------                                          |  40%
  |                                                                            
  |-----------------------------                                         |  41%
  |                                                                            
  |-----------------------------                                         |  42%
  |                                                                            
  |------------------------------                                        |  43%
  |                                                                            
  |-------------------------------                                       |  44%
  |                                                                            
  |--------------------------------                                      |  45%
  |                                                                            
  |--------------------------------                                      |  46%
  |                                                                            
  |---------------------------------                                     |  47%
  |                                                                            
  |----------------------------------                                    |  48%
  |                                                                            
  |----------------------------------                                    |  49%
  |                                                                            
  |-----------------------------------                                   |  50%
  |                                                                            
  |------------------------------------                                  |  51%
  |                                                                            
  |------------------------------------                                  |  52%
  |                                                                            
  |-------------------------------------                                 |  53%
  |                                                                            
  |--------------------------------------                                |  54%
  |                                                                            
  |--------------------------------------                                |  55%
  |                                                                            
  |---------------------------------------                               |  56%
  |                                                                            
  |----------------------------------------                              |  57%
  |                                                                            
  |-----------------------------------------                             |  58%
  |                                                                            
  |-----------------------------------------                             |  59%
  |                                                                            
  |------------------------------------------                            |  60%
  |                                                                            
  |-------------------------------------------                           |  61%
  |                                                                            
  |-------------------------------------------                           |  62%
  |                                                                            
  |--------------------------------------------                          |  63%
  |                                                                            
  |---------------------------------------------                         |  64%
  |                                                                            
  |----------------------------------------------                        |  65%
  |                                                                            
  |----------------------------------------------                        |  66%
  |                                                                            
  |-----------------------------------------------                       |  67%
  |                                                                            
  |------------------------------------------------                      |  68%
  |                                                                            
  |------------------------------------------------                      |  69%
  |                                                                            
  |-------------------------------------------------                     |  70%
  |                                                                            
  |--------------------------------------------------                    |  71%
  |                                                                            
  |--------------------------------------------------                    |  72%
  |                                                                            
  |---------------------------------------------------                   |  73%
  |                                                                            
  |----------------------------------------------------                  |  74%
  |                                                                            
  |----------------------------------------------------                  |  75%
  |                                                                            
  |-----------------------------------------------------                 |  76%
  |                                                                            
  |------------------------------------------------------                |  77%
  |                                                                            
  |-------------------------------------------------------               |  78%
  |                                                                            
  |-------------------------------------------------------               |  79%
  |                                                                            
  |--------------------------------------------------------              |  80%
  |                                                                            
  |---------------------------------------------------------             |  81%
  |                                                                            
  |---------------------------------------------------------             |  82%
  |                                                                            
  |----------------------------------------------------------            |  83%
  |                                                                            
  |-----------------------------------------------------------           |  84%
  |                                                                            
  |------------------------------------------------------------          |  85%
  |                                                                            
  |------------------------------------------------------------          |  86%
  |                                                                            
  |-------------------------------------------------------------         |  87%
  |                                                                            
  |--------------------------------------------------------------        |  88%
  |                                                                            
  |--------------------------------------------------------------        |  89%
  |                                                                            
  |---------------------------------------------------------------       |  90%
  |                                                                            
  |----------------------------------------------------------------      |  91%
  |                                                                            
  |----------------------------------------------------------------      |  92%
  |                                                                            
  |-----------------------------------------------------------------     |  93%
  |                                                                            
  |------------------------------------------------------------------    |  94%
  |                                                                            
  |------------------------------------------------------------------    |  95%
  |                                                                            
  |-------------------------------------------------------------------   |  96%
  |                                                                            
  |--------------------------------------------------------------------  |  97%
  |                                                                            
  |--------------------------------------------------------------------- |  98%
  |                                                                            
  |--------------------------------------------------------------------- |  99%
  |                                                                            
  |----------------------------------------------------------------------| 100%
#> --- Fitting final model for CBD ~ spc ----
#> Fitting model...
#> Cross-validating...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |-                                                                     |   1%
  |                                                                            
  |-                                                                     |   2%
  |                                                                            
  |--                                                                    |   3%
  |                                                                            
  |---                                                                   |   4%
  |                                                                            
  |----                                                                  |   5%
  |                                                                            
  |----                                                                  |   6%
  |                                                                            
  |-----                                                                 |   7%
  |                                                                            
  |------                                                                |   8%
  |                                                                            
  |------                                                                |   9%
  |                                                                            
  |-------                                                               |  10%
  |                                                                            
  |--------                                                              |  11%
  |                                                                            
  |--------                                                              |  12%
  |                                                                            
  |---------                                                             |  13%
  |                                                                            
  |----------                                                            |  14%
  |                                                                            
  |----------                                                            |  15%
  |                                                                            
  |-----------                                                           |  16%
  |                                                                            
  |------------                                                          |  17%
  |                                                                            
  |-------------                                                         |  18%
  |                                                                            
  |-------------                                                         |  19%
  |                                                                            
  |--------------                                                        |  20%
  |                                                                            
  |---------------                                                       |  21%
  |                                                                            
  |---------------                                                       |  22%
  |                                                                            
  |----------------                                                      |  23%
  |                                                                            
  |-----------------                                                     |  24%
  |                                                                            
  |------------------                                                    |  25%
  |                                                                            
  |------------------                                                    |  26%
  |                                                                            
  |-------------------                                                   |  27%
  |                                                                            
  |--------------------                                                  |  28%
  |                                                                            
  |--------------------                                                  |  29%
  |                                                                            
  |---------------------                                                 |  30%
  |                                                                            
  |----------------------                                                |  31%
  |                                                                            
  |----------------------                                                |  32%
  |                                                                            
  |-----------------------                                               |  33%
  |                                                                            
  |------------------------                                              |  34%
  |                                                                            
  |------------------------                                              |  35%
  |                                                                            
  |-------------------------                                             |  36%
  |                                                                            
  |--------------------------                                            |  37%
  |                                                                            
  |---------------------------                                           |  38%
  |                                                                            
  |---------------------------                                           |  39%
  |                                                                            
  |----------------------------                                          |  40%
  |                                                                            
  |-----------------------------                                         |  41%
  |                                                                            
  |-----------------------------                                         |  42%
  |                                                                            
  |------------------------------                                        |  43%
  |                                                                            
  |-------------------------------                                       |  44%
  |                                                                            
  |--------------------------------                                      |  45%
  |                                                                            
  |--------------------------------                                      |  46%
  |                                                                            
  |---------------------------------                                     |  47%
  |                                                                            
  |----------------------------------                                    |  48%
  |                                                                            
  |----------------------------------                                    |  49%
  |                                                                            
  |-----------------------------------                                   |  50%
  |                                                                            
  |------------------------------------                                  |  51%
  |                                                                            
  |------------------------------------                                  |  52%
  |                                                                            
  |-------------------------------------                                 |  53%
  |                                                                            
  |--------------------------------------                                |  54%
  |                                                                            
  |--------------------------------------                                |  55%
  |                                                                            
  |---------------------------------------                               |  56%
  |                                                                            
  |----------------------------------------                              |  57%
  |                                                                            
  |-----------------------------------------                             |  58%
  |                                                                            
  |-----------------------------------------                             |  59%
  |                                                                            
  |------------------------------------------                            |  60%
  |                                                                            
  |-------------------------------------------                           |  61%
  |                                                                            
  |-------------------------------------------                           |  62%
  |                                                                            
  |--------------------------------------------                          |  63%
  |                                                                            
  |---------------------------------------------                         |  64%
  |                                                                            
  |----------------------------------------------                        |  65%
  |                                                                            
  |----------------------------------------------                        |  66%
  |                                                                            
  |-----------------------------------------------                       |  67%
  |                                                                            
  |------------------------------------------------                      |  68%
  |                                                                            
  |------------------------------------------------                      |  69%
  |                                                                            
  |-------------------------------------------------                     |  70%
  |                                                                            
  |--------------------------------------------------                    |  71%
  |                                                                            
  |--------------------------------------------------                    |  72%
  |                                                                            
  |---------------------------------------------------                   |  73%
  |                                                                            
  |----------------------------------------------------                  |  74%
  |                                                                            
  |----------------------------------------------------                  |  75%
  |                                                                            
  |-----------------------------------------------------                 |  76%
  |                                                                            
  |------------------------------------------------------                |  77%
  |                                                                            
  |-------------------------------------------------------               |  78%
  |                                                                            
  |-------------------------------------------------------               |  79%
  |                                                                            
  |--------------------------------------------------------              |  80%
  |                                                                            
  |---------------------------------------------------------             |  81%
  |                                                                            
  |---------------------------------------------------------             |  82%
  |                                                                            
  |----------------------------------------------------------            |  83%
  |                                                                            
  |-----------------------------------------------------------           |  84%
  |                                                                            
  |------------------------------------------------------------          |  85%
  |                                                                            
  |------------------------------------------------------------          |  86%
  |                                                                            
  |-------------------------------------------------------------         |  87%
  |                                                                            
  |--------------------------------------------------------------        |  88%
  |                                                                            
  |--------------------------------------------------------------        |  89%
  |                                                                            
  |---------------------------------------------------------------       |  90%
  |                                                                            
  |----------------------------------------------------------------      |  91%
  |                                                                            
  |----------------------------------------------------------------      |  92%
  |                                                                            
  |-----------------------------------------------------------------     |  93%
  |                                                                            
  |------------------------------------------------------------------    |  94%
  |                                                                            
  |------------------------------------------------------------------    |  95%
  |                                                                            
  |-------------------------------------------------------------------   |  96%
  |                                                                            
  |--------------------------------------------------------------------  |  97%
  |                                                                            
  |--------------------------------------------------------------------- |  98%
  |                                                                            
  |--------------------------------------------------------------------- |  99%
  |                                                                            
  |----------------------------------------------------------------------| 100%
#> --- Fitting final model for CBDA ~ spc ----
#> Fitting model...
#> Cross-validating...
#> 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |-                                                                     |   1%
  |                                                                            
  |-                                                                     |   2%
  |                                                                            
  |--                                                                    |   3%
  |                                                                            
  |---                                                                   |   4%
  |                                                                            
  |----                                                                  |   5%
  |                                                                            
  |----                                                                  |   6%
  |                                                                            
  |-----                                                                 |   7%
  |                                                                            
  |------                                                                |   8%
  |                                                                            
  |------                                                                |   9%
  |                                                                            
  |-------                                                               |  10%
  |                                                                            
  |--------                                                              |  11%
  |                                                                            
  |--------                                                              |  12%
  |                                                                            
  |---------                                                             |  13%
  |                                                                            
  |----------                                                            |  14%
  |                                                                            
  |----------                                                            |  15%
  |                                                                            
  |-----------                                                           |  16%
  |                                                                            
  |------------                                                          |  17%
  |                                                                            
  |-------------                                                         |  18%
  |                                                                            
  |-------------                                                         |  19%
  |                                                                            
  |--------------                                                        |  20%
  |                                                                            
  |---------------                                                       |  21%
  |                                                                            
  |---------------                                                       |  22%
  |                                                                            
  |----------------                                                      |  23%
  |                                                                            
  |-----------------                                                     |  24%
  |                                                                            
  |------------------                                                    |  25%
  |                                                                            
  |------------------                                                    |  26%
  |                                                                            
  |-------------------                                                   |  27%
  |                                                                            
  |--------------------                                                  |  28%
  |                                                                            
  |--------------------                                                  |  29%
  |                                                                            
  |---------------------                                                 |  30%
  |                                                                            
  |----------------------                                                |  31%
  |                                                                            
  |----------------------                                                |  32%
  |                                                                            
  |-----------------------                                               |  33%
  |                                                                            
  |------------------------                                              |  34%
  |                                                                            
  |------------------------                                              |  35%
  |                                                                            
  |-------------------------                                             |  36%
  |                                                                            
  |--------------------------                                            |  37%
  |                                                                            
  |---------------------------                                           |  38%
  |                                                                            
  |---------------------------                                           |  39%
  |                                                                            
  |----------------------------                                          |  40%
  |                                                                            
  |-----------------------------                                         |  41%
  |                                                                            
  |-----------------------------                                         |  42%
  |                                                                            
  |------------------------------                                        |  43%
  |                                                                            
  |-------------------------------                                       |  44%
  |                                                                            
  |--------------------------------                                      |  45%
  |                                                                            
  |--------------------------------                                      |  46%
  |                                                                            
  |---------------------------------                                     |  47%
  |                                                                            
  |----------------------------------                                    |  48%
  |                                                                            
  |----------------------------------                                    |  49%
  |                                                                            
  |-----------------------------------                                   |  50%
  |                                                                            
  |------------------------------------                                  |  51%
  |                                                                            
  |------------------------------------                                  |  52%
  |                                                                            
  |-------------------------------------                                 |  53%
  |                                                                            
  |--------------------------------------                                |  54%
  |                                                                            
  |--------------------------------------                                |  55%
  |                                                                            
  |---------------------------------------                               |  56%
  |                                                                            
  |----------------------------------------                              |  57%
  |                                                                            
  |-----------------------------------------                             |  58%
  |                                                                            
  |-----------------------------------------                             |  59%
  |                                                                            
  |------------------------------------------                            |  60%
  |                                                                            
  |-------------------------------------------                           |  61%
  |                                                                            
  |-------------------------------------------                           |  62%
  |                                                                            
  |--------------------------------------------                          |  63%
  |                                                                            
  |---------------------------------------------                         |  64%
  |                                                                            
  |----------------------------------------------                        |  65%
  |                                                                            
  |----------------------------------------------                        |  66%
  |                                                                            
  |-----------------------------------------------                       |  67%
  |                                                                            
  |------------------------------------------------                      |  68%
  |                                                                            
  |------------------------------------------------                      |  69%
  |                                                                            
  |-------------------------------------------------                     |  70%
  |                                                                            
  |--------------------------------------------------                    |  71%
  |                                                                            
  |--------------------------------------------------                    |  72%
  |                                                                            
  |---------------------------------------------------                   |  73%
  |                                                                            
  |----------------------------------------------------                  |  74%
  |                                                                            
  |----------------------------------------------------                  |  75%
  |                                                                            
  |-----------------------------------------------------                 |  76%
  |                                                                            
  |------------------------------------------------------                |  77%
  |                                                                            
  |-------------------------------------------------------               |  78%
  |                                                                            
  |-------------------------------------------------------               |  79%
  |                                                                            
  |--------------------------------------------------------              |  80%
  |                                                                            
  |---------------------------------------------------------             |  81%
  |                                                                            
  |---------------------------------------------------------             |  82%
  |                                                                            
  |----------------------------------------------------------            |  83%
  |                                                                            
  |-----------------------------------------------------------           |  84%
  |                                                                            
  |------------------------------------------------------------          |  85%
  |                                                                            
  |------------------------------------------------------------          |  86%
  |                                                                            
  |-------------------------------------------------------------         |  87%
  |                                                                            
  |--------------------------------------------------------------        |  88%
  |                                                                            
  |--------------------------------------------------------------        |  89%
  |                                                                            
  |---------------------------------------------------------------       |  90%
  |                                                                            
  |----------------------------------------------------------------      |  91%
  |                                                                            
  |----------------------------------------------------------------      |  92%
  |                                                                            
  |-----------------------------------------------------------------     |  93%
  |                                                                            
  |------------------------------------------------------------------    |  94%
  |                                                                            
  |------------------------------------------------------------------    |  95%
  |                                                                            
  |-------------------------------------------------------------------   |  96%
  |                                                                            
  |--------------------------------------------------------------------  |  97%
  |                                                                            
  |--------------------------------------------------------------------- |  98%
  |                                                                            
  |--------------------------------------------------------------------- |  99%
  |                                                                            
  |----------------------------------------------------------------------| 100%

optimized_app
#> Grid search results: 
#>        formula recipe min property max property ncomp   rsq  rmse
#> 1   THC ~ spc       1       0.0136         5.44     2 0.532 0.679
#> 2 * THC ~ spc       2       0.0136         5.44     3 0.690 0.578
#> 3   THCA ~ spc      1       0.0200        12.13     7 0.780 1.539
#> 4 * THCA ~ spc      2       0.0200        12.13     6 0.820 1.407
#> 5   CBD ~ spc       1       0.0262         5.89     1 0.148 0.675
#> 6 * CBD ~ spc       2       0.0262         5.89     1 0.175 0.668
#> 7   CBDA ~ spc      1       0.0100        24.64     4 0.684 3.454
#> 8 * CBDA ~ spc      2       0.0100        24.64     2 0.682 3.431
#>   largest_residual rsq_sd rmse_sd largest_residual_sd outliers    method
#> 1             1.87 0.1112   0.171               0.978        0 PLS (nwp)
#> 2             1.67 0.1025   0.191               1.047        0 PLS (nwp)
#> 3             4.27 0.1033   0.366               1.323        0 PLS (nwp)
#> 4             3.63 0.0726   0.265               0.944        0 PLS (nwp)
#> 5             2.17 0.0849   0.301               1.599        0 PLS (nwp)
#> 6             2.18 0.0734   0.300               1.573        0 PLS (nwp)
#> 7            11.37 0.2049   1.312               6.147        0 PLS (nwp)
#> 8            11.66 0.2310   1.477               6.749        0 PLS (nwp)
#>  
#> *best model 
#> ---
#> Suggested models: 
#> Model:  THC ~ spc 
#> Spectral preprocessing recipe (device: "proximate"): 
#>  - Step 1: prep_resample
#>     min_wav: 1001; max_wav: 1700; resolution: 2
#>  - Step 2: prep_snv
#>  - Step 3: prep_derivative
#>     m: 2; w: 11; p: 9; algorithm: 'nwp'
#> Method:  PLS (nwp) 
#> 
#> Model:  THCA ~ spc 
#> Spectral preprocessing recipe (device: "proximate"): 
#>  - Step 1: prep_resample
#>     min_wav: 1001; max_wav: 1700; resolution: 2
#>  - Step 2: prep_snv
#>  - Step 3: prep_derivative
#>     m: 2; w: 11; p: 9; algorithm: 'nwp'
#> Method:  PLS (nwp) 
#> 
#> Model:  CBD ~ spc 
#> Spectral preprocessing recipe (device: "proximate"): 
#>  - Step 1: prep_resample
#>     min_wav: 1001; max_wav: 1700; resolution: 2
#>  - Step 2: prep_snv
#>  - Step 3: prep_derivative
#>     m: 2; w: 11; p: 9; algorithm: 'nwp'
#> Method:  PLS (nwp) 
#> 
#> Model:  CBDA ~ spc 
#> Spectral preprocessing recipe (device: "proximate"): 
#>  - Step 1: prep_resample
#>     min_wav: 1001; max_wav: 1700; resolution: 2
#>  - Step 2: prep_snv
#>  - Step 3: prep_derivative
#>     m: 2; w: 11; p: 9; algorithm: 'nwp'
#> Method:  PLS (nwp) 
#> 
# }