| Line 1... |
Line 1... |
| 1 |
% File src/library/stats/man/selfStart.Rd
|
1 |
% File src/library/stats/man/selfStart.Rd
|
| 2 |
% Part of the R package, https://www.R-project.org
|
2 |
% Part of the R package, https://www.R-project.org
|
| 3 |
% Copyright 1995-2013 R Core Team
|
3 |
% Copyright 1995-2017 R Core Team
|
| 4 |
% Distributed under GPL 2 or later
|
4 |
% Distributed under GPL 2 or later
|
| 5 |
|
5 |
|
| 6 |
\name{selfStart}
|
6 |
\name{selfStart}
|
| - |
|
7 |
\title{Construct Self-starting Nonlinear Models}
|
| 7 |
\encoding{UTF-8}
|
8 |
\encoding{UTF-8}
|
| 8 |
\alias{selfStart}
|
9 |
\alias{selfStart}
|
| 9 |
\alias{selfStart.default}
|
10 |
\alias{selfStart.default}
|
| 10 |
\alias{selfStart.formula}
|
11 |
\alias{selfStart.formula}
|
| 11 |
\title{Construct Self-starting Nonlinear Models}
|
- |
|
| 12 |
\description{
|
12 |
\description{
|
| 13 |
Construct self-starting nonlinear models.
|
13 |
Construct self-starting nonlinear models to be used in
|
| - |
|
14 |
\code{\link{nls}}, etc. Via function \code{initial} to compute
|
| - |
|
15 |
approximate parameter values from data, such models are
|
| - |
|
16 |
\dQuote{self-starting}, i.e., do not need a \code{start} argument in,
|
| - |
|
17 |
e.g., \code{\link{nls}()}.
|
| 14 |
}
|
18 |
}
|
| 15 |
\usage{
|
19 |
\usage{
|
| 16 |
selfStart(model, initial, parameters, template)
|
20 |
selfStart(model, initial, parameters, template)
|
| 17 |
}
|
21 |
}
|
| 18 |
\arguments{
|
22 |
\arguments{
|
| 19 |
\item{model}{a function object defining a nonlinear model or
|
23 |
\item{model}{a function object defining a nonlinear model or
|
| 20 |
a nonlinear formula object of the form \code{~expression}.}
|
24 |
a nonlinear \code{\link{formula}} object of the form \code{~ expression}.}
|
| 21 |
\item{initial}{a function object, taking three arguments: \code{mCall},
|
25 |
\item{initial}{a function object, taking three arguments: \code{mCall},
|
| 22 |
\code{data}, and \code{LHS}, representing, respectively, a matched
|
26 |
\code{data}, and \code{LHS}, representing, respectively, a matched
|
| 23 |
call to the function \code{model}, a data frame in
|
27 |
call to the function \code{model}, a data frame in
|
| 24 |
which to interpret the variables in \code{mCall}, and the expression
|
28 |
which to interpret the variables in \code{mCall}, and the expression
|
| 25 |
from the left-hand side of the model formula in the call to \code{nls}.
|
29 |
from the left-hand side of the model formula in the call to \code{nls}.
|
| Line 34... |
Line 38... |
| 34 |
\code{deriv} function. By default, a template is generated with the
|
38 |
\code{deriv} function. By default, a template is generated with the
|
| 35 |
covariates in \code{model} coming first and the parameters in
|
39 |
covariates in \code{model} coming first and the parameters in
|
| 36 |
\code{model} coming last in the calling sequence.}
|
40 |
\code{model} coming last in the calling sequence.}
|
| 37 |
}
|
41 |
}
|
| 38 |
\details{
|
42 |
\details{
|
| - |
|
43 |
\code{\link{nls}()} calls \code{\link{getInitial}} and the
|
| - |
|
44 |
\code{initial} function for these self-starting models.
|
| - |
|
45 |
|
| 39 |
This function is generic; methods functions can be written to handle
|
46 |
This function is generic; methods functions can be written to handle
|
| 40 |
specific classes of objects.
|
47 |
specific classes of objects.
|
| 41 |
}
|
48 |
}
|
| 42 |
\value{
|
49 |
\value{
|
| 43 |
a function object of class \code{"selfStart"}, for the \code{formula}
|
50 |
a \code{\link{function}} object of class \code{"selfStart"}, for the
|
| 44 |
method obtained by applying
|
51 |
\code{formula} method obtained by applying \code{\link{deriv}}
|
| 45 |
\code{deriv} to the right hand side of the \code{model} formula. An
|
52 |
to the right hand side of the \code{model} formula. An
|
| 46 |
\code{initial} attribute (defined by the \code{initial} argument) is
|
53 |
\code{initial} attribute (defined by the \code{initial} argument) is
|
| 47 |
added to the function to calculate starting estimates for the
|
54 |
added to the function to calculate starting estimates for the
|
| 48 |
parameters in the model automatically.
|
55 |
parameters in the model automatically.
|
| 49 |
}
|
56 |
}
|
| 50 |
\author{\enc{José}{Jose} Pinheiro and Douglas Bates}
|
57 |
\author{\enc{José}{Jose} Pinheiro and Douglas Bates}
|
| 51 |
|
58 |
|
| 52 |
\seealso{
|
59 |
\seealso{
|
| 53 |
\code{\link{nls}}, \code{\link{getInitial}}.
|
60 |
\code{\link{nls}}, \code{\link{getInitial}}.
|
| - |
|
61 |
|
| 54 |
Each of the following are \code{"selfStart"} models (with examples)
|
62 |
Each of the following are \code{"selfStart"} models (with examples)
|
| 55 |
\code{\link{SSasymp}}, \code{\link{SSasympOff}}, \code{\link{SSasympOrig}},
|
63 |
\code{\link{SSasymp}}, \code{\link{SSasympOff}}, \code{\link{SSasympOrig}},
|
| 56 |
\code{\link{SSbiexp}}, \code{\link{SSfol}}, \code{\link{SSfpl}},
|
64 |
\code{\link{SSbiexp}}, \code{\link{SSfol}}, \code{\link{SSfpl}},
|
| 57 |
\code{\link{SSgompertz}}, \code{\link{SSlogis}}, \code{\link{SSmicmen}},
|
65 |
\code{\link{SSgompertz}}, \code{\link{SSlogis}}, \code{\link{SSmicmen}},
|
| 58 |
\code{\link{SSweibull}}
|
66 |
\code{\link{SSweibull}}.
|
| 59 |
|
67 |
|
| - |
|
68 |
Further, package \CRANpkg{nlme}'s \code{\link[nlme]{nlsList}}.
|
| 60 |
}
|
69 |
}
|
| 61 |
\examples{
|
70 |
\examples{
|
| 62 |
## self-starting logistic model
|
71 |
## self-starting logistic model
|
| 63 |
|
72 |
|
| 64 |
SSlogis <- selfStart(~ Asym/(1 + exp((xmid - x)/scal)),
|
73 |
## The "initializer" (finds initial values for parameters from data):
|
| 65 |
function(mCall, data, LHS)
|
74 |
initLogis <- function(mCall, data, LHS) {
|
| 66 |
{
|
- |
|
| 67 |
xy <- sortedXyData(mCall[["x"]], LHS, data)
|
75 |
xy <- data.frame(sortedXyData(mCall[["input"]], LHS, data))
|
| 68 |
if(nrow(xy) < 4) {
|
76 |
if(nrow(xy) < 4)
|
| 69 |
stop("Too few distinct x values to fit a logistic")
|
77 |
stop("too few distinct input values to fit a logistic model")
|
| 70 |
}
|
- |
|
| 71 |
z <- xy[["y"]]
|
78 |
z <- xy[["y"]]
|
| 72 |
if (min(z) <= 0) { z <- z + 0.05 * max(z) } # avoid zeroes
|
79 |
## transform to proportion, i.e. in (0,1) :
|
| - |
|
80 |
rng <- range(z); dz <- diff(rng)
|
| 73 |
z <- z/(1.05 * max(z)) # scale to within unit height
|
81 |
z <- (z - rng[1L] + 0.05 * dz)/(1.1 * dz)
|
| 74 |
xy[["z"]] <- log(z/(1 - z)) # logit transformation
|
82 |
xy[["z"]] <- log(z/(1 - z)) # logit transformation
|
| 75 |
aux <- coef(lm(x ~ z, xy))
|
83 |
aux <- coef(lm(x ~ z, xy))
|
| 76 |
parameters(xy) <- list(xmid = aux[1], scal = aux[2])
|
84 |
pars <- coef(nls(y ~ 1/(1 + exp((xmid - x)/scal)),
|
| - |
|
85 |
data = xy,
|
| 77 |
pars <- as.vector(coef(nls(y ~ 1/(1 + exp((xmid - x)/scal)),
|
86 |
start = list(xmid = aux[[1L]], scal = aux[[2L]]),
|
| 78 |
data = xy, algorithm = "plinear")))
|
87 |
algorithm = "plinear"))
|
| - |
|
88 |
setNames(pars[c(".lin", "xmid", "scal")], nm = mCall[c("Asym", "xmid", "scal")])
|
| - |
|
89 |
}
|
| - |
|
90 |
|
| 79 |
setNames(c(pars[3], pars[1], pars[2]),
|
91 |
SSlogis <- selfStart(~ Asym/(1 + exp((xmid - x)/scal)),
|
| 80 |
mCall[c("Asym", "xmid", "scal")])
|
92 |
initial = initLogis,
|
| 81 |
}, c("Asym", "xmid", "scal"))
|
93 |
parameters = c("Asym", "xmid", "scal"))
|
| - |
|
94 |
|
| 82 |
|
95 |
|
| 83 |
# 'first.order.log.model' is a function object defining a first order
|
96 |
# 'first.order.log.model' is a function object defining a first order
|
| 84 |
# compartment model
|
97 |
# compartment model
|
| 85 |
# 'first.order.log.initial' is a function object which calculates initial
|
98 |
# 'first.order.log.initial' is a function object which calculates initial
|
| 86 |
# values for the parameters in 'first.order.log.model'
|
99 |
# values for the parameters in 'first.order.log.model'
|
| 87 |
|
100 |
#
|
| 88 |
# self-starting first order compartment model
|
101 |
# self-starting first order compartment model
|
| 89 |
\dontrun{
|
102 |
\dontrun{
|
| 90 |
SSfol <- selfStart(first.order.log.model, first.order.log.initial)
|
103 |
SSfol <- selfStart(first.order.log.model, first.order.log.initial)
|
| 91 |
}
|
104 |
}
|
| 92 |
|
105 |
|