0

I'm super new in bayesian analysis and I'm trying to practice with an example for Classic Capture-recapture models: Mh2

This is my code

nind <- dim(venados)[1] 
K <- 43 
ntraps <- 13
M <- 150 
nz <- M - nind 
Yaug <- array(0, dim = c(M, ntraps, K))
Yaug[1:nind,,] <- venados
y <- apply(Yaug, c(1,3), sum) 
y[y > 1] <- 1
Bundle data
data1 <- list(y = y, nz = nz, nind = nind, K = K, sup = Buffer)

# Model JAGS 
sink("Mh2_jags.txt")
cat("
    model{

  # Priors
    p0 ~ dunif(0,1)
    mup <- log(p0/(1-p0))
    sigmap ~ dunif(0,10)
    taup <- 1/(sigmap*sigmap)
    psi ~ dunif(0,1)  

  # Likelihood
    for (i in 1:(nind+nz)) {   
    z[i] ~ dbern(psi)
    lp[i] ~ dnorm(mup,taup)
    logit(p[i]) <- lp[i]
    y[i] ~ dbin(mu[i],K)  
 } # i

    N <- sum(z[1:(nind+nz)])
    D <- N/sup*100
} # modelo
",fill = TRUE)
sink()

# Inicial values
inits <- function(){list(z = as.numeric(y >= 1), psi = 0.6, p0 = runif(1), sigmap = runif(1, 0.7, 1.2), lp = rnorm(M, -0.2))}

params1 <- c("p0","sigmap","psi","N","D")

# MCMC 
ni <- 10000; nt <- 1; nb <- 1000; nc <- 3

# JAGS and posteriors
fM2 <- jags(data1, inits, params1, "Mh2_jags.txt", n.chains = nc, n.thin = nt, n.iter = ni, n.burnin = nb)

I received this error message

Processing function input....... 

Done. 
 
Compiling model graph
   Resolving undeclared variables
Deleting model

Error in jags.model(file = model.file, data = data, inits = inits, n.chains = n.chains,  : 
  RUNTIME ERROR:
Compilation error on line 16.
Dimension mismatch in subset expression of y

I have read that some letters as s and n have to be changed. However, I do not know what to do. Please if you could give an advice.

Thank you very much

SLV
  • 1

1 Answers1

0

The issue is because y is two dimensional but the model assumes it is one dimensional. If you are assuming that the secondary surveys are i.i.d. Bernoulli trials (and each session had K trials)n then you would just need to take the sum of the rows of the y matrix. Assuming this is the case then you just need to modify a couple lines at the top of this script.

nind <- dim(venados)[1] 
K <- 43 
ntraps <- 13
M <- 150 
nz <- M - nind 
Yaug <- array(0, dim = c(M, ntraps, K))
Yaug[1:nind,,] <- venados
y <- apply(Yaug, c(1,3), sum) 
y[y > 1] <- 1

# Take the rowSum
y_vector <- rowSums(y)

# Use y_vector instead of y
data1 <- list(y = y_vector, nz = nz, nind = nind, K = K, sup = Buffer)

Conversely, if you wanted to include covariates for the observational process (and those covariates vary by survey) you would use the matrix y and modify the model.

sink("Mh2_jags_Kloop.txt")
cat("
    model{

  # Priors
    p0 ~ dunif(0,1)
    mup <- log(p0/(1-p0))
    sigmap ~ dunif(0,10)
    taup <- 1/(sigmap*sigmap)
    psi ~ dunif(0,1)  

  # Likelihood
    for (i in 1:(nind+nz)) {   
    z[i] ~ dbern(psi)
    lp[i] ~ dnorm(mup,taup)
    logit(p[i]) <- lp[i]
      # Loop over K surveys
      for(j in 1:K){
        y[i,j] ~ dbern(p[i]*z[i])
    }  
 } # i

    N <- sum(z[1:(nind+nz)])
    D <- N/sup*100
} # modelo
",fill = TRUE)
sink()

Finally, you don't specify what mu is within the model. I think you want it to be p, but you also need to link the latent state model to the observational state model (if z=0 then that individual cannot be sampled. In this case you would interpret psi as the probability that nind+nz individuals are at your site.

# Model JAGS 
sink("Mh2_jags.txt")
cat("
    model{

  # Priors
    p0 ~ dunif(0,1)
    mup <- log(p0/(1-p0))
    sigmap ~ dunif(0,10)
    taup <- 1/(sigmap*sigmap)
    psi ~ dunif(0,1)  

  # Likelihood
    for (i in 1:(nind+nz)) {   
    z[i] ~ dbern(psi)
    lp[i] ~ dnorm(mup,taup)
    logit(p[i]) <- lp[i]
    y[i] ~ dbin(p[i] * z[i],K)  
 } # i

    N <- sum(z[1:(nind+nz)])
    D <- N/sup*100
} # modelo
",fill = TRUE)
sink()
mfidino
  • 3,030
  • 1
  • 9
  • 13