Simulate events and retain records through the nth event
Source:R/sequences.R
simulate_until_event.RdSimulate events and retain records through the nth event
Usage
simulate_until_event(
data,
definition,
occurrence = 1L,
id = "id",
period = "period",
seed = NULL,
envir = parent.frame(),
batch = NULL
)Arguments
- data
Long-form base
data.frame.- definition
One binary variable definition from
define_variable().- occurrence, id, period
Event trimming arguments.
- seed
Optional random seed. A single number makes the call reproducible and restores the session's random-number state afterwards;
NULL(the default) draws from the session's stream and advances it.- envir
Formula evaluation environment.
- batch
Optional single positive whole number. When given, the simulator runs
batchtimes and returns a plainlistofbatchresults, each exactly what the same call withoutbatchreturns. With aseed, every dataset gets its own seed drawn fromseed, so the whole batch is reproducible; without one the datasets are consecutive draws from the session's random-number stream. The defaultNULLreturns a single result.
Value
A simulab_sim base data.frame with the columns of data
followed by the event indicator named by the variable of definition.
Each unit keeps its rows up to and including the period of its
occurrence-th event, and all its rows when that event never occurs.
Examples
# Long-form input: one row per unit per period.
data <- expand_periods(data.frame(id = 1:30), periods = 8,
id = "id", period = "period")
result <- simulate_until_event(
data,
definition = define_variable("event", formula = "0.3", distribution = "binary"),
occurrence = 1,
seed = 1
)
head(result)
#> <simulab_sim:trimmed_events> 6 rows x 4 columns
#> id period time event
#> 1 1 0 0 0
#> 2 1 1 1 0
#> 3 1 2 2 0
#> 4 1 3 3 1
#> 5 2 0 0 0
#> 6 2 1 1 0