[英]Using update() function in conjunction with gam() in new mgcv package
In order to test a series of gam
models for capture counts for each of seven sex/life-stages of a study species, I used a function that employed update()
to iteratively substitute into the base model a sequence of predictors and produce an AIC score for each. 为了测试用于研究物种的七个性别/生命阶段中每个阶段的捕获计数的一系列
gam
模型,我使用了一个函数,该函数使用update()
将一系列预测变量迭代替换为基本模型并产生AIC每个得分。 There appears to be a problem implementing the same code with a gam
model in the new mgcv
package. 在新的
mgcv
软件包中,使用gam
模型实现相同的代码似乎存在问题。 Here is a functional, simplified subset of data to proceed. 这是要处理的功能性简化数据子集。
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))
Before I updated mgcv
to 1.8-9, the function ran successfully in this form (note, I edited henceforth to directly reference the variables, as opposed to attaching repex): 在我将
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))
}
I would then produce a list of AIC scores with something like this: 然后,我将生成带有以下内容的AIC分数列表:
lapply(c('Solar'),function(x) MAIC(repex[ , x]))
After I updated R to 3.2.3 and mgcv
to 1.8-9, I ran the above scripts as well as simply testing the function with: 在将R更新为3.2.3,并将
mgcv
更新为1.8-9之后,我运行了上述脚本,并仅使用以下命令测试了该功能:
MAIC(repex$Solar)
and receive this message: 并收到此消息:
Error in eval(expr, envir, enclos) : object 'x' not found
I have been futzing around with this and have determined that, contrary to some suggestions, there isn't anything essentially problematic with the line of code m<-update(m,.~.+x)
. 我一直在忙着解决这个问题,并确定与某些建议相反,代码行
m<-update(m,.~.+x)
基本上没有任何问题。 I simplified the above MAIC
function to try to locate the source of trouble, and ran it successfully with the above repex data subset with a glm()
and lm()
call: 我简化了上面的
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)
But when I change the model to a gam, I receive the above error: 但是,当我将模型更改为gam时,会收到上述错误:
MAIC3 <- function(x){
m <- gam(repex$M~s(repex$Day),data=repex)
m <- update(m,.~.+x)
return(AIC(m))
}
MAIC(repex$Solar)
This happens no matter how the base gam is constructed, unless the update()
is omitted as follows: 无论如何构建基本游戏,除非除非按以下方式省略
update()
,否则都会发生这种情况:
MAIC4<-function(x){
m<-gam(repex$M~s(repex$Day)+x,data=repex)
return(AIC(m))
}
MAIC4(repex$Solar)
My sessionInfo()
call brings: 我的
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
My ls()
call brings: 我的
ls()
调用带来了:
[1] "MAIC" "MAIC1" "MAIC2" "MAIC3" "MAIC4" "repex"
My find("x")
call brings: 我的
find("x")
调用带来了:
character(0)
Finally, I cross-checked the AIC scores of MAIC1(repex$Solar)
, MAIC2(repex$Solar)
, and MAIC4(repex$Solar)
, respectively, with: 最后,我分别通过以下方式交叉检查了
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))
Hopefully this helps clear things up. 希望这有助于清理问题。
reformulate()
is a sensible way to do this: 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")
This makes it easier, eg, to operate over a vector of column names. 例如,这使操作列名称向量更容易。
If you really want to be able to use "raw" variable names (eg Solar
rather than "Solar"
, you can use deparse(substitute())
如果您真的想使用“原始”变量名(例如
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)
(alternatively, you could just write MAIC2 <- function(x) MAIC(deparse(substitute(x)))
...) (或者,您可以只写
MAIC2 <- function(x) MAIC(deparse(substitute(x)))
...)
If you really want to use update
with raw variables it takes a little more magic ... 如果您真的想使用带有原始变量的
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.