Forecasting sales allowing for advertising expenditure
Welcome to the last chapter of the course!
The auto.arima()
function will fit a dynamic regression model with ARIMA errors. The only change to how you used it previously is that you will now use the xreg
argument containing a matrix of regression variables. Here are some code snippets from the video:
> fit <- auto.arima(uschange[, "Consumption"],
xreg = uschange[, "Income"])
> # rep(x, times)
> fcast <- forecast(fit, xreg = rep(0.8, 8))
You can see that the data is set to the Consumption
column of uschange
, and the regression variable is the Income
column. Furthermore, the rep()
function in this case would replicate the value 0.8 exactly eight times for the matrix argument xreg
.
In this exercise, you will model sales data regressed against advertising expenditure, with an ARMA error to account for any serial correlation in the regression errors. The data are available in your workspace as advert
and comprise 24 months of sales and advertising expenditure for an automotive parts company. The plot shows sales vs advertising expenditure.
Think through everything you have learned so far in this course, inspect the advert
data in your console, and read each instruction carefully to tackle this challenging exercise.
This exercise is part of the course
Forecasting in R
Exercise instructions
- Plot the data in
advert
. The variables are on different scales, so usefacets = TRUE
. - Fit a regression with ARIMA errors to
advert
by setting the first argument ofauto.arima()
to the"sales"
column, second argumentxreg
to the"advert"
column, and third argumentstationary
toTRUE
. - Check that the fitted model is a regression with AR(1) errors. What is the increase in sales for every unit increase in advertising? This coefficient is the third element in the
coefficients()
output. - Forecast from the fitted model specifying the next 6 months of advertising expenditure as 10 units per month as
fc
. To repeat 10 six times, use therep()
function insidexreg
like in the example code above. - Plot the forecasts
fc
and fill in the provided code to add an x label"Month"
and y label"Sales"
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Time plot of both variables
autoplot(___, ___)
# Fit ARIMA model
fit <- auto.arima(___[, ___], xreg = ___[, ___], stationary = ___)
# Check model. Increase in sales for each unit increase in advertising
salesincrease <- ___(___)[___]
# Forecast fit as fc
fc <- forecast(___, xreg = ___)
# Plot fc with x and y labels
autoplot(___) + xlab(___) + ylab(___)