-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.R
227 lines (179 loc) · 6.71 KB
/
plot.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
#' Plotting methods
#'
#' Plotting methods for outputs of \code{bnmonitor} functions
#'
#' @param x an appropriate object
#' @param x The output of node_monitor.
#' @param which select the monitor to plot, either "marginal" or "conditional" (for output of \code{node_monitor} only).
#' @param ... for compatibility
#' @name plot
#' @return A plot specific to the object it is applied to.
NULL
#'@importFrom ggplot2 ggplot xlab ylab theme_minimal ggtitle geom_hline
#'
#' @method plot seq_marg_monitor
#'@export
#' @rdname plot
#'
plot.seq_marg_monitor <- function(x,...){
temp <- data.frame(x= 1:length(x$Seq_Marg_Monitor), y=x$Seq_Marg_Monitor[1:length(x$Seq_Marg_Monitor)])
p <- suppressWarnings(ggplot(temp, aes(temp[,1],temp[,2])) + geom_point() + xlab('Index') + ylab('Standardized Z Statistic') + theme_minimal() + ggtitle(paste0("Marginal Node Monitor for ", x$node.name)) + geom_hline(yintercept=1.96, linetype="dashed", color = "red") + geom_hline(yintercept=-1.96, linetype="dashed", color = "red"))
return(p)
}
#' @rdname plot
#'@export
#'
#'@method plot CD
plot.CD <- function(x,...){
x$plot
}
#'
#'@importFrom ggplot2 ggplot xlab ylab theme_minimal ggtitle geom_hline
#'@rdname plot
#'
#' @method plot seq_cond_monitor
#'@export
#'
#'
plot.seq_cond_monitor <- function(x,...){
temp <- data.frame(x= 1:length(x$Seq_Cond_Monitor), y=x$Seq_Cond_Monitor[1:length(x$Seq_Cond_Monitor)])
p <- suppressWarnings(ggplot(temp, aes(temp[,1],temp[,2])) + geom_point() + xlab('Index') + ylab('Standardized Z Statistic') + theme_minimal() + ggtitle(paste0("Conditional Node Monitor for ", x$node.name)) + geom_hline(yintercept=1.96, linetype="dashed", color = "red") + geom_hline(yintercept=-1.96, linetype="dashed", color = "red"))
return(p)
}
#' @importClassesFrom bnlearn bn.fit
#' @importFrom graphics plot.new
#' @importFrom bnlearn arcs
#'@importFrom RColorBrewer brewer.pal
#'@importFrom grDevices colorRampPalette
#'@importFrom qgraph qgraph
#' @method plot node_monitor
#'@export
#'@rdname plot
#'
plot.node_monitor <- function(x, ...){
nb.cols <- length(names(x$DAG$nodes))
my.colors <- colorRampPalette(brewer.pal(8, "Blues"))(nb.cols)
max.val <- ceiling(max(abs(x$Global_Monitor$Score)))
my.palette <- colorRampPalette(my.colors)(max.val)
node.colors <- my.palette[floor(abs(x$Global_Monitor$Score))]
qgraph(x$DAG, color = node.colors, ...)
# nodes <- create_node_df(n=length(x$DAG$nodes),
# type= names(x$DAG$nodes),
# label=names(x$DAG$nodes),
# style="filled",
# fontcolor="black",
# fillcolor=node.colors, .name_repair = "unique")
# from.nodes <- arcs(x$DAG)[,1]
# to.nodes <- arcs(x$DAG)[,2]
# edges <- create_edge_df(from=match(from.nodes,names(x$DAG$nodes)),
# to=match(to.nodes,names(x$DAG$nodes)))
# p <- suppressWarnings(create_graph(
# nodes_df = nodes,
# edges_df = edges)
# %>% render_graph( ... )
# )
#return(p)
}
#'
#' @method plot influential_obs
#'@export
#'@rdname plot
#'
#' @importFrom ggplot2 xlab ylab theme_minimal
plot.influential_obs <- function(x,...){
index <- 1:length(x$score)
value <- x$score
data <- data.frame(index=index, value = value)
p <- suppressWarnings(ggplot(data, aes(index, value))+ geom_point() + xlab('Index') + ylab('Leave-One-Out Score') + theme_minimal())
return(p)
}
#' @rdname plot
#'@export
#'
#'@method plot jeffreys
#'
plot.jeffreys <- function(x,...){
x$plot
}
#'@export
#' @rdname plot
#'@method plot kl
#'
plot.kl <- function(x,...){
x$plot
}
#'@importFrom RColorBrewer brewer.pal
#'@importFrom graphics plot.new
#'@importFrom grDevices colorRampPalette
#'@importFrom bnlearn arcs
#'@importFrom qgraph qgraph
#'@method plot final_node_monitor
#'@rdname plot
#'@export
plot.final_node_monitor <- function(x, which, ...){
if(which!="marginal" & which!="conditional")stop("wrong input for which")
from.nodes <- arcs(x$DAG)[,1]
to.nodes <- arcs(x$DAG)[,2]
#edges <- create_edge_df(from=match(from.nodes,x$Node_Monitor$node),
# to=match(to.nodes,x$Node_Monitor$node))
l <- length(names(x$DAG$nodes))
my.colors <- colorRampPalette(brewer.pal(8, "Greens"))(l)
max.val <- ceiling(max(abs(x$Node_Monitor$marg.z.score[is.finite(x$Node_Monitor$marg.z.score)])))
max.val.cond <- ceiling(max(abs(x$Node_Monitor$cond.z.score[is.finite(x$Node_Monitor$cond.z.score)])))
my.palette <- colorRampPalette(my.colors)(max.val)
my.palette.cond <- colorRampPalette(my.colors)(max.val.cond)
node.colors <- my.palette[floor(abs(x$Node_Monitor$marg.z.score))+1]
node.colors.cond <- my.palette.cond[floor(abs(x$Node_Monitor$cond.z.score))+1]
#nodes <- create_node_df(n=length(x$Node_Monitor$node),
# type= x$Node_Monitor$node,
# label=x$Node_Monitor$node,
# nodes = x$Node_Monitor$node,
# style="filled",
# fontcolor="black",
# fillcolor=node.colors)
#nodes.cond <- create_node_df(n=length(x$Node_Monitor$node),
# type= x$Node_Monitor$node,
# label=x$Node_Monitor$node,
# nodes = x$Node_Monitor$node,
# style="filled",
# fontcolor="black",
# fillcolor=node.colors.cond)
#graph <- create_graph(
# nodes_df = nodes,
# edges_df = edges)
#plot <- suppressWarnings(render_graph(graph, title="Marginal Node Monitors", ...))
#graph.cond <- create_graph(
# nodes_df = nodes.cond,
# edges_df = edges)
#plot.cond <- suppressWarnings(render_graph(graph.cond, title="Conditional Node Monitors", ...))
if(which == "marginal"){qgraph(x$DAG, color = node.colors, ...)
}
else if(which=="conditional"){ qgraph(x$DAG, color = node.colors.cond, ...)
}
}
#' @importFrom ggplot2 ggtitle xlab ylab theme_minimal theme scale_colour_discrete geom_hline
#' @method plot seq_pa_ch_monitor
#'@export
#'@rdname plot
#' @importFrom ggplot2 xlab ylab theme_minimal
plot.seq_pa_ch_monitor <- function(x,...){
index <- 1:length(x)
value <- x[1:length(x)]
data <- data.frame(index=index, value = value)
p <- suppressWarnings(ggplot(data, aes(index, value))+ geom_point() + xlab('Relevant sample size') + ylab('Standardized Z Statistic') + theme_minimal() + geom_hline(yintercept=1.96, linetype="dashed", color = "red") + geom_hline(yintercept=-1.96, linetype="dashed", color = "red"))
return(p)
}
#'@export
#'@rdname plot
#'@method plot sensitivity
#'
plot.sensitivity <- function(x,...){
x$plot
}
#'@export
#'@rdname plot
#'@method plot fro
#'
plot.fro <- function(x,...){
x$plot
}