Nothing
#' Exponential 3 Model Fit
#'
#' Function that fits to \eqn{f(x) = a*(e^{(x/b)^p} - 1)} and returns generic model
#' outputs.
#'
#' Zero background and increasing absolute response are assumed. Parameters are
#' "a" (y scale), "b" (x scale), "p" (power), and error term "er".
#' success = 1 for a successful fit, 0 if optimization failed, and NA if
#' nofit = TRUE. cov = 1 for a successful hessian inversion, 0 if it fails, and NA
#' if nofit = TRUE. aic, rme, modl, parameters, and parameter sds are set to
#' NA in case of nofit or failure.
#'
#' @param conc Vector of concentration values NOT in log units.
#' @param resp Vector of corresponding responses.
#' @param bidirectional If TRUE, model can be positive or negative; if FALSE, it
#' will be positive only.
#' @param verbose If TRUE, gives optimization and hessian inversion details.
#' @param nofit If nofit = TRUE, returns formatted output filled with missing values.
#' @param dmin Minimum allowed value of p.
#' @param errfun Which error distribution to assume for each point, defaults to
#' "dt4". "dt4" is the original 4 degrees of freedom t-distribution. Another
#' supported distribution is "dnorm", the normal distribution.
#'
#' @importFrom methods is
#' @importFrom numDeriv hessian
#' @importFrom stats constrOptim median
#'
#' @return Named list containing: success, aic (Akaike Information Criteria),
#' cov (success of covariance calculation), rme (root mean square error),
#' modl (vector of model values at given concentrations),
#' parameters values, parameter sd (standard deviation) estimates, pars
#' (vector of parameter names), sds (vector of parameter sd names).
#' @export
#'
#' @examples
#' fitexp3(c(.03,.1,.3,1,3,10,30,100), c(0,0,.1, .2, .4, 1, 4, 50))
fitexp3 = function(conc, resp, bidirectional = TRUE, verbose = FALSE, nofit = FALSE, dmin = .3, errfun = "dt4"){
fenv <- environment()
#initialize myparams
pars <- paste0(c("a", "b", "p", "er"))
sds <- paste0(c("a", "b", "p","er"), "_sd")
myparams = c("success", "aic", "cov", "rme", "modl", pars, sds, "pars", "sds")
#returns myparams with appropriate NAs
if(nofit){
out = as.list(rep(NA_real_, length(myparams)))
names(out) = myparams
out[["success"]] = out[["cov"]] = NA_integer_
out[["pars"]] = pars
out[["sds"]] = sds
return(out)
}
#median at each conc, for multi-valued responses
rmds <- tapply(resp, conc, median)
#get max response and corresponding conc
if(!bidirectional) mmed = rmds[which.max(rmds)] else mmed = rmds[which.max(abs(rmds))] #shortened this code
mmed_conc <- as.numeric(names(mmed)) #fixed this bug
resp_max <- max(resp)
resp_min <- min(resp)
conc_min <- min(conc)
conc_max <- max(conc)
er_est <- if ((rmad <- mad(resp)) > 0) log(rmad) else log(1e-16)
###--------------------- Fit the Model ----------------------###
## Starting parameters for the Model
a0 = mmed #use largest response with desired directionality
if(a0 == 0) a0 = .01 #if 0, use a smallish number
g <- c(a0, # y scale (a)
conc_max, # x scale (b); curve scaled to highest resp and max conc
1.2, # power(p)
er_est )# logSigma (er)
## Generate the bound matrices to constrain the model.
# a b p er
Ui <- matrix(c( 1, 0, 0, 0,
-1, 0, 0, 0,
0, 1, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, -1, 0),
byrow = TRUE, nrow = 6, ncol = 4)
if(!bidirectional){
bnds <- c(1e-8*abs(a0), -1e8*abs(a0), # a bounds
1e-2*conc_max, -1e8*conc_max, # b bounds (lower bound avoids overflow at max conc, max power)
dmin, -8) # p bounds (p > 1, following bmd guidelines)
} else {
bnds <- c(-1e8*abs(a0), -1e8*abs(a0), # a bounds
1e-2*conc_max, -1e8*conc_max, # b bounds (lower bound avoids overflow at max conc, max power)
dmin, -8) # p bounds (p > 1, following bmd guidelines)
}
Ci <- matrix(bnds, nrow = 6, ncol = 1)
## Optimize the model
fit <- try(constrOptim(g,
tcplObj,
ui = Ui,
ci = Ci,
mu = 1e-6,
method = "Nelder-Mead",
control = list(fnscale = -1,
reltol = 1e-10,
maxit = 6000),
conc = conc,
resp = resp,
fname = "exp3",
errfun = errfun),
silent = !verbose)
## Generate some summary statistics
if (!is(fit, "try-error")) { # The model fit the data
if(verbose) cat("Exp3 >>>",fit$counts[1],fit$convergence,"\n")
success <- 1L
aic <- 2*length(fit$par) - 2*fit$value # 2*length(fit$par) - 2*fit$value
mapply(assign,
c(pars),
fit$par,
MoreArgs = list(envir = fenv))
## Calculate rmse for gnls
modl <- exp3(fit$par, conc)
rme <- sqrt(mean((modl - resp)^2, na.rm = TRUE))
## Calculate the sd for the gnls parameters
fit$cov <- try(solve(-hessian(tcplObj,
fit$par,
conc = conc,
resp = resp,
fname = "exp3",
errfun = errfun)),
silent = !verbose)
if (!is(fit$cov, "try-error")) { # Could invert gnls Hessian
cov <- 1L
diag_sqrt <- suppressWarnings(sqrt(diag(fit$cov)))
if (any(is.nan(diag_sqrt))) {
mapply(assign,
sds,
NaN,
MoreArgs = list(envir = fenv))
} else {
mapply(assign,
sds,
diag_sqrt,
MoreArgs = list(envir = fenv))
}
} else { # Could not invert gnls Hessian
cov <- 0L
mapply(assign,
c(sds),
NA_real_,
MoreArgs = list(envir = fenv))
}
} else { # Curve did not fit the data
success <- 0L
aic <- NA_real_
cov <- NA_integer_
rme <- NA_real_
modl = NA_real_
mapply(assign,
c(pars, sds),
NA_real_,
MoreArgs = list(envir = fenv))
}
return(mget(myparams))
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.