-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhte_plot.R
176 lines (148 loc) · 4.88 KB
/
hte_plot.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
#' Plot the GATE estimate
#' @import ggplot2
#' @import ggthemes
#' @importFrom stats sd
#' @importFrom rlang .data
#' @param x An table object. This is typically an output of \code{evaluate_hte()} function.
#' @param ... Further arguments passed to the function.
#' @importFrom ggplot2 .data
#' @importFrom ggdist geom_pointinterval
#' @return A plot of ggplot2 object.
#' @export
plot.hte <- function(x, ...){
# parameters
estimate = x
# format output
bind_rows(estimate$out_algs$qoi$GATE) %>%
mutate(
estimate = gate,
lower = gate - qnorm(0.975)*sd,
upper = gate + qnorm(0.975)*sd,
algorithm = gsub("_", " ", alg)) -> data
# plot GATE estimates
out = gate_ggplot(data)
return(out)
}
#' @export
plot_CI <- function(x, ...) {
UseMethod("plot_CI")
}
#' Plot the uniform confidence interval
#' @import ggplot2
#' @import ggthemes
#' @importFrom stats sd
#' @importFrom rlang .data
#' @importFrom scales percent
#' @importFrom tidyr pivot_longer
#' @importFrom purrr map
#' @param x An object of \code{evaluate_hte()} class. This is typically an output of \code{evaluate_hte()} function.
#' @param alpha Significance level. Default is 0.05.
#' @param ... Further arguments passed to the function.
#' @return A plot of ggplot2 object.
#' @export
plot_CI.hte <- function(
x,
alpha = 0.05,
...){
# parameters
estimate = x
estimate_algs = estimate$out_algs
estimate_user = estimate$out_user
data_algs = tibble()
data_user = tibble()
# -----------------------------------------
# estimate HTE from ML algorithms
# -----------------------------------------
# load the beta values
results <- readRDS("data/optimization_values.rds")
# round the alpha to 2 decimal places
alpha_round <- round(alpha, 2)
# get the optimal parameters
min_uniform_beta_0 <- results$beta_0[results$alphas == alpha_round]
min_uniform_beta_1 <- results$beta_1[results$alphas == alpha_round]
min_pointwise_score <- results$normal_law[results$alphas == alpha_round]
# get the estimate from ML algorithms
if(length(estimate_algs) != 0){
# parameters
fit = estimate_algs$qoi
cv = estimate_algs$cv
# format output under cross validation -----------------------------------------
if(cv == TRUE){
print("Not supported under cross-validation")
}
# format output under sample splitting -----------------------------------------
if(cv == FALSE){
# parameters
data = estimate_algs$df$data
algorithms = estimate_algs$df$algorithms
Tcv = estimate_algs$estimates[['Tcv']] %>% as.numeric()
Ycv = estimate_algs$estimates[['Ycv']] %>% as.numeric()
purrr::map(fit$URATE, ~.x) %>%
bind_rows() %>%
mutate(
RATEmin = rate - min_uniform_beta_1*sd - min_uniform_beta_0*sd[length(sd)]*length(sd)/seq(1, length(sd)),
RATEpoint = rate - min_pointwise_score*sd,
fraction = rep(seq(1,length(Ycv))/length(Ycv), length(algorithms)),
type = lapply(algorithms, function(x)rep(x,length(Ycv))) %>% unlist
) %>%
rename(
`GATE estimate` = rate,
`Uniform lower band` = RATEmin,
`Pointwise lower band` =RATEpoint) %>%
tidyr::pivot_longer(
., cols = c(
"GATE estimate", "Uniform lower band", "Pointwise lower band"),
names_to = "Type",
values_to = "value"
) -> data_algs
}
}
# -----------------------------------------
# get HTE from the user-defined function
# -----------------------------------------
if(length(estimate_user) != 0){
# parameters
fit = estimate_user$qoi
cv = estimate_user$cv
Tcv = estimate_user$estimates[['Tcv']] %>% as.numeric()
Ycv = estimate_user$estimates[['Ycv']] %>% as.numeric()
fit$AUPEC %>%
bind_rows() %>%
mutate(
RATEmin = rate - min_uniform_beta_1*sd - min_uniform_beta_0*sd[length(sd)]*length(sd)/seq(1, length(sd)),
RATEpoint = rate - min_pointwise_score*sd,
fraction = rep(seq(1,length(Ycv))/length(Ycv), 1),
type = lapply("user-defined", function(x)rep(x,length(Ycv))) %>% unlist) %>%
rename(
`GATE estimate` = rate,
`Uniform lower band` = RATEmin,
`Pointwise lower band` =RATEpoint) %>%
tidyr::pivot_longer(
., cols = c(
"GATE estimate", "Uniform lower band", "Pointwise lower band"),
names_to = "Type",
values_to = "value"
) -> data_user
}
# dataframe for plotting
data <- bind_rows(data_algs, data_user)
saveRDS(data, "data.rds")
# plot
ggplot(data, aes(x=fraction, y=value)) +
geom_line(alpha=0.8, aes(color = Type)) +
scale_colour_few("Dark") +
scale_linewidth_continuous(range = c(0.5, 1.5)) +
xlab("Maximum Proportion Treated") +
ylab("GATE Estimates") +
facet_wrap(~type) +
scale_x_continuous(labels=scales::percent) +
theme_few() +
geom_hline(yintercept = 0, color = "black", linewidth = 0.5, linetype = "dotted") +
theme(
legend.position = "right",
text = element_text(size=13.5),
axis.text = element_text(size=10),
strip.text = element_text(size = 13.5)
) -> out
return(out)
}