Moving window PLS regression

Partial Least Square (PLS) regression is one of the staples of chemometrics. It solves the multicollinearity problem present in most spectroscopy data, while at the same time projecting the data into a conveniently small set of components useful for regression. This is very good, but sometimes some of the measured spectral bands are uncorrelated with the parameter we are trying to predict. Moving window PLS regression is a useful technique to identify and select useful bands and improve the quality of our regression model.

In this post we will discuss a Python implementation of moving window PLS regression and some recommendations to make the most of it with real world data. For related posts on PLS regression feel free to check out:

The data used in this post are taken from the paper In Situ Measurement of Some Soil Properties in Paddy Soil Using Visible and Near Infrared Spectroscopy by Ji Wenjun et al. The associated data is available here.

Implementing the moving window: the basics

First of all: what is a window and why do we want to move it? A window is simply a defined wavelength (or wave number) range which is usually narrower than the full measurement range. For instance we may have measured a NIR spectrum from 900 nm to 1400 nm, and a window is a limited range – say for instance 1000-1250 nm – within.

Selecting a narrower range before running PLS regression enables one to test whether there is a spectral range producing a tighter prediction. In other words some spectral components may be more representative than others for predicting an unknown sample parameter. A window will enable to home on only those components.

For this reason, after having defined a window, we actually want to move it. In general we don’t know where the representative components are located. To find out, we can slide the window through the spectral range and optimise a PLS model at each position. In this way we can quantitatively compare models obtained at different locations and identify whether an optimal location exists.

In the quest for the ultimate prediction model, a judicious application of the moving window must be definitely part of your bag of tricks!

Standard PLS: Python code

Before looking at the moving window PLS, let’s quickly write some code for conventional PLS. This bit has been extensively discussed in our PLS post, but in the spirit of having a self-contained post, let’s quickly cover it here as well, albeit in less detail.

First thing, as always, the imports

The dataset contains several labels corresponding to the same spectra. We are going to use the “TOC” label (total organic carbon, for those of you who are interested).

And now here’s the code snippet to run a simple PLS optimiser. We fit a PLS regressor to the data and locate the minimum root mean square error (RMSE) in cross-validation as a function of the number of PLS components.

The minimum RMSE = 5.14 is attained with 13 components.

We can now run a fit with 13 components and check the cross-validation results with a scatter plot.

Well, it’s clear that we still have some work to do. It’s time to put our moving window to work.

Moving window PLS: Python code

Firstly, let’s write down the function we’ll be using. We’ll comment the relevant lines below.

In order to define a window we need two parameters: its size and its position. Both parameters will have to be adjusted to find the best result. The function above varies only the position parameter, while the size parameter is tuned in a separate loop. You can modify the function to loop over the window size as well, if you wish.

I haven’t done that for a specific reason: the function contains already a nested loop – over the window position and over the number of PLS components. Adding a third loop would have made the code a bit too convoluted, and harder to explain.

Anyway, the spirit of the mwpls function is simple: we define a window and we slide it over the allowed range. At each position we optimise a PLS regression using cross-validation. More specifically we define the RMSE metric and we seek the two parameters (window position and number of components) that yield the minimum RMSE.

The first thing to note is the definition of the rmse array, which is directly affected by the choice of the range in the number of components we decide to loop over. The reason is straightforward: we can’t choose a number of component larger than the window size. If the window is 20 pixels wide, we can’t use more than 20 components.

Remember, PLS is a linear transformation of coordinates. If we start from 20 coordinates (the wavelengths contained in the window) we can’t use more components than that to transform our data. That is the reason we call the function min(20,win_size). We want to loop over 20 PLS components unless the window is narrower than that.

The other thing to note is the position of the window, which is defined as the position of its first point. The window goes from the pixel i to the pixel i+win_size.

The rest is standard Python. We populate the 2D rmse array in the nested loop, and we search for the minimum when the loop is over. You can optionally display the RMSE map in this parameter space, which might give further insights on the optimisation process.

Optimising the window size

The function mwpls requires the window size to be specified beforehand. As discussed, we chose to do it this way to avoid another level in the nested for loop which would make the function unnecessary convoluted. The other reason is that we usually don’t need a fine grid in the window size array. A few points will generally be enough.

For instance, this is the typical code you can run

We just specified 5 values for the window size, in step of 20 pixels. Looking at the code output you can get something like this (the output might be slightly different depending on the initialisation of the random seed for cross-validation)

This immediately gives you insights as to what is going on. A window size of 80 pixels starting at the position 104 produces a minimum RMSE of 3.96 for 16 PLS components. This is a much better result when compared to the full-spectrum PLS. In that case we had RMSE above 5, more than 20% improvement just by selecting the right portion of the spectrum.

Now, let’s take the optimised values, run the PLS and visualise the results.

While there is still some work to do, the result is undoubtedly much better that the previous one. One might try to apply derivatives or other preprocessing tricks to improve the result further. For now I hope you got the basic idea on implementing a moving window PLS in your data analysis pipeline.

One last thing. As a bonus you can highlight the portion of the spectra that gives the best results. This can be helpful to assess which feature (or features) in the spectra are more useful for prediction and perhaps gain some intuition of the process at work. Here’s how you can do that.

That’s it for now. Thanks for reading and see you next time.