jranke / chemCal
1
lod <- function(object, ..., alpha = 0.05, beta = 0.05, method = "default", tol = "default")
2
{
3 2
  UseMethod("lod")
4
}
5

6
lod.default <- function(object, ..., alpha = 0.05, beta = 0.05, method = "default", tol = "default")
7
{
8 0
  stop("lod is only implemented for univariate lm objects.")
9
}
10

11
lod.lm <- function(object, ..., alpha = 0.05, beta = 0.05, method = "default", tol = "default")
12
{
13 2
  if (length(object$weights) > 0) {
14 0
    stop(paste(
15 0
      "\nThe detemination of a lod from calibration models obtained by",
16 0
      "weighted linear regression requires confidence intervals for",
17 0
      "predicted y values taking into account weights for the x values",
18 0
      "from which the predictions are to be generated.",
19 0
      "This is not supported by the internally used predict.lm method.",
20 0
      sep = "\n"
21
    ))
22
  }
23 2
  xname <- names(object$model)[[2]]
24 2
  xvalues <- object$model[[2]]
25 2
  yname <- names(object$model)[[1]]
26 2
  newdata <- data.frame(0)
27 2
  names(newdata) <- xname
28 2
  y0 <- predict(object, newdata, interval = "prediction", 
29 2
    level = 1 - 2 * alpha)
30 2
  yc <- y0[[1,"upr"]]
31 2
  if (method == "din") {
32 2
    y0.d <- predict(object, newdata, interval = "prediction",
33 2
      level = 1 - 2 * beta)
34 2
    deltay <- y0.d[[1, "upr"]] - y0.d[[1, "fit"]]
35 2
    lod.y <- yc + deltay 
36 2
    lod.x <- inverse.predict(object, lod.y)$Prediction
37
  } else {
38 2
    f <- function(x) {
39 2
      newdata <- data.frame(x)
40 2
      names(newdata) <- xname
41 2
      pi.y <- predict(object, newdata, interval = "prediction",
42 2
        level = 1 - 2 * beta)
43 2
      yd <- pi.y[[1,"lwr"]]
44 2
      (yd - yc)^2
45
    }
46 2
    if (tol == "default") tol = min(xvalues[xvalues !=0]) / 1000
47 2
    lod.x <- optimize(f, interval = c(0, max(xvalues) * 10), tol = tol)$minimum
48 2
    newdata <- data.frame(x = lod.x)
49 2
    names(newdata) <- xname
50 2
    lod.y <-  predict(object, newdata)
51 2
    names(lod.y) <- NULL
52
  }
53 2
  lod <- list(lod.x, lod.y)
54 2
  names(lod) <- c(xname, yname)
55 2
  return(lod)
56
}

Read our documentation on viewing source code .

Loading