Robust MSC

The post on Robust PCA sparked a few conversations with readers of this blog on ‘robust’ approaches to data preprocessing. If you worked with real datasets you probably know that a few outliers can really derail your preprocessing pipeline and therefore need to be dealt with appropriately. Robust PCA was an example. A robust version of the Multiplicative Scatter Correction algorithm (Robust MSC), discussed here, is another.

Before working through the code, here’s two references you might want to look at, if you need a refresher on MSC.

The wonders of the median

MSC estimates and correct for (undesirable) scattering effects by comparing a spectrum to a reference spectrum. The reference spectrum is assumed to be free of scattering effects.

In practice, a reference spectrum is rarely available. What is more easily available is the average spectrum of a multiple-sample dataset. Assuming that scattering is the results of sample surface effects, and assuming also that these effects vary randomly from sample to sample, the average spectrum should produce a close approximation to a scatter-free spectrum.

However, the average spectrum could be easily affected by the presence of one or more outliers, which then could have a deleterious effect on the rest of the dataset through the MSC correction.

The secret to make a robust version of MSC is surprisingly simple: all we need is to replace the mean with the median. The (arithmetic) mean in a dataset is given by sum of the values divided by their number. The median, instead, is the value that sits exactly in the middle of the distribution. Half of the data points are above the median, the other half sit below.

 The median is therefore way more robust against outliers: it makes little difference if a value lies 100 times or 1000 times above the mean. If it falls on the 50% of points on the right-hand side of the distribution, the median won’t change.

This brings us to the promised implementation of a robust version of MSC (RMSC).

Robust MSC correction class

Our implementation of the RMSC follows closely what we’ve done with the custom MSC class developed in a previous post.

RMSC is implemented as a transformer, i.e. a function that modifies the data ‘in a supervised or unsupervised way’ before an estimator (for instance a PLS regressor) can be fitted.

For all the details, refer to the post linked above and to the examples provided in the scikit-learn Github page.

Here’s the class.

You’ll notice that the above class above is nearly identical to the MSC class in the previous post. The difference is the ‘robust’ option that will replace the mean centre correction with the median centre correction.

The example usage is also nearly identical, but let’s repeat it for completeness. 1) Import the relevant libraries, 2) import some data, 3) define a pipeline containing the RMSC correction class and PLS regression, and 4) estimate the optimal number of latent variables with a GridSearchCV.

1) Imports

2) Load some data from our GitHub repo and define the arrays

3) and 4) Define and fit a pipeline. Note the option robust=True in the call to the msc  function

One thing to note in closing: if the data has no outliers, the median is extremely similar to the mean. Therefore, there is no real harm in using the robust=True option as a default.

As always, thanks for reading and until next time.

Daniel

**

Feature image by Jarod Lovekamp on Pexels

Work With Me

Need help with chemometrics, spectroscopy, or statistical modeling? I offer consulting, research support, and personalised training.

Get in touch →