1
$\begingroup$

I have a dataset where I have observed counts for a phenomenon for every week in a 5-years period in 10 geographic areas.

We believe that this count is affected by seasonality, but also additively by "input" variables.

I would like model seasonnality using a spline on "weeks". I expect seasonnality to have a similar "shape" for each 10 regions, but the "amplitude" of the seasonnality effect could be different for each regions.

If I was doing this using dummy variables, I would try building the following non linear model:

Y ~ Intercept
+ BetaInput1 * X_input1 + BetaInput2 * X_input2
+ Beta_Area2 * IsArea2 + Beta_Area3 * IsArea3 + ... + BetaArea10 * IsArea10
+ Beta_January * Beta_SeasonalSentivityOfArea1 * IsArea1_in_January + ... + Beta_December * Beta_SeasonalSentivityOfArea1 * IsArea1_in_December
+ Beta_January * Beta_SeasonalSentivityOfArea2 * IsArea2_in_January + ... + Beta_December * Beta_SeasonalSentivityOfArea2 * IsArea2_in_December
+ ....
+ Beta_December * Beta_SeasonalSentivityOfArea10 * IsArea10_in_December

Where I would evaluate 34 betas:
10 intercepts 2 betas for the impact of Input1 and Input2 12 seasonal betas ( 1 for each month) 10 sentivity of area to seasonnality

Is there any way I could calculate a spline on weeks instead of 12 seasonal betas?

The effect of input1 and input2 should be additive rather than multiplicative.

thanks,

$\endgroup$

1 Answer 1

2
$\begingroup$

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.

$\endgroup$
5
  • 1
    $\begingroup$ Wow, my own blog post from the bottom of the heap :) Thanks Gavin, I really appreciate the time, effort and knowledge you poured into answer. My initial tests give poor looking results when I set m=1, but the first model with the random spline basis model is promising. $\endgroup$
    – Zoltan
    Commented May 18, 2017 at 19:25
  • $\begingroup$ Gavin, In my initial post, I specified that we expect that sensisibility to seasonnality would be multiplicative. For example, one region could multiply of the DoY by 2 while another would multiply it by 0.5. My understanding is that none of these models allow to do that. For example, understanding of the models with m=1 (penalizing first derivative) will try to go from one "level" of difference (+100 counts ) to another (+200 counts) , instead of being "always 2x the amount of counts". Is this correct? $\endgroup$
    – Zoltan
    Commented May 19, 2017 at 17:29
  • $\begingroup$ All the models I describe would allow for what you are calling multiplicative seasonality; they all estimate the seasonal signal for each region so if one region has twice the amplitude of another, these factor-smooth splines will pick it up. I basically describe two approaches: i) fit using by variable smoother, or ii) fit using true random effect splines. The m = 1 business is just how you choose to parameterise each of these two options. If you don't grok how they decompose the model for separate seasonality by region, just ignore them and use the simpler versions instead. $\endgroup$ Commented May 19, 2017 at 17:53
  • $\begingroup$ The simplest version of each is just the first model I show in the two sections. The main difference between the two is whether you expect each region's seasonality to have similar levels of wiggliness (use the bs = "fs" option, or the by variables but setting the ID arg), or not (use the by variable smooths). As region is just a factor, what you are calling multiplicative is really just the multiplication of a dummy variable (0,1) indicating if the $i$th row is in region $z$ or not with a spline basis expansion. It's not multiplicative in sense of a time series where... $\endgroup$ Commented May 19, 2017 at 17:57
  • $\begingroup$ ...the seasonal pattern varies with the mean of the time series or some such other systematic variation. $\endgroup$ Commented May 19, 2017 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.