Title: | Estimation in Reducible Stochastic Differential Equations |
---|---|
Description: | Maximum likelihood estimation for univariate reducible stochastic differential equation models. Discrete, possibly noisy observations, not necessarily evenly spaced in time. Can fit multiple individuals/units with global and local parameters, by fixed-effects or mixed-effects methods. Ref.: Garcia, O. (2019) "Estimating reducible stochastic differential equations by conversion to a least-squares problem", Computational Statistics 34(1): 23-46, <doi:10.1007/s00180-018-0837-4>. |
Authors: | Oscar Garcia [aut, cre] |
Maintainer: | Oscar Garcia <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.1 |
Built: | 2025-02-08 04:37:26 UTC |
Source: | https://github.com/ogarciav/resde |
The main functions for model fitting are sdemodel()
and sdefit()
. First, specify the model structure in
sdemodel()
, including the variable transformation, any
re-parameterizations, initial condition, and the presence or not of process,
measurement, and initial condition noise. Then, fit the model with
sdefit()
, indicating the data to be used and starting parameter
values for the iterations. For hierarchical models, one must also
indicate which are the global and local parameters, and if fixed
locals or a mixed effects method should be used.
Some auxilliary functions include the Box-Cox transformation bc()
,
and the unified transformation unitran()
.
For detailed usage see the vignette: vignette("resde-vignette", package="resde")
.
Maintainer: Oscar Garcia [email protected] (ORCID)
Garcia, O. (2019) "Estimating reducible stochastic differential equations by conversion to a least-squares problem". Computational Statistics 34(1), 23-46. doi:10.1007/s00180-018-0837-4
Useful links:
# Richards model dH^c = b(a^c - H^c) dt + s dW for tree heights tree1 <- subset(Loblolly, Seed == Seed[1]) # first tree m <- sdemodel(~x^c, beta0=~b*a^c, beta1=~-b, mum=0) # no measurement error sdefit(m, x="height", t="age", data=tree1, start=c(a=70, b=0.1, c=0.5))
# Richards model dH^c = b(a^c - H^c) dt + s dW for tree heights tree1 <- subset(Loblolly, Seed == Seed[1]) # first tree m <- sdemodel(~x^c, beta0=~b*a^c, beta1=~-b, mum=0) # no measurement error sdefit(m, x="height", t="age", data=tree1, start=c(a=70, b=0.1, c=0.5))
These functions calculate the Box-Cox transformation, its inverse, and derivative.
bc(x, lambda) bc_inv(y, lambda) bc_prime(y, lambda)
bc(x, lambda) bc_inv(y, lambda) bc_prime(y, lambda)
x , y
|
Numeric vector ( |
lambda |
Numeric scalar, power parameter. |
bc()
uses expm1()
, wich is more accurate
for small lambda
than a more "obvious" alternative like
if (abs(lambda) < 6e-9) log(y) else (y^lambda - 1) / lambda
The difference might be important
in optimization applications. See example below. Similarly,
bc_inv()
uses log1p()
.
bc()
: Returns the transform value(s).
bc_inv()
: Computes the inverse of bc()
.
bc_prime()
: Gives the derivative of bc()
with respect to y
.
bc()
: The Box-Cox transformation
bc_inv()
: Inverse of the Box-Cox transformation
bc_prime()
: Derivative of the Box-Cox transformation
bc(0.5, 1.5) bc(1, 0) obvious <- function(lambda){(0.6^lambda - 1) / lambda} # at y = 0.6 plot(obvious, xlab="lambda", xlim=c(1e-6, 1e-9), log="x") bc_inv(-0.4, 1.5) bc_inv(0, 0) bc_prime(0.5, 1.5) bc_prime(1, 0)
bc(0.5, 1.5) bc(1, 0) obvious <- function(lambda){(0.6^lambda - 1) / lambda} # at y = 0.6 plot(obvious, xlab="lambda", xlim=c(1e-6, 1e-9), log="x") bc_inv(-0.4, 1.5) bc_inv(0, 0) bc_prime(0.5, 1.5) bc_prime(1, 0)
ML estimation of parameters for a reducible SDE
sdefit(model, x, t, unit=NULL, data=NULL, start=NULL, global=NULL, local=NULL, known=NULL, method="nls", control=NULL, phi=NULL, phiprime=NULL)
sdefit(model, x, t, unit=NULL, data=NULL, start=NULL, global=NULL, local=NULL, known=NULL, method="nls", control=NULL, phi=NULL, phiprime=NULL)
model |
Model specification, as produced by |
x , t
|
Vectors with variables, or names of columns in data frame. |
unit |
If applicable, unit id vector, or name of its column in data frame. |
data |
Data frame, if data not given directly in |
start |
Named vector or named list with starting parameter values for non-hierarchical models. They can also be given in global. |
global |
Named vector or list of global parameters and their starting values for hierarchical models. Can also contain starting values for non-hierarchical models. |
local |
Named vector or list of local parameters and their starting values for hierarchical models. The value can be a vector with values for each unit, or a single scalar that applies to all the units. |
known |
Named vector or list with any parameters that should be fixed at given values. |
method |
|
control |
Optional control list for |
phi |
Optional transformation function. If |
phiprime |
Optional derivative function. If |
List with two components: a list fit
containing the output from
the optimizer (nls
or nlme
), and a list more
containing
sigma estimates, log-likelihood, AIC and BIC. Note that in fit
, "residual sum-of-squares"
corresponds to uvector
, not to x
or y
. Same for nls
and nlme
methods like fitted
or residuals
applied to fit
.
m <- sdemodel(phi=~x^c, beta0=~b*a^c, beta1=~-b) mod1 <- sdefit(m, "height", "age", data=Loblolly[Loblolly$Seed=="301",], start=c(a=70, b=0.1, c=1)) mod2 <- sdefit(m, "height", "age", "Seed", Loblolly, global=c(b=0.1, c=0.5), local=c(a=72))
m <- sdemodel(phi=~x^c, beta0=~b*a^c, beta1=~-b) mod1 <- sdefit(m, "height", "age", data=Loblolly[Loblolly$Seed=="301",], start=c(a=70, b=0.1, c=1)) mod2 <- sdefit(m, "height", "age", "Seed", Loblolly, global=c(b=0.1, c=0.5), local=c(a=72))
Specify transformation and re-parametrizations for reducible SDE model.
sdemodel(phi=~x, phiprime=NULL, beta0=~beta0, beta1=~beta1, t0=0, x0=0, mu0=0, mup=1, mum=1)
sdemodel(phi=~x, phiprime=NULL, beta0=~beta0, beta1=~beta1, t0=0, x0=0, mu0=0, mup=1, mum=1)
phi |
Transformation formula |
phiprime |
Optional formula for derivative of |
beta0 , beta1
|
Optional formulas or constants, possibly giving a re-parameterization,. |
t0 , x0
|
Formulas or constants for the initial condition. |
mu0 |
Formula or constant for the initial condition |
mup , mum
|
Formulas or constants for the process and measurement |
List with model specification, to be used by sdefit()
.
richards <- sdemodel(phi=~x^c, beta0=~b*a^c, beta1=~-b, mum=0)
richards <- sdemodel(phi=~x^c, beta0=~b*a^c, beta1=~-b, mum=0)
Display the model specification
sdemodel_display(model)
sdemodel_display(model)
model |
SDE model specification, as produced by sdemodel() |
Invisibly returns its argument
mod <- sdemodel(); sdemodel_display(mod)
mod <- sdemodel(); sdemodel_display(mod)
Normally not called by the user directly, used by sdefit()
.
Converts an expression, in a character string, to a function.
str2fun_theta(s)
str2fun_theta(s)
s |
String representation of a function of |
Function of x
and theta
, theta
being a named vector or list of parameters.
str2fun_theta("x^c / a")
str2fun_theta("x^c / a")
Calculates a variable transformation that produces various growth curve models, depending on the
values of two shape parameters, alpha
and beta
. Models can also
be specified by name. Uses bc(), bc_inv(), bc_prime()
.
unitran(x, name=NULL, par=NULL, alpha=NULL, beta=NULL, reverse="auto") unitran_inv(y, name=NULL, par=NULL, alpha=NULL, beta=NULL, reverse="auto") unitran_prime(x, name=NULL, par=NULL, alpha=NULL, beta=NULL, reverse="auto")
unitran(x, name=NULL, par=NULL, alpha=NULL, beta=NULL, reverse="auto") unitran_inv(y, name=NULL, par=NULL, alpha=NULL, beta=NULL, reverse="auto") unitran_prime(x, name=NULL, par=NULL, alpha=NULL, beta=NULL, reverse="auto")
x , y
|
Variable to be transformed, |
name |
Optional model name, case-insensitive, in quotes. One of |
par |
Model parameter, if needed and model name supplied. |
alpha , beta
|
Shape parameters, if the model is not specified by name. |
reverse |
Reverse |
unitran()
: Transformed x
, i.e., .
unitran_inv()
: Inverse of unitran()
, .
unitran_prime()
: Derivative of unitran()
, .
unitran()
: Unified transformation.
unitran_inv()
: Inverse of unitran().
unitran_prime()
: Derivative of unitran()
with respect to x
.
curve(unitran(x, "Gompertz")) # same as unitran(x, alpha=0, beta=0) curve(unitran_inv(y, "logistic"), xname="y", from=-4, to=4) curve(unitran_prime(x, "logistic"))
curve(unitran(x, "Gompertz")) # same as unitran(x, alpha=0, beta=0) curve(unitran_inv(y, "logistic"), xname="y", from=-4, to=4) curve(unitran_prime(x, "logistic"))
Templates for user-supplied transformation and drivative functions, used by
sdefit()
if specified in parameters phi
and/or
phiprime
. To be completed by the user.
userphi(x, theta) userphiprime(x, theta)
userphi(x, theta) userphiprime(x, theta)
x |
Numeric vector, variable to be transformed. |
theta |
Named list of transformation parameters |
Transformed variable
Transformation derivative
userphi()
: transformation
userphiprime()
: derivative
These functions are not normally called directly by the user.
Function uvector()
is used by sdefit()
. Function
uvector_noh()
is a more limited version, maintained for documentation
purposes. Function logdet_and_v()
is used by uvector()
and
uvector_noh()
.
uvector(x, t, unit = NULL, beta0, beta1, eta, eta0, x0, t0, lambda, mum = 1, mu0 = 1, mup = 1, sorted = FALSE, final = FALSE) uvector_noh(x, t, beta0, beta1, eta, eta0, x0, t0, lambda, final = FALSE) logdet.and.v(cdiag, csub = NULL, z)
uvector(x, t, unit = NULL, beta0, beta1, eta, eta0, x0, t0, lambda, mum = 1, mu0 = 1, mup = 1, sorted = FALSE, final = FALSE) uvector_noh(x, t, beta0, beta1, eta, eta0, x0, t0, lambda, final = FALSE) logdet.and.v(cdiag, csub = NULL, z)
x , t
|
Data vectors |
unit |
Unit id vector, if any. |
beta0 , beta1 , eta , eta0 , x0 , t0
|
SDE parameters or re-parameterizations. |
lambda |
Named list of parameters(s) for |
mum , mu0 , mup
|
Optional |
sorted |
Data already ordered by increasing t? |
final |
Mode, see below. |
cdiag |
Vector with the diagonal elements |
csub |
Vector with sub-diagonal |
z |
A numeric vector |
uvector()
and uvector_noh()
calculate a vector of
residuals for sum of squares minimization by nls()
or nlme()
.
The first one works both for single-unit and for bilevel hierarchical models.
It is backward-compatible with uvector_noh()
, which is only for
single-unit models but simpler and easier to understand. They require a
transformation function phi(x, theta)
, and a function
phiprime(x, theta)
for the derivative dy/dx, where theta
is a
list containing the transformation parameters.
logdet_and_v()
calculates and
, where
, with
lower-triangular.
The three functions are essentially unchanged from García (2019)
<doi:10.1007/s00180-018-0837-4>, except for a somewhat safer computation
for very small beta1
, and adding in logdet_and_v()
a shortcut
for when is diagonal (e.g., when
). The
transformation functions
phi
and phiprime
can be passed as
globals, as in the original, or in an environment named trfuns
.
uvector()
and uvector_noh()
: If final = FALSE
(default), return a vector whose sum of squares should be minimized over the
parameters to obtain maximum-likelihood estimates. If final = TRUE
,
passing the ML parameter estimates returns a list with the sigma estimates,
the maximized log-likelihood, and AIC and BIC criteria..
logdet_and_v()
: List with elements logdet
and v
.
uvector()
: Estimation vector, general
uvector_noh()
: Estimation vector, non-hierarchical
logdet.and.v()
: Logarithm of determinant, and vector