Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
It is common to want forecasts to be positive, or to require them to be within some specified range
Positive forecasts
To impose a positivity constraint, simply work on the log scale. With the forecast package in R, this can be handled by specifying the Box-Cox parameter
library(fpp) fit <- ets(eggs, lambda=0) plot(forecast(fit, h=50)) |
Forecasts constrained to an interval
To see how to handle data constrained to an interval, imagine that the egg prices were constrained to lie within
where
# Bounds a <- 50 b <- 400 # Transform data y <- log((eggs-a)/(b-eggs)) fit <- ets(y) fc <- forecast(fit, h=50) # Back-transform forecasts fc$mean <- (b-a)*exp(fc$mean)/(1+exp(fc$mean)) + a fc$lower <- (b-a)*exp(fc$lower)/(1+exp(fc$lower)) + a fc$upper <- (b-a)*exp(fc$upper)/(1+exp(fc$upper)) + a fc$x <- eggs # Plot result on original scale plot(fc) |
The prediction intervals from these transformations have the same coverage probability as on the transformed scale, because quantiles are preserved under monotonically increasing transformations.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.