Skip to content

Commit 11df8e8

Browse files
committed
allowed changing correlation method in triplot and group_variables
1 parent 8f2f342 commit 11df8e8

9 files changed

+54
-12
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: triplot
22
Title: Explaining Correlated Features in Machine Learning Models
3-
Version: 1.2.0
3+
Version: 1.3.0
44
Authors@R:
55
c(person("Katarzyna", "Pekala", email = "[email protected]",
66
role = c("aut", "cre")),
@@ -18,10 +18,10 @@ Depends: R (>= 3.6)
1818
License: GPL-3
1919
Encoding: UTF-8
2020
LazyData: true
21-
RoxygenNote: 7.1.0
21+
RoxygenNote: 7.1.1
2222
Imports:
2323
ggplot2,
24-
DALEX (>= 1.2),
24+
DALEX (>= 1.3),
2525
glmnet,
2626
ggdendro,
2727
patchwork

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# triplot 1.3
2+
3+
* changed the way plot(model_triplot) is build, so it works correctly with
4+
patchwork 1.0.1
5+
* changed parameter in DALEX::feature_importance call from n_sample to N (due
6+
to change in DALEX 1.2.1)
7+
* added parameter to change correlation method in group_variables,
8+
cluster_variables, triplot
9+
10+
111
# triplot 1.2
212

313
* last vertical line in hierarchical_importance() plot of model_triplot() shows

R/group_variables.R

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#' @param x dataframe with only numeric columns
66
#' @param clust_method the agglomeration method to be used
77
#' see \code{\link[stats]{hclust}} methods
8+
#' @param cor_method the correlation method to be used
9+
#' see \code{\link[stats]{cor}} methods
810
#'
911
#' @return an hclust object
1012
#'
@@ -26,13 +28,14 @@ cluster_variables <- function(x, ...)
2628
#' @export
2729
#' @rdname cluster_variables
2830

29-
cluster_variables.default <- function(x, clust_method = "complete", ...) {
31+
cluster_variables.default <- function(x, clust_method = "complete",
32+
cor_method = "spearman", ...) {
3033

3134
stopifnot(all(sapply(x, is.numeric)))
3235

3336
# build clustering tree ---------------------------------------------------
3437

35-
x_hc <- hclust(as.dist(1 - abs(cor(x, method = "spearman"))),
38+
x_hc <- hclust(as.dist(1 - abs(cor(x, method = cor_method))),
3639
method = clust_method)
3740

3841
class(x_hc) <- c("cluster_variables", "hclust")
@@ -186,6 +189,8 @@ list_variables <- function(x, h) {
186189
#' @param h correlation value for tree cutting
187190
#' @param clust_method the agglomeration method to be used
188191
#' see \code{\link[stats]{hclust}} methods
192+
#' @param cor_method the correlation method to be used
193+
#' see \code{\link[stats]{cor}} methods
189194
#'
190195
#' @examples
191196
#' library("DALEX")
@@ -196,11 +201,13 @@ list_variables <- function(x, h) {
196201
#' @export
197202

198203

199-
group_variables <- function(x, h, clust_method = "complete") {
204+
group_variables <- function(x, h, clust_method = "complete",
205+
cor_method = "spearman") {
200206

201207
# make a tree and prepare a list with aspects grouping --------------------
202208

203-
cv <- cluster_variables(x, clust_method = clust_method)
209+
cv <- cluster_variables(x, clust_method = clust_method,
210+
cor_method = cor_method)
204211
res <- list_variables(cv, h)
205212

206213
return(res)

R/hierarchical_importance.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#' "ratio" returns \code{drop_loss/drop_loss_full_model}.
2424
#' @param clust_method the agglomeration method to be used, see
2525
#' \code{\link[stats]{hclust}} methods
26+
#' @param cor_method the correlation method to be used see
27+
#' \code{\link[stats]{cor}} methods
2628
#' @param absolute_value if TRUE, aspects importance values will be drawn as
2729
#' absolute values
2830
#' @param show_labels if TRUE, plot will have annotated axis Y
@@ -66,6 +68,7 @@ hierarchical_importance <- function(x, data, y = NULL,
6668
B = 10,
6769
fi_type = c("raw", "ratio", "difference"),
6870
clust_method = "complete",
71+
cor_method = "spearman",
6972
...) {
7073

7174
if (all(type != "predict", is.null(y))) {
@@ -77,7 +80,7 @@ hierarchical_importance <- function(x, data, y = NULL,
7780

7881
# Building helper objects ---------------------------------------------
7982

80-
x_hc <- hclust(as.dist(1 - abs(cor(data, method = "spearman"))),
83+
x_hc <- hclust(as.dist(1 - abs(cor(data, method = cor_method))),
8184
method = clust_method)
8285
cutting_heights <- x_hc$height
8386
aspects_list_previous <- list_variables(x_hc, 1)

R/triplot.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#' "ratio" returns \code{drop_loss/drop_loss_full_model}.
3131
#' @param clust_method the agglomeration method to be used, see
3232
#' \code{\link[stats]{hclust}} methods
33+
#' @param cor_method the correlation method to be used see
34+
#' \code{\link[stats]{cor}} methods
3335
#' @param ... other parameters
3436
#'
3537
#' @import stats
@@ -73,6 +75,7 @@ calculate_triplot.explainer <- function(x,
7375
fi_type =
7476
c("raw", "ratio", "difference"),
7577
clust_method = "complete",
78+
cor_method = "spearman",
7679
...) {
7780

7881
type <- match.arg(type)
@@ -109,6 +112,7 @@ calculate_triplot.explainer <- function(x,
109112
B = B,
110113
fi_type = fi_type,
111114
clust_method = clust_method,
115+
cor_method = cor_method,
112116
label = label)
113117
}
114118

@@ -126,6 +130,7 @@ calculate_triplot.default <- function(x, data, y = NULL,
126130
B = 10,
127131
fi_type = c("raw", "ratio", "difference"),
128132
clust_method = "complete",
133+
cor_method = "spearman",
129134
...) {
130135

131136
type <- match.arg(type)
@@ -142,11 +147,13 @@ calculate_triplot.default <- function(x, data, y = NULL,
142147
loss_function = loss_function,
143148
B = B,
144149
fi_type = fi_type,
145-
clust_method = clust_method)
150+
clust_method = clust_method,
151+
cor_method = cor_method)
146152

147153
# Calculations for third plot -----------------------------------------------
148154

149-
cv <- cluster_variables(data, clust_method)
155+
cv <- cluster_variables(data, clust_method = clust_method,
156+
cor_method = cor_method)
150157

151158
# Calculations for first plot -----------------------------------------------
152159

man/calculate_triplot.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/cluster_variables.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/group_variables.Rd

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/hierarchical_importance.Rd

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)