-
Notifications
You must be signed in to change notification settings - Fork 25
/
update.R
348 lines (302 loc) · 8.44 KB
/
update.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#' Run Model on New Data
#'
#' Given a table of new parameter values with a new
#' parameter set per line, runs iteratively Markov models
#' over these sets.
#'
#' `newdata` must be a `data.frame` with the
#' following properties: the column names must be parameter
#' names used in [define_parameters()]; and an
#' optional column `.weights` can give the respective
#' weight of each row in the target population.
#'
#' Weights are automatillcally scaled. If no weights are
#' provided equal weights are used for each strata.
#'
#' For the plotting function, the `type` argument can
#' take the following values: `"cost"`, `"effect"`
#' or `"icer"` to plot the heterogeneity of the
#' respective values. Furthermore `"ce"` and
#' `"count"` can produce from the combined model plots
#' similar to those of [run_model()].
#'
#' @name update_model
#' @param object The result of [run_model()].
#' @param newdata A `data.frame` of new parameter sets,
#' one column per parameter and one row per parameter set.
#' An optional `.weights` column can be included for
#' a weighted analysis.
#' @param x Updated model to plot.
#' @param strategy A model index, character or numeric.
#' @param result The the result to plot (see details).
#' @param type Plot simple values or differences?
#' @param ... Additional arguments passed to
#' `geom_histogram`. Especially usefull to specify
#' `binwidth`.
#'
#' @section Warning:
#'
#' Histograms do not account for weights. On the other
#' hand summary results do.
#'
#' @return A `data.frame` with one row per model/value.
#' @export
#'
#' @example inst/examples/example_update.R
#'
update.run_model <- function(object, newdata, ...) {
if (! any(class(object) %in% "run_model")) {
stop("'object' must be the result of 'run_model()'.")
}
has_weights <- ".weights" %in% names(newdata)
if (has_weights) {
weights <- newdata$.weights
newdata <- dplyr::select(newdata, -.data$.weights)
} else {
message("No weights specified in update, using equal weights.")
weights <- rep(1, nrow(newdata))
}
ce <- get_ce(object) %>%
compat_lazy_dots()
list_res <- list()
for (n in get_strategy_names(object)) {
message(sprintf("Updating strategy '%s'...", n))
suppressMessages({
list_res <- c(
list_res,
list(eval_strategy_newdata(
object, strategy = n,
newdata = newdata
))
)
})
}
names(list_res) <- get_strategy_names(object)
for (n in names(list_res)) {
list_res[[n]]$.strategy_names <- n
list_res[[n]]$.index <- seq_len(nrow(newdata))
}
res <-
dplyr::bind_rows(list_res)
suppressMessages({
res_total <- res %>%
dplyr::rowwise() %>%
dplyr::do(get_total_state_values(.data$.mod)) %>%
dplyr::bind_cols(res %>% dplyr::select(-.data$.mod)) %>%
dplyr::ungroup() %>%
dplyr::mutate(!!!ce) %>%
dplyr::left_join(
dplyr::tibble(
.index = seq_len(nrow(newdata)),
.weights = weights
)
) %>%
dplyr::ungroup()
})
comb_mods <- combine_models(
newmodels = list_res,
weights = weights,
oldmodel = object
)
structure(
list(
updated_model = res_total,
newdata = newdata,
model = object,
combined_model = comb_mods,
has_weights = has_weights,
weights = weights
),
class = c("updated_model", class(res))
)
}
#' @export
print.updated_model <- function(x, ...) {
print(summary(x), ...)
}
#' @export
#' @rdname update_model
plot.updated_model <- function(x, type = c("simple", "difference",
"counts", "ce", "values"),
result = c("cost", "effect", "icer"),
strategy = NULL,
...) {
type <- match.arg(type)
result <- match.arg(result)
if (type %in% c("counts", "ce", "values")) {
return(plot(x$combined_model,
type = type, strategy = strategy,
...)
)
}
if (is.null(strategy)) {
strategy <- get_strategy_names(get_model(x))
if (type == "difference") {
strategy <- setdiff(strategy, get_noncomparable_strategy(get_model(x)))
}
} else {
strategy <- check_strategy_index(
get_model(x),
strategy,
allow_multiple = TRUE
)
}
if (get_noncomparable_strategy(get_model(x)) %in% strategy &&
"difference" %in% type) {
stop("Cannot represent value differences from uncomparable strategy.")
}
if (type == "simple" && result == "icer") {
stop("Result 'icer' can conly be computed with type = 'difference'.")
}
switch(
paste(type, result, sep = "_"),
simple_cost = {
x_var <- ".cost"
x_lab <- "Cost"
},
simple_effect = {
x_var <- ".effect"
x_lab <- "Effect"
},
difference_cost = {
x_var <- ".dcost"
x_lab <- "Cost Diff."
},
difference_effect = {
x_var <- ".deffect"
x_lab <- "Effect Diff."
},
difference_icer = {
x_var <- ".icer"
x_lab <- "ICER"
}
)
summary(x)$scaled_results %>%
dplyr::filter(
.data$.strategy_names %in% strategy
) %>%
ggplot2::ggplot(ggplot2::aes_string(x = x_var)) +
ggplot2::geom_histogram(...) +
ggplot2::xlab(x_lab)+
ggplot2::facet_grid(.strategy_names ~ .)
}
scale.updated_model <- function(x, scale = TRUE, center = TRUE) {
.bm <- get_root_strategy(get_model(x))
res <- x$updated_model
if (scale) {
res <- res %>%
dplyr::mutate(
.cost = .data$.cost / .data$.n_indiv,
.effect = .data$.effect / .data$.n_indiv
)
}
if (center) {
res <- res %>%
dplyr::group_by(.data$.index) %>%
dplyr::mutate(
.cost = (.data$.cost - sum(.data$.cost * (.data$.strategy_names == .bm))),
.effect = (.data$.effect - sum(.data$.effect * (.data$.strategy_names == .bm)))
) %>%
dplyr::ungroup()
}
res
}
get_model.updated_model <- function(x) {
x$model
}
#' @export
summary.updated_model <- function(object, ...) {
strategy_names <- get_strategy_names(
get_model(object)
)[ord_eff <- order(get_effect(get_model(object)))]
list_res <- list()
tab_scaled <- object %>%
scale(center = FALSE) %>%
dplyr::group_by(.data$.index) %>%
dplyr::do(compute_icer(
.data, strategy_order = ord_eff)
)
for (.n in strategy_names) {
tmp <- tab_scaled %>%
dplyr::filter(.data$.strategy_names == .n)
list_res <- c(
list_res,
lapply(
c(".cost", ".effect", ".dcost", ".deffect", ".icer"),
function(x) {
wsum <- wtd_summary(
tmp[[x]],
tmp$.weights
)
is.na(wsum) <- ! is.finite(wsum)
tab_summary <- matrix(
wsum,
nrow = 1
)
colnames(tab_summary) <- names(wsum)
cbind(
data.frame(Model = .n,
Value = x),
tab_summary
)
}
)
)
}
tab_res <-
do.call("rbind", list_res)
tab_res$Value <- tab_res$Value %>%
factor(
levels = c(".cost", ".effect",
".dcost", ".deffect",
".icer"),
labels = c("Cost", "Effect",
"Cost Diff.", "Effect Diff.",
"Icer")
)
mat_res <- dplyr::select(
tab_res,
-.data$Model,
-.data$Value
) %>%
as.matrix()
rownames(mat_res) <- tab_res$Model %>%
paste(tab_res$Value, sep = " - ")
structure(
list(
summary_results = tab_res,
scaled_results = tab_scaled,
model = object,
to_print = mat_res,
sum_comb = summary(object$combined_model, ...)
),
class = c("summary_updated_model", class(tab_res))
)
}
get_model.summary_updated_model <- function(x) {
x$model
}
#' @export
print.summary_updated_model <- function(x, ...) {
object <- get_model(x)
cat(sprintf(
"An analysis re-run on %i parameter sets.\n\n",
nrow(object$newdata)
))
if (! object$has_weights) {
cat("* Unweighted analysis.")
} else {
cat("* Weigths distribution:\n\n")
print(summary(object$weights))
cat(sprintf("\nTotal weight: %s",
format(sum(object$weights))))
}
cat("\n\n* Values distribution:\n\n")
print(x$to_print, na.print = "-")
cat("\n* Combined result:\n\n")
print(x$sum_comb)
invisible(x)
}
get_newdata <- function(x) {
x$newdata
}