2016-07-24 10 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**.