I use a GAM to model interactions between two continuous and one unordered categorical factor with three levels. Depending on significance of the smooth terms I would like to extract and visualize either level-specific smooth predictions if they are significant, or the interaction between the two continuous variables across all levels is, in case this one is signficant.
Using the iris dataset here because it is pretty close to my own data, which contains different species and their traits as predictors and matings as reponse (I am trying to model fitness landscapes). I specified the model below in an attempt to exclude the other terms (e.g., using predict.gam(mod1, newdata, exclude=c( "ti(Sepal.Length,Sepal.Width):Speciessetosa", "ti(Sepal.Length,Sepal.Width):Speciesversicolor", "ti(Sepal.Length,Sepal.Width):Speciesvirginica")
, if this makes sense (see below):
## data
data(iris)
## gam
mod1 = gam(Petal.Width ~ Sepal.Length * Sepal.Width * Species +
s(Sepal.Length, k=3) +
s(Sepal.Length, by=Species, k=3) +
s(Sepal.Width, k=3) +
s(Sepal.Width, by=Species, k=3) +
ti(Sepal.Length, Sepal.Width, k=3) +
ti(Sepal.Length, Sepal.Width, by=Species, k=3),
method = "REML",
data=iris
)
> anova(mod1)
Family: gaussian
Link function: identity
Formula:
Petal.Width ~ Sepal.Length * Sepal.Width * Species + s(Sepal.Length,
k = 3) + s(Sepal.Length, by = Species, k = 3) + s(Sepal.Width,
k = 3) + s(Sepal.Width, by = Species, k = 3) + ti(Sepal.Length,
Sepal.Width, k = 3) + ti(Sepal.Length, Sepal.Width, by = Species,
k = 3)
Parametric Terms:
df F p-value
Sepal.Length 1 4.30623 0.039860
Sepal.Width 1 NaN NaN
Species 0 NaN NaN
Sepal.Length:Sepal.Width 1 4.10032 0.044835
Sepal.Length:Species 2 0.94772 0.390174
Sepal.Width:Species 0 NaN NaN
Sepal.Length:Sepal.Width:Species 2 1.02435 0.361785
Approximate significance of smooth terms:
edf Ref.df F p-value
s(Sepal.Length) 1.000000528612 1.000001005439 11.22252 0.0010463
s(Sepal.Length):Speciessetosa 1.000000232442 1.000000464223 4.81234 0.0299554
s(Sepal.Length):Speciesversicolor 0.000001331420 0.000002637874 0.04797 0.9997172
s(Sepal.Length):Speciesvirginica 1.000000996148 1.000001892404 2.09408 0.1501724
s(Sepal.Width) 1.000007172805 1.000012865152 3.19529 0.0760765
s(Sepal.Width):Speciessetosa 1.000000855121 1.000001708770 0.10222 0.7496783
s(Sepal.Width):Speciesversicolor 1.665759568126 1.888265235757 1.03729 0.2710795
s(Sepal.Width):Speciesvirginica 0.641978333598 0.870807147692 2.25871 0.1630554
ti(Sepal.Length,Sepal.Width) 0.873528400557 0.982977416832 7.70746 0.0067218
ti(Sepal.Length,Sepal.Width):Speciessetosa 0.000001845268 0.000003682513 0.00065 0.5000000
ti(Sepal.Length,Sepal.Width):Speciesversicolor 0.000002545775 0.000005019515 0.03278 0.9996770
ti(Sepal.Length,Sepal.Width):Speciesvirginica 0.000003049801 0.000005392319 0.08124 0.9994726
So, considering these results, I would like to visualize the smooth for s(Sepal.Length):Speciessetosa
separately from s(Sepal.Length)
, because the smooth effect is significant, but also ti(Sepal.Length,Sepal.Width)
because the across-species smooth is significant, unlike the by-species terms.
Does this model specification make sense both in the context of i) my research question (do species differ in how their trait interaction-surfaces affect the response var) and ii) to selectively extract smooth surfaces within and across categorical levels?
Related: