2016-07-24 8 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"の場合はオフセットが考慮されますが、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**. 
関連する問題