gavinsimpson / gratia

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

Add a `plot_predictions()` or equivalent function #300

Open gavinsimpson opened 4 months ago

gavinsimpson commented 4 months ago

The marginaleffects package has a plot_predictions() function which combines (from a gratia viewpoint) data_slice() with fitted_values().

It has a condition argument which allows the user to either specify up to four covariates via a character vector or a named list (names of the list are covariate labels). The order of the covariates in the vector/list are used for:

  1. x-axis,
  2. colour / shape,
  3. facet variable 1
    • if only three covariates specified this is passed to facet_wrap(), or
    • if four covariates are specified this is is the column facet variable in facet_grid()
  4. facet variable 2, for the rows of facet_grid().

The character vector form requires some defaults for generating data:

The list form allows more flexibility; not sure I will implement everything. It allows:

Can't call this plot_predictions as that will clash with marginaleffects and users (including me) should want to use both packages for interrogating GAMs. Also, following my design principles for the package, it would be good to return a data frame of the required predicted values (with attributes to inform on conditioning variables) and then have a draw() method for that object. Will need a subsetting method for [ if I do this...