r - mgcv:如何识别 gam 和 gamm 模型中的确切节点值?

标签 r mgcv

我正在使用 gams 来拟合资源选择函数,以确定候鸟的能量开发阈值。我的模型如下所示:

m4 <-gam(used~ti(propwells_buff_res1500, bs = "cr", k = 5) +
ti(year, bs = "cr", k = 5) + 
ti(propwells_buff_res1500, year, bs = "cr", k = 5), 
family = binomial(link = "cloglog"), data=mov, gamma=1.4, method="ML")

used 是动物使用的位置,propwells_buff_res1500 是随机生成的“可用”点(由 1500 米半径的圆圈缓冲),其中具有不同数量的能量开发。我已将结限制为 5,但是,我希望能够提取精确的结值,因为据我所知,结值代表阈值……也就是动物使用下降的表面扰动百分比。

我希望这是有道理的。如果没有,我只想知道如何获得结值。从 plot(m4) 中,我可以看到非线性线的斜率开始发生变化的位置,但了解确切的值将非常有帮助。

到目前为止,我已经尝试过:

smooth <- m4$smooth[[3]]

smooth$knots 
##this knot option isn't available to me, 
##I saw it in an old post from 2016, figured out that XP should replace knots

smooth$XP
##and all this returns is list()

非常感谢任何帮助,谢谢。

最佳答案

为了得到结,您可以提取边际平滑项的 xp 分量(注意它是小写的 xp 因为有一个 XP 在平滑的顶层,这是另外一回事)。

举个例子

library('mgcv')
## simulate some data
set.seed(1729)
df <- gamSim(2) # this is a bivariate example
## fit the model
mod <- gam(y ~ ti(x, bs = 'cr', k = 5) + 
               ti(z, bs = 'cr', k = 5) +
               ti(x, z, bs = rep('cr', 2), k = 5),
           data = df$data, method = 'REML')
## extract the 3rd smooth
sm <- mod[['smooth']][[3]]

边缘基在sm$margin中,它只是两个平滑对象的列表:

r$> str(sm$margin, max = 1)                          
List of 2
 $ :List of 21
  ..- attr(*, "class")= chr [1:2] "cr.smooth" "mgcv.smooth"
  ..- attr(*, "qrc")=List of 4
  .. ..- attr(*, "class")= chr "qr"
  ..- attr(*, "nCons")= int 1
 $ :List of 21
  ..- attr(*, "class")= chr [1:2] "cr.smooth" "mgcv.smooth"
  ..- attr(*, "qrc")=List of 4
  .. ..- attr(*, "class")= chr "qr"
  ..- attr(*, "nCons")= int 1

其中每一个都有一个 xp 组件:

sm_x <- sm$margin[[1]]
sm_z <- sm$margin[[2]]

因此,x 的边缘 CRS 的结点是:

r$> sm_x$xp
          0%          25%          50%          75%         100%
0.0005697084 0.2477067126 0.4704501621 0.7121602102 0.9960833385

对于 z

r$> sm_z$xp
         0%         25%         50%         75%        100% 
0.007381999 0.244705125 0.488819070 0.717802322 0.991505836

为什么要有这些值(value)观?它们位于观察到的协变量值的五分位数处:

r$> with(df$data, quantile(x, probs = seq(0, 1, length = 5)))
          0%          25%          50%          75%         100%
0.0005697084 0.2477067126 0.4704501621 0.7121602102 0.9960833385
r$> with(df$data, quantile(z, probs = seq(0, 1, length = 5)))
         0%         25%         50%         75%        100% 
0.007381999 0.244705125 0.488819070 0.717802322 0.991505836

这就是 mgcv 为 CRS 基础设置结的方式。可以使用 place.knots() 恢复确切位置:

r$> with(df$data, place.knots(x, 5))
[1] 0.0005697084 0.2477067126 0.4704501621 0.7121602102 0.9960833385
r$> with(df$data, place.knots(z, 5))
[1] 0.007381999 0.244705125 0.488819070 0.717802322 0.991505836

但从边缘平滑对象拉结更安全,因为用户始终可以通过 gam()knots 参数指定结。

关于r - mgcv:如何识别 gam 和 gamm 模型中的确切节点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59291417/

相关文章:

r - R中的大型固定效应二项式回归

r - 如何使用 R 创建和保存 1x1 像素透明 png?

r - 创建数据框列表,每个数据框在不同的列上排序

r - 强制 GAM 模型拟合为单调并通过 R mgcv 的固定点 (x0, y0)

r - AIC() 和 model$aic 在 mgcv::gam() 中给出不同的结果

r - 将日历与 Shiny 的散点图链接起来

创建 h2o 集成模型时,h2o-ensemble 收到错误请求(临时 ID RTMP_5 已存在),即使之前工作很艰难,也会出现 R 错误

R gam 和 mgcv 之间的包冲突?

r - 使用 mgcv 的 GAM 对比图

r - 在自适应平滑中提取 P 样条的节点、基、系数和预测