2016-07-24 103 views
1

我想要使用選項type="terms"單獨評估GAM模型中預測變量的每個組成部分。作爲完整性檢查,我使用選項type="response"將結果與總預測評估進行了比較。結果不同。這裏是一個例子:mgcv:predict.gam()爲type =「terms」和type =「response」提供了不同的結果

library(mgcv) 
n<-200 
sig <- 2 
dat <- gamSim(1,n=n,scale=sig) 
b<-gam(y~x0+s(I(x1^2))+s(x2)+offset(x3),da=dat) 

nd <- data.frame(x0=c(.25,.5),x1=c(.25,.5),x2=c(.25,.5),x3=c(.25,.5)) 

a1 <- predict.gam(b,newdata=nd,type="response") 
a2 <- rowSums(predict.gam(b,newdata=nd,type="terms")) + b$coefficients[1] 
a1 - a2 # Should be zero! 
# 1 2 
# 0.25 0.50 

任何人都可以幫我解決這個問題嗎?非常感謝您的幫助!

回答

1

你的模型:

y ~ x0 + s(I(x1^2)) + s(x2) + offset(x3) 

具有偏置項。

type = "link"type = "response"時,將考慮偏移predict.gam,但在type = "terms"時不考慮。

a1 <- predict.gam(b, newdata=nd, type="response") 
#  1   2 
#11.178280 6.865068 

a2 <- predict.gam(b, newdata=nd, type="terms") 
#   x0 s(I(x1^2))  s(x2) 
#1 0.006878346 -1.8710120 5.6467813 
#2 0.013756691 -0.6037635 -0.1905571 
#attr(,"constant") 
#(Intercept) 
# 7.145632 

所以你必須增加抵消自己:

a2 <- rowSums(a2) + b$coef[1] + nd$x3 
#  1   2 
#11.178280 6.865068 

現在a1a2是相同的。


在你不知道的情況下,我對你的文檔中?predict.gam

type: ... When ‘type="terms"’ each component of the linear 
     predictor is returned seperately (possibly with standard 
     errors): this includes parametric model components, followed 
     by each smooth component, **but excludes any offset and any 
     intercept**. 
相關問題