Temporal dataset simulation

In this vignette

This vignette is complementary to the “The Full Game” and it provides further details on how to create periodic patterns in the data corresponding to cycles within days, weeks, seasons or years using the package ascot.

library(ascot)
library(patchwork)
library(ggplot2)

Create a time series

The first step is to create a timeline using the set_timeline function which allows you to define the length of your study period in weeks, months or years. Please note that the dataset created is on a daily resolution.

# Determine duration of time series
time_df <- set_timeline(2, "years")
head(time_df)
#> [1] "2001-01-01" "2001-01-02" "2001-01-03" "2001-01-04" "2001-01-05"
#> [6] "2001-01-06"

Create a temporal pattern

From here, you can create a temporal pattern for the specified time period using the function define_pattern_t. You can generate a weekly, seasonal and annual pattern. There are two components to generate the data: amplitude and noise. The amplitude corresponds to the distance from 0 while noise is how much variation there is. Below you can see examples of data sets with patterns at different time scales and with different noise settings.

# Create a time series   
weekly_time<-set_timeline(2, "month")

# Set week pattern without noise
weekly_timeseries_nonoise <- define_pattern_t(weekly_time,
                                weekly_amplitude = 1,
                                weekly_noise = 0,
                                seasonal_amplitude = 0,
                                seasonal_noise = 0,
                                annual_amplitude = 0,
                                annual_noise = 0)
#> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns
weekly_timeseries_nonoise$label<-as.factor("Without noise")

# Set week pattern with noise  
weekly_timeseries_noise <- define_pattern_t(weekly_time,
                                weekly_amplitude = 1,
                                weekly_noise = 2,
                                seasonal_amplitude = 0,
                                seasonal_noise = 0,
                                annual_amplitude = 0,
                                annual_noise = 0)
#> Warning: One quarter or less of data, ignoring arguments for seasonal and annual
#> patterns
weekly_timeseries_noise$label<-as.factor("With noise")

#Bind weekly patterns (with and without noise) datasets into single data frame
week_timeseries<-rbind(weekly_timeseries_nonoise, weekly_timeseries_noise)

# Create a time series   
season_time<-set_timeline(9, "month")

# Set seasonally pattern without noise 
season_timeseries_nonoise <- define_pattern_t(season_time,
                                weekly_amplitude = 0,
                                weekly_noise = 0,
                                seasonal_amplitude = 1,
                                seasonal_noise = 0,
                                annual_amplitude = 0,
                                annual_noise = 0)
#> Warning: Only one year or less of data, ignoring arguments for annual patterns
season_timeseries_nonoise$label<-as.factor("Without noise")

# Set seasonally pattern with noise 
season_timeseries_noise <- define_pattern_t(season_time,
                                weekly_amplitude = 0,
                                weekly_noise = 0,
                                seasonal_amplitude = 1,
                                seasonal_noise = 2,
                                annual_amplitude = 0,
                                annual_noise = 0)
#> Warning: Only one year or less of data, ignoring arguments for annual patterns
season_timeseries_noise$label<-as.factor("With noise")

#Bind seasonally patterns (with and without noise) datasets into single data frame
season_timeseries<-rbind(season_timeseries_nonoise, season_timeseries_noise)


# Set annual pattern without noise 
annual_timeseries_nonoise <- define_pattern_t(time_df,
                                weekly_amplitude = 0,
                                weekly_noise = 0,
                                seasonal_amplitude = 0,
                                seasonal_noise = 0,
                                annual_amplitude = 1,
                                annual_noise = 0)
annual_timeseries_nonoise$label<-as.factor("Without noise")

# Set annual pattern with noise 
annual_timeseries_noise <- define_pattern_t(time_df,
                                weekly_amplitude = 0,
                                weekly_noise = 0,
                                seasonal_amplitude = 0,
                                seasonal_noise = 0,
                                annual_amplitude = 1,
                                annual_noise = 2)
annual_timeseries_noise$label<-as.factor("With noise")

#Bind annual patterns (with and without noise) datasets into single data frame
annual_timeseries<-rbind(annual_timeseries_nonoise, annual_timeseries_noise)

# Create plots
week_plot<-ggplot(week_timeseries, aes(x = date, y = pattern, color=label)) +
  geom_path()+
  geom_point() +
  theme_bw()+
  labs(color="", title ="Weekly pattern", x="Date", y="Value")+
  scale_color_manual(values=c("black", alpha("grey",0.5))) 

season_plot<-ggplot(season_timeseries, aes(x = date, y = pattern, color=label)) +
  geom_path()+
  geom_point() +
  theme_bw()+
  labs(color="", title="seasonally pattern", x="Date", y="Value")+ 
  scale_color_manual(values=c("black", alpha("grey",0.5))) 

annual_plot<-ggplot(annual_timeseries, aes(x = date, y = pattern, color=label)) +
  geom_path()+
  geom_point() +
  theme_bw() +
  labs(color="", title="Annual pattern", x="Date", y="Value")+ 
  scale_color_manual(values=c("black", alpha("grey",0.5))) 

week_plot/season_plot/annual_plot + plot_layout(guides = "collect")

It is possible to create a data set with patterns at multiple scales.In the example below, there is a weekly and seasonally pattern in the data.

# Set time series  
timeseries<-set_timeline(6, "month")

# Set time series pattern without noise 
timeseries_nonoise <- define_pattern_t(timeseries,
                                weekly_amplitude = 2,
                                weekly_noise = 0,
                                seasonal_amplitude = 1,
                                seasonal_noise = 0,
                                annual_amplitude = 0,
                                annual_noise = 0)
#> Warning: Only one year or less of data, ignoring arguments for annual patterns
timeseries_nonoise$label<-as.factor("Without noise")

# Set time series pattern with noise 
timeseries_noise <- define_pattern_t(timeseries,
                                weekly_amplitude = 2,
                                weekly_noise = 1,
                                seasonal_amplitude = 1,
                                seasonal_noise = 1,
                                annual_amplitude = 0,
                                annual_noise = 0)
#> Warning: Only one year or less of data, ignoring arguments for annual patterns

timeseries_noise$label<-as.factor("With noise")

timeseries_combined<-rbind(timeseries_nonoise, timeseries_noise)

ggplot(timeseries_combined, aes(x = date, y = pattern, color=label)) +
  geom_path()+
  geom_point() +
  theme_bw() +
  labs(color="", x="Date", y="Value")+ 
  scale_color_manual(values=c("black", alpha("grey",0.5)))