1
votes

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!

1
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.Gregor Thomas
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
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
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
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: onlinelibrary.wiley.com/doi/10.1890/09-1129.1/abstractGrant Adams

1 Answers

0
votes

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