-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path7-interpret.Rmd
182 lines (129 loc) · 4.55 KB
/
7-interpret.Rmd
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
---
title: "Interpretation"
output:
html_document:
toc: true
toc_float: true
---
# Interpretation
```{r setup_7, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# This file provides the startup() function.
source("R/_startup.R")
# Load desired packages and report any missing packages that should be installed.
startup(auto_install = FALSE, verbose = FALSE)
# Load any additional R files in the R/ directory.
ck37r::load_all_code("R", verbose = TRUE)
```
## Load data {-}
```{r load_data_7}
# Created in 3-clean-finalize.Rmd
# Objects included: data, vars, var_df
# renv also includes a load() method, so we specify base:: here.
base::load("data/clean-finalize-imputed.RData")
```
## Variable importance
### Random forest
```{r varimp_rf}
# Created in 5-modeling.Rmd
base::load("data/model-rf.RData")
rf_imp = ranger::importance(rf)
# Sort by descending importance.
rf_imp = rf_imp[order(rf_imp, decreasing = TRUE)]
# Review top 10 real quick.
round(rf_imp, 4)[1:10]
print_imp = as.data.frame(rf_imp[, drop = FALSE])
print_imp
# Mean decrease in accuracy.
colnames(print_imp) = c("mean_dec_acc")
print_imp$var = rownames(print_imp)
print_imp2 = print_imp
# Add ranking to the rownames.
print_imp2$var = paste0(1:nrow(print_imp2), ". ", print_imp2$var)
print_imp2
colnames(print_imp2)[2] = "Variable"
# Reverse ordering of columns.
print_imp2 = print_imp2[, c(2, 1)]
rownames(print_imp2) = NULL
#
#print_imp2 = print_imp2[, c("Variable", "Mean Decrease Accuracy (%)")]#, "Description")]
#print_imp2 = print_imp2[, c("Variable", "\thead{Mean\\{}Decrease\\{}Accuracy (%)}")]#, "Description")]
print_imp2
# Convert to a percentage.
print_imp2[, 2] = print_imp2[, 2] * 100
# TODO: fix variable names and restrict to ~15 variables.
# Manually escape variable names.
print_imp2$Variable = gsub("_", "\\_", print_imp2$Variable, fixed = TRUE)
print_imp2
colnames(print_imp2)[1] = "\\thead{Variable}"
colnames(print_imp2)[2] = "\\thead{Mean\\\\{}Decrease\\\\{}Accuracy (\\%)}"
# Top 20.
(kab_table =
kable(print_imp2[1:20, ],
format = "latex",
digits = c(0, 3),
booktabs = TRUE,
escape = FALSE,
row.names = FALSE))
cat(kab_table %>% kable_styling(latex_options = "striped"),
file = "tables/vim-rf-top20.tex")
```
## Accumulated local effect plots
```{r ale_plot}
task = makeClassifTask(data = data[, c(vars$predictors, vars$outcomes[1])],
target = vars$outcomes[1])
learner = makeLearner("classif.ranger",
predict.type = "prob",
# TODO: confirm best mtry.
mtry = 4,
num.trees = 200L,
num.threads = get_cores())
# This takes 1 second
system.time({
mod.mlr = mlr::train(learner, task)
})
mod.mlr$learner.model
mod2 = Predictor$new(mod.mlr, data = data[, !names(data) %in% vars$outcomes[1]],
y = data[[vars$outcomes[1]]])
# Default plot
#effect$plot()
# Calculate multiple ALE effects at a time.
system.time({
effect2 = iml::FeatureEffects$new(mod2,
grid.size = 20L,
features = c("oldpeak", "trestbps"))
})
# Default plot.
effect2$effects[["oldpeak"]]$plot()
# Improved plot
# TODO: move into ck37r
plot_ale = function(var, var_display) {
if (FALSE) {
effect = iml::FeatureEffect$new(mod2,
grid.size = 32L,
feature = var)
old_results = effect$results
effect$results = old_results[old_results$.class == 1, ]
}
old_results = effect2$effects[[var]]$results
effect2$effects[[var]]$results = old_results[old_results$.class == 1, ]
(p = effect2$effects[[var]]$plot(rug = FALSE) +
theme_minimal() + theme(strip.text.x = element_blank()) +
labs(x = var_display))
# Density plot
(p2 = ggplot(data = data, aes_string(x = var)) +
geom_density(fill = "gray70", color = "gray40") +
theme_minimal() + labs(y = "Sample\nDensity") +
#scale_x_log10(breaks = breaks, limits = limits) +
theme(axis.title.x = element_blank(),
axis.title.y = element_text(size = 8),
# Include x-axis major gridlines to ensure that plots are aligned.
panel.grid.major.y = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank()))
print(cowplot::plot_grid(p, p2, align = "v", ncol = 1, rel_heights = c(0.9, 0.1)))
ggsave(paste0("visuals/ale-", var, ".pdf"), width = 4, height = 4)
}
plot_ale("oldpeak", "Old peak")
```