prepareFit: Bayesian Model To Identify Factors Affecting Wildlife-Vehicle...

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/code.R

Description

Bayesian Model To Identify Factors Affecting Wildlife-Vehicle Collisions

Usage

1
prepareFit(X, alphas, collisions, nYear, Area, departement)

Arguments

X

a data.frame containing the numeric variables supposed to have an effect on the wildlife-vehicle collisions (columns) for spatial unit (rows).

alphas

a character string vector with length equal to ncol(X) giving the name, for each column of X, of the group of variables to which each column of X belongs. This vector is used to describe how to group the variables so that they are either in the model or out of the model together. In general, alphas[i] is set equal to names(X)[i], except if several columns are to be considered as one variable. For example, if X contains several dummy variables (i.e. coded 0/1) corresponding to the same factor "habitat" (e.g. Urban, agriculture, forest), then, we might set names(X)[i] <- "habitat" for all i corresponding to this habitat.

collisions

an integer vector with length equal to nrow(X) defining the number of wildlife-ungulate collisions in each studied spatial unit.

nYear

an integer vector with length equal to nrow(X) defining the number of Years during which wildlife-ungulate collisions have been recorded in each studied spatial unit.

Area

a numeric vector with length equal to nrow(X) defining the area of each studied spatial unit.

departement

a character vector with length equal to nrow(X) defining the department of each studied spatial unit.

Details

prepareFit prepares the elements required for the fit of the Kuo-Mallik model (a Bayesian model used to identify factors affecting wildlife-vehicle collisions).

Value

a list with all elements required for the fit of the model with JAGS, that is: (i) data4jags: the list of the data required by the model, to be passed to the argument data of the function jags.model of the package rjags, (ii) ini: list of starting values for the parameters, to be passed to the argument init of the function jags.model, (iii) modelstring: a character string containing the model fit by JAGS, (iv) coefnames: vector of character strings containing the names of the coefficients of interest in the model, to be passed to the argument variable.names of the function coda.samples of the package rjags.

Author(s)

Clement Calenge, clement.calenge@oncfs.gouv.fr

See Also

jags.model, coda.samples

Examples

 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
## Load the data:
data("dataCollision")

## Prepare the data
X <- dataCollision$RedDeer$X

## Sets the elevation <600 to NULL (redundant with other elevations:
## if the elevation is neither >1500, nor between 600 and 1500,
## it is necessarily < 600
X$elev600 <- NULL

## Set the variable roads to NULL (not useful here, as noted in the
## paper)
X$roads <- NULL

## prepares the alpha:
## vector alphas: the name of alphas is the same as the columns of X...
alphas <- names(X)
## ... except for elevation and habitat,
## which correspond to several variables in X
alphas[7:8] <- "elev"
alphas[11:14] <- "hab"

## Note that the variables Agriculture, Open, Urban, and Forest
## are strongly correlated together. To reduce the correlation and
## improve mixing, we transform these variables with the help of a PCA
## (package ade4)
hab <- X[,11:14]
X <- X[,-c(11:14)]
pc <- ade4::dudi.pca(hab,scannf = FALSE, nf=4)

## We scale the variables to improve mixing
X <- as.data.frame(scale(X))

## and we add the transformed habitat variables from the PCA:
li1 <- pc$l1
X$AX1 <- li1[,1]
X$AX2 <- li1[,2]
X$AX3 <- li1[,3]
X$AX4 <- li1[,4]

## Use of the function preparefit
pf <- prepareFit(X, alphas, dataCollision$RedDeer$coll,
                 dataCollision$RedDeer$Y, dataCollision$RedDeer$Area,
                 dataCollision$RedDeer$departement)

## Not run: 
## WARNING: very long execution (about 1 hour)!!
## the results are stored in the dataset "modelRedDeer"
## But if you want to try it, the data are now ready for the fit:

mo <- jags.model(textConnection(pf$modelstring), ini=pf$ini, data=pf$data4jags)
update(mo, n.iter=1000)
modelRedDeer <- coda.samples(mo, variable.names = pf$coefnames,
                             n.iter = 500000, thin=100)

## End(Not run)

ClementCalenge/ungulateCollisions documentation built on May 6, 2019, 12:05 p.m.