Introduction for the TSstudio Package

Rami Krispin

2018-02-07

Overview

The TSstudio package provides a set of tools for descriptive analysis of a time series data supporting “ts”, “mts”, “zoo” and “xts” objects. That includes rich and interactive visualization plots, based on the engine of the Plotly package, for seasonality, correlations, residuals, and forecasting performance plots. In addition, the TSstudio package supports the time series visualization functions of the MLstudio package.

Installation

Install the stable version from CRAN:

install.packages("TSstudio")

or install the development version from Github:

# install.packages("devtools")
devtools::install_github("RamiKrispin/TSstudio")

Key Features

Visualization of time series objects with the ts_plot function

library(TSstudio)
library(xts)
library(zoo)
library(quantmod)
# Loading the stock price of key technology companies:
tckrs <- c("GOOGL", "FB", "AAPL", "MSFT")
getSymbols(tckrs, 
           from = "2013-01-01",
           src = "yahoo")
## [1] "GOOGL" "FB"    "AAPL"  "MSFT"
# Visual Google closing price since 2013
Google <- GOOGL$GOOGL.Close
class(Google)
## [1] "xts" "zoo"
# Basic plot
ts_plot(Google)
# Adding titles and slider
ts_plot(Google, 
        title = "Google Stock Prices Since 2013",
        Xtitle = "Sourch: Yahoo Finance", 
        Ytitle = "Closing Price in USD",
        slider = TRUE
        )

Available formats

In addition for the “ts” object, the function supports “mts”, “zoo” and “xts” objects, including multiple time series objects:

closing <- cbind(GOOGL$GOOGL.Close, FB$FB.Close, AAPL$AAPL.Close, MSFT$MSFT.Close)
names(closing) <- c("Google", "Facebook", "Apple", "Microsoft")

class(closing)
## [1] "xts" "zoo"
dim(closing)
## [1] 1284    4
# You can plot all the series in one plot using the type option:
ts_plot(closing, 
        title = "Top Technology Companies Stocks Prices Since 2013",
        type = "single")
# or use the default option - "multiple" and plot series on separate plots:
ts_plot(closing,
        title = "Top Technology Companies Stocks Prices Since 2013")

Seasonality analysis

The TSstudio provides a variety of tools for seasonality analysis, currently supporting only monthly or quarterly data. The monthly consumption of natural gas in the US (USgas dataset) is a good example of a seasonal pattern within time series data:

# Load the US monthly natural gas consumption
data("USgas")

class(USgas)
## [1] "ts"
ts_plot(USgas,
        title = "US Natural Gas Consumption",
        Xtitle = "Year",
        Ytitle = "Billion Cubic Feet"
        )

The ts_seasonal function

The ts_seasonal function provides 3 types of seasonal plots:

  1. “normal” - break of a series by year, this allows identifying a seasonal pattern within the year
ts_seasonal(USgas, type = "normal")
  1. “cycle” - break of a series by the cycle units (i.e. months or quarters), it can be used to identify trends and patterns between the cycle units:
ts_seasonal(USgas, type = "cycle")
  1. “box” - for representing the cycle units with a box plot:
ts_seasonal(USgas, type = "box")

Alternatively, setting the type = “all”, print the three options above together in one plot:

ts_seasonal(USgas, type = "all")

The ts_heatmap and ts_surface functions

Another useful visualization tool for seasonality analysis is the ts_heatmap function for time series objects, where the y axis represents the cycle units and x axis represents the years:

ts_heatmap(USgas)

Similarly, the heatmap could be represented by a surface 3d plot:

ts_surface(USgas)

Polar plot

The ts_polar function provides a polar plot demonstrative of time series data where the year is represented by color and the magnitude is represented by the size of the cycle unit layer:

ts_polar(USgas)

Decomposing time series

The ts_decompose function transforms the decompose function output(of the stats package) to an interactive format. In addition to the “additive” and the “multiplicative” options of the original decompose function, there is the “both” option which represents both options side by side:

ts_decompose(USgas, type = "both")

Correlation Analysis

Similarly to the decompose function, the TSstudio transforms the acf and pacf functions into a colorful version with the ts_acf and ts_pacf:

ts_acf(USgas, lag.max = 36)
ts_pacf(USgas, lag.max = 36)

Another intuitive method to identify correlation between the series and its lags is with lag plot:

ts_lags(USgas)

Applications for forecasting

Splitting time series object for training and testing partitions

The TSstudio package provides a set of supporting tools for training and testing forecasting models. The ts_split function splits the series into training and testing partitions based on a given horizon:

# set the forecast horizon for 12 months
h <- 12

# Split the data into training and testing sets (leaving the last 12 months for testing)
split_USgas <- ts_split(USgas, sample.out = h)

train <- split_USgas$train
test <- split_USgas$test


head(train, 5)
## [1] 2510.5 2330.7 2050.6 1783.3 1632.9
head(test, 5)
## [1] 2867.3 2899.4 2326.2 2572.2 1926.7

Residuals analysis

The check_res function visualize the traditional plot of the residuals over time, and the corresponding auto correlation function and the histogram (of the residuals):

library(forecast)
# Building a model on the training set
fit <- auto.arima(train, lambda = BoxCox.lambda(train))

# Checking the residuals
check_res(fit)

Forecast evaluation with the test_forecast

The test_forecast function visualizes the fitted values vs the training partition values and the forecasted values vs the testing partitions values. In addition, the tooltip of the plot provides information about the model performance (MAPE and RMSE of the training and testing partitions):

fc <- forecast(fit, h = h)

test_forecast(actual = USgas, forecast.obj = fc, test = test)

Other utility tools

ts to data frame format

The ts_reshape function transforms time series data (“ts”, “zoo” and “xts” classes) into a friendly data frame format using the years as the columns and the cycle units as rows (currently supporting only monthly and quarterly frequencies):

# Loading the Total US Vehicle Sales data 
data("USVSales")

ts_plot(USVSales, title = "Total US Vehicle Sales",
        Ytitle = "Thousands of Units",
        Xtitle = "Source: U.S. Bureau of Economic Analysis")
head(ts_plot)
##                                                                        
## 1 function (ts.obj, line.mode = "lines", width = 2, dash = NULL,       
## 2     color = NULL, slider = FALSE, type = "multiple", Xtitle = NULL,  
## 3     Ytitle = NULL, title = NULL, Xgrid = FALSE, Ygrid = FALSE)       
## 4 {                                                                    
## 5     `%>%` <- magrittr::`%>%`                                         
## 6     df <- p <- plot_list <- dim_flag <- plot_list <- obj.name <- NULL
USVSales_df <- ts_reshape(USVSales)

library(DT)

datatable(USVSales_df, filter = 'top', options = list(
  pageLength = nrow(USVSales_df), autoWidth = TRUE
))

Converting “zoo” or “xts” objects to “ts” class

The xts_to_ts or the zoo_to_ts functions convert “zoo” or “xts” objects into “ts” class:

# Loading the University of Michigan Consumer Sentiment 
data("Michigan_CS")

Michigan_CS_ts <- xts_to_ts(Michigan_CS)

ts_plot(Michigan_CS, title = "University of Michigan Consumer Sentiment - 'xts' format")
ts_plot(Michigan_CS_ts, title = "University of Michigan Consumer Sentiment - 'ts' format")
class(Michigan_CS)
## [1] "xts" "zoo"
class(Michigan_CS_ts)
## [1] "ts"
head(Michigan_CS)
##          [,1]
## Jan 1980 67.0
## Feb 1980 66.9
## Mar 1980 56.5
## Apr 1980 52.7
## May 1980 51.7
## Jun 1980 58.7
head(Michigan_CS_ts)
##       Jan  Feb  Mar  Apr  May  Jun
## 1980 67.0 66.9 56.5 52.7 51.7 58.7
head(index(Michigan_CS))
## [1] "Jan 1980" "Feb 1980" "Mar 1980" "Apr 1980" "May 1980" "Jun 1980"
head(time(Michigan_CS_ts))
##           Jan      Feb      Mar      Apr      May      Jun
## 1980 1980.000 1980.083 1980.167 1980.250 1980.333 1980.417
periodicity(Michigan_CS)
## Monthly periodicity from Jan 1980 to Dec 2017
frequency(Michigan_CS_ts)
## [1] 12
cycle(Michigan_CS_ts)
##      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1980   1   2   3   4   5   6   7   8   9  10  11  12
## 1981   1   2   3   4   5   6   7   8   9  10  11  12
## 1982   1   2   3   4   5   6   7   8   9  10  11  12
## 1983   1   2   3   4   5   6   7   8   9  10  11  12
## 1984   1   2   3   4   5   6   7   8   9  10  11  12
## 1985   1   2   3   4   5   6   7   8   9  10  11  12
## 1986   1   2   3   4   5   6   7   8   9  10  11  12
## 1987   1   2   3   4   5   6   7   8   9  10  11  12
## 1988   1   2   3   4   5   6   7   8   9  10  11  12
## 1989   1   2   3   4   5   6   7   8   9  10  11  12
## 1990   1   2   3   4   5   6   7   8   9  10  11  12
## 1991   1   2   3   4   5   6   7   8   9  10  11  12
## 1992   1   2   3   4   5   6   7   8   9  10  11  12
## 1993   1   2   3   4   5   6   7   8   9  10  11  12
## 1994   1   2   3   4   5   6   7   8   9  10  11  12
## 1995   1   2   3   4   5   6   7   8   9  10  11  12
## 1996   1   2   3   4   5   6   7   8   9  10  11  12
## 1997   1   2   3   4   5   6   7   8   9  10  11  12
## 1998   1   2   3   4   5   6   7   8   9  10  11  12
## 1999   1   2   3   4   5   6   7   8   9  10  11  12
## 2000   1   2   3   4   5   6   7   8   9  10  11  12
## 2001   1   2   3   4   5   6   7   8   9  10  11  12
## 2002   1   2   3   4   5   6   7   8   9  10  11  12
## 2003   1   2   3   4   5   6   7   8   9  10  11  12
## 2004   1   2   3   4   5   6   7   8   9  10  11  12
## 2005   1   2   3   4   5   6   7   8   9  10  11  12
## 2006   1   2   3   4   5   6   7   8   9  10  11  12
## 2007   1   2   3   4   5   6   7   8   9  10  11  12
## 2008   1   2   3   4   5   6   7   8   9  10  11  12
## 2009   1   2   3   4   5   6   7   8   9  10  11  12
## 2010   1   2   3   4   5   6   7   8   9  10  11  12
## 2011   1   2   3   4   5   6   7   8   9  10  11  12
## 2012   1   2   3   4   5   6   7   8   9  10  11  12
## 2013   1   2   3   4   5   6   7   8   9  10  11  12
## 2014   1   2   3   4   5   6   7   8   9  10  11  12
## 2015   1   2   3   4   5   6   7   8   9  10  11  12
## 2016   1   2   3   4   5   6   7   8   9  10  11  12
## 2017   1   2   3   4   5   6   7   8   9  10  11  12