Skip to content

Instantly share code, notes, and snippets.

@briatte
Last active December 20, 2015 00:39
Show Gist options
  • Save briatte/6043373 to your computer and use it in GitHub Desktop.
Save briatte/6043373 to your computer and use it in GitHub Desktop.
Visually weighted quantile regressions. Initial code by Martin Johnsson.
library(quantreg)
model.rq <- rq(Temp ~ Wind, airquality, tau=1:99/100)
quantile.regressions <- data.frame(t(coef(model.rq)))
colnames(quantile.regressions) <- c("intercept", "slope")
quantile.regressions$quantile <- rownames(quantile.regressions)
quantile.regressions
library(ggplot2)
quantile.regressions$x = 1 - abs(as.numeric(substring(quantile.regressions$quantile, 6)) - .5)
qplot(x=Wind, y=Temp, data=airquality, geom = "blank") +
geom_abline(aes(intercept=intercept, slope=slope,
colour=x^8), data=quantile.regressions) +
geom_point(alpha = .5) +
scale_color_gradient2("", low = "yellow", high = "orangered", limits = c(0, 1)) +
theme(panel.grid.major = element_blank())
# want more yellow and less white?
qplot(x=Wind, y=Temp, data=airquality, geom = "blank") +
geom_abline(aes(intercept=intercept, slope=slope,
colour=x^8), data=quantile.regressions) +
geom_point(alpha = .5) +
scale_color_gradient2("", low = "white", midpoint = .5, mid = "yellow", high = "orangered", limits = c(0, 1)) +
theme(panel.grid.major = element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment