Quantile regression: geom_quantile
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
About this notebook
This notebook follows the examples on ggplot2's reference page for
geom_quantile(), which covers
stat_quantile() too, in the order the page gives them. Each example shows ggplot2's code first
and then Giggly's. An example Giggly cannot draw yet keeps its ggplot2 code and names what it is
waiting for, so the page is also a list of what remains.
geom_quantile() fits a quantile regression of y on x and draws a line for each quantile it
fits: a continuous analogue of a box plot. stat_quantile() does the fitting, with the quantreg
package's rq(), or its rqss() for smooth quantiles. Giggly has none of this yet.
The quartiles and the median
1 / hwy turns miles per gallon into gallons per mile. Unless quantiles says otherwise, the
lines are at the quantiles 0.25, 0.5 and 0.75.
m <-
ggplot(mpg, aes(displ, 1 / hwy)) +
geom_point()
m + geom_quantile()
Not in Giggly yet: geom_quantile().
Choosing the quantiles
m + geom_quantile(quantiles = 0.5)
q10 <- seq(0.05, 0.95, by = 0.05)
m + geom_quantile(quantiles = q10)
The first draws the median alone, the second a line at every 0.05 from 0.05 to 0.95.
Not in Giggly yet: geom_quantile() and its quantiles option.
Smooth quantiles
# You can also use rqss to fit smooth quantiles
m + geom_quantile(method = "rqss")
# Note that rqss doesn't pick a smoothing constant automatically, so
# you'll need to tweak lambda yourself
m + geom_quantile(method = "rqss", lambda = 0.1)
Not in Giggly yet: geom_quantile() with its method and lambda options and the smooth quantile
regression it fits, and the quantreg package's rqss().
A fixed colour, width and transparency
# Set aesthetics to fixed value
m + geom_quantile(colour = "red", linewidth = 2, alpha = 0.5)
Not in Giggly yet: geom_quantile().