Automatic frequency and trend calculation from a time series indexSource:
Automatic frequency and trend calculation from a time series index
tk_get_frequency(idx, period = "auto", message = TRUE) tk_get_trend(idx, period = "auto", message = TRUE)
A date or datetime index.
Either "auto", a time-based definition (e.g. "2 weeks"), or a numeric number of observations per frequency (e.g. 10).
A boolean. If
message = TRUE, the frequency or trend is output as a message along with the units in the scale of the data.
Returns a scalar numeric value indicating the number of observations in the frequency or trend span.
A frequency is loosely defined as the number of observations that comprise a cycle
in a data set. The trend is loosely defined as time span that can
be aggregated across to visualize the central tendency of the data.
It's often easiest to think of frequency and trend in terms of the time-based units
that the data is already in. This is what
enable: using time-based periods to define the frequency or trend.
As an example, a weekly cycle is often 5-days (for working
days) or 7-days (for calendar days). Rather than specify a frequency of 5 or 7,
the user can specify
period = "1 week", and
tk_get_frequency() will detect the scale of the time series and return 5 or 7
based on the actual data.
period argument has three basic options for returning a frequency.
"auto": A target frequency is determined using a pre-defined template (see
time-based duration: (e.g. "1 week" or "2 quarters" per cycle)
numeric number of observations: (e.g. 5 for 5 observations per cycle)
period = "auto", the
tk_time_scale_template() is used to calculate the frequency.
As an example, the trend of daily data is often best aggregated by evaluating
the moving average over a quarter or a month span. Rather than specify the number
of days in a quarter or month, the user can specify "1 quarter" or "1 month",
time_trend() function will return the correct number of observations
per trend cycle. In addition, there is an option,
period = "auto", to
auto-detect an appropriate trend span depending on the data. The
is used to define the appropriate trend span.
Time Scale Template
tk_time_scale_template() is a Look-Up Table used by the trend and period to find the
appropriate time scale. It contains three features:
The algorithm will inspect the scale of the time series and select the best frequency or trend that matches the scale and number of observations per target frequency. A frequency is then chosen on be the best match.
library(dplyr) idx_FB <- FANG %>% filter(symbol == "FB") %>% pull(date) # Automated Frequency Calculation tk_get_frequency(idx_FB, period = "auto") #> frequency = 5 observations per 1 week #>  5 # Automated Trend Calculation tk_get_trend(idx_FB, period = "auto") #> trend = 64 observations per 3 months #>  64 # Manually Override Trend tk_get_trend(idx_FB, period = "1 year") #> trend = 252 observations per 1 year #>  252