This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com. When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
This R Markdown document contains the code necessary to implement two different Bayesian models to a sample of Capros aper collected in the west coast of Portugal:
+ Bayesian VBGM 3p: Bayesian inference approach to the regular VBGM
+ Bayesian VBGM 5p: Bayesian inference approach to a 5-parameter VBGM model
library(readxl)
library(rjags)
## Loading required package: coda
## Linked to JAGS 4.3.1
## Loaded modules: basemod,bugs
DADOS_ORGANIZADOS_CAP <- read_excel("DADOS_ORGANIZADOS_CAP.xlsx", sheet = "Dados_idade_final")
DIRECTreads<-na.omit(DADOS_ORGANIZADOS_CAP[,c(1,3,4,19,47)]) # removing any NA cells
colnames(DIRECTreads)<-c("fish_id", "ano", "mes", "tl", "idade") # changing variables names
# chnaging data to list format for the jags model
Age_cap<-DIRECTreads$idade
length_cap<-DIRECTreads$tl
N_cap <- length(length_cap)
# Model
model_VBGM3p_TOTAL <- "
model {
# Priors
L_inf ~ dnorm(20, 0.5) # prior for maximum length
k ~ dnorm(0.5, 2) # prior for the growth rate constant
t0 ~ dnorm(-2, 2) # prior for the time when the organism was born
tau ~ dunif(0,10)
# Likelihood
for (i in 1:N) {
L[i] ~ dnorm(mu[i], tau)
mu[i] <- L_inf * (1 - exp(-k*(t[i] - t0)))
}
}
"
# Parameters to monitor
params <- c("L_inf", "k", "t0")
# Initial values
inits1 <- list(L_inf = 17, k = 0.17, t0 = -2)
# MCMC settings
n_chains <- 3 # number of chains
n_burnin <- 5000 # burn in period
n_iter <- 100000 # number of iterations
n_thin <- 15 # thinning period
# Compiling the model
VBGM3p_TOTAL <- jags.model(textConnection(model_VBGM3p_TOTAL), data = list(t = Age_cap, L = length_cap, N = N_cap), inits = inits1, n.chains = 3)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph information:
## Observed stochastic nodes: 463
## Unobserved stochastic nodes: 4
## Total graph size: 1019
##
## Initializing model
# Running the model
update(VBGM3p_TOTAL, n.iter = n_burnin)
samples_VBGM3p_TOTAL <- coda.samples(VBGM3p_TOTAL, variable.names = params, n.iter = n_iter, thin = n_thin, n.chains = n_chains)
# Checking for autocorrelation and the density distributions
plot(samples_VBGM3p_TOTAL)
# Summarizing the results
summary(samples_VBGM3p_TOTAL)
##
## Iterations = 6015:105990
## Thinning interval = 15
## Number of chains = 3
## Sample size per chain = 6666
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## L_inf 14.0117 0.17777 0.0012571 0.0028513
## k 0.2883 0.02311 0.0001634 0.0003787
## t0 -2.2958 0.19908 0.0014078 0.0031927
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## L_inf 13.687 13.8877 14.0021 14.1246 14.3894
## k 0.245 0.2725 0.2874 0.3035 0.3356
## t0 -2.709 -2.4232 -2.2882 -2.1579 -1.9301
# Model
model_VBGM5p <- "
model {
# Likelihood
for (i in 1:N) {
L[i] ~ dnorm(mu[i], tau)
mu[i] <- Linf * (1 - exp(-k0 * (t[i] - t0))) * (t[i] < t1) +
Linf * (1 - exp(-k0 * (t1 - t0) - k1 * (t[i] - t1))) * (t[i] >= t1)
}
# Priors
Linf ~ dnorm(20, 0.5) # prior for maximum length
k0 ~ dnorm(0.3,1) # prior for the initial growth rate
k1 ~ dnorm(0.15,1) # prior for the second growth rate
t0 ~ dnorm(-2, 0.5) # prior for the time when the organism was born
t1 ~ dnorm(3, 0.5) # prior for the time when the organism changes it's growth rate
tau ~ dunif(0,5) # prior for precision
}
"
# Parameters to monitor
params_5p <- c("Linf", "k0", "t0", "k1", "t1")
# Initial values
inits_5p <- list(Linf = 17, k0 = 0.20, t0 = -2, k1 = 0.17, t1 = 3)
# MCMC settings
n_chains <- 3 # number of chains
n_burnin <- 5000 # burn in period
n_iter <- 1000000 # number of iterations
n_thin <- 100 # thinning period
# Compiling the model
model_5p <- jags.model(textConnection(model_VBGM5p), data = list(t = Age_cap, L = length_cap, N = N_cap), inits = inits_5p, n.chains = n_chains)
## Compiling model graph
## Resolving undeclared variables
## Allocating nodes
## Graph information:
## Observed stochastic nodes: 463
## Unobserved stochastic nodes: 6
## Total graph size: 1170
##
## Initializing model
# Running the model
update(model_5p, n.iter = n_burnin)
samples_cap5p <- coda.samples(model_5p, variable.names = params_5p, n.iter = n_iter, thin = n_thin, n.chains = n_chains)
# Summarizing the results
summary(samples_cap5p)
##
## Iterations = 6100:1006000
## Thinning interval = 100
## Number of chains = 3
## Sample size per chain = 10000
##
## 1. Empirical mean and standard deviation for each variable,
## plus standard error of the mean:
##
## Mean SD Naive SE Time-series SE
## Linf 19.70301 1.36681 7.891e-03 0.0241011
## k0 0.19290 0.02929 1.691e-04 0.0005536
## k1 0.04608 0.01088 6.284e-05 0.0001819
## t0 -2.12622 0.24688 1.425e-03 0.0033873
## t1 2.44435 0.27051 1.562e-03 0.0026030
##
## 2. Quantiles for each variable:
##
## 2.5% 25% 50% 75% 97.5%
## Linf 17.11177 18.75760 19.67946 20.62288 22.45291
## k0 0.14294 0.17205 0.19025 0.21069 0.25796
## k1 0.03009 0.03845 0.04427 0.05181 0.07233
## t0 -2.64205 -2.28458 -2.11369 -1.95095 -1.68480
## t1 1.94708 2.25461 2.43028 2.62257 3.00359
plot(samples_cap5p)