Spectra Lab
Methods and references

How Spectra Lab calculates the numbers

Formulas, a worked example you can reproduce, the standards these statistics relate to, and the check of the engine against numpy and scipy.

Background: real mango spectra, colour = wavelength (false colour beyond the visible).
Real data

What real peaks look like

Thirty-six intact mango fruit, measured 684 to 990 nm. Every thin line is one fruit; the heavy line is their mean. The labelled features are found in the data, not drawn in.

Mango, NIR signal vs wavelength
Data: Anderson, Walsh, Subedi (2020), Mendeley Data, doi 10.17632/46htwnp833.1, CC BY 4.0; 36 spectra sampled from the teaching subset, values as supplied in the file. Line colour marks wavelength; beyond the visible it is false colour. The chart has its own light and dark switch, independent of the page.
Definitions

The formulas, exactly as the calculators use them

Let ri be the laboratory (reference) value and pi the model prediction for sample i, for n samples the model did not learn from. di = pi − ri.

bias = (1/n) Σ d_i SEP = sqrt( Σ (d_i − bias)² / (n − 1) ) RMSEP = sqrt( Σ d_i² / n ) slope, intercept = least-squares fit of p on r R² = Sxy² / (Sxx · Syy) (squared Pearson correlation) SD_ref = sqrt( Sxx / (n − 1) ) RPD = SD_ref / SEP SEL = sqrt( Σ (a_i − b_i)² / (2n) ) (blind duplicate pairs a, b) SEP target = 1.5 × SEL to 2 × SEL

Two conventions differ between software packages, so state them whenever you compare numbers: SEP here divides by n − 1 (the bias is removed first), and RMSEP divides by n. RMSEP² = SEP² (n − 1)/n + bias², so the two agree only when the bias is small and n is large. The slope is the fit of predicted on reference; some packages fit the other way round, which gives a different number.

Bias is tested with t = bias / (SEP / √n) against Student's t at n − 1 degrees of freedom. The calculators accept 3 or more pairs; a result from a handful of samples is a poor estimate, whatever the arithmetic says.

Worked example

Eight samples, every number reproducible

Illustrative values chosen to show the arithmetic, not measured data. Paste them into the free check and you get the same results.

#ReferencePredictedPredicted minus reference
110.110.4+0.3
212.412.0-0.4
39.810.3+0.5
415.214.6-0.6
511.011.5+0.5
613.713.2-0.5
78.99.4+0.5
814.114.8+0.7
StatisticValue
Pairs (n)8
Bias0.125
SEP0.531
RMSEP0.512
Slope0.862
Intercept1.773
R²0.955
SD of reference2.289
RPD4.310

These results are computed by the same JavaScript file the page ships (nir_stats.js) when this page is built, not typed in by hand.

How the engine was checked

Compared with numpy and scipy on five test sets

The browser engine and a reference implementation (numpy and scipy.stats.linregress, with the n − 1 convention) were run on five synthetic sets of 12, 30, 80, 200 and 500 pairs from a fixed random seed. Every statistic was compared: bias, SEP, RMSEP, slope, intercept, R², SD of reference and RPD.

  • Largest relative difference over all statistics and all five sets: 8 × 10−14 (floating-point rounding).
  • The check can fail: computing SEP with n instead of n − 1 changes SEP by 4.3 % on the 12-pair set, far above that tolerance, so a convention error would be caught.
  • The test sets are synthetic and show that the arithmetic is right. They do not show that any particular NIR model is good.
Limits

What these numbers cannot tell you

  • Use samples the model never saw. Statistics on the calibration set describe how well the model memorised, not how it will perform.
  • RPD bands are rules of thumb. Suggested cut-offs differ between fields and authors. Read RPD together with the range of your samples, the laboratory error (SEL) and what accuracy your decision needs.
  • A validation holds for the population it covers. Samples from a new season, instrument, supplier or range need their own check.
  • The reference method has error too. A SEP close to SEL is near the best any model can show.
  • The free check needs only pairs. It cannot see outliers in the spectra, wavelength choices or overfitting inside the model. The Lab report covers those on real data.
References

Standards these statistics relate to

Listed from the publishers' catalogue pages. The standards are paid documents and we have not reproduced their text; consult them for their exact procedures and limits. Where a standard and this page differ, follow the standard.

Try it

Run the check on your own model

Check my calibration See a real validation report