3
$\begingroup$

I have read similar questions submitted here, and have looked in Wood's 2017 book (Generalized additive models: An introduction with R, 2nd edition) but cannot find an answer to my problem.

To set up the question, consider first a simple linear model:fit<-lm(Y~X+Z). Once I get the coefficients from this simple model (say, Y_hat=2.1+0.9X+3.2Z) then I can get the predicted values that Y would have if I set the coefficient of Z to zero (Y_hat*=2.1+0.9X) and the residuals of Y around this new equation.

Now, imagine that I do a generalized additive model (say, with the gamm function of the mgcv package): fit<-gamm(Y~s(X)+s(Z)). How can I get the coefficients of this GAM in such a way that I can do the same thing as with the simple linear equation; i.e. get the predicted values that Y would have if I set the coefficients associated with the smooth function of Z to zero and then obtain the residuals around this new equation? If I extract the coefficients of a GAM (eg coef(fit$lme)) then I can see the coefficients associated with the intercept and of each of the basis functions, but I don't know how to proceed after that. Thanks for any suggestions.

$\endgroup$
2
  • $\begingroup$ Have you looked at predict? mgcv provides a method for gam objects and there's gammit::predict_gamm if you want to condition on random effects. $\endgroup$
    – PBulls
    Commented Jan 5 at 19:21
  • $\begingroup$ Thanks. I will look into this. I note that there is an "exclude=" argument in predict.gam that might be relevant. $\endgroup$ Commented Jan 5 at 22:05

1 Answer 1

2
$\begingroup$

Given

fit <- gamm(Y ~ s(X) + s(Z))

and assuming Y is where you want to predict...

yhat <- predict(fit$gam, newdata = Y, exclude = "s(Z)")

or

yhat <- predict(fit$gam, newdata = Y, terms = c("(Intercept)", "s(Z)"))

should give you want you want, and where prediction doesn't even consider what the random effects are doing if you use them in such a model.

When doing this exclude/terms thing you need to know the labels for the smooths esp if things get more exotic. Look carefully at how terms are named in the output from summary(fit$gam) for example to see how you need to name terms passed to exclude or terms arguments

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.