Convert simulation results to a data.frame.
Usage
# S3 method for class 'ensemble_stockflow'
as.data.frame(
x,
row.names = NULL,
optional = FALSE,
which = c("summary", "sims")[1],
direction = "long",
sim = NULL,
condition = NULL,
vars = NULL,
type = NULL,
...
)Arguments
- x
Output of
simulate().- row.names
NULL or a character vector giving the row names for the data frame. Missing values are not allowed.
- optional
Ignored parameter.
- which
Type of data to return. Either
"summary"for a summary statistics, or"sims"for individual simulation trajectories. Defaults to"summary".- direction
Format of data frame, either "long" (default) or "wide".
- sim
Indices of the individual trajectories to include if which =
"sims". Defaults toNULL, which includes all trajectories. Including a high number of trajectories will create a large object.- condition
Indices of the conditions to include. Defaults to
NULL, which includes all conditions.- vars
Variables to plot. Defaults to
NULLto plot all variables.- type
Variable types to retain in the data frame. Must be one or more of 'stock', 'flow', 'constant', 'aux', 'lookup', or 'func'. Defaults to
NULLto include all types.- ...
Optional parameters
Value
A data.frame with simulation results. For direction = "long" (default),
the data frame has three columns: time, variable, and value.
For direction = "wide", the data frame has columns time followed by
one column per variable.
Examples
sfm <- stockflow("sir") |>
# Randomize initial values of all stocks to show variation in the ensemble
update(c(susceptible, infected, recovered),
eqn = runif(1, min = 20, max = 800)
)
# Run ensemble simulation with 3 simulations,
# saving only 20 timepoints per simulation
sims <- ensemble(sfm, n = 3, save_length = 20, save_sims = TRUE)
#> Starting ensemble simulation in "R" with 3 simulations.
#> ✔ Ensemble simulation completed in 0.2007 seconds.
# Get summary statistics in long format
df <- as.data.frame(sims)
head(df, n = 1)
#> condition variable time mean median missing_count quant1 quant2
#> 1 1 infected 0 314.1899 246.0185 0 36.78391 649.5417
# Get summary statistics in wide format
df_wide <- as.data.frame(sims, direction = "wide")
head(df_wide, n = 1)
#> condition time mean.infected median.infected missing_count.infected
#> 1 1 0 314.1899 246.0185 0
#> quant1.infected quant2.infected mean.recovered median.recovered
#> 1 36.78391 649.5417 488.0095 488.5935
#> missing_count.recovered quant1.recovered quant2.recovered mean.susceptible
#> 1 0 389.0273 586.4952 211.2914
#> median.susceptible missing_count.susceptible quant1.susceptible
#> 1 142.6226 0 85.96698
#> quant2.susceptible
#> 1 394.9842
# Get individual simulations in wide format
df_wide_sims <- as.data.frame(sims, which = "sims", direction = "wide")
head(df_wide_sims, n = 1)
#> time sim condition infected recovered susceptible
#> 1 0 1 1 670.7798 488.5935 82.98511