-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc_node_inla_glm.R
executable file
·196 lines (172 loc) · 6.79 KB
/
calc_node_inla_glm.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
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
#' Fit a given regression using INLA
#'
#' Internal wrapper to INLA and are called from \code{fitAbn.bayes} and \code{buildScoreCache.bayes}.
#'
#' @param child.loc index of current child node.
#' @param dag.m.loc dag as matrix.
#' @param data.df.loc data df,
#' @param data.dists.loc list of distributions.
#' @param ntrials.loc \code{rep(1,dim(data.df)[1])}.
#' @param exposure.loc \code{rep(1,dim(data.df)[1])}.
#' @param compute.fixed.loc TRUE.
#' @param mean.intercept.loc the prior mean for all the Gaussian additive terms for each node. INLA argument \code{control.fixed=list(mean.intercept=...)} and \code{control.fixed=list(mean=...)}.
#' @param prec.intercept.loc the prior precision for all the Gaussian additive term for each node. INLA argument \code{control.fixed=list(prec.intercept=...)} and \code{control.fixed=list(prec=...)}.
#' @param mean.loc the prior mean for all the Gaussian additive terms for each node. INLA argument \code{control.fixed=list(mean.intercept=...)} and \code{control.fixed=list(mean=...)}. Same as \code{mean.intercept.loc}.
#' @param prec.loc the prior precision for all the Gaussian additive term for each node. INLA argument \code{control.fixed=list(prec.intercept=...)} and \code{control.fixed=list(prec=...)}. Same as \code{prec.intercept.loc}.
#' @param loggam.shape.loc the shape parameter in the Gamma distribution prior for the precision in a Gaussian node. INLA argument \code{control.family=list(hyper = list(prec = list(prior="loggamma",param=c(loggam.shape, loggam.inv.scale))))}.
#' @param loggam.inv.scale.loc the inverse scale parameter in the Gamma distribution prior for the precision in a Gaussian node. INLA argument \code{control.family=list(hyper = list(prec = list(prior="loggamma",param=c(loggam.shape, loggam.inv.scale))))}.
#' @param verbose.loc FALSE to not print additional output.
#' @param nthreads number of threads to use for INLA. Default is \code{fit.control[["ncores"]]} or \code{build.control[["ncores"]]} which is the number of cores specified in \code{control} and defaults to 1.
#'
#' @return If INLA failed, FALSE or an error is returned. Otherwise, the direct output from INLA is returned.
#' @family Bayes
#' @keywords internal
calc.node.inla.glm <-
function(child.loc = NULL,
dag.m.loc = NULL,
data.df.loc = NULL,
data.dists.loc = NULL,
ntrials.loc = NULL,
exposure.loc = NULL,
compute.fixed.loc = NULL,
mean.intercept.loc = NULL,
prec.intercept.loc = NULL,
mean.loc = NULL,
prec.loc = NULL,
loggam.shape.loc = NULL,
loggam.inv.scale.loc = NULL,
verbose.loc = FALSE,
nthreads = NULL) {
if (nthreads == 1) {
INLA::inla.setOption("num.threads", "1:1")
} else if (nthreads > 1) {
# inlathreads <- paste0(nthreads, ":1")
# INLA::inla.setOption("num.threads", inlathreads)
if (verbose.loc) {
message("Nested parallelism detected. Limiting INLA (inner loop) to 1 thread to prevent unexpected behaviour.\n")
}
INLA::inla.setOption("num.threads", "1:1")
} else if (nthreads < 1) {
stop("invalid number of threads for INLA")
}
if (verbose.loc) {message("INLA threads (outer:inner) set to ", INLA::inla.getOption("num.threads"), "\n")}
#print(data.df.loc);
## 1. get the formula part of the call - create a string of this
if (length(which(dag.m.loc[child.loc,-child.loc] == 1)) == 0) {
## independent node
str.eqn.str <-
paste(colnames(dag.m.loc)[child.loc], "~1,")
} else {
## have some covariate
if (dim(dag.m.loc)[1] == 2) {
## special case - 2x2 DAG and so names are not retained when -child.loc
str.eqn.str <-
paste(colnames(dag.m.loc)[child.loc],
"~",
colnames(dag.m.loc)[-child.loc],
",",
sep = "")
} else {
str.eqn.str <-
paste(colnames(dag.m.loc)[child.loc],
"~",
paste(names(which(
dag.m.loc[child.loc,-child.loc] == 1
)), collapse = "+", sep = ""),
",",
sep = "")
}
}
#cat(str.eqn.str,"\n");
#stop("");
## 2. data set
str.data <- "data=data.df.loc,"
## 3. family
str.family <-
paste("family=\"", data.dists.loc[[child.loc]], "\",", sep = "")
## 4. additional parameter for number of trials (binomial) or exposure (poisson)
str.extra <- ""
if (data.dists.loc[[child.loc]] == "binomial") {
str.extra <- paste("Ntrials=ntrials.loc,", sep = "")
}
if (data.dists.loc[[child.loc]] == "poisson") {
str.extra <- paste("E=exposure.loc,", sep = "")
}
if (data.dists.loc[[child.loc]] == "gaussian") {
str.extra <-
paste(
"control.family=list(hyper = list(prec = list(prior=\"loggamma\",param=c(",
loggam.shape.loc,
",",
loggam.inv.scale.loc,
")))),\n",
sep = ""
)
}
## 5. get the full command
res <- NULL
start.str <- "res <- INLA::inla("
end.str <-
paste(
"\ncontrol.fixed=list(mean.intercept=",
mean.intercept.loc,
",\n",
"prec.intercept=",
prec.intercept.loc,
",\n",
"mean=",
mean.loc,
",\n",
"prec=",
prec.loc,
",\n",
"compute=",
compute.fixed.loc,
"))\n",
sep = ""
)
#error.str <- "inla.arg=\"-b 2>/dev/null\",";
r <- NULL
full.command <-
paste(
"r <- try(",
start.str,
str.eqn.str,
str.data,
str.family,
str.extra,
#error.str,
end.str,
",silent=TRUE)",
sep = ""
)
## 6. some debugging - if requested
if (verbose.loc) {
cat("commands which are parsed and sent to inla().\n")
print(full.command)
}
## 7. now run the actual command - parse and eval - is parsed in current scope and so data.df exists here
#eval(parse(text=full.command));
eval(parse(text = full.command))
#cat("got r=\n");print(r);
#cat("type=",typeof(r),"\n");cat("length=",length(r),"\n");cat("names=",names(r),"\n");
#if (is.null(r) || inherits(r, "try-error")) {
if (inherits(r, what = "try-error")) {
warning(r)
return(FALSE)
} else if (length(r) == 1) {
### INLA failed
warning("INLA failed\n")
return(FALSE)
} else {
## 8. return the results
if (!compute.fixed.loc) {
## only want marginal likelihood
return(res$mlik[1])
## n.b. [1] is so we choose the integrated estimate and not just the Gaussian
} else {
## alternatively get *all* the output from inla() - copious
return(res)
}
}
} ## end of function