This repository was archived by the owner on Nov 10, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathSTM_02.R
More file actions
599 lines (538 loc) · 24.4 KB
/
Copy pathSTM_02.R
File metadata and controls
599 lines (538 loc) · 24.4 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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
##############################################################################
### Cohort State-Transition Models in R ###
##############################################################################
# This code forms the basis for the state-transition model of the article:
# 'A Tutorial on Time-Dependent Cohort State-Transition Models in R'
# Authors:
# - Fernando Alarid-Escudero <fernando.alarid@cide.edu>
# - Eline Krijkamp
# - Eva A. Enns
# - Alan Yang
# - M.G. Myriam Hunink
# - Petros Pechlivanoglou
# - Hawre Jalal
# Please cite the article when using this code
#
# To program this tutorial we made use of
# R version 4.0.2 (2020-06-22)
# Platform: 64-bit operating system, x64-based processor
# Running under: Windows 10
# RStudio: Version 1.3.1073 2009-2020 RStudio, Inc
##############################################################################
############################# Code of Appendix ###############################
##############################################################################
# Implements a time-independent Sick-Sicker cSTM model #
# Standard of Care (SoC): best available care for the patients with the disease. This scenario reflects the natural history of the disease progressions
# Strategy A: treatment A is given to all sick patients, patients in sick and sicker, but does only improves the utility of those being sick.
# Strategy B: treatment B reduces disease progression from sick to sicker. However, it is not possible to distinguish those sick from sicker and therefore all individuals in one of the two sick states get the treatment.
# Strategy AB: This strategy combines treatment A and treatment B. The disease progression is reduced and Sick individuals has an improved utility.
# This model incorporates time-dependent transition probabilities
################################ Initial setup ###############################
rm(list = ls()) # remove any variables in R's memory
### Install packages
# install.packages("dplyr") # to manipulate data
# install.packages("tidyr") # to manipulate data
# install.packages("reshape2") # to manipulate data
# install.packages("ggplot2") # to visualize data
# install.packages("gridExtra") # to visualize data
# install.packages("scales") # for dollar signs and commas
# install.packages("boot") # to handle log odds and log odds ratios
# install.packages("devtools") # to ensure compatibility among packages
# install.packages("dampack") # for CEA and calculate ICERs
# devtools::install_github("DARTH-git/darthtools") # to install darthtools (>= 0.4.1) from GitHub
### Load packages
library(dplyr)
library(tidyr)
library(reshape2)
library(ggplot2)
library(gridExtra)
library(scales)
library(boot)
library(dampack)
library(darthtools)
# darthtools versions up to 0.4.0 assign the Simpson's 1/3 within-cycle
# correction weights by vector position instead of by time (see README.md).
if (utils::packageVersion("darthtools") < "0.4.1") {
stop("Please update darthtools to version 0.4.1 or later: ",
"remotes::install_github(\"DARTH-git/darthtools\")")
}
library(doParallel)
### Load supplementary functions
source("R/Functions.R")
################################ Model input #################################
## General setup
n_age_init <- 25 # age at baseline
n_age_max <- 100 # maximum age of follow up
n_cycles <- n_age_max - n_age_init # time horizon, number of cycles
v_names_states <- c("H", "S1", "S2", "D") # the 4 health states of the model:
# Healthy (H), Sick (S1), Sicker (S2), Dead (D)
n_states <- length(v_names_states) # number of health states
# Discounting factors
d_c <- 0.03 # discount rate for costs
d_e <- 0.03 # discount rate for QALYs
# Strategies
v_names_str <- c("Standard of care", # store the strategy names
"Strategy A",
"Strategy B",
"Strategy AB")
n_str <- length(v_names_str) # number of strategies
# Within-cycle correction (WCC) using Simpson's 1/3 rule
v_wcc <- darthtools::gen_wcc(n_cycles = n_cycles,
method = "Simpson1/3") # vector of wcc
## Transition probabilities (per cycle), hazard ratios and odds ratio
p_HS1 <- 0.15 # probability to become Sick when Healthy conditional on surviving
p_S1H <- 0.5 # probability to become Healthy when Sick conditional on surviving
p_S1S2 <- 0.105 # probability to become Sicker when Sick conditional on surviving
hr_S1 <- 3 # hazard ratio of death in Sick vs Healthy
hr_S2 <- 10 # hazard ratio of death in Sicker vs Healthy
# Effectiveness of treatment B
hr_S1S2_trtB <- 0.6 # hazard ratio of becoming Sicker when Sick under treatment B
## Age-dependent mortality rates
lt_usa_2015 <- read.csv("data/LifeTable_USA_Mx_2015.csv")
v_r_mort_by_age <- lt_usa_2015 %>%
select(Total) %>%
as.matrix() # anyone above 100 have the same mortality
names(v_r_mort_by_age) <- lt_usa_2015$Age
## State rewards
# Costs
c_H <- 2000 # cost of remaining one cycle Healthy
c_S1 <- 4000 # cost of remaining one cycle Sick
c_S2 <- 15000 # cost of remaining one cycle Sicker
c_D <- 0 # cost of being dead (per cycle)
c_trtA <- 12000 # cost of treatment A
c_trtB <- 13000 # cost of treatment B
# Utilities
u_H <- 1 # utility when Healthy
u_S1 <- 0.75 # utility when Sick
u_S2 <- 0.5 # utility when Sicker
u_D <- 0 # utility when Dead
u_trtA <- 0.95 # utility when being treated
## Transition rewards
du_HS1 <- 0.01 # disutility when transitioning from Healthy to Sick
ic_HS1 <- 1000 # increase in cost when transitioning from Healthy to Sick
ic_D <- 2000 # increase in cost when dying
# Discount weight (equal discounting is assumed for costs and effects)
v_dwc <- 1 / ((1 + d_e) ^ (0:n_cycles))
v_dwe <- 1 / ((1 + d_c) ^ (0:n_cycles))
### Process model inputs
## Age-specific transition probabilities to the Dead state
# extract age-specific all-cause mortality for ages in model time horizon
v_r_HDage <- v_r_mort_by_age[(n_age_init + 1) + 0:(n_cycles - 1)]
# compute mortality rates
v_r_S1Dage <- v_r_HDage * hr_S1 # Age-specific mortality rate in the Sick state
v_r_S2Dage <- v_r_HDage * hr_S2 # Age-specific mortality rate in the Sicker state
# transform rates to probabilities
v_p_HDage <- rate_to_prob(v_r_HDage) # Age-specific mortality risk in the Healthy state
v_p_S1Dage <- rate_to_prob(v_r_S1Dage) # Age-specific mortality risk in the Sick state
v_p_S2Dage <- rate_to_prob(v_r_S2Dage) # Age-specific mortality risk in the Sicker state
## Transition probability of becoming Sicker when Sick for treatment B
# transform probability to rate
r_S1S2 <- prob_to_rate(p = p_S1S2)
# apply hazard ratio to rate to obtain transition rate of becoming Sicker when Sick for treatment B
r_S1S2_trtB <- r_S1S2 * hr_S1S2_trtB
# transform rate to probability
p_S1S2_trtB <- rate_to_prob(r = r_S1S2_trtB) # probability to become Sicker when Sick
# under treatment B conditional on surviving
###################### Construct state-transition models #####################
#### Create transition arrays ####
# Initialize 3-D array
a_P <- array(0, dim = c(n_states, n_states, n_cycles),
dimnames = list(v_names_states, v_names_states, 0:(n_cycles - 1)))
### Fill in array
## From H
a_P["H", "H", ] <- (1 - v_p_HDage) * (1 - p_HS1)
a_P["H", "S1", ] <- (1 - v_p_HDage) * p_HS1
a_P["H", "D", ] <- v_p_HDage
## From S1
a_P["S1", "H", ] <- (1 - v_p_S1Dage) * p_S1H
a_P["S1", "S1", ] <- (1 - v_p_S1Dage) * (1 - (p_S1H + p_S1S2))
a_P["S1", "S2", ] <- (1 - v_p_S1Dage) * p_S1S2
a_P["S1", "D", ] <- v_p_S1Dage
## From S2
a_P["S2", "S2", ] <- 1 - v_p_S2Dage
a_P["S2", "D", ] <- v_p_S2Dage
## From D
a_P["D", "D", ] <- 1
### For strategies B and AB
## Initialize transition probability array for strategies B and AB
a_P_strB <- a_P
## Only need to update the probabilities involving the transition from Sick to Sicker, p_S1S2
# From S1
a_P_strB["S1", "S1", ] <- (1 - v_p_S1Dage) * (1 - (p_S1H + p_S1S2_trtB))
a_P_strB["S1", "S2", ] <- (1 - v_p_S1Dage) * p_S1S2_trtB
### Check if transition probability matrix is valid (i.e., elements cannot < 0 or > 1)
check_transition_probability(a_P, verbose = TRUE)
check_transition_probability(a_P_strB, verbose = TRUE)
### Check if transition probability matrix sum to 1 (i.e., each row should sum to 1)
check_sum_of_transition_array(a_P, n_states = n_states, n_cycles = n_cycles, verbose = TRUE)
check_sum_of_transition_array(a_P_strB, n_states = n_states, n_cycles = n_cycles, verbose = TRUE)
#### Run Markov model ####
## Initial state vector
# All starting healthy
v_s_init <- c(H = 1, S1 = 0, S2 = 0, D = 0) # initial state vector
v_s_init
## Initialize cohort trace for age-dependent (ad) cSTM for srategies SoC and A
m_M_ad <- matrix(0,
nrow = (n_cycles + 1), ncol = n_states,
dimnames = list(0:n_cycles, v_names_states))
# Store the initial state vector in the first row of the cohort trace
m_M_ad[1, ] <- v_s_init
## Initialize cohort trace for srategies B and AB
m_M_ad_strB <- m_M_ad # structure and initial states remain the same.
## Initialize transition array which will capture transitions from each state to another over time
# for srategies SoC and A
a_A <- array(0,
dim = c(n_states, n_states, n_cycles + 1),
dimnames = list(v_names_states, v_names_states, 0:n_cycles))
# Set first slice of a_A with the initial state vector in its diagonal
diag(a_A[, , 1]) <- v_s_init
# For srategies B and AB, the array structure and initial state are identical
a_A_strB <- a_A
## Iterative solution of age-dependent cSTM
for(t in 1:n_cycles){
## Fill in cohort trace
# For srategies SoC and A
m_M_ad[t + 1, ] <- m_M_ad[t, ] %*% a_P[, , t]
# For srategies B and AB
m_M_ad_strB[t + 1, ] <- m_M_ad_strB[t, ] %*% a_P_strB[, , t]
## Fill in transition-dynamics array
# For srategies SoC and A
a_A[, , t + 1] <- m_M_ad[t, ] * a_P[, , t]
# For srategies B and AB
a_A_strB[, , t + 1] <- m_M_ad_strB[t, ] * a_P_strB[, , t]
}
## Store the cohort traces in a list
l_m_M <- list(m_M_ad,
m_M_ad,
m_M_ad_strB,
m_M_ad_strB)
names(l_m_M) <- v_names_str
#### Plot Outputs ####
## Plot the cohort traces
plot_trace(m_M_ad)
plot_trace_strategy(l_m_M)
## Plot the epidemiology outcomes
survival_plot <- plot_surv(l_m_M, v_names_death_states = "D") +
theme(legend.position = "")
prevalence_S1_plot <- plot_prevalence(l_m_M,
v_names_sick_states = c("S1"),
v_names_dead_states = "D") +
theme(legend.position = "")
prevalence_S2_plot <- plot_prevalence(l_m_M,
v_names_sick_states = c("S2"),
v_names_dead_states = "D") +
theme(legend.position = "")
prevalence_S1S2_plot <- plot_prevalence(l_m_M,
v_names_sick_states = c("S1", "S2"),
v_names_dead_states = "D") +
theme(legend.position = "")
prop_sicker_plot <- plot_proportion_sicker(l_m_M,
v_names_sick_states = c("S1", "S2"),
v_names_sicker_states = c("S2")) +
theme(legend.position = "bottom")
grid.arrange(survival_plot,
prevalence_S1_plot,
prevalence_S2_plot,
prevalence_S1S2_plot,
prop_sicker_plot,
ncol = 1, heights = c(0.75, 0.75, 0.75, 0.75, 1))
#### State Rewards ####
## Vector of state utilities under strategy SoC
v_u_SoC <- c(H = u_H,
S1 = u_S1,
S2 = u_S2,
D = u_D)
## Vector of state costs under strategy SoC
v_c_SoC <- c(H = c_H,
S1 = c_S1,
S2 = c_S2,
D = c_D)
## Vector of state utilities under strategy A
v_u_strA <- c(H = u_H,
S1 = u_trtA,
S2 = u_S2,
D = u_D)
## Vector of state costs under strategy A
v_c_strA <- c(H = c_H,
S1 = c_S1 + c_trtA,
S2 = c_S2 + c_trtA,
D = c_D)
## Vector of state utilities under strategy B
v_u_strB <- c(H = u_H,
S1 = u_S1,
S2 = u_S2,
D = u_D)
## Vector of state costs under strategy B
v_c_strB <- c(H = c_H,
S1 = c_S1 + c_trtB,
S2 = c_S2 + c_trtB,
D = c_D)
## Vector of state utilities under strategy AB
v_u_strAB <- c(H = u_H,
S1 = u_trtA,
S2 = u_S2,
D = u_D)
## Vector of state costs under strategy AB
v_c_strAB <- c(H = c_H,
S1 = c_S1 + (c_trtA + c_trtB),
S2 = c_S2 + (c_trtA + c_trtB),
D = c_D)
## Store the vectors of state utilities for each strategy in a list
l_u <- list(SQ = v_u_SoC,
A = v_u_strA,
B = v_u_strB,
AB = v_u_strAB)
## Store the vectors of state cost for each strategy in a list
l_c <- list(SQ = v_c_SoC,
A = v_c_strA,
B = v_c_strB,
AB = v_c_strAB)
## Store the transition array for each strategy in a list
l_a_A <- list(SQ = a_A,
A = a_A,
B = a_A_strB,
AB = a_A_strB)
# assign strategy names to matching items in the lists
names(l_u) <- names(l_c) <- names(l_a_A) <- v_names_str
## create empty vectors to store total utilities and costs
v_tot_qaly <- v_tot_cost <- vector(mode = "numeric", length = n_str)
names(v_tot_qaly) <- names(v_tot_cost) <- v_names_str
#### Loop through each strategy and calculate total utilities and costs ####
for (i in 1:n_str) {
v_u_str <- l_u[[i]] # select the vector of state utilities for the ith strategy
v_c_str <- l_c[[i]] # select the vector of state costs for the ith strategy
a_A_str <- l_a_A[[i]] # select the transition array for the ith strategy
#### Array of state rewards ####
# Create transition matrices of state utilities and state costs for the ith strategy
m_u_str <- matrix(v_u_str, nrow = n_states, ncol = n_states, byrow = T)
m_c_str <- matrix(v_c_str, nrow = n_states, ncol = n_states, byrow = T)
# Expand the transition matrix of state utilities across cycles to form a transition array of state utilities
a_R_u_str <- array(m_u_str,
dim = c(n_states, n_states, n_cycles + 1),
dimnames = list(v_names_states, v_names_states, 0:n_cycles))
# Expand the transition matrix of state costs across cycles to form a transition array of state costs
a_R_c_str <- array(m_c_str,
dim = c(n_states, n_states, n_cycles + 1),
dimnames = list(v_names_states, v_names_states, 0:n_cycles))
#### Apply transition rewards ####
# Apply disutility due to transition from H to S1
a_R_u_str["H", "S1", ] <- a_R_u_str["H", "S1", ] - du_HS1
# Add transition cost per cycle due to transition from H to S1
a_R_c_str["H", "S1", ] <- a_R_c_str["H", "S1", ] + ic_HS1
# Add transition cost per cycle of dying from all non-dead states
a_R_c_str[-n_states, "D", ] <- a_R_c_str[- n_states, "D", ] + ic_D
#### Expected QALYs and Costs for all transitions per cycle ####
# QALYs = life years x QoL
# Note: all parameters are annual in our example. In case your own case example is different make sure you correctly apply .
a_Y_c_str <- a_A_str * a_R_c_str
a_Y_u_str <- a_A_str * a_R_u_str
#### Expected QALYs and Costs per cycle ####
## Vector of QALYs and Costs
v_qaly_str <- apply(a_Y_u_str, 3, sum) # sum the proportion of the cohort across transitions
v_cost_str <- apply(a_Y_c_str, 3, sum) # sum the proportion of the cohort across transitions
#### Discounted total expected QALYs and Costs per strategy and apply half-cycle correction if applicable ####
## QALYs
v_tot_qaly[i] <- t(v_qaly_str) %*% (v_dwe * v_wcc)
## Costs
v_tot_cost[i] <- t(v_cost_str) %*% (v_dwc * v_wcc)
}
########################## Cost-effectiveness analysis #######################
### Calculate incremental cost-effectiveness ratios (ICERs)
df_cea <- calculate_icers(cost = v_tot_cost,
effect = v_tot_qaly,
strategies = v_names_str)
df_cea
### Create CEA table in proper format
table_cea <- format_table_cea(df_cea)
table_cea
### CEA frontier
plot(df_cea, label = "all", txtsize = 16) +
expand_limits(x = max(table_cea$QALYs) + 0.1) +
theme(legend.position = c(0.8, 0.2))
###################### Probabilistic Sensitivity Analysis ######################
### Load model, CEA and PSA functions
source('R/Functions STM_02.R')
# List of input parameters
l_params_all <- list(
# Transition probabilities (per cycle), hazard ratios
v_r_HDage = v_r_HDage, # constant rate of dying when Healthy (all-cause mortality)
p_HS1 = 0.15, # probability to become Sick when Healthy conditional on surviving
p_S1H = 0.5, # probability to become Healthy when Sick conditional on surviving
p_S1S2 = 0.105, # probability to become Sicker when Sick conditional on surviving
hr_S1 = 3, # hazard ratio of death in Sick vs Healthy
hr_S2 = 10, # hazard ratio of death in Sicker vs Healthy
# Effectiveness of treatment B
hr_S1S2_trtB = 0.6, # hazard ratio of becoming Sicker when Sick under B under treatment B
## State rewards
# Costs
c_H = 2000, # cost of remaining one cycle in Healthy
c_S1 = 4000, # cost of remaining one cycle in Sick
c_S2 = 15000, # cost of remaining one cycle in Sicker
c_D = 0, # cost of being dead (per cycle)
c_trtA = 12000, # cost of treatment A
c_trtB = 13000, # cost of treatment B
# Utilities
u_H = 1, # utility when Healthy
u_S1 = 0.75, # utility when Sick
u_S2 = 0.5, # utility when Sicker
u_D = 0, # utility when Dead
u_trtA = 0.95, # utility when being treated with A
## Transition rewards
du_HS1 = 0.01, # disutility when transitioning from Healthy to Sick
ic_HS1 = 1000, # increase in cost when transitioning from Healthy to Sick
ic_D = 2000 # increase in cost when dying
)
# store the parameter names into a vector
v_names_params <- names(l_params_all)
# Test function to generate PSA input dataset
generate_psa_params(10)
# Number of simulations
n_sim <- 1000
# Generate PSA input dataset
df_psa_input <- generate_psa_params(n_sim = n_sim)
# First six observations
head(df_psa_input)
# Histogram of parameters
ggplot(melt(df_psa_input, variable.name = "Parameter"), aes(x = value)) +
facet_wrap(~Parameter, scales = "free") +
geom_histogram(aes(y = ..density..)) +
scale_x_continuous(breaks = scales::pretty_breaks(n = 3)) +
theme_bw(base_size = 16) +
theme(axis.text = element_text(size=6))
# Initialize data.frames with PSA output
# data.frame of costs
df_c <- as.data.frame(matrix(0,
nrow = n_sim,
ncol = n_str))
colnames(df_c) <- v_names_str
# data.frame of effectiveness
df_e <- as.data.frame(matrix(0,
nrow = n_sim,
ncol = n_str))
colnames(df_e) <- v_names_str
#### Conduct probabilistic sensitivity analysis ####
# Run Markov model on each parameter set of PSA input dataset in series
n_time_init_psa_series <- Sys.time()
for(i in 1:n_sim){
l_out_temp <- calculate_ce_out(df_psa_input[i, ])
df_c[i, ] <- l_out_temp$Cost
df_e[i, ] <- l_out_temp$Effect
# Display simulation progress
if(i/(n_sim/100) == round(i/(n_sim/100), 0)) { # display progress every 5%
cat('\r', paste(i/n_sim * 100, "% done", sep = " "))
}
}
n_time_end_psa_series <- Sys.time()
n_time_total_psa_series <- n_time_end_psa_series - n_time_init_psa_series
print(paste0("PSA with ", comma(n_sim), " simulations run in series in ",
round(n_time_total_psa_series, 2), " ",
units(n_time_total_psa_series)))
### Run Markov model on each parameter set of PSA input dataset in parallel
# ## Get OS
# os <- get_os()
# print(paste0("Parallelized PSA on ", os))
#
# no_cores <- parallel::detectCores() - 1
#
# n_time_init_psa <- Sys.time()
#
# ## Run parallelized PSA based on OS
# if(os == "macosx"){
# # Initialize cluster object
# cl <- parallel::makeForkCluster(no_cores)
# # Register clusters
# doParallel::registerDoParallel(cl)
# # Run parallelized PSA
# df_ce <- foreach::foreach(i = 1:n_sim, .combine = rbind) %dopar% {
# l_out_temp <- calculate_ce_out(df_psa_input[i, ])
# df_ce <- c(l_out_temp$Cost, l_out_temp$Effect)
# }
# # Extract costs and effects from the PSA dataset
# df_c[i, ] <- df_ce[, 1:n_str]
# df_e[i, ] <- df_ce[, (n_str+1):(2*n_str)]
# # Register end time of parallelized PSA
# n_time_end_psa <- Sys.time()
# }
# if(os == "windows"){
# # Initialize cluster object
# cl <- parallel::makeCluster(no_cores)
# # Register clusters
# doParallel::registerDoParallel(cl)
# opts <- list(attachExportEnv = TRUE)
# # Run parallelized PSA
# df_ce <- foeach::foreach(i = 1:n_samp, .combine = rbind,
# .export = ls(globalenv()),
# .packages=c("dampack"),
# .options.snow = opts) %dopar% {
# l_out_temp <- calculate_ce_out(df_psa_input[i, ])
# df_ce <- c(l_out_temp$Cost, l_out_temp$Effect)
# }
# # Extract costs and effects from the PSA dataset
# df_c[i, ] <- df_ce[, 1:n_str]
# df_e[i, ] <- df_ce[, (n_str+1):(2*n_str)]
# # Register end time of parallelized PSA
# n_time_end_psa <- Sys.time()
# }
# if(os == "linux"){
# # Initialize cluster object
# cl <- parallel::makeCluster(no_cores)
# # Register clusters
# doParallel::registerDoMC(cl)
# # Run parallelized PSA
# df_ce <- foreach::foreach(i = 1:n_sim, .combine = rbind) %dopar% {
# l_out_temp <- calculate_ce_out(df_psa_input[i, ])
# df_ce <- c(l_out_temp$Cost, l_out_temp$Effect)
# }
# # Extract costs and effects from the PSA dataset
# df_c[i, ] <- df_ce[, 1:n_str]
# df_e[i, ] <- df_ce[, (n_str+1):(2*n_str)]
# # Register end time of parallelized PSA
# n_time_end_psa <- Sys.time()
# }
# # Stope clusters
# stopCluster(cl)
# n_time_total_psa <- n_time_end_psa - n_time_init_psa
# print(paste0("PSA with ", comma(n_sim), " simulations run in series in ",
# round(n_time_total_psa, 2), " ",
# units(n_time_total_psa)))
# Create PSA object for dampack
l_psa <- make_psa_obj(cost = df_c,
effectiveness = df_e,
parameters = df_psa_input,
strategies = v_names_str)
l_psa$strategies <- v_names_str
colnames(l_psa$effectiveness)<- v_names_str
colnames(l_psa$cost)<- v_names_str
# Vector with willingness-to-pay (WTP) thresholds.
v_wtp <- seq(0, 200000, by = 5000)
# Cost-Effectiveness Scatter plot
plot(l_psa)
## Conduct CEA with probabilistic output
# Compute expected costs and effects for each strategy from the PSA
df_out_ce_psa <- summary(l_psa)
# Calculate incremental cost-effectiveness ratios (ICERs)
df_cea_psa <- calculate_icers(cost = df_out_ce_psa$meanCost,
effect = df_out_ce_psa$meanEffect,
strategies = df_out_ce_psa$Strategy)
df_cea_psa
## Plot cost-effectiveness frontier
plot(df_cea_psa, label = "all") +
expand_limits(x = 20.8)
## Cost-effectiveness acceptability curves (CEACs) and frontier (CEAF)
ceac_obj <- ceac(wtp = v_wtp, psa = l_psa)
# Regions of highest probability of cost-effectiveness for each strategy
summary(ceac_obj)
# ceac_obj$Strategy <- ordered(ceac_obj$Strategy, levels = v_names_str)
# CEAC & CEAF plot
plot(ceac_obj, txtsize = 16, xlim = c(0, NA)) +
theme(legend.position = "bottom")
## Expected Loss Curves (ELCs)
elc_obj <- calc_exp_loss(wtp = v_wtp, psa = l_psa)
elc_obj
# ELC plot
plot(elc_obj, log_y = FALSE, txtsize = 16, xlim = c(0, NA)) +
theme(legend.position = "bottom")
## Expected value of perfect information (EVPI)
evpi <- calc_evpi(wtp = v_wtp, psa = l_psa)
# EVPI plot
plot(evpi, effect_units = "QALY")