OK, so in this video, we are going to look at how to apply auto arima to champagne sales.
Now, before we begin this lecture, I want to give you an opportunity to stop this video and to try
to do this on your own.
Since you've already learned all the code you need to complete this exercise, you technically do not
need my help.
So if you want to treat this as an exercise, please download the data set from the euro given in this
notebook.
But after you get the euro, close the notebook and build a model on your own.
OK, so if you want to try this as an exercise, please do so now.
Otherwise, let's continue.
OK, so let's skip the imports, since you've seen this all before.
The next step is to update stat's bottles.
Again, this is because the newest version differs from the defaults on CoLab.
The next step is to install PMed Yarema.
The next step is to download our CSFI.
The next step is to read in our CCV using PDF that read CSFI.
The next step is to call the dot head so we remember what our data looks like.
Since the column name in our data frame is kind of user unfriendly, we're going to rename it to sales.
The next step is to plot our data, so you remember what it looks like.
So, again, there's a pretty strong seasonal component, but the amplitude of the cycles seems to grow
over time.
The next step is to compute the log, transform.
Now, we're not going to go through every possible option in this script, but please try anything you're
curious about.
The next step is to set our index frequency two months.
The next step is to split our data into train and test.
The next step is to create indexes to our data frame for both the train and test sets.
The next step is to import PMed Yarema.
The next step is to run auto arima on the log sales will set Seasonale equals true to start using stepwise
search.
OK, so this gives us a Remar one zero one zero one one.
The next step is to plot our insane plan out of sample predictions, since I've shown you this code
before.
I'm not going to explain it again.
OK, so we see that our forecasts very closely matches the true Time series.
The next step is to compute the R-squared of our prediction.
OK, so we get about zero point nine five, which makes sense.
Now, we know that it's not entirely necessary to use a seasonal model when you have seasonal data.
So the next thing I want to try is to simply do a grid search over non seasonal Arima models to see
what we can find.
Not that I've said seasonal to false in step.
Wise to false.
OK, so our best model is a Rhema 12 one one.
This makes sense since the seasonal period of the time series is 12 months.
Now, just note that in this case, using PMed, Yarema is kind of unnecessary if you want to do a grid
search and your goal is to get the model with the best test predictions.
It's probably better to just use the method we learned earlier, which was walk forward validation.
So why are we doing grid search instead of the default stepwise search?
Well, I encourage you to try this code with step by step.
True.
So you can see that you can't just trust this method blindly.
The next step is to plot our predictions using the same code as before.
OK, so our predictions are pretty close, as you might expect.
The next step is to compute the R-squared.
So the R-squared of our non seasonal model is zero point nine seven, which is a bit better than before.
The next step in the script is to choose our Arima model manually by using the Akef and the.
So let's start by plotting the akef.
So what we can see from this is that there is a highly significant point around like 12, which makes
sense, but note that it's not very common to see models with high IQ values.
The next step is to plot the pickoff now you might be wondering, what is this strange argument method?
The answer is that, as you recall, finding the chief involves doing linear regression.
It turns out that there are multiple ways to do this.
Unfortunately, with the default method, you're going to get an invalid answer, which is not useful.
Using ordinarily squares seems to fix the problem.
So that's what we'll use.
OK, so we see that we get statistically significant values up to like 12.
This makes sense and it gives us a reason to set the 12.
Another interesting result from our grid search was that it shows D equals one, which means that it
took the first difference.
That, to me is a bit strange because there's no obvious trend in the data.
If we plot the first difference of the log sales, we see that it doesn't really accomplish much, the
TIME series is still seasonal, just with a different pattern.
The next step is to plot the akef of the difference in log sales.
So interestingly, this looks very similar to the Akef of the original Time series.
The next step is to plot the scale of the difference in log sales.
Again, it essentially tells us the exact same thing as the chief of the original Time series.
So one way to justify taking the difference of our Time series is to use the ADF test.
So let's run the ATF test on the log sales.
OK, so as you recall, the P-value is the second element in this tuple, using a significant threshold
of five percent, we would not reject the null hypothesis.
Therefore, we would not say that this time series is stationary.
The next step is to try the AIDS test on the difference in log sales.
So this time, our P-value is far below the significant threshold of five percent in this case, we
do reject the null hypothesis and we say that the TIME series is stationary.
So this gives us some justification for setting vehicles one.
So in this next block of code, I've copied a function written earlier which plots in Narima forecast,
so please check the previous lectures if you want to learn what this code does.
The next step is to train our Arima model using the parameters suggested by the pilots we just saw.
So I've chosen P equals 12 since that's the maximum significant leg in the life.
I've chosen vehicles, one based on the results of our ADF test.
And I've also chosen Q equals two for good measure.
OK, so this seems like a pretty good fit.
The next step is to check the R-squared.
So the R-squared is about zero point nine eight seven, which is better than before.
OK, so I hope this served as an educational example of how to apply the tools we learned about to New
Time series data.
Now, in practice, you'd want to use all the tools you learned previously, such as walk forward validation
and so forth.
So although we got a better test R-squared on this particular split, it's not necessarily true that
this model is better when you consider other split's.