How to attain predictive response values used for vis.gam plots?

Viewed 23

Is it possible to get the predicted response values that are used to plot 3D contour plots in vis.gam. I want to know what the predicted response for each grid cell is in the plot. I am using the MGCV package and GAM function. My model is:

pose_mod <- gam(POSEtrans ~ s(Elevation) + s(Heatload) + 
                  s(Precip_Spring_Fall_Total) + s(PSSP6) + s(BRTE) +
                  TSF + s(PointID, bs = 're'), 
                family=betar(link='logit'), method = "REML",
                data = iData) 

vis.gam(pose_mod, view=c("BRTE", "Elevation"),
        type = 'response',theta=45,ticktype="detailed", n.grid = 20)
1 Answers

The easiest way that I know is to use my {gratia} package (you'll need the development version on GitHub, which you can install using the intructions on the GitHub site: https://gavinsimpson.github.io/gratia/ or if you can't build from sources, use the binary builds from R-universe: https://gavinsimpson.r-universe.dev/ui#package:gratia).

library("gratia")

ds <- data_slice(pose_mod, BRTE = evenly(BRTE, n = 20),
                 Elevation = evenly(Elevation, n = 20))

This follows the conventions in {mgcv} and vis.gam(), such that you get the same grid of points. The you could predict() (from {mgcv} or use fitted_values() from {gratia} with ds to get the predicted expectation of the response for each grid cell:

fitted_values(pose_mod, data = ds, scale = "response")
Related