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

difference_smooths() fails on a decomposed HGAM #223

Closed gavinsimpson closed 8 months ago

gavinsimpson commented 1 year ago
library("gratia")
library("mgcv")
data("iris")

mod1 <- gam(Petal.Width ~ Species + 
         s(Sepal.Length, k=4) +
         s(Sepal.Length, by=Species, k=4) +
         s(Sepal.Width, k=4) +
         s(Sepal.Width, by=Species, k=4) +
         ti(Sepal.Length, Sepal.Width, k=4) +
         ti(Sepal.Length, Sepal.Width, by=Species, k=4),
       method = "REML",
       data = iris)

# this fails
difference_smooths(mod1, smooth = "ti(Sepal.Length,Sepal.Width)")
gavinsimpson commented 1 year ago

I suspect the issue is because there will be 4 smooths here that contain "ti(Sepal.Length,Sepal.Width)" and I'm not identifying the correct set of smooths for each pair as a result.

For example, the code below works, which is the equivalent model in non-decomposed form:

library("gratia")
library("mgcv")
data("iris")

mod2 <- gam(Petal.Width ~ Species + 
         te(Sepal.Length, Sepal.Width, by=Species, k=4),
       method = "REML",
       data = iris)

# this fails
difference_smooths(mod2, smooth = "te(Sepal.Length,Sepal.Width)")