Memory-Based Learning in Spectral Chemometrics

Functions for dissimilarity analysis and memory-based learning (MBL, a.k.a local modeling) in complex spectral data sets. Most of these functions are based the methods presented in Ramirez-Lopez et al. (2013) .

Leo Ramirez-Lopez & Antoine Stevens

Visit the resemble site here

Installing the package is very simple:


If you do not have the following packages installed, in some cases it is better to install them first


Note: Apart from these packages we stronly recommend to download and install Rtools (directly from here or from CRAN This is important for obtaining the proper C++ toolchain that you might need for using resemble.

Then, install resemble

install.packages('C:/MyFolder/', repos = NULL)

The development version can be obtained at the package website

After installing resemble you should be also able to run the following lines:





Xu <- NIRsoil$spc[!as.logical(NIRsoil$train),]
Yu <- NIRsoil$CEC[!as.logical(NIRsoil$train)]
Yr <- NIRsoil$CEC[as.logical(NIRsoil$train)]
Xr <- NIRsoil$spc[as.logical(NIRsoil$train),]

Xu <- Xu[!,]
Xr <- Xr[!,]

Yu <- Yu[!]
Yr <- Yr[!]

# Example of the mbl function
# A mbl approach (the spectrum-based learner) as implemented in Ramirez-Lopez et al. (2013)
# An exmaple where Yu is supposed to be unknown, but the Xu (spectral variables) are known
ctrl <- mblControl(sm = 'pc', pcSelection = list('opc', 40),
                   valMethod = 'NNv', center = TRUE)

sbl.u <- mbl(Yr = Yr, Xr = Xr, Yu = NULL, Xu = Xu,
             mblCtrl = ctrl,
             dissUsage = 'predictors',
             k = seq(40, 150, by = 10),
             method = 'gpr')


resemble implements a function dedicated to non-linear modelling of complex visible and infrared spectral data based on memory-based learning (MBL, a.k.a instance-based learning or local modelling in the chemometrics literature). The package also includes functions for: computing and evaluate spectral similarity/dissimilarity matrices; projecting the spectra onto low dimensional orthogonal variables; removing irrelevant spectra from a reference set; etc.

The functions for computing and evaluate spectral similarity/dissimilarity matrices can be summarized as follows:

fDiss: Euclidean and Mahalanobis distances as well as the cosine dissimilarity (a.k.a spectral angle mapper)
corDiss: correlation and moving window correlation dissimilarity
sid: spectral information divergence between spectra or between the probability distributions of spectra
orthoDiss: principal components and partial least squares dissimilarity (including several options)
simEval: evaluates a given similarity/dissimilarity matrix based on the concept of side information

The functions for projecting the spectra onto low dimensional orthogonal variables are:

pcProjection: projects the spectra onto a principal component space
plsProjection: projects the spectra onto a partial least squares component space (a.k.a projection to latent structures)
orthoProjection: reproduces either the pcProjection or the plsProjection functions

The projection functions also offer different options for optimizing/selecting the number of components involved in the projection.

The functions modelling the spectra using memory-based learning are:

mblControl: controls some modelling aspects of the mbl function
mbl: models the spectra by memory-based learning

Some additional miscellaneous functions are:

print.mbl: prints a summary of the results obtained by the mbl function
plot.mbl: plots a summary of the results obtained by the mbl function
print.localOrthoDiss: prints local distance matrices generated with the orthoDiss function

In order to expand a little bit more the explanation on the mbl function, let's define first the basic input datasets:

  • Reference (training) set: Dataset with n reference samples (e.g. spectral library) to be used in the calibration of spectral models. Xr represents the matrix of samples (containing the spectral predictor variables) and Yr represents a given response variable corresponding to Xr.

  • Prediction set : Data set with m samples where the response variable (Yu) is unknown. However it can be predicted by applying a spectral model (calibrated by using Xr and Yr) on the spectra of these samples (Xu).

In order to predict each value in Yu, the mbl function takes each sample in Xu and searches in Xr for its k-nearest neighbours (most spectrally similar samples). Then a (local) model is calibrated with these (reference) neighbours and it immediately predicts the correspondent value in Yu from Xu. In the function, the k-nearest neighbour search is performed by computing spectral similarity/dissimilarity matrices between samples. The mbl function offers the following regression options for calibrating the (local) models:

'gpr': Gaussian process with linear kernel
'pls': Partial least squares
'wapls1': Weighted average partial least squares 1
'wapls2': Weighted average partial least squares 2 (no longer supported)


  • Infrared spectroscopy
  • Chemometrics
  • Local modelling
  • Spectral library
  • Lazy learning
  • Soil spectroscopy

Other R'elated stuff

Bug report and development version

You can send an e-mail to the package maintainer ([email protected]) or create an issue on github.


News for the resemble package

Versions 1.0 and 1.1.1

  • 19.03.2013 Hello world! Initial release of the package

Version 1.2 (alma de coco)

  • 04.03.2016 A patch was released for and extrange bug that prevented to run mbl in parallel when the gpr method was used.
  • 19.01.2016 Now it is possible to locally optimize the maximum and minimum pls factors in wapls1 local regressions.
  • 09.12.2015 Many thanks to Eva Ampe and Lorenzo Menichetti for their suggestions.
  • 08.12.2015 A method for better estimates of RMSE values computed for the 'wapls1' method has been implemented.
  • 08.12.2015 The 'wapls2' method of the mbl function is no longer supported because of several drawbacks computing reliable uncertainty estimates
  • 18.11.2015 Several functions are now based on C++ for faster computations.
  • 23.04.2014 Added default variable names when they are missing and an error message when the names of Xr do not match the names of Xu.
  • 23.04.2014 plot.mbl draws now the circles around the actual centre function when the spectra is not centred for mbl
  • 20.03.2013 The function movcorDist was removed since. it was included by mistake in the previous version of the package. The corDiss function
    can be used in raplacement of movcorDist.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


2.0.0 by Leonardo Ramirez-Lopez, a year ago

Report a bug at

Browse source code at

Authors: Leonardo Ramirez-Lopez [aut, cre] , Antoine Stevens [aut, ctb] , Raphael Viscarra Rossel [ctb] , Craig Lobsey [ctb] , Alex Wadoux [ctb] , Timo Breure [ctb]

Documentation:   PDF Manual  

Task views:

MIT + file LICENSE license

Imports foreach, iterators, Rcpp, mathjaxr, magrittr, lifecycle, data.table

Suggests prospectr, parallel, doParallel, testthat, formatR, rmarkdown, bookdown, knitr

Linking to Rcpp, RcppArmadillo

See at CRAN