Yes. I'll ignore the Input
variables as how you model the different seasonalities can be added on to a model containing the Input
terms.
There are several ways to do this, but all are considered factor-smooth interactions.
Standard factor-smooth interactions
To model seasonality, and for convenience I'll use DoY
as my variable indicating what day of the year the sampling took place on. Further assuming that variable region
is a factor/categorical variable.
Your data might look like:
response | region | DoY
=========|========|====
0.234 | A | 23
... | ... | ...
You can create a design matrix for the model by dummy or treatment contrasts for the region
variable, plus the evaluation of the basis functions at the values of DoY
for each observation. This has the effect of estimating a separate spline for each region
, with each spline by default having it's own smoothness parameter in the implementation in the R package mgcv. Such a model in R and mgcv notation would be
m <- gam(response ~ region + s(Doy, by = region), data = foo)
The factor/categorical term region
is needed because each of the region
-specific splines is subject to identifiability constraints and as such in centred about 0. The implication of this is that without the region
parametric term, the splines would be trying to model the seasonality within each region plus the average value of the response in each splines particular region.
Variations on this theme involve making region
an ordered categorical (factor) variable. In that case, what you get with mgcv is more like a traditional ANOVA approach to modelling. In this setting, the reference level of region
gets it's only spline of DoY
, whilst the other levels of region
get a spline that estimates the difference between the reference level spline and the one for the current level. This is just like dummy or treatment contrasts in ANOVA - the intercept represents the reference level and the parametric terms reflect the differences between each other level and the reference level.
Another variation on this theme is to generate a common seasonality spline and then region-specific deviations from the common spline. Such a model can be fitted by using two splines instead of one, and using a first-derivative penalty on the region-specific deviation splines. In other respects this model is the same as the standard by
smooth I mention initially:
m <- gam(response ~ region + s(DoY) + s(Doy, by = region, m = 1),
data = foo)
In this model, the first spline is the common seasonality spline. The second spline is by region
and represents the region-specific deviations from the common spline. The m = 1
is how we request a first derivative penalty.
Usually, GAMs employing penalized splines use a second derivative penalty, which penalizes curvature or wiggliness of the fitted spline; we want smooth fitted functions. The first derivative penalty does not penalize wiggliness. instead it penalizes deviation from a flat or constant function. This is why the above model works; penalizing deviations from the flat function is, when combined with the common spline actually penalizing deviation from the common spline.
If that is unclear, think of the flat function as being a value of 0 everywhere. The penalty controls how far the fitted spline moves away from the 0 line. To get the fitted smooth for each region, we take the value of the common spline and add on to it the value of that region's region-specific deviation spline. If that region follows the common seasonality pattern then we'd want the region-specific spline to be a flat line, which we said was equal to a value of 0, because we want the fitted seasonality for that region to have 0 deviation from the common spline.
Random factor-smooth interactions
The above by
variable smooths involve estimating parametric terms for region
. (I didn't include that we used method = "REML"
or method = "ML"
in the model fitting code.) The splines would ideally be fitted by treating them in mixed model form and estimating using REML or ML. But that still means that we have these parametric terms that relate to each spline we fit in the standard by
variable smoothing setting.
Instead, we could treat each region-specific spline as being a true random effect (a combination of random intercept plus the equivalent of random slopes but for splines.) Such a model will fit a separate spline for each region
but it will do so by treating the entire spline as a set of random effects. Call these random splines, but that is probably some heresy I'm committing in doing so. An important difference is that each spline will have a similar amount of wiggliness if we use this random spline approach. In the by
variable spline approach, each region's spline can have a different amount of wiggliness.
In mgcv this random spline basis model is set up using
m <- gam(response ~ s(Doy, region, bs = "fs"), data = foo)
Note here that we don't need the region
parametric term (the region-specific mean response is now like a random intercept in the spline), and we specify the smooth in the same way as a 2-d spline, but we use the bs
argument to indicate this is a factor-smooth spline. (Again, I am leaving off the method
and other arguments that should also be specified.)
A variation on this random spline approach is to use the common spline plus region-specific deviations approach, in which case we have:
m <- gam(response ~ s(Doy) + s(Doy, region, bs = "fs", m = 1),
data = foo)
Because of the smoothness selection, the model fits will be pulled towards the common spline, so this is a way of estimating the equivalent of the population effect of DoY
. There are other variations here that will include more shrinkage towards the population level.