Day 19 — A calibration line

Lecture slides · Quiz · Weekly revision

Today’s question

A spectrophotometer reports an absorbance, a number without units, but you want to know the concentration of your sample in mg/L. To translate one into the other, you measure standards, solutions of known concentration, and fit a straight line through their readings. What are the concentrations of three unknown samples that read 0.310, 0.520 and 0.900?

The standards in data/calibration.csv cover 0 to 10 mg/L, and the unknowns are in data/unknowns.csv. Both are simulated; see data/README.md. No chemistry is needed beyond the idea of a concentration.

Save today: calibration-report.Rmd and its HTML, output/day19-unknowns.csv, both figures, and transfer-calibration.Rmd with its HTML. Create calibration-report.Rmd at the project root and put the code below into it.

Plotting the standards

We prepared the concentrations and measured the absorbance, so concentration goes on the x-axis and absorbance on the y-axis:

library(readr)
library(dplyr)
library(ggplot2)

standards <- read_csv("data/calibration.csv", show_col_types = FALSE)
calibration_plot <- ggplot(standards,
                          aes(x = concentration_mg_l, y = absorbance)) +
  geom_point(size = 2) +
  labs(x = "Known concentration (mg/L)", y = "Absorbance") +
  theme_minimal()
calibration_plot

The six readings rise from 0.041 to 0.687 almost along a straight line. We describe that line as absorbance = intercept + slope × concentration. The intercept is the reading at zero concentration, a background signal; the slope is how much the absorbance rises for each extra mg/L.

Fitting the line

Use lm() to find out the intercept and slope of the line that fits the standards best:

calibration_fit <- lm(absorbance ~ concentration_mg_l, data = standards)
coef(calibration_fit)

lm() fits a linear model: ~ separates the response on the left from the variable that explains it on the right, and coef() returns the fitted intercept and slope. The R documentation for lm describes more options, but we only need this straight line.

You should get an intercept of 0.03957 and a slope of 0.06489 per mg/L: each extra mg/L adds about 0.065 to the absorbance. The intercept is not zero, because even the blank standard reads 0.041; leave it in the line rather than forcing the line through zero. Add the line to the plot:

calibration_plot <- calibration_plot +
  geom_abline(intercept = coef(calibration_fit)[1],
              slope = coef(calibration_fit)[2], colour = "steelblue")
calibration_plot

[1] and [2] pick the first and second coefficient.

Prepared standards and their fitted calibration line.

How close are the standards to the line?

A residual is a measured reading minus the reading the line predicts at that concentration. A positive residual means the standard lies above the line. Use residuals() to find out whether the line misses any standard badly, or bends away from them:

standards <- standards |>
  mutate(fitted_absorbance = fitted(calibration_fit),
         residual = residuals(calibration_fit))
standards

residual_plot <- ggplot(standards,
                        aes(x = concentration_mg_l, y = residual)) +
  geom_hline(yintercept = 0, linetype = "dashed") +
  geom_point(size = 2) +
  labs(x = "Known concentration (mg/L)", y = "Residual absorbance") +
  theme_minimal()
residual_plot

Deviations of standard readings from the fitted line, in absorbance units.

Check the first standard by hand: 0.041 − 0.0396 = +0.0014, just above the line. All six residuals lie within ±0.008, on both sides of zero and without a curved pattern, so a straight line describes the standards well between 0 and 10 mg/L. Nothing was measured outside that range.

Concentrations of the unknowns

Turning the line around gives concentration = (absorbance − intercept) / slope. Use it to find out the concentration of each unknown, but only where its reading lies between the lowest and highest standard:

unknowns <- read_csv("data/unknowns.csv", show_col_types = FALSE)
intercept <- coef(calibration_fit)[1]
slope <- coef(calibration_fit)[2]

unknowns <- unknowns |>
  mutate(within_range = absorbance >= min(standards$absorbance) &
                        absorbance <= max(standards$absorbance),
         concentration_mg_l = if_else(
           within_range, (absorbance - intercept) / slope, NA_real_))
unknowns
write_csv(unknowns, "output/day19-unknowns.csv")

& requires both conditions to be true, and if_else() gives a missing value, NA_real_, where the reading is out of range. U1 is 4.17 mg/L and U2 7.40 mg/L. U3 reads 0.900, above the highest standard (0.687). The formula would give 13.3 mg/L, but no standard was measured there and many assays level off at high concentrations, so U3 gets NA. In the lab, it would be diluted and measured again.

In calibration-report.Rmd, explain what a standard is, show both plots, give the slope with its units (absorbance per mg/L), report U1 and U2, and explain why U3 has no estimate. Save the figures with ggsave(), restart R and render.

We prepared concentration and measured absorbance, so absorbance is the response: lm(absorbance ~ concentration_mg_l). We then rearrange that line to get a concentration. lm(concentration_mg_l ~ absorbance) is a different line.

A diluted sample

A different sample read far above the standards, so 1.00 mL of it was diluted with solvent to a total of 10.00 mL and measured again. The diluted solution read 0.520. Assuming nothing is lost during dilution, its concentration is ten times lower than the original’s. Use the calibration line and the dilution factor 10.00 / 1.00 to find out the concentration of the original sample, and add the calculation to your report.

diluted_absorbance <- 0.520
dilution_factor <- 10.00 / 1.00
diluted_mg_l <- (diluted_absorbance - intercept) / slope
original_mg_l <- diluted_mg_l * dilution_factor
c(diluted_mg_l = diluted_mg_l, original_mg_l = original_mg_l)

The diluted solution is 7.40 mg/L, so the original is 74.04 mg/L. This works because 0.520 is a new reading inside the standards. It does not give a value for U3, which would need its own dilution and reading.

Does rounding the coefficients matter?

A report might print the line as absorbance = 0.04 + 0.06 × concentration. Use round(intercept, 2) and round(slope, 2) in a separate calculation to find out what U1 would come out as if someone used these rounded numbers.

With the full coefficients U1 is 4.17 mg/L; with the rounded ones it is 4.50 mg/L, 8% too high (and U2 becomes 8.00 instead of 7.40). The slope is a small number, so rounding it to 0.06 changes it a lot. Calculate with intercept and slope, and round only the result you write in the text.

A second instrument

Another instrument was calibrated with six standards from 0 to 5 mg/L (data/calibration-transfer.csv), and two samples, X1 and X2, were measured on it (data/unknowns-transfer.csv). Create transfer-calibration.Rmd with its own import, plot, residual plot and fit, using new object names such as transfer_standards and transfer_fit. Use its line to find out the concentrations of X1 and X2, keeping the range check.

The new line has intercept 0.0216 and slope 0.1001 per mg/L, so the old line does not apply to this instrument. The standards read 0.024 to 0.525. X1 (0.270) is 2.48 mg/L; X2 (0.700) is above the standards and gets NA, to be diluted and measured again. Putting 2.48 back into the line, intercept + slope * 2.48, returns 0.270: a quick check that the algebra is right.

Check your understanding

  • What does a positive residual mean? The standard reads higher than the line predicts.
  • Does a good fit from 0 to 10 mg/L support an estimate at 100 mg/L? No; only between the lowest and highest standard.
  • Are the concentration estimates exact? No; the standards, the readings and the fitted line all carry some uncertainty, which we have not calculated.

Leave with: calibration-report.Rmd, its HTML, output/day19-unknowns.csv, both figures, and transfer-calibration.Rmd with its HTML.

Next: Day 20 — A biological association.