knitr::opts_chunk$set( message = F, warning = F, collapse = TRUE, comment = "#>", fig.path = "README-" )
library(timetk)
A collection of tools for working with time series in R
The timetk
package enables a user to more easily work with time series objects in R. The package has tools for inspecting and manipulating the time-based index, expanding the time features for data mining and machine learning, and converting time-based objects to and from the many time series classes. The following are key benefits:
tbl
) and the major time series data types xts
, zoo
, zooreg
, and ts
: Simplifies coercion and maximizes time-based data retention during coercion to regularized time series (e.g. ts
).An example of the forecasting capabilities as shown in vignette TK03 - Forecasting Using a Time Series Signature with timetk
.
knitr::include_graphics("README_1_bikes_forecast.png")
The package contains the following functions:
Get an index: tk_index
returns the time series index of time
series objects, models. The argument timetk_idx
can be used to return a
special timetk "index" attribute for regularized ts
objects that returns a
non-regularized date / date-time index if present.
Get critical timeseries information: tk_get_timeseries_signature
and tk_get_timeseries_summary
takes an index and provides a time series decomposition and key summary attributes of the index, respectively. The tk_augment_timeseries_signature
expedites adding the time series decomposition to the time series object.
Make a future timeseries: tk_make_future_timeseries
models a
future time series after an existing time series index.
Coercion functions: tk_tbl
, tk_ts
, tk_xts
, tk_zoo
, and
tk_zooreg
coerce time-based tibbles tbl
to and from each of the main
time-series data types xts
, zoo
, zooreg
, ts
, maintaining the time-based
index.
Load libraries and start with some time series data
library(timetk) library(lubridate)
Use the FB time series.
FB_tbl <- FANG %>% filter(symbol == "FB") FB_tbl
Get the timeseries index.
idx <- tk_index(FB_tbl) head(idx)
Get the time series signature from the index, a tibble of decomposed features that are useful for data mining and machine learning.
tk_get_timeseries_signature(idx)
Get the time series summary from the index, a single-row tibble of key summary information from the time series.
# General summary tk_get_timeseries_summary(idx)[1:6] # Frequency summary tk_get_timeseries_summary(idx)[6:12]
Use an index to make a future time series.
holidays <- c("2017-01-02", "2017-01-16", "2017-02-20", "2017-04-14", "2017-05-29", "2017-07-04", "2017-09-04", "2017-11-23", "2017-12-25") %>% ymd() idx_future <- tk_make_future_timeseries( idx, n_future = 366, skip_values = holidays, inspect_weekdays = TRUE) head(idx_future)
tail(idx_future)
Coercion to xts
, zoo
, or ts
is simplified. The data is ordered correctly automatically using the column containing the date or datetime information. Non-numeric columns are automatically dropped with a warning to the user (the silent = TRUE
hides the warnings).
# xts FB_xts <- tk_xts(FB_tbl, silent = TRUE)
# zoo FB_zoo <- tk_zoo(FB_tbl, silent = TRUE)
# ts FB_ts <- tk_ts(FB_tbl, start = 2013, freq = 252, silent = TRUE)
This covers the basics of the timetk
package capabilities. Here's how to get started.
Download development version with latest features:
``` {r, eval = FALSE}
devtools::install_github("business-science/timetk")
_Or, download CRAN approved version_: ```r install.packages("timetk")
A lot of innovative time series and forecasting work is going on that ultimately benefits the community. We'd like to thank the following people and packages that came before timetk
in time series analysis and machine learning.
maltese
: Similar in respect to timetk
in that it enables machine learning-friendly data frame generation exposing a number of critical features that can be used for forecasting.lubridate
: Contains an excellent set of functions to extract components of the date and datetime index.xts
and zoo
: Fundamental packages for working with time series enabling creation of a time series index for ts
class and calculating periodicity. The timetk
package includes a vignette to help users get up to speed quickly:
timetk
timetk
timetk
timetk
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.