RJAGS Code

advertisement
RJAGS Code
#----------------------------------------------------------------------------------##
RJAGS Code
#
Specifies the model environment
#
Finds posterior samples
#
Convergence tests
#
Parameter estimates and 95% credible intervals
#
Pseudo P-values
#-----------------------------------------------------------------------------------
## Preliminary
# R Packages needed:
# "rjags"
# "reshape2" if you store data in wide format (RJAGS/JAGS requires long format)
# You also need JAGS http://mcmc-jags.sourceforge.net/ and need to specify a
JAGS model (See ESM 3)
#To standardise the variables
meanuse =
apply(cbind(datause$Dur,datause$WT,datause$SPERM,datause$HC),2,mean) #
finds the mean values for duration, crab weight, spermatophore length and
haemocyanin
sduse = apply(cbind(datause$Dur,datause$WT,datause$SPERM,datause$HC),2,sd)
# finds the SD for duration, crab weight, spermatophore length and haemocyanin
#Define variables
StDuR = (datause$Dur-meanuse[1])/sduse[1] # standardized Duration variable
POP = datause$POP # POP (HAN/MTB) area variable.
ID = datause$ID # individual index/Crab ID
OBS = as.numeric(datause$OBS) # observation no. index
StOBS = (OBS - mean(OBS))/sd(OBS)
StWT = (datause$WT - meanuse[2])/sduse[2] # standardized crab weight
StSPERM = (datause$SPERM - meanuse[3])/sduse[3] # standardized
spermatophore length
StHC = (datause$HC - meanuse[4])/sduse[4] # standardized haemocyanin conc
## Specify the setup for the MCMC
nchain = 3 # number of markov chains to run
nadapt = 10000 # burn-in
nrun = 25000 #20000 # posterior sample for each chain
## Find the inital values
inituse = vector('list',nchain)
for(i in 1:nchain){
beta = rnorm(8) # initial value for GLM of mean
gamma = rnorm(5) # initial value for GLM for st. dev
tau.mu = rlnorm(2) # initial value for random effects precision parameters
tau.sd = rlnorm(1) # initial value for random effects precision parameters
redun.mu = rnorm(2) # initial value for redundant parameterization -- these should
not be started at 0
redun.sd = rnorm(1) # initial value for redundant parameterization -- these should
not be started at 0
inituse[[i]] = list(beta=beta,gamma=gamma,tau.mu=tau.mu,tau.sd=tau.sd,redun.mu
= redun.mu,redun.sd=redun.sd) # list with all initial values
}
## The following line runs the adaptive phase of the MCMC in JAGS
# you need the JAGS model specified and saved beofre you can proceed
jags.data =
list(y = StDuR,
POPx=1*(POP=="MTB"),
IDx=ID,
StOBSx=StOBS,
StWTx=StWT,
StSPERMx=StSPERM,
StHCx=StHC,
nID=length(unique(ID)),
n=length(StDuR))
m1 = jags.model('SHC_model_bugs.R',
data = jags.data,inits=inituse,
n.chains = nchain, n.adapt = nadapt)
##The following line then finds the posterior sample for parameters listed
output = jags.samples(m1,c("beta","gamma", "sd.mu","sd.sd","re.mu","re.sd"), n.iter
= nrun)
## Summaries using coda.samples
# Parameters describing the mean model:
summary(as.mcmc(t(output$beta[,,1])))
# Parameters describing the standard deviation model:
summary(as.mcmc(t(output$gamma[,,1])))
# Parameters describing the random effects distributions
summary(as.mcmc(t(output$sdind[,,1])))
summary(as.mcmc(output$sdobs[,,1])) #Note no 't' transponse function as only 1
random effect here
## Convergence diagnostics
gelman.diag(out1)
## Psuedo-P values
#Psuedo P for Beta (mean model fixed effects)
tmp1 <- apply(output[["beta"]],1,function(M) mean(M>0))
tmp2 <- apply(output[["beta"]],1,function(M) mean(M<0))
p.beta <- 2*pmin(tmp1,tmp2)
#Psuedo P for Gamma (SD model fixed effects)
tmp1 <- apply(output[["gamma"]],1,function(M) mean(M>0))
tmp2 <- apply(output[["gamma"]],1,function(M) mean(M<0))
p.gamma <- 2*pmin(tmp1,tmp2)
p.gamma
Download