knitr::opts_chunk$set( collapse = TRUE, comment = "#>", warning = F )
Model community abundance to test functioning of glmmTMB and determine if there is an overall decline in total captures over time.
library(avesdemazan) # Model fitting library(lme4) # glmer() for glmm library(glmmTMB) # glmm with AR-1 # library(blme) # bglmer (used?) # # Model evaluation # library(afex) # functionall_fit() # library(multcomp) # library(emmeans) # library(bbmle) # aictab # library(arm) # se.fit() # Result visualization library(ggplot2) # markdown etc library(knitr)
Load data. All individuals captures within a session totaled up, ignoring species.
data(ecuador_tot)
The data is 86 by 6
dim(ecuador_tot)
The data frame looks like this:
head(ecuador_tot, 10)
summary(ecuador_tot)
NOTE: This shows total captures of species that are included in the GLMM, NOT total captures of ALL species. Species that did not have enough captures to meet the inclusion critera are not included, and unbanded individuals are not included.
Split data by session
and plot by year
. This is NOT how the data are modeled because time is set up as a continuous variable.
ggplot(data = ecuador_tot, aes(y = N/tot_net_hours*1000, x = year, color = session)) + geom_point() + facet_wrap(~Location) + geom_smooth(method = lm,se = F) + theme_bw()
Ignore sessions and plot by time_cts
.
ggplot(data = ecuador_tot, aes(y = N/tot_net_hours*1000, x = time_cts)) + geom_point() + facet_wrap(~Location) + geom_smooth(method = lm,se = F) + theme_bw()
Basic GLM for model exploration.
glm_fit <- glm(N ~ 1 + time_cts + # continuous time Location + # Location time_cts*Location + # offset(log(tot_net_hours)), family = poisson, data = ecuador_tot)
Time is significant, but with no time_cts*:Location interaction.
summary(glm_fit)
Poisson GLMM; no autocorrelation, poisson errors.
glmm_fit01 <- lme4::glmer(N ~ 1 + time_cts + # Null Location + Location*time_cts + #(1|session) + #(1|Location) + (1|year:session) + # all captures w/in a session-year (1|year:session:Location) + # all caps w/in a session-year-loc #(0+time_cts|Location) + # rand slope for location offset(log(tot_net_hours)), family = poisson, data = ecuador_tot) summary(glmm_fit01)
Not significant
glmm_fit02 <- lme4::glmer(N ~ 1 + time_cts + # Null #Location + #Location*time_cts + #(1|session) + #(1|Location) + (1|year:session) + # all captures w/in a session-year (1|year:session:Location) + # all caps w/in a session-year-loc (0+time_cts|Location) + # rand slope for location offset(log(tot_net_hours)), family = poisson, data = ecuador_tot) summary(glmm_fit02) # fixef(glmm_fit)[2] # slopes_loc <- fixef(glmm_fit)[2]+ranef(glmm_fit)$Location # # CI.up # CI.lo <- fixef(glmm_fit)[2]-ranef(glmm_fit)$Location # # # +se.ranef(glmm_fit)$Location*2 # fixef(glmm_fit)[2]-se.ranef(glmm_fit)$Location*2
library(splines) glmmTMB_poisson <- glmmTMB(N ~ 1 + time_cts + # Null Location + Location*time_cts + (1|year:session) + # all captures w/in a session-year (1|year:session:Location) + # all caps w/in a session-year-loc (0+time_cts|Location) + # rand slope for location offset(log(tot_net_hours)), family = poisson, data = ecuador_tot) summary(glmmTMB_poisson)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.