R Tutorial: Forecasting with time series

preview_player
Показать описание

---

Now that we have built the model, we need to forecast the future values of our data! That's why we are here!

The best part about time series models using information from the past is that you could forecast the future. If we know the pattern on how an observation at one time period is related to an observation at another time period, then we can recursively forecast the future using this pattern (assuming it stays the same).

What time-series models are doing is essentially finding the pattern - or signal - in your data. They are then extrapolating this signal repeatedly over time. Now extrapolation sounds really bad, so instead, we call it forecasting. Of course, this forecast is not going to be perfect. In your data, you have both signal and noise. The remaining noise is what helps us estimate confidence intervals around our forecasts.

The forecast function in R makes forecasting time series models easy to do. You need to put in your model object as well as how many time periods into the future you would like to forecast - that is the "h = " option. As you can see here, we want to forecast our time series model 22 observations into the future.

Let's visualize this forecast. You could just use the plot function, but I want to compare the forecast with the validation set. First, create an xts object from the forecast, called the "mean" attribute of your forecast object, then the plot function for your validation and the lines for your forecast.

Hmmm... That's an interesting looking forecast.

There are a lot of ways of measuring accuracy in time series models. Two of the most common ways of doing so are the Mean Absolute Error, called the MAE, and the Mean Absolute Percentage Error called the MAPE.

The MAE is the average measure of how far away, in absolute terms, your prediction is from the actual value. The best part of the MAE is that it is easily measured in the scale of your data. However, that is also the downside. How so? What if I told you my prediction was off on average by $100,000? If I was predicting US GDP, then you would be very impressed! If I was predicting the price of my hamburger dinner last night, then you would think I have no clue what I am doing! Without a reference, an average prediction error might be out of place.

That is what the MAPE is for! The MAPE is the average measure of how far away in absolute PERCENTAGE terms, your prediction is from the actual value. This makes your prediction not dependent on scale.

The forecast object that we created for the mountain region, called forecast_M_t, has many components, not just the forecast. To get the forecast, we need to ask for the mean object with the $.

To make it easier to compare the forecast with the validation data set we also use the as dot numeric functions. Once we do that we can easily calculate both the MAE and MAPE.

Let's see how we did! Well, for the MAE we were only off by around 199 products sold on average in the mountain region! Is that good? Not sure. Let's check the MAPE. It looks like we were off by a little more than 9.5% on average. That might put things in better context.

Now let's see how well you can predict the metropolitan region!
Рекомендации по теме
Комментарии
Автор

🚩





🐂

lapro