-
Notifications
You must be signed in to change notification settings - Fork 0
/
efficiency_simulations.R
214 lines (189 loc) · 5.82 KB
/
efficiency_simulations.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
rm(list = ls())
library(magrittr)
library(simChef)
library(future)
RESULTS_DIR <- here::here(
file.path("01_lo-siRF", "results", "simulations_efficiency")
)
SAVE <- TRUE
USE_CACHED <- TRUE
options(simChef.plot_theme = "vthemes")
set.seed(331)
# plan(multisession, workers = 5)
#### Create experiment parts ####
efficiency_dgp_fun <- function(n, beta0, beta1, beta2, beta12,
eff1 = 1, eff2 = 1, int_eff = 1,
err_fun = rnorm, ...) {
eff1_vec <- rbinom(n, 1, eff1)
eff2_vec <- rbinom(n, 1, eff2)
int_eff_vec <- rbinom(n, 1, int_eff)
y0 <- beta0 + err_fun(n, ...)
y1 <- beta0 + beta1 * eff1_vec + err_fun(n, ...)
y2 <- beta0 + beta2 * eff2_vec + err_fun(n, ...)
y12 <- beta0 + (beta1 + beta2 + beta12) * int_eff_vec + err_fun(n, ...)
x <- matrix(0, nrow = 4 * n, ncol = 2)
x[(n + 1):(2 * n), 1] <- 1
x[(2 * n + 1):(3 * n), 2] <- 1
x[(3 * n + 1):(4 * n), 1:2] <- 1
y <- c(y0, y1, y2, y12)
out <- list(
x = x,
y = y,
eff1_vec = eff1_vec,
eff2_vec = eff2_vec,
int_eff_vec = int_eff_vec
)
}
quant_reg_fun <- function(x, y, tau = 0.5, min_thr = 1e-16, ...) {
fit_df <- data.frame(.y = y, x)
fit <- quantreg::rq(formula = .y ~ X1 * X2, tau = tau, data = fit_df)
fit_summary <- summary(fit, se = "boot")$coefficients %>%
as.data.frame() %>%
tibble::rownames_to_column("Term") %>%
dplyr::rename(Coefficient = Value) %>%
dplyr::filter(Term == "X1:X2") %>%
dplyr::mutate(`Pr(>|t|)` = max(min_thr, `Pr(>|t|)`))
return(fit_summary)
# return(broom::tidy(fit_summary))
}
quant_reg <- create_method(
.method_fun = quant_reg_fun, .name = "Quantile Regression (tau = 0.5)"
)
nested_feature_cols <- NULL
feature_col <- "Term"
pval_col <- "Pr(>|t|)"
coef_col <- "Coefficient"
fi_pval <- create_evaluator(
.eval_fun = summarize_feature_importance,
.name = 'P-values Summary',
eval_id = 'pval',
nested_cols = nested_feature_cols,
feature_col = feature_col,
imp_col = pval_col
)
plot_results <- function(fit_results = NULL, eval_results, vary_params = NULL,
eval_name, eval_id, add_ggplot_layers = NULL) {
vary_params <- unique(vary_params)
plt_df <- eval_results[[eval_name]] %>%
dplyr::filter(!is.na(!!rlang::sym(vary_params)))
plt <- plt_df %>%
tidyr::unnest(cols = tidyselect::all_of(sprintf("raw_%s", eval_id))) %>%
vdocs::plot_boxplot(
x_str = vary_params, y_str = sprintf("raw_%s", eval_id)
) +
ggplot2::labs(y = "P-value")
if (vary_params == "sd") {
plt <- plt + ggplot2::labs(x = bquote("SD of Noise" ~ (sigma)))
} else if (vary_params == "beta12") {
plt <- plt + ggplot2::labs(x = bquote("Interaction Effect" ~ (beta[12])))
}
n_dgps <- length(unique(plt_df$.dgp_name))
if (n_dgps > 1) {
plt <- plt + ggplot2::facet_wrap(~ .dgp_name)
}
if (!is.null(add_ggplot_layers)) {
for (ggplot_layer in add_ggplot_layers) {
plt <- plt + ggplot_layer
}
}
return(plt)
}
fi_pval_plot <- create_visualizer(
.viz_fun = plot_results,
.name = 'P-values Plot',
eval_name = 'P-values Summary',
eval_id = 'pval',
add_ggplot_layers = list(
ggplot2::scale_y_continuous(trans = "log10"),
ggplot2::geom_hline(yintercept = 0.05, linetype = "dashed", color = "red"),
ggplot2::coord_cartesian(ylim = c(NA, 1))
)
)
#### Experiment configurations ####
n <- 500
beta0 <- 0
beta1 <- -1
beta2 <- -1
beta12 <- 0
err_fun <- rnorm
noise_sd <- 1
experiment_config_ls <- list(
`Efficiency = 1` = list(
dgp_args = list(
n = n, beta0 = beta0, beta1 = beta1, beta2 = beta2, beta12 = beta12,
eff1 = 1, eff2 = 1, int_eff = 1,
err_fun = err_fun, sd = noise_sd
)
),
`CCDC141-IGF1R, Healthy` = list(
dgp_args = list(
n = n, beta0 = beta0, beta1 = beta1, beta2 = beta2, beta12 = beta12,
eff1 = 0.66, eff2 = 0.9, int_eff = 0.725,
err_fun = err_fun, sd = noise_sd
)
),
`CCDC141-TTN, Healthy` = list(
dgp_args = list(
n = n, beta0 = beta0, beta1 = beta1, beta2 = beta2, beta12 = beta12,
eff1 = 0.66, eff2 = 0.8, int_eff = 0.93,
err_fun = err_fun, sd = noise_sd
)
),
`CCDC141-IGF1R, Diseased` = list(
dgp_args = list(
n = n, beta0 = beta0, beta1 = beta1, beta2 = beta2, beta12 = beta12,
eff1 = 0.92, eff2 = 0.83, int_eff = 0.695,
err_fun = err_fun, sd = noise_sd
)
),
`CCDC141-TTN, Diseased` = list(
dgp_args = list(
n = n, beta0 = beta0, beta1 = beta1, beta2 = beta2, beta12 = beta12,
eff1 = 0.92, eff2 = 0.99, int_eff = 0.92,
err_fun = err_fun, sd = noise_sd
)
)
)
#### Run experiments ####
n_reps <- 100
beta12s <- c(0, -0.1, -0.2, -0.5, -1)
noise_sds <- c(0.1, 0.2, 0.5, 1, 1.5, 2)
for (exp_name in names(experiment_config_ls)) {
exp_args <- experiment_config_ls[[exp_name]]
dgp <- do.call(
create_dgp,
args = c(
list(
.dgp_fun = efficiency_dgp_fun,
.name = exp_name
),
exp_args$dgp_args
)
)
experiment_name <- sprintf("%s Simulation", exp_name)
experiment <- create_experiment(
name = experiment_name,
save_dir = file.path(RESULTS_DIR, experiment_name)
) %>%
add_dgp(dgp) %>%
add_method(quant_reg) %>%
add_evaluator(fi_pval) %>%
add_visualizer(fi_pval_plot)
# vary across beta12 signal
results <- experiment %>%
add_vary_across(.dgp = dgp$name, beta12 = beta12s) %>%
run_experiment(
n_reps = n_reps, save = SAVE, use_cached = USE_CACHED
)
# vary across noise levels
results <- experiment %>%
remove_vary_across() %>%
add_vary_across(.dgp = dgp$name, sd = noise_sds) %>%
run_experiment(
n_reps = n_reps, save = SAVE, use_cached = USE_CACHED
)
}
render_docs(
save_dir = RESULTS_DIR, show_eval = FALSE,
title = "Examining the effect of varying gene-silencing efficiencies on epistasis testing: A simulation study"
)