gavinsimpson/schoenberg

get_smooth doesn't handle factor by variables smooths

gavinsimpson opened this issue · 1 comments

schoenberg:::get_smooth() doesn't work for factor by variable smooths.

library("mgcv")
library("schoenberg") 

dat <- gamSim(4, n = 1000)
mod <- gam(y ~ fac + s(x2, by = fac), data = dat)

schoenberg:::get_smooth(mod, "s(x2)")

produces

> schoenberg:::get_smooth(mod, "s(x2)")
Error in object[["smooth"]][[which_smooth(object, term)]] : 
  subscript out of bounds

This is because which_smooth() correctly returns the indices of all smooths containing the label "s(x2)":

> schoenberg:::which_smooth(mod, "s(x2)")
[1] 1 2 3

Options are

  1. in get_smooth() always return a list of smooths, even if there is only 1 smooth, so that we handle factor by variables, or
  2. add a new argument to specify the level of the factor by variable for if the smooth is a factor by smooth. This way the function only even returns a single smooth object.

Think I prefer 2.

I changed my mind about this and reversed most of this. There is now a function get_by_smooth that can be used for a specific level of the smooth. But changing get_smooth() now is problematic.