## Quantile regression - mini-demo ## install quantreg library from R ## for more info, see: CADE BS 2003. A gentle introduction to quantile regression for ecologists. FRONTIERS IN ECOLOGY AND THE ENVIRONMENT 1 : 412 2003 library(quantreg) ## Generate some triangular data x = rep(0:10,100) y = abs(rnorm(length(x),sd=x)) plot(x,y) ### First plot the linear regression line through these data: model = lm(y~x) abline(model) # It's positive and significant, but clearly the pattern violated the assumption of homogeneous variances in y, so linear regression is not appropriate. Instead we can fit regression line for quantiles to test for diverging boundaries: ## 5th quantile mod05 = rq(y~x,tau=0.05) abline(mod05,col='red') ## 95th quantile mod95 = rq(y~x,tau=0.95) abline(mod95,col='blue') ## median quantile mod50 = rq(y~x,tau=0.50) abline(mod50,col='green') ## Are slopes of 5th and 95th different: summary(mod05) summary(mod95) ## Slopes +/- 2*SE clearly don't overlap. This is just a brief illustration - there is a large literature and variety of methods available in this library if you want to explore quantile regression in more detail