demo/EMUE.R

## propagate demo: examples of the EMUE Compendium (EMPIR project 17NRM05 "EMUE",
## "Good practice in evaluating measurement uncertainty - Compendium of examples", EURAMET 2021)
## demo("EMUE", package = "propagate")
## For each example: propagate() output (GUM uncertainty framework and Monte Carlo), a histogram of
## the Monte Carlo output with the best fitting distribution, and a comparison with the published values.
library(propagate)
library(crayon)

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; note = TRUE marks a documented
## discrepancy of the published value (printed as NOTE instead of DIFF)
gauge <- function(item, value, published, se = 0, note = FALSE) {
  d <- if (grepl(".", published, fixed = TRUE)) nchar(sub("^[^.]*\\.", "", published)) else 0
  pub <- as.numeric(published); tol <- 0.5 * 10^-d + 4 * se; ok <- abs(value - pub) <= tol
  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(105)

######## 3.5 Molar mass of phenol C6H5OH: rectangular atomic weights ########
EXP <- expression(6 * C + 6 * H + O)
BP <- data.frame(name = c("C", "H", "O"), value = c(12.0106, 1.007975, 15.9994),
                 a = c(0.001, 0.000135, 0.00037), dist = "rect")
RP <- propagate(EXP, makeDat(EXP, data = BP, output = "sim", nsim = M), type = "sim", check = FALSE)
RP
y <- RP$resSIM; U95 <- unname(diff(RP$sim[5:6])/2)
cat("Comparison with EMUE 3.5 (M = 1e5):\n")
gauge("M(phenol) [g/mol]", RP$sim["mean.MC"], "94.1108", sqrt(1 + M/1E5) * seMean(y)/sqrt(2))
gauge("u [g/mol]", RP$sim["u.MC"], "0.0035", sqrt(1 + M/1E5) * seSD(y)/sqrt(2))
gauge("U95 [g/mol]", U95, "0.0059", 6E-6)
gauge("k = U95/u", U95/RP$sim["u.MC"], "1.67", 0.003)
cat("  ", grep("validated", capture.output(summary(RP)), value = TRUE), "\n")   ## GUF NOT validated (trapezoidal output)

######## 3.6 Calibration of a weight (EA-4/02 M: 2022, S2) ########
EXM <- expression(mS + dmD + dm + dmC + dB)
BM <- data.frame(name = c("mS", "dmD", "dm", "dmC", "dB"), value = c(10000.005, 0, 0.020, 0, 0),
                 U = c(0.045, NA, NA, NA, NA), k = c(2, NA, NA, NA, NA), u = c(NA, NA, 0.025/sqrt(3), NA, NA),
                 a = c(NA, 0.015, NA, 0.010, 0.010), dist = c("normal", "rect", "normal", "rect", "rect"))
RM <- propagate(EXM, makeDat(EXM, data = BM, output = "sim", nsim = M), type = "sim", check = FALSE)
RM
y <- RM$resSIM
cat("Comparison with EMUE 3.6 (M = 2e5):\n")
gauge("m_X [g]", RM$sim["mean.MC"], "10000.025", sqrt(1 + M/2E5) * seMean(y)/sqrt(2))
gauge("u [g]", RM$sim["u.MC"], "0.029", sqrt(1 + M/2E5) * seSD(y)/sqrt(2))
gauge("95 % interval, low [g]", RM$sim[5], "9999.967", 2E-4); gauge("95 % interval, high [g]", RM$sim[6], "10000.082", 2E-4)

######## 5.3 Type A evaluation with correlated indications: GUM H.2 with n = 6 ########
H6 <- cbind(V = c(5.007, 4.994, 5.005, 4.990, 4.999, 4.999),
            I = c(19.663, 19.639, 19.640, 19.685, 19.678, 19.661),
            phi = c(1.0456, 1.0438, 1.0468, 1.0428, 1.0433, 1.0445))
R53 <- propagate(expression((V/I) * cos(phi)), H6, type = "raw", cov = "full", nsim = M, check = FALSE)
R53
cat("Comparison with EMUE 5.3 (Tables 5.2, 5.3):\n")
gauge("mean V [V]", R53$data[1, "V"], "4.9990"); gauge("mean I [mA]", R53$data[1, "I"], "19.6610")
gauge("mean phi [rad] (printed truncated)", R53$data[1, "phi"], "1.04446", note = TRUE)
gauge("u(V) [V]", R53$data[2, "V"], "0.0026"); gauge("u(I) [mA]", R53$data[2, "I"], "0.0077")
gauge("u(phi) [rad]", R53$data[2, "phi"], "0.00061")
gauge("r(V, I)", R53$corMat["V", "I"], "-0.355"); gauge("r(V, phi)", R53$corMat["V", "phi"], "0.858")
gauge("r(I, phi)", R53$corMat["I", "phi"], "-0.645")
gauge("nu_eff = n - 1 (Willink 2007)", R53$ws.df, "5")

######## E4.8 Pressure drop in a flow measurement (Tables E4.8.3, E4.8.4) ########
EX1 <- expression((p2/T2 - p1/T1) * 293.15 * 1800/(t2 - t1) + eA)
EX2 <- expression(((p2 - p1)/T1 - dT * p1/T1^2) * 293.15 * 1800/(t2 - t1) + eA)
BE <- data.frame(name = c("p1", "p2", "T1", "T2", "dT", "t1", "t2", "eA"),
                 value = c(50.001, 49.951, 292.85, 292.95, 0.10, 1000, 2800, 0),
                 u = c(0.0028, 0.0028, NA, NA, NA, NA, NA, 0.0025),
                 a = c(NA, NA, 0.15, 0.15, 0.02, 5, 5, NA),
                 dist = c("normal", "normal", "rect", "rect", "tri", "rect", "rect", "normal"))
RE1 <- propagate(EX1, makeDat(EX1, data = BE, output = "sim", nsim = M), type = "sim", check = FALSE)
RE1
y <- RE1$resSIM
cat("Comparison with EMUE E4.8, model E4.8.7 (Table E4.8.3):\n")
gauge("GUF estimate", RE1$prop["mean.1"], "-0.067120"); gauge("GUF u", RE1$prop["u.1"], "0.02143")
gauge("MCM u", RE1$sim["u.MC"], "0.02143", seSD(y))
gauge("MCM interval, low", RE1$sim[5], "-0.10789", 2 * seQ(y, 0.025))
gauge("MCM interval, high (minus sign restored)", RE1$sim[6], "-0.02634", 2 * seQ(y, 0.975))
RE2 <- propagate(EX2, makeDat(EX2, data = BE, output = "sim", nsim = M), type = "sim", check = FALSE)
RE2
y <- RE2$resSIM
cat("Comparison with EMUE E4.8, model E4.8.13 (Table E4.8.4):\n")
gauge("GUF estimate", RE2$prop["mean.1"], "-0.067143"); gauge("GUF u", RE2$prop["u.1"], "0.00489")
gauge("MCM interval, low", RE2$sim[5], "-0.07671", 2 * seQ(y, 0.025))
## Errata of the compendium: the upper interval endpoints in Tables E4.8.3/E4.8.4 lack their minus
## sign, and the MCM result printed for E4.8.13 is not consistent with the stated inputs.

Try the propagate package in your browser

Any scripts or data that you put into this service are public.

propagate documentation built on Nov. 10, 2026, 9:07 a.m.