簡體   English   中英

mgcv:如何在 gam 和 gamm 模型中識別精確的結值?

[英]mgcv: How to identify exact knot values in a gam and gamm model?

我正在使用 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()

我真的很感激任何幫助,謝謝。

要獲得結點,您可以提取邊際平滑項的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

但從邊緣平滑對象中拉出結更安全,因為用戶始終可以通過gam()knots參數指定結。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM