Re: Autocorrelated MCMCs and choice of samplers to resolve it

90 views
Skip to first unread message

Perry de Valpine

unread,
Nov 24, 2020, 5:14:18 PM11/24/20
to Andrew, nimble-users
Hi Andrew,

Many thanks for sharing a full example and inviting ideas on it. It looks like this was a daunting request for a fairly involved model.  Any takers out there?

-Perry

On Fri, Nov 6, 2020 at 2:22 PM Andrew <ajden...@gmail.com> wrote:
Hello,

I am having trouble with reducing autocorrelation and increasing mixing in my MCMCs for different N-mixture models. Though the input datasets are different, I suspect that some of the underlying issues may be related.

One model is a multi-species (Dorazio-Royle-Yamaura) N-mixture, and the other is a single-species (Dail-Madsen) N-mixture. In addition to MCMC autocorrelation and insufficient mixing, I have also found multimodal posteriors for many of the model parameters estimated, especially those in my process- and observation-level submodels. To date, I have not yet determined whether such problems relate to poor initialization, choices of priors, and/or choices of samplers. I have made many changes to each over the past few months.

Would any users/developers be willing to please look over the enclosed model, perhaps run it, and later provide feedback on any potential issues with the initialization, choices of priors, and/or choices (or uses) of samplers?

To date, I have applied RW_block (and sometimes with propCov constraints) or AF_slice samplers to problematic parameters, but I must admit that I don't know how best to fine tune the samplers toward efficient processing, especially considering the input data includes counts of both rare and common species.

Below is a reproducible example for the multi-species N-mixture model, and attached is the necessary input data, including z-scored covariates. Any insights or suggestions to improving this model would be much appreciated, and so thank you for your time and assistance in advance.

Sincerely,
Andrew

###############################################################

library(nimble) ; library(coda)

win.data <- readRDS("win.data.rds")  # load the input data

modelCode <- nimbleCode({
  
  ## Priors for community parameters
  for(k in 1:nspec){
    phi[k] ~ dunif(0, 1)  # adjustment for zero-inflation
    
    ## Alpha parameters (detection)
    alpha0[k] ~ dnorm(mu.alpha0, sd=sd.alpha0)  # species-specific intercepts on detection
    alpha1[k] ~ dnorm(mu.alpha1, sd=sd.alpha1)     # linear julian day on detection
    alpha2[k] ~ dnorm(mu.alpha2, sd=sd.alpha2)      # quadratic julian day on detection
    
    ## Beta parameters (abundance)
    for(y in 1:nyear){
      beta0[k,y] ~ dnorm(mu.beta0[y], sd=sd.beta0[y])     # species- and year-specific intercepts on abundance
    }
    beta1[k] ~ dnorm(mu.beta1, sd=sd.beta1)     # cp type (dummy coded, 0/1) on abundance
    beta2[k] ~ dnorm(mu.beta2, sd=sd.beta2)      # percent forb cover on abundance
    beta3[k] ~ dnorm(mu.beta3, sd=sd.beta3)      # landscape metrics on abundance
  }
  tau.lam <- pow(sd.lam, -2)
  sd.lam ~ dunif(0, 2)     # site heterogeneity in abundance
  tau.p <- pow(sd.p, -2)
  sd.p ~ dunif(0, 2)      # site heterogeneity in detection
  
  ## Priors for community hyperparameters
  for(y in 1:nyear){
    mu.beta0[y] ~ dnorm(0, 0.01)
    sd.beta0[y] ~ dunif(0, 10)
  }
  # abundance model
  mu.beta1 ~ dnorm(0, 0.01)
  sd.beta1 ~ dunif(0, 10)
  mu.beta2 ~ dnorm(0, 0.01)
  sd.beta2 ~ dunif(0, 10)
  mu.beta3 ~ dnorm(0, 0.01)
  sd.beta3 ~ dunif(0, 10)
  
  # detection model
  mu.alpha0 ~ dnorm(mu.a0L, sd.alpha0)
  sd.alpha0 ~ dunif(0, 1)
  mu.a0 ~ dunif(0, 1)
  mu.a0L <- logit(mu.a0)
  mu.alpha1 ~ dnorm(0, 0.01)
  sd.alpha1 ~ dunif(0, 10)
  mu.alpha2 ~ dnorm(0, 0.01)
  sd.alpha2 ~ dunif(0, 10)
  
  ## Ecological model for true abundance (process model)
  for(k in 1:nspec){
    for(i in 1:nsite){
      eps.lam[i] ~ dnorm(0, tau.lam)       spatial random effects on abundance
      a[i,k] ~ dbern(phi[k])     # adjustment for zero-inflation
      N[i,k] ~ dpois(a[i,k] * lambda[i,k])
      loglam[i,k] <- beta0[k, year[i]] + beta1[k] * CPType23[i] + beta2[k] * PercForb[i] + beta3[k] * OC50_1[i] + eps.lam[i]
      loglam.lim[i,k] <- min(250, max(-250, loglam[i,k]))     # stabilize log
      lambda[i,k] <- exp(loglam.lim[i,k])
      
      ## Compute presence/absence matrix z (for N > 0) from latent abundance
      z[i,k] <- step(N[i,k]-1)  # returns TRUE if N >= 0
    }
  }
  
  ## Observation model for replicated counts (detection model)
  for(k in 1:nspec){
    for(i in 1:nsite){
      for(j in 1:nrep){
        eps.p[i] ~ dnorm(0, tau.p)      # spatial random effects on detection
        Yc[i,j,k] ~ dbin(p[i,j,k], N[i,k])
        p[i,j,k] <- 1 / (1 + exp(-lp.lim[i,j,k]))
        lp.lim[i,j,k] <- min(250, max(-250, mu[i,j,k]))      # stabilize logit
        mu[i,j,k] <- alpha0[k] + alpha1[k] * JulianDay[i] + alpha2[k] * pow(JulianDay[i], 2) + eps.p[i]
      }
    }
  }
  
  for(k in 1:nspec){
    for(y in 1:nyear){
      mlambda[k,y] <- phi[k] * exp(beta0[k,y])     # expected abundance on natural scale
    }
    Nocc.fs[k] <- sum(z[1:nsite,k])     # number of occupied sites
    logit(mp[k]) <- alpha0[k]       # mean detection on natural scale
  }
  for (i in 1:nsite) {
    Nrich[i] <- sum(z[i,1:nspec])       # number of occurring species at each site
    Nbirds[i] <- sum(N[i,1:nspec])     # number of total individuals at each site
  }
}) 

Ycd <- win.data$Yc

nyear <- length(c(2005,2006))
nsite <- dim(Ycd)[1]
nrep <- (Ycd)[2]
nspec <- dim(Ycd)[3]

const <- list(year=c(rep(1,14),rep(2,14)), nyear=nyear, nsite=nsite, nrep=nrep, nspec=nspec)

## Prepare initial values
ast <- matrix(rep(1, dim(Ycd)[3] * dim(Ycd)[1]), 
              nrow = dim(Ycd)[1])     # all species will begin as present
some.more <- 5
Nst <- apply(Ycd, c(1,3), max, na.rm = T) + some.more     # initial abundance > obs abundance
initFunction <- function() list(
  # starting values for latent N and its dependents
  a = ast,
  phi = runif(dim(Ycd)[3],0,1),
  N = Nst,      # starting values for species-specific abundance
  
  ## starting values for community parameters
  # detection process
  alpha0 = rnorm(dim(Ycd)[3]),     # detection intercepts
  alpha1 = rnorm(dim(Ycd)[3]),     # linear julian day on detection
  alpha2 = rnorm(dim(Ycd)[3]),       # quadratic julian day on detection
  
  # abundance process
  beta0 = matrix(rnorm(dim(Ycd)[3]*nyear),
                 nrow = dim(Ycd)[3], ncol = nyear),      # abundance intercepts
  beta1 = rnorm(dim(Ycd)[3]),      # cp type (dummy coded) on abundance
  beta2 = rnorm(dim(Ycd)[3]),       # percent forb cover on abundance
  beta3 = rnorm(dim(Ycd)[3]),       # landscape metrics on abundance
  
  ## starting values for community hyperparameters
  # detection process
  mu.alpha0 = rnorm(1, logit(runif(1,0,1)), runif(1,0,1)),
  mu.a0 = runif(1,0,1),
  mu.beta0 = rnorm(2),
  sd.beta0 = runif(2,0,5),
  sd.alpha0 = runif(1,0,1),
  mu.alpha1 = rnorm(1),
  sd.alpha1 = runif(1,0,5),
  mu.alpha2 = rnorm(1),
  sd.alpha2 = runif(1,0,5),
  
  # abundance process
  mu.beta1 = rnorm(1),
  sd.beta1 = runif(1,0,5),
  mu.beta2 = rnorm(1),
  sd.beta2 = runif(1,0,5),
  mu.beta3 = rnorm(1),
  sd.beta3 = runif(1,0,5),
  
  # spatial random effects on abundance/detection
  sd.lam=runif(1, 0, 2), eps.lam=rnorm(nsite),
  sd.p=runif(1, 0, 2), eps.p=rnorm(nsite))

params <- c("phi", "mlambda", "mp", 
            "beta0", "beta1", "beta2", "beta3",
            "alpha0", "alpha1", "alpha2", 
            "N", "Nrich", "Nbirds")

model <- nimbleModel(code = modelCode, name = "modelCode",
                     constants = const, data = win.data,
                     inits = initFunction())

Cmodel <- compileNimble(model)
modelConf <- configureMCMC(model)
modelConf$setMonitors(params)
modelConf$removeSampler(c("alpha0","alpha1","alpha2","beta0","beta1","beta2","beta3","p"))

modelConf$addSampler(target = c("p[1:28,1:3,1:35]"), type = "RW_block")
modelConf$addSampler(target = c("beta0[1:35,1]"), type = "RW_block") #year 1 intercepts
modelConf$addSampler(target = c("beta0[1:35,2]"), type = "RW_block") #year 2 intercepts
modelConf$addSampler(target = c("beta1[1:35]","beta2[1:35]","beta3[1:35]"), type = "RW_block")
modelConf$addSampler(target = c("alpha1[1:35]","alpha2[1:35]"), type = "RW_block")
modelConf$addSampler(target = c("alpha0[1:35]"), type = "RW_block")

modelMCMC <- buildMCMC(modelConf)
CmodelMCMC <- compileNimble(modelMCMC, project = model)

ni <- 10000 ; nb <- 5000 ; nt <- 5 ; nc <- 3 ; set.seed <- 999
results <- runMCMC(CmodelMCMC, niter=ni, nburnin=nb, thin=nt, nchains=nc,
                   samples=TRUE, summary=TRUE, samplesAsCodaMCMC=TRUE, setSeed = seed)


--
You received this message because you are subscribed to the Google Groups "nimble-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nimble-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nimble-users/0ef71001-eb40-4505-b655-be0441010a82n%40googlegroups.com.

Andrew D

unread,
Nov 25, 2020, 2:47:49 PM11/25/20
to Perry de Valpine, nimble-users
Thanks, Perry. The model (and associated ask) is certainly daunting, but I have recently acquired both improved chain mixing and closer convergence on stable estimates, which was a consequence of (1) adjusting my input data (e.g., removing problematic samples) and (2) using "AF_slice" samplers on much longer chains simulated in parallel. I think that I may be on a better track now.

Best,
Andrew
--
Andrew J. Dennhardt
Graduate Research Assistant
Michigan State University
480 Wilson Road
Room 13, Natural Resources Bldg.
East Lansing, MI 48824
Phone: 517-355-3030
Fax: 517-432-1699
Mobile: 309-738-3228
Email: denn...@msu.edu
Reply all
Reply to author
Forward
0 new messages