R ggplot中预测数据与实际数据之间的差距
我试图以一种好的ggplot格式绘制一些数据、拟合值和预测,但当我以我认为应该有效的方式绘制数据时,我会在实际数据和预测之间找到一个差距。这个差距毫无意义,但如果它消失了就好了 可以用来重现我的问题的一些R代码是:R ggplot中预测数据与实际数据之间的差距,r,ggplot2,forecast,R,Ggplot2,Forecast,我试图以一种好的ggplot格式绘制一些数据、拟合值和预测,但当我以我认为应该有效的方式绘制数据时,我会在实际数据和预测之间找到一个差距。这个差距毫无意义,但如果它消失了就好了 可以用来重现我的问题的一些R代码是: library(xts) library(tidyverse) library(forecast) dates <- seq(as.Date("2016-01-01"), length = 100, by = "days") realdata <- arima.si
library(xts)
library(tidyverse)
library(forecast)
dates <- seq(as.Date("2016-01-01"), length = 100, by = "days")
realdata <- arima.sim(model = list(ar = 0.7, order = c(1,1,0)), n = 99)
data <- xts(realdata, order.by = dates)
user_arima <- arima(data, order = c(1,1,0))
user_arimaf <- forecast(user_arima)
fits <- xts(user_arimaf$fitted, order.by = dates)
fcastdates <- as.Date(dates[100]) + 1:10
meancast <- xts(user_arimaf$mean[1:10], order.by = fcastdates)
lowercast95 <- xts(user_arimaf$lower[1:10], order.by = fcastdates)
uppercast95 <- xts(user_arimaf$upper[1:10], order.by = fcastdates)
frame <- merge(data, fits, meancast, uppercast95, lowercast95, all = TRUE, fill = NA)
frame <- as.data.frame(frame) %>%
mutate(date = as.Date(dates[1] + 0:(109)))
frame %>%
ggplot() +
geom_line(aes(date, data, color = "Data")) +
geom_line(aes(date, fits, color = "Fitted")) +
geom_line(aes(date, meancast, color = "Forecast")) +
geom_ribbon(aes(date, ymin=lowercast95,ymax=uppercast95),alpha=.25) +
scale_color_manual(values = c(
'Data' = 'black',
'Fitted' = 'red',
'Forecast' = 'darkblue')) +
labs(color = 'Legend') +
theme_classic() +
ylab("some data") +
xlab("Date") +
labs(title = "chart showing a gap",
subtitle = "Shaded area is the 95% CI from the ARIMA")
库(xts)
图书馆(tidyverse)
图书馆(预测)
日期关闭间隙需要在空白区域中的<代码> Meunast>/Cudio>列中提供数据点。我想只使用最后一个“真实”数据点的值是有意义的
# Grab the y-value corresponding to the date just before the gap.
last_data_value = frame[frame$date == as.Date("2016-04-09"), "data"]
# Construct a one-row data.frame.
extra_row = data.frame(data=NA_real_,
fits=NA_real_,
meancast=last_data_value,
uppercast95=last_data_value,
lowercast95=last_data_value,
date=as.Date("2016-04-09"))
# Add extra row to the main data.frame.
frame = rbind(frame, extra_row)