Search code examples
rplotgammgcv

Extract values used to make plot for parametric component of GAM in R


I have performed a GAM that includes both continuous smooth terms and a categorical variable. I have plotted the model (mod) using plot(mod,residuals=T,all.terms=T,pages=1). This produces plots of the two smooth parameters as well as the parametric parameter. I want to extract the values used to make these plots so I can re do them and make them look nicer. If I save the plot in an object, this gives me everything I need for the smooth terms, but doesn't contain any information about the parametric component: plot.mod=plot(mod,residuals=T,all.terms=T,select=0). But I can't see where the numbers are coming from for the default plotting of the parametric component. Is there a way to extract these as well?

Here is a reproducible example of what I have done so far

library(mgcv)

# create some data
data=data.frame(response=c(10,12,8,9,3,4,5,5,4,5,4,5,4,1),pred1=c(9,8,8,9,6,7,6,4,3,4,2,3,3,1),pred2=as.factor(c("A","C","B","B","A","A","C","B","C","A","C","B","A","B")),pred3=c(1,6,3,4,8,6,4,5,7,10,11,3,12,1))

# run the GAM
mod <- gam(response ~ s(pred1,k=8) + pred2 + s(pred3,k=5), data=data, family=gaussian(), method="REML")

# the default plot
plot(mod,residuals=T,all.terms=T,pages=1)

# save values in an object. But this only saves the smooth terms.
plot.mod=plot(mod,residuals=T,all.terms=T,select=0)

# How can I extract the values used to plot the parametric term?

The plot I'm trying to extract the data to make: enter image description here


Solution

  • From the plot.gam documentation, termplot is used for the parametric terms, so

    plot.para <- termplot(mod, se = TRUE, plot = FALSE)
    

    saves that plot to a list.
    The format is different than the others, but the data is there.