Reputation: 914
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)
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
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))
Upvotes: 2