ARIMA modeling and forecasting | Time Series in Python Part 2

Data Science Dojo · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

Builds an ARIMA time series model using Python's statsmodels package, predicting and forecasting future timestamps

Full Transcript

[Music] hi welcome back to the stutter science doge a video tutorial series on time series in part 1 we left to that difference in our data to make it more stationary as this is a requirement of many time series models in part 2 we'll take our different stutter and start modeling on it and forecast into the future so what we need to do now is look at the autocorrelation function and partial autocorrelation plots or a CFP ACF for short so these plots help determine the number of order aggressive terms and moving average terms an autoregressive moving average model or to spot the seasonality or periodic trends so explain what I mean by order aggressive and moving average so order aggressive basically is able to forecast the next timestamps value by regressing over the previous values and a moving average is able to forecast the next timestamps value by averaging the previous values so auto regressive integrated moving average model which is the one we're going to use is useful for non stationary data as it allows us to difference the data plus has an additional seasonal differencing parameter for seasonal non stationary data so first let's produce these plots and then I'll explain how to interpret them so we're going to produce our first plots going to be a CF pot I know different style and we're going to produce a nice PACAF blood as well okay let's have a look at these okay so the ACF and the PCA F plot includes a 95% confidence interval Ben so anything outsides this kind of um you know shaded band here it's a statistically significant correlation so if we see a significant spike at lag X in the ACF that helps us determine the number of moving average terms and if we see a significant spike at lag X in the PA CF that helps us determine the number of autoregressive terms so here in the ACF plot we see a spike at about one here so that will turn help us determine the number of moving average terms and if we look at the PA CF we can see two major spikes here so one added about five and one I think in about thirteen so that will help us determine the number of AR terms for now we're just going to go ahead with a model that only includes about five AR terms and see how that goes so now that we have looked at our ACF NP ACF plots we can now build our ARIMA model that takes into account the amount of terms that we need to use and just keep in mind this models also going to infer the frequency so we need to make sure the gaps between our date times are we've always done modeling okay so let's call this um ARMA one model [Music] and I'm going to play our Reema model [Music] we're gonna give it our data and the order of terms is gonna be our AR ma terms and differencing so first of all put in number of AR terms here two rounds of differences or two sets of differences and one ma term here and I'm going to put an option here or specified trans parameters as false this kind of ensures if you said it as true ensures that things are kept stationary but you'll see why I have to set this as false later on in the video tutorial series when we talk about issues with our model and we're going to print the summary of our model um so we can get a few details little serious let's do that now we'll explain how to interpret the summary as well okay let's go ahead and run this so we've had a look at our autocorrelation and partial autocorrelation now we've built our model alright so this shows us a summary of our model here we want to probably look at the p values for our coefficient of our terms here so our our terms and our ma terms here so looking at this is is useful because if the p value for say an AR or an MA coefficient is greater than 0.05 which is our significance level I kind of cut off them up to determine whether it's significant or not then we can say it's probably not significant enough of a term to keep in the model so how you look at this we might want to remodel and include only this they are our ma term here as the other ones might not be necessary but for the purpose of demonstration let's go ahead and then we'll discuss issues with our model later on so next step is we want to predict the next 5 hours on the next 5 timestamps ahead which is our test holdout set so a comment these out so they're not too much of a distraction [Music] and we're given our model and we use the predictor function here and I'm going to give it the time stamps from the last time stamp was basically 6:00 p.m. on the 6th of February 9 2019 so I'm going to take the time stamps into the future from the last time stamp which is from 7:00 p.m. to 11:00 p.m. on the 5:00 time stamps ahead so let's do this I'm also going to make this type levels and you'll see why later on we need to specify that ok let's run this was it going to print these predictions obviously [Music] okay let's run this [Music] all right so here are our forecasts or our predictions for the next five hours ahead we can kind of see going this sort of downward trajectory here so it predicts that sentiment is likely to go normal to turn in a kind of bad direction but what we need to keep in mind is for time series we need to back transform our D difference predicted values with our D differenced or original actual values this is automatically done when predicting so when we specified type levels here we kind of wanted to predict on the original scale not on the d difference to kind of scale nevertheless we're going to demonstrate how to DEET rants form say two rounds of differences using cumulative sum when you've been given original data so the first step in that is we want to basically get the second round of differences back to the first round of differences and then take that D different starter and get it back to the original so kind of like it's two-step process so let's go ahead and do it through this so as I said we want to get our second round of differences back to the first round so I'll just call this undo one take our second round of differences and we're going to fill in any missing values just so they didn't cause the same problems okay and the next step we want to get that difference data or one difference data back to the original so this one do you [Music] fill in any missing values okay now we can compare these so the difference or they're going to be very small differences between our original data and our undifferenced utter we're going to round it up to six places after the decimal point I mean our values only come in six places after the decimal point anyway so they're not very big differences to care about but they're essentially the same when we do round it up six places past the decimal point so let's have a look at this and we'll just look at our original data first to about six places after the decimal point I want to see if it's equal to the same as on difference to total six places left of this one point and just for our iron sanity check we can just look at the first few values for the original values and compare it with the D difference values to see there on pop all the same you okay let's have a look at this okay cool so it's come back as true as if there are though differences or real differences between them so our on different starter and our original values are on par and you can have your own kind of sanity check here to make sure to say the first few examples are definitely the same now that we have modelled the data and made our predictions we'll compare our predictions against the actual values in part three thanks for watching if you found this video tutorial useful give us a like otherwise you can check out our other videos at data science dojo tutorials [Music]

Original Description

In part 2 of this video series, learn how to build an ARIMA time series model using Python's statsmodels package and predict or forecast N timestamps ahead into the future. Now that we have differenced our data to make it more stationary, we need to determine the Autoregressive (AR) and Moving Average (MA) terms in our model. To determine this, we look at the Autocorrelation Function plot and Partial Autocorrelation Function plot. 0:00 – Introduction 1:12 – ACF and PACAF plots 2:50 – Building the ARIMA model 5:53 – Forecasting 8:27 – Comparison Watch Part 1 Here: https://tutorials.datasciencedojo.com/time-series-python-reading-data/ Watch Part 3 Here: https://tutorials.datasciencedojo.com/mean-absolute-error-forecast/ Code, R & Python Script Repository: https://code.datasciencedojo.com/rebeccam/tutorials/tree/master/Time%20Series Packages Used: pandas matplotlib StatsModels statistics -- Learn more about Data Science Dojo here: https://datasciencedojo.com/data-science-bootcamp/ Watch the latest video tutorials here: https://tutorials.datasciencedojo.com/ See what our past attendees are saying here: https://datasciencedojo.com/bootcamp/reviews/#videos -- Like Us: https://www.facebook.com/datasciencedojo/ Follow Us: https://twitter.com/DataScienceDojo Connect with Us: https://www.linkedin.com/company/data-science-dojo Also, find us on: Instagram: https://www.instagram.com/data_science_dojo/ Vimeo: https://vimeo.com/datasciencedojo #arimamodeling #arimaforecasting #timeseries
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Data Science Dojo · Data Science Dojo · 0 of 60

← Previous Next →
1 Feature Engineering and Predictive Modeling | Data Analytics with R and Azure ML | Community Webinar
Feature Engineering and Predictive Modeling | Data Analytics with R and Azure ML | Community Webinar
Data Science Dojo
2 Data Exploration and Visualization | Beginning Azure ML | Part 3
Data Exploration and Visualization | Beginning Azure ML | Part 3
Data Science Dojo
3 Reading External Data Sources | Beginning Azure ML | Part 2
Reading External Data Sources | Beginning Azure ML | Part 2
Data Science Dojo
4 Importing Data, Accessing, & Creating a New Experiment | Beginning Azure ML | Part 1
Importing Data, Accessing, & Creating a New Experiment | Beginning Azure ML | Part 1
Data Science Dojo
5 Casting Columns & Renaming Columns | Beginning Azure ML | Part 4
Casting Columns & Renaming Columns | Beginning Azure ML | Part 4
Data Science Dojo
6 Scrub Missing Values & Project Columns | Beginning Azure ML | Part 5
Scrub Missing Values & Project Columns | Beginning Azure ML | Part 5
Data Science Dojo
7 Feature Engineering & R Script | Beginning Azure ML | Part 6
Feature Engineering & R Script | Beginning Azure ML | Part 6
Data Science Dojo
8 Building Your First Model | Beginning Azure ML |  Part 7
Building Your First Model | Beginning Azure ML | Part 7
Data Science Dojo
9 Run and Fine-Tune Multiple Models | Beginning Azure ML | Part 8
Run and Fine-Tune Multiple Models | Beginning Azure ML | Part 8
Data Science Dojo
10 Deploying Your First Predictive Model As a Web Service | Beginning Azure ML | Part 9
Deploying Your First Predictive Model As a Web Service | Beginning Azure ML | Part 9
Data Science Dojo
11 Using R API to Obtain Predictions From Your Web Service Beginning Azure ML | Part 10
Using R API to Obtain Predictions From Your Web Service Beginning Azure ML | Part 10
Data Science Dojo
12 Using Python API to Obtain Predictions From Your Web Service | Beginning Azure ML | Part 11
Using Python API to Obtain Predictions From Your Web Service | Beginning Azure ML | Part 11
Data Science Dojo
13 Twitter Sentiment Analysis | Natural Language Processing | Community Webinar
Twitter Sentiment Analysis | Natural Language Processing | Community Webinar
Data Science Dojo
14 Listening to the Melody of the Universe (LIGO Gravitational Waves Presentation) | Community Webinar
Listening to the Melody of the Universe (LIGO Gravitational Waves Presentation) | Community Webinar
Data Science Dojo
15 David Wechsler on the Impact of Data Science Bootcamp
David Wechsler on the Impact of Data Science Bootcamp
Data Science Dojo
16 Andrew Choi on the Impact of Data Science Bootcamp
Andrew Choi on the Impact of Data Science Bootcamp
Data Science Dojo
17 Microsoft's Software Engineer Shares Her Experience with Data Science Bootcamp
Microsoft's Software Engineer Shares Her Experience with Data Science Bootcamp
Data Science Dojo
18 Michael DAndrea on the Impact of Data Science Bootcamp
Michael DAndrea on the Impact of Data Science Bootcamp
Data Science Dojo
19 Data Driven Decision-Making with Data Science Bootcamp: Artem Kopelev's Revelation
Data Driven Decision-Making with Data Science Bootcamp: Artem Kopelev's Revelation
Data Science Dojo
20 Learn the Fundamentals of Data Science: Srinivas Rao's Experience with Data Science Bootcamp
Learn the Fundamentals of Data Science: Srinivas Rao's Experience with Data Science Bootcamp
Data Science Dojo
21 Re-Learning Data Science with Data Science Bootcamp: Analyst's Revelation
Re-Learning Data Science with Data Science Bootcamp: Analyst's Revelation
Data Science Dojo
22 Scale R to Big Data with Hadoop & Spark | Community Webinar
Scale R to Big Data with Hadoop & Spark | Community Webinar
Data Science Dojo
23 Enhancing Skills with Data Science Bootcamp: Sharon Lane-Getaz's Revelation
Enhancing Skills with Data Science Bootcamp: Sharon Lane-Getaz's Revelation
Data Science Dojo
24 Ryan DeMartino on the Impact of Data Science Bootcamp
Ryan DeMartino on the Impact of Data Science Bootcamp
Data Science Dojo
25 Software Engineer at Microsoft Reveals About His Experience with Data Science Bootcamp
Software Engineer at Microsoft Reveals About His Experience with Data Science Bootcamp
Data Science Dojo
26 Wade Wimer on the Impact of Data Science Bootcamp
Wade Wimer on the Impact of Data Science Bootcamp
Data Science Dojo
27 Analyzing Data with Data Science Bootcamp: Hannah Richta's Revelation
Analyzing Data with Data Science Bootcamp: Hannah Richta's Revelation
Data Science Dojo
28 Applying Data Science Skills to The Current Role with Bootcamp: Marcos Lacayo's Revelation
Applying Data Science Skills to The Current Role with Bootcamp: Marcos Lacayo's Revelation
Data Science Dojo
29 Lance Milner on the Impact of Data Science Bootcamp
Lance Milner on the Impact of Data Science Bootcamp
Data Science Dojo
30 Deloitte's Data Scientist Revelation: Learning Predictive Analytics with Data Science Bootcamp
Deloitte's Data Scientist Revelation: Learning Predictive Analytics with Data Science Bootcamp
Data Science Dojo
31 Rajesh Patil's Experience at Data Science Bootcamp As an Enterprise Architect
Rajesh Patil's Experience at Data Science Bootcamp As an Enterprise Architect
Data Science Dojo
32 Michael Atlin on the Impact of Data Science Bootcamp
Michael Atlin on the Impact of Data Science Bootcamp
Data Science Dojo
33 Amina Tariq's In-Person Experience at Data Science Bootcamp
Amina Tariq's In-Person Experience at Data Science Bootcamp
Data Science Dojo
34 Ceo's Revelation about Data Science Bootcamp
Ceo's Revelation about Data Science Bootcamp
Data Science Dojo
35 Stephen Miller Describes His Experience at Data Science Dojo's Bootcamp
Stephen Miller Describes His Experience at Data Science Dojo's Bootcamp
Data Science Dojo
36 Kevin Hillaker on the Impact of Data Science Bootcamp
Kevin Hillaker on the Impact of Data Science Bootcamp
Data Science Dojo
37 Marko Topalovic's Experience with Data Science Bootcamp
Marko Topalovic's Experience with Data Science Bootcamp
Data Science Dojo
38 Text Analytics With Python, Cognitive Services & PowerBI | Data Analytics | Community Webinar
Text Analytics With Python, Cognitive Services & PowerBI | Data Analytics | Community Webinar
Data Science Dojo
39 Unisys Manager's Revelation: Visualizing Real Time Data with Data Science Bootcamp
Unisys Manager's Revelation: Visualizing Real Time Data with Data Science Bootcamp
Data Science Dojo
40 Learn Data Mining with Data Science Bootcamp: Ryan LaBrie's Revelation
Learn Data Mining with Data Science Bootcamp: Ryan LaBrie's Revelation
Data Science Dojo
41 Vang Xiong on the Impact of Data Science Bootcamp
Vang Xiong on the Impact of Data Science Bootcamp
Data Science Dojo
42 Data Scientist's Experience at Our Data Science Bootcamp
Data Scientist's Experience at Our Data Science Bootcamp
Data Science Dojo
43 Alejandro Wolf Yadlin on the Impact of Data Science Bootcamp
Alejandro Wolf Yadlin on the Impact of Data Science Bootcamp
Data Science Dojo
44 Introduction To Titanic Kaggle Competition | Part 1
Introduction To Titanic Kaggle Competition | Part 1
Data Science Dojo
45 Learning How to Code in R with Data Science Bootcamp: Priscilla Mannuel's Revelation
Learning How to Code in R with Data Science Bootcamp: Priscilla Mannuel's Revelation
Data Science Dojo
46 Andrew Berman On Why Data Science Bootcamp Is Better Fit for Him
Andrew Berman On Why Data Science Bootcamp Is Better Fit for Him
Data Science Dojo
47 How To Do Titanic Kaggle Competition in R | Part 3.1
How To Do Titanic Kaggle Competition in R | Part 3.1
Data Science Dojo
48 How to do the Titanic Kaggle competition in R | Part 3.1
How to do the Titanic Kaggle competition in R | Part 3.1
Data Science Dojo
49 Delve Deeper into Data Science with Data Science Bootcamp
Delve Deeper into Data Science with Data Science Bootcamp
Data Science Dojo
50 Bank of America Data Scientist Reveals His Experience of Data Science Bootcamp
Bank of America Data Scientist Reveals His Experience of Data Science Bootcamp
Data Science Dojo
51 Shaena Montanari on the Impact of Data Science Bootcamp
Shaena Montanari on the Impact of Data Science Bootcamp
Data Science Dojo
52 Types of Sampling | Introduction to Data Mining | Part 12
Types of Sampling | Introduction to Data Mining | Part 12
Data Science Dojo
53 Sampling for Data Selection | Introduction to Data Mining | Part 11
Sampling for Data Selection | Introduction to Data Mining | Part 11
Data Science Dojo
54 Data Aggregation | Introduction to Data Mining | Part 10
Data Aggregation | Introduction to Data Mining | Part 10
Data Science Dojo
55 Data Cleaning | Introduction to Data Mining | Part 9
Data Cleaning | Introduction to Data Mining | Part 9
Data Science Dojo
56 Missing & Duplicated Data | Introduction to Data Mining | Part 8
Missing & Duplicated Data | Introduction to Data Mining | Part 8
Data Science Dojo
57 Data Noise | Introduction to Data Mining | Part 7
Data Noise | Introduction to Data Mining | Part 7
Data Science Dojo
58 Graph and Ordered Data | Introduction to Data Mining | Part 5
Graph and Ordered Data | Introduction to Data Mining | Part 5
Data Science Dojo
59 Document Data & Transaction Data | Introduction to Data Mining | Part 4
Document Data & Transaction Data | Introduction to Data Mining | Part 4
Data Science Dojo
60 Data Quality | Introduction to Data Mining | Part 6
Data Quality | Introduction to Data Mining | Part 6
Data Science Dojo

Related Reads

Chapters (5)

Introduction
1:12 ACF and PACAF plots
2:50 Building the ARIMA model
5:53 Forecasting
8:27 Comparison
Up next
Best AI Tools for Social Media Marketing in 2026 (Beginner-Friendly AI Tools)
Pin Generator
Watch →