
Last chance! 50% off unlimited learning
Sale ends in
Simulate
to collect
results from individual replications of a simulation study.# Generic functions used by 'Simulate'
put_into(bucket,value)
pour_out(bucket,...)# This generates a bucket that puts data
# into a data frame
default_bucket(size=1)
## S3 method for class 'default_bucket':
put_into(bucket,value)
## S3 method for class 'default_bucket':
pour_out(bucket,...)
## S3 method for class 'default_bucket':
dim(x)
## S3 method for class 'default_bucket':
as.matrix(x,...)
## S3 method for class 'default_bucket':
as.data.frame(x,...)
# This generates a bucket that puts data
# into a text file.
textfile_bucket(size=1,name=date())
## S3 method for class 'textfile_bucket':
put_into(bucket,value)
## S3 method for class 'textfile_bucket':
pour_out(bucket,...)
## S3 method for class 'textfile_bucket':
dim(x)
## S3 method for class 'textfile_bucket':
as.data.frame(x,...)
## S3 method for class 'textfile_bucket':
as.matrix(x,...)
## S3 method for class 'bucket':
print(x,...)
## S3 method for class 'bucket':
[(x,i,...)
Simulate
either according to its nsim
argument or according
to default_bucket
returns
an object of class "default_bucket"
,
while function textfile_bucket
returns
an object of class "textfile_bucket"
. The methods for dim
, as.data.frame
,
and as.matrix
give the usual return values,
of the generic functions.
put_into
returns nothing.
pour_out
returns the bucket.
default_bucket
and textfile_bucket
do;
and to define methods of put_into
, pour_out
,
dim
, as.data.frame
, as.matrix
for
the newly defined bucket class.The put_into
method should add a list of values to the bucket,
the pour_out
method should close the bucket
against further input and pour
out in the data contained in the bucket into a fixed data structure.
After pour_out
, put_into
should fail.
The default_bucket
method, e.g. puts the data into a data frame,
the textfile_bucket
method closes the textfile into which
data were written.
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)
)
}
Sim_default_bucket <- Simulate(
Normal.example(mean,sd,n),
expand.grid(
mean=0,
sd=c(1,10),
n=c(10,100)
),
nsim=200)
tempfile_bucket <- function(n)textfile_bucket(n,name=tempfile())
Sim_textfile_bucket <- Simulate(
Normal.example(mean,sd,n),
expand.grid(
mean=0,
sd=c(1,10),
n=c(10,100)
),
nsim=200,
bucket=tempfile_bucket
)
Sim_default_bucket
Sim_default_bucket[1:10]
Sim_textfile_bucket
Sim_textfile_bucket[1:10]
# Access of the textfile generated by 'textfile_bucket':
Sim_textfile_bucket$name
read.table(Sim_textfile_bucket$name,header=TRUE,nrow=10)
Run the code above in your browser using DataLab