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

`fitted_values()` should warn about data combinations that aren't present in the model #164

Open gavinsimpson opened 2 years ago

gavinsimpson commented 2 years ago

It would be useful to have fitted_values() try to warn about data combinations (perhaps factors only - ignore numeric by factor interactions) that didn't exist in the original data. predict.gam() just doesn't even return an NA for such newdata which the causes an error in fitted_values() when we try to bind the output from predict() on to the data passed to the fitted_values().

factor_combos should be able to provide all the combos that exist in the data used to fit the model. Perhaps this can be solved by

  1. select out all the factor terms from data
  2. run distinct() on the above
  3. run fitted_combos on object
  4. compare the two objects, find combos in data that aren't in the data used to fit the model
  5. get the indices of those rows from data
  6. predict() but on for data with missing combos removed
  7. assign output from predict to the rows of data that did have a combo used to fit the model, other rows are set to NA, so we avoid the error in the bind_cols() step.