I have a question in regards to using the coefficients from a GAM which uses smooth variables.
I want to know how to do so manually, i.e. not just plugging into the predict function.
I tried an example, but just by looking at the coefficients for the smoothed terms I immediately knew that plugging them into the equation would not yield the same values as the predict function.
Here is my example, which comes from the Hitters dataset in ISLR2:
library(ISLR2)
library(mgcv)
### One more model, for stackexchange
gam_stack=gam(Walks~s(RBI, k=3)+s(Hits, k=5), data=Hitters, method="REML")
### Our new data:
new_data=data.frame(RBI=c(25), Hits=c(110))
### The prediction using predict:
test_pred=predict(gam_stack, new_data)
test_pred
1
37.15598
### getting the coefficients from the model:
coefs_gam_stack=gam_stack$coefficients
coefs_gam_stack
(Intercept) s(RBI).1 s(RBI).2 s(Hits).1 s(Hits).2 s(Hits).3 s(Hits).4
38.742236 6.844722 5.544233 6.384631 -2.667766 15.174375 2.232861
I want to use the coefficients extracted, coefs_gam_stack above, to predict walks, the way one could for a linear model. Simply looking at the coefficients it is easy to see that simply plugging in the values in the new_data vector above will not yield the same prediction as using the predict function. The predict function for the new data gives a predicted number of walks of 37.15598, which is test_pred in my code. Obviously looking at the coefficients, which are found in coefs_gam_stack above, simply plugging in the values in new_data won't yield the same result as the predict function.
Obviously I'm doing something wrong, and as I am somewhat new to GAM modelling it is clearly my incomplete understanding of both the methodology and the output of the gam function in R.
Any help would be greatly appreciated, obviously you can't just plug the coefficients and new_data values in the way one can with a linear model. I suspect what I am missing has something to do with knots, but I'm not quite sure.
I have seen similar stackexchange questions answered using the lpmatrix from the predict function, but this seems only to get a matrix of the data used to build the model, whereas I want to be able to manually predict with new data.
Thanks in advance! I'm hoping this will really solidify my understanding of GAM modelling. Right now I can still use it, and use the predict function, but I want to bolster my knowledge of just what is going on underneath the hood.
lpm <- predict(gam_stack, new_data, type = "lpmatrix"); lpm %*% coefs_gam_stack
Your question boils down to how to create the linear predictor matrix ("lpmatrix") manually, which seems like a tedious exercise to me. The source code ofpredict.gam
is quite long but you could work through it. You could also start from theory: I would start with working through Wood S (2017). "Generalized Additive Models: An Introduction with R". $\endgroup$