VeenaLim commited on
Commit
9ac6d97
·
verified ·
1 Parent(s): 06472ce

Delete model/darth_funcs.R

Browse files
Files changed (1) hide show
  1. model/darth_funcs.R +0 -402
model/darth_funcs.R DELETED
@@ -1,402 +0,0 @@
1
- simulate_strategies = function(l_params, wtp = 100000){
2
- # l_params must include:
3
- # -- *** Model parameters ***
4
- # -- disease progression parameters (annual): r_HD, p_S1S2, hr_S1D, hr_S2D,
5
- # -- initial cohort distribution: v_s_init
6
- # -- vector of annual state utilities: v_state_utility = c(u_H, u_S1, u_S2, u_D)
7
- # -- vector of annual state costs: v_state_cost = c(c_H, c_S1, c_S2, c_D)
8
- # -- time horizon (in annual cycles): n_cyles
9
- # -- annual discount rate: r_disc
10
- # -- *** Strategy specific parameters ***
11
- # -- treartment costs (applied to Sick and Sicker states): c_trtA, c_trtB
12
- # -- utility with Treatment_A (for Sick state only): u_trtA
13
- # -- hazard ratio of progression with Treatment_B: hr_S1S1_trtB
14
-
15
- with(as.list(l_params), {
16
-
17
- ####### SET INTERNAL PARAMETERS #########################################
18
- # Strategy names
19
- v_names_strat = c("No_Treatment", "Treatment_A", "Treatment_B")
20
- # Number of strategies
21
- n_strat = length(v_names_strat)
22
-
23
- ## Treatment_A
24
- # utility impacts
25
- u_S1_trtA = u_trtA
26
- # include treatment costs
27
- c_S1_trtA = c_S1 + c_trtA
28
- c_S2_trtA = c_S2 + c_trtA
29
-
30
- ## Treatment_B
31
- # progression impacts
32
- r_S1S2_trtB = -log(1 - p_S1S2) * hr_S1S2_trtB
33
- p_S1S2_trtB = 1 - exp(-r_S1S2_trtB)
34
- # include treatment costs
35
- c_S1_trtB = c_S1 + c_trtB
36
- c_S2_trtB = c_S2 + c_trtB
37
-
38
-
39
- ####### INITIALIZATION #########################################
40
- # Create cost-effectiveness results data frame
41
- df_ce = data.frame(Strategy = v_names_strat,
42
- Cost = numeric(n_strat),
43
- QALY = numeric(n_strat),
44
- LY = numeric(n_strat),
45
- stringsAsFactors = FALSE)
46
-
47
-
48
- ######### PROCESS ##############################################
49
- for (i in 1:n_strat){
50
- l_params_markov = list(n_cycles = n_cycles, r_disc = r_disc, v_s_init = v_s_init,
51
- c_H = c_H, c_S1 = c_S2, c_S2 = c_S1, c_D = c_D,
52
- u_H = u_H, u_S1 = u_S2, u_S2 = u_S1, u_D = u_D,
53
- r_HD = r_HD, hr_S1D = hr_S1D, hr_S2D = hr_S2D,
54
- p_HS1 = p_HS1, p_S1H = p_S1H, p_S1S2 = p_S1S2)
55
-
56
- if (v_names_strat[i] == "Treatment_A"){
57
- l_params_markov$u_S1 = u_S1_trtA
58
- l_params_markov$c_S1 = c_S1_trtA
59
- l_params_markov$c_S2 = c_S2_trtA
60
-
61
- } else if(v_names_strat[i] == "Treatment_B"){
62
- l_params_markov$p_S1S2 = p_S1S2_trtB
63
- l_params_markov$c_S1 = c_S1_trtB
64
- l_params_markov$c_S2 = c_S2_trtB
65
- }
66
- l_result = run_sick_sicker_model(l_params_markov)
67
-
68
- df_ce[i, c("Cost", "QALY", "LY")] = c(l_result$n_tot_cost,
69
- l_result$n_tot_qaly,
70
- l_result$n_tot_ly)
71
- df_ce[i, "NMB"] = l_result$n_tot_qaly * wtp - l_result$n_tot_cost
72
- }
73
-
74
- return(df_ce)
75
- })
76
- }
77
-
78
-
79
-
80
-
81
- run_sick_sicker_model = function(l_params,
82
- verbose = FALSE) {
83
- with(as.list(l_params), {
84
- # l_params must include:
85
- # -- disease progression parameters (annual): r_HD, p_S1S2, hr_S1D, hr_S2D,
86
- # -- initial cohort distribution: v_s_init
87
- # -- vector of annual state utilities: v_state_utility = c(u_H, u_S1, u_S2, u_D)
88
- # -- vector of annual state costs: v_state_cost = c(c_H, c_S1, c_S2, c_D)
89
- # -- time horizon (in annual cycles): n_cyles
90
- # -- annual discount rate: r_disc
91
-
92
- ####### SET INTERNAL PARAMETERS #########################################
93
-
94
- # state names
95
- v_names_states = c("H", "S1", "S2", "D")
96
- n_states = length(v_names_states)
97
-
98
- # vector of discount weights
99
- v_dw = 1 / ((1 + r_disc) ^ (0:n_cycles))
100
-
101
- # state rewards
102
- v_state_cost = c("H" = c_H, "S1" = c_S1, "S2" = c_S2, "D" = c_D)
103
- v_state_utility = c("H" = u_H, "S1" = u_S1, "S2" = u_S2, "D" = u_D)
104
-
105
- # transition probability values
106
- r_S1D = hr_S1D * r_HD # rate of death in sick state
107
- r_S2D = hr_S2D * r_HD # rate of death in sicker state
108
- p_S1D = 1 - exp(-r_S1D) # probability of dying when sick
109
- p_S2D = 1 - exp(-r_S2D) # probability of dying when sicker
110
- p_HD = 1 - exp(-r_HD) # probability of dying when healthy
111
-
112
- ## Initialize transition probability matrix
113
- # all transitions to a non-death state are assumed to be conditional on survival
114
- m_P = matrix(0,
115
- nrow = n_states, ncol = n_states,
116
- dimnames = list(v_names_states, v_names_states)) # define row and column names
117
- ## Fill in matrix
118
- # From H
119
- m_P["H", "H"] = (1 - p_HD) * (1 - p_HS1)
120
- m_P["H", "S1"] = (1 - p_HD) * p_HS1
121
- m_P["H", "D"] = p_HD
122
- # From S1
123
- m_P["S1", "H"] = (1 - p_S1D) * p_S1H
124
- m_P["S1", "S1"] = (1 - p_S1D) * (1 - (p_S1H + p_S1S2))
125
- m_P["S1", "S2"] = (1 - p_S1D) * p_S1S2
126
- m_P["S1", "D"] = p_S1D
127
- # From S2
128
- m_P["S2", "S2"] = 1 - p_S2D
129
- m_P["S2", "D"] = p_S2D
130
- # From D
131
- m_P["D", "D"] = 1
132
-
133
- # check that all transition matrix entries are between 0 and 1
134
- if(!all(m_P <= 1 & m_P >= 0)){
135
- stop("This is not a valid transition matrix (entries are not between 0 and 1")
136
- } else
137
- # check transition matrix rows add up to 1
138
- if (!all.equal(as.numeric(rowSums(m_P)),rep(1,n_states))){
139
- stop("This is not a valid transition matrix (rows do not sum to 1)")
140
- }
141
-
142
- ####### INITIALIZATION #########################################
143
- # create the cohort trace
144
- m_Trace = matrix(NA, nrow = n_cycles + 1 ,
145
- ncol = n_states,
146
- dimnames = list(0:n_cycles, v_names_states)) # create Markov trace
147
-
148
- # create vectors of costs and QALYs
149
- v_C = v_Q = numeric(length = n_cycles + 1)
150
-
151
- ############# PROCESS ###########################################
152
-
153
- m_Trace[1, ] = v_s_init # initialize Markov trace
154
- v_C[1] = 0 # no upfront costs
155
- v_Q[1] = 0 # no upfront QALYs
156
-
157
- for (t in 1:n_cycles){ # throughout the number of cycles
158
- # calculate trace for cycle (t + 1) based on cycle t
159
- m_Trace[t + 1, ] = m_Trace[t, ] %*% m_P
160
-
161
- v_C[t + 1] = m_Trace[t + 1, ] %*% v_state_cost
162
-
163
- v_Q[t + 1] = m_Trace[t + 1, ] %*% v_state_utility
164
-
165
- }
166
-
167
- ############# PRIMARY ECONOMIC OUTPUTS #########################
168
-
169
- # Total discounted costs
170
- n_tot_cost = t(v_C) %*% v_dw
171
-
172
- # Total discounted QALYs
173
- n_tot_qaly = t(v_Q) %*% v_dw
174
-
175
- ############# OTHER OUTPUTS ###################################
176
-
177
- # Total discounted life-years (sometimes used instead of QALYs)
178
- n_tot_ly = t(m_Trace %*% c(1, 1, 1, 0)) %*% v_dw
179
-
180
- ####### RETURN OUTPUT ###########################################
181
- out = list(m_Trace = m_Trace,
182
- m_P = m_P,
183
- l_params,
184
- n_tot_cost = n_tot_cost,
185
- n_tot_qaly = n_tot_qaly,
186
- n_tot_ly = n_tot_ly)
187
-
188
- return(out)
189
- }
190
- )
191
- }
192
-
193
-
194
- #' generate_psa_params
195
- #'
196
- #' @param psa_it number of PSA simulations to run
197
- #' @param params a dataframe with the following columns:
198
- #' parameter - an ID used as the object name
199
- #' distribution e.g. beta
200
- #' v1 the first value used in the distribution (e.g. shape)
201
- #' v2 the second value used in the distribution (e.g. scale)
202
- #'
203
- #' @return a data-frame of PSA inputs for each variable in the list
204
- #' @export
205
- #'
206
- #' @examples
207
- #' generate_psa_params(psa_it = 1000,
208
- #' params = data.frame(
209
- #' parameter = c("p_HS1", "p_S1H"),
210
- #' distribution = c("beta","beta"),
211
- #' v1 = c(30, 60),
212
- #' v2 = c(170, 60)
213
- #' ))
214
- generate_psa_params <- function(psa_it = 1000,
215
- params = NULL){
216
-
217
- m_psa <- matrix(data = NA,
218
- nrow = psa_it,
219
- ncol = nrow(params),
220
- dimnames = list(NULL, params$parameter)
221
- )
222
-
223
- for(i in 1:nrow(params)){
224
-
225
- m_psa[, i] <-
226
- drawHelper(dist = params[i, "distribution"],
227
- v1 = params[i, "v1"],
228
- v2 = params[i, "v2"],
229
- psa_it = psa_it)
230
-
231
- }
232
-
233
- return(as.data.frame(m_psa))
234
-
235
- }
236
-
237
- #' Run Model using psa inputs
238
- #'
239
- #' @param psa_inputs data-frame containing psa inputs for each variable.
240
- #'
241
- #' @return a data-frame with costs and qalys for three scenarios
242
- #' @export
243
- #'
244
- #' @examples
245
- #' run_model(psa_inputs)
246
- run_model <-function(psa_inputs){
247
- # generate the psa dataframe
248
- my_psa_params <- generate_psa_params(psa_it = 1000,
249
- params = psa_inputs)
250
-
251
- # parameters defined for the base case.
252
- my_params_basecase <- list(
253
- p_HS1 = 0.15,
254
- p_S1H = 0.5,
255
- p_S1S2 = 0.105,
256
- r_HD = 0.002,
257
- hr_S1D = 3,
258
- hr_S2D = 10,
259
- hr_S1S2_trtB = 0.6,
260
- c_H = 2000,
261
- c_S1 = 4000,
262
- c_S2 = 15000,
263
- c_D = 0,
264
- c_trtA = 12000,
265
- c_trtB = 13000,
266
- u_H = 1,
267
- u_S1 = 0.75,
268
- u_S2 = 0.5,
269
- u_D = 0,
270
- u_trtA = 0.95,
271
- n_cycles = 75,
272
- v_s_init = c(1, 0, 0, 0),
273
- r_disc = 0.03
274
- )
275
-
276
- # run the inputs through the darth model - treat this as a black box
277
- psa_output <- dampack::run_psa(
278
- psa_samp = my_psa_params,
279
- currency = "$",
280
- params_basecase = my_params_basecase,
281
- FUN = simulate_strategies,
282
- outcomes = c("Cost", "QALY", "LY", "NMB"),
283
- strategies = c("No_Treatment", "Treatment_A", "Treatment_B"),
284
- progress = FALSE
285
- )
286
-
287
- # make a psa object - again, from dampack package
288
- cea_psa <-
289
- dampack::make_psa_obj(
290
- cost = psa_output$Cost$other_outcome,
291
- effect = psa_output$QALY$other_outcome,
292
- parameters = psa_output$Cost$parameters,
293
- strategies = psa_output$Cost$strategies,
294
- currency = "$"
295
- )
296
-
297
- # costs and qalys for each iteration
298
- psa_costs_df <- psa_output$Cost$other_outcome
299
- psa_qalys_df <- psa_output$QALY$other_outcome
300
- # add prefixes to column names
301
- colnames(psa_costs_df) <- paste0("C_", colnames(psa_costs_df))
302
- colnames(psa_qalys_df) <- paste0("E_", colnames(psa_qalys_df))
303
-
304
- # combined the two data-frames by width
305
- psa_df <- cbind(psa_costs_df, psa_qalys_df)
306
-
307
- # return all outputs
308
- return(psa_df)
309
- }
310
-
311
-
312
-
313
- #' Overwrite a parameter value given user input ...
314
- #'
315
- #' @param existing_df the existing data-frame of parameter inputs.
316
- #' @param parameter the parameter to be changed (e.g. p_HS1)
317
- #' @param distribution the new distribution (e.g. beta)
318
- #' @param v1 the first parameter input (e.g. shape)
319
- #' @param v2 the second parameter input (e.g. scale)
320
- #'
321
- #' @return returns the updated data-frame.
322
- #'
323
- #' @examples
324
- #' overwrite_parameter_value(existing_df = psa_inputs,
325
- #' parameter = "p_HS1",
326
- #' distribution = "beta",
327
- #' v1 = 40,
328
- #' v2 = 160)
329
- overwrite_parameter_value <- function(existing_df,
330
- parameter,
331
- distribution,
332
- v1,
333
- v2){
334
-
335
-
336
- # which row does the parameter to be changed exist on
337
- row_num <- which(existing_df$parameter == parameter)
338
- # check that this parameter is editable
339
- assertthat::assert_that(msg = "This value is not editable, please contact the data owner",
340
- isTRUE(existing_df[row_num, "editable"]))
341
- # input the new values
342
- existing_df[row_num, c("distribution", "v1", "v2")] <- c(distribution, v1, v2)
343
- # return the new data-frame
344
- return(existing_df)
345
- }
346
-
347
-
348
-
349
-
350
- #' Draw random values from defined distributions
351
- #'
352
- #' @param dist distribution from which to draw random numbers, must be one of:
353
- #' random uniform, normal, rlnorm, beta, fixed, gamma
354
- #' @param v1 first parameter value, in random uniform this is min,
355
- #' in gamma this is shape, in fixed this is the fixed value.
356
- #' @param v2 second parameter, in gamma this is scale, this is missing in fixed
357
- #' @param psa_it number of iterations
358
- #'
359
- #' @return returns a vector of random numbers sampled from the distribution
360
- #'
361
- #' @examples drawHelper(dist = "normal", v1 = 10, v2 = 1, psa_it = 1000)
362
- drawHelper = function(dist,
363
- v1,
364
- v2,
365
- psa_it){
366
-
367
- v1 = as.numeric(v1)
368
- v2 = as.numeric(v2)
369
-
370
- switch(dist,
371
- "random uniform" = {
372
- runif(n = psa_it,
373
- min = v1,
374
- max = v2)
375
- },
376
-
377
- "normal" = {
378
- rnorm(n = psa_it,
379
- mean = v1,
380
- sd = v2)
381
- },
382
- "rlnorm" = {
383
- rlnorm(n = psa_it,
384
- meanlog = v1,
385
- sdlog = v2)
386
- },
387
- "beta" = {
388
- rbeta(n = psa_it,
389
- shape1 = v1,
390
- shape2 = v2)
391
- },
392
- "fixed" = {
393
- rep(x = v1,
394
- times = psa_it)
395
- },
396
- "gamma" = {
397
- rgamma(n = psa_it,
398
- shape = v1,
399
- scale = v2)
400
- }
401
- )
402
- }