-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplot_pred.R
More file actions
118 lines (117 loc) · 3.64 KB
/
plot_pred.R
File metadata and controls
118 lines (117 loc) · 3.64 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
#' Plot the predictor matrix of an imputation model
#'
#' @param data A predictor matrix for `mice`, typically generated with [mice::make.predictorMatrix] or [mice::quickpred].
#' @param vrb String, vector, or unquoted expression with variable name(s), default is "all".
#' @param method Character string or vector with imputation methods.
#' @param label Logical indicating whether predictor matrix values should be displayed.
#' @param square Logical indicating whether the plot tiles should be squares.
#' @param rotate Logical indicating whether the variable name labels should be rotated 90 degrees.
#' @param grid Logical indicating whether borders should be present between tiles.
#'
#' @return An object of class `ggplot2::ggplot`.
#'
#' @examples
#' pred <- mice::quickpred(mice::nhanes)
#' plot_pred(pred)
#' @export
plot_pred <-
function(data,
vrb = "all",
method = NULL,
label = TRUE,
square = TRUE,
rotate = FALSE,
grid = TRUE) {
verify_data(data, pred = TRUE)
p <- nrow(data)
if (!is.null(method) && is.character(method)) {
if (length(method) == 1) {
method <- rep(method, p)
}
if (length(method) == p) {
ylabel <- "Imputation method"
}
}
if (is.null(method)) {
method <- rep("", p)
ylabel <- ""
}
if (!is.character(method) || length(method) != p) {
cli::cli_abort("Method should be NULL or a character string or vector (of length 1 or `ncol(data)`).")
}
vrb <- substitute(vrb)
if (vrb[1] == "all") {
vrb <- names(data)
} else {
vrb <- names(dplyr::select(as.data.frame(data), {{vrb}}))
}
vrbs <- row.names(data)
long <- data.frame(
vrb = 1:p,
prd = rep(vrbs, each = p),
ind = matrix(data, nrow = p * p, byrow = TRUE)
) %>% dplyr::mutate(clr = factor(
.data$ind,
levels = c(-3, -2, 0, 1, 2),
labels = c(
"inclusion-restriction variable",
"cluster variable",
"not used",
"predictor",
"random effect"
),
ordered = TRUE
))
gg <-
ggplot2::ggplot(long,
ggplot2::aes(
x = .data$prd,
y = .data$vrb,
label = .data$ind,
fill = .data$clr
)) +
ggplot2::scale_x_discrete(limits = vrbs, position = "top") +
ggplot2::scale_y_reverse(
breaks = 1:p,
labels = vrbs,
sec.axis = ggplot2::dup_axis(labels = method, name = ylabel)
) +
ggplot2::scale_fill_manual(
values = c(
"inclusion-restriction variable" = "orangered",
"cluster variable" = "lightyellow",
"not used" = "grey90",
"predictor" = "palegreen3",
"random effect" = "deepskyblue"
)
) +
ggplot2::labs(
x = "Imputation model predictor",
y = "Variable to impute",
fill = "",
color = ""
) +
theme_minimice()
if (grid) {
gg <- gg + ggplot2::geom_tile(color = "black", alpha = 0.6)
} else {
gg <- gg + ggplot2::geom_tile(alpha = 0.6)
}
if (all(method == "")) {
gg <-
gg + ggplot2::theme(axis.ticks.y.right = ggplot2::element_blank())
}
if (label) {
gg <- gg + ggplot2::geom_text(color = "black", show.legend = FALSE)
}
if (square) {
gg <- gg + ggplot2::coord_fixed(expand = FALSE)
} else {
gg <- gg + ggplot2::coord_cartesian(expand = FALSE)
}
if (rotate) {
gg <-
gg + ggplot2::theme(axis.text.x.top = ggplot2::element_text(angle = 90))
}
return(gg)
}