Well, hello, Internet and welcome to part seven of my Time series forecasting tutorial.
And this part of the tutorial we're going to talk about seasonal Remax and I have a lot to do, so let's
get into it.
Okay, so seasonal Arema X is going to support exogenous regressor variables.
Big word.
Don't worry about it.
You'll understand and it'll completely make sense.
Okay, so you have an dodginess variables and what they are is they are dependent on many outside influences.
So for example, the cost of a tomato is based on the weather, pests, cost of fuel, etcetera.
That is an dodginess variables.
What we are considering is exogenous variables and they are not affected by outside influences and are
either completely or are largely fixed in the model.
An example you will see in this video is the effect of holidays on the price of oil.
Another could be something like the skill a farmer has in producing a certain profit based off of their
ability to judge upcoming demand weather, how to control pests and etcetera.
Okay, so let's just jump into an example here.
All right.
So what we will be studying is the price of Brent oil.
So this is per barrel and of course everything's available in the description and on GitHub.
So you can go and download all the data that I am using here largely that I created from scratch.
All right.
So what I'm going to do here first is I'm going to get data between 1993 and 1995 for no particular
reason, just looked like good data to use.
All right.
And how we are going to get it is I am going to define that.
I want using our index all of our data from 1993.
And why don't I just go and copy this and paste this inside of here?
And I'm also going to look for all of our data in regards to price of oil up to 1995.
All right.
So this is the time frame that we are going to be working with.
Now, what I want to do is I want to change my frequency here to day, which is based off of the data
that we have.
There's also going to be a holiday column inside of here, because what we're analyzing is do travel
holidays cause the price of oil to go up.
So as you can see here, what we have are our dates.
We have the price of a barrel of oil and whether it was a holiday or not.
So on this, this is actually representing July 4th.
That was the period I had to get.
And if it is a holiday, it will have a one.
Otherwise it will have a zero.
This is the price.
This is the date.
Okay.
So this is what our data looks like in its pure form.
So what I want to do is I want to fill in all my missing holiday columns with zeros because I don't
want to have it go and create holidays where there are none.
So I'm just going to say holiday is equal to oil, dataframe and holiday.
And in the situation in which there is no data, I want to fill it with a zero.
Otherwise I'm going to say that I want any other missing data to be filled with whatever just came before
it.
All right.
Just as we have done many times before.
Now I can come in here and plot this out just to look at what we have so far.
So we're looking at the price and we want to plot that.
And I can say fig size is equal to 16 eight.
And there you can see is the rise and fall of our oil prices.
And what we want to do is figure out how we can go and mark it up so that we'll be able to see when
holidays show up.
So let's just get rid of that for now.
I'm going to then say fig axis plot subplots and fig size 16 by eight, then axis is equal to and I'm
just going to copy this right there, paste it in, close that off, throw a semicolon in there so it
doesn't print it twice.
And then I want to go and get all of the holiday information, which once again is marked with a one.
So what I'm going to do is I want all my holidays to be a different color.
Let's just call this color array and it's going to go green.
In, which is normally going to be Easter.
And these are going to be holidays where people normally travel.
And you can guess if you think the price of oil goes up just prior or during those periods of time.
And by the end of the tutorial, you will have an answer of whether it does or not.
I'm going to start off with my index being equal to zero so we can get green first.
Then I'm going to say for X in oil, dataframe, oil, dataframe.
And specifically we're looking for holiday in those situations in which the holiday column has a one
inside of it.
Well, I am going to then print a line that is going to represent that specific holiday and I'll have
this be x equals x and the color is going to be equal to and go grab a color out of my color array and
I'm going to have my line width be shrunk down to 0.5.
And then I'm going to say if color array C index is equal to K, well, in that situation, I'm going
to reset my index back to being equal to zero else I am just going to increment my index by one.
And if I run it, you can see here is our oil price data and you're also going to be able to see where
the different holidays are.
And you can make some judgments here based off of what the data is doing, whether the time in which
we have very large travel holidays is in any way affecting the price of oil.
In some regards, it looks like it might.
In other regards, it looks like it has absolutely no effect.
So what we need to do is figure out if it does.
All right.
So we need to import a couple libraries here and we're going to see if there is a seasonal component
to this data or not.
And we'll use seasonal decompose like we have in the past.
So oil, dataframe and price is what we're looking at, resource plot run that.
And yes, it looks like there is a seasonal component.
Looks like there is a lot of mess down here in the residual and you can see how the trend is changing.
If we want to just show the seasonal component, we can also go seasonal plot and let's go and set our
fig size in this situation to 16 by eight.
And I always forget to put that equal sign in there.
And yes, indeed, it definitely looks like there's a seasonal component here in one regards or another.
Okay, well now it's time for Auto.arima to do its job.
So I'm going to say auto Arima oil, DataFrame and price.
And I'm going to say that this is representing a seven day period and Trace is going to be equal to
true.
And I want to do a summary and it's going to go find the best values here for our season Arima model.
And there it is.
Okay, so this is what we are told to work with.
So let's just copy that and let's come down here and make ourselves some predictions.
So paste that in there.
I am first going to come in and find out how many pieces of data we have here.
So that's 726 And just as we have so many times before, I'm going to say training is equal to oil dataframe
and the location is going to go from the beginning to 580.
And then after that, the test dataframe is going to go from 580 to the end.
Going to create my model here season Arima X oil dataframe price and just go and get put the ordered
data in there exactly as it shows up.
So it's just a whole bunch of ones and seasonal order is going to be equal to 200.
Whoops, 2007.
We need to fit our model.
Then we need to define our starting point whenever we are going to make our prediction.
So that's just going to be the length of our training data frame and is the length of our.
The training data frame plus the length of our test data frame and its zero index.
So minus one, make our prediction which is going to be equal to our result.
Call predict on it from the start to the end.
And I am going to I don't need that.
I am going to rename this prediction and we're going to plot this.
So I'm going to say test data frame and price specifically, let's call plot on it.
Say that we want a legend and our figure size is going to be equal to 16 by eight.
Also going to plot our prediction and legend also true.
And I am also going to go and get that holiday loop we created up here.
Where is it?
Here it is.
So let's go and grab this guy, copy it, paste it in right here, and let's run it.
And you can see here is our prediction.
And it is wildly accurate.
So that is looking very good.
And the main question is, does the holiday data either make the model worse or does it in some way
improve upon it?
Well, what we want to do here first is go and see how accurate this actually is.
And I'm going to use root mean squared for this.
So root, mean squared error and I'll say test DF and price and put it up against our prediction.
And of course don't forget to import it and you can see our prediction is pretty good.
So what would happen if we go and train it using the holiday data?
Will it improve or will it make things worse?
We will find out.
And just because this may not work doesn't mean that extraneous outside information won't help improve
your model in the future.
So don't take this as, Oh, this was terrible or this is wonderful.
And it just depends upon the specific circumstances whether things are going to work or not.
So we're saying here we want to use that holiday data as part of a prediction model, seasonal.
Mark this as true.
And we're going to say seven days and Trace is going to be equal to true.
And we want to do a summary.
All right.
And we got exactly the same results.
So let's go and copy that.
Jump down here, paste that in just so we can refer to it.
All right.
So this time, what we're going to do is we are going to tell it to train and to to to distort the prediction
using this holiday data.
So it's going to distort it, whether it, you know, based off of whether it thinks it will work or
not.
And now you will know if that holiday data does in any way make our predictions more accurate.
Okay.
So holiday, that's what we are saying to weigh in on here.
And the order is going to be exactly the same as before.
And the seasonal order is also going to be exactly the same as before.
That's kind of a sign there.
Let's get our result after we fit our model.
And start is going to be the length of let's just copy this exactly the same and let's just go and copy
the all of this stuff, actually go paste it in there and then we'll just change what we need to change.
Everything here is exactly the same down here where we have prediction start and of course going to
be the same, except I have to put in the exogenous data, which is going to be our test data frame
with specifics on the holiday rename.
And that will let's just change this name and we'll leave it prediction.
That's fine.
And then we want to test our price.
Everything else here is the same and the prediction and the plot are the same.
And now we'll find out if the travel holidays have an effect on oil prices.
And if we run it, you can see that, no, they dramatically messed up our results.
And as far as we can tell, at least with the data we're looking at, the travel holidays have no effect
on the price of oil.
So there you go, guys.
Hopefully you found that.
Useful.
And like always, please leave your questions and comments down below.
Otherwise till next time.