rggplot2xtsforecastggfortify

ggfortify autoplot confidence intervals level


I am trying to estimate 95% confidence intervals using autoplot from ggfortify, however I am not able to achieve it. If I use the forecast package and forecast ahead 3 weeks with 95% CI it works fine. See below:

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

I get the correct 95% confidence intervals when I plot a forecast package object (d.forecast in this case)

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()

enter image description here

But if I do:

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')

enter image description here

It defaults to 80% confidence intervals. I tried to set the level of confidence inside predict() but it gets ignored. I also tried the level inside autoplot() and also did not work. Questions: How can I accomplish different levels of confidence using autoplot from ggfortify? Is it correct to use predict.alpha here or it is intended for the alpha color of the predicted point estimate? Also, is it possible to connect the fitted green line to the predicted red line?


Solution

  • I'm surprised you're not getting an error and are seeing the plots you're showing. Unfortunately I cannot reproduce your plots

    1. When I load ggfortify after forecast, I couldn't find a way to use forecasts autoplot. That's because ggfortify does not actually export autoplot; instead it overwrites the autoplot method from forecast. So ggfortify::autoplot(...) shouldn't work, and should throw the error

      Error: 'autoplot' is not an exported object from 'namespace:ggfortify'

    2. There is also no predict argument of autoplot.forecast or autoplot.ts, so I'm not sure where that comes from.

    Is there a reason why you want to use forecast and ggfortify? Why not stick with forecasts autoplot for plotting? Here is an example based on your sample data and d.arima

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

    enter image description here

    The light and dark areas correspond to the 95% and 80% CIs, respectively.

    Tested using forecast_8.10 and ggfortify_0.4.7.