Search code examples
rpredictionpolynomialsquantregquantile-regression

Obtaining fitted values from second-order quantile regressions


I'm sure this is easily resolvable, but I have a question regarding quantile regression.

Say I have a data frame which follows the trend of a second-order polynomial curve and I construct a quantile regression fitted through different parts of the data:

##Data preperation
set.seed(5)
d <- data.frame(x=seq(-5, 5, len=51))
d$y <- 50 - 0.3*d$x^2 + rnorm(nrow(d))

##Quantile regression
Taus <- c(0.1,0.5,0.9)
QUA<-rq(y ~ 1 + x + I(x^2), tau=Taus, data=d)
plot(y~x,data=d)
for (k in 1:length(Taus)){
 curve((QUA$coef[1,k])+(QUA$coef[2,k])*(x)+(QUA$coef[3,k])*(x^2),lwd=2,lty=1, add = TRUE)
}

I can obtain the maximum y value through the 'predict.rq' function and you can see this the following plot.

Plot obtained from above code

 ##Maximum prediction
 Pred_df<- as.data.frame(predict.rq(QUA))
 apply(Pred_df,2,max)

So my question is how do I obtain the x-value which corresponds to the maximum y-value (i.e. the break in slope) for each quantile?


Solution

  • The package broom could be very useful here:

    library(broom)
    library(dplyr)
    augment(QUA) %>% 
      group_by(.tau) %>% 
      filter(.fitted == max(.fitted))