1

我正在尝试使用 ggfortify 的自动绘图来估计 95% 的置信区间,但是我无法实现。如果我使用预测包并以 95% CI 提前 3 周进行预测,则效果很好。见下文:

wt <- structure(list(DOC = c(3, 10, 17, 24, 31, 38, 45, 52, 59, 66, 
73, 80, 87, 94, 101), AvgWeight = c(1, 1.66666666666667, 2.06666666666667, 
2.275, 3.83333333333333, 6.2, 7.4, 8.5, 10.25, 11.1, 13.625, 
15.2, 16.375, 17.8, 21.5), PondName = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Pond01", class = "factor"), 
    SampleDate = structure(c(1182585600, 1183190400, 1183795200, 
    1184400000, 1185004800, 1185609600, 1186214400, 1186819200, 
    1187424000, 1188028800, 1188633600, 1189238400, 1189843200, 
    1190448000, 1191052800), class = c("POSIXct", "POSIXt"))), .Names = c("DOC", 
"AvgWeight", "PondName", "SampleDate"), row.names = c(NA, 15L
), class = "data.frame")  

wt$SampleDate <- as.Date(wt$SampleDate)  
wt
     DOC AvgWeight PondName SampleDate
1    3  1.000000   Pond01 2007-06-23
2   10  1.666667   Pond01 2007-06-30
3   17  2.066667   Pond01 2007-07-07
4   24  2.275000   Pond01 2007-07-14
5   31  3.833333   Pond01 2007-07-21
6   38  6.200000   Pond01 2007-07-28
7   45  7.400000   Pond01 2007-08-04
8   52  8.500000   Pond01 2007-08-11
9   59 10.250000   Pond01 2007-08-18
10  66 11.100000   Pond01 2007-08-25
11  73 13.625000   Pond01 2007-09-01
12  80 15.200000   Pond01 2007-09-08
13  87 16.375000   Pond01 2007-09-15
14  94 17.800000   Pond01 2007-09-22
15 101 21.500000   Pond01 2007-09-29

    library(forecast)
    library(ggfortify)
    library(ggplot2)
    library(xts) 

pond <- as.xts(wt$AvgWeight,order.by=seq(as.Date("2007-06-23"), by=7, len=15))
pond 
d.arima <- auto.arima(pond)
d.arima;fitted(d.arima)
d.forecast <- forecast(d.arima, level = c(95), h = 3)
d.forecast


 > d.forecast
    Point Forecast    Lo 95    Hi 95
106           25.2 23.14483 27.25517
113           28.9 24.30450 33.49550
120           32.6 24.91026 40.28974

当我绘制一个预测包对象(在这种情况下为 d.forecast)时,我得到了正确的 95% 置信区间

autoplot(d.forecast,ts.colour='dodgerblue',predict.colour='green',
predict.linetype='dashed',ts.size=1.5,conf.int.fill='azure3') +
 xlab('DOC') + ylab('AvgWeight-grs') + theme_bw()

在此处输入图像描述

但如果我这样做:

ggfortify::autoplot(d.arima,predict=predict(d.arima,n.ahead=3),conf.int=TRUE,predict.alpha = 
0.05,fitted.colour="green",
predict.colour='red',predict.linetype='solid')

在此处输入图像描述

它默认为 80% 置信区间。我试图在 predict() 中设置置信度,但它被忽略了。我还尝试了 autoplot() 中的关卡,但也没有用。问题:如何使用 ggfortify 的 autoplot 实现不同程度的置信度?在这里使用 predict.alpha 是正确的还是用于预测点估计的 alpha 颜色?另外,是否可以将拟合的绿线连接到预测的红线?

4

1 回答 1

0

我很惊讶你没有收到错误并且正在看到你正在展示的情节。不幸的是我无法重现你的情节

  1. 当我加载ggfortify 之后 forecast,我找不到使用forecasts的方法autoplot。那是因为ggfortify实际上并没有 export autoplot; 相反,它会覆盖autoplot. forecast所以ggfortify::autoplot(...)不应该工作,应该抛出错误

    错误:“autoplot”不是从“命名空间:ggfortify”导出的对象

  2. 也没有orpredict的论点,所以我不确定它来自哪里。autoplot.forecastautoplot.ts

你有什么理由要使用forecast and ggfortify吗?为什么不坚持使用forecastsautoplot进行绘图?这是一个基于您的示例数据的示例d.arima

autoplot(forecast(d.arima)) + theme_minimal()

在此处输入图像描述

亮区和暗区分别对应于 95% 和 80% CI。

使用forecast_8.10和进行测试ggfortify_0.4.7

于 2020-03-02T05:19:45.403 回答