R Programming/Quantile Regression
From Wikibooks, open books for an open world
|
|
This section is a stub. You can help Wikibooks by expanding it. |
Contents |
[edit] Introduction
- Thanks to Roger Koenker R is the best package for quantile regression methods.
- see the quantreg[1] package and Roger Koenker's personal webpage
[edit] Simple quantile model
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") 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 in the same time :
q2 <- rq(y ~ x, tau = -1) plot(q2, nrow = 2, ncol = 1)
[edit] Computing time
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
[edit] Resources
- Koenker, Roger (2005) Quantile Regression, Cambridge University Press. ISBN 0-521-60827-9
[edit] References
- ↑ Roger Koenker (2010). quantreg: Quantile Regression. R package version 4.50. http://CRAN.R-project.org/package=quantreg