Search code examples
rquantilequantreg

How to extract observations included in a particular quantile in quantile regression?


I have a database of ~2000 observations and made a quantile regression on the 95th percentile using quantreg package.

I wanted to identify the observations that were actually used for calculating the slope and intercept for the 95th percentile regression in order to perform further analysis. Is there any way to do that?

This is the code for quantreg I used so far:

datos<-quantreg.example
library(quantreg)
rq(y ~ x, tau=0.95, data=datos, method="br", model = TRUE) 

and here is the data file: http://www.filedropper.com/quantregexample


Solution

  • Alright, I thought I would whip up a quick solution to what I think your question is asking for:

    set.seed(123)
    library(dplyr) #data transformation
    library(quantreg) #quantile regression
    #make dummy data
    df <- data.frame(x = sample(1:10, 200, replace = T))
    df$y <- df$x + rnorm(200)
    #fit quantile regression
    my_q <- rq(y~x, data = df, tau = 0.95)
    #use dplyr to get 95% quantile at each x
    df_q <- df %>% group_by(x) %>% summarise(yq = quantile(y, probs = .95))
    #quick viz with red points being 95% quantiles
    with(df, plot(x,y))
    legend('topleft',legend = '95% Conditional Quantiles',col = 'red',pch = 19, bty = 'n')
    with(df_q, points(x, yq, col = 'red', pch = 19))
    abline(reg = my_q)
    

    enter image description here Hope this helps.