Skip to contents

Uses zoo_simulate underneath to generate simulated time series lists for testing and learning purposes.

This function supports progress bars generated by the progressr package, and accepts a parallelization setup via future::plan() (see examples).

Usage

tsl_simulate(
  n = 2,
  cols = 5,
  rows = 100,
  time_range = c("2010-01-01", "2020-01-01"),
  data_range = c(0, 1),
  seasons = 0,
  na_fraction = 0,
  independent = FALSE,
  irregular = TRUE,
  seed = NULL
)

Arguments

n

(optional, integer) Number of time series to simulate. Default: 2.

cols

(optional, integer) Number of columns of each time series. Default: 5

rows

(optional, integer) Length of each time series. Minimum is 10, but maximum is not limited. Very large numbers might crash the R session. Default: 100

time_range

(optional character or numeric vector) Time interval of the time series. Either a character vector with dates in format YYYY-MM-DD or or a numeric vector. If there is a mismatch between time_range and rows (for example, the number of days in time_range is smaller than rows), the upper value in time_range is adapted to rows. Default: c("2010-01-01", "2020-01-01")

data_range

(optional, numeric vector of length 2) Extremes of the time series values. Default: c(0, 1)

seasons

(optional, integer) Number of seasons in the resulting time series. The maximum number of seasons is computed as floor(rows/3). Default: 0

na_fraction

(optional, numeric) Value between 0 and 0.5 indicating the approximate fraction of NA data in the simulated time series. Default: 0.

independent

(optional, logical) If TRUE, each new column in a simulated time series is averaged with the previous column to generate dependency across columns, and each new simulated time series is weighted-averaged with a time series template to generate dependency across time series. Irrelevant when cols < 2 or n < 2, and hard to perceive in the output when seasons > 0. Default: FALSE

irregular

(optional, logical) If TRUE, the time intervals between consecutive samples are irregular. Default: TRUE

seed

(optional, integer) Random seed used to simulate the zoo object. If NULL (default), a seed is selected at random. Default: NULL

Value

time series list

See also

Other simulate_time_series: zoo_simulate()

Examples

#parallelization setup
future::plan(
 future::multisession,
 workers = 2 #set to parallelly::availableWorkers() - 1
)

#progress bar
# progressr::handlers(global = TRUE)

# generates a different time series list on each iteration when seed = NULL
tsl <- tsl_simulate(
  n = 2,
  seasons = 4
)

if(interactive()){
  tsl_plot(
    tsl = tsl
  )
}

# generate 3 independent time series
tsl_independent <- tsl_simulate(
  n = 3,
  cols = 3,
  independent = TRUE
)
#> distantia::zoo_name_set(): renaming zoo time series from 'A' to 'B'.
#> distantia::zoo_name_set(): renaming zoo time series from 'A' to 'C'.

if(interactive()){
  tsl_plot(
    tsl = tsl_independent
  )
}

# generate 3 independent time series
tsl_dependent <- tsl_simulate(
  n = 3,
  cols = 3,
  independent = FALSE
)

if(interactive()){
  tsl_plot(
    tsl = tsl_dependent
  )
}

# with seasons
tsl_seasons <- tsl_simulate(
  n = 3,
  cols = 3,
  seasons = 4,
  independent = FALSE
)

if(interactive()){
  tsl_plot(
    tsl = tsl_seasons
  )
}

#disable parallelization
future::plan(
  future::sequential
)