I am trying to extract the time varying smoothing parameter of my gam.
This is the model:
gam1 <- gam(as.numeric(A5_1)~s(tt,k=k)+s(tt,by=A5_1L,k=k)+s(tt,by=A5_2L,k=k)+
s(tt,by=A5_5L,k=k)+s(tt,by=A5_9L,k=k),data=data_subset)
gam2 <- gam(as.numeric(A5_2)~s(tt,k=k)+s(tt,by=A5_1L,k=k)+s(tt,by=A5_2L,k=k)+
s(tt,by=A5_5L,k=k)+s(tt,by=A5_9L,k=k),data=data_subset)
gam5 <- gam(as.numeric(A5_5)~s(tt,k=k)+s(tt,by=A5_1L,k=k)+s(tt,by=A5_2L,k=k)+
s(tt,by=A5_5L,k=k)+s(tt,by=A5_9L,k=k),data=data_subset)
gam9 <- gam(as.numeric(A5_9)~s(tt,k=k)+s(tt,by=A5_1L,k=k)+s(tt,by=A5_2L,k=k)+
s(tt,by=A5_5L,k=k)+s(tt,by=A5_9L,k=k),data=data_subset)
summary(gam1)
look like this:
I can create plots showing smoothing parameters over time like this:
plot(gam1, select=2,ylim=c(-3,1),rug=F,xlab="time points",
ylab=substitute(paste("Joy",italic("(t-1)"), "on Joy",italic("(t)"))))
Plot is looking like this:
So far I found the predict.gam()
function:
predict.gam(gam1, type = "terms")
The predict function gives me:
(nrows=103)
However the output doesn't match the plot.
The line in the plot start around 0.2, whereas the smoothing
Parameter in from predict.gam()
is around 1.1. How can I extract
the correct smoothing parameters over time?
In genereal I want these parameters, so that i can create different qgraphs() over time. One qgraph at the beginning, middle and end. Like network analysis plots. If there is a direct way of creating multiple qgraph() plots from time varying gam, I will gladly take this as well.
Maybe I will even use qgraph.animate() once I created a matrix of those Parameters.
Thanks a lot!
It won't look like the plot because the plot is showing the estimated effect for a 100 ordered values over the observed range of tt
. The predict()
call is giving you back the fitted values for your data because you didn't supply anything to the newdata
argument.
Two options are to:
Save the output from plot.gam()
: plt <- plot.gam(....)
Now plt
will contain an object with all the data used to create the plot. If you plot all smooths rather than just one, you'll get a list back with a data object per smooth.
Prepare some new data to predict at and pass that to predict()
as newdata
. You'll need to provide data for tt
plus all the by
variables used in the model. expand.grid()
is useful for this.