-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhte_summary.R
302 lines (253 loc) · 8.15 KB
/
hte_summary.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#' Summarize Heterogeneity and Consistency Tests
#' @param object An object of \code{evaluate_hte} class (typically an output of \code{evaluate_hte()} function).
#' @param ... Other parameters.
#' @import purrr
#' @importFrom stats pnorm
#' @export
summary.hte <- function(object, ...) {
# parameters
out <- urate_algs_vec <- urate_user_vec <- list()
estimate_algs = object$out_algs
estimate_user = object$out_user
# algorithm <- object$df$algorithm
# cv <- object$cv
# fit <- object$qoi
# -----------------------------------------
# estimate HTE from ML algorithms
# -----------------------------------------
if(length(estimate_algs) != 0){
# parameters
algorithm <- estimate_algs$df$algorithm
cv <- estimate_algs$cv
fit <- estimate_algs$qoi
# compute quantities under sample splitting -----------------------------------------
if (cv == FALSE) {
# group HTE
gate_algs_vec <- fit$GATE %>%
purrr::map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(
statistic = gate / sd,
p.value = 2 * pnorm(abs(gate / sd), lower.tail = FALSE),
upper = gate + qnorm(0.975) * sd,
lower = gate - qnorm(0.975) * sd
) %>%
rename(
estimate = gate,
std.deviation = sd,
algorithm = alg,
group = group,
z.score = statistic
) %>%
select(
group, algorithm, estimate, std.deviation, lower, upper, z.score, p.value)
urate_algs_vec <- fit$URATE %>%
purrr::map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(
statistic = rate / sd,
p.value = 2 * pnorm(abs(rate / sd), lower.tail = FALSE)
) %>%
group_by(alg) %>%
mutate(
fraction = seq(1,length(rate))/length(rate),
est = rate - 1.2*sd - 0.68*sd[length(sd)] * length(sd)/seq(1, length(sd)),
best_ind = which.max(est),
# estimate
proportion = fraction[best_ind],
best_rate = rate[best_ind],
conf.low.uniform = rate[best_ind] - 1.2*sd[best_ind] - 0.68* sd[length(sd)] * length(sd)/best_ind
) %>%
filter(rate == best_rate) %>%
rename(
estimate = rate,
std.deviation = sd,
algorithm = alg,
z.score = statistic
) %>%
select(
algorithm, estimate, std.deviation, conf.low.uniform, z.score, p.value)
out <- list(
GATE = gate_algs_vec,
URATE = urate_algs_vec)
}
# compute quantities under cross-validation -----------------------------------------
if (cv == TRUE) {
# group HTE
gate_algs_vec <- fit$GATE %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(
statistic = gate / sd,
p.value = 2 * pnorm(abs(gate / sd), lower.tail = FALSE),
upper = gate + qnorm(0.975) * sd,
lower = gate - qnorm(0.975) * sd
) %>%
rename(
estimate = gate,
std.deviation = sd,
algorithm = alg,
group = group,
z.score = statistic
) %>%
select(
group, algorithm, estimate, std.deviation, lower, upper, z.score, p.value)
# exceptional reponders not supported for CV
urate_algs_vec <- NULL
}
out <- list(
GATE = gate_algs_vec,
URATE = urate_algs_vec)
}
if(length(estimate_user) != 0){
# parameters
algorithm <- estimate_user$df$algorithm
cv <- estimate_user$cv
fit <- estimate_user$qoi
# group HTE
gate_user_vec <- fit$GATE %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(
statistic = gate / sd,
p.value = 2 * pnorm(abs(gate / sd), lower.tail = FALSE),
upper = gate + qnorm(0.975) * sd,
lower = gate - qnorm(0.975) * sd
) %>%
rename(
estimate = gate,
std.deviation = sd,
algorithm = alg,
group = group,
z.score = statistic
) %>%
select(
group, algorithm, estimate, std.deviation, lower, upper, z.score, p.value)
# exceptional reponders
urate_user_vec <- fit$URATE %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(
statistic = rate / sd,
p.value = 2 * pnorm(abs(rate / sd), lower.tail = FALSE)
) %>%
group_by(alg) %>%
mutate(
fraction = seq(1,length(rate))/length(rate),
est = rate - 1.2*sd - 0.68*sd[length(sd)] * length(sd)/seq(1, length(sd)),
best_ind = which.max(est),
# estimate
proportion = fraction[best_ind],
best_rate = rate[best_ind],
conf.low.uniform = rate[best_ind] - 1.2*sd[best_ind] - 0.68* sd[length(sd)] * length(sd)/best_ind
) %>%
filter(rate == best_rate) %>%
rename(
estimate = rate,
std.deviation = sd,
algorithm = alg,
z.score = statistic
) %>%
select(
algorithm, estimate, std.deviation, conf.low.uniform, z.score, p.value)
out <- list(
GATE = bind_rows(gate_algs_vec, gate_user_vec),
URATE = bind_rows(urate_algs_vec, urate_user_vec)
)
}
class(out) <- c("summary.hte", class(out))
return(out)
}
#' Print
#' @importFrom cli cat_rule
#' @param x An object of \code{summary.hte} class. This is typically an output of \code{summary.hte()} function.
#' @param ... Other parameters. Currently not supported.
#' @export
print.summary.hte <- function(x, ...) {
# GATE
cli::cat_rule(left = "GATE")
print(as.data.frame(x[["GATE"]]), digits = 2)
cli::cat_line("")
# URATE
cli::cat_rule(left = "URATE")
if (is.null(x[["URATE"]]) || ncol(x[["URATE"]]) == 0) {
cli::cat_line("Not supported with cross-validation")
} else {
print(as.data.frame(x[["URATE"]]), digits = 2)
}
cli::cat_line("")
}
#' Summarize Hetereogeneity and Consistency Tests
#' @param object An object of \code{test_hte} class (typically an output of \code{test_hte()} function).
#' @param ... Other parameters.
#' @importFrom stats pnorm
#' @export
summary.test_hte <- function(object, ...) {
out <- list()
consist_tibble <- tibble()
het_tibble <- tibble()
## -----------------------------------------
## hypothesis tests
## -----------------------------------------
if (names(object[1]) == "consist") {
# parameters for test_hte object
consist <- object$consist
het <- object$het
consist_names <- names(consist)
het_names <- names(het)
# reformat
out[["Consistency"]] <- consist %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(algorithm = consist_names) %>%
rename(statistic = stat,
p.value = pval) %>%
select(algorithm, statistic, p.value)
out[["Heterogeneity"]] <- het %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(algorithm = het_names) %>%
rename(statistic = stat,
p.value = pval) %>%
select(algorithm, statistic, p.value)
}
if (names(object[1]) == "consistcv") {
# parameters for test_hte object
consist <- object$consistcv
het <- object$hetcv
consist_names <- names(consist)
het_names <- names(het)
# reformat
out[["Consistency_cv"]] <- consist %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(algorithm = consist_names) %>%
rename(statistic = stat,
p.value = pval) %>%
select(algorithm, statistic, p.value)
out[["Heterogeneity_cv"]] <- het %>%
map(., ~ as_tibble(.)) %>%
bind_rows() %>%
mutate(algorithm = het_names) %>%
rename(statistic = stat,
p.value = pval) %>%
select(algorithm, statistic, p.value)
}
class(out) <- c("summary.test_hte", class(out))
return(out)
}
#' Print
#' @importFrom cli cat_rule
#' @param x An object of \code{summary.test_hte} class. This is typically an output of \code{summary.test_hte()} function.
#' @param ... Other parameters.
#' @export
print.summary.test_hte <- function(x, ...) {
# Rank Consistency Test
cli::cat_rule(left = "Rank Consistency Test Results")
print(as.data.frame(x[["Consistency"]], digits = 2))
cli::cat_line("")
# Group Heterogeneity Test
cli::cat_rule(left = "Group Heterogeneity Test Results")
print(as.data.frame(x[["Heterogeneity"]], digits = 2))
cli::cat_line("")
}