-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_utils.R
More file actions
149 lines (117 loc) · 4.28 KB
/
Copy pathplot_utils.R
File metadata and controls
149 lines (117 loc) · 4.28 KB
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
library("ggplot2")
library("dplyr")
computeAcceptanceRatios <- function(x){
N <- dim(x)[1]
aR <- sapply(2:N, function(i){
return(x[i] == x[i-1])
})
aRval <- 1 - length(aR[1:(N-1)][aR[1:(N-1)]])/(N - 1)
return(aRval)
}
#### plotting acceptance ratio vs eps and i
plot_acceptance_3d <- function(N, p, i, eps, h) {
ars <- array(dim = c(length(eps), length(i)), dimnames = list(eps = eps, i = i))
for (k in 1:length(i)) {
for (j in 1:length(eps)) {
sam <- gmat::mh_sphere(N = N, k = p - i[k] + 1, i = i[k], eps = eps[j], h = h)
ars[j, k] <- computeAcceptanceRatios(sam)
}
}
wd <- getwd()
dir.create(paste0(wd, "/plot/"), showWarnings = FALSE)
df <- ars %>% as.tbl_cube(met_name = "ars") %>% as_tibble()
pl <- ggplot(df, aes(x = eps, y = i, z = ars)) +
geom_contour(aes(colour = ..level..)) +
scale_color_gradient(low = "blue", high = "red") +
theme_bw() +
theme(text = element_text(size = 20)) +
xlab("Perturbation variance") +
ylab("Row number") +
ggtitle("Contour plot of the acceptance ratio surface")
ggsave(filename = "acceptance_3d.pdf", plot = pl, device = "pdf", path = "plot",
width = 9, height = 4)
}
#### plotting acceptance ratio vs i
plot_acceptance_k <- function(N, p, i, eps, h) {
eps_len <- length(eps)
ars <- array(dim = c(length(i), length(eps)), dimnames = list(i = i, eps = eps))
for (k in 1:length(i)) {
for (j in 1:eps_len) {
sam <- gmat::mh_sphere(N = N, k = p - i[k] + 1, i = i[k], eps = eps[j], h = h)
ars[k, j] <- computeAcceptanceRatios(sam)
}
}
wd <- getwd()
dir.create(paste0(wd, "/plot/"), showWarnings = FALSE)
palette <- colorRampPalette(colors = c("black","red"))
colors <- palette(eps_len)
df <- ars %>% as.tbl_cube(met_name = "ars") %>% as_tibble()
df$eps <- as.factor(df$eps)
pl <- ggplot(df, aes(x = i, y = ars, group = eps, color = eps)) +
geom_line() +
geom_point() +
theme_bw() +
theme(text = element_text(size = 20), legend.position = "bottom") +
scale_color_manual(values = colors) +
xlab("Row number") +
ylab("Acceptance ratio") +
ggtitle("")
ggsave(filename = "acceptance_k.pdf", plot = pl, device = "pdf", path = "plot",
width = 7, height = 5)
}
#### plotting acceptance ratio vs eps
plot_acceptance_eps <- function(N, p, i, eps, h) {
ars <- array(dim = c(length(i), length(eps)), dimnames = list(i = i, eps = eps))
for (k in 1:length(i)) {
for (j in 1:length(eps)) {
sam <- gmat::mh_sphere(N = N, k = p - i[k] + 1, i = i[k], eps = eps[j], h = h)
ars[k, j] <- computeAcceptanceRatios(sam)
}
}
wd <- getwd()
dir.create(paste0(wd, "/plot/"), showWarnings = FALSE)
palette <- colorRampPalette(colors = c("black","red"))
colors <- palette(length(i))
df <- ars %>% as.tbl_cube(met_name = "ars") %>% as_tibble()
df$i <- as.factor(df$i)
pl <- ggplot(df, aes(x = eps, y = ars, group = i, color = i)) +
geom_line() +
geom_point() +
theme_bw() +
theme(text = element_text(size = 20), legend.position = "bottom") +
scale_color_manual(values = colors) +
xlab("Perturbation variance") +
ylab("Acceptance ratio") +
ggtitle("")
ggsave(filename = "acceptance_eps.pdf", plot = pl, device = "pdf", path = "plot",
width = 7, height = 5)
}
#### plotting time experiment
plot_time <- function(p, method, fname, dir_name = "res", log = FALSE) {
res <- array(dim = c(length(p), length(method)), dimnames = list(p = p, method = method))
for (i in 1:length(p)) {
for (j in 1:length(method)) {
time <- readRDS(file = paste0(dir_name,"/t_",method[j],"_",p[i], ".rds"))
res[i, j] <- as.double(time, unit = "secs")
}
}
wd <- getwd()
dir.create(paste0(wd, "/plot/"), showWarnings = FALSE)
palette <- colorRampPalette(colors = c("black","red"))
colors <- palette(length(method))
df <- res %>% as.tbl_cube(met_name = "time") %>% as_tibble()
df$method <- as.factor(df$method)
pl <- ggplot(df, aes(x = p, y = time, color = method, group = method)) +
geom_line() +
geom_point() +
theme_bw() +
theme(text = element_text(size = 20), legend.position = "bottom") +
xlab("Number of nodes") +
ylab("Time in seconds") +
scale_color_manual(values = colors)
if (log == TRUE) {
pl <- pl + scale_y_log10()
}
ggsave(filename = fname, plot = pl, device = "pdf", path = "plot/", width = 7,
height = 4)
}