0

I am using quantreg package to calculate quantile regressions. I am fitting regressions for a number of taus and would like to calculate predicted values and 95% confidence intervals. The problem is that the function predict.qr seems not to allow for more than one quantile if you add confidence limits. So far, I solved the problem using loops but that makes my code terribly long. Minimal example:

1) one quantile (0.5) works fine

# prediction data set
pred.df <- data.frame(disp = 150:160) 

fit <- rq(mpg ~ disp, data = mtcars, tau = 0.5)

predict.rq(fit, 
           newdata = pred.df, 
           interval = "confidence")

2) more than one quantile and only predicted values (no ci) also works fine

fit <- rq(mpg ~ disp, data = mtcars, tau = c(0.5, 0.6))
predict.rq(fit, 
           newdata = pred.df)

3) when more than one quantile (0.5, 0.75) and ci, it does not work any more

fit <- rq(mpg ~ disp, data = mtcars, tau = c(0.5, 0.6))

predict.rq(fit, 
           newdata = pred.df, 
           interval = "confidence")

Therefore my question: is it possible to get predicted values and confidence intervals for more than one quantile regression using only predict.rq and avoid loops?

ehi
  • 409
  • 8
  • 23

1 Answers1

1

You may use lapply(), which is a loop, but it is quick to implement:

lapply(c(0.5, 0.6), function(tau) {

fit <- rq(mpg ~ disp, data = mtcars, tau = tau)

predict.rq(fit, 
           newdata = pred.df, 
           interval = "confidence")
})

Alternatively, you can create a wrapper function for your code and then use Vectorize() to have a vectorized version of such a function wrt the tau argument, so to use that function in the future without the need of implementing a loop.

Davide Passaretti
  • 2,741
  • 1
  • 21
  • 32