knitr::opts_chunk$set(echo = TRUE)

Load data

load("./data/ann_caps4sex_ratio.RData")

Load libraries

Function that calls all libraries needed for analysis

source("./R/load_libraries.R")
load_libraries()

Data subset

Remove Aceitillar b/c is does not have an age assigned; Aceitillar is analysed is sperate set of scripts

i.aceit <- which(ann_caps4sex_ratio$site == "Aceitillar")

Model Aceitillar

Prep

ann_caps4sex_ratio$year <- factor(ann_caps4sex_ratio$year)
ann_caps4sex_ratio$band <- factor(ann_caps4sex_ratio$band)

summary(ann_caps4sex_ratio[i.aceit,c("band","year","site","spp.code")],10)

Subset just Aceitillar

ann_caps4sex_ratio.aceit <- ann_caps4sex_ratio[i.aceit,]

ID 2001 to 2002

Captures very rare

i.0102 <- which(ann_caps4sex_ratio.aceit$year == "2001-2002")

length(i.0102)
ann_caps4sex_ratio.aceit2 <- ann_caps4sex_ratio.aceit[-i.0102,]
# 
# ann_caps4sex_ratio.aceit2$year <- as.character(ann_caps4sex_ratio.aceit2$year)
# ann_caps4sex_ratio.aceit2$spp.code <- as.character(ann_caps4sex_ratio.aceit2$spp.code)
# with(ann_caps4sex_ratio.aceit2[-i.0102,],
#      table(spp.code,year))

How many cpatures per species

dcast(data = ann_caps4sex_ratio.aceit2,
      formula = spp.code ~ age.AHY.01,
      fun.aggregate = length,
      value.var = "age.AHY.01")

Rare at Aceitillar

rare <- c("BFGR","HILC","NOMO","YFGR","GREP",
"COYE","PAWA","PRAW")
i.rare <- which(ann_caps4sex_ratio.aceit2$spp.code %in% rare)

ann_caps4sex_ratio.aceit3 <- ann_caps4sex_ratio.aceit2[-i.rare,]
dcast(data = ann_caps4sex_ratio.aceit3,
      formula = spp.code ~ age.AHY.01,
      fun.aggregate = length,
      value.var = "age.AHY.01")

GLMM for Aceitillar

Null model

ann_caps4sex_ratio.aceit3$sex.MF <- NA
ann_caps4sex_ratio.aceit3$sex.MF[which(ann_caps4sex_ratio.aceit3$sex == "M")] <- "M"
ann_caps4sex_ratio.aceit3$sex.MF[which(ann_caps4sex_ratio.aceit3$sex == "F")] <- "F"
ann_caps4sex_ratio.aceit3$sex.MF <- factor(ann_caps4sex_ratio.aceit3$sex.MF)

m.sex.fixef.aceit.0 <- bglmer(sex.MF ~ 1 +
               (1|band) +
               (1|year) +
               #(1|site) + #not applicable
               #(1|site:spp.code) #not applicable
               (1|spp.code), # not applicable
              data = ann_caps4sex_ratio.aceit3,
              family = binomial,
              glmerControl(optimizer = "Nelder_Mead")
              )

Check results

#fixed effects
invlogit(fixef(m.sex.fixef.aceit.0))

#random effects for each spp (BLUPs)
invlogit(ranef(m.sex.fixef.aceit.0)[[2]])

fixed effeccts: status.focals

Model effect of status

#status.focals
m.sex.fixef.aceit.stat <- update(m.sex.fixef.aceit.0, . ~ . + status.focals)

#mean parameterization
m.sex.fixef.aceit.means <- bglmer(sex.MF ~ - 1 + status.focals+
               (1|band) +
               (1|year) +
               #(1|site) + #not applicable
               #(1|site:spp.code) #not applicable
               (1|spp.code), #
              data = ann_caps4sex_ratio.aceit3,
              family = binomial,
              glmerControl(optimizer = "Nelder_Mead")
              )

Check output

invlogit(fixef(m.sex.fixef.aceit.means))
invlogit(ranef(m.sex.fixef.aceit.means)[[2]])

Inference

ICtab(m.sex.fixef.aceit.0,
       m.sex.fixef.aceit.stat,
       base = TRUE,
       logLik = TRUE,
       type = "AICc")

Calculate CIs

#merTools::predictInterval()

newdat.aceit <- expand.grid(
          band = m.sex.fixef.aceit.stat@frame$band[1]
          ,year = unique(m.sex.fixef.aceit.stat@frame$year)[1]
          #,site = unique(m.sex.fixef.aceit.stat@frame$site)[1]
          #,site.sex.cent = unique(m.sex.fixef.aceit.stat@frame$site.age.cent)
          ,spp.code = unique(m.sex.fixef.aceit.stat@frame$spp.code)[1]
          ,status.focals = unique(m.sex.fixef.aceit.stat@frame$status.focals)
          )

names(m.sex.fixef.aceit.stat@frame)
out.aceit <- predictInterval(m.sex.fixef.aceit.stat, 
                       newdata =   newdat.aceit,
                which = "fixed", 
                level = 0.95,
                n.sims = 10,
                stat = "mean",
                include.resid.var = FALSE)

out.aceit <- cbind(newdat.aceit,out.aceit)

Graph

names(out.aceit) <- gsub("status.focals","Residency",names(out.aceit))
gg.aceit <- ggplot(data = out.aceit,
        aes(y = invlogit(fit),
            x = Residency,
            color = Residency,
            shape = Residency)) +
  geom_point(size = 3)+
  xlab("Residency") +
  ylab("Sex ratio (% Male)") +
  geom_errorbar(aes(ymax = invlogit(upr),
                  ymin = invlogit(lwr),
                  width = 0)) +
  ylim(0,1) +
  ylab("")

Plot graph of both Mencia and Aceitillar

cowplot::plot_grid(gg.mencia, gg.aceit, labels = c('Mencia', 'Aceitillar'),
          rel_widths = c(2,1.5),
          label_x = c(0.125,0.125))


brouwern/DRmencia documentation built on May 6, 2019, 12:24 p.m.