############################################## R code to perform the analysis by the MCMC method using JAGS software ############################################## #MCMC setting n.chains<-3 n.iter<-50000 n.burnin<-10000 n.thin<-20 nmcmc<-n.chains*(n.iter-n.burnin)/n.thin library("jagsUI") ############ ##a=1: Adult / a=2: Juvenile model_poigam <- list(list(), list()) for(a in 1:2){ model.file <- "C:/Users/yuich/Dropbox/master thesis/script\\RESTmodel.txt" #set pass of the file sink(model.file) cat("model { for(t in 1:n.period){ #Estimating staying time in the REST model for(i in 1:Nstay[t]){ stay[i, t] ~ dlnorm(mm[t], tau[t]) #log-normal distribution is used to estimate staying time. } mstay[t] <- pow(2.718282,(mm[t]+1/tau[t]/2)) #Estimating density at each camera in REST model for(i in 1:Ncam){ pcy[i, t] ~ dpois(mu.y[i, t]) y[i, t] ~ dpois(mu.y[i, t]) mu.y[i, t] <- mu[i, t]*u[i, t] log(mu[i, t]) <- log(S)+log(eff[i, t])+log(rho[i, t])+log(actpro[t])-mm[t]-1/(2*tau[t]) rho[i, t] <- exp(coeffs[5, t]+ coeffs[1, t]*pc1[i]+ coeffs[2, t]*pc2[i]+ coeffs[3, t]*pc3[i]+ coeffs[4, t]*pc4[i]) #Landscape variables are used to estimate local density(=rho). u[i, t] ~ dgamma(alpha,alpha) } #Prior distributions mm[t] ~ dgamma(0.1,0.1) tau[t]~ dgamma(0.1,0.1) r[t] ~ dgamma(0.1,0.01) for(i in 1:4){ coeffs[i, t] ~ dunif(-3, 3) } #Density estimate in 1kmĀ² grid cell for(m in 1:Nmesh){ d_mesh[m, t] <- exp(coeffs[5, t]+ coeffs[1, t]*pc1_m[m]+ coeffs[2, t]*pc2_m[m]+ coeffs[3, t]*pc3_m[m]+ coeffs[4, t]*pc4_m[m]) } #Estimating the density in management-units and the catchability for(u in 1:Nunit){ d_unit[u, t] <- inprod(d_mesh[1:Nmesh, t], envwgt[u, 1:Nmesh]) #Estimating the density in management-units cap_b[u, t] ~ dpois(lambda_b[u, t]) lambda_b[u, t] <- catch_b[t]*d_unit[u, t] *eff_trap_b[u, t] #Estimating the catchability cap_s[u, t] ~ dpois(lambda_s[u, t]) lambda_s[u, t] <- catch_s[t]*d_unit[u, t] *eff_trap_s[u, t] } #prior distribution catch_b[t] ~ dunif(0, 1) #Catchability of box traps catch_s[t] ~ dunif(0, 1) #Catchability of snare traps } #Prior distribution for(t in 2:n.period){ coeffs[5, t] ~ dnorm(coeffs[5, t-1], tau_c)T(0, 5) } coeffs[5, 1] ~ dunif(0, 5) alpha <- 1/invalpha invalpha ~ dgamma(0.1, 0.1) } ") sink() datalist <- list(y=RESTDATA$y[[a]], S=RESTDATA$S, eff=RESTDATA$eff[[a]], stay=RESTDATA$stay[[a]], Nstay=RESTDATA$nstay[[a]], Ncam=RESTDATA$Ncam, n.period=RESTDATA$n.period[[a]], actpro=RESTDATA$actpro[[a]], u=matrix(1, RESTDATA$Ncam, RESTDATA$n.period[[a]]), invalpha=10, pc1=RESTDATA$pc_500mbuffer_agg[,1], pc2=RESTDATA$pc_500mbuffer_agg[,2], pc3=RESTDATA$pc_500mbuffer_agg[,3], pc4=RESTDATA$pc_500mbuffer_agg[,4], pc1_m=RESTDATA$pc_1kmmesh[,1], pc2_m=RESTDATA$pc_1kmmesh[,2], pc3_m=RESTDATA$pc_1kmmesh[,3], pc4_m=RESTDATA$pc_1kmmesh[,4], Nunit=RESTDATA$Nunit, Nmesh=RESTDATA$Nmesh, envwgt=RESTDATA$env_wgt, cap_b=RESTDATA$cap_b[[a]], eff_trap_b=RESTDATA$eff_trap_b[[a]], cap_s=RESTDATA$cap_s[[a]], eff_trap_s=RESTDATA$eff_trap_s[[a]], tau_c=1/4 ) #####DATA DESCRIPTION #y: the number of passage through the focal area #S: size of the focal area #eff: camera-trapping effort #stay: staying time data #Nstay: the number of staying time data #Ncam: the total number of cameras #n.period: the number of months #actpro: activity proportion #invalpha: dispersion parameter #pc: landscape variables #Nunit: the number of management units #Nmesh: the number of 1km cells #envwgt: proportion of habitat area at each mesh in each management unit #cap: the number of captured individuals #eff_trap: trapping efforts of harvest records ##################### inits <- function(){ list(mm=rep(1.5, RESTDATA$n.period[[a]]), tau=rep(1, RESTDATA$n.period[[a]]), coeffs=rbind(matrix(0, 4, RESTDATA$n.period[[a]]), rep(2, RESTDATA$n.period[[a]])), r=rep(0.1, RESTDATA$n.period[[a]]), catch_b=rep(0.1, RESTDATA$n.period[[a]]), catch_s=rep(0.1, RESTDATA$n.period[[a]]) ) } parameters <- c("rho","mstay","coeffs", "d_unit", "catch_b", "catch_s") model_poigam[[a]] <- jags(datalist, inits,parameters, model.file, n.chains=n.chains, n.iter=n.iter, n.burnin=n.burnin, n.thin=n.thin) }