-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrorWasserstein.R
160 lines (130 loc) · 5.18 KB
/
errorWasserstein.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
# Script to look at error bars on wasserstein stat via subsampling.
# Are they larger than diff between posteriors
library(tidyverse)
library(ggplot2)
path <- getwd()
figPath <- paste0(path, '/figures/')
load('postR0.RData')
# function for distance from posterior with full data
wassersteinCompare <- function(df) {
op <- data.frame()
for (i in 1:100) {
fullData <- sample(na.omit(df$fullData), ceiling(0.5 * length(na.omit(df$fullData))), replace = FALSE)
dateData <- sample(na.omit(df$dateData), ceiling(0.5 * length(na.omit(df$dateData))), replace = FALSE)
seqData <- sample(na.omit(df$seqData), ceiling(0.5 * length(na.omit(df$seqData))), replace = FALSE)
noData <- sample(na.omit(df$noData), ceiling(0.5 * length(na.omit(df$noData))), replace = FALSE)
op <- rbind(op, c(transport::wasserstein1d(fullData, dateData),
transport::wasserstein1d(fullData, seqData),
transport::wasserstein1d(fullData, noData),
coda::effectiveSize(coda::as.mcmc(fullData)),
coda::effectiveSize(coda::as.mcmc(dateData)),
coda::effectiveSize(coda::as.mcmc(seqData)),
coda::effectiveSize(coda::as.mcmc(noData))
))
}
# note burnin is already removed in getPosteriors.R, producting chains object
names(op) <- c('fullData||dateData', 'fullData||seqData', 'fullData||noData',
'fullDataESS', 'dateDataESS', 'seqDataESS', 'noDataESS')
return(op)
}
set.seed(1234)
tmp <- lapply(chains, function(x) wassersteinCompare(x))
tmp <- list()
for (i in seq_along(chains)){
tmp[[i]] <- wassersteinCompare(chains[[i]])
print(paste0("### DONE", i, " ###"))
}
# add names
for (i in seq_along(tmp)){
id <- rep(names(chains)[i], dim(tmp[[i]])[1])
tmp[[i]] <- cbind(tmp[[i]], id)
}
save(tmp, file = "subsampChains.RData")
# classify subsamples
classify <- function(df) {
subsampClass <- vector()
for (i in 1:dim(df)[1]){
if(df[i, 1] < df[i, 2]) {
subsampClass <- c(subsampClass, 'Date-Driven')
} else {
subsampClass <- c(subsampClass, 'Seq-Driven')
}
}
return(cbind(df, subsampClass))
}
subsampWass <- do.call(rbind, tmp)
subsampWass <- classify(subsampWass)
save(subsampWass, file = "errorWasserstein.RData")
################################################################
### Can start from here once errorWasserstein.RData is saved ###
################################################################
# Now compare to full classification
load('wassersteinData.RData')
load('errorWasserstein.RData')
# bind full sim data to subsampled data
errorClass <- subsampWass %>%
left_join(wassersteinData, by = "id")
colnames(errorClass)[1:3] <- c("sub_wd", "sub_ws", "sub_wn")
# now add dSD
errorClass <- errorClass %>%
mutate(dSD = abs(wd - ws)) %>%
mutate(mismatch = case_when(
subsampClass == class ~ 0,
subsampClass != class ~ 1,
)) %>%
select(id, dSD, mismatch, sampProp, rate)
errorClass <- errorClass %>%
group_by(id, dSD) %>%
mutate(num = sum(mismatch)) %>%
#select(id, dSD, num, sampProp, rate) %>%
distinct()
# plotting
viol <- ggplot(errorClass, aes(y = dSD, x = num > 0, fill = num > 0)) +
geom_violin(draw_quantiles = c(0.5)) +
geom_point(shape = 21, size = 2, position = position_jitterdodge(), color = "black", alpha = 1) +
xlab("") + ylab(latex2exp::TeX("$\\log_{10}(d_{SD})$")) +
scale_x_discrete(labels = c("No Mismatch", "Some Mismatch")) +
scale_fill_manual(values = alpha(c("dodgerblue", "red"), 0.6),
labels = c("No Mismatch", "Some Mismatch")) +
scale_y_continuous(trans = scales::log10_trans(),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))) +
theme_minimal() +
theme(legend.position = "none",
legend.title = element_blank(),
legend.text = element_text(size = 14),
axis.title = element_text(size = 16),
axis.text = element_text(size = 14),
axis.text.x = element_blank())
line <- ggplot(errorClass, aes(y = num, x = dSD, fill = num > 0)) +
geom_point(shape = 21, size = 2, color = "black") +
geom_smooth(se = F, col = "black", fill = NA, lwd = 0.5) +
ylab("Number of Misclassifications") + xlab(latex2exp::TeX("$\\log_{10}(d_{SD})$")) +
scale_x_continuous(trans = scales::log10_trans(),
breaks = scales::trans_breaks("log10", function(x) 10^x),
labels = scales::trans_format("log10", scales::math_format(10^.x))) +
scale_fill_manual(values=alpha(c("dodgerblue", "red"), 0.8),
labels = c("No Mismatch", "Some Mismatch")) +
theme_minimal() +
theme(legend.position = "none",
legend.title = element_blank(),
legend.text = element_text(size = 16),
axis.title = element_text(size = 14),
axis.text = element_text(size = 14))
leg <- cowplot::get_legend(line + theme(legend.position = "bottom"))
p1 <- cowplot::plot_grid(line, viol, labels = "AUTO", ncol = 2)
p1 <- cowplot::plot_grid(p1, leg, ncol = 1, rel_heights = c(5, 1))
tiff(file = paste0(figPath, "errorWasserstein.tiff"), compression = "lzw",
units = "in", width = 6, height = 5, res = 300)
p1
dev.off()
# num of datasets where mismatch occurred
length(unique(errorClass[which(errorClass$mismatch == 1), ]$id))
# = 99
# num missmatches total
errorClass <- errorClass %>%
ungroup()
print(sum(errorClass$mismatch))
# =99 - actually the same!
# how many of 400 had a mismatch
length(unique(errorClass[which(errorClass$mismatch == 1), ]$id)) # = 99