#' findLogFunction
#'
#' Calculates the log function interpolating cathode resistance data
#' resistance = a + b * log(age)
#'
#' @param d datatable containing the data
#' @param age numeric pot age in days
#' @param res numeric cathode resistance in microhm
#' @param coeff logical if FALSE (default) return function, if TRUE returns vector c(a, b)
#'
#' @return function or vector
#' @export
#'
#' @examples
#' data("resCathData")
#' d <- resCathData
#' findLogFunction(d, agebsq, rucv)
#'
#'
findLogFunction <- function(d, age, res, coeff = F) {
# test of no 0 value for age
minAge <- dplyr::pull(dplyr::summarise(d, min({{age}})))
stopifnot(minAge > 0)
# Formula a + b log(x)
form <- paste0(names(dplyr::select(d, {{res}})), " ~ log(", names(dplyr::select(d, {{age}})), ")")
modlm <- stats::lm(formula = form, d)
a <- summary(modlm)$coefficients[[1]]
b <- summary(modlm)$coefficients[[2]]
logFun <- function(x, a1 = a, b1 = b){
a1 + b1 * log(x)
}
if (!coeff) return(logFun) else return(c(a, b))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.