Food Analysis Lab · W13
← → / Space to change slide · F fullscreen · Tap either side to turn pages
Food Analysis Lab · W13

PLS Regression I: Acetic Acid

Quantitative NIR Calibration — PLS Regression for Acetic Acid
Food Analysis Lab · 4-hour session · each group uses its own NIR
Calibration set design · collinearity and LVs · choosing LVs by grouped cross-validation · RMSEC/RMSECV/RMSEP · RPD · instrument comparison
PLS regressionGrouped cross-validationRMSECVRPDOrange OR13 / Colab NB11
Before we start, a video from a winery
Wineries use NIR/FTIR to screen quality quickly,
but where does the model’s accuracy come from?

Wine factory production line (14:32–15:53, rapid NIR/FTIR QC) ↗

The core idea of today: wine and vinegar production lines both use spectrometers to screen composition quickly, but the spectrometer itself doesn’t know the “answer” — it is “taught” by a set of samples with known reference values. Today those reference values are the acetic acid results from the W4 manual titration. Systematic bias in the reference method carries straight into the calibration; independent random error in the reference values raises the RMSEP of the validation — this sentence runs through the whole session.

Today’s task: prepare a series of acetic acid standards of known concentration, measure their NIR spectra, use PLS regression to “translate” spectra into concentration, and learn to choose model complexity by cross-validation and report performance honestly on an independent test set.

W13 / PLS COURSE TOUR

PLS acidity: group samples, then validate honestly

Full narration and captions; simulated NB11 spectra. Player and downloads · Interactive lab

W13 / PREDICT · OBSERVE · EXPLAIN

Six acts: groups, PLS, locked settings, and testing

Today’s learning goals

After W13 you should be able to…

  • Explain how PLS uses LVs to handle “many wavelengths, few samples, collinearity”, and how it differs from PCA
  • Design calibration/test sets and tell interpolation from extrapolation
  • Choose LVs by grouped cross-validation and recognize underfitting / overfitting
  • Calculate RMSE / bias / R² / RPD and grade a model by RPD
  • Explain how reference-method bias and random error affect the calibration and RMSEP
  • Run OR13 in Orange and read the equivalent sklearn code
  • Complete your group’s PLS model and model report card
  • Explain why a model can’t be applied directly across instruments

All model numbers today come from the demo data in Colab NB11 (Orange OR13 reads the same data, so the numbers match); in class, each group measures its own spectra, then repeats the same workflow.

Calibration set design

Standards must cover the range; the test set must never be used to tune parameters

  • Dilute concentrated vinegar into 14 equally spaced standards (0–10% w/v), plus 2 vinegars (the white vinegar has a W4 titration value)
  • Kennard–Stone (KS) = pick the samples that best span the spectral space as the calibration set: first pick the two most different spectra, then each time pick the sample “farthest from those already selected”
  • KS picks using each sample’s mean spectrum; but when modeling, the 3 cuvette reloads are each saved separately, not averaged, and all 3 are treated as independent rows
  • The 3 spectra of the same sample must go together on the same side; otherwise it is data leakage, and the error looks falsely low
  • Use the test set only once, at the end, to compute RMSEP — never to choose the number of LVs or the preprocessing
NB11/OR13 demo data (16 samples × 3 cuvette reloads):

Calibration set 12 samples (36 spectra), including the 0% and 10% end points + commercial vinegar B
Test set 4 samples (12 spectra): STD-06 (4.62%), STD-08 (6.15%), STD-11 (8.46%), white vinegar (W4 titration) (4.50%)
The white vinegar is always placed in the test set as a “real sample with a known answer” checkpoint; KS decides the rest.
Preparation example (C₁V₁=C₂V₂): to make 100 mL of a 4.00% standard from 9.0% acetic acid: V₁ = 4.00 × 100 ÷ 9.0 = 44.44 mL, then dilute to 100 mL with water.
Interpolation vs. extrapolation

The model is trustworthy only within the concentration range it has seen

Calibration range 0–10%: interpolation (the model has seen this stretch)Extrapolation (not seen)024681012Acetic acid concentration (% w/v)?
  • Interpolation: a new sample falls inside the calibration range (0–10%) — the model has samples nearby to support it, so the prediction is trustworthy
  • Extrapolation: a new sample falls outside the range (e.g., 12%) — the model hasn’t seen it, and the spectrum–concentration relationship may no longer hold
  • So the 0% and 10% end points must stay in the calibration set (blue dots = calibration set, orange triangles = test set, all inside the range); the report must state the applicability domain: this model applies only to 0–10% w/v acetic acid in water
From a calibration curve to the whole spectrum

W07 used one wavelength; today we use the whole spectrum

Calibration curve (one wavelength): A = m·C + b
Used in reverse: C = (A − b) ÷ m

Whole spectrum (many wavelengths):
ŷ = b₀ + b₁x₁ + b₂x₂ + … + bpxp
   = b₀ + Σ bjxj
  • For the calibration curve, R², and RMSE, see W07 Calibration curve and validation metrics ↗
  • ŷ = predicted acetic acid concentration; xj = absorbance at the j-th wavelength; bj = weight (coefficient) of that wavelength; p = number of wavelengths
  • Solving every bj directly is called multiple linear regression (MLR); the W07 handheld Brix meter does exactly this with a few filters
⚠️
Problem: more unknowns than equations
Each demo spectrum has 60 wavelengths (the real InnoSpectra has 228), but the calibration set has only 12 samples (36 spectra).
To solve for 61 or more b values with only 12 “known answers”, MLR has infinitely many solutions, and adjacent wavelengths move almost together (next slide: collinearity).

The fix: don’t solve for 60 b values directly; first condense the spectrum into a few “latent variables” — that is PLS.

Collinearity

When two wavelengths change almost in sync, the coefficients become unstable

  • Absorbances at adjacent wavelengths rise and fall almost together: x₁ ≈ x₂, correlation coefficient r ≈ 1 (for the correlation coefficient see W04 Method comparison ↗)
  • Then many different sets of coefficients are equally accurate on the calibration samples — MLR can’t tell which set is right
  • Pick the wrong set, and a new sample with just a little noise gets its prediction amplified into a large error
  • Instead of solving for each wavelength’s coefficient directly, PLS first merges wavelengths that move together into LVs, so the coefficients are naturally stable
ModelCalibration sample
x₁=0.50, x₂=0.50
New sample
x₁=0.52, x₂=0.50
ŷ = x₁ + x₂1.001.02
ŷ = 11x₁ − 9x₂1.001.22

Both models give 1.00 on the calibration sample (equally accurate); x₁ gains only 0.02 of noise, yet the first model gives 1.02 and the second gives 1.22 — the larger the coefficients and the more their opposite signs cancel, the more the model amplifies noise.

PLS’s solution: latent variables (LV)

Combine the 60 wavelengths by weighted sum into a few new variables

① Spectrum X: 60 wavelengthsx₁, x₂, …, x₆₀ (after SNV)② One weight w per wavelengthWeights of NB11 LV 1 (orange = increases with concentration)③ LV score tt = Σ wⱼ·xⱼ④ Concentration ŷŷ = b₀ + q₁t₁ + q₂t₂
Too few / too many LVs

Number of LVs = model complexity; it needs to be just right

📉
Too few LVs: underfitting
The model can’t capture the signal → RMSEC and RMSECV are both high and close to each other; the predicted-vs-reference plot curves systematically, and the residuals show a pattern
📈
Too many LVs: overfitting
The model starts treating noise as signal → RMSEC keeps falling while RMSECV rises again; the number of LVs far exceeds the chemically plausible number of components (acetic acid, water, scattering)
What overfitting looks like

The curve passes through every point, yet predicts new samples worse

x (schematic)— straight line (less flexible)— 4th-degree curve (through every point)◆ New sample (true value)
  • The 5 points (black dots) carry a little measurement noise; the straight line misses some points, while the 4th-degree curve passes through all of them (calibration error = 0)
  • But now predict two new samples (green diamonds):
New sampleStraight line4th-degree curve
x = 1.5 (true value 1.5)1.560.75
x = 4.5 (true value 4.5)4.473.64

The number of LVs controls how many latent variables the model uses: more LVs fit the calibration set more closely (RMSEC↓), but what they fit may be noise, so new samples do worse (RMSECV, RMSEP↑) — for preprocessed input, PLS is still a linear combination; adding LVs does not turn it into a higher-degree polynomial. The 4th-degree curve here is only an analogy for the overfitting idea that “fitting too tightly sacrifices performance on new samples”; it does not mean PLS itself fits a polynomial. This figure uses illustrative numbers, not NB11 data.

How cross-validation works

Each time, leave out one sample as the “examinee”, rotating 12 times

123456789101112Round 1Round 2Round 3⋮Round 12Used for modeling (11 samples × 3 spectra)Left out and predicted (3 spectra of 1 sample)
  • Each round leaves out 1 sample (with its 3 spectra), builds the model on the other 11, and predicts the one left out; repeat for 12 rounds to collect 36 predictions made “without the model having seen that sample” → compute RMSECV
  • Splitting a sample’s 3 replicate spectra across both sides = data leakage: the model “recognizes” that cuvette, and RMSECV looks falsely low
  • Orange: Test and Score → Cross validation by feature → sample_id (already set in OR13)

This approach is called leave-one-group-out (grouped by sample_id); it must be run for every number of LVs to draw the RMSECV vs. LV curve.

Water and preprocessing

Choose preprocessing by comparing RMSECV, not by guessing

  • Water gives the strongest NIR signal (the large O–H band near 1450 nm); the acetic acid signal hides beside it, showing up as small changes in the local C–H / O–H shape
  • Reloading the cuvette shifts and tilts the whole spectrum up or down — unrelated to concentration, so it must be removed first
  • SNV = subtract each spectrum’s own mean and divide by its own standard deviation; SG first derivative = take the slope after local polynomial smoothing (W11 Preprocessing ↗)
Preprocessing (NB11)Best LVMinimum RMSECV (% w/v)
raw (none)30.458
SNV20.400
SG first derivative30.490

In the NB11 demo data, SNV gives the smallest RMSECV (LV=2), and all later figures use SNV. With your class’s data the conclusion may differ — so compare every time.

PLS autoscales by default

Orange’s Scale checkbox: each wavelength is first divided by its own SD

⚖️
What autoscaling does
Each wavelength (each column) first has its own mean subtracted, then is divided by its own standard deviation (SD) — giving every wavelength the same “volume”, so wavelengths with little variation are magnified too.
🔧
On by default in both sklearn and Orange
In sklearn PLSRegression(scale=True) and the Orange PLS widget, Scale is checked by default. NB11 and OR13 both keep the default, so the numbers on both sides match (RMSECV 0.400).
🔁
Different from the W12 PCA advice
W12 PCA ↗ recommends that PCA on spectra usually not autoscale (it magnifies noisy wavelengths). Here PLS uses the software default — don’t mix up the two weeks’ settings.
🧪
Try it yourself
In Orange, uncheck PLS’s Scale (or set scale=False in sklearn) and see whether RMSECV gets larger or smaller — decide by RMSECV again, don’t guess.
Data · LV selection

Choosing the number of LVs: the minimum of RMSECV vs. RMSEC falling all the way

NB11 demo data (preprocessing: SNV, cross-validation grouped by sample_id). RMSEC keeps falling as LVs increase; RMSECV is lowest at LV=2 (0.400), then rises and levels off — after LV=2 the further drop in RMSEC just fits noise in the calibration set, which is overfitting.
Mini-game ①Is this a sign of underfitting or overfitting?0 / 8
Sort the 8 phenomena below into “Underfitting” or “Overfitting”.
How errors are calculated

Residual e = predicted − reference; all four metrics are computed from e

MetricFormulaIn plain words
RMSE√( Σe² ÷ n )On average, how far off (same unit as concentration)
biasΣe ÷ nHow much too high (+) or too low (−) overall
R²1 − Σe² ÷ Σ(y − ȳ)²Proportion of the reference-value variance the model explains (W07 ↗)
RPDSD(ytest) ÷ RMSEPHow many times larger the samples’ own spread is than the error

e = residual; n = number of spectra; y = reference value (W4 titration / prepared concentration); ȳ = mean of the reference values; SD = standard deviation. Computed on the calibration set it is called RMSEC, by cross-validation RMSECV, and on the test set RMSEP.

Small example: 4 residuals e = 0.2, −0.1, 0.1, −0.2

Σe² = 0.04 + 0.01 + 0.01 + 0.04 = 0.10
RMSE = √(0.10 ÷ 4) = √0.025 ≈ 0.158
bias = (0.2 − 0.1 + 0.1 − 0.2) ÷ 4 = 0

bias = 0 does not mean there is no error: positives and negatives cancel out, so look at RMSE and bias together.

Figures of merit

Model report card: SNV, 2 LVs

MetricMeaningNB11 value
RMSECThe calibration set testing itself (optimistic)0.269% w/v
RMSECVGrouped cross-validation, used to choose LVs0.400% w/v
RMSEPIndependent test set; use this in the final report0.427% w/v
R²(test)Proportion of the reference-value variance the model explains0.929
bias(test)Mean over- / under-prediction on the test set−0.054% w/v
RPDSD(ytest) 1.67 ÷ RMSEP3.91
RPD grading (course-wide standard)
< 2: insufficient
2–2.5: rough screening
2.5–3: screening
▶ 3–5: QC quantification (this model 3.91)
> 5: excellent
RPD describes only the relative performance on this test set: RPD varies with the concentration spread of the test set itself, and this test set has only 4 independent samples, so the number involves some luck; whether the model is truly suitable for “QC quantification” also depends on RMSEP, bias, and the acceptance tolerance, and should be verified with more independent samples — a conclusion can’t rest on the RPD grade alone.
Random error in the reference method raises RMSEP: the standard deviation of a single W4 titration is about 0.05% w/v; if the reference values come from single titrations, RMSEP can hardly be meaningfully smaller than that; if the computed value is far below it, suspect data leakage first. (Systematic bias is a different matter — it shifts the calibration directly and does not go away with more samples; if the reference values are themselves averages of several titrations, this floor also gets smaller.)
Predicted vs. reference

Calibration set (cross-validation) and test set, against the 1:1 line

Test sample (reference value)Residuals e of the 3 cuvette reloads
STD-06 (4.62)+0.72, +0.16, −0.26
STD-08 (6.15)+0.64, −0.12, −0.15
STD-11 (8.46)−0.25, −0.02, −0.17
White vinegar (W4 titration) (4.50)−0.62, +0.19, −0.78

NB11 demo data. Blue dots = cross-validation predictions of the calibration set; orange triangles = test set (final model). The closer the points are to the diagonal, the more accurate; a consistent over- or under-prediction is bias. The 12 residuals in the table at right are the source of RMSEP 0.427.

Mini-game ②Calculation challenge: compute RMSEP from the previous slide’s 12 residualsUnanswered
The test set on the previous slide has 4 samples × 3 cuvette reloads, 12 residuals in all (predicted − reference, % w/v): +0.72, +0.16, −0.26, +0.64, −0.12, −0.15, −0.25, −0.02, −0.17, −0.62, +0.19, −0.78. Find RMSEP. Formula: RMSEP = √( Σe² ÷ n ), n = 12.
% w/v
Orange hands-on: OR13

File → SNV → Select Rows → PLS → two Test and Score

Fileor13_acetic.csv16 samples × 3Preprocess SpectraSNVSelect Rowssplit = calibrationThe rest = test setPLSComponents = 2Scale ✓ (default)Test and Score ①CV by feature: sample_idRMSE → RMSECVTest and Score ②Test on test dataRMSE → RMSEPScatter PlotReference vs. prediction
  • Test and Score ① takes only the calibration set (Matching from Select Rows); choose Cross validation by feature → sample_id: the RMSE column = RMSECV 0.400
  • Test and Score ② also takes the test set (Unmatched → Test Data); choose Test on test data: the RMSE column = RMSEP 0.427

Exercise: change PLS Components to 1–6 and see at which number the RMSE in ① is lowest.

🐍 Equivalent sklearn (Colab NB11)
pls = PLSRegression(n_components=2)   # scale=True is the default
y_cv = cross_val_predict(pls, X_cal, y_cal,
        groups=sample_id, cv=LeaveOneGroupOut())
pls.fit(X_cal, y_cal)
y_pred = pls.predict(X_test)      # → RMSEP
A separate simulation (5 NIR instruments across the class)

Instrument and band comparison: RMSEP side by side

GroupInstrumentBandDispersion / detectorExcluded bandPreprocessing / LVRMSEP (separate simulation, % w/v)
G1InnoSpectra NIR-S-G1900–1700 nmDLP + single InGaAs—SNV / 20.122
G2InnoSpectra R141600–2400 nmDLP + extended InGaAsExcludes 1885–1994 nm (water absorption saturated)First derivative / 40.106
G3Avantes fiber-optic spectrometer900–1650 nmGrating + InGaAs array—SNV / 20.132
G4Avantes fiber-optic spectrometer (short-wave, bought on Taobao, awaiting delivery)600–1100 nmGrating + Si array—SNV / 20.325
G5NeoSpectra Micro Dev Kit1350–2500 nmMEMS FT-NIR + single InGaAsExcludes 1889–1987 and 2495–2500 nm (water absorption saturated)SNV / 20.129
Note: this slide is a separate simulation, not NB11 data — the same 16 acetic acid samples are re-simulated with each instrument’s band, resolution, and noise, and a PLS model is built for each (teaching simulation, not measured); it is used only to compare the relative merits of the instruments, so don’t compare these numbers directly with the NB11 RMSEP earlier.
Short-wave 600–1100 nm retains only the acetic acid C–H third overtone (near 905 nm, ≈4ν), the weakest signal and the largest RMSEP; long-wave (R14, NeoSpectra) has the C–H first overtone (near 1705 nm, ≈2ν) and the combination band (near 2270 nm), the strongest signal (for overtones see W10 Overtones ↗) — but at a 1 mm path length water absorption saturates near 1940 nm, so that region must be excluded before modeling.
A model can’t be applied directly across instruments: a model from instrument A usually shows clear bias on instrument B and must be recalibrated, or use calibration transfer = measure a small set of common samples on both instruments, work out a conversion that “translates” B’s spectra into A’s form, then keep using A’s model.
4-hour lab schedule

Lecture → prepare standards → measure → Orange modeling → compare

TimeContents
0:00–0:55Lecture: calibration set design, interpolation/extrapolation, collinearity and LVs, choosing LVs by cross-validation, error metrics
0:55–1:35Prepare the standard series (dilute concentrated vinegar / glacial acetic acid by weighing, in the fume hood); prepare the W4 commercial vinegar samples
1:35–2:35NIR measurement (cuvette, 3 reloads per sample, each saved separately; first confirm your group’s instrument supports cuvette transmission mode)
2:35–3:30Orange OR13: SNV → PLS → Test and Score (cross-validation grouped by sample_id) to choose LVs, then compute RMSEP on the test set
3:30–4:00Whole-class roundup: each group’s model report card; compare RMSEP and RPD across instruments
Safety rules

Glacial acetic acid: corrosive / flammable; cuvette handling

🧪
Glacial acetic acid is corrosive and flammable, with a pungent odor
Work entirely inside the fume hood, wear gloves and safety goggles, and keep away from ignition sources
🧤
When diluting, always add to water
When preparing standards, add the acetic acid slowly to the water and swirl to mix; never the reverse
🔬
Confirm transmission mode for the cuvette first
Be sure to check with the TA which configuration of your group’s NIR supports cuvette transmission measurement; if the light path is installed backwards, the whole batch is wasted
🧴
Rinse the cuvette immediately after use
Prevents acetic acid residue from corroding the cuvette or interfering with the next student’s measurement
Mini-game ③Order the PLS modeling workflow— / 8
Use ▲▼ to arrange the 8 steps of PLS quantitative modeling in the correct order.
Mini-game ④Calibration set vs. test set: which does this use belong to?0 / 8
Sort the 8 statements into “Calibration set” or “Test set”.
Mini-game ⑤PLS principles: live quiz0 / 6
Mini-game ⑥Decision challenge: instruments and quality interpretation0 / 5
Homework

Colab NB11

📥
Organize your group’s spectra and concentrations
Your group’s calibration/test spectra and W4 titration reference values; confirm the long-format data are correct (the 3 cuvette reloads share the same sample_id)
📈
Plot RMSECV vs. LV
Use cross-validation grouped by sample_id to choose the best number of LVs for your group’s data; attach the plot
📊
Calculate all six quality metrics
RMSEC, RMSECV, RMSEP, R², bias, RPD, with the calculation steps and the RPD grade
🆚
Compare with other groups’ instruments
Find a group with a different instrument / band, compare RMSEP, and discuss possible reasons

Colab NB11: Open NB11 in Colab ↗ · Beginner (no coding): Orange workflow OR13 ↗. The report must include: model report card (preprocessing, number of LVs, RMSEC/RMSECV/RMSEP, R², bias, RPD, applicable concentration range), and a discussion comparing with the model of another instrument.

Self-check

By the end of today you should be able to…

Tap to tick — make sure you really can do it.

中文