data <- csvToUMF("2010_middens_mgrs.csv", type="unmarkedFrameOccu")
fm0 <- occu(~1 ~1, data)
fm1 <- occu(~days ~1, data)
fm2 <- occu(~mean_temp ~1, data)
fm3 <- occu(~min_temp ~1, data)
fm4 <- occu(~humidity ~1, data)
fm5 <- occu(~precip ~1, data)
fm6 <- occu(~precip*mean_temp ~1, data)
fm7 <- occu(~precip*min_temp ~1, data)
fm8 <- occu(~precip+min_temp ~1, data)
fm9 <- occu(~precip*days ~1, data)
start.model <- fm0
det.var <- as.vector(c("fm0", "fm1", "fm2", "fm3", "fm4", "fm5", "fm6", "fm7",
"fm8", "fm9"))
f.AICc.occu.sig <- function(start.model, blocks = det.var, max.iter=NULL,
detocc = 2, AICcut = 1, print.log=TRUE){
modlst <- c(start.model)
x <- 2
if (detocc == 2) coeff <- length(start.model@estimates@estimates$state@estimates)
else coeff <- length(start.model@estimates@estimates$det@estimates)
best <- FALSE
model.basis <- start.model
keep <- list(start.model)
AICmin <- AIClst <- AICc(start.model)
cutoff <- 20
if (is.null(max.iter) | max.iter > length(blocks)) max.iter <- length(blocks)
for(ii in 1:max.iter){
models <- list()
coeff <- coeff + 1
cnt <- 1
for (jj in 1:length(keep)) {
for(kk in 1:length(blocks)) {
if (detocc == 2) form <- as.formula(paste("~. ~. + ", blocks[kk]))
else form <- as.formula(paste("~. + ", blocks[kk], "~. "))
if (class(dummy <- try(update(keep[[jj]], form))) == "unmarkedFitOccu") {
flag <- 0
if (detocc == 2) {
for(dd in 1:length(sqrt(diag (vcov(dummy@estimates@estimates$state))))) {
if(diag (vcov(dummy@estimates@estimates$state))[[dd]] < 0
|| sqrt(diag (vcov(dummy@estimates@estimates$state))[[dd]]) > cutoff)
flag <- 1
break
}
}
else {
for(dd in 1:length(sqrt(diag (vcov(dummy@estimates@estimates$det))))) {
if(diag (vcov(dummy@estimates@estimates$det))[[dd]] < 0
|| sqrt(diag (vcov(dummy@estimates@estimates$det))[[dd]]) > cutoff)
flag <- 1
break
}
}
if (flag == 0) {
for (bb in 1:length(AIClst)) {
if (round(AICc(dummy),digits=6) == round(AIClst[bb], digits=6)) {
flag <- 1
break
}
}
}
}
else {flag <- 1}
if (flag == 0) {
models[[cnt]] <- dummy
modlst[[x]] <- models[[cnt]]
AIClst <- c(AIClst, AICc(models[[cnt]]))
x <- x + 1
cnt <- cnt + 1
}
}
}
if(length(LL <- unlist(lapply(models, function(x) {AICc(x)}))) == 0) break
keep <- list()
k = 1
cont <- 0
for (mm in order(LL, decreasing=FALSE)) {
if (LL[mm]< AICmin + AICcut) {
if (detocc == 2) {
if (length(models[[mm]]@estimates@estimates$state@estimates) == coeff) {
keep[[k]]<- models[[mm]]
k <- k + 1
if (LL[mm]<AICmin) {
AICmin <- LL[mm]
cont <- 1
}
}
}
else {
if (length(models[[mm]]@estimates@estimates$det@estimates) == coeff) {
keep[[k]]<- models[[mm]]
k <- k + 1
if (LL[mm]<AICmin) {
AICmin <- LL[mm]
cont <- 1
}
}
}
}
else break
}
if (length(keep) == 0) break
}
fitlst <- fitList(fits = modlst)
modsel <- modSel(fitlst,nullmod=NULL)
return(list(model=model.basis, modlst=modlst, fitlst=fitlst, modsel=modsel))
}
MGRSr<- f.AICc.occu.sig(start.model=start.model, blocks=det.var, max.iter=30, AICcut=1)
################### TEST OUTPUT #########################
#########################################################
MGRSr
$model
Call:
occu(formula = ~1 ~ 1, data = data)
Occupancy:
Estimate SE z P(>|z|)
1.88 0.639 2.94 0.00331
Detection:
Estimate SE z P(>|z|)
1.65 0.372 4.43 9.3e-06
AIC: 71.09312
$modlst
$modlst[[1]]
Call:
occu(formula = ~1 ~ 1, data = data)
Occupancy:
Estimate SE z P(>|z|)
1.88 0.639 2.94 0.00331
Detection:
Estimate SE z P(>|z|)
1.65 0.372 4.43 9.3e-06
AIC: 71.09312
$fitlst
An object of class "unmarkedFitList"
Slot "fits":
$`1`
Call:
occu(formula = ~1 ~ 1, data = data)
Occupancy:
Estimate SE z P(>|z|)
1.88 0.639 2.94 0.00331
Detection:
Estimate SE z P(>|z|)
1.65 0.372 4.43 9.3e-06
AIC: 71.09312
$modsel
nPars AIC delta AICwt cumltvWt
1 2 71.09 0.00 1.00 1.00