Dev P
Dev P

Reputation: 449

Plot forecast and actual values

I all. I need some help from statistics expert. I have made a simple arima forecast for few values in as. But I have taken a subset of values in train_as.

Now is there a way to plot both actual values and forecasted values here. Like the actual values in 2019 is 4,8,12,16 and the forecast is 9,10,11,12. Can we plot this?

as <- data.frame(a=c(1,2,3,4,2,4,6,8,4,8,12,16))
train_as <- as[c(1:8),]
a1 <- ts(train_as,start = c(2017,1),end = c(2017,8),frequency = 4)
fit_arima <-auto.arima(a1, trace= TRUE, ic ="aic")
print(summary(fit_arima))
checkresiduals(fit_arima)
fcst <- forecast(fit_arima,h=4)
autoplot(fcst,include = 8)

Upvotes: 2

Views: 9303

Answers (2)

Rob Hyndman
Rob Hyndman

Reputation: 31800

This is easy to do using the forecast package with the autolayer() function.

library(forecast)
library(ggplot2)

as <- data.frame(a = c(1, 2, 3, 4, 2, 4, 6, 8, 4, 8, 12, 16))

# Convert to a time series
y <- ts(as$a, start = 2017, frequency = 4)

# Split in two
a1 <- subset(y, end = 8)
a2 <- subset(y, start = 9)

# Fit model
fit_arima <- auto.arima(a1, ic = "aic")

# Compute forecasts
fcst <- forecast(fit_arima, h = 4)

# Plot forecasts and test set
autoplot(fcst) + autolayer(a2)

Created on 2019-12-02 by the reprex package (v0.3.0)

Upvotes: 2

s__
s__

Reputation: 9485

You can try something like this, first you create your test dataset:

test_as <- as[c(9:12),]

Now a data.frame to plot, you can see the real data, the time, and the predicted values (and their ICs) that should be with the same length of the time and real data, so I pasted a NAs vector with length equal to the difference between the real data and the predicted, and the predicted (same for the ICs). Note the time is generated by quarter with zoo package:

library(zoo)
df <-
data.frame(real = as$a,
           pred = c(rep(NA,length(as$a)-length(data.frame(fcst)[,1])),data.frame(fcst)[,1]),
           time =  zoo::as.yearqtr(seq(as.Date("2017/1/1"), as.Date("2019/12/1"), by = "quarter"), format = "%Y-%m-%d"),
           Lo80 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,2])),data.frame(fcst)[,2]),
           Hi80 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,3])),data.frame(fcst)[,3]),
           Lo95 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,4])),data.frame(fcst)[,4]),
           Hi95 =c(rep(NA,length(as$a)-length(data.frame(fcst)[,5])),data.frame(fcst)[,5])) 

Now you can plot it:

library(ggplot2)
ggplot(df, aes(time, pred, group = 1)) +
  geom_line() +
  geom_line(aes(time, real, group = 1), color = "red")+
  geom_ribbon(aes(time, ymin = Lo95, ymax = Hi95), fill = "red", alpha = 0.25) +
  geom_ribbon(aes(time, ymin = Lo80, ymax = Hi80), fill = "red", alpha = 0.25) +
  theme_light()

enter image description here

Upvotes: 0

Related Questions