Nothing
## propagate demo: examples of the EURACHEM/CITAC Guide CG 4 "Quantifying Uncertainty in Analytical
## Measurement" (QUAM:2012.P1, 3rd edition), Appendix A
## demo("QUAM", package = "propagate")
## Not included: A7 (lead in water by double isotope dilution mass spectrometry), whose measurement equation
## involves several dozen isotope ratios and correction factors.
library(propagate)
library(crayon) ## green(), red(), blue() in gauge()
M <- 1E6 ## Monte Carlo trials
## comparison of a computed value with a published (gauge) value; the published value is given as a
## character string, so that its decimals define the rounding tolerance; 'se' adds 4 Monte Carlo
## standard errors (of both calculations) for Monte Carlo results; 'extra' adds the propagated rounding
## of published intermediate values; note = TRUE marks a documented discrepancy (NOTE instead of DIFF)
gauge <- function(item, value, published, se = 0, note = FALSE, extra = 0) {
d <- if (grepl(".", published, fixed = TRUE)) nchar(sub("^[^.]*\\.", "", published)) else 0
pub <- as.numeric(published); value <- unname(value); tol <- 0.5 * 10^-d + 4 * se + extra; ok <- abs(value - pub) <= tol * (1 + 1E-9) ## exact half-units of rounding pass
cat(sprintf(" %-46s published %12s propagate %14.*f |diff| %8.2g tol %8.2g %s\n",
item, published, d + 2, value, abs(value - pub), tol, if (ok) green("PASS") else if (note) red("NOTE") else blue("DIFF")))
invisible(ok)
}
## Monte Carlo standard errors (both calculations with M trials) of the mean and the standard deviation of y
seMean <- function(y) sqrt(2) * sd(y)/sqrt(length(y))
seSD <- function(y) { k <- mean((y - mean(y))^4)/var(y)^2; sqrt(2) * sd(y) * sqrt((k - 1)/(4 * length(y))) }
seQ <- function(y, p) { f <- density(y, from = quantile(y, p), to = quantile(y, p), n = 1)$y; sqrt(2) * sqrt(p * (1 - p)/length(y))/f }
set.seed(2012)
######## A1 Preparation of a calibration standard: c_Cd = 1000 m P / V [mg/L] ########
## V from its components: calibration (+/- 0.1 mL, triangular), repeatability (0.02 mL),
## temperature (+/- 0.084 mL, rectangular); P: 0.9999 +/- 0.0001 (rectangular)
EX1 <- expression(1000 * m * P/(V + dVcal + dVrep + dVtemp))
B1 <- data.frame(name = c("m", "P", "V", "dVcal", "dVrep", "dVtemp"), value = c(100.28, 0.9999, 100.0, 0, 0, 0),
u = c(0.05, NA, 0, NA, 0.02, NA), a = c(NA, 0.0001, NA, 0.1, NA, 0.084),
dist = c("normal", "rect", "normal", "tri", "normal", "rect"))
A1 <- propagate(EX1, makeDat(EX1, data = B1, output = "sim", nsim = M), type = "sim", check = FALSE)
A1
cat("Comparison with QUAM A1 (Tables A1.1 - A1.3):\n")
gauge("u(V, calibration) [mL]", 0.1/sqrt(6), "0.04"); gauge("u(V, temperature) [mL]", 0.084/sqrt(3), "0.05")
gauge("u(V) [mL]", sqrt(0.1^2/6 + 0.02^2 + 0.084^2/3), "0.07"); gauge("u(P)", 0.0001/sqrt(3), "0.000058")
## QUAM propagates the rounded u(V) = 0.07 mL (exact 0.0665 mL): extra tolerance 1002.7 x 0.005/100 mg/L
eV <- 1002.7 * 0.005/100
gauge("c_Cd [mg/L]", A1$prop["mean.1"], "1002.7"); gauge("u(c_Cd) [mg/L]", A1$prop["u.1"], "0.9", extra = eV)
gauge("u(c_Cd)/c_Cd", A1$prop["u.1"]/A1$prop["mean.1"], "0.0009", extra = eV/1002.7); gauge("U = 2u [mg/L]", 2 * A1$prop["u.1"], "1.8", extra = 2 * eV)
gauge("MC u(c_Cd) [mg/L]", A1$sim["u.MC"], "0.9", seSD(A1$resSIM), extra = eV)
######## A2 Standardising a sodium hydroxide solution: c_NaOH = 1000 m P / (M V) [mol/L] ########
A2 <- propagate(expression(1000 * m * P * rep/(Mk * V)),
cbind(rep = c(1, 0.0005, Inf), m = c(0.3888, 0.00013, Inf), P = c(1.0, 0.00029, Inf), Mk = c(204.2212, 0.0038, Inf),
V = c(18.64, 0.013, Inf)), type = "stat", nsim = M, check = FALSE)
A2
cat("Comparison with QUAM A2 (Table A2.1):\n")
gauge("c_NaOH [mol/L]", A2$prop["mean.1"], "0.10214"); gauge("u(c_NaOH) [mol/L]", A2$prop["u.1"], "0.00010")
gauge("u(c_NaOH)/c_NaOH", A2$prop["u.1"]/A2$prop["mean.1"], "0.00097")
gauge("MC u(c_NaOH) [mol/L]", A2$sim["u.MC"], "0.00010", seSD(A2$resSIM))
######## A3 An acid/base titration: c_HCl = 1000 m P V_T2 / (V_T1 M V_HCl) [mol/L] ########
A3 <- propagate(expression(1000 * m * P * VT2 * rep/(VT1 * Mk * VHCl)),
cbind(rep = c(1, 0.001, Inf), m = c(0.3888, 0.00013, Inf), P = c(1.0, 0.00029, Inf), VT2 = c(14.89, 0.015, Inf),
VT1 = c(18.64, 0.016, Inf), Mk = c(204.2212, 0.0038, Inf), VHCl = c(15, 0.011, Inf)), type = "stat", nsim = M, check = FALSE)
A3
cat("Comparison with QUAM A3 (Table A3.1):\n")
gauge("c_HCl [mol/L]", A3$prop["mean.1"], "0.10139")
gauge("u(c_HCl) [mol/L] (Table A3.1 inputs)", A3$prop["u.1"], "0.00016", note = TRUE)
gauge("u(c_HCl)/c_HCl (Table A3.1 inputs)", A3$prop["u.1"]/A3$prop["mean.1"], "0.0016", note = TRUE)
## NOTE: the inputs of Table A3.1 (single determination: rep 0.001, u(VT2) = 0.015 mL, u(VT1) = 0.016 mL) give
## u = 0.00019 mol/L. The published 0.00016 mol/L is the result of the triple determination (A3.7): repeatability
## 0.001/sqrt(3), u(VT2) = 0.014 mL and u(VT1) = 0.015 mL, which reproduces it:
A3t <- propagate(expression(1000 * m * P * VT2 * rep/(VT1 * Mk * VHCl)),
cbind(rep = c(1, 0.001/sqrt(3), Inf), m = c(0.3888, 0.00013, Inf), P = c(1.0, 0.00029, Inf), VT2 = c(14.89, 0.014, Inf),
VT1 = c(18.64, 0.015, Inf), Mk = c(204.2212, 0.0038, Inf), VHCl = c(15, 0.011, Inf)), type = "stat", nsim = M, check = FALSE)
A3t
gauge("u(c_HCl) [mol/L] (triple determination, A3.7)", A3t$prop["u.1"], "0.00016")
gauge("u(c_HCl)/c_HCl (triple determination, A3.7)", A3t$prop["u.1"]/A3t$prop["mean.1"], "0.0016")
######## A4 Organophosphorus pesticide in bread (in-house validation): relative uncertainty budget ########
## precision from the duplicate analyses of Table A4.2: relative u = sd(relative differences)/sqrt(2)
data(QUAM.A4)
rd <- with(QUAM.A4, (D1 - D2)/((D1 + D2)/2)); uP <- sd(rd)/sqrt(2)
## recovery Rec = 0.9 (u = 0.043), other sources (relative u 0.2); result relative to 1
A4 <- propagate(expression(Fprec * Foth * 0.9/Rec), cbind(Fprec = c(1, uP, Inf), Rec = c(0.9, 0.043, Inf), Foth = c(1, 0.2, Inf)),
type = "stat", nsim = M, check = FALSE)
A4
cat("Comparison with QUAM A4 (Table A4.1):\n")
gauge("sd of the relative differences", sd(rd), "0.382"); gauge("relative u(precision)", uP, "0.27")
gauge("relative u(Rec)", 0.043/0.9, "0.048"); gauge("relative u(P_op)", A4$prop["u.1"], "0.34")
cat(" (Monte Carlo: the large relative uncertainties give a skewed output, 95 % interval [",
round(A4$sim[5], 3), ",", round(A4$sim[6], 3), "] around 1)\n")
######## A5 Cadmium release from ceramic ware: r = c0 V_L d f_acid f_time f_temp / a_V [mg/dm^2] ########
## c0 and u(c0) from the AAS calibration (Table A5.2) and two readings of the leach solution (QUAM E.4)
data(QUAM.A5)
FIT <- lm(absorbance ~ conc, data = QUAM.A5); S <- summary(FIT)$sigma
Sxx <- sum((QUAM.A5$conc - mean(QUAM.A5$conc))^2)
uc0 <- unname(S/coef(FIT)[2] * sqrt(1/2 + 1/15 + (0.26 - mean(QUAM.A5$conc))^2/Sxx))
cat("Comparison with QUAM A5, calibration (Table A5.2, E.4):\n")
gauge("slope B1", coef(FIT)[2], "0.2410"); gauge("intercept B0", coef(FIT)[1], "0.0087")
gauge("residual standard deviation S", S, "0.005486"); gauge("S_xx [(mg/L)^2]", Sxx, "1.2")
gauge("u(c0) [mg/L]", uc0, "0.018")
A5 <- propagate(expression(c0 * VL * d * facid * ftime * ftemp/aV),
cbind(c0 = c(0.26, uc0, Inf), VL = c(0.332, 0.0018, Inf), d = c(1, 0, Inf), aV = c(5.73, 0.19, Inf),
facid = c(1, 0.0008, Inf), ftime = c(1, 0.001, Inf), ftemp = c(1, 0.06, Inf)), type = "stat", nsim = M, check = FALSE)
A5
cat("Comparison with QUAM A5 (Table A5.1):\n")
gauge("r [mg/dm^2]", A5$prop["mean.1"], "0.015")
gauge("u(r) [mg/dm^2] (Table A5.1)", A5$prop["u.1"], "0.0014", note = TRUE)
gauge("u(r)/r (Table A5.1)", A5$prop["u.1"]/A5$prop["mean.1"], "0.092", note = TRUE)
## NOTE: the spreadsheet of QUAM (Table A5.4) gives u_c(r) = 0.001465 mg/dm^2, i.e. 0.0015 mg/dm^2 and a relative
## uncertainty of 0.097; Table A5.1 prints 0.0014 and 0.092
gauge("u(r) [mg/dm^2] (spreadsheet 0.001465, rounded)", A5$prop["u.1"], "0.0015")
######## A6 Crude fibre in animal feeding stuffs (collaborative trial data), at 2.5 % m/m ########
## repeatability/reproducibility (0.29 % m/m) and other effects (0.115 % m/m)
A6 <- propagate(expression(C + dR + dO), cbind(C = c(2.5, 0, Inf), dR = c(0, 0.29, Inf), dO = c(0, 0.115, Inf)),
type = "stat", nsim = M, check = FALSE)
A6
cat("Comparison with QUAM A6 (Table A6.1):\n")
gauge("u(C_fibre) [% m/m]", A6$prop["u.1"], "0.31"); gauge("u(C_fibre)/C_fibre", A6$prop["u.1"]/2.5, "0.12")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.