# NOT RUN {
## self-starting logistic model
## The "initializer" (finds initial values for parameters from data):
initLogis <- function(mCall, data, LHS) {
xy <- data.frame(sortedXyData(mCall[["input"]], LHS, data))
if(nrow(xy) < 4)
stop("too few distinct input values to fit a logistic model")
z <- xy[["y"]]
## transform to proportion, i.e. in (0,1) :
rng <- range(z); dz <- diff(rng)
z <- (z - rng[1L] + 0.05 * dz)/(1.1 * dz)
xy[["z"]] <- log(z/(1 - z)) # logit transformation
aux <- coef(lm(x ~ z, xy))
pars <- coef(nls(y ~ 1/(1 + exp((xmid - x)/scal)),
data = xy,
start = list(xmid = aux[[1L]], scal = aux[[2L]]),
algorithm = "plinear"))
setNames(pars[c(".lin", "xmid", "scal")], nm = mCall[c("Asym", "xmid", "scal")])
}
SSlogis <- selfStart(~ Asym/(1 + exp((xmid - x)/scal)),
initial = initLogis,
parameters = c("Asym", "xmid", "scal"))
# 'first.order.log.model' is a function object defining a first order
# compartment model
# 'first.order.log.initial' is a function object which calculates initial
# values for the parameters in 'first.order.log.model'
#
# self-starting first order compartment model
# }
# NOT RUN {
SSfol <- selfStart(first.order.log.model, first.order.log.initial)
# }
# NOT RUN {
## Explore the self-starting models already available in R's "stats":
pos.st <- which("package:stats" == search())
mSS <- apropos("^SS..", where = TRUE, ignore.case = FALSE)
(mSS <- unname(mSS[names(mSS) == pos.st]))
fSS <- sapply(mSS, get, pos = pos.st, mode = "function")
all(sapply(fSS, inherits, "selfStart")) # -> TRUE
## Show the argument list of each self-starting function:
str(fSS, give.attr = FALSE)
# }
Run the code above in your browser using DataLab