diff options
Diffstat (limited to 'R')
-rw-r--r-- | R/chemCal-package.R | 86 | ||||
-rw-r--r-- | R/linearity.R | 107 |
2 files changed, 187 insertions, 6 deletions
diff --git a/R/chemCal-package.R b/R/chemCal-package.R index 8cc8c76..54ab190 100644 --- a/R/chemCal-package.R +++ b/R/chemCal-package.R @@ -54,7 +54,7 @@ #' # LQ = 3.04 * LC (Currie 1999, p. 120) #' 3.04 * lod(m, alpha = 0.01, beta = 0.5)$x #' -NULL +"din32645" @@ -72,7 +72,7 @@ NULL #' Lewi, P.J., Smeyers-Verbeke, J. (1997) Handbook of Chemometrics and #' Qualimetrics: Part A, Chapter 8. #' @keywords datasets -NULL +"massart97ex1" @@ -128,7 +128,7 @@ NULL #' # of 15, but the graphical procedure of Massart (p. 201) to derive the #' # variances on which the weights are based is quite inaccurate anyway. #' -NULL +"massart97ex3" @@ -147,7 +147,7 @@ NULL #' @source Rocke, David M. und Lorenzato, Stefan (1995) A two-component model #' for measurement error in analytical chemistry. Technometrics 37(2), 176-184. #' @keywords datasets -NULL +"rl95_cadmium" @@ -167,7 +167,7 @@ NULL #' @source Rocke, David M. und Lorenzato, Stefan (1995) A two-component model #' for measurement error in analytical chemistry. Technometrics 37(2), 176-184. #' @keywords datasets -NULL +"rl95_toluene" @@ -188,7 +188,81 @@ NULL #' Toronto. #' \url{https://sites.chem.utoronto.ca/chemistry/coursenotes/analsci/stats/index.html} #' @keywords datasets -NULL +"utstats14" + + +#' Nitrite calibration data +#' +#' Example dataset B.1 from DIN 38402 with concentrations in µg/L and the extinction +#' as response measured using continuous flow analysis (CFA) according to +#' ISO 13395. +#' +#' @name din38402b1 +#' @docType data +#' @format A tibble containing 12 concentration levels with the respective +#' instrument response values. +#' @references DIN 38402-51:2017-05, Beuth Verlag, Berlin. +#' https://dx.doi.org/10.31030/2657448 +#' @keywords datasets +"din38402b1" + + + + + +#' Copper calibration data +#' +#' Example dataset B.3 from DIN 38402. Cu was measured according to ISO 11885, +#' using ICP-OES. The concentration are reported in mg/L and the response as +#' counts/s, describing the count of photons that are detected by the +#' photomultiplier detector of the device. +#' +#' @name din38402b3 +#' @docType data +#' @format A tibble containing 13 concentration levels and the respective +#' instrument response values. +#' @references DIN 38402-51:2017-05, Beuth Verlag, Berlin. +#' https://dx.doi.org/10.31030/2657448 +#' @keywords datasets +"din38402b3" + + + + + +#' Carbamazepin calibration data +#' +#' Example dataset B.6 from DIN 38402 measured using LC-MS/MS. The +#' concentrations are reported in in µg/L and the response in arbitrary +#' units (AU). +#' +#' @name din38402b6 +#' @docType data +#' @format A tibble containing 12 concentration levels and the respective +#' instrument response values. +#' @references DIN 38402-51:2017-05, Beuth Verlag, Berlin. +#' https://dx.doi.org/10.31030/2657448 +#' @keywords datasets +"din38402b6" + + + + + +#' Iron calibration data +#' +#' Example dataset C.3 from DIN 38402 determined by ion chromatography. +#' Concentrations are reported in mg/L and the extinction as response. +#' +#' @name din38402c3 +#' @docType data +#' @format A tibble containing 10 concentration levels and the respective +#' response values. +#' @references DIN 38402-51:2017-05, Beuth Verlag, Berlin. +#' https://dx.doi.org/10.31030/2657448 +#' @keywords datasets +"din38402c3" + diff --git a/R/linearity.R b/R/linearity.R new file mode 100644 index 0000000..8a4f09b --- /dev/null +++ b/R/linearity.R @@ -0,0 +1,107 @@ +#' Assess the linearity of a calibration curve +#' +#' A function to create diagnostic plots for the assessment of the linearity of +#' calibration data based on their point-to-point slope or the curvature. +#' The underlying methods follow ISO 84 66-1:2021 and DIN 32 402-51:2017 +#' (German Industrial Norm). +#' +#' The point-to-point slope method is based on the assumption that the slope +#' between two points should not vary greatly within the linear range. +#' +#' The curvature method is similar to the point-to-point slope method. Here, +#' the ratio between the instrument signal and the concentration of the +#' calibration standard is assumed not to vary greatly within the linear range. +#' +#' The use of the Mandel test is discouraged due to its limitations in the +#' identification of non-linear behaviour of calibration curves (Andrade and +#' Gomes-Carracedo, 2013). +#' +#' @param x numeric vector of independent values (usually concentrations). +#' @param y numeric vector of dependent values (usually the signal of the +#' analytical device). +#' @param method character string. Supported methods are "slope" and +#' "curvature". +#' @param tolerance numeric value between 0 and 1, describing the acceptable +#' deviation from the median of the slopes or the signal-to-concentration +#' ratio. The default tolerance is 10%. +#' @return returns a diagnostic plot +#' +#' @author Anıl Axel Tellbüscher +#' +#' @importFrom graphics abline +#' @importFrom graphics lines +#' @importFrom stats median +#' +#' @examples +#' # Continuous Flow Analysis (CFA) data +#' data(din38402b1) +#' +#' # Point-to-point slope plot +#' linearity(din38402b1$conc, din38402b1$ext, method = "slope") +#' +#' # Curvature plot +#' linearity(din38402b1$conc, din38402b1$ext, method = "curvature") +#' +#' @references ISO 8466-1:2021. Water quality — Calibration and evaluation of +#' analytical methods — Part 1: Linear calibration function +#' +#' J. M. Andrade and M. P. Gomez-Carracedo (2013) Notes on the use of +#' Mandel's test to check for nonlinearity in laboratory calibrations. +#' Analytical Methods 5(5), 1145 - 1149. +#' +#' @export +# Function to assess linearity of data using either slope or curvature method +linearity <- function(x, y, method = c("slope", "curvature"), tolerance = 0.1) { + + # Check data integrity + # Ensure that x and y vectors have the same length + stopifnot("x and y must have the same length!" = length(x) == length(y)) + + method <- match.arg(method) + + # Calculate the 'result' based on the chosen method + if (method == "slope") { + # For the 'slope' method, calculate the difference between consecutive points + x_diff = diff(x) # Difference in x values + y_diff = diff(y) # Difference in y values + result = y_diff / x_diff # Point-to-point slope (rate of change) + } else if (method == "curvature") { + # For the 'curvature' method, calculate the signal-to-concentration ratio + result = y / x # Element-wise division of y by x + } + + # Calculate the median of the results for tolerance check + result_median <- median(result) + + # Define upper and lower tolerance boundaries + upper_tolerance <- result_median + tolerance * result_median + lower_tolerance <- result_median - tolerance * result_median + + # Create a data frame to store the result and corresponding indices + df <- data.frame(result = result, index = 1:length(result)) + + # Identify points that fall outside the tolerance range + outside_tolerance <- rbind( + subset(df, result > upper_tolerance), # Points above the upper tolerance + subset(df, result < lower_tolerance) # Points below the lower tolerance + ) + + # Basic scatter plot of the result against the index + plot(result ~ index, data = df, + main = "linearity assessment", ylab = method, + pch = 16) + + # Draw a line connecting all the points to visualize the trend + lines(df$index, df$result, col = "blue") # Blue line connecting points + + # Highlight points that are outside the tolerance range in red + points(x = outside_tolerance$index, y = outside_tolerance$result, + pch = 16, col = "red") + + # Add a horizontal line at the median value of the result + abline(h = result_median, col = "red") + + # Add dashed horizontal lines at the upper and lower tolerance limits + abline(h = upper_tolerance, col = "red", lty = 3) # Upper tolerance + abline(h = lower_tolerance, col = "red", lty = 3) # Lower tolerance +} |