<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-multivariate-regression-exercises-part-4/"></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/01/forecasting-multivariate-regression-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-multivariate-regression-exercises-part-4"> 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://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/forecasting-part-4-4.png"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-5477" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/forecasting-part-4-4-150x150.png?resize=150%2C150" data-recalc-dims="1"></a></p>
<p>In the previous exercises of this series, forecasts were based only on an analysis of the forecast variable. Another approach to forecasting is to use external variables, which serve as predictors. This set of exercises focuses on forecasting with the standard multivariate linear regression.<br>
Running regressions may appear straightforward but this method of forecasting is subject to some pitfalls:<br>
(1) a basic difficulty is selection of predictor variables (which is more of an art than a science),<br>
(2) a possible problem is the dependence of a forecast on assumptions about expected values of predictor variables,<br>
(3) another problem can arise if autocorrelation is present in regression residuals (it implies, among other things, that not all information, which could be used for forecasting, was retrieved from the forecast variable).<br>
This set of exercises allow to practice in using the <code>regsubsets</code> function from the <code>leaps</code> package to run sets of regressions, making and plotting forecast from a multivariate regression, and testing residuals for autocorrelation (which requires the <code>lmtest</code> package to be installed). The model selection is based on the Bayesian information criterion (BIC).<br>
The exercises make use of the quarterly data on light vehicles sales (in thousands of units), real disposable personal income (per capita, in chained 2009 dollars), civilian unemployment rate (in percent), and finance rate on personal loans at commercial banks (24 month loans, in percent) in the USA for 1976-2016 from FRED, the Federal Reserve Bank of St. Louis database (<a target="_blank" rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/04/vehicles.csv">download here</a>).<br>
For other parts of the series follow the tag <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/tags/forecasting">forecasting</a>.<br>
Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/05/01/forecasting-multivariate-regression-exercises-part-4-solutions">here</a>. </p>
<p><strong>Exercise 1</strong><br>
Load the dataset, and plot the <code>sales</code> variable. </p>
<p><strong>Exercise 2</strong><br>
Create the <code>trend</code> variable (by assigning a successive number to each observation), and lagged versions of the variables <code>income</code>, <code>unemp</code>, and <code>rate</code> (lagged by one period). Add them to the dataset.<br>
(Note that the base R libraries do not include functions for creating lags for non-time-series data, so the variables can be created manually).</p>
<p><strong>Exercise 3</strong><br>
Run all possible linear regressions with <code>sales</code> as the dependent variable and the others as independent variables using the <code>regsubsets</code> function from the <code>leaps</code> package (pass a formula with all possible dependent variables, and the dataset as inputs to the function).<br>
Plot the output of the function.</p>
<p><strong>Exercise 4</strong><br>
Note that <code>regsubsets</code> returns only one “best” model (in terms of BIC) for each possible number of dependent variables. Run all regressions again, but increase the number of returned models for each size to 2.<br>
Plot the output of the function.</p>
<p><strong>Exercise 5</strong><br>
Look at the plots from the previous exercises and find the model with the lowest value of BIC. Run a linear regression for the model, save the result in a variable, and print its summary.</p>
<p><strong>Exercise 6</strong><br>
Load an additional <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/04/vehicles_assumptions.csv">dataset</a> with assumptions on future values of dependent variables. Use the dataset and the model obtained in the previous exercise to make a forecast for the next 4 quarters with the <code>forecast</code> function (from the package with the same name). Note that the names of the lagged variables in the assumptions data have to be identical to the names of the corresponding variables in the main dataset.<br>
Plot the summary of the forecast.</p>
<p><strong>Exercise 7</strong><br>
The <code>plot</code> function does not automatically draw plots for forecasts obtained from regression models with multiple predictors, but such plots can be created manually. As the first step, create a vector from the <code>sales</code> variable, and append the forecast (mean) values to this vector. Then use the <code>ts</code> function to transform the vector to a quarterly time series that starts in the first quarter of 1976.</p>
<p><strong>Exercise 8</strong><br>
Plot the forecast in the following steps:<br>
(1) create an empty plot for the period from the first quarter of 2000 to the fourth quarter of 2017,<br>
(2) plot a black line for the sales time series for the period 2000-2016,<br>
(3) plot a thick blue line for the sales time series for the fourth quarter of 2016 and all quarters of 2017.<br>
Note that a line can be plotted using the <code>lines</code> function, and a subset of a time series can be obtained with the <code>window</code> function.</p>
<p><strong>Exercise 9</strong><br>
Perform the Breusch-Godfrey test (the <code>bgtest</code> function from the <code>lmtest</code> package) to test the linear model obtained in the exercise 5 for autocorrelation of residuals. Set the maximum order of serial correlation to be tested to 4.<br>
Is the autocorrelation present?<br>
(Note that the null hypothesis of the test is the absence of autocorrelation of the specified orders).</p>
<p><strong>Exercise 10</strong><br>
Use the <code>Pacf<code> function from the <code>forecast</code> package to explore autocorrelation of residuals of the linear model obtained in the exercise 5. Find at which lags partial correlation between lagged values is statistically significant at 5% level.<br>
Residuals can be obtained from the model using the <code>residuals</code> function.
<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: 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 title="Multiple Regression (Part 3) Diagnostics" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics/">Multiple Regression (Part 3) Diagnostics </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/01/forecasting-multivariate-regression-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-multivariate-regression-exercises-part-4"> 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/kFWbuelqcQU"></a></div></code></code>