ggfortify自动绘图置信区间水平

问题描述 投票:1回答: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

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

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

但是如果我这样做:

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

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

r ggplot2 xts forecast ggfortify
1个回答
0
投票

我很惊讶您没有出现错误,并且看到了所显示的图。不幸的是,我无法复制您的剧情

  1. 当我加载ggfortify 之后 forecast时,我找不到使用forecastautoplot的方法。这是因为ggfortify实际上并未导出autoplot;而是从autoplot覆盖forecast方法。因此,ggfortify::autoplot(...)应该不起作用,并且应该引发错误

    错误:“自动绘图”不是从“命名空间:ggfortify”导出的对象

  2. 也没有predictautoplot.forecastautoplot.ts参数,所以我不确定它来自哪里。

您为什么要使用forecast ggfortify?为什么不坚持使用forecastautoplot进行绘图?这是一个基于您的样本数据和d.arima

的示例
autoplot(forecast(d.arima)) + theme_minimal()

enter image description here

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

使用forecast_8.10ggfortify_0.4.7测试。

© www.soinside.com 2019 - 2024. All rights reserved.