Search code examples
rgam

GAMs in R: calculating the contribution of individual terms


Say I have a GAM in R calculated with the gam function of the mgcv package. I can get the model predicted values for the given data set as follows:

# Load data
data("mtcars")

# Fit model
g <- gam(mpg ~ s(disp, k = 3) + s(hp, k = 3), data = mtcars)

# Get values for given data set
predict(g)

Now, what I'm interested in is how much each predictor contributes to each predicted value. So, for the first car in the data set, Mazda RX4, how much of the predicted value for mpg is due to the term for disp and how much is due to hp. Is there an easy way to extract this information from the model?


Solution

  • The answer is predict(g, type = "terms")