Well, hello, Internet and welcome to part six of my Time series forecasting tutorial series.
And this part of the tutorial I'm going to show you how to make predictions using seasonal arema.
And I have a lot to do, so let's get into it.
Okay, so previously we were trying to force seasonal data, which is why we were getting imperfect
results in this tutorial.
What I'm going to do is I'm going to see how the world is doing in regards to bringing down CO2 emissions.
And I'm going to make some predictions about what we can expect with our future.
Now, like always, all of these imports are exactly the same as what we have used previously, and
everything that is here is available in a link in the description, so you can go ahead and download
everything.
Now with seasonal Arima, we will be using an additional set of parameters that describe seasonal components
of our models.
So we are going to have a seasonal regression Differencing moving averages and then we're going to have
an additional parameter M which is going to represent the rows in each seasonal cycle.
So for example, if we have monthly data with a yearly seasonal cycle, M in that situation would be
equal to 12.
All right.
So going to import some different libraries that we have used in the past.
And right here what I did was I went and imported some information on CO2 emissions going way back into
the 1960s.
And we're going to see if we are bringing down these emissions or not.
So what I'm going to do is I'm going to say CO2 DF And I'm specifically going to look at CO2.
DF From CO2.
DF With the indexes above of 1960.
All right.
So I'm just going to shorten the amount of data that we are looking at.
Then I'm going to need to come in here and set my frequency and this will be CO2 as frequency and I'm
going to set it as month start or just simply the month.
And then what I'm going to do is I'm going to have some data here that is going to be missing.
And I'm just going to say to take whatever the value just previous to it was, which shouldn't hurt
our results in any regards.
And then I'm going to come in and plot this out so we can get an understanding of what type of data
we're dealing with and whether CO2 emissions are going down or not.
As you can see, they are not going down and this is going up pretty far.
How much into the future is this CO2?
So this is going from 1960, right up until October 2020.
So that's where we are right now.
In regards to CO2 emissions, No, they are not going down.
Now, what I want to do is verify that there is a seasonal component and I'm going to be using seasonal
decompose for this.
So our result is going to be seasonal decompose and throw in CO2 dataframe and the model is going to
be additive because it is not growing in a multiple, as far as I can tell.
What I mean by that is basically the emissions are going up about the same amount.
So this would have to be bigger and then this would have to be bigger than that for this to be multiplicative.
And we can plot this out and we can definitely see that, yes, there is a seasonal component here.
There's a lot of residual mess.
Here's our trend line and here's our original data.
All right.
So now to start making our predictions, I'm going to use Auto.arima and this is Pyramid Arima like
we've used previously.
So I'm going to say go and get our dataframe with CO2 data inside of it.
Inside of it.
And then I'm going to say, yes, it is seasonal.
I'm going to say that this is in basically yearly increments, 12 representing the months.
Then we're going to have trace is true so that we can see everything being put together and a summary
of this and you can see it's going through all the results and looking for the lowest AIC value.
Don't worry about this.
This is just warnings that we do not need to worry about.
And you can see here are our final results.
Took quite a long time to get them and there is our final results and then we can plug these in and
test them.
By the way, if you want to go and just get rid of these warnings that don't matter at all, just go
and put this inside of there and they will be all gone.
Okay.
All right.
So now what we want to do is come in.
And get the length of our data so we can decide how much we're going to use for training and how much
we are going to use for testing.
So our training part is going to be equal to Co two data frame and our location.
We're going to go from the start up to 80% of 729, which is going to work out to 583.
And then the testing data frame.
If you didn't watch the previous parts of this tutorial, well chances are you already left.
But if you haven't, you should definitely watch it.
Otherwise it's very understandable if you are confused here.
Okay, Now to create our model, we're going to use our seasonal and we're going to get into the X part
in the very last part of this tutorial series here, which is coming up next.
So we're going to pass in training and then we just say order is equal to and we take what it provided.
So what did it say is 013?
So let's just come down here and put 013 in.
Then we're going to have our seasonal part.
So this will be seasonal order is equal to and this is going to be 101 12.
Right?
Like that.
Okay.
So after we go and create our model, we need to fit it.
So I'll say model and fit and then we have to decide where we're starting at.
And that's going to be the length of the training data frame and we will end from the training data
frame plus our test data frame.
And then we have to subtract one from there because it's zero indexed, then we're going to type in
prediction is our result and call predict on it.
So from our start to our end and then our type in this situation is going to be linear and this is just
means we're looking for a linear prediction.
While if we went inside of here and instead typed in levels, that means to predict the original using
endogenous variables which we do not have any of.
So we're going to have this be linear, and then I'm going to name this prediction and then we can say
test DF and plot.
Say that we want this in the legend and fig size is going to be, I'll just make this 16 eight and then
we can go and plot our prediction as well.
And legend true.
Whoops, put legend and if we run it, you can see we're right in line with what we have here.
So looking pretty good.
We can then come in and test our prediction and I'm going to use the mean squared error in this situation.
Let's first off command and check on our mean so Co two DF and mean that comes out to 356 because it's
good to actually make predictions based off of what we have.
I'm going to get the square root of this mean squared error and our prediction and see how much we're
off by.
And you can see we're off by 3.43, which is actually pretty good.
So now let's come down here and let's predict our future.
So I'm going to say model is equal to seasonal.
Arima I'm just going to use the X version here in the next part of the tutorial.
I'll get more specifically into what the X part is just outside influence.
That's all it is.
Okay, so we're using the same order, same seasonal order also.
And if you ever have any trouble getting the same results as me, just go to GitHub, get the thing.
Everything's free.
Of course, download it and run it and I need to fit this as well.
And let's say we want to look into the future to see if our Co two levels are supposed to fall.
Okay, here is our data frame and then we'll say length Co two DF And let's say that we want to jump
into the future by 12 months type.
I'll make this levels this time.
You actually see, you normally get the same results from either one and I will rename this future.
I can then go and get our test data and plot that out.
Legend.
True and fig size equal to 16 and eight.
And then also plot our future legend.
Also true.
Forgot to put the equals sign here.
And if we run it, you can see that the prediction is that CO2 levels are going to continue to rise
into the next year because they rise every single year.
So everything is as we would predict.
And then in the next part of the tutorial, what I'm going to do is I'm going to talk about seasonal
Arima with a focus on exogenous regressor variables, which just means outside influences and how they
can maybe or maybe not help us make even more accurate predictions.
So like always, please leave your questions and comments down below otherwise till next time.