Googme
Googme

Reputation: 914

How to plot actual vs. predicted values with a confidence interval on time series graph?

I have some difficulties to nicely (gg)plot actual vs. predicted values.

Here some data:

# I use caret in order to split the data into train and test
library(caret)
data(economics) # from caret
library(forecast)
# that is recommended for time series data
timeSlices <- createTimeSlices(1:nrow(economics), 
                       initialWindow = 36, horizon = 10, fixedWindow = TRUE)

 trainSlices <- timeSlices[[1]]
 testSlices <- timeSlices[[2]]

# I'm not really sure about the periods
fit <- tbats(economics[trainSlices[[1]],]$unemploy, seasonal.periods=c(4,12), use.trend=TRUE, use.parallel=TRUE)

# Using forecast for prediction
pred <- forecast(fit,h=length(economics[testSlices[[1]],]$unemploy))
# Here I plot the forecast
plot(pred)

enter image description here

Here I just actually stuck and dont really know how to add the test data aka testSlices to that particular pred object with the corresponding test/train labels. Perhaps there is also a way to use a different style on the confidence interval. Thank you!

Upvotes: 1

Views: 3392

Answers (1)

Jav
Jav

Reputation: 2313

While there is definitely cleaner way of doing it.

Fast way to do it, is like this:

lines(x = as.numeric(rownames(economics[testSlices[[1]],])), economics[testSlices[[1]],]$unemploy, col = "red")

Update for comment:

Add labels:

legend(x = "topleft", legend = c("Predicted", "Test Data"), col = c("blue", "red"), lty = c(1, 1))

enter image description here

Upvotes: 2

Related Questions