Generating quantile forecasts in R

September 7, 2014

(This article was first published on Hyndsight » R, and kindly contributed to R-bloggers)

From today’s email:

I have just finished reading a copy of ‘Forecasting:Principles and Practice’ and I have found the book really interesting. I have particularly enjoyed the case studies and focus on practical applications.

After finishing the book I have joined a forecasting competition to put what I’ve learnt to the test. I do have a couple of queries about the forecasting outputs required. The output required is a quantile forecast, is this the same as prediction intervals? Is there any R function to produce quantiles from 0 to 99?

If you were able to point me in the right direction regarding the above it would be greatly appreciated.

Many Thanks,

Presumably the competition is GEFCOM2014 which I’ve posted about before.

The future value of a time series is unknown, so you can think of it as a random variable, and its distribution is the “forecast distribution”. A “quantile forecast” is a quantile of the forecast distribution. The usual point forecast is often the mean or the median of the forecast distribution. A prediction interval is a range of specified coverage probability under that distribution. For example, if we assume the forecast distribution is normal, then the 95% prediction interval is defined by the 2.5% and 97.5% quantiles of the forecast distribution.

Still assuming normality, we could generate the forecast quantiles from 1% to 99% in R using

qnorm((1:99)/100, m, s)

where mu and sigma are the estimated mean and standard deviation of the forecast distribution. So if you are using the forecast package in R, you can do something like this:

fit <- auto.arima(WWWusage)
fc <- forecast(fit, h=20, level=95)
qf <- matrix(0, nrow=99, ncol=20)
m <- fc$mean
s <- (fc$upper-fc$lower)/1.96/2
for(h in 1:20)
  qf[,h] <- qnorm((1:99)/100, m[h], s[h])
matlines(101:120, t(qf), col=rainbow(120), lty=1)


Of course, assuming a normal distribution is rather restrictive and not very interesting. For a more interesting but much more complicated approach to generating quantiles, see my 2010 paper on Density forecasting for long-term peak electricity demand.

To leave a comment for the author, please follow the link and comment on their blog: Hyndsight » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)