# R Programming/Quantile Regression

This section is a stub.You can help Wikibooks by expanding it. |

Quantile regression is a very old method which has become popular only in the last years thanks to computing progress. One of the main researcher in this area is also a R practitioner and has developed a specific package for quantile regressions (**quantreg**)^{[1]} ·^{[2]}.

In theory, Quantile regression are also linear and thus could have been included in the Linear regression page. However, this is a very specific topic and we think that it is worth writing a specific page for this topic.

## Contents

## Simple quantile modelEdit

We simulate from a simple quantile model. We first generate a uniform error term u and a covariate x.

```
N <- 10^3
u <- runif(N)
x <- 1 + rnorm(N)
y <- qnorm(u, mean = 0, sd = 2) + qnorm(u, mean = 1, sd = 1) * x
```

We estimate the quantile model for some values of tau (the quantile) and plot the coefficients :

```
q1 <- rq(y ~ x, tau = seq(.1,.9,.1))
summary(q1)
plot(q1)
```

We then plot the scatterplot, the predicted values using a standard linear model and the predicted values using a quantile linear model :

```
plot(x,y, col = "grey")
m1 <- lm(y ~ x)
abline(m1, col = "red")
taus <- seq(.1,.9,.1)
for (i in 1:length(taus)){
abline(rq(y ~ x, tau = taus[i]), col = "blue")
}
grid()
```

We can also estimate the model for all quantiles at the same time :

```
q2 <- rq(y ~ x, tau = -1)
plot(q2, nrow = 2, ncol = 1)
```

### Computing timeEdit

For large data sets it is better to use the "fn" or "pfn" method.

```
> N <- 10^5
> u <- runif(N)
> x <- 1 + rnorm(N)
> y <- qnorm(u, mean = 0, sd = 2) + qnorm(u, mean = 1, sd = 1) * x
> system.time(rq(y ~ x, tau = .5, method = "br"))
user system elapsed
1.48 0.00 1.48
> system.time(rq(y ~ x, tau = .5, method = "fn"))
user system elapsed
0.60 0.00 0.61
> system.time(rq(y ~ x, tau = .5, method = "pfn"))
user system elapsed
0.30 0.00 0.29
```

## ResourcesEdit

- Koenker, Roger (2005)
*Quantile Regression*, Cambridge University Press. ISBN 0-521-60827-9

## ReferencesEdit

- ↑ Roger Koenker (2010). quantreg: Quantile Regression. R package version 4.50. http://CRAN.R-project.org/package=quantreg
- ↑ Roger Koenker's personal webpage