This vignette continues the example of the pied flycatchers discussed in the vignette “pied-flycatchers-1” by adding a random slope to the fixed effects model. I highly recommend that you work through the other vignette first.
First you have to load the package:
## Load package
library(dalmatian)
The raw data for this example is provided in the package and can be accessed with:
## Load pied flycatcher data
data(pied_flycatchers_1)
As before we will consider load as a response variable rounded to the nearest whole number and define the lower and upper bounds:
## Create variables bounding the true load
$lower=ifelse(pfdata$load==0,log(.001),log(pfdata$load-.049)) pfdata
## Warning in log(pfdata$load - 0.049): NaNs produced
$upper=log(pfdata$load+.05) pfdata
The model we consider will be the similar to the random effects model in “pied-flycatchers-1”. We will modify this model by including a random, individual slope for the effect of \(log(IVI)\) as well as the random intercept in the mean model. The new model for the mean will be: \[\mu_{ij}=\beta_0 + \beta_1 \mathrm{log(IVI)_{ij}} + \beta_2 \mathrm{broodsize}_i + \beta_3 \mathrm{sex}_i + \epsilon_{1i} + \epsilon_{2i} \mathrm{log(IVI)_{ij}}\] For convenience, we will also simplify the model of the dispersion by assuming that the variance is constant across all observations. This can be done by setting: \[\log(\phi_{ij})=\psi_0\].
The objects defining the new mean and dispersion components are specified as:
# Random component of mean
=list(fixed=list(name="alpha",
mymeanformula=~ log(IVI) + broodsize + sex,
priors=list(c("dnorm",0,.001))),
random=list(name="epsilon",
formula=~-1 + indidx + indidx:log(IVI)))
# Random component of dispersion
=list(fixed=list(name="psi",
mydispformula=~1,
priors=list(c("dnorm",0,.001))),
link="log")
The new model can now be run using dalmatian
in exactly the same way as above. However, we will make one change. In order to shorten the convergence time, we will base initial values on the results of the fixed effects model and then provide these as input. In particular, we will define initial values for the fixed effects of both the mean and dispersion components by taking the values from the final iterations of each of the chains run for the fixed effects model. For convenience, we set the random effects variances equal to 1 in all three chains. Note that the chains have been run for a relatively small number of iterations and heavily subsampled to satisfy the size requirements for packages on . Neither of these is recommended in practice.
## Set working directory
## By default uses a system temp directory. You probably want to change this.
<- tempdir()
workingDir
## Define list of arguments for jags.model()
<- list(file=file.path(workingDir,"pied_flycatcher_3_jags.R"),n.adapt=1000)
jm.args
## Define list of arguments for coda.samples()
<- list(n.iter=1000)
cs.args
## Run the model using dalmatian
<- dalmatian(df=pfdata,
pfmcmc3 mean.model=mymean,
dispersion.model=mydisp,
jags.model.args=jm.args,
coda.samples.args=cs.args,
rounding=TRUE,
lower="lower",
upper="upper",
debug=FALSE)
Once again, we can use the wrapper functions provided with dalmatian
to conduct convergence diagnostics and compute summary statistics for the posterior distribution from which we can make inference. Note that the chains have been thinned prior to creating the traceplots in order to reduce the size of the package.
## Compute convergence diagnostics
<- convergence(pfmcmc3,raftery=list(r=.01)) pfconvergence3
## Gelman-Rubin diagnostics
$gelman pfconvergence3
## Potential scale reduction factors:
##
## Point est. Upper C.I.
## alpha.(Intercept) 1.00 1.01
## alpha.log(IVI) 1.00 1.01
## alpha.broodsizeIncreased 1.01 1.02
## alpha.sexMale 1.02 1.06
## psi.(Intercept) 1.00 1.01
## sd.epsilon.indidx 1.01 1.04
## sd.epsilon.indidx:log(IVI) 1.01 1.03
## Raftery diagnostics
$raftery pfconvergence3
##
## Quantile (q) = 0.025
## Accuracy (r) = +/- 0.005
## Probability (s) = 0.95
##
## You need a sample size of at least 3746 with these values of q, r and s
## Effective sample size
$effectiveSize pfconvergence3
## alpha.(Intercept) alpha.log(IVI)
## 359.1056 1548.6355
## alpha.broodsizeIncreased alpha.sexMale
## 172.0363 143.7425
## psi.(Intercept) sd.epsilon.indidx
## 538.2383 447.4075
## sd.epsilon.indidx:log(IVI)
## 122.9022
## Generate traceplots
<- traceplots(pfmcmc3, nthin = 20, show = FALSE) pftraceplots3
## Fixed effects for mean
$meanFixed pftraceplots3
## Fixed effects for dispersion
$dispersionFixed pftraceplots3
## Random effects variances for mean
$meanRandom pftraceplots3
## Random effects variances for dispersion
$dispersionRandom pftraceplots3
## NULL
## Compute numerical summaries
<- summary(pfmcmc3) pfsummary3
## Print numerical summaries
pfsummary3
##
## Iterations = 1001:2000
## Thinning interval = 1
## Number of chains = 3
## Sample size per chain = 1000
##
## Posterior Summary Statistics for Each Model Component
##
## Mean Model: Fixed Effects
## Mean Median SD Lower 95% Lower 50% Upper 50% Upper 95%
## (Intercept) -3.11 -3.10 0.09 -3.28 -3.17 -3.05 -2.93
## log(IVI) 0.11 0.12 0.01 0.09 0.11 0.13 0.14
## broodsizeIncreased 0.10 0.10 0.07 -0.02 0.04 0.13 0.24
## sexMale -0.08 -0.08 0.07 -0.21 -0.13 -0.04 0.05
##
## Mean Model: Random Effects
## Mean Median SD Lower 95% Lower 50% Upper 50% Upper 95%
## indidx 0.21 0.21 0.03 0.14 0.18 0.23 0.27
## indidx:log(IVI) 0.02 0.02 0.01 0.00 0.01 0.03 0.04
##
## Dispersion Model: Fixed Effects
## Mean Median SD Lower 95% Lower 50% Upper 50% Upper 95%
## (Intercept) -0.75 -0.75 0.03 -0.8 -0.77 -0.73 -0.69
## Generate caterpillar
<- caterpillar(pfmcmc3,show = FALSE) pfcaterpillar3
## Fixed effects for mean
$meanFixed pfcaterpillar3
## Fixed effects for dispersion
$dispersionFixed pfcaterpillar3
The convergence diagnostics and traceplots suggest that the chains should be run for longer. In particular, the effective sample size for the variance of the random slopes is very small, but we will proceed with inference for illustration. In the numerical summaries we see that the estimated variance of the random slopes is very small, 0.0204748 with 95% credible interval (2.7611746^{-5},0.040385). This suggests that there is very little inter-individual variation in the effect of log(IVI)
.
Estimates of the individual random effects (predictions for frequentists) can be obtained from the function ranef()
. This function computes posterior summary statistics for the individual random effects. In this case, the random effects for the mean include both the intercept and the effect of log(IVI)
.
The following code computes the summary statistics and plots the predicted values of the random slopes with HPD 95% confidence intervals.
## Compute summary statistics for random effects
<- ranef(pfresults3) pfranef3
## Load ggplot2
library(ggplot2)
## Identify number of individuals
<- nlevels(pfdata$indidx)
nind
## Plot predicted random slopes
ggplot(data=as.data.frame(pfranef3$mean[nind+(1:nind),]),aes(x=1:nind,y=Mean)) +
geom_point() +
geom_errorbar(aes(ymin=`Lower 95%`,ymax=`Upper 95%`)) +
geom_abline(intercept=0,slope=0)
Note that the 95% credible intervals for all of the values include 0. This is further evidence that the effect of log(IVI)
does not vary significantly between the individuals.