Usage
P(
trafo = NULL,
parameters = NULL,
condition = NULL,
compile = FALSE,
modelname = NULL,
method = NULL,
cores = NULL,
verbose = FALSE,
derivMode = "forward",
...
)Arguments
- trafo
An eqnvec, named character or eqnlist, or a list of those named by condition, which builds one transformation per condition.
NULL(default) returnsNULL. Expressions follow cppDE::expressions.- parameters
Outer parameter names. Default
NULL, the symbols oftrafo.- condition
Condition label of a single
trafo, defaultNULL; a list takes the list names instead.- compile
Logical.
TRUEcompiles all conditions into one shared object namedmodelname, or withmodelname = NULLinto one shared object per condition;FALSE(default) leaves it tocompile().- modelname
Base name of the generated files, default
NULL, which is"expl_parfn"or"impl_parfn", followed by_<condition>.- method
"explicit","implicit", orNULL(default) for"implicit"iftrafois an eqnlist and"explicit"otherwise.- cores
Number of parallel jobs generating and compiling the sources of a list; on Windows the sources are generated one after the other.
NULL(default) usesdetectFreeCores().- verbose
Logical. Print the compiler output, default
FALSE.- derivMode
The derivatives to build, one or more of
"forward"(default),"reverse","forward-forward"and"forward-reverse", or"none", passed toPexpl()orPimpl().- ...
Further arguments of
Pexpl()(attachInput,outdir) orPimpl()(forcings,keepRoot,flow,controlsPTC,outdir), asmethodselects.
Value
A parfn, called as p(pars, ...), see section Calling a dMod
function. The random effects of a transformation built by insert()
stay attached, so randomEffects() reads them from the result.
Calling a dMod function
The leading arguments depend on the class and are matched by position or by name:
prediction function:
x(times, pars, ...), returning a prdlist;observation function:
g(out, pars, ...)with a prdlistout, returning a prdlist;parameter transformation:
p(pars, ...), returning a list with one parvec per condition;objective function:
obj(pars, ...), returning an objlist.
pars is a named numeric vector. If it has a deriv attribute, as the
output of a parameter transformation does, the derivatives are taken with
respect to the parameters behind it. A composition is called according to
the resulting class:
g * x * p is a prediction function, (g * x * p)(times, pars).
The other arguments are named:
fixed: named numeric vector of parameters that enter the evaluation without derivatives. DefaultNULL.deriv:TRUE(default) returns first derivatives with respect topars. Every function of the chain has to be built withderivMode = "forward", seeodemodel(),Y()andP().deriv2:TRUEreturns second derivatives as well. DefaultFALSE. Every function of the chain has to be built withderivMode = "forward-forward".hessian: objective functions only, see the table below. DefaultNULL.sweep: objective functions only."forward"(default) propagates the sensitivities through the chain;"reverse"computes the gradient in one backward pass, whose cost does not grow with the number of parameters."reverse"needs every function of the chain built withderivMode = "reverse", and with"forward-reverse"forderiv2 = TRUE.conditions: character vector, the conditions to evaluate. Default all conditions of the function.env: environment for intermediate results. DefaultNULL, a new one. An objective function stores the prediction there aspredictionand returns the environment as attributeenv.cores: number of threads for the ODE solves and the residual kernel. DefaultgetOption("dMod2.cores", 1L).
Prediction, observation and parameter functions accept hessian and
sweep and ignore them. With deriv = TRUE an objective function returns
this Hessian:
sweep | hessian | deriv2 = FALSE | deriv2 = TRUE |
"forward" | NULL, TRUE | Gauss-Newton | exact |
"forward" | FALSE | none | none, with a warning |
"reverse" | NULL | none | exact |
"reverse" | TRUE | none, with a warning | exact |
"reverse" | FALSE | none | none, with a warning |
hessian = "sparse" returns the Gauss-Newton Hessian of the forward sweep
as a sparse Matrix, assembled from the blocks of the conditions; trust()
factorizes it where the argument sparse takes the sparse path, and vcov()
solves it for the columns of which.
hessian = "expected" returns, in place of the Gauss-Newton Hessian of the
forward sweep, the expected (Fisher) information of the likelihood:
2 / sigma^2 in the prediction and 4 / sigma^2 in sigma per data point,
positive semidefinite also where an error model depends on the parameters.
The Gauss-Newton Hessian keeps the exact curvature of the loss in the
prediction and sigma, which is indefinite there and shows the optimizer a
saddle.
With deriv = FALSE it returns the value alone.
Examples
# \donttest{
## One transformation per condition, all compiled into one shared object
trafo <- list(
ctrl = eqnvec(k1 = "exp(log_k1)", k2 = "exp(log_k2)", A = "A0"),
treat = eqnvec(k1 = "exp(log_k1 + log_fold)", k2 = "exp(log_k2)", A = "A0"))
p <- P(trafo, compile = TRUE, modelname = "P_example", outdir = tempdir())
#> using C++ compiler: g++ [-O2 -DNDEBUG -w -fPIC]
getParameters(p)
#> [1] "log_k1" "log_k2" "A0" "log_fold"
p(c(log_k1 = 0, log_k2 = -1, A0 = 2, log_fold = log(3)))
#> $ctrl
#> Parameter vector:
#> k1 : 1
#> k2 : 0.367879
#> A : 2
#>
#> Attributes:
#> deriv : 3 x 3 matrix
#> deriv2 : <none>
#> fixed : <none>
#>
#> $treat
#> Parameter vector:
#> k1 : 3
#> k2 : 0.367879
#> A : 2
#>
#> Attributes:
#> deriv : 3 x 4 matrix
#> deriv2 : <none>
#> fixed : <none>
#>
# }