簡體   English   中英

在新的mgcv軟件包中將update()函數與gam()結合使用

[英]Using update() function in conjunction with gam() in new mgcv package

為了測試用於研究物種的七個性別/生命階段中每個階段的捕獲計數的一系列gam模型,我使用了一個函數,該函數使用update()將一系列預測變量迭代替換為基本模型並產生AIC每個得分。 在新的mgcv軟件包中,使用gam模型實現相同的代碼似乎存在問題。 這是要處理的功能性簡化數據子集。

repex=structure(list(Day = c(183L, 190L, 197L, 204L, 211L, 218L, 225L, 
232L, 239L, 246L, 175L, 182L), M = c(18L, 43L, 22L, 20L, 
7L, 1L, 1L, 0L, 0L, 0L, 0L, 17L), Solar = c(77L, 59L, 20L, 
55L, -3L, -44L, 13L, 58L, 8L, 6L, -28L, 12L)), .Names = c("Day", "M", "Solar"), 
class = "data.frame", row.names = c(NA, -12L))

在我將mgcv更新為1.8-9之前,該函數以這種形式成功運行(注意,此后我進行了編輯以直接引用變量,而不是附加repex):

MAIC<-function(x){
  m<-gam(repex$M~s(repex$Day),data=repex,family=poisson)
  m<-update(m,.~.+x)
  return(AIC(m))
}

然后,我將生成帶有以下內容的AIC分數列表:

lapply(c('Solar'),function(x) MAIC(repex[ , x]))

在將R更新為3.2.3,並將mgcv更新為1.8-9之后,我運行了上述腳本,並僅使用以下命令測試了該功能:

MAIC(repex$Solar)

並收到此消息:

 Error in eval(expr, envir, enclos) : object 'x' not found

我一直在忙着解決這個問題,並確定與某些建議相反,代碼行m<-update(m,.~.+x)基本上沒有任何問題。 我簡化了上面的MAIC函數以嘗試查找問題的根源,並使用glm()lm()調用通過上述repex數據子集成功運行了它:

MAIC1 <- function(x){
  m <- glm(repex$M~repex$Day,data=repex,family=poisson)
  m <- update(m,.~.+x)
  return(AIC(m))
}
MAIC1(repex$Solar)

MAIC2 <- function(x){
  m <- lm(repex$M~repex$Day,data=repex)
  m <- update(m,.~.+x)
  return(AIC(m))
}
MAIC2(repex$Solar)

但是,當我將模型更改為gam時,會收到上述錯誤:

MAIC3 <- function(x){
  m <- gam(repex$M~s(repex$Day),data=repex)
  m <- update(m,.~.+x)
  return(AIC(m))
}
MAIC(repex$Solar)

無論如何構建基本游戲,除非除非按以下方式省略update() ,否則都會發生這種情況:

MAIC4<-function(x){
  m<-gam(repex$M~s(repex$Day)+x,data=repex)
  return(AIC(m))
}
MAIC4(repex$Solar)

我的sessionInfo()調用帶來了:

R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1 

我的ls()調用帶來了:

[1] "MAIC"  "MAIC1" "MAIC2" "MAIC3" "MAIC4" "repex"

我的find("x")調用帶來了:

character(0)

最后,我分別通過以下方式交叉檢查了MAIC1(repex$Solar)MAIC2(repex$Solar)MAIC4(repex$Solar)的AIC得分:

AIC(glm(repex$M~repex$Day+repex$Solar,data=repex,family=poisson))
AIC(lm(repex$M~repex$Day+repex$Solar,data=repex))
AIC(gam(repex$M~s(repex$Day)+repex$Solar,data=repex))

希望這有助於清理問題。

reformulate()是執行此操作的明智方法:

repex <- data.frame(Day = c(183, 190, 197, 204, 211, 
 218, 225, 232, 239, 246, 175, 182),
   M = c(18, 43, 22, 20, 7, 1, 1, 0, 0, 0, 0, 17), 
  Solar = c(77, 59, 20, 55, -3, -44, 13, 58, 
     8, 6, -28, 12))

library(mgcv)
MAIC <- function(x) { 
    form <- reformulate(c("s(Day)",x),response="M")
    m <- gam(form, data=repex,family=poisson)
    return(AIC(m)) 
}
MAIC("Solar")

例如,這使操作列名稱向量更容易。

如果您真的想使用“原始”變量名(例如Solar而不是"Solar" ,則可以使用deparse(substitute())

MAIC2 <- function(x) {
    xx <- deparse(substitute(x))
    form <- reformulate(c("s(Day)",xx),response="M")
    m <- gam(form, data=repex,family=poisson)
    return(AIC(m)) 
}
MAIC2(Solar)

(或者,您可以只寫MAIC2 <- function(x) MAIC(deparse(substitute(x))) ...)

如果您真的想使用帶有原始變量的update ,則需要更多的魔術...

MAIC3 <- function(x) {
   m <- gam(M~s(Day),data=repex,family=poisson)
   m2 <- update(m,bquote(.~.+.(substitute(x))))
   return(AIC(m2))
}
MAIC3(Solar)

暫無
暫無

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

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