bake {pomp} | R Documentation

## Bake, stew, and freeze

### Description

Tools for reproducible computations.

### Usage

    bake(file, expr, seed = NULL, kind = NULL, normal.kind = NULL)

    stew(file, expr, seed = NULL, kind = NULL, normal.kind = NULL)

    freeze(expr, seed = NULL, kind = NULL, normal.kind = NULL)

### Arguments

`file` | Name of the binary data file in which the result will be stored or retrieved, as appropriate. For `bake`, this will contain a single object and hence be an RDS file (extension ‘rds’); for `stew`, this will contain one or more named objects and hence be an RDA file (extension ‘rda’).
`expr` | Expression to be evaluated.
`seed, kind, normal.kind` | optional. To set the state and of the RNG. See `set.seed`. The default, `seed = NULL`, will not change the RNG state. `seed` should be a single integer. See `set.seed`.

### Details

On cooking shows, recipes requiring lengthy baking or stewing are prepared beforehand. The `bake` and `stew` functions perform analogously: an computation is performed and stored in a named file. If the function is called again and the file is present, the computation is not executed. Instead, the results are loaded from the file in which they were previously stored. Moreover, via their optional `seed` argument, `bake` and `stew` can control the pseudorandom-number generator (RNG) for greater reproducibility. After the computation is finished, these functions restore the pre-existing RNG state to avoid side effects.

The `freeze` function doesn't save results, but does set the RNG state to the specified value and restore it after the computation is complete.

Both `bake` and `stew` first test to see whether `file` exists. If it does, `bake` reads it using `readRDS` and returns the resulting object. By contrast, `stew` loads the file using `load` and copies the objects it contains into the user's workspace (or the environment of the call to `stew`).

If `file` does not exist, then both `bake` and `stew` evaluate the expression `expr`; they differ in the results that they save. `bake` saves the value of the evaluated expression to `file` as a single object. The name of that object is not saved. By contrast, `stew` creates a local environment within which `expr` is evaluated; all objects in that environment are saved (by name) in `file`.

### Value

`bake` returns the value of the evaluated expression `expr`. Other objects created in the evaluation of `expr` are discarded along with the temporary, local environment created for the evaluation.

The latter behavior differs from that of `stew`, which returns the names of the objects created during the evaluation of `expr`. After `stew` completes, these objects exist in the parent environment (that from which `stew` was called).

`freeze` returns the value of evaluated expression `expr`. However, `freeze` evaluates `expr` within the parent environment, so other objects created in the evaluation of `expr` will therefore exist after `freeze` completes.

`bake` and `stew` return information about the time used in evaluating the expression. This is recorded in the `system.time` attribute of the return value. In addition, if `seed` is specified, information about the seed (and the kind of random-number generator used) are stored as attributes of the return value.

### Author(s)

Aaron A. King

### Examples

      bake(file="example1.rds",{
        x <- runif(1000)
        mean(x)
      })

      stew(file="example2.rda",{
        x <- runif(10)
        y <- rnorm(n=10,mean=3*x+5,sd=2)
      })

      plot(x,y)

      freeze(runif(3),seed=5886730)
      freeze(runif(3),seed=5886730)

[Package pomp version 2.3 Index]