knitr::opts_chunk$set(echo = TRUE)
dcast(data = ann_caps2, formula = spp.code ~ age.AHY.01, fun.aggregate = length, value.var = "age.AHY.01")
dcast(data = ann_caps2, formula = spp.code ~ sex, fun.aggregate = length, value.var = "sex")
with(ann_caps2, table(sex,age.AHY,useNA = "always"))
ann_caps2$sex[is.na(ann_caps2$sex)] <- "U"
i.BANA <- with(ann_caps2, which(spp.code == "BANA" & sex == "M")) #1 i.BFGR <- with(ann_caps2, which(spp.code == "BFGR" & sex == "U")) #6 i.CMWA <- with(ann_caps2, which(spp.code == "CMWA" & sex == "U")) #3 i.COYE <- with(ann_caps2, which(spp.code == "COYE" & sex == "U")) #3 i.OVEN <- with(ann_caps2, which(spp.code == "OVEN" & sex == "U")) #3 i.rmv <- c(i.BANA,i.BFGR,i.CMWA,i.COYE,i.OVEN) ann_caps5 <- ann_caps2[-i.rmv,]
i.ann_caps5_aceit <- which(ann_caps5$site == "Aceitillar")
cant.sex <- c("BANA", "BCPT", "BWVI", "GREP", "GTGT", "HILC", "NOMO", "OVEN", "PAWA", "RLTH", "STOF", "YFGR")
i.cant.sex <- which(ann_caps2$spp.code %in% cant.sex) ann_caps6 <- ann_caps2[-i.cant.sex,]
i.ann_caps6_aceit <- which(ann_caps6$site == "Aceitillar")
dcast(data = ann_caps6[-i.ann_caps6_aceit,], formula = spp.code ~ sex.M, fun.aggregate = length, value.var = "sex")
m.age.rand.slopes0 <- bglmer(as.numeric(age.AHY.01)-1 ~ 1 + (1|band) + #ann_caps2 contains all obs of each indivial (1|year) + (1|site) + #(1|site:spp.code) #this would be ideal but prob overkill (site.age.cent|spp.code), data = ann_caps6[-i.ann_caps6_aceit,], family = binomial#, #glmerControl(optimizer = "Nelder_Mead") )
m.age.rand.slopesXsex.a <- bglmer(as.numeric(age.AHY.01)-1 ~ 1 + (1|band) + #ann_caps2 contains all obs of each indivial (1|year) + (1|site) + #(1|site:spp.code) #this would be ideal but prob overkill (site.age.cent + sex|spp.code), data = ann_caps6[-i.ann_caps6_aceit,], family = binomial, glmerControl(optimizer = "Nelder_Mead") )
# works w/ just M-F and Nelder_Mead # works w/ just M-F-U and Nelder_Mead m.age.rand.slopesXsex.b <- bglmer(as.numeric(age.AHY.01)-1 ~ 1 + (1|band) + #ann_caps2 contains all obs of each indivial (1|year) + (1|site) + #(1|site:spp.code) #this would be ideal but prob overkill (site.age.cent |spp.code:sex), data = ann_caps5[-i.ann_caps5_aceit,], family = binomial, glmerControl(optimizer = "Nelder_Mead") )
out <- data.frame(beta = ranef(m.age.rand.slopesXsex.b)[["spp.code:sex"]]$site.age.cent, se = se.coef(m.age.rand.slopesXsex.b)["spp.code:sex"][["spp.code:sex"]][,"site.age.cent"]) row.names(out) <- gsub(":"," ",row.names(out)) ids <- stringi::stri_split(row.names(out),regex= " ", simplify = T) out$Species <- ids[,1] out$Sex <- ids[,2]
From model m.age.rand.slopes
out$Species <- factor(out$Species, levels = focals) out$Status <- "Resident" out$Status[which(out$Species %in% focal.mig)] <- "Migrant" pd <- position_dodge(0.5) ggplot(dat = out, aes(y = exp(beta), x = Species, color = Status, shape = Sex)) + geom_point(position = pd, size = 3) + geom_errorbar(positio = pd, aes(ymax = exp(beta+1.96*se), ymin = exp(beta-1.96*se)), width = 0) + geom_hline(yintercept = exp(0)) + coord_flip() + facet_wrap(~Status,scales = "free") + ylab("Trend")
Aceitillar models seperatealy b/c it does not have a value for "site.age"
Similar to above but required tweaking to converged. Current version should be identical but w/ different optimizer
m.age.fixef.0 <- bglmer(as.numeric(age.AHY.01)-1 ~ 1 + (1|band) + (1|year) + (1|site) + #(1|site:spp.code) #this would be ideal but prob overkill (site.age.cent|spp.code), data = ann_caps2[-i.ann_caps2_aceit,], family = binomial, glmerControl(optimizer = "Nelder_Mead") )
#status fixed effect ## (almost?) always converged m.age.fixef.stat <- update(m.age.fixef.0, . ~ . + status.focals) #site.age.cent fixed effect ## throws warning m.age.fixef.site.age <- update(m.age.fixef.0, . ~ . + site.age.cent) #convergence issues ##fails: with bobyqa under most/all conditions ##works: w/ Nelder_Mean when only intial captures are used AND when all recaps used ### even with Nelder_Mean does throw warning m.age.fixef.add <- update(m.age.fixef.0, . ~ . + status.focals + site.age.cent) #convergence issues ##fails with bobyqa under ALL conditions ##fails ... ##works: w/ Nelder_Mean when only intial captures are used ## m.age.fixef.full <- update(m.age.fixef.0, . ~ . + status.focals*site.age.cent)
ICtab(m.age.fixef.0, m.age.fixef.stat, m.age.fixef.site.age, #conv warn m.age.fixef.add, #fails m.age.fixef.full, base = TRUE, logLik = TRUE, type = "AICc")
#merTools::predictInterval() newdat <- expand.grid( band = m.age.fixef.full@frame$band[1] ,year = unique(m.age.fixef.full@frame$year)[1] ,site = unique(m.age.fixef.full@frame$site)[1] ,site.age.cent = unique(m.age.fixef.full@frame$site.age.cent) ,spp.code = unique(m.age.fixef.full@frame$spp.code)[1] ,status.focals = unique(m.age.fixef.full@frame$status.focals) ) names(m.age.fixef.full@frame) out <- predictInterval(m.age.fixef.full, newdata = newdat, which = "fixed", level = 0.95, #n.sims = 10, stat = "median", include.resid.var = FALSE) out <- cbind(newdat,out)
Determine mean of site.age variable to de-center it while plotting
covar.mean <- mean(ann_caps2$site.age.init,na.rm = TRUE)
Plots
gg.mencia <- ggplot(data = out, aes(y = invlogit(fit), x = site.age.cent+7.557782, color = status.focals)) + geom_line(aes(linetype = status.focals), size = 1) + xlab("Site age") + ylab("Age ratio (AHY vs all ages)") + geom_ribbon(aes(ymax = invlogit(upr), ymin = invlogit(lwr), fill = status.focals), alpha = 0.125, linetype = 0) + ylim(0,1) + theme(legend.position="none")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.