-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_run_analysis.r
199 lines (162 loc) · 6.81 KB
/
2_run_analysis.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
### USER MODIFIABLE PARAMETERS #################################################
data_subfolder <- "example_1"
data_version_name <- "V1-240110"
# `r_k_threshold` indicates which observations may be excluded if the ratio
# of the sample sizes is x% higher or lower than the median of all observations.
# For example, if the median value of r_k in the sample was 5, and the threshold
# was 0.20, only observations with r_k between 80% and 120% of 5: 4-6.
# `data_version_name` and `r_k_threshold` get appended to create
# `analysis_version_name`.
r_k_threshold <- 0.10
# r_k_threshold <- 0.20
# r_k_threshold <- 0.25
# r_k_threshold <- Inf # No threshold - NOT ADVISED
stop_on_data_error <- TRUE # Stop on any data entry error. RECOMMENDED.
stop_on_data_error <- FALSE # Exclude rows with errors. NOT RECOMMENDED.
overwrite_existing_output <- TRUE
# overwrite_existing_output <- FALSE
### BEGIN PROGRAM ##############################################################
# 1. Check if working directory is ../BayesDLR
project_root <- base::getwd()
working_subfolder <-
tail(strsplit(x = project_root, split = "/")[[1]], 1)
if(working_subfolder != "BayesDLR"){
stop("Load BayesDLR.Rproj before proceeding. See README.MD for instructions.")
} else {
analysis_version_name <-
paste0(data_version_name, "_r_k_", r_k_threshold*100, "_pct")
code_dir <- file.path(project_root, "code")
analysis_dir <- file.path(project_root, "analyses", data_subfolder)
# Load configuration file
config_file <- paste0(analysis_version_name, ".yml")
config_path <- file.path(analysis_dir, config_file)
if(!file.exists(config_path)) {
stop("Configuration file not found:\n Path: ", config_path, "\n",
" Confirm `data_subfolder` (", data_subfolder, "), ",
"`data_version_name` (", data_version_name, "), and ",
"`r_k_threshold` (", r_k_threshold, ") are correctly specified.")
} else {
configuration <- config::get(file = config_path)
for(i in 1:length(configuration)) {
assign(x = names(configuration)[i], value = configuration[[i]])
}
data_dir <- file.path(project_root, "data", data_subfolder)
processed_data_dir <- file.path(data_dir, "processed_data")
source_path <- file.path(code_dir, "source", "bayes_dlr_functions.r")
workflow_1_path <-
file.path(code_dir, "workflow", "1_process_raw_data.r")
workflow_2_1_path <-
file.path(code_dir, "workflow", "2.1_fit_1_component_beta_binomial_mml.r")
workflow_2_2_path <-
file.path(code_dir, "workflow", "2.2_fit_2_component_beta_binomial_mml.r")
workflow_3_path <-
file.path(code_dir, "workflow", "3_extract_model_results.r")
required_paths <-
c(source_path,
config_path,
input_path,
processed_data_dir,
workflow_1_path,
workflow_2_1_path,
workflow_2_2_path,
workflow_3_path
)
required_paths_exist <- file.exists(required_paths)
if(!all(required_paths_exist)){
stop("The following paths could not be found:\n",
paste(required_paths[which(!required_paths_exist)], collapse = "\n"))
} else {
# Process Raw Data
data_check_result <-
callr::r(
func =
function(path, ...){
source(file = path)
},
args =
list(
project_root = project_root,
data_subfolder = data_subfolder,
data_version_name = data_version_name,
config_path = config_path,
overwrite_existing_output = overwrite_existing_output,
path = workflow_1_path
),
env =
c(
"bayes_dlr_project_root" = project_root,
"bayes_dlr_data_subfolder" = data_subfolder,
"bayes_dlr_data_version_name" = data_version_name,
"bayes_dlr_config_path" = config_path,
"bayes_dlr_overwrite_existing_output" = overwrite_existing_output
)
)$value
if(data_check_result$any_invalid_rows & stop_on_data_error){
stop("Data entry errors detected in input file.\n",
" File: ", input_path, "\n Rows: ",
paste0(data_check_result$invalid_rows, collapse = ", "))
} else {
if(data_check_result$any_invalid_rows){
warning("Data entry errors detected in input file.\n",
" File: ", input_path, "\n Rows: ",
paste0(data_check_result$invalid_rows, collapse = ", "), "\n",
"\n REVIEW DATA CAREFULLY BEFORE PROCEEDING.")
}
callr::r(
func =
function(path, ...){
source(file = path)
},
args =
list(
project_root = project_root,
data_subfolder = data_subfolder,
data_version_name = data_version_name,
config_path = config_path,
bb_1_component_seed = bb_1_component_seed,
analysis_version_name = analysis_version_name,
path = workflow_2_1_path
),
env =
c(
"bayes_dlr_project_root" = project_root,
"bayes_dlr_data_subfolder" = data_subfolder,
"bayes_dlr_data_version_name" = data_version_name,
"bayes_dlr_config_path" = config_path,
"bayes_dlr_bb_1_component_seed" = bb_1_component_seed,
"bayes_dlr_r_k_threshold" = r_k_threshold,
"bayes_dlr_analysis_version_name" = analysis_version_name,
"bayes_dlr_config_path" = config_path
)
)$value
callr::r(
func =
function(path, ...){
source(file = path)
},
args =
list(
project_root = project_root,
data_subfolder = data_subfolder,
data_version_name = data_version_name,
config_path = config_path,
bb_1_component_seed = bb_1_component_seed,
analysis_version_name = analysis_version_name,
path = workflow_2_2_path
),
env =
c(
"bayes_dlr_project_root" = project_root,
"bayes_dlr_data_subfolder" = data_subfolder,
"bayes_dlr_data_version_name" = data_version_name,
"bayes_dlr_config_path" = config_path,
"bayes_dlr_bb_2_component_seed" = bb_2_component_seed,
"bayes_dlr_r_k_threshold" = r_k_threshold,
"bayes_dlr_analysis_version_name" = analysis_version_name,
"bayes_dlr_config_path" = config_path
)
)$value
}
}
}
}