Skip to content

learn-co-students/dsc-testing-for-trends-lab-teacher-onboarding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Testing for Trends - Lab

Introduction

In this lab, you'll practice your knowledge of testing for stationarity.

Objectives

You will be able to:

  • Use rolling statistics as a check for stationarity
  • Use the Dickey-Fuller test and conclude whether or not a dataset is exhibiting stationarity

Importing the data

Let's look at some new data. In this lab, we'll work with a time series in Python by using the popular Air Passengers dataset.

Start by running the cell below to import the necessary libraries.

# Import necessary libraries
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
%matplotlib inline

The dataset is stored in 'passengers.csv'. Import it and view the first five rows.

# Import 'passengers.csv'
data = None

# View the first five rows

Change the 'Month' column over to a datetime type and make sure it is set as the index of the DataFrame.

# Change the type of 'Month' to datetime


# Set 'Month' as the index
# Check the index

Now that we have successfully created a time series, we can use the .plot() method in pandas to visually inspect this time series.

# Plot the time series data 

Wec can see that 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 the Dickey-Fuller test.

Rolling Statistics

Use the .rolling() method to find the 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, the mean is increasing with time and thus, this is not a stationary series.

Dickey-Fuller Test

Use the Dickey-Fuller test to verify your visual result.

from statsmodels.tsa.stattools import adfuller

Summary

In this lab, you checked for the stationarity of a time series in Python. Next, we'll further explore stationarity and how to make sure to make time series stationary!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published