forked from LeaDambly/IM_warbler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MakeSpatialRegion.R
77 lines (74 loc) · 3.29 KB
/
MakeSpatialRegion.R
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
# just took PointedSDM's function apart a bit because buffer function used in MakeSpatialRegion doesn't work with unprojected data - so no buffering here
MakeSpatialRegion <- function (data = NULL, coords = c("X", "Y"), meshpars, bdry = NULL,
proj = CRS("+proj=utm")) {
require(rgeos)
region.bdry <- inla.sp2segment(bdry)
mesh <- inla.mesh.2d(boundary = region.bdry, cutoff = meshpars$cutoff,
max.edge = meshpars$max.edge, offset = meshpars$offset)
spde <- inla.spde2.matern(mesh = mesh, alpha = 2)
dd <- deldir::deldir(mesh$loc[, 1], mesh$loc[, 2])
tiles <- deldir::tile.list(dd)
poly.gpc <- as(bdry@polygons[[1]]@Polygons[[1]]@coords, "gpc.poly")
w <- sapply(tiles, function(p) rgeos::area.poly(rgeos::intersect(as(cbind(p$x,
p$y), "gpc.poly"), poly.gpc)))
return(list(mesh = mesh, spde = spde, w = w))
}
# modified function to take PC priors
FitModel2 <- function (..., formula = NULL, CovNames = NULL, mesh, spat.ind = "i",
predictions = FALSE, tag.pred = "pred", control.fixed = NULL,
waic = FALSE, dic = FALSE, nthreads = NULL)
{
stck <- inla.stack(...)
if (is.null(CovNames)) {
CovNames <- unlist(stck$effects$names)
CovNames <- CovNames[!CovNames %in% c(spat.ind)]
}
else {
if (!is.null(formula)) {
warning("CovNames and formula are both not NULL: CovNames will be ignored")
}
}
mesh <- inla.spde2.matern(mesh)
if (!is.null(spat.ind)) {
CovNames <- c(CovNames, paste0("f(", spat.ind, ", model=mesh)"))
}
if (is.null(control.fixed)) {
control.fixed <- list(mean = 0)
}
if (is.null(formula)) {
Formula <- formula(paste(c("resp ~ 0 ", CovNames), collapse = "+"))
}
else {
if (is.null(spat.ind)) {
Formula <- formula
}
else {
if (any(grepl(paste0("(", spat.ind, ","), formula,
fixed = TRUE))) {
warning(paste0(spat.ind, " already in formula, so will be ignored"))
Formula <- formula
} else {
Formula <- update(formula, paste0(" ~ . + f(", spat.ind,
", model=mesh)"))
}
}
}
mod <- inla(Formula, family = c("poisson", "binomial"), control.family = list(list(link = "log"),
list(link = "cloglog")), data = inla.stack.data(stck),
verbose = FALSE, control.results = list(return.marginals.random = FALSE,
return.marginals.predictor = FALSE), control.predictor = list(A = inla.stack.A(stck),
link = NULL, compute = TRUE), control.fixed = control.fixed,
Ntrials = inla.stack.data(stck)$Ntrials, E = inla.stack.data(stck)$e,
num.threads = nthreads,
control.compute = list(waic = waic, dic = dic))
if (predictions) {
id <- inla.stack.index(stck, tag.pred)$data
pred <- data.frame(mean = mod$summary.fitted.values$mean[id],
stddev = mod$summary.fitted.values$sd[id])
res <- list(model = mod, predictions = pred)
}
else {
res <- mod
}
res
}