In this lab, you'll practice your knowledge on testing for stationarity.
You will be able to:
- Check and comment on the stationarity of time series data
- Use rolling statistics as an initial check for stationarity
- Use the Dickey Fuller Test for performing statistical testing for time series stationarity
Let's look at some new data. In this lab, We'll work by creating a time-series object in Python by using the popular Air Passengers dataset.
This dataset is stored as passengers.csv
.
# Import necessary libraries
Import passengers.csv
and view the head.
# Read the dataset 'passengers.csv' and view the head
# Month #Passengers
# 0 1949-01-01 112
# 1 1949-02-01 118
# 2 1949-03-01 132
# 3 1949-04-01 129
# 4 1949-05-01 121
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Month | #Passengers | |
---|---|---|
0 | 1949-01-01 | 112 |
1 | 1949-02-01 | 118 |
2 | 1949-03-01 | 132 |
3 | 1949-04-01 | 129 |
4 | 1949-05-01 | 121 |
Change the Month
column over to a datetime
object and make sure it is set as the index.
# Set month column as a timeseries object, and make it the index
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 144 entries, 0 to 143
Data columns (total 2 columns):
Month 144 non-null datetime64[ns]
#Passengers 144 non-null int64
dtypes: datetime64[ns](1), int64(1)
memory usage: 2.3 KB
# check the head and the new index
# DatetimeIndex(['1949-01-01', '1949-02-01', '1949-03-01', '1949-04-01',
# '1949-05-01', '1949-06-01', '1949-07-01', '1949-08-01',
# '1949-09-01', '1949-10-01',
# ...
# '1960-03-01', '1960-04-01', '1960-05-01', '1960-06-01',
# '1960-07-01', '1960-08-01', '1960-09-01', '1960-10-01',
# '1960-11-01', '1960-12-01'],
# dtype='datetime64[ns]', name='Month', length=144, freq=None)
DatetimeIndex(['1949-01-01', '1949-02-01', '1949-03-01', '1949-04-01',
'1949-05-01', '1949-06-01', '1949-07-01', '1949-08-01',
'1949-09-01', '1949-10-01',
...
'1960-03-01', '1960-04-01', '1960-05-01', '1960-06-01',
'1960-07-01', '1960-08-01', '1960-09-01', '1960-10-01',
'1960-11-01', '1960-12-01'],
dtype='datetime64[ns]', name='Month', length=144, freq=None)
Now that we have successfully created a TS object, we can use simple plot()
function in pandas to visually incpect this time-series.
# Plot the time series data
It is clearly evident that there is an overall increasing trend in the data along with some seasonal variations. However, it might not always be possible to make such visual inferences. Let's reconfirm this here using both rolling statistics and
Use the .rolling()
function to find rolling mean and rolling std with a window of 12 months. Plot the original curve along with the rolling mean and standard error.
#Determine rolling statistics
#Plot rolling statistics
Though the variation in standard deviation is small, mean is clearly increasing with time and thus, this is not a stationary series.
Use the Dickey-Fuller Test to verify your visual result.
from statsmodels.tsa.stattools import adfuller
#Perform Dickey-Fuller test:
# Extract and display test results in a user friendly manner
# Results of Dickey-Fuller Test:
# Test Statistic 0.815369
# p-value 0.991880
# #Lags Used 13.000000
# Number of Observations Used 130.000000
# Critical Value (1%) -3.481682
# Critical Value (5%) -2.884042
# Critical Value (10%) -2.578770
# dtype: float64
Results of Dickey-Fuller Test:
Test Statistic 0.815369
p-value 0.991880
#Lags Used 13.000000
Number of Observations Used 130.000000
Critical Value (1%) -3.481682
Critical Value (5%) -2.884042
Critical Value (10%) -2.578770
dtype: float64
Repeat the previous steps for the NYSE monthly data , stored in "NYSE_monthly.csv".
In this lab, we learnt to check for the stationarity of a time-series object in Python. Next, we'll further explore stationarity and how to make sure to make time series stationary!