Making Time Series Stationary Made Easy with auto_stationarize()

rtip
healthyrts
timeseries
Author

Steven P. Sanderson II, MPH

Published

October 18, 2023

Introduction

When working with time series data, one common challenge is dealing with non-stationary data. Non-stationary time series can be a headache for analysts, but fear not, because we have a handy tool to make your life easier. Say hello to the auto_stationarize() function from the {healthyR.ts} package.

What’s in the Box?

Before we get into the nitty-gritty of how this function works, let’s take a look at its syntax:

auto_stationarize(.time_series)

The .time_series parameter should be a vector or a time series object. This function’s primary goal is to attempt to stationarize your time series data. But what does that mean, and why is it important?

Stationarity: The Why and the How

Stationarity is a crucial concept in time series analysis. A stationary time series is one whose statistical properties, like mean, variance, and autocorrelation, don’t change over time. Dealing with stationary data is much simpler because many time series models assume stationarity.

Now, here’s the magic of auto_stationarize(): it automatically handles stationarity for you.

The Swiss Army Knife of Time Series

This function is like a Swiss Army knife for your time series data. It checks if your data is already stationary using the Augmented Dickey-Fuller test. If it is, great, you get your original time series back.

But what if it’s not? Well, that’s where the real fun begins.

Transformations Galore

If your time series isn’t stationary, auto_stationarize() goes the extra mile to make it so. It attempts a series of transformations until it succeeds. Here’s the process:

  1. Augmented Dickey-Fuller Test: First, the function runs the Augmented Dickey-Fuller test to determine if your time series is stationary. If it’s already stationary, you’re done.

  2. Logarithmic Transformation: If the test suggests your data isn’t stationary, the function tries a logarithmic transformation. This transformation can be helpful when dealing with data that grows exponentially over time.

  3. Differencing: If logarithmic transformation doesn’t do the trick, the function resorts to differencing. Differencing involves subtracting each value from its previous value, effectively converting your data into the change between time periods.

What You Get

If auto_stationarize() succeeds in making your data stationary, it returns a list with two valuable elements:

  1. stationary_ts: This is your shiny, new stationary time series, ready for analysis.

  2. ndiffs: This little number tells you the order of differencing applied to make your data stationary. It’s a useful piece of information if you need to understand how your data was transformed.

Examples

Let’s see some examples.

library(healthyR.ts)

auto_stationarize(AirPassengers)
The time series is already stationary via ts_adf_test().
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432
auto_stationarize(BJsales)
The time series is not stationary. Attempting to make it stationary...
Differencing of order 1 made the time series stationary.
$stationary_ts
Time Series:
Start = 2 
End = 150 
Frequency = 1 
  [1] -0.6 -0.1 -0.5  0.1  1.2 -1.6  1.4  0.3  0.9  0.4 -0.1  0.0  2.0  1.4  2.2
 [16]  3.4  0.0 -0.7 -1.0  0.7  3.7  0.5  1.4  3.6  1.1  0.7  3.3 -1.0  1.0 -2.1
 [31]  0.6 -1.5 -1.4  0.7  0.5 -1.7 -1.1 -0.1 -2.7  0.3  0.6  0.8  0.0  1.0  1.0
 [46]  4.2  2.0 -2.7 -1.5 -0.7 -1.3 -1.7 -1.1 -0.1 -1.7 -1.8  1.6  0.7 -1.0 -1.5
 [61] -0.7  1.7 -0.2  0.4 -1.8  0.8  0.7 -2.0 -0.3 -0.6  1.3 -1.4 -0.3 -0.9  0.0
 [76]  0.0  1.8  1.3  0.9 -0.3  2.3  0.5  2.2  1.3  1.9  1.5  4.5  1.7  4.8  2.5
 [91]  1.4  3.5  3.2  1.5  0.7  0.3  1.4 -0.1  0.2  1.6 -0.4  0.9  0.6  1.0 -2.5
[106] -1.4  1.2  1.6  0.3  2.3  0.7  1.3  1.2 -0.2  1.4  3.0 -0.4  1.3 -0.9  1.2
[121] -0.8 -1.0 -0.8 -0.1 -1.5  0.3  0.2 -0.5 -0.1  0.3  1.3 -1.1 -0.1 -0.5  0.3
[136] -0.7  0.7 -0.5  0.6 -0.3  0.2  2.1  1.5  1.8  0.4 -0.5 -1.0  0.4  0.5

$ndiffs
[1] 1

The function attempted to stationarize the BJsales data set, let’s take a visuali look at it before and after, we will also use the adf_test() function on it before and after.

plot(BJsales)

ts_adf_test(BJsales)
$test_stat
[1] -2.110919

$p_value
[1] 0.5301832
stationary_time_series <- auto_stationarize(BJsales)
The time series is not stationary. Attempting to make it stationary...
Differencing of order 1 made the time series stationary.
plot(stationary_time_series$stationary_ts)

Try It Yourself

The best way to grasp the power of auto_stationarize() is by trying it yourself. Install the {healthyR.ts} package, load your time series data, and give it a whirl. The ease and simplicity of making your time series stationary with just one function call will leave you impressed.

Conclusion

In the world of time series analysis, making your data stationary is a crucial step. The auto_stationarize() function from the {healthyR.ts} package takes the headache out of this process. Whether you’re dealing with financial data, weather patterns, or any other time series, this function is your trusty companion.

So, what are you waiting for? Transform your non-stationary time series into a stationary one with ease, thanks to auto_stationarize(). Your future self will thank you for it.

Happy coding and data analysis!