VeenaLim commited on
Commit
62c2a8f
·
verified ·
1 Parent(s): 39a252a

Initial commits

Browse files
Files changed (5) hide show
  1. Dockerfile +29 -0
  2. api/plumber.R +70 -0
  3. api/plumber_hf.R +80 -0
  4. model/darth_funcs.R +402 -0
  5. parameter_distributions.csv +21 -0
Dockerfile ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use rocker/r-ver as base image with R pre-installed
2
+ FROM rocker/r-ver:4.3.2
3
+
4
+ # Install system dependencies
5
+ RUN apt-get update && apt-get install -y \
6
+ libcurl4-openssl-dev \
7
+ libssl-dev \
8
+ libxml2-dev \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # Install required R packages
12
+ RUN R -e "install.packages(c('plumber', 'dampack', 'readr', 'assertthat'), repos='https://cloud.r-project.org/')"
13
+
14
+ # Create app directory
15
+ WORKDIR /app
16
+
17
+ # Copy API and model files
18
+ COPY api/plumber_hf.R /app/api/plumber.R
19
+ COPY model/darth_funcs.R /app/model/darth_funcs.R
20
+ COPY parameter_distributions.csv /app/parameter_distributions.csv
21
+
22
+ # Install jsonlite for JSON parsing
23
+ RUN R -e "install.packages('jsonlite', repos='https://cloud.r-project.org/')"
24
+
25
+ # Expose port 7860 (Hugging Face Spaces default)
26
+ EXPOSE 7860
27
+
28
+ # Run the Plumber API
29
+ CMD ["R", "-e", "pr <- plumber::plumb('/app/api/plumber.R'); pr$run(host='0.0.0.0', port=7860)"]
api/plumber.R ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(dampack)
2
+ library(readr)
3
+ library(assertthat)
4
+
5
+ #* @apiTitle Client API hosting sensitive data
6
+ #*
7
+ #* @apiDescription This API contains sensitive data, the client does not
8
+ #* want to share this data but does want a consultant to build a health
9
+ #* economic model using it, and wants that consultant to be able to run
10
+ #* the model for various inputs
11
+ #* (while holding certain inputs fixed and leaving them unknown).
12
+
13
+ #* Run the DARTH model
14
+ #* @serializer csv
15
+ #* @param path_to_psa_inputs is the path of the csv file containing the PSA parameters
16
+ #* @param model_functions gives the GitHub repo to source the model code
17
+ #* @param param_updates gives the replacement values of the editable parameters
18
+ #* @post /runDARTHmodel
19
+ function(path_to_psa_inputs = "parameter_distributions.csv",
20
+ model_functions = paste0("https://raw.githubusercontent.com/",
21
+ "BresMed/plumberHE/main/R/darth_funcs.R"),
22
+ param_updates = data.frame(
23
+ parameter = c("p_HS1", "p_S1H"),
24
+ distribution = c("beta", "beta"),
25
+ v1 = c(25, 50),
26
+ v2 = c(150, 70)
27
+ )) {
28
+
29
+
30
+ # source the model functions from the shared GitHub repo...
31
+ source(model_functions)
32
+
33
+ # read in the csv containing parameter inputs
34
+ psa_inputs <- as.data.frame(readr::read_csv(path_to_psa_inputs))
35
+
36
+ # for each row of the data-frame containing the variables to be changed...
37
+ for(n in 1:nrow(param_updates)){
38
+
39
+ # update parameters from API input
40
+ psa_inputs <- overwrite_parameter_value(
41
+ existing_df = psa_inputs,
42
+ parameter = param_updates[n,"parameter"],
43
+ distribution = param_updates[n,"distribution"],
44
+ v1 = param_updates[n,"v1"],
45
+ v2 = param_updates[n,"v2"])
46
+ }
47
+
48
+ # run the model using the single run-model function.
49
+ results <- run_model(psa_inputs)
50
+
51
+ # check that the model results being returned are the correct dimensions
52
+ # here we expect a single dataframe with 6 columns and 1000 rows
53
+ assertthat::assert_that(
54
+ all(dim(x = results) == c(1000, 6)),
55
+ class(results) == "data.frame",
56
+ msg = "Dimensions or type of data are incorrect,
57
+ please check the model code is correct or contact an administrator.
58
+ This has been logged"
59
+ )
60
+
61
+ # check that no data matching the sensitive csv data is included in the output
62
+ # searches through the results data-frame for any of the parameter names,
63
+ # if any exist they will flag a TRUE, therefore we assert that all = F
64
+ assertthat::assert_that(all(psa_inputs[, 1] %in%
65
+ as.character(unlist(x = results,
66
+ recursive = T)) == F))
67
+
68
+ return(results)
69
+
70
+ }
api/plumber_hf.R ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ library(dampack)
2
+ library(readr)
3
+ library(assertthat)
4
+
5
+ #* @apiTitle DARTH Health Economic Model API
6
+ #*
7
+ #* @apiDescription This API runs a health economic Markov model (Sick-Sicker)
8
+ #* for probabilistic sensitivity analysis. Hosted on Hugging Face Spaces.
9
+
10
+ #* Health check endpoint
11
+ #* @get /health
12
+ function() {
13
+ list(status = "healthy", timestamp = Sys.time())
14
+ }
15
+
16
+ #* Run the DARTH model
17
+ #* @serializer csv
18
+ #* @param param_updates gives the replacement values of the editable parameters (JSON string)
19
+ #* @post /runDARTHmodel
20
+ function(param_updates = NULL) {
21
+
22
+ # Source the model functions from local file or GitHub
23
+ model_functions_path <- "/app/model/darth_funcs.R"
24
+ if (file.exists(model_functions_path)) {
25
+ source(model_functions_path)
26
+ } else {
27
+ # Fallback to GitHub if local file not found
28
+ source(paste0("https://raw.githubusercontent.com/",
29
+ "BresMed/plumberHE/main/R/darth_funcs.R"))
30
+ }
31
+
32
+ # Read in the CSV containing parameter inputs
33
+ psa_inputs_path <- "/app/parameter_distributions.csv"
34
+ if (!file.exists(psa_inputs_path)) {
35
+ psa_inputs_path <- "parameter_distributions.csv"
36
+ }
37
+ psa_inputs <- as.data.frame(readr::read_csv(psa_inputs_path))
38
+
39
+ # Parse param_updates if provided
40
+ if (!is.null(param_updates) && param_updates != "") {
41
+ # Handle both JSON string and data frame input
42
+ if (is.character(param_updates)) {
43
+ param_updates <- jsonlite::fromJSON(param_updates)
44
+ }
45
+
46
+ # Convert to data frame if needed
47
+ if (!is.data.frame(param_updates)) {
48
+ param_updates <- as.data.frame(param_updates)
49
+ }
50
+
51
+ # For each row of the data-frame containing the variables to be changed...
52
+ for(n in 1:nrow(param_updates)) {
53
+ # Update parameters from API input
54
+ psa_inputs <- overwrite_parameter_value(
55
+ existing_df = psa_inputs,
56
+ parameter = param_updates[n, "parameter"],
57
+ distribution = param_updates[n, "distribution"],
58
+ v1 = param_updates[n, "v1"],
59
+ v2 = param_updates[n, "v2"]
60
+ )
61
+ }
62
+ }
63
+
64
+ # Run the model using the single run-model function
65
+ results <- run_model(psa_inputs)
66
+
67
+ # Check that the model results being returned are the correct dimensions
68
+ assertthat::assert_that(
69
+ all(dim(x = results) == c(1000, 6)),
70
+ class(results) == "data.frame",
71
+ msg = "Dimensions or type of data are incorrect, please check the model code."
72
+ )
73
+
74
+ # Check that no sensitive data is included in the output
75
+ assertthat::assert_that(
76
+ all(psa_inputs[, 1] %in% as.character(unlist(x = results, recursive = TRUE)) == FALSE)
77
+ )
78
+
79
+ return(results)
80
+ }
model/darth_funcs.R ADDED
@@ -0,0 +1,402 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
parameter_distributions.csv ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ parameter,distribution,v1,v2,editable
2
+ p_HS1,beta,30,170,T
3
+ p_S1H,beta,60,60,T
4
+ p_S1S2,beta,84,716,T
5
+ hr_S1,rlnorm,1.098612,0.01,T
6
+ hr_S2,rlnorm,2.302585,0.02,T
7
+ hr_S1S2_trtB,rlnorm,-0.5108256,0.1,F
8
+ c_H,gamma,100,20,T
9
+ c_S1,gamma,177.8,22.5,T
10
+ c_S2,gamma,225,66.7,T
11
+ c_trtA,gamma,576,20.8,F
12
+ c_trtB,gamma,676,19.2,F
13
+ c_D,fixed,0,,T
14
+ u_H,beta,200,3,T
15
+ u_S1,beta,130,45,T
16
+ u_S2,beta,230,230,T
17
+ u_D,fixed,0,,T
18
+ u_trtA,beta,300,15,F
19
+ du_HS1,beta,11,1088,T
20
+ ic_HS1,gamma,25,40,F
21
+ ic_D,gamma,100,20,T