JAGS
Learning Objectives
• Be able to represent ecological systems as a network of
known and unknowns linked by deterministic and
stochastic relationships.
• Understand the basis for factoring simple Bayesian
models using laws of probability.
• Be able to derive conditional probabilities from networks
(DAGS).
• Use conditional probabilities as basis for writing JAGS
code.
Exercise: write a Bayesian model for the model of
light Limitation of Trees
ϒ= max. growth rate at high light
c=minimum light requirement
α=slope of curve at low light
0.6
0.0
0.2
yi ~ Normal ( i , )
0.4
p ( yi | ) Normal ( i , )
Growth Rate
0.8
1.0
( Li c)
i
( ) ( Li c)
0
1
2
3
4
light Availability
5
6
7
Bayesian Networks
Li
, , c
yi
The heads of the arrows specify variables on
the left hand side of the conditioning in the
joint distribution; the tails are on the right hand
Side. Anything without an arrow leading to it
is either known as data or “known” as a prior
with numeric arguments. If the prior is
uninformative, these quantities are “known
unknowns”.
They are also called “directed acyclic graphs (DAG)” and
“Bayesian belief networks”
g ( Li , )
i
proc , , c
Process
model
Parameter
model
( Li c)
i
( ) ( Li c)
p ( yi | ) Normal ( i , )
yi ~ Normal ( i , )
Imperative
Declarative
BUGS
JAGS
BUGS [GUI interface]
R2WinBUGS Bayesian inference Using Gibbs Sampling
R2OpenBUGS
WinBUGS (Windows)
Others…
OpenBUGS (cross-platform)
GeoBUGS
rjags
R2jags
Others…
JAGS [command line]
Just Another Gibbs Sampler
JAGS (cross-platform)
IMPERATIVE
From R:
1. Specify initial conditions
2. Specify data
3. Specify model
4. Specify run conditions
5. Call JAGS
6. Manipulate JAGS output (rjags , coda others)
From JAGS:
1. Choose algorithm
2. Generate chains
3. Send chain output back to R
DECLARATIVE
JAGS
tree.data<-read.csv("Hemlock-light-data.csv")
# Specify initial condition.
mod.inits<-function(){
list(a=rnorm(1,40,2), b=ruinf(1,1.5,2.5), c=runif(1,-5,5), tau=0.001)
}
# Specify data; must be a list.
data=list(
n=nrow(tree.data),
x=as.numeric(tree.data$Light),
y=as.numeric(tree.data$Observed.growth.rate )
)
# JAGS model
model{
for (i in 1:n)
{
mu[i]<-(a*x[i]+c)/((a/b)+x[i]+c) # prediction/scientific model
y[i]~ dnorm(mu[i],tau) # likelihood
}
# Prior distributions
tau~dgamma(0.001,.001)
a~dgamma(0.001,.001)
c~dunif(-10, 10)
b~dgamma(.001,.001)
sigma<-1/sqrt(tau)
} # end of model
The JAGS model can be inserted inside of R code or saved as
a text file and called from R.
# Set run conditions: number of iterations for adaptation & runs, number of chains, etc
n.adapt=500
n.update = 1000
n.iter = 3000
# Call to JAGS
jm=jags.model(“jags_light_example.R",data=data,mod.inits,n.chains=3,n.adapt = n.adapt)
# Burnin the chain (we normally throw these out)
update(jm, n.iter=n.update)
# Generate coda and jags objects for parameters to monitor and deviance.
zm<-coda.samples(jm,variable.names=c("a", "b", "c","mu“, “deviance”),n.iter=n.iter,
n.thin=10)
zj<-jags.samples(jm,variable.names=c("a", "b","c","mu“, “deviance”), n.iter=n.iter,
n.thin=10)
# Plot parameters
plot(zm,,ask = dev.interactive())
xyplot(zm,ask = dev.interactive())
densityplot(zm,ask = dev.interactive())
# Plot predictions
b=summary(zj$mu,quantile,c(.025,.5,.976))$stat
plot(tree.data$Light, tree.data$Observed.growth.rate, xlab="Light", ylab="Growth
Rate", pch=16, col="blue")
lines(tree.data$Light, b[2,])
lines(tree.data$Light, b[1,], lty="dashed")
lines(tree.data$Light, b[3,], lty="dashed")
# Convergence diagnostics
rejectionRate(zm) # sampling conjugate
gelman.diag(zm) # var in chains, stable=1, want r < 1.2
heidel.diag(zm) # requires convergence
raftery.diag(zm) # how many iter you need for convergence
The gang
Kiona Ogle
Mevin Hooten
Tom Hobbs
© Copyright 2026 Paperzz