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

smooth_samples() would fail if supplied data when relocating data columns #255

Closed gavinsimpson closed 8 months ago

gavinsimpson commented 8 months ago

smooth_samples() was failing when data was supplied that contained more variables than were used in the smooth that was being sampled. Hence this generally fail unless a single smooth was being sampled from or the model contained only a single smooth. The function never intended to retain all the variables in data but was written in such a way that it would fail when relocating the data columns to the end of the posterior sampling object.

library("mgcv")
library("gratia")

df <- data_sim("eg1")
m_gam <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data = df, method = "REML")
smooth_samples(m_gam, data = df, seed = 2, n = 1)

fails with

> smooth_samples(m_gam, data = df, seed = 2, n = 1)                                              
Error in `relocate()` at gratia/R/posterior-samples.R:810:5:
! Can't subset columns that don't exist.
✖ Columns `y`, `x1`, `x2`, `x3`, `f`, etc. don't exist.
Run `rlang::last_trace()` to see where the error occurred.