1

I am creating a varying-coefficient GAMM using 'mgcv' in R with a continuous 'by' variable by using the by setting. However, I am having difficulty in locating the parameter estimate of the effect of the 'by' variable. In this example we determine the spatially-dependent effect of temperature t on sole eggs (i.e. how the linear effect of temperature on sole eggs changes across space):

require(mgcv)
require(gamair)
data(sole)
b = gam(eggs ~ s(la,lo) + s(la,lo, by = t), data = sole)

We can then plot the predicted effects of s(la,lo, by = t) against the predictor t:

pred <- predict(b, type = "terms", se.fit =T)
by.variable.prediction <- pred[[1]][,2]
plot(x= sole$t, y = by.variable.prediction)

However, I can't find a listing/function with the parameter estimates of the 'by' variable t for each sampling location. summary(), coef(), and predict() do not give you the parameter estimates.

Any help would be appreciated!

  • Have a look at `?mgcv::s`, specifically the description of `by`. If `t` is numeric then the value of `t` multiplies the smooth. If `t` is a factor then there is a separate smooth for each level of the factor. If you want to extract the smooths themselves (I wouldn't recommend it... don't see what use they'd be), [this might help](http://stackoverflow.com/q/15584541/903061). – Gregor Thomas Mar 17 '16 at 23:07
  • Thanks for the advice! I looked around `mgcv::s` and it doesn't really give you much. With a numeric `t` predictor, in my case, we are assuming a linear relationship who's slope could change across `la,lo`, so for each unique `la,lo` we get a specific slope, correct? Then I should be able to find the magnitude and direction of that slope somewhere, it's just being elusive :). I don't want to extract the smooths, just want to plot that variable-coefficient term using those slopes. – Grant Adams Mar 18 '16 at 17:32
  • For each `la` and `lo`, there is a smoothed value that gets multiplied by `t`. There isn't anything else. This is what the documentation states. You can interpret `t` as the "slope" of the smoothed `la,lo` variable, or you can interpret the `la,lo` smooth as the slope of `t`. – Gregor Thomas Mar 18 '16 at 17:50
  • Smoothing is done with multiplicative constants - it doesn't make sense to fit an additional parameter to multiply a smooth by, because any such value can be directly incorporated into the smooth. – Gregor Thomas Mar 18 '16 at 17:55
  • Thanks for getting back to me! That makes sense. Would you know of any resources for plotting the things? I'm trying for something similar to: http://onlinelibrary.wiley.com/doi/10.1890/09-1129.1/abstract – Grant Adams Mar 18 '16 at 19:11
  • I don't have access to that article. `mgcv` has nice plotting built in. I'd highly recommend [Simon Wood's book on GAMs](http://www.amazon.com/Generalized-Additive-Models-Introduction-Statistical/dp/1584884746). – Gregor Thomas Mar 18 '16 at 19:18
  • Thanks. Unfortunately, the Wood 2006 book doesn't go into much detail on these models. If your are interested the article for free is at [NOAA's site](http://www.sefsc.noaa.gov/FEBpub/Bartolino_etal_2011_Ecology.pdf). Figure 4 is the one that plots a similar model. – Grant Adams Mar 18 '16 at 20:23

1 Answers1

0

So the coefficient for the variable t is the value where t is equal to 1, conditional on the latitude and longitude. So one way to get the coefficient/parameter estimate for t at each latitude and longitude is to construct your own dataframe with a range of latitude/longitude combinations with t=1 and run predict.gam on that (rather than running predict.gam on the data used the fit the model, as you have done). So:

preddf <- expand.grid(list(la=seq(min(sole$la), max(sole$la), length.out=100),
                           lo=seq(min(sole$lo), max(sole$lo), length.out=100),
                           t=1))

preddf$parameter <- predict(b, preddf, type="response")

And then if you want to visualize this coefficient over space, you could graph it with ggplot2.

library(ggplot2)

ggplot(preddf) + 
  geom_tile(aes(x=lo, y=la, fill=parameter))
Amadou Kone
  • 907
  • 11
  • 21