gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
202 stars 28 forks source link

The ordering of plots in draw.gam doesn't always match the order in which the smooths entered the model #154

Closed gavinsimpson closed 2 years ago

gavinsimpson commented 2 years ago

I think something must be going on to reorder the smooth column somewhere in all the group_by, mapping, nesting/unnesting, binding, etc that happens in draw.gam()

An easy solution should be to use smooths() to reorder the column into the order that the smooths entered the model.

gavinsimpson commented 2 years ago

As an extreme example, see this for the Swiss extreme rainfall example data set used in the second edition of Simon Wood's book:

require("gamair")
library("gratia")
library("mgcv")

data(swer, package = "gamair")

m <- gam(list(exra ~ s(nao) + s(elevation) + climate.region + s(N, E),
                   ~ s(year) + s(elevation) + climate.region + s(N, E),
                   ~ climate.region),
         family = gevlss(),
         data = swer)

draw(m)

with the draw(m) resulting in

swer-draw-output

Where the smooths for the scale parameter of the GEV come before those for the location parameter, and are in the wrong order within those parameters!