0
我一直試圖在一個函數中使用包mgcv
來適應多個GAM,並通過模型選擇過程粗略地選擇最合適的模型。但是,我的功能運行第一個模型,然後似乎不再識別輸入數據dat
。mgcv :: gamm()和MuMIn :: dredge()錯誤
我得到的錯誤
錯誤is.data.frame(數據):對象 '逸' 未找到。
我覺得這是一個範圍的問題,我看here,並here尋求幫助,但不能弄明白。
代碼和數據如下(希望再現的): 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)
任何幫助,將不勝感激。
感謝, 康納爾
我認爲錯誤是get.models調用疏通模型對象,因爲當爲運行: 'M1 < - 疏通(GAMM( Temp〜s(Month,bs =「cc」,k = k.month)+ s(Date,bs ='cr')+ Year, data = dat,correlation = corARMA(form =〜1年份,p = 1), control = ctrl))' 該函數不會因此錯誤而崩潰。 –