## 9.3 模型预测

### 示例：美国个人消费及收入

fcast <- forecast(fit, xreg=rep(mean(uschange[,2]),8))
autoplot(fcast) + xlab("年份") +
ylab("百分比变化")+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))

### 示例：耗电量的预测

xreg <- cbind(MaxTemp = elecdaily[, "Temperature"],
MaxTempSq = elecdaily[, "Temperature"]^2,
Workday = elecdaily[, "WorkDay"])
fit <- auto.arima(elecdaily[, "Demand"], xreg = xreg)
checkresiduals(fit)

#>
#>  Ljung-Box test
#>
#> data:  Residuals from Regression with ARIMA(2,1,2)(2,0,0)[7] errors
#> Q* = 28, df = 4, p-value = 1e-05
#>
#> Model df: 10.   Total lags used: 14

autoplot(elecdaily[,'Demand'], series="真实值") +
autolayer(fitted(fit), series="拟合值") +
ylab("") +
ggtitle("日度耗电量（千兆瓦）") +
guides(colour=guide_legend(title=" "))+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))


fcast <- forecast(fit,
xreg = cbind(rep(26,14), rep(26^2,14), c(0,1,0,0,1,1,1,1,1,0,0,1,1,1)))
#> Warning in forecast.forecast_ARIMA(fit, xreg =
#> cbind(rep(26, 14), rep(26^2, : xreg contains different
#> column names from the xreg used in training. Please check
#> that the regressors are in the same order.
autoplot(fcast) + ylab("日度耗电量（千兆瓦）") +
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))

2015年的最初两周的日度耗电量的预测值比较准确。由于2014年底耗电量较低（由于许多人休暑假），导致未来两周的耗电量预测值也较低。