Stationary vs Non-Stationary Time Series Data
R
# Generate time vector t <- 1:300 # Generate stationary time series set.seed (123) y_stationary <- rnorm ( length (t), mean = 0, sd = 1) y_stationary <- y_stationary / max (y_stationary) # Generate non-stationary time series with a trend set.seed (456) y_trend <- cumsum ( rnorm ( length (t), mean = 0, sd = 4)) + t / 100 y_trend <- y_trend / max (y_trend) # Set up a more attractive layout for the plots par (mfcol = c (2, 2), mar = c (4, 4, 2, 1)) # Plot stationary time series plot (t, y_stationary, type = 'l' , col = 'darkgreen' , xlab = "Time (t)" , ylab = "Y(t)" , main = "Stationary Time Series" , cex.main = 1.2, cex.lab = 1.1) # ACF for stationary time series acf_y_stationary <- acf (y_stationary, lag.max = length (y_stationary), plot = FALSE ) plot (acf_y_stationary, main = 'ACF - Stationary Time Series' , cex.main = 1.2, cex.lab = 1.1) # Plot non-stationary time series with trend plot (t, y_trend, type = 'l' , col = 'steelblue' , xlab = "Time (t)" , ylab = "Y(t)" , main = "Non-Stationary Time Series with Trend" , cex.main = 1.2, cex.lab = 1.1) # ACF for non-stationary time series with trend acf_y_trend <- acf (y_trend, lag.max = length (y_trend), plot = FALSE ) plot (acf_y_trend, main = 'ACF - Non-Stationary Time Series with Trend' , cex.main = 1.2, cex.lab = 1.1) |
Output:
In this code generates and plots two time series: one stationary (y_stationary) and one non-stationary with a trend (y_trend).
- Stationary Time Series:
- The first plot depicts a stationary time series generated with random normal noise.
- A stationary time series has a constant mean, variance, and autocorrelation structure over time.
- ACF (Autocorrelation Function) for Stationary Time Series:
- The second plot shows the autocorrelation function (ACF) for the stationary time series.
- In a stationary series, the ACF tends to decay rapidly, indicating a lack of long-term dependencies.
- Non-Stationary Time Series with Trend:
- The third plot illustrates a non-stationary time series with a cumulative sum of random normal noise and a linear trend.
- Non-stationary time series exhibit changing statistical properties, often with trends or seasonality.
- ACF for Non-Stationary Time Series with Trend:
- The fourth plot displays the autocorrelation function (ACF) for the non-stationary time series with a trend.
- In a non-stationary series, the ACF may show slower decay, reflecting the persistence of dependencies.
- Stationarity is evident in the first set of plots where the mean, variance, and autocorrelation structure remain relatively constant over time.
The second set of plots demonstrates how the ACF can be a useful diagnostic tool for identifying stationary and non-stationary time series. The decay pattern in the ACF provides insights into the series’ temporal dependencies.
Stationarity of Time Series Data using R
In this article, we will discuss about Stationarity of Time Series Data, its characteristics, and types, why stationarity matters, and How to test it using R.