Simulation | R Documentation |
An RTMB objective function can be run in 'simulation mode' where standard likelihood evaluation is replaced by corresponding random number generation. This facilitates automatic simulation under some restrictions. Simulations can be obtained directly from the model object by obj$simulate()
or used indirectly via checkConsistency.
simref(n)
## S3 replacement method for class 'simref'
dim(x) <- value
## S3 method for class 'simref'
length(x)
## S3 method for class 'simref'
dim(x)
## S3 method for class 'simref'
is.array(x)
## S3 method for class 'simref'
is.matrix(x)
## S3 method for class 'simref'
as.array(x, ...)
## S3 method for class 'simref'
is.na(x)
## S3 method for class 'simref'
x[...]
## S3 replacement method for class 'simref'
x[...] <- value
## S3 method for class 'simref'
Ops(e1, e2)
## S3 method for class 'simref'
Math(x, ...)
## S3 method for class 'simref'
t(x)
## S3 method for class 'simref'
diff(x, lag = 1L, differences = 1L, ...)
## S3 method for class 'simref'
Summary(..., na.rm = FALSE)
n |
Length |
x |
Object of class 'simref' |
value |
Replacement (numeric) |
... |
Extra arguments |
e1 |
First argument |
e2 |
Second argument |
lag |
As diff |
differences |
As diff |
na.rm |
Ignored |
In simulation mode all log density evaluation, involving either random effects or observations, is interpreted as probability assignment.
direct vs indirect Assignments can be 'direct' as for example
dnorm(u, log=TRUE) ## u ~ N(0, 1)
or 'indirect' as in
dnorm(2*(u+1), log=TRUE) ## u ~ N(-1, .25)
Indirect assignment works for a limited set of easily invertible functions - see methods(class="simref")
.
Simulation order Note that probability assignments are sequential: All information required to draw a new variable must already be simulated. Vectorized assignment implicitly occurs elementwise from left to right. For example the assignment
dnorm(diff(u), log=TRUE)
is not valid without a prior assignment of u[1]
, e.g.
dnorm(u[1], log=TRUE)
Supported distributions Assignment must use supported density functions. I.e.
dpois(N, exp(u), log=TRUE)
cannot be replaced by
N * u - exp(u)
The latter will have no effect in simulation mode (the simulation will be NA
).
Return value Note that when in simulation mode, the density functions all return zero. The actual simulation is written to the input argument by reference. This is very unlike standard R semantics.
An object with write access to store the simulation.
simref()
: Construct simref
dim(simref) <- value
: Equivalent of dim<-
length(simref)
: Equivalent of length
dim(simref)
: Equivalent of dim
is.array(simref)
: Equivalent of is.array
is.matrix(simref)
: Equivalent of is.matrix
as.array(simref)
: Equivalent of as.array
is.na(simref)
: Equivalent of is.na
[
: Equivalent of [
`[`(simref) <- value
: Equivalent of [<-
Ops(simref)
: Equivalent of Ops
Math(simref)
: Equivalent of Math
t(simref)
: Equivalent of t
diff(simref)
: Equivalent of diff
Summary(simref)
: Summary operations are not invertible and will throw an error.
s <- simref(4)
s2 <- 2 * s[1:2] + 1
s2[] <- 7
s ## 3 3 NA NA
## Random walk
func <- function(p) {
u <- p$u
ans <- -dnorm(u[1], log=TRUE) ## u[1] ~ N(0,1)
ans <- ans - sum(dnorm(diff(u), log=TRUE)) ## u[i]-u[i-1] ~ N(0,1)
}
obj <- MakeADFun(func, list(u=numeric(20)), random="u")
obj$simulate()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.