I'd like to extract coefficients and upper and lower bounds from a quantile regression using the quantreg
package. Here's an example from the help file.
data(engel)
attach(engel)
taus <- c(.05,.1,.25,.75,.9,.95)
f <- rq((foodexp)~(income),tau=taus)
sf <- summary(f)
sf[1]
#[[1]]
#Call: rq(formula = (foodexp) ~ (income), tau = taus)
#tau: [1] 0.05
#Coefficients:
# coefficients lower bd upper bd
#(Intercept) 124.88004 98.30212 130.51695
#income 0.34336 0.34333 0.38975
I know I can use coefficients()
to get the coefficients.
cf <- t(data.frame(coefficients(f))) # transpose for better arrangement
cf
# (Intercept) income
#tau..0.05 124.88004 0.3433611
#tau..0.10 110.14157 0.4017658
#tau..0.25 95.48354 0.4741032
#tau..0.75 62.39659 0.6440141
#tau..0.90 67.35087 0.6862995
#tau..0.95 64.10396 0.7090685
But I can't figure out how to get the upper/lower bounds that appear in summary()
. I looked at str(sf)
, but I did not see how to extract.
Ultimately, I'd like to put taus, coefficients, and upper/lower bounds in a dataframe for further processing.
I'm assuming you just want the coefficients on the non-intercept term. How about this
sapply(sf, function(x) c(tau=x$tau, x$coefficients[-1, ]))
That will iterate over the different levels of tau
and extract the intervals for the coefficients
[,1] [,2] [,3] [,4] [,5] [,6]
tau 0.0500000 0.1000000 0.2500000 0.7500000 0.9000000 0.9500000
coefficients 0.3433611 0.4017658 0.4741032 0.6440141 0.6862995 0.7090685
lower bd 0.3433270 0.3420992 0.4203298 0.5801552 0.6493680 0.6739000
upper bd 0.3897500 0.4507941 0.4943288 0.6904127 0.7422294 0.7344405