R – How to Obtain Local Time Slope from GAM Model Using mgcv

generalized-additive-modelmgcvr

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?

Best Answer

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)

where:

  • 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)