simulate_target {gratis}R Documentation

Generating time series with controllable features using MAR models

Description

simulate_target simulate one time series of length 'length' from a MAR model with target features and returns a ts or msts object. generate_target simulate multiple time series of length 'length' from a MAR model with target features and returns a tsibble object. The index of the tsibble is guessed from the seasonal periods. The specified features should not depend on the scale of the time series as the series is scaled during simulation.

Usage

simulate_target(
  length = 100,
  seasonal_periods = 1,
  feature_function,
  target,
  k = ifelse(length(seasonal_periods) == 1, 3, length(seasonal_periods)),
  tolerance = 0.05,
  trace = FALSE,
  parallel = FALSE
)

generate_target(
  length = 100,
  nseries = 10,
  seasonal_periods = 1,
  feature_function,
  target,
  k = ifelse(length(seasonal_periods) == 1, 3, length(seasonal_periods)),
  tolerance = 0.05,
  trace = FALSE,
  parallel = FALSE
)

Arguments

length

length of the time series to be generated.

seasonal_periods

Either a scalar or a numeric vector of length k containing the number of seasonal periods for each component.

feature_function

a function that returns a vector of features from a time series.

target

target feature values of the same length as that returned by feature_function().

k

integer specifying number of components to use for MAR models. Default is 3 unless there are multiple seasonal periods specified.

tolerance

average tolerance per feature. The genetic algorithm will attempt to find a solution where the average difference between the series features and target is less than tolerance. A larger value will give a faster but less precise solution.

trace

logical indicating if details of the search should be shown.

parallel

An optional argument which allows to specify if the Genetic Algorithm should be run sequentially or in parallel.

nseries

Number of series to generate

Value

A time series object of class "ts" or "msts".

Author(s)

Yanfei Kang and Rob J Hyndman

Examples

set.seed(1)
library(tsfeatures)
my_features <- function(y) {
  c(entropy(y), acf = acf(y, plot = FALSE)$acf[2:3, 1, 1])
}
# Simulate a ts with specified target features
y <- simulate_target(
  length = 60, feature_function = my_features, target = c(0.5, 0.9, 0.8)
)
my_features(y)
plot(y)
## Not run: 
# Generate a tsibble with specified target features
df <- generate_target(
  length = 60, feature_function = my_features, target = c(0.5, 0.9, 0.8)
)
df %>% 
 as_tibble() %>%
 group_by(key) %>%
 dplyr::summarise(value = my_features(value), 
           feature=c("entropy","acf1", "acf2"),
           .groups = "drop")
autoplot(df)
# Simulate time series similar to an existing series
my_features <- function(y) {
  c(stl_features(y)[c("trend", "seasonal_strength", "peak", "trough")])
}
y <- simulate_target(
  length = length(USAccDeaths),
  seasonal_periods = frequency(USAccDeaths),
  feature_function = my_features, target = my_features(USAccDeaths)
)
tsp(y) <- tsp(USAccDeaths)
plot(cbind(USAccDeaths, y))
cbind(my_features(USAccDeaths), my_features(y))
## End(Not run)

[Package gratis version 1.0.7 Index]