Description Usage Arguments Details Value Examples
Simulate
is a function to simplify simulation studies.
It can be used to conduct Monte Carlo studies of statistical estimators,
discrete event, and agent based simulations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 
step 
an expression that produces simulation results for each replication; can be a function call, or a braced expression that “returns” a value like a function body. 
conditions 
an optional data frame or object coerceable into a data frame. Each row of this data frame defines an experimental condition. 
start 
either NULL or an expression that computes starting values
for 
cleanup 
either NULL or an expression does some cleaning up after
the exectution of all 
... 
other substitutions for 
nsim 
an integer value; the number of replication in each experimental setting. If

seed 
either NULL or an integer value suitable for 
trace 
an integer value determining the amount of information
output during the simulation process. If 
keep.data 
logical value; if TRUE, return values of the expression
in 
keep.states 
logical value; if TRUE, a list of all variables
defined in 
keep.seed 
logical value; if TRUE, the state of the random number generator
is saved in an attribute "seed" of the return value of 
restore.seed 
logical value; if TRUE, the state of the random number generator is restored after conducting the simulations. 
bucket 
a function that returns a 
msg 
a character string, the message shown if an interrupt condition is signalled. 
Simulate
calls or evaluates its first argument, step
,
or, if a conditions
argument is given, nsim
times
for each row of the conditions
data frame.
Before repeatingly evaluating step
, the expression start
, if present,
is evaluated, which may be used to create starting values for
a simulatation of to setup up the scenery for an agentbased simulation.
After repeatingly evaluating step
, the expression cleanup
,
if present, is evaluated.
If restore.seed
is given, the state of the random generator
is saved before conducting the simulation and restored afterwards. Therefore
step
, start
, or cleanup
may call set.seed
without affecting the generation of random numbers after a call to
Simulate
.
interrupt
raises an interrupt condition, which acts like
a user interrupt.
Note that if an interrupt condition is signalled during a (replicated)
evaluation of step
the results of previous replications are
still saved and Simulate
jumps to the next condition of
the simulation experiment (if there is any). That is, if a simulation
is interrupted by the user because it takes too long, the results so far
produced by the simulation are not lost.
On the other hand, interrupt
can be used to determine at runtime
how often step
is evaluated.
A data frame that contains experimental conditions and simulation results.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63  Normal.example < function(mean=0,sd=1,n=10){
x < rnorm(n=n,mean=mean,sd=sd)
c(
Mean=mean(x),
Median=median(x),
Var=var(x)
)
}
Normal.simres < Simulate(
Normal.example(mean,sd,n),
expand.grid(
mean=0,
sd=c(1,10),
n=c(10,100)
),
nsim=200,
trace=50)
if(require(mtable)){
genTable(sd(Median)~sd+n,data=Normal.simres)
}
expr.simres < Simulate(
median(rnorm(n,mean,sd)),
expand.grid(
n=c(10,100),
mean=c(0,1),
sd=c(1,10)
),
nsim=200,
trace=50
)
if(require(mtable)){
genTable(c(mean(result),sd(result))~sd+n+mean,data=expr.simres)
}
## Not run:
## This takes a little bit longer
lm.example < function(a=0,b=1,n=101,xrange=c(1,1),serr=1){
x < seq(from=xrange[1],to=xrange[2],length=n)
y < a + b*x + rnorm(n,sd=serr)
lm.res < lm(y~x)
coef < lm.res$coef
names(coef) < c("a","b")
coef
}
lm.simres < Simulate(
lm.example(n=n,serr=serr),
expand.grid(
serr=c(0.1,1,10),
n=c(11,101,501)
),
nsim=200,
trace=50
)
if(require(mtable)){
genTable(c(sd(a),sd(b))~serr+n,data=lm.simres)
}
## End(Not run)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.