# Classifying spectra

I am currently trying to do the following in R:

I have thousands of measured spectra (x,y; see below). Each spectra has one or two peaks. Also I have sets of "training" spectra obtained in more controlled conditions and I would like to know which of my training spectra has the closest match to the measured spectra!?

I was thinking that some sort of pattern recognition would be useful but I know too little to make an informed choice as this is a bit outside of my usual work-area

• What is the most promising way/function in R to do this kind of pattern recognition I want?
• In case pattern recognition (like PCA) is not the most promising way, what other options are there?

I am looking for sample bits of code or literature dealing with this kind of data analysis.

EDIT The peak position will most probably always be the same, however the laser used to record the spectra is temperature controlled and slight variations are possible. The intensity will change depending on experimental conditions. The two peaks sould be treated as independet peaks.

-
Do your spectra have their peaks at the same locations, but varying powers? Or do the peaks shift? Are the peaks independent or interrelated? Such a priory information may help choosing a much powerful model than feeding your raw data into a generic data mining algorithm. –  GaBorgulya Apr 27 '11 at 12:03
@GaBorgulya I added some more information to the question –  Martin H Apr 27 '11 at 20:59

Perhaps dynamic time warping is appropriate? Maybe you can use it to measure the distance between your "query" signals and your training signals (dtw actually calculates alignments between two signals, from which you can derive the distance). You'd then pick the signal from your training data that's the nearest neighbor to your query.

There is a dtw package on CRAN that lets you do this for R, which also has a very nice vignette.

The alignments are a bit intensive to compute using dtw, there is a Java implementation that calculates a fast approximation to the alignments if you want to pursue this further.

-

This is a straightforward classification problem. The trick is in finding a feature vector for each spectrum. Then we can simply associate each one with the nearest training spectrum in the feature space.

The simplest way to reduce each spectrum to a vector is to represent it as the parameters of a mixture model. Gaussian is the obvious candidate, but a distribution with non-negative support like gamma seems appropriate for your problem too. I'll use Gaussians for illustration since their parameters are easy to estimate by inspection.

We are given the assumption that there are invariably two peaks, so this makes the model very simple: a mean and a variance for each of the two normal densities, plus a relative mixture weight (we can fix one without loss of generality) adding up to five real numbers. In other words, we are mapping each spectrum to a 5D vector.

Your illustration can be roughly approximated as $\mathbf x=[66\; 10 \;103 \;10 \;9]$, where the first four co-ordinates describe the densities and last co-ordinate determines the relative weight of the second density in the mixture. This is what the Gaussian mixture approximation looks like:

I assume you are capable of estimating the parameters of the mixture model with your spectra so I'll skip that part.

Another feature extraction algorithm, as you guessed and cbeleites alluded to below, is PCA. This works by taking the eigendecomposition of the autocovariance of your spectra. The features then are the principal values. The benefit of this approach is that it offers the most compact possible representation of your spectra; i.e., if you allot a fixed number of dimensions to your feature vector, the principal values are the optimal choice to reduce the mean squared error. The drawback is that it works offline (online SVD/PCA variants notwithstanding), while the mixture model algorithm is online.

The final part is calculating the distances. The simplest metric is the Euclidean distance, which equally weights the features. If you want to emphasize certain features over others, such as the mean over the variance, you can use a weighted norm:

$\| x - y \|_p = \left[ \sum_i w_i ( x_i-y_i )^p \right]^{1/p}$, where the weights $w_i$ sum to unity.

-
how do you know it is easy, if the OP didn't even write what kind of spectra he measures? Which implies that we don't have any idea what kind of variation to expect due to instrumental conditions. –  cbeleites May 9 '12 at 13:37
We know variations are "slight", there are only "two peaks", whose "position will most probably always be the same", and "independent". In the absence of additional information to the contrary I call this easy and support this simple mixture model. We can use a richer spectra model, but it will increase the feature space, so it has to be warranted. –  Emre May 9 '12 at 16:31
Oh, maybe there's a difference in our language interpretation. Spectral band/peak positions that are "most probably always the same" and the next part of the sentence indicating variation due to T cycles of the laser (sounds like variation in x position, but that is not really specified) raise at least a warning for me... Actually, I'd have tried to go for a more restricted model: seeing whether the training data can give us either pure component spectra allows to derive a difference spectrum - which means a single parameter to be estimated from each spectrum. Anyways, we're a year late... –  cbeleites May 9 '12 at 16:45