How to visualize results of the distributed lag model using mgcv package I'm recently reading the book entitled "Generalized Additive Models --- An Introduction with R 2nd edition". When reading chapter 7.4.2--- a distributed lag model for pollution related death, I would like to know how the R code for plots at left and middle of Figure 7.15 (as follows). The GAM model is:
    apl <- bam(death ~ s(time, bs="cr", k=200) + te(pm10, lag, 
            k=c(10, 5)) + te(o3, tmp, lag, k=c(8, 8, 5)), 
            family=poisson, data=dat)

where pm10, lag, o3, and tmp are all matrices.

 A: For left figure, this can be accomplished by using predict.gam/predict.bam using type = 'terms'.
You just need to set up a new data for each pm10 and lag.
For example, for lag0
pm10 <- seq(min(pm10, na.rm = T), max(pm10, na.rm = T), length = 1000)
l <- rep(0, 1000)
newdata <- data.frame(pm10 = pm10, lag = l)

If there are multiple smoothing terms, just add a single value to the data.frame as long as that value is in the range of that variable, say
newdata$o3 <- o3[1]
newdata$time <- time[1]
newdata$tmp <- tmp[1]

then do
pred <- predict.bam(apl, newdata = newdata, type = "terms", 
                    terms = "te(pm10,lag)")
plot(pred~pm10, type = 'l', ylim = c(-0.08, 0.08))

Here is my attempt to produce the first graph.

As for the middle one, the principle should be the same, in which you need to setup a new dataframe for grid combination between o3 and tmp first, and lag value as before, so that grid prediction for o3 and tmp would be calculated for each lag value. Then you can use par(new=TRUE) to overlay them together as suggested in the book. I haven't tried this one, but will give it a go later.
