-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFitModel.R
More file actions
60 lines (59 loc) · 2.37 KB
/
FitModel.R
File metadata and controls
60 lines (59 loc) · 2.37 KB
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
# modified function to take PC priors
FitModel <- 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
}