[英]Obtaining predictions from an mgcv::gam fit that contains a matrix “by” variable to a smooth
[英]MGCV get design matrix
基于样条的 GAM 回归由以下成本函数定义:
cost = ||y - S \\beta ||^2 + scale * integral(|S'' \\beta|^2)
其中S
是由样条定义的设计矩阵。
在 RI 中可以使用以下代码计算gam
:
library('mgcv')
data = data.frame('x'=c(1,2,3,4,5), 'y'=c(1,0,0,0,1))
g = gam(y~s(x, k = 4),family = 'binomial', data = data, scale = 0.5)
plot(g)
我想得到由s()
函数生成的设计矩阵S
我怎样才能做到这一点?
我相信有两种方法可以从gamObject
获取设计矩阵
library('mgcv')
data <- data.frame('x'=c(1,2,3,4,5), 'y'=c(1,0,0,0,1))
g <- gam(y~s(x, k = 4),family = 'binomial', data = data, scale = 0.5)
plot(g)
(option1 <- predict(g, type = "lpmatrix"))
# (Intercept) s(x).1 s(x).2 s(x).3
# 1 1 1.18270529 -0.39063809 -1.4142136
# 2 1 0.94027407 0.07402655 -0.7071068
# 3 1 -0.03736554 0.32947477 0.0000000
# 4 1 -0.97272283 0.21209396 0.7071068
# 5 1 -1.11289099 -0.22495720 1.4142136
# attr(,"model.offset")
# [1] 0
(option2 <- model.matrix.gam(g))
# (Intercept) s(x).1 s(x).2 s(x).3
# 1 1 1.18270529 -0.39063809 -1.4142136
# 2 1 0.94027407 0.07402655 -0.7071068
# 3 1 -0.03736554 0.32947477 0.0000000
# 4 1 -0.97272283 0.21209396 0.7071068
# 5 1 -1.11289099 -0.22495720 1.4142136
# attr(,"model.offset")
# [1] 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.