## 5.6 回归预测

### 事前预测与事后预测

#### 示例：澳大利亚季度啤酒产量

beer2 <- window(ausbeer, start=1992)
fit.beer <- tslm(beer2 ~ trend + season)
fcast <- forecast(fit.beer)
autoplot(fcast) +
ggtitle("利用线性回归模型预测啤酒产出")+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))

### 基于不同情景的预测

fit.consBest <- tslm(
Consumption ~ Income + Savings + Unemployment,
data = uschange)
h <- 4
newdata <- data.frame(
Income = c(1, 1, 1, 1),
Savings = c(0.5, 0.5, 0.5, 0.5),
Unemployment = c(0, 0, 0, 0))
fcast.up <- forecast(fit.consBest, newdata = newdata)
newdata <- data.frame(
Income = rep(-1, h),
Savings = rep(-0.5, h),
Unemployment = rep(0, h))
fcast.down <- forecast(fit.consBest, newdata = newdata)
fit.consBest <- tslm(
Consumption ~ Income + Savings + Unemployment,
data = uschange)
h <- 4
newdata <- cbind(
Income = c(1, 1, 1, 1),
Savings = c(0.5, 0.5, 0.5, 0.5),
Unemployment = c(0, 0, 0, 0)
) %>%
as.data.frame()
fcast.up <- forecast(fit.consBest, newdata = newdata)
newdata <- cbind(
Income = rep(-1, h),
Savings = rep(-0.5, h),
Unemployment = rep(0, h)
) %>%
as.data.frame()
fcast.down <- forecast(fit.consBest, newdata = newdata)

autoplot(uschange[, 1]) +
xlab('年份')+ylab("美国消费的变化 %") +
autolayer(fcast.up, PI = TRUE, series = "增加") +
autolayer(fcast.down, PI = TRUE, series = "减少") +
guides(colour = guide_legend(title = "不同场景"))+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))

### 预测区间

(5.4)表明，当 $$x$$$$\bar{x}$$ 相差较大时，生成的预测区间会更宽。也就是说，当预测变量值接近样本均值时，模型的预测结果更可靠。

#### 示例

fit.cons <- tslm(Consumption ~ Income, data = uschange)
h <- 4
fcast.ave <- forecast(fit.cons,
newdata = data.frame(Income = rep(mean(uschange[, "Income"]), h)))
fcast.up <- forecast(fit.cons,
newdata = data.frame(Income = rep(5, h)))
autoplot(uschange[, "Consumption"]) +
xlab('年份')+
ylab("美国消费变化率 %") +
autolayer(fcast.ave, PI = TRUE, series = "平均增长") +
autolayer(fcast.up, PI = TRUE, series = "极值增长") +
guides(colour = guide_legend(title = "不同情况"))+
theme(text = element_text(family = "STHeiti"))+
theme(plot.title = element_text(hjust = 0.5))