【问题标题】:mgcv: How to identify exact knot values in a gam and gamm model?mgcv:如何识别 gam 和 gamm 模型中的精确结值?
【发布时间】:2019-12-11 17:48:45
【问题描述】:

我正在使用 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 是随机生成的“可用”点(由 1500m 半径圆缓冲),其中具有不同数量的能量发展。我已将节数限制为 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()

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

【问题讨论】:

    标签: r mgcv


    【解决方案1】:

    要获得结,您可以提取边缘平滑项的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
    

    为什么是这些值?它们位于观察到的协变量值的五分位数:

    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
    

    但是从边缘光滑的物体上拉出结更安全,因为用户总是可以通过knots 参数指定结到gam()

    【讨论】:

    • 哇,谢谢。我回来了,这对我很有帮助。我有另一个问题。我在这里与年交互地表干扰--- ti(propwells_buff_res1500, year, bs = "cr", k = 5) --- 了解表面干扰(propwells..)的选择如何逐年变化。我能够为每一年(17 年)生成图,但我想知道是否有可能获得每一年的结值??
    • 每年没有结;在协变量 year 上有一组结,但它们根本没有变化。如答案所示,您可以从ti() 平滑中提取边缘基的结,但它们是静态的,它们不会随协变量值而变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 2020-05-01
    • 2021-07-31
    • 1970-01-01
    • 2016-01-07
    相关资源
    最近更新 更多