sim-pdmpModel-method: Simulation of a pdmpModel object

Description Usage Arguments Value Note See Also Examples

Description

Simulation of a pdmpModel object

Usage

1
2
3
## S4 method for signature 'pdmpModel'
sim(obj, initialize = FALSE, seed = 1,
  outrate = FALSE, nroot = 1e+06, outSlot = TRUE, ...)

Arguments

obj

obj of class pdmpModel or one of its subclasses

initialize

boolean varialbe. If initialize = TRUE and obj contains a user-defined initializing function (initfunc), this function will be called before the simulation. This can be useful i. e. to set a random initial value (see examples). If random numbers are generated within initfunc, they will be affected by the given seed. To avoid this, add set.seed(NULL) to the function definition of initfunc. Parameter initialize defaults to FALSE, because it is not necessary for simulating piecewise deterministic Markov models.

seed

an integer or NULL. This makes the result of sim reproducible, although random numbers are used during execution. Simulation with equal seeds will lead to equal results. If seed is set to NULL, a new one is created from the current time and the process ID.

outrate

boolean variable. During simulation, the survival function is simulated too, because it is needed to determine the next jump time. Setting outrate = TRUE will return all simulated values including the survival function I. If outrate = FALSE, the values for I will not be returned.

nroot

number of maximal possible jumps. The default value is 1e+06 and should be sufficient. If the simulation breaks before the end of simulation time, try to set a higher value for nroot.

outSlot

boolean variable. If FALSE, only the result of the simulation is returned. If TRUE, the whole obj is returned with the simulation result stored in slot out.

...

optional parameters passed to the solver function (e.g. hmax for lsoda).

Value

The returned value depends on the parameter outSlot. If outSlot = TRUE, the function returns the complete pdmpModel instance with the simulation result saved in the out slot. Otherwise, only the simulation result is returned.

Note

If the result is stored in slot out, it can get lost if the value of the other slots is changed via <-. See pdmp-accessors for further informations.

See Also

function multSim or multSimCsv for multiple simulations, ... for plot and summary methods of the simulation.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
data("simplePdmp") # load object of class pdmpModel

#### simulations

# simulate and plot the result
plot(sim(simplePdmp, seed = 1))

# simulate and plot the result with random seed
plot(sim(simplePdmp, seed = NULL))

# simulating again with seed = 1 leads to the same result as before
plot(sim(simplePdmp, seed = 1))

#### be careful about slot out!

out(simplePdmp) # NULL, because results have not been stored

simplePdmp <- sim(simplePdmp) # simulation results are stored in out
head(out(simplePdmp))

init(simplePdmp) <- c(f = 5, d = 1)
head(out(simplePdmp)) # NULL, because slot init has changed

simplePdmp <- sim(simplePdmp, outSlot = FALSE)
str(simplePdmp) # only the simulation result, the pdmpModel object is lost

### an example with initialize = TRUE

initModel <- pdmpModel(
  descr = "a model with random initial values",
  init = c(f = 0, d = 1),
  discStates = list(d = c(-1, 1)),
  initfunc = function(pdmp){
    set.seed(NULL) # necessary to be not affected by parameter 'seed'
    init(pdmp) <- c("f" = runif(1, min = 1, max = 100), "d" = 1) 
    invisible(pdmp)
  },
  times = c(from = 0, to = 10, by = 0.01),
  dynfunc = function(t, x, parms) c(x["d"]*t, 0),
  ratefunc = function(t, x, parms) 1,
  jumpfunc = function(t, x, parms, jtype) c(-x["f"], (-1)*x["d"])
)
print(init(initModel))
print(head(out(sim(initModel, seed = 2, initialize = TRUE))))
print(head(out(sim(initModel, seed = 5, initialize = TRUE))))

CharlotteJana/pdmpsim documentation built on July 2, 2019, 5:37 a.m.