Unsurprisingly, you may want to save your results to your hard disk in case of power outages or random system crashes to allow restarting at the interrupted location, save more complete versions of the analysis results in case you want to inspect the complete simulation results at a later time, store/restore the R seeds for debugging and replication purposes, and so on. This document demonstrates various ways in which SimDesign saves output to hard disks.

As usual, define the functions of interest.

library(SimDesign)
# SimFunctions()

Design <- createDesign(N = c(10,20,30))
Generate <- function(condition, fixed_objects = NULL) {
    dat <- rnorm(condition$N)    
    dat
}

Analyse <- function(condition, dat, fixed_objects = NULL) {
    ret <- c(p = t.test(dat)$p.value)
    ret
}

Summarise <- function(condition, results, fixed_objects = NULL) {
    ret <- EDR(results, alpha = .05)
    ret
}

This is a very simple simulation that takes very little time to complete, however it will be used to show the basic saving concepts supported in SimDesign. Note that more detailed information is located in the runSimulation documentation.

1 Option: save = TRUE (Default is TRUE)

The save flag triggers whether temporary results should be saved to the hard-disk in case of power outages and crashes. When this flag is used results can easily be restored automatically and the simulation can continue where it left off after the hardware problems have been dealt with. In fact, no modifications in the code required because runSimulation() will automatically detect temporary files to resume from (so long as they are resumed from the same computer node; otherwise, see the save_details list).

As a simple example, say that in the \(N=30\) condition something went terribly wrong and the simulation crashed. However, the first two design conditions are perfectly fine. The save flag is very helpful here because the state is not lost and the results are still useful. Finally, supplying a filename argument will safely save the aggregate simulation results to the hard-drive for future reference; however, this won’t be called until the simulation is complete.

Analyse <- function(condition, dat, fixed_objects = NULL) {
    if(condition$N == 30) stop('Danger Will Robinson!')
    ret <- c(p = t.test(dat)$p.value)
    ret
}

res <- runSimulation(Design, replications = 1000, save=TRUE, filename='my-simple-sim',
                     generate=Generate, analyse=Analyse, summarise=Summarise)
## 
## 
Design: 1/3;   RAM Used: 57.8 Mb;   Replications: 1000;   Total Time: 0.00s 
##  Conditions: N=10
## 
## 
Design: 2/3;   RAM Used: 57.8 Mb;   Replications: 1000;   Total Time: 0.31s 
##  Conditions: N=20
## 
## 
Design: 3/3;   RAM Used: 57.8 Mb;   Replications: 1000;   Total Time: 0.62s 
##  Conditions: N=30
## 

Check that temporary file exists.

files <- dir()
files[grepl('SIMDESIGN', files)]
## character(0)

Notice here that the simulation stopped at 67% because the third design condition threw too many consecutive errors (this is a built-in fail-safe in SimDesign). However, after we fix this portion of the code the simulation can be restarted at the previous state and continue on as normal. Therefore, no time is lost.

Analyse <- function(condition, dat, fixed_objects = NULL) {
    ret <- c(p = t.test(dat)$p.value)
    ret
}

res <- runSimulation(Design, replications = 1000, save=TRUE, filename='my-simple-sim',
                     generate=Generate, analyse=Analyse, summarise=Summarise)
## 
## 
Design: 1/3;   RAM Used: 57.8 Mb;   Replications: 1000;   Total Time: 0.00s 
##  Conditions: N=10
## 
## 
Design: 2/3;   RAM Used: 57.7 Mb;   Replications: 1000;   Total Time: 0.29s 
##  Conditions: N=20
## 
## 
Design: 3/3;   RAM Used: 57.8 Mb;   Replications: 1000;   Total Time: 0.57s 
##  Conditions: N=30
## 

Check which files exist.

files <- dir()
files[grepl('SIMDESIGN', files)]
## character(0)
files[grepl('my-simp', files)]
## [1] "my-simple-sim-1.rds" "my-simple-sim.rds"

Notice that when complete, the temporary file is removed from the hard-drive.

4 Recommendations

My general recommendation when running simulations is to supply a filename = 'some_simulation_name' when your simulation is finally ready for run time (particularly for simulations which take a long time to finish), and to leave the default save = TRUE and store_results = TRUE to track any temporary files in the event of unexpected crashes and to store the results objects for future inspection (should the need arise). As the aggregation of the simulation results is often what you are interested in then this approach will ensure that the results are stored in a succinct manner for later analyses. However, if RAM is suspected to be an issue as the simulation progresses then using save_results = TRUE is strongly recommended to avoid memory-based storage issues.