Description Usage Format Details Source Examples
This help page describes how we fitted the model f2, supposing a
differente habitat use by the roe deer in Chize, la Petite Pierre and
Trois-Fontaines. The R and JAGS code used to fit the model is
included in the Examples section of this help page, and the dataset
coefficientModel2
is an object of class mcmc.list
(package rjags) containing the sampled values of the coefficients.
1 | data("coefficientsModel2")
|
This object is an object of class mcmc.list
containing the
values of the coefficients sampled for three chains.
The fitted model was the following (see paper) for the multinomial logit of the probability of use of the scrubs:
log(P(scrubs)/P(CWS)) = a0f[site] + apf[site] * PoleStageInHomeRange + aff[site] * ScrubsInHomeRange + eps1
Where eps1
is a normal overdispersion residual, and for the
probability of use of the pole stage:
log(P(pole stage)/P(CWS)) = a0p[site] + app[site] * PoleStageInHomeRange + apf[site] * ScrubsInHomeRange + eps2
Where eps2
is a normal overdispersion residual, and site
is an integer value taking the value 1 (Chize), 2 (La Petite Pierre)
or 3 (Trois-Fontaines).
Sonia Said, Centre national d'etude et de recherche appliquee "Cervides-Sangliers", Office national de la chasse et de la faune sauvage, Birieux, Ain, France.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | #########################################
##
## 1. Model fit
## We load the data used for the fit:
data(HS3sites)
## We remove the information concerning the meadows
## (negligible, see the paper)
HS3sites$locs$meadows <- NULL
HS3sites$hr$meadows <- NULL
## Calculates the total number of relocations
HS3sites$N <- apply(HS3sites$locs,1,sum)
## stores the number of animals
HS3sites$J <- nrow(HS3sites$locs)
## For a better mixing, we scale the covariates
HS3sites$hr <- scale(HS3sites$hr)
## Not run:
## starting values
init <- list(
list(a0f=rep(-10,3),apf=rep(0,3),aff=rep(0,3), afp=rep(0,3),app=rep(0,3)),
list(a0f=rep(-10,3),apf=rep(1,3),aff=rep(1,3), afp=rep(-1,3),app=rep(-1,3)),
list(a0f=rep(-10,3),apf=rep(-1,3),aff=rep(1,3), afp=rep(1,3),app=rep(-1,3)))
## We store the JAGS code for the model in the file model2.jags
cat("model {
for (s in 1:3) {
a0f[s] ~ dnorm(0,0.001)
a0p[s] ~ dnorm(0,0.001)
aff[s] ~ dnorm(0,0.001)
afp[s] ~ dnorm(0,0.001)
apf[s] ~ dnorm(0,0.001)
app[s] ~ dnorm(0,0.001)
}
sig ~ dunif(0,100)
for (j in 1:J) {
eps1[j]~dnorm(0,sig)
eps2[j]~dnorm(0,sig)
eps3[j]~dnorm(0,sig)
ep[j,1] <- a0f[site[j]] + aff[site[j]]*hr[j,1] + apf[site[j]]*hr[j,2] + eps1[j]
ep[j,2] <- a0p[site[j]] + afp[site[j]]*hr[j,1] + app[site[j]]*hr[j,2] + eps2[j]
p[j,1] <- exp(ep[j,1])/(1+exp(ep[j,1])+exp(ep[j,2]))
p[j,2] <- exp(ep[j,2])/(1+exp(ep[j,1])+exp(ep[j,2]))
p[j,3] <- 1/(1+exp(ep[j,1])+exp(ep[j,2]))
locs[j,]~dmulti(p[j,],N[j])
}
}
", file = "model2.jags")
################################################################
##
## IF YOU WANT TO CHECK HOW THE FIT WORKS, COPY AND PASTE THE
## FOLLOWING CODE, BUT NOTE THAT IT CAN TAKE SEVERAL HOURS TO EXECUTE!!!
## IF YOU DO NOT WANT TO WAIT, THE RESULTING SAMPLED COEFFICIENTS ARE
## STORED IN THE DATASET coefficientsModel2
## THAT IS LOADED FURTHER BELOW.
## initialization and burn-in (very slow)
mo2 <- jags.model("model2.jags", n.adapt=20000, n.chain=3,
data=HS3sites, inits=init)
## Sample 2000000 coefficients from the posterior (warning! very slow)
coefficientsModel2 <- coda.samples(mo2,
variable.names=c("a0f", "apf", "aff", "afp", "a0p",
"app", "sig"),
n.iter=2000000, thin=50)
## End(Not run)
## TO AVOID WAITING A LONG TIME FOR THE FIT, WE HAVE STORED THE RESULTS
## IN THE DATASET coefficientsModel2. LOAD THESE RESULTS HERE:
data(coefficientsModel2)
##################################################################
##################################################################
##################################################################
##################################################################
##################################################################
##################################################################
##################################################################
##
##
##
## As we note in the paper, there were three animals characterized by a
## very particular composition of their home range: the individual X15
## in Chize was characterized by a very large availability of scrubs,
## the individual X24 in LPP was characterized by a very low
## availability of scrubs, and the individual X64 in TF was
## characterized by both an availability of scrubs nearly equal to zero
## and a large availability of pole stage.
##
##
## 2. We fitted the model f2
## after removal of these three individuals
## Removal of the three individuals
dj <- HS3sites
dj$locs <- dj$locs[!row.names(dj$hr)%in%c("X15_ch","X24_lpp","X64_tf"),]
dj$N <- dj$N[!row.names(dj$hr)%in%c("X15_ch","X24_lpp","X64_tf")]
dj$site <- dj$site[!row.names(dj$hr)%in%c("X15_ch","X24_lpp","X64_tf")]
dj$hr <- dj$hr[!row.names(dj$hr)%in%c("X15_ch","X24_lpp","X64_tf"),]
dj$J <- nrow(dj$locs)
## Not run:
## Fit the model
## WARNING!!!!!!!!!!!!!!!!!!!!!!
## VERY SLOW!!!!!!!!!!!!!!!!!!!
mo2a <- jags.model("model2.jags", n.adapt=20000, n.chain=3,
data=dj, inits=init)
## We draw a small sample, just to compare the distributions
## WARNING!!!!!!!!!!!!!!!!!!!!!!
## VERY SLOW!!!!!!!!!!!!!!!!!!!
coefficientsModel2Without3Deer <- coda.samples(mo2a,
variable.names=c("a0f", "apf", "aff", "afp", "a0p",
"app", "sig"),
n.iter=20000, thin=1)
#########################################
##
## 3. Plot the posterior distributions of the coefficients for the full dataset
## Note that because we have scaled the variables prior to the fit (to
## improve mixing), we have to back-transform the coefficients.
## centering and scale of the explanatory variables
sc <- attr(HS3sites$hr,"scaled:scale")
ce <- attr(HS3sites$hr,"scaled:center")
## We bind the three chains together
cor2 <- do.call(rbind,coefficientsModel2)
## prepare the plot
par(mfrow = c(3,2))
#### Plot of the different coefficients:
## a0f
a0f <- cor2[,c("a0f[1]","a0f[2]","a0f[3]")] -
cor2[,c("aff[1]","aff[2]","aff[3]")]*ce[1]/sc[1] -
cor2[,c("apf[1]","apf[2]","apf[3]")]*ce[2]/sc[2]
colnames(a0f) <- c("Chize", "LPP","TF")
boxplot(a0f, xlab="Site", ylab="a0f", col="grey",
main="a0f", ylim=c(-15,3))
abline(h=0)
## a0p
a0p <- cor2[,c("a0p[1]","a0p[2]","a0p[3]")] -
cor2[,c("afp[1]","afp[2]","afp[3]")]*ce[1]/sc[1] -
cor2[,c("app[1]","app[2]","app[3]")]*ce[2]/sc[2]
colnames(a0p) <- c("Chize", "LPP","TF")
boxplot(a0p, xlab="Site", ylab="a0p", col="grey",
main="a0p", ylim=c(-15,3))
abline(h=0)
## aff
aff <- cor2[,c("aff[1]","aff[2]","aff[3]")]
aff <- aff/sc[1]
colnames(aff) <- c("Chize", "LPP","TF")
boxplot(aff, xlab="Site", ylab="aff", col="grey",
main="aff")
abline(h=0)
## afp
afp <- cor2[,c("afp[1]","afp[2]","afp[3]")]
afp <- afp/sc[1]
colnames(afp) <- c("Chize", "LPP","TF")
boxplot(afp, xlab="Site", ylab="afp", col="grey",
main="afp")
abline(h=0)
## apf
apf <- cor2[,c("apf[1]","apf[2]","apf[3]")]
apf <- apf/sc[2]
colnames(apf) <- c("Chize", "LPP","TF")
boxplot(apf, xlab="Site", ylab="apf", col="grey",
main="apf")
abline(h=0)
## app
app <- cor2[,c("app[1]","app[2]","app[3]")]
app <- app/sc[2]
colnames(app) <- c("Chize", "LPP","TF")
boxplot(app, xlab="Site", ylab="app", col="grey",
main="app")
abline(h=0)
#########################################
##
## 4. Plot the posterior distributions of the coefficients for the
## reduced dataset
## We bind the three chains together
cor2 <- do.call(rbind,coefficientsModel2Without3Deer)
## prepare a new plot for comparison
x11()
par(mfrow = c(3,2))
#### Plot of the different coefficients:
## a0f
a0f <- cor2[,c("a0f[1]","a0f[2]","a0f[3]")] -
cor2[,c("aff[1]","aff[2]","aff[3]")]*ce[1]/sc[1] -
cor2[,c("apf[1]","apf[2]","apf[3]")]*ce[2]/sc[2]
colnames(a0f) <- c("Chize", "LPP","TF")
boxplot(a0f, xlab="Site", ylab="a0f", col="grey",
main="a0f", ylim=c(-15,3))
abline(h=0)
## a0p
a0p <- cor2[,c("a0p[1]","a0p[2]","a0p[3]")] -
cor2[,c("afp[1]","afp[2]","afp[3]")]*ce[1]/sc[1] -
cor2[,c("app[1]","app[2]","app[3]")]*ce[2]/sc[2]
colnames(a0p) <- c("Chize", "LPP","TF")
boxplot(a0p, xlab="Site", ylab="a0p", col="grey",
main="a0p", ylim=c(-15,3))
abline(h=0)
## aff
aff <- cor2[,c("aff[1]","aff[2]","aff[3]")]
aff <- aff/sc[1]
colnames(aff) <- c("Chize", "LPP","TF")
boxplot(aff, xlab="Site", ylab="aff", col="grey",
main="aff")
abline(h=0)
## afp
afp <- cor2[,c("afp[1]","afp[2]","afp[3]")]
afp <- afp/sc[1]
colnames(afp) <- c("Chize", "LPP","TF")
boxplot(afp, xlab="Site", ylab="afp", col="grey",
main="afp")
abline(h=0)
## apf
apf <- cor2[,c("apf[1]","apf[2]","apf[3]")]
apf <- apf/sc[2]
colnames(apf) <- c("Chize", "LPP","TF")
boxplot(apf, xlab="Site", ylab="apf", col="grey",
main="apf")
abline(h=0)
## app
app <- cor2[,c("app[1]","app[2]","app[3]")]
app <- app/sc[2]
colnames(app) <- c("Chize", "LPP","TF")
boxplot(app, xlab="Site", ylab="app", col="grey",
main="app")
abline(h=0)
#################################
## ##
## The two plots are similar. ##
## ##
#################################
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.