How to extract fitted values of GAM {mgcv} for each variable in R?

泄露秘密 提交于 2019-12-03 10:14:53

Not quite sure if I follow you correctly, but predict(model, type = "terms") might be the solution you're looking for.

Update

I don't think these are standardised. Possibly some of the coefficients are just negative.

Consider the example from the help file ?mgcv:::predict.gam:

library(mgcv)
n<-200
sig <- 2
dat <- gamSim(1,n=n,scale=sig)

b<-gam(y~s(x0)+s(I(x1^2))+s(x2)+offset(x3),data=dat)

The results below illustrate that these are in fact the contributions that are being used for each predictor to calculate the fitted values (by calculating the sum of each of these contributions and then adding the intercept and the offset).

> head(predict(b))
        1         2         3         4         5         6 
 9.263322  2.822200  7.137201  4.902631 14.558401 11.889092 
> head(rowSums(predict(b, type = "terms")) + attr(predict(b, type = "terms"), "constant") + dat$x3)
        1         2         3         4         5         6 
 9.263322  2.822200  7.137201  4.902631 14.558401 11.889092 

To return predicted values on the same scale of the response you need to set predict(model, type = "response")

The default behaviour of the gam is type = "link" which returns the linear predictor and often with standard errors (thus the positive and negative values you found).

Read more on the ?mgcv::predict.gam help page.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!