Skip to content

Instantly share code, notes, and snippets.

@sillasgonzaga
Created July 7, 2016 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sillasgonzaga/f0a1d033ab9d6d7b7f4dd03f8c27464e to your computer and use it in GitHub Desktop.
Save sillasgonzaga/f0a1d033ab9d6d7b7f4dd03f8c27464e to your computer and use it in GitHub Desktop.
Prediction intervals for forecasting models
pi_accuracy <- function(fc, yobs){
# source: http://ellisp.github.io/blog/2016/01/30/hybrid-forecasts
# checks the success of prediction intervals of an object of class
# forecast with actual values
if(length(yobs) != length(fc$mean)){
stop("yobs needs to be the same length as the forecast period.")
}
n <- length(yobs)
yobsm <- cbind(yobs, yobs)
In <- (yobsm > fc$lower & yobsm < fc$upper)
colnames(In) <- c("Series 1", "Series 2")
Success <- colMeans(In)
return(list(In = In, Success = Success, n = n))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment