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)
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
<- set_timeline(2, "years")
time_df head(time_df)
#> [1] "2001-01-01" "2001-01-02" "2001-01-03" "2001-01-04" "2001-01-05"
#> [6] "2001-01-06"
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
<-set_timeline(2, "month")
weekly_time
# Set week pattern without noise
<- define_pattern_t(weekly_time,
weekly_timeseries_nonoise 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
$label<-as.factor("Without noise")
weekly_timeseries_nonoise
# Set week pattern with noise
<- define_pattern_t(weekly_time,
weekly_timeseries_noise 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
$label<-as.factor("With noise")
weekly_timeseries_noise
#Bind weekly patterns (with and without noise) datasets into single data frame
<-rbind(weekly_timeseries_nonoise, weekly_timeseries_noise)
week_timeseries
# Create a time series
<-set_timeline(9, "month")
season_time
# Set seasonally pattern without noise
<- define_pattern_t(season_time,
season_timeseries_nonoise 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
$label<-as.factor("Without noise")
season_timeseries_nonoise
# Set seasonally pattern with noise
<- define_pattern_t(season_time,
season_timeseries_noise 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
$label<-as.factor("With noise")
season_timeseries_noise
#Bind seasonally patterns (with and without noise) datasets into single data frame
<-rbind(season_timeseries_nonoise, season_timeseries_noise)
season_timeseries
# Set annual pattern without noise
<- define_pattern_t(time_df,
annual_timeseries_nonoise weekly_amplitude = 0,
weekly_noise = 0,
seasonal_amplitude = 0,
seasonal_noise = 0,
annual_amplitude = 1,
annual_noise = 0)
$label<-as.factor("Without noise")
annual_timeseries_nonoise
# Set annual pattern with noise
<- define_pattern_t(time_df,
annual_timeseries_noise weekly_amplitude = 0,
weekly_noise = 0,
seasonal_amplitude = 0,
seasonal_noise = 0,
annual_amplitude = 1,
annual_noise = 2)
$label<-as.factor("With noise")
annual_timeseries_noise
#Bind annual patterns (with and without noise) datasets into single data frame
<-rbind(annual_timeseries_nonoise, annual_timeseries_noise)
annual_timeseries
# Create plots
<-ggplot(week_timeseries, aes(x = date, y = pattern, color=label)) +
week_plotgeom_path()+
geom_point() +
theme_bw()+
labs(color="", title ="Weekly pattern", x="Date", y="Value")+
scale_color_manual(values=c("black", alpha("grey",0.5)))
<-ggplot(season_timeseries, aes(x = date, y = pattern, color=label)) +
season_plotgeom_path()+
geom_point() +
theme_bw()+
labs(color="", title="seasonally pattern", x="Date", y="Value")+
scale_color_manual(values=c("black", alpha("grey",0.5)))
<-ggplot(annual_timeseries, aes(x = date, y = pattern, color=label)) +
annual_plotgeom_path()+
geom_point() +
theme_bw() +
labs(color="", title="Annual pattern", x="Date", y="Value")+
scale_color_manual(values=c("black", alpha("grey",0.5)))
/season_plot/annual_plot + plot_layout(guides = "collect") week_plot
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
<-set_timeline(6, "month")
timeseries
# Set time series pattern without noise
<- define_pattern_t(timeseries,
timeseries_nonoise 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
$label<-as.factor("Without noise")
timeseries_nonoise
# Set time series pattern with noise
<- define_pattern_t(timeseries,
timeseries_noise 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
$label<-as.factor("With noise")
timeseries_noise
<-rbind(timeseries_nonoise, timeseries_noise)
timeseries_combined
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)))