TSstudio 0.1.2

[This article was first published on Rami Krispin, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Happy to announce the release of TSstudio 0.1.2 to CRAN. The TSstudio package provides tools for descriptive and predictive analysis of time series data, utilizing the visualization enegin of the plotly package and forecasting models from the forecast, forecastHybrid and bsts packages.


Install the stable version from CRAN:


or install the development version from Github:

# install.packages("devtools")

New features

The new release includes new set of functions for forecasting automation with the use of backtesting and ‘horse race’ approach, forecast visualization, quantile plot of time series data, and new datasets.

The new release includes set of new functions for data visualization and as well for forecasting. In addition, there is major improvment in some of the existing functions with the ability to use multiple inputs (ts, xts, zoo, data.frame, data.table, and tbl) and new color palettes.


The ts_backtesting function provides you the ability to train, test and evaluate multiple models with the use of backtesting approach. This allows automating the forecasting process by running a ‘horse race’ between different models or approaches while testing them over multiple periods to evaluate their performance over time. The example below demonstrated the use of the function to forecast the monthly consumption of natural gas in the US for the next five years (or 60 months). By default, the function is testing seven different models, using expended window over six periods:


##  The USgas series is a ts object with 1 variable and 225 observations
##  Frequency: 12 
##  Start time: 2000 1 
##  End time: 2018 9
usgas_backtesting <- ts_backtesting(USgas,
                                 periods = 6, # Set the number of periods in the backtesting
                                 window_size = 12, # Set the length of the testing set
                                 h = 60, # Set the horizon of the final forecast
                                 plot = FALSE,
                                 error = "MAPE" # Set the error matrix
##    Model_Name  avgMAPE    sdMAPE  avgRMSE   sdRMSE
## 1  auto.arima 6.148333 0.9886843 184.1150 15.82619
## 2      hybrid 6.691667 0.6159681 196.2350 16.66238
## 3      nnetar 7.195000 1.1111571 255.5100 23.01805
## 4       tbats 7.468333 0.7641313 208.5633 17.60290
## 5        bsts 7.581667 1.8553966 215.4200 44.64997
## 6 HoltWinters 7.736667 0.6686903 218.5017 19.05705
## 7         ets 7.778333 1.7040824 202.5533 45.95689

By default, the model which performed the best in the testing sets, according to the error criteria (RMSE or MAPE), will be selected by the function to forecast the series. In the case of the USgas series, as you can see in the leaderboard above, the auto.arima model achieved the best results and therefore, the function will select this model to forecast the future values:

# Plotting the results

In addition, the output of this function includes all the output from the trained models and their forecasts. For instance, you can check the residuals of the selected model:


Or pull the forecast of the hybrid model:


A short video of this function is available here

The ts_seasonal function

The ts_seasonal function is now supported data frame objects (data.frame, data.table, and tbl), in addition to the time series objects (ts, xts, and zoo). The function has three modes, which can define with the type argument:

  • normal - subsetting and plotting the series by its full cycle (or year), this allows identifying if there is a repeated pattern in the series from year to year
ts_seasonal(USgas, type = "normal")
  • cycle - plotting each one of the cycle units over time.
ts_seasonal(USgas, type = "cycle")
  • box - box plot of each cycle unit
ts_seasonal(USgas, type = "box")

Alternatively, you can set the type = "all" to plot all the three options together

ts_seasonal(USgas, type = "all")

In addition, it is possible to modify the palettes of the plot with any of the RColorBrewer and viridis packages palettes. For example, in the plot below, the colors of the normal mode (first plot) is set inferno palette with the palette_normal argument, and the colors of the second and third plots are set to Accent palette with the palette argument:

ts_seasonal(USgas, type = "all", palette_normal = "inferno", palette = "Accent")

Note that the colors in the first plot scaled according to the order of the year, and the colors of the months in the second and third plots aligned to each other.

Quantile plot for time series data

Another new feature is the ts_quantile function for plotting quantile plot of time series data, using different aggregations methods. This function, for now, support only objects with time or date object as index (e.g., xts, zoo, data.frame, data.table, tbl) with a frequency of half-hour and above. In the example below, demonstrate the use of the function with a the UKgrid dataset, which represents the UK national electricity transmission system dataset:


# Exracting the net demand data
nd <- extract_grid(type = "tbl", columns = "ND",start = 2011, end = 2017) 

## # A tibble: 6 x 2
##   TIMESTAMP              ND
## 1 2011-01-01 00:00:00 34606
## 2 2011-01-01 00:30:00 35092
## 3 2011-01-01 01:00:00 34725
## 4 2011-01-01 01:30:00 33649
## 5 2011-01-01 02:00:00 32644
## 6 2011-01-01 02:30:00 32092

By default, the function plot the quantile of the series according to the frequency units of the series. In the case of the UKgrid dataset above, since the series has a frequency of half-hour (or 48), the plot represents 48 quantiles, one for each half-hour of the day. The period argument provides a subset view of the series using an upper-level frequency (e.g., for half-hourly frequency a view by day of the week, month, quarter or year). This could be very useful when multi seasonality patterns exist in the data. For example, you can view the quantile of the series by the day of the week:

            period = "weekdays", 
            title = "UK Natioanl Grid Net Demand by Weekdays",
            n = 2 # Set the number of rows in the plot


The ts_heatmap funcation is now supports multiple time series classes, including data frame objects(as long as there is a time or data column in the data), supporting data with daily frequency and lower (weekly, monthly, etc.):

UKgrid_daily <- extract_grid(type = "data.frame", 
                             columns = "ND",
                             aggregate = "daily")

##    TIMESTAMP      ND
## 1 2011-01-01 1671744
## 2 2011-01-02 1760123
## 3 2011-01-03 1878748
## 4 2011-01-04 2076052
## 5 2011-01-05 2103866
## 6 2011-01-06 2135202

Similarly to the ts_seasonl function, the ts_heatmap supprorting color palettes from the RColorBrewer and viridis packages:

ts_heatmap(USgas, color = "Reds")

The ts_lags function

The ts_lags function, is now allowing to select a set of lags (either a sequence of specific lags). By default the function is plotting the first 12 lags:


Alternatively, you can select a specific set of lags, such as the seasonal lags:

ts_lags(USgas, lags = c(12, 24, 36, 48))

Road map

The scope of the current release was on forecasting automation, extending the support of the package functions for additional data inputs (such as data.frame, data.table, and tbl).

The focus of the next release is mainly on:

  • Automation of the forecasting process - extending the functionality of the ts_backtesting function
  • Extending the support of the existing function (when applicable) for the Facebook prophet model and the new time series object tsibble

To leave a comment for the author, please follow the link and comment on their blog: Rami Krispin.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)