[英]mgcv::gamm() and MuMIn::dredge() errors
I've been trying to fit multiple GAMs using the package mgcv
within a function, and crudely select the most appropriate model through model selection procedures. 我一直在尝试使用函数中的
mgcv
软件包来适合多个GAM,并通过模型选择过程粗略地选择最合适的模型。 But my function runs the first model then doesn't seem to recognise the input data dat
again. 但是我的函数运行第一个模型,然后似乎无法再次识别输入数据
dat
。
I get the error 我得到错误
Error in is.data.frame(data) : object 'dat' not found.
is.data.frame(data)中的错误:找不到对象'dat'。
I think this is a scoping problem and I've looked here , and here for help but cannot figure it out. 我认为这是一个范围界定问题,我已经在这里和此处寻求帮助,但无法解决。
Code and data are as follows (hopefully reproducible): https://github.com/cwaldock1/Help/blob/master/test_gam.csv 代码和数据如下(希望可复制): https : //github.com/cwaldock1/Help/blob/master/test_gam.csv
library(mgcv)
# Function to fit multiple models
best.mod <- function(dat) {
# Set up control structure
ctrl <- list(niterEM = 0, msVerbose = TRUE, optimMethod="L-BFGS-B")
# AR(1)
m1 <- get.models(dredge(gamm(Temp ~ s(Month, bs = "cc") + s(Date, bs = 'cr') + Year,
data = dat, correlation = corARMA(form = ~ 1|Year, p = 1),
control = ctrl)), subset=1)[[1]]
# AR(2)
m2 <- get.models(dredge(gamm(Temp ~ s(Month, bs = "cc") + s(Date, bs = 'cr') + Year,
data = dat, correlation = corARMA(form = ~ 1|Year, p = 2),
control = ctrl)), subset=1)[[1]]
# AR(3)
m3 <- get.models(dredge(gamm(Temp ~ s(Month, bs = "cc") + s(Date, bs = 'cr') + Year,
data = dat, correlation = corARMA(form = ~ 1|Year, p = 3),
control = ctrl)), subset = 1)[[1]]
### Select best model to work with based on unselective AIC criteria
if(AIC(m2$lme) > AIC(m1$lme)){mod = m1}else{mod = m2}
if(AIC(mod$lme) > AIC(m3$lme)){mod = m3}else{mod = mod}
return(mod$gam)
}
mod2 <- best.mod(dat = test_gam)
Any help would be greatly appreciated. 任何帮助将不胜感激。
Thanks, Conor 谢谢,康纳
get.models
evaluates in model's formula
environment, which in gamm
is (always?) .GlobalEnv
, while it should be function's environment (ie sys.frames(sys.nframe())
). get.models
在模型的formula
环境(在gamm
中始终是gamm
)中.GlobalEnv
,而它应该是函数的环境(即sys.frames(sys.nframe())
)。
So, instead of 所以,代替
get.models(ms, 1)
use 采用
eval(getCall(ms, 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.