R/makeDat.R

Defines functions .sampleBudget .datType makeDat

Documented in makeDat

## makeDat() -- propagate 1.3-0
## Creates the 'data' matrix for propagate() from
##  (a) variables in the workspace (as before),
##  (b) a file (.csv, .txt, .tsv, .dat, .xlsx, .xls) or a data frame in 'columns' layout:
##      one column per variable; rows = repeated observations (type = "raw"),
##      mean/u/df (type = "stat") or samples (type = "sim"),
##  (c) a file or data frame in 'budget' layout: one row per input quantity, as in an
##      uncertainty budget, converted to mean/u/df (output = "stat") or to samples from the
##      stated distributions (output = "sim").

makeDat <- function(expr = NULL, file = NULL, data = NULL, layout = c("auto", "columns", "budget"),
                    output = c("stat", "sim"), nsim = 1E6, sheet = 1, envir = parent.frame(), ...)
{
  layout <- match.arg(layout)
  output <- match.arg(output)
  if (!is.null(expr) && !is.expression(expr)) stop("makeDat: 'expr' must be an expression!")
  VARS <- if (!is.null(expr)) all.vars(expr) else NULL
  
  ## ------------------------------------------------------- (a) workspace
  if (is.null(file) && is.null(data)) {
    if (is.null(VARS)) stop("makeDat: supply 'expr', 'file' or 'data'!")
    miss <- VARS[!vapply(VARS, exists, logical(1), envir = envir)]
    if (length(miss)) stop("makeDat: object(s) '", paste(miss, collapse = "', '"), "' in expression but not in workspace!")
    LIST <- lapply(VARS, function(v) as.numeric(get(v, envir = envir)))
    LEN <- vapply(LIST, length, integer(1))
    if (length(unique(LEN)) > 1) 
      message("makeDat: variables have different lengths (", paste(VARS, LEN, sep = " = ", collapse = ", "), 
              "); shorter ones are padded with NA.")
    DATA <- sapply(LIST, function(v) c(v, rep(NA, max(LEN) - length(v))))
    DATA <- matrix(DATA, ncol = length(VARS), dimnames = list(NULL, VARS))
    return(.datType(DATA))
  }
  
  ## ------------------------------------------------------- read file
  if (!is.null(file)) {
    if (!file.exists(file)) stop("makeDat: file '", file, "' not found!")
    ext <- tolower(sub(".*\\.([^.]+)$", "\\1", basename(file)))
    if (ext %in% c("xlsx", "xls")) {
      if (!requireNamespace("readxl", quietly = TRUE)) 
        stop("makeDat: reading Excel files requires package 'readxl'; alternatively save the sheet as .csv.")
      data <- as.data.frame(readxl::read_excel(file, sheet = sheet, ...))
    } else if (ext %in% c("csv", "txt", "tsv", "dat")) {
      first <- readLines(file, n = 1, warn = FALSE)
      sep <- if (grepl("\t", first)) "\t" else if (lengths(regmatches(first, gregexpr(";", first))) > 
                                                   lengths(regmatches(first, gregexpr(",", first)))) ";" else ","
      dec <- if (sep == ";") "," else "."          ## European CSV: ';' separator, decimal comma
      data <- utils::read.table(file, header = TRUE, sep = sep, dec = dec, stringsAsFactors = FALSE,
                                check.names = FALSE, strip.white = TRUE, ...)
    } else stop("makeDat: unknown file type '.", ext, "' (use .csv, .txt, .tsv, .dat, .xlsx or .xls)!")
  }
  data <- as.data.frame(data, stringsAsFactors = FALSE)
  
  ## ------------------------------------------------------- layout
  cn <- tolower(names(data))
  if (layout == "auto") layout <- if ("name" %in% cn && any(c("mean", "value", "x", "estimate") %in% cn)) "budget" else "columns"
  
  ## (b) columns: one column per variable
  if (layout == "columns") {
    num <- vapply(data, is.numeric, logical(1))
    if (!all(num)) stop("makeDat: non-numeric column(s) '", paste(names(data)[!num], collapse = "', '"), 
                        "' (check decimal separator or header).")
    DATA <- as.matrix(data)
    if (!is.null(VARS)) {
      miss <- setdiff(VARS, colnames(DATA))
      if (length(miss)) stop("makeDat: variable(s) '", paste(miss, collapse = "', '"), "' of 'expr' not found in the data!")
      DATA <- DATA[, VARS, drop = FALSE]
    }
    return(.datType(DATA))
  }
  
  ## (c) budget: one row per input quantity
  ## 'u' (standard) and 'U' (expanded) are distinguished by case; all other names are case-insensitive
  on <- names(data)
  col <- function(...) { a <- which(cn %in% c(...)); if (length(a)) data[[a[1]]] else NULL }
  colCS <- function(...) { a <- which(on %in% c(...)); if (length(a)) data[[a[1]]] else NULL }
  NAME <- as.character(col("name")); MEAN <- as.numeric(col("mean", "value", "x", "estimate"))
  U <- colCS("u", "u_std", "std"); Uexp <- colCS("U", "U_exp", "Uexp", "expanded"); K <- col("k")
  A <- col("a", "halfwidth", "half-width"); DF <- col("df", "nu", "dof"); DIST <- col("dist", "distribution", "pdf")
  DD <- col("d", "ctrap_d")                                      ## curvilinear trapezoid: inexactness of the limits
  n <- length(NAME)
  DIST <- if (is.null(DIST)) rep(NA, n) else tolower(as.character(DIST))
  DF <- if (is.null(DF)) rep(Inf, n) else as.numeric(DF)
  DF[is.na(DF)] <- Inf
  DIST[is.na(DIST) | DIST == ""] <- ifelse(is.finite(DF[is.na(DIST) | DIST == ""]), "t", "normal")
  DIST[DIST %in% c("rectangular", "uniform", "unif")] <- "rect"
  DIST[DIST %in% c("triangular", "triang")] <- "tri"
  DIST[DIST %in% c("gaussian", "norm")] <- "normal"
  DIST[DIST %in% c("arcsin", "u-shaped")] <- "arcsine"
  DIST[DIST %in% c("curvilinear trapezoid", "curvilinear trapezoidal", "ctrap")] <- "ctrap"
  bad <- setdiff(unique(DIST), c("normal", "t", "rect", "tri", "arcsine", "ctrap"))
  if (length(bad)) stop("makeDat: unknown distribution(s) '", paste(bad, collapse = "', '"), 
                        "' (use normal, t, rect, tri, arcsine, ctrap).")
  DDv <- if (is.null(DD)) rep(NA_real_, n) else as.numeric(DD)
  if (any(DIST == "ctrap" & is.na(DDv))) stop("makeDat: distribution 'ctrap' needs the column 'd' (inexactness of the limits).")
  
  ## standard uncertainty: u, or U/k (JCGM 101:2008, 6.4.9.7), or half-width a
  SU <- rep(NA_real_, n)
  if (!is.null(U)) SU <- as.numeric(U)
  if (!is.null(Uexp)) { i <- is.na(SU) & !is.na(Uexp)
    if (any(i & (is.null(K) || any(is.na(K[i]))))) stop("makeDat: expanded uncertainty given without coverage factor 'k'!")
    SU[i] <- as.numeric(Uexp[i])/as.numeric(K[i]) }
  AH <- if (is.null(A)) rep(NA_real_, n) else as.numeric(A)
  fac <- c(rect = sqrt(3), tri = sqrt(6), arcsine = sqrt(2))
  i <- is.na(SU) & !is.na(AH) & DIST %in% names(fac)
  SU[i] <- AH[i]/fac[DIST[i]]
  i <- is.na(SU) & !is.na(AH) & DIST == "ctrap"                  ## JCGM 101:2008, 6.4.3: u^2 = a^2/3 + d^2/9
  SU[i] <- sqrt(AH[i]^2/3 + DDv[i]^2/9)
  i <- DIST %in% names(fac) & is.na(AH) & !is.na(SU)
  AH[i] <- SU[i] * fac[DIST[i]]
  if (any(is.na(MEAN) | is.na(SU))) stop("makeDat: missing value or uncertainty for '", 
                                         paste(NAME[is.na(MEAN) | is.na(SU)], collapse = "', '"), "'!")
  ## v1.3-0: degrees of freedom of Type B inputs are kept if given (reliability of the limits, JCGM 100:2008, G.4.2),
  ## otherwise Inf (set above); they affect only the effective degrees of freedom of the GUF, not the sampling
  
  if (output == "stat") {
    if (any(DIST %in% c(names(fac), "ctrap")))
      message("makeDat: rectangular/triangular/arcsine inputs are represented by mean and u; propagate() ",
              "then samples them from t/Gaussian margins. Use output = \"sim\" to sample the stated distributions.")
    DATA <- rbind(MEAN, SU, DF)
    dimnames(DATA) <- list(NULL, NAME)
  } else {
    DATA <- .sampleBudget(data.frame(name = NAME, mean = MEAN, u = SU, df = DF, dist = DIST, a = AH, d = DDv), nsim)
  }
  if (!is.null(VARS)) {
    miss <- setdiff(VARS, colnames(DATA))
    if (length(miss)) stop("makeDat: variable(s) '", paste(miss, collapse = "', '"), "' of 'expr' not in the budget!")
    DATA <- DATA[, VARS, drop = FALSE]
  }
  attr(DATA, "budget") <- data.frame(name = NAME, mean = MEAN, u = SU, df = DF, dist = DIST, a = AH, d = DDv)
  .datType(DATA, if (output == "stat") "stat" else "sim")
}

## guess and report the 'type' for propagate()
.datType <- function(DATA, type = NULL) {
  if (is.null(type)) type <- if (nrow(DATA) <= 3) "stat" else if (nrow(DATA) < 1000) "raw" else "sim"
  message("makeDat: ", nrow(DATA), " x ", ncol(DATA), " data => use propagate(..., type = \"", type, "\").")
  attr(DATA, "type") <- type
  DATA
}

## v1.3-0: samples from an interpreted uncertainty budget (also used by propagate(..., adaptive = TRUE)
## to draw new blocks for type = "sim")
.sampleBudget <- function(B, nsim)
{
  S <- sapply(seq_len(nrow(B)), function(j) switch(B$dist[j],
    normal  = rnorm(nsim, B$mean[j], B$u[j]),
    t       = B$mean[j] + B$u[j] * rt(nsim, B$df[j]),                ## scale = u (JCGM 101, 6.4.9)
    rect    = runif(nsim, B$mean[j] - B$a[j], B$mean[j] + B$a[j]),
    tri     = rtriang(nsim, B$mean[j] - B$a[j], B$mean[j], B$mean[j] + B$a[j]),
    arcsine = rarcsin(nsim, B$mean[j] - B$a[j], B$mean[j] + B$a[j]),
    ctrap   = rctrap(nsim, B$mean[j] - B$a[j], B$mean[j] + B$a[j], B$d[j])))
  matrix(S, ncol = nrow(B), dimnames = list(NULL, B$name))
}

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.