forked from giabaio/BCEA
/
evppi.default.R
150 lines (122 loc) · 3.83 KB
/
evppi.default.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
#' @rdname evppi
#' @export
#'
evppi.default <- function(he, ...) {
stop("No method available", call. = FALSE)
}
#' @rdname evppi
#'
#' @examples
#' data(Vaccine, package = "BCEA")
#' treats <- c("Status quo", "Vaccination")
#' bcea_vacc <- bcea(e.pts, c.pts, ref = 2, interventions = treats)
#' inp <- createInputs(vaccine_mat)
#' evppi(bcea_vacc, c("beta.1.", "beta.2."), inp$mat)
#' @export
#'
evppi.bcea <- function(he,
param_idx = NULL,
input,
N = NULL,
plot = FALSE,
residuals = TRUE,
method = NULL, ...) {
if (!requireNamespace("voi", quietly = TRUE)) {
stop(
"Package \"voi (>= 1.0.1)\" must be installed to use this function.",
call. = FALSE
)
}
comp_ids <- c(he$comp, he$ref)
outputs <- list(e = he$e[, comp_ids],
c = he$c[, comp_ids],
k = he$k)
if (!is.null(method))
method <- tolower(method)
# allow alternative name for Sadatsafavi method
if (length(method) > 0 && method == "sad") method <- "sal"
# replace column numbers with names
pars <-
if (all(is.numeric(param_idx))) {
if (any(!param_idx %in% 1:ncol(input)))
stop("Column number(s) not in available parameter inputs")
param_idx <- names(input)[param_idx]
} else {
param_idx
}
n_sims <- nrow(input)
n_outputs <- nrow(outputs$c)
# this way passes on error checking to voi::evppi
if (n_sims == n_outputs) {
# subset number of PSA samples; default all
row_idxs <-
if (is.null(N) || (N >= n_sims)) {
1:n_sims
} else {
sample(1:n_sims, size = N, replace = FALSE)
}
input <- data.frame(input[row_idxs, ])
outputs$e <- outputs$e[row_idxs, ]
outputs$c <- outputs$c[row_idxs, ]
}
res <- voi::evppi(outputs, inputs = input, pars = pars, method = method,
check = TRUE, plot_inla_mesh = plot, ...)
voi_methods <- unname(attr(res, "methods"))
voi_models <- attr(res, "models")
# method name returned from evppi
method_nm <- voi_methods[1]
form <-
if (method_nm == "gam") {
paste("te(", paste(param_idx, ",", sep = "",
collapse = ""), "bs='cr')")
} else {NULL}
has_fitted_values <- residuals && (method_nm %in% c("inla", "gp", "gam"))
if (has_fitted_values) {
fitted_c <- get_fitted_("c", voi_methods, voi_models)
fitted_e <- get_fitted_("e", voi_methods, voi_models)
} else {
fitted_c <- NULL
fitted_e <- NULL
}
residuals_out <-
list(fitted.costs = fitted_c,
fitted.effects = fitted_e,
select = row_idxs)
out <- c(
list(
evppi = res$evppi,
index = pars,
k = res$k,
evi = he$evi,
parameters = paste(pars, collapse = " and "),
time = list("Fitting for Effects" = NULL,
"Fitting for Costs" = NULL,
"Calculating EVPPI" = NULL),
method = list("Methods for Effects" = voi_methods,
"Methods for Costs" = voi_methods)),
residuals_out,
list(formula = form,
pars = res$pars,
res = res))
structure(out, class = c("evppi", class(out)))
}
#' Get fitted values from evppi object
#'
#' @importFrom purrr list_cbind map
#' @keywords internal
#' @return matrix
#
get_fitted_ <- function(val, voi_methods, voi_models) {
method_nm <- voi_methods[1]
# named differently in different methods
fitted_lup <- c(inla = "fitted", gp = "fitted", gam = "fitted.values")
fitted_txt <- fitted_lup[method_nm]
fitted_ls <-
purrr::map(voi_models[[1]][[val]],
~unname(as.data.frame(.x[[fitted_txt]]))) |>
rev()
suppressMessages(
purrr::list_cbind(fitted_ls)) |>
as.matrix() |>
cbind(0)
}