-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting.R
130 lines (121 loc) · 5.87 KB
/
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
require(gridExtra)
require(tidyverse)
require(grid)
require(xtable)
ggmat <- tibble(values = c(c(0, beta),
as.vector(coef(cv.l, s = cv.l$lambda.1se)),
as.vector(coef(cv.r, s = cv.r$lambda.1se)),
as.vector(coef(cv.ropt, s = cv.ropt$lambda.1se)),
c(coef(lm1), rep(0, sum(omegas[2:3]))),
c(coef(lm2), rep(0, omegas[3])),
coef(lm3)
),
source = factor(rep(c("True values",
"Lasso",
"Ridge regression",
"Hierarchical ridge",
"Linear model, \n main effects",
"Linear model, \n + second order interactions",
"Linear model, \n + third order interactions"),
each = (sum(omegas)+1)),
levels= c("True values", "Lasso", "Ridge regression",
"Hierarchical ridge",
"Linear model, \n main effects",
"Linear model, \n + second order interactions",
"Linear model, \n + third order interactions") ),
index = c(rep(1:(sum(omegas) + 1), 7))
)
ylims <- max(abs(ggmat$values))*c(-1,1)
xlims <- c(1, sum(omegas)+1)
g1 <- ggplot(ggmat[1:(4*(sum(omegas) +1)),], aes(x = index, y = values)) +
facet_wrap(~source, ncol = 3) +
geom_hline(yintercept = 0 , color = "red", linetype = 3) +
geom_point(size = .9) +
geom_vline(xintercept = cumsum(omegas)+1, linetype = 2) +
theme_bw() +
theme(strip.text.x = element_text(size = 12),
strip.background = element_blank(),
legend.position="none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
plot.margin = unit(c(1,1,0,1), "lines")) +
xlim(xlims) +
ylim(ylims)
g1 <- ggplot_gtable(ggplot_build(g1))
g1$layout$clip[g1$layout$name %in% c("panel-1-1", "panel-2-1","panel-3-1",
"panel-1-2", "panel-2-2","panel-3-2")] <- "off"
g2 <- ggplot(ggmat[(4*(sum(omegas)+1)+1):nrow(ggmat),], aes(x = index, y = values)) +
facet_wrap(~source) +
geom_hline(yintercept = 0 , color = "red", linetype = 3) +
geom_point(size = .9) +
theme_bw() +
theme(strip.text.x = element_text(size = 12),
strip.background = element_blank(),
legend.position="none",
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank(),
axis.title.y=element_blank(),
plot.margin = unit(c(0,1,2,1), "lines")) +
xlim(xlims) +
ylim(ylims) +
# horizontal bin lines
annotation_custom(grob = linesGrob(), xmin = 1, xmax = cumsum(omegas)[1],
ymin = ylims[1]-.5, ymax = ylims[1]-.5) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[1]+1,
xmax = cumsum(omegas)[2], ymin = ylims[1]-.5,
ymax = ylims[1]-.5) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[2]+1,
xmax = cumsum(omegas)[3], ymin = ylims[1]-.5,
ymax = ylims[1]-.5) +
# vertical bin lines
annotation_custom(grob = linesGrob(), xmin = 1, xmax = 1,
ymin = ylims[1]-.5, ymax = ylims[1]-.2) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[1],
xmax = cumsum(omegas)[1], ymin = ylims[1]-.5,
ymax = ylims[1]-.2) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[1]+1,
xmax = cumsum(omegas)[1]+1, ymin = ylims[1]-.5,
ymax = ylims[1]-.2) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[2],
xmax = cumsum(omegas)[2], ymin = ylims[1]-.5,
ymax = ylims[1]-.2) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[2]+1,
xmax = cumsum(omegas)[2]+1, ymin = ylims[1]-.5,
ymax = ylims[1]-.2) +
annotation_custom(grob = linesGrob(), xmin = cumsum(omegas)[3],
xmax = cumsum(omegas)[3], ymin = ylims[1]-.5,
ymax = ylims[1]-.2) +
# vertical bin ticks
annotation_custom(grob = linesGrob(),
xmin = mean(c(1, omegas[1])),
xmax = mean(c(1, omegas[1])),
ymin = ylims[1]-.6,
ymax = ylims[1]-.35) +
annotation_custom(grob = linesGrob(),
xmin = mean(c(cumsum(omegas)[1]+1, cumsum(omegas)[2])),
xmax = mean(c(cumsum(omegas)[1]+1, cumsum(omegas)[2])),
ymin = ylims[1]-.6,
ymax = ylims[1]-.35) +
annotation_custom(grob = linesGrob(),
xmin = mean(c(cumsum(omegas)[2]+1, cumsum(omegas)[3])),
xmax = mean(c(cumsum(omegas)[2]+1, cumsum(omegas)[3])),
ymin = ylims[1]-.6,
ymax = ylims[1]-.35) +
# bin text
annotation_custom(textGrob("k = 1", gp = gpar(fontsize = 10)),
xmin = mean(c(1, omegas[1])),xmax = mean(c(1, omegas[1])),
ymin = ylims[1]-.8, ymax = ylims[1]-.8) +
annotation_custom(textGrob("k = 2", gp = gpar(fontsize = 10)),
xmin = mean(c(cumsum(omegas)[1]+1, cumsum(omegas)[2])),
xmax = mean(c(cumsum(omegas)[1]+1, cumsum(omegas)[2])),
ymin = ylims[1]-.8, ymax = ylims[1]-.8) +
annotation_custom(textGrob("k = 3", gp = gpar(fontsize = 10)),
xmin = mean(c(cumsum(omegas)[2]+1, cumsum(omegas)[3])),
xmax = mean(c(cumsum(omegas)[2]+1, cumsum(omegas)[3])),
ymin = ylims[1]-.8, ymax = ylims[1]-.8)
g2 <- ggplot_gtable(ggplot_build(g2))
g2$layout$clip[g2$layout$name %in% c("panel-1-1", "panel-2-1","panel-3-1")] <- "off"
g <- grid.arrange(g1, g2, heights = c(1.65, 1))