-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvds_plotting.r
142 lines (119 loc) · 5.2 KB
/
dvds_plotting.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
library(dplyr)
library(ggplot2)
set.seed(1)
# Plots DVDS simulation results
# =============================================
# Reading data
binary_dvds = read.table("Simulations/dvds_output/binary_dvds.csv", header=T) %>%
mutate(outcome = "Binary", method = "DVDS")
binary_zsb = read.table("Simulations/dvds_output/binary_zsb.csv", header=T) %>%
mutate(outcome = "Binary", method = "ZSB")
continuous_dvds = read.table("Simulations/dvds_output/continuous_dvds.csv", header=T) %>%
mutate(outcome = "Continuous", method = "DVDS")
continuous_zsb = read.table("Simulations/dvds_output/continuous_zsb.csv", header=T) %>%
mutate(outcome = "Continuous", method = "ZSB")
df = bind_rows(binary_dvds, binary_zsb, continuous_dvds, continuous_zsb)
# =============================================
# Computing sharp bounds
# Binary bounds via simulation
Lambdas = as.numeric(unique(df$Lambda))
n = 500000
d = 5
X = matrix(runif(n*d, -1, 1), nrow = n)
e = 1/(1 + exp(X[,1] + 0.5*(X[,2]>0) + 0.5*X[,2]*X[,3]))
mu = 1/(1 + exp(0.5*X[,1] + X[,2] + 0.25*X[,2]*X[,3]))
true_upper = sapply(Lambdas, function(l) {
psi1plus = mean(e*mu + (1-e)*pmin(1 - 1/l + mu/l, mu*l))
psi0mins = mean((1-e)*mu + e*pmax(1 - l + mu*l, mu/l))
psi1plus - psi0mins
})
true_lower = sapply(Lambdas, function(l) {
psi1mins = mean(e*mu + (1-e)*pmax(1 - l + mu*l, mu/l))
psi0plus = mean((1-e)*mu + e*pmin(1 - 1/l + mu/l, mu*l))
psi1mins - psi0plus
})
binary_truth = data.frame("outcome" = "Binary", "Lambda" = Lambdas,
"true_upper" = true_upper,
"true_lower" = true_lower)
# Continuous bounds via explicit formulas
continuous_truth = data.frame("outcome" = "Continuous", "Lambda" = Lambdas,
"true_upper" = (1+1/3)*(Lambdas^2 - 1)/Lambdas*dnorm(qnorm(Lambdas/(Lambdas+1))),
"true_lower" =-(1+1/3)*(Lambdas^2 - 1)/Lambdas*dnorm(qnorm(Lambdas/(Lambdas+1))))
truebounds = bind_rows(binary_truth, continuous_truth)
df = left_join(df, truebounds, by=c("Lambda", "outcome"))
# =============================================
# Plotting results
pdf(file = "Simulations/dvds_output/art/binary_comparison.pdf", width=6, height=4)
df %>%
filter(outcome == "Binary") %>%
mutate(method = ifelse(method == "DVDS", "Binary DVDS", "Binary ZSB")) %>%
mutate(Lambda = round(Lambda, 2)) %>%
ggplot() +
geom_boxplot(aes(x = factor(Lambda), y = upper, col = factor(method), fill = factor(method)), alpha = 0.25) +
geom_boxplot(aes(x = factor(Lambda), y = lower, col = factor(method), fill = factor(method)), alpha = 0.25) +
geom_line(aes(x = as.numeric(factor(Lambda)), y = true_lower), size = 1) +
geom_line(aes(x = as.numeric(factor(Lambda)), y = true_upper), size = 1) +
facet_wrap(.~ method) +
theme_bw(base_size = 15) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "None"
) +
scale_x_discrete(breaks = levels(factor(round(df$Lambda, 2)))[c(1, 6, 11, 16, 20)]) +
xlab(expression(Lambda)) +
ylab("")
dev.off()
pdf(file = "Simulations/dvds_output/art/continuous_comparison.pdf", width=6, height=4)
df %>%
filter(outcome == "Continuous") %>%
mutate(method = ifelse(method == "DVDS", "Continuous DVDS", "Continuous ZSB")) %>%
mutate(Lambda = round(Lambda, 2)) %>%
ggplot() +
geom_boxplot(aes(x = factor(Lambda), y = upper, col = factor(method), fill = factor(method)), alpha = 0.25) +
geom_boxplot(aes(x = factor(Lambda), y = lower, col = factor(method), fill = factor(method)), alpha = 0.25) +
geom_line(aes(x = as.numeric(factor(Lambda)), y = true_lower), size = 1) +
geom_line(aes(x = as.numeric(factor(Lambda)), y = true_upper), size = 1) +
facet_wrap(.~ method) +
theme_bw(base_size = 15) +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "None"
) +
scale_x_discrete(breaks = levels(factor(round(df$Lambda, 2)))[c(1, 6, 11, 16, 20)]) +
xlab(expression(Lambda)) +
ylab("")
dev.off()
# =============================================
# Compute coverage
coverage = df %>%
filter(method == "DVDS") %>%
group_by(Lambda, outcome) %>%
mutate(covered = (lower.CI < true_lower & true_upper < upper.CI)) %>%
summarise(coverage = mean(covered))
binary.coverage = coverage %>% filter(outcome == "Binary") %>% pull(coverage)
continuous.coverage = coverage %>% filter(outcome == "Continuous") %>% pull(coverage)
names(binary.coverage) = round(unique(df$Lambda), 2)
names(continuous.coverage) = round(unique(df$Lambda), 2)
coveragePrint = function(cover.rates) {
return(paste0(
"coverage ranging from $",
round(100 * min(cover.rates), 1),
"\\%$ (when $\\Lambda = ",
names(cover.rates)[which.min(cover.rates)],
"$) to $",
round(100 * max(cover.rates), 1),
"\\%$ (when $\\Lambda = ",
names(cover.rates)[which.max(cover.rates)],
"$) with average coverage of $",
round(100 * mean(cover.rates), 1),
"\\%"
))
}
fileConn <- file("Simulations/dvds_output/binary_coverageRates.tex")
writeLines(coveragePrint(binary.coverage), fileConn)
close(fileConn)
fileConn <- file("Simulations/dvds_output/continuous_coverageRates.tex")
writeLines(coveragePrint(continuous.coverage), fileConn)
close(fileConn)