How to Fit a TBATS Model in R

Spread the love

Time series forecasting is an important aspect of data science and has broad applications ranging from finance and economics to healthcare and natural resource management. While traditional methods like ARIMA have been widely used for time series forecasting, more advanced methods are continuously being developed to better model complex seasonal patterns and other underlying structures in time series data. One such modern technique is the TBATS model, which stands for Trigonometric Seasonal, Box-Cox Transformation, ARMA errors, Trend and Seasonal components.

In this article, we will discuss the underlying concepts behind TBATS and how to fit a TBATS model using the R programming language. We’ll take a step-by-step approach, covering:

  1. What is a TBATS Model?
  2. Installing Necessary R Packages
  3. Preparing Your Data
  4. Exploratory Data Analysis (EDA)
  5. Fitting a TBATS Model
  6. Model Evaluation
  7. Forecasting Future Values
  8. Additional Tuning and Advanced Topics

1. What is a TBATS Model?

TBATS is an acronym that explains the major components of the model:

  • Trigonometric Seasonal: Captures seasonality with Fourier series.
  • Box-Cox Transformation: Stabilizes variance.
  • ARMA Errors: Captures autoregressive and moving average errors.
  • Trend: Captures both deterministic and stochastic trend.
  • Seasonal Components: Captures multiple seasonalities.

TBATS excels in handling multiple seasonalities, even when the seasonal periods are very large or non-integer.

2. Installing Necessary R Packages

Before we can fit a TBATS model, we need to install the forecast package in R, which provides the tbats() function.


Load the package:


3. Preparing Your Data

Assuming you have your time series data in a CSV file named “timeseries_data.csv”, you can read it into an R dataframe like this:

data <- read.csv("timeseries_data.csv")

For this example, let’s assume the data contains a single column value that represents the time series values.

4. Exploratory Data Analysis (EDA)

It’s a good practice to visualize the data before fitting any model:

plot(data$value, type = "l")

Look for any patterns, trends, or seasonalities.

5. Fitting a TBATS Model

Fitting a TBATS model is as simple as calling the tbats() function:

fit <- tbats(data$value)

6. Model Evaluation

After fitting the model, it’s important to evaluate its performance. This can be done through plots and statistical tests.

Residual Plot:


Statistical Tests:

You can use the Box.test() function to perform the Ljung-Box test.

Box.test(fit$residuals, lag = 20, type = "Ljung-Box")

7. Forecasting Future Values

Once the model is fitted and evaluated, we can use it to forecast future values.

forecasts <- forecast(fit, h = 20)

Here, h is the number of future steps to forecast.

8. Additional Tuning and Advanced Topics

Hyperparameter Tuning:

You can modify various hyperparameters like seasonal periods, Box-Cox transformations, etc., by explicitly setting them in the tbats() function:

fit <- tbats(y = data$value, seasonal.periods = c(7, 365.25))

Parallel Processing:

For large data, TBATS can be computationally expensive. You can enable parallel processing by setting the use.parallel argument to TRUE:

fit <- tbats(y = data$value, use.parallel = TRUE)


The TBATS model is a powerful tool for time series forecasting, particularly when you are dealing with complex seasonal patterns and multiple seasonalities. In this article, we’ve walked through how to install the necessary packages, prepare your data, perform exploratory data analysis, fit the TBATS model, evaluate it, and make forecasts. Armed with this knowledge, you can now go ahead and apply TBATS models to your own time series data in R.

Posted in RTagged

Leave a Reply