<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/forecasting-arimax-model-exercises-part-5/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/05/05/forecasting-arimax-model-exercises-part-5/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-arimax-model-exercises-part-5"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/forecasting_arimax.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-5539" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/05/forecasting_arimax-300x166.png?resize=300%2C166" data-recalc-dims="1"></a></p>
<p>The standard ARIMA (autoregressive integrated moving average) model allows to make forecasts based only on the past values of the forecast variable. The model assumes that future values of a variable linearly depend on its past values, as well as on the values of past (stochastic) shocks. The ARIMAX model is an extended version of the ARIMA model. It includes also other independent (predictor) variables. The model is also referred to as the vector ARIMA or the dynamic regression model.<br>
The ARIMAX model is similar to a multivariate regression model, but allows to take advantage of autocorrelation that may be present in residuals of the regression to improve the accuracy of a forecast.<br>
This set of exercises provides a practice in using the <code>auto.arima</code> function from the <code>forecast</code> package to make forecasts with the ARIMAX model. A function from the <code>lmtest</code> package is also used to check the statisical significance of regression coeffcients.<br>
The exercises make use of the <code>Icecream</code> dataset from the <code>Ecdat</code> package. The dataset contains the following variables:</p>
<ul>
<li> ice cream consumption in the USA (in pints, per capita),</li>
<li> average family income per week (in USD),</li>
<li> price of ice cream (per pint), and</li>
<li> average temperature (in Fahrenheit).</li>
</ul>
<p>The number of observations is 30. They correspond to four-weekly periods in the span from March 18, 1951 to July 11, 1953 (<a target="_blank" rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/04/Icecream.csv">download here</a>).</p>
<p>For other parts of the series follow the tag <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/tags/forecasting">forecasting</a>.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/05/05/forecasting-arimax-model-exercises-part-5-solutions">here</a>. </p>
<p><strong>Exercise 1</strong><br>
Load the dataset, and plot the variables <code>cons</code> (ice cream consumption), <code>temp</code> (temperature), and <code>income</code>.</p>
<p><strong>Exercise 2</strong><br>
Estimate an ARIMA model for the data on ice cream consumption using the <code>auto.arima</code> function. Then pass the model as input to the <code>forecast</code> function to get a forecast for the next 6 periods (both functions are from the <code>forecast</code> package).</p>
<p><strong>Exercise 3</strong><br>
Plot the obtained forecast with the <code>autoplot.forecast</code> function from the <code>forecast</code> package.</p>
<p><strong>Exercise 4</strong><br>
Use the <code>accuracy</code> function from the <code>forecast</code> package to find the mean absolute scaled error (MASE) of the fitted ARIMA model.</p>
<p><strong>Exercise 5</strong><br>
Estimate an extended ARIMA model for the consumption data with the temperature variable as an additional regressor (using the <code>auto.arima</code> function). Then make a forecast for the next 6 periods (note that this forecast requires an assumption about the expected temperature; assume that the temperature for the next 6 periods will be represented by the following vector: <code> fcast_temp &lt;- c(70.5, 66, 60.5, 45.5, 36, 28)</code>).<br>
Plot the obtained forecast.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/05/05/forecasting-arimax-model-exercises-part-5/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-arimax-model-exercises-part-5"></aside>
<div><strong>Learn more</strong> about Forecasting in the online course <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/product/time-series-analysis-and-forecasting-in-r/">Time Series Analysis and Forecasting in R</a>. In this course you will learn how to:
<ul>
<li>A complete introduction on Forecasting</li>
<li>Work thru an exponentional smoothing instruction</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 6</strong><br>
Print summary of the obtained forecast. Find the coefficient for the temperature variable, its standard error, and the MASE of the forecast. Compare the MASE with the one of the initial forecast.</p>
<p><strong>Exercise 7</strong><br>
Check the statistical significance of the temperature variable coefficient using the the <code>coeftest</code> function from the <code>lmtest</code> package. Is the coefficient statistically significant at 5% level?</p>
<p><strong>Exercise 8</strong><br>
The function that estimates the ARIMA model can input more additional regressors, but only in the form of a matrix. Create a matrix with the following columns:</p>
<ol>
<li> values of the temperature variable,</li>
<li> values of the income variable,</li>
<li> values of the income variable lagged one period,</li>
<li> values of the income variable lagged two periods.</li>
</ol>
<p>Print the matrix.<br>
Note: the last three columns can be created by prepending two <code>NA</code>‘s to the vector of values of the income variable, and using the obtained vector as an input to the <code>embed</code> function (with the <code>dimension</code> parameter equal to the number of columns to be created).</p>
<p><strong>Exercise 9</strong><br>
Use the obtained matrix to fit three extended ARIMA models that use the following variables as additional regressors:</p>
<ol>
<li>temperature, income,</li>
<li>temperature, income at lags 0, 1, </li>
<li>temperature, income at lags 0, 1, 2.</li>
</ol>
<p>Examine the summary for each model, and find the model with the lowest value of the Akaike information criterion (AIC).<br>
Note that the AIC cannot be used for comparison of ARIMA models with different orders of integration (expressed by the middle terms in the model specifications) because of a difference in the number of observations. For example, an AIC value from a non-differenced model, ARIMA (p, 0, q), cannot be compared to the corresponding value of a differenced model, ARIMA (p, 1, q).</p>
<p><strong>Exercise 10</strong><br>
Use the model found in the previous exercise to make a forecast for the next 6 periods, and plot the forecast. (The forecast requires a matrix of the expected temperature and income for the next 6 periods; create the matrix using the <code>fcast_temp</code> variable, and the following values for expected income: <code>91, 91, 93, 96, 96, 96</code>).<br>
Find the mean absolute scaled error of the model, and compare it with the ones from the first two models in this exercise set.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Forecasting: Linear Trend and ARIMA Models Exercises (Part-2)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/15/forecasting-linear-models-exercises-part-2/">Forecasting: Linear Trend and ARIMA Models Exercises (Part-2) </a></li>
<li><a title="Forecasting: Multivariate Regression Exercises (Part-4)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/05/01/forecasting-multivariate-regression-exercises-part-4/">Forecasting: Multivariate Regression Exercises (Part-4) </a></li>
<li><a title="Forecasting: Exponential Smoothing Exercises (Part-3)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/17/forecasting-exponential-smoothing-exercises-part-3/">Forecasting: Exponential Smoothing Exercises (Part-3) </a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/05/05/forecasting-arimax-model-exercises-part-5/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-arimax-model-exercises-part-5"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Xt7xzPDe7FA"></a></div>