mgcv: predict.gam() 对 type = "terms" 和 type = "response" 给出不同的结果
mgcv: predict.gam() gives different results for type = "terms" and type = "response"
我想使用选项 type="terms"
分别评估来自 GAM 模型的预测变量的每个组件。作为完整性检查,我将结果与使用选项 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
谁能帮我解决这个问题?非常感谢您的帮助!
您的模特:
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
现在 a1
和 a2
是一样的。
如果您想知道,我在 ?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**.
我想使用选项 type="terms"
分别评估来自 GAM 模型的预测变量的每个组件。作为完整性检查,我将结果与使用选项 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
谁能帮我解决这个问题?非常感谢您的帮助!
您的模特:
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
现在 a1
和 a2
是一样的。
如果您想知道,我在 ?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**.