-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherror_analysis.r
413 lines (334 loc) · 16.1 KB
/
error_analysis.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
## Error analysis
setwd("~/Desktop/Code/laplace_approximation/Script")
.libPaths("~/Rlib/")
library(rstan)
library(dplyr)
library(tidyr)
library(scales)
library(boot)
library(directlabels)
library(latex2exp)
source("tools/errorTools.r")
source("tools/analysisTools.r")
################################################################
## Analysis of the disease map
modelName <- "disease_map"
fit_standard <- readRDS(file = file.path("deliv", modelName,
paste0(modelName, "Fit.Rsave")))
fit_benchmark <- readRDS(file = file.path("deliv", modelName,
paste0(modelName, "100long_Fit.Rsave")))
modelName <- "disease_map_ela"
fit_laplace <- readRDS(file = file.path("deliv", modelName,
paste0(modelName, "Fit.Rsave")))
pars <- c("alpha", "rho", "theta[1]", "theta[2]")
# it's worth examining the trace plots
traceplot(fit_standard, pars = pars)
traceplot(fit_laplace, pars = pars)
# Compare the Monte Carlo estimates
summary_standard <- summary(fit_standard, pars = pars)[[1]]
summary_benchmark <- summary(fit_benchmark, pars = pars)[[1]]
summary_laplace <- summary(fit_laplace, pars = pars)[[1]]
# plot Monte Carlo estimate with error
mcse_plot(summary_standard, summary_laplace, summary_benchmark, pars)
# Plot error
iter <- seq(from = 1, to = 500, by = 1)
nChains <- 4
samples_standard <- rstan::extract(fit_standard, pars, permuted = FALSE) # [, 1, ]
samples_benchmark <- rstan::extract(fit_benchmark, pars, permuted = FALSE)
samples_laplace <- rstan::extract(fit_laplace, pars, permuted = FALSE) # [, 1, ]
benchmark <- rep(NA, length(pars))
for (i in 1:length(pars)) benchmark[i] <-
mean(samples_benchmark[, , i])
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T) +
scale_y_continuous(trans = "log")
# Plot the error as a function of time.
# The time for each iteration is the mean warmup time,
# plus the estimated time to produce n samples,
# based on the mean total sampling time.
include_warmup <- F
time_standard <- iteration_time(fit_standard, iter, include_warmup)
time_laplace <- iteration_time(fit_laplace, iter, include_warmup)
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, plot_time = T,
time_standard = time_standard,
time_laplace = time_laplace,
one_chain = F, average_chain = T) +
# scale_x_continuous(trans = "log") +
scale_y_continuous(trans = "log")
#####################################################################
## Analysis for SKIM model
modelName <- "skim_logit"
fit_standard <- readRDS(file = file.path("deliv", modelName, "cluster",
paste0(modelName, "Fit.Rsave")))
benchmarks <- 1:6
fit_benchmark <- list()
for (i in benchmarks) {
fit_benchmark[[i]] <- readRDS(file = file.path("deliv", modelName,
paste0(modelName, "long_", benchmarks[i],
"_Fit.Rsave")))
}
fit_benchmark <- sflist2stanfit(fit_benchmark)
modelName <- "skim_logit_ela"
fit_laplace <- readRDS(file = file.path("deliv", modelName, "cluster",
paste0(modelName, "Fit.Rsave")))
pars <- c("caux", "lambda[81]", "lambda[86]", "tau", "xi")
# pars <- c("lambda[81]")
summary_standard <- summary(fit_standard, pars = pars)[[1]]
summary_benchmark <- summary(fit_benchmark, pars = pars)[[1]]
summary_laplace <- summary(fit_laplace, pars = pars)[[1]]
# plot Monte Carlo estimate with error
mcse_plot(summary_standard, summary_laplace, summary_benchmark, pars) +
scale_y_continuous(trans = "log")
## Plot error
# TEST -- see if the plots are more interpretable with one chain
iter <- seq(from = 1, to = 2000, by = 1)
nChains <- 6 # 6
samples_standard <- rstan::extract(fit_standard, pars, permuted = FALSE)
samples_benchmark <- rstan::extract(fit_benchmark, pars, permuted = FALSE)
samples_laplace <- rstan::extract(fit_laplace, pars, permuted = FALSE)
log_parm <- T
benchmark <- rep(NA, length(pars))
if (!log_parm) {
for (i in 1:length(pars)) benchmark[i] <- mean(samples_benchmark[, , i])
} else {
for (i in 1:length(pars)) benchmark[i] <-
mean(log(samples_benchmark[, , i]))
}
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = log_parm, f = c(mean)) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides="lr")
# Plot for Neurips
# first create benchmark for plots
quantile_90 <- function(x) { quantile(x, probs = 0.9) }
benchmark <- c(mean(log(samples_benchmark[, , 1])),
quantile_90(log(samples_benchmark[, , 2])),
quantile_90(log(samples_benchmark[, , 3])),
mean(log(samples_benchmark[, , 4])),
mean(log(samples_benchmark[, , 5])))
include_warmup <- F
time_standard <- iteration_time(fit_standard, iter, include_warmup)
time_laplace <- iteration_time(fit_laplace, iter, include_warmup)
f <- c(mean, quantile_90, quantile_90, mean, mean)
parm_label <- c(TeX("$E( \\log \ c_{aux})$"),
TeX("$Q_{90}( \\log \\lambda_{2516})$"),
TeX("$Q_{90}( \\log \\lambda_{2586})$"),
TeX("$E( \\log \\tau)$"),
TeX("$E( \\log \\chi)$"))
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = T, f = f, plot_time = T,
time_laplace = time_laplace,
time_standard = time_standard,
parm_label = parm_label) +
theme(legend.position = "none", text = element_text(size = 25)) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^floor(x)),
labels = trans_format("log10", math_format(10^.x))) +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^floor(x)),
labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides="l")
# Check which covariates are softly selected (i.e. inspect 90th quantile)
pars <- c("lambda")
log_lambda <- log(rbind(rstan::extract(fit_benchmark, pars)$lambda))
select_lambda(log_lambda, quant = 0.9, n_select = 6)
# 86 160 179 81 120 151
log_lambda <- log(rbind(rstan::extract(fit_standard, pars)$lambda))
select_lambda(log_lambda, quant = 0.9, n_select = 6)
# 86 160 179 81 120 151
log_lambda <- log(rbind(rstan::extract(fit_laplace, pars)$lambda))
select_lambda(log_lambda, quant = 0.9, n_select = 6)
# 86 179 160 81 120 48
# Examine some of the high scoring lambdas.
pars <- c("lambda[86]", "lambda[160]", "lambda[179]", "lambda[81]")
# First compute the 90th quantile of log lambda
samples_benchmark <- rstan::extract(fit_laplace, pars)
quantile_90_log <- function(x) { quantile(log(x), probs = 0.9) }
benchmark <- lapply(samples_benchmark, quantile_90_log)
benchmark <- unlist(benchmark, use.names = F)
quantile_90 <- function(x) { quantile(x, probs = 0.9) }
samples_standard <- rstan::extract(fit_standard, pars, permuted = F)
samples_laplace <- rstan::extract(fit_laplace, pars, permuted = F)
parm_label <- c(TeX("$\\c_mathrm{aux}"),
TeX("$\\rho$"),
TeX("$\\theta_1$"),
TeX("$\\theta_2$"))
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = log_parm, f = quantile_90) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides="lr")
# Next, examine the probability that log lambda is greater than
# a certain threshold
threshold <- 2.5
prob_threshold_log <- function(x) { sum(log(x) > threshold) / length(x) }
benchmark <- lapply(samples_benchmark, prob_threshold_log)
benchmark <- unlist(benchmark, use.names = F)
prob_threshold <- function(x) { sum(x > threshold) / length(x) }
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = log_parm, f = prob_threshold) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
annotation_logticks(sides="lr")
#####################################################################
## Analysis for horseshoe regression
modelName <- "bernoulli_logit_glm_rhs"
fit_standard <- list()
fit_standard[[1]] <- readRDS(file = file.path("deliv", modelName, "cluster",
paste0(modelName, "_1_to_4Fit.Rsave")))
fit_standard[[2]] <- readRDS(file = file.path("deliv", modelName, "cluster",
paste0(modelName, "_5_to_6Fit.Rsave")))
fit_standard <- sflist2stanfit(fit_standard)
modelName <- "bernoulli_logit_glm_ela"
fit_laplace <- readRDS(file = file.path("deliv", modelName, "cluster",
paste0(modelName, "Fit.Rsave")))
pars <- c("tau", "lambda[1816]", "lambda[2586]", "caux")
summary_standard <- summary(fit_standard, pars = pars)[[1]]
summary_laplace <- summary(fit_laplace, pars = pars)[[1]]
# for the benchmark, read in the summary file generated by cmdstan
# Need to skip first and last row
summary_benchmark_full <- read.table("deliv_cmdstan/summary.dat",
skip = 6, nrows = 18108)
summary_benchmark <- summary_benchmark_full[pars, ]
# plot Monte Carlo estimate with error (not for f, since we will
# examine these after applying an inverse logit transformation)
mcse_plot(summary_standard, summary_laplace, summary_benchmark, pars)
# plot error as a function of iterations
iter <- seq(from = 1, to = 2000, by = 1)
nChains <- 6
# WARNING -- make sure the order of parameters are consistent between
# samples and saved benchmarks!!
# pars <- c("tau", "caux", "lambda[2586]", "lambda[1816]")
pars <- c("tau", "caux", "lambda[1816]", "lambda[2586]")
samples_standard <- rstan::extract(fit_standard, pars = pars, permuted = FALSE)
samples_laplace <- rstan::extract(fit_laplace, pars = pars, permuted = FALSE)
# for testing purposes
logs_laplace <- log(samples_laplace)
logs_standard <- log(samples_standard)
log_parm = T
# Let's first start by analyzing the first 4 parameters (as we
# will put these on the log scale).
pars <- pars[1:4]
if (!log_parm) {
benchmark <- summary_benchmark[, 1]
} else {
nChains_benchmark <- 8
mean_log <- array(NA, c(nChains_benchmark, length(pars)))
fileName <- "bernoulli_logit_glm_rhs.logmean"
for (i in 1:nChains_benchmark) {
table <- read.csv(file = paste0("deliv_cmdstan/",
fileName, ".", i, ".csv"))
# TODO -- find a slicker way to do the next operation.
# mean_log[i, ] <- c(table[[2]])
elements <- c(table[[2]])
for (k in 2:length(pars)) elements <- c(elements,
table[[k + 1]])
mean_log[i, ] <- elements
}
benchmark <- colMeans(mean_log)
}
error_plot(samples_standard, samples_laplace, benchmark,
pars, one_chain = F, average_chain = T,
log_parm = log_parm, f = c(mean)) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x)))
# (For Neurips paper), let's examine the error for expectation value
# of caux and tau, and 90th quantile of lambda[1816] and lambda[2586].
# do the same analysis with quantiles.
modelName <- "bernoulli_logit_glm_rhs"
file <- file.path("deliv_cmdstan", paste0(modelName, ".90quantile.csv"))
benchmark_quantile <- read.csv(file = file)
benchmark_quantile <- unname(benchmark_quantile[, 2])
# Recall pars = (tau, caux, lambda[1816], lambda[2586])
benchmark <- c(benchmark[1], benchmark[2], benchmark_quantile[2],
benchmark_quantile[1])
# benchmark <- c(benchmark[1], benchmark_quantile[2],
# benchmark_quantile[1], benchmark[4])
quantile_90 <- function(x) { quantile(x, probs = 0.9) }
f <- c(mean, mean, quantile_90, quantile_90)
include_warmup <- F
time_standard <- iteration_time(fit_standard, iter, include_warmup)
time_laplace <- iteration_time(fit_laplace, iter, include_warmup)
parm_label <- c(TeX("$E( \\log \ c_{aux})$"),
TeX("$Q_{90}( \\log \\lambda_{1816})$"),
TeX("$Q_{90}( \\log \\lambda_{2586})$"),
TeX("$E( \\log \\tau)$"))
error_plot(samples_standard, samples_laplace, benchmark,
pars, one_chain = F, average_chain = T,
log_parm = T, f = f, plot_time = T,
time_standard = time_standard,
time_laplace = time_laplace) + #,
# parm_label = parm_label) +
theme(legend.position = "none", text = element_text(size = 25)) +
scale_y_log10(breaks = trans_breaks("log10", function(x) 10^floor(x)),
labels = trans_format("log10", math_format(10^.x))) +
scale_x_log10(breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))) +
xlab("time (s)") + ylab("squared error")
# Let's examine the probability for the first two patients
pars <- c("f[1]", "f[2]", "f[99]", "f[100]")
samples_standard <- rstan::extract(fit_standard, pars = pars, permuted = FALSE)
samples_standard <- inv.logit(samples_standard)
samples_laplace <- rstan::extract(fit_laplace, pars = pars, permuted = FALSE)
samples_laplace <- inv.logit(samples_laplace)
# read in expected value for probability
nChains_benchmark <- 8
mean_prob <- array(NA, c(length(pars), nChains_benchmark))
for (i in 1:nChains_benchmark) {
chain_prob <- read.csv(paste0("deliv_cmdstan/", fileName, ".", i, ".csv"))
mean_prob[, i] <- c(chain_prob$`f.1.`, chain_prob$`f.2.`,
chain_prob$`f.99.`, chain_prob$`f.100.`)
}
benchmark <- rowMeans(mean_prob)
benchmark
# 0.2024666 0.3154635 0.8985348 0.9599858
c(mean(samples_standard[, , 1]), mean(samples_standard[, , 2]),
mean(samples_standard[, , 3]), mean(samples_standard[, , 4]))
# 0.2077414 0.3144079 0.8952827 0.9577220
c(mean(samples_laplace[, , 1]), mean(samples_laplace[, , 2]),
mean(samples_laplace[, , 3]), mean(samples_laplace[, , 4]))
# 0.2588491 0.2721569 0.8141369 0.8930643
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = F, f = mean) +
scale_y_continuous(trans = "log")
##
# Let's take a closer look at the lambdas
pars <- c("lambda[2586]", "lambda[1816]", "lambda[4960]",
"lambda[4238]", "lambda[4843]", "lambda[3381]",
"lambda[4647]")
# Read in benchmark for probability of exceeding threshold
# (the prob is computed for each chain, it suffices to
# average out the quantities).
modelName <- "bernoulli_logit_glm_rhs"
file <- file.path("deliv_cmdstan", paste0(modelName, ".prob"))
benchmark <- read.csv(file = paste0(file, "1.csv"))
benchmark <- as.numeric(unname(benchmark[, -1]))
# prob_threshold is defined above
samples_standard <- rstan::extract(fit_standard, pars = pars, permuted = FALSE)
samples_laplace <- rstan::extract(fit_laplace, pars = pars, permuted = FALSE)
prob_threshold_ <- function(x) { prob_threshold(x, threshold)}
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = log_parm, f = prob_threshold_) +
scale_y_continuous(trans = "log")
# do the same analysis with quantiles.
file <- file.path("deliv_cmdstan", paste0(modelName, ".90quantile.csv"))
benchmark <- read.csv(file = file)
benchmark <- unname(benchmark[, 2])
error_plot(samples_standard, samples_laplace, benchmark,
pars = pars, one_chain = F, average_chain = T,
log_parm = log_parm, f = quantile_90) +
scale_y_continuous(trans = "log")