buckets: Abstract Data Structures to Collect Simulation Results

Description Usage Arguments Details Value Examples

Description

Buckets (objects that inherit from S3 class "bucket") are abstract data structures that are internally used by function Simulate to collect results from individual replications of a simulation study.

Usage

 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
# 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,...]

Arguments

bucket,x,data

an object that inherits from class "bucket".

value

a named list of results of a replication to put into the bucket.

size

a numerical value, the number of rows by which to extend the bucket if it is full. This will be set by Simulate either according to its nsim argument or according to getOption("Simulation.chunk.size")

name

a name for the textfile into which results are written.

i

a numerical vector to select replication results from the bucket.

...

other arguments, ignored or passed to other methods.

Details

If a user wants to provide another class of buckets for collecting results for 'Simulate' (s)he needs to define a function that returns a bucket object, as 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.

Value

The function 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.

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
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)

msimul documentation built on May 31, 2017, 2:23 a.m.