I have a model where I try to model the linear trend in my response over time The trend may vary by elevation and location

As far as I understood the mgcv package in R, a GAM model would look like:

Y ~ Year + elevation + Year:elevation +s(lat, lon, bs="gp") + 
     ti(lat, lon, Year, d=c(2, 1), bs=c("gp", "tp"))

I have a gaussian error and identity link.

I saw ti() is a tensor product interaction, but it is a non-linear interaction. I want a non-linear interaction for location but not for year, here I want just a slope

How can I estimate a slope for each location?

In linear regression considering only year and elevation I would sum the slope of year and the slope of my interaction with elevation (multiplied by my observed elevation) so b_Year + b_Year:elevation*elevation but how do I do that for my location term? -or how to reparameterize?


1 Answer 1


This kind of smooth-linear interaction is known as a varying coefficient model. To fit these kinds of effects in {mgcv} you use the by argument and pass it a continuous variable. SO I would expect something like this to do what you want:

Y ~ Year + elevation + Year:elevation +
  s(lat, lon, bs = "gp") + 
  ti(lat, lon, d = 2, bs = "gp", by = Year)


  • the s() term accounts for the spatial variation in Y, and
  • the ti() term accounts for the spatial variation in the effect of Year on Y (the spatially-varying linear trend)
  • $\begingroup$ thanks a lot! minor add: In case I have two treatments (factor) for which I want to compare the slope, my by=Year will "occupy" the by=treatment. Is there a workaround? or does this would need two separate models with slope comparison afterwards? $\endgroup$
    – Jmmer
    Commented May 16, 2022 at 15:07
  • $\begingroup$ -would it work to set the sp in a te(lat,long,year,d=c(2,1),sp=?,by=treatment) manually somehow, i.e. so super high that this will be penalized to a linear term? but how would I set a sp for year only but not lat,long.. thanks a lot for any hint , I am a little stuck here. $\endgroup$
    – Jmmer
    Commented May 18, 2022 at 18:29
  • 1
    $\begingroup$ An option might be to do ti(lat, long, treatment, bs = c("tp", "re"), d = c(2,1), by = Year) and treat the treatment variable as a (new style) random effect otherwise I don't think I know of what to do what you want unless you want to go to a smooth effect of year. Your sp thing would work in the sense that it would keep the marginal smooth of Year linear but you'd have to also figure out how to control the interaction smoothing parameter, and that model would have the same interpretation as a smoothly varying slope in the sense of the varying coefficient model $\endgroup$ Commented May 19, 2022 at 9:10
  • $\begingroup$ thanks a lot! I will try the ti(lat, long, treatment, bs = c("tp", "re"), d = c(2,1), by = Year) and compare results to two separate models for each treatment! $\endgroup$
    – Jmmer
    Commented May 19, 2022 at 16:19

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.