Skip to content

Commit 5539847

Browse files
committed
CRAN v0.4.0 submission, removed basis_type
1 parent ed16fe0 commit 5539847

File tree

7 files changed

+107
-136
lines changed

7 files changed

+107
-136
lines changed

CRAN-SUBMISSION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
Version: 0.3.0
2-
Date: 2023-05-07 19:09:58 UTC
3-
SHA: 05fe2cc809e6039689541ac5c49bc0d9e164cb1e
1+
Version: 0.4.0.0
2+
Date: 2023-11-08 19:12:12 UTC
3+
SHA:
4+
ed16fe0592acf196ce64bd22eb8b35b47ec3db1f

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Shiny app text, plot dimensions, and text cleaned up.
77
- Classification tour now uses a horizontal layout.
88
- Cleaned up the text on the facet panels for `global_tour()` and `radial_cheem_tour()`.
9+
- Removed support for the `basis_type` argument. support for alternative bases types is really an extension of the analysis.
910
- Recreate the saved classification model, they fit too well to work as illustrations.
1011
- Set seed more consistently. All model and attribution shifted a bit, but will be more replicable going forward.
1112
- Minor documentation and code clean up and clarifications.

R/1_cheem_lists.r

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ model_performance <- function(
6363
#' Produces the plot data.frame of 1 layer. consumed downstream in cheem_ls.
6464
#'
6565
#' @param x The explanatory variables of the model.
66-
#' @param basis_type The type of basis used to approximate the data and
67-
#' attribution space from. Defaults to "pca".
68-
#' Expects "pca" or "olda" (requires `class`).
6966
#' @param class Optional, (n, 1) vector, a variable to group points by.
7067
#' This can be the same as or different from `y`, the target variable.
7168
#' @param label Optionally provide a character label to store reminder
@@ -83,27 +80,21 @@ model_performance <- function(
8380
# #' cheem:::global_view_df_1layer(X)
8481
global_view_df_1layer <- function(
8582
x,
86-
class = NULL, ## required for olda
87-
basis_type = c("pca", "olda"),
88-
label = "label"
83+
class = NULL,
84+
label = "label"
8985
){
9086
d <- 2 ## Fixed display dimensionality
91-
basis_type <- match.arg(basis_type)
9287
if(is.null(class)) class <- as.factor(FALSE)
9388

9489
## Projection
9590
x_std <- spinifex::scale_01(x)
96-
if(basis_type == "olda" & is.null(class))
97-
stop("global_view_df_1layer: Basis type was olda without a class, a class must be provided for olda.")
98-
basis <- switch(basis_type,
99-
pca = stats::prcomp(x_std)$rotation[, 1:d],
100-
olda = spinifex::basis_olda(x_std, class, d))
91+
basis <- stats::prcomp(x_std)$rotation[, 1:d]
10192
proj <- spinifex::scale_01(x_std %*% basis)
10293

10394
## Column bind wider
104-
ret <- data.frame(basis_type, label, 1:nrow(x), class, proj)
105-
colnames(ret) <- c("basis_type", "label", "rownum", "class", paste0("V", 1:d))
106-
attr(ret, paste0(basis_type, ":", label)) <- basis
95+
ret <- data.frame(label, 1:nrow(x), class, proj)
96+
colnames(ret) <- c("label", "rownum", "class", paste0("V", 1:d))
97+
attr(ret, label) <- basis
10798

10899
## Return
109100
ret
@@ -126,9 +117,6 @@ global_view_df_1layer <- function(
126117
#' @param label Optionally provide a character label to store reminder
127118
#' text for the type of model and local explanation used.
128119
#' Defaults to "label".
129-
#' @param basis_type The type of basis used to approximate the data and
130-
#' attribution space from. Defaults to "pca".
131-
#' Expects "pca" or "olda" (requires `class`).
132120
#' @param verbose Logical, if start time and run duration should be printed.
133121
#' Defaults to getOption("verbose").
134122
#' @return A list of data.frames needed for the `shiny` application.
@@ -212,15 +200,13 @@ cheem_ls <- function(
212200
attr_df,
213201
pred = NULL,
214202
class = NULL,
215-
basis_type = c("pca", "olda"), ## class req for olda
216203
label = "label",
217204
verbose = getOption("verbose")
218205
){
219206
rownum <- V2 <- projection_nm <- NULL
220207
## Checks
221208
if(verbose) tictoc::tic("cheem_ls")
222209
d <- 2 ## Hard coded display dimensionality
223-
basis_type <- match.arg(basis_type)
224210
is_classification <- is_discrete(y)
225211
x <- data.frame(x)
226212
y <- as.numeric(y)
@@ -240,12 +226,12 @@ cheem_ls <- function(
240226
.pca_var <- stats::prcomp(spinifex::scale_01(x))$sdev^2
241227
.var_exp <- round(100*.pca_var/sum(.pca_var), 0)
242228
.glob_dat <- global_view_df_1layer(
243-
x, class, basis_type,
229+
x, class,
244230
paste0("data, PC1 (", .var_exp[1], "%) by PC2 (", .var_exp[2], "%)"))
245231
.pca_var <- stats::prcomp(spinifex::scale_01(attr_df))$sdev^2
246232
.var_exp <- round(100*.pca_var/sum(.pca_var), 0)
247233
.glob_attr <- global_view_df_1layer(
248-
attr_df, class, basis_type,
234+
attr_df, class,
249235
paste0("attribution, PC1 (", .var_exp[1], "%) by PC2 (", .var_exp[2], "%)"))
250236
.glob_view <- rbind(.glob_dat, .glob_attr)
251237
## List of the bases
@@ -306,7 +292,7 @@ cheem_ls <- function(
306292
data.frame(V1 = .decode_df$prediction + .xjitter,
307293
V2 = as.numeric(.decode_df$y) + .yjitter) %>%
308294
spinifex::scale_01()
309-
.yhaty_df <- data.frame(basis_type = NA, label = .y_axis_label,
295+
.yhaty_df <- data.frame(label = .y_axis_label,
310296
rownum = 1:nrow(x), class = .decode_df$class, .yhaty_df)
311297
.glob_view <- rbind(.glob_view, .yhaty_df)
312298

@@ -337,7 +323,6 @@ cheem_ls <- function(
337323
.glob_view$tooltip <- rep_len(tooltip, .N)
338324
.decode_df$tooltip <- tooltip
339325
## Ensure facet order is kept.
340-
.glob_view$basis_type <- factor(.glob_view$basis_type, unique(.glob_view$basis_type))
341326
.glob_view$label <- factor(.glob_view$label, unique(.glob_view$label))
342327

343328
## Cleanup and return

man/cheem_ls.Rd

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

man/global_view_df_1layer.Rd

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

tests/testthat/test-2_visualization.r

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
1-
## Setup -----
2-
{
3-
library("spinifex")
4-
library("cheem")
5-
library("testthat")
6-
7-
## Classification:
8-
c_X <- penguins_na.rm[, 1:4]
9-
c_Y <- c_clas <- penguins_na.rm$species
10-
## Regression:
11-
r_X <- amesHousing2018_NorthAmes[, 1:9]
12-
r_clas <- amesHousing2018_NorthAmes$SubclassMS
13-
r_Y <- amesHousing2018_NorthAmes$SalePrice
14-
15-
r_pred <- ames_rf_pred
16-
c_pred <- penguin_xgb_pred
17-
r_attr <- ames_rf_shap
18-
c_attr <- penguin_xgb_shap
19-
c_chee <- cheem_ls(c_X, c_Y, c_attr, c_pred, c_clas, "pca", "label", FALSE)
20-
r_chee <- cheem_ls(r_X, r_Y, r_attr, r_pred, r_clas, "pca", "label", FALSE)
21-
}
22-
23-
## basis_attr -----
24-
c_bas_attr <- sug_basis(c_attr, 1)
25-
r_bas_attr <- sug_basis(r_attr, 2)
26-
27-
test_that("basis_attr", {
28-
expect_equal(class(c_bas_attr), c("matrix", "array"))
29-
expect_equal(class(r_bas_attr), c("matrix", "array"))
30-
})
31-
32-
## proto_basis1d_distribution -----
33-
c_ggt <- ggtour(c_bas_attr, scale_sd(c_X), angle = .3) +
34-
proto_basis1d_distribution(
35-
attr_df = c_attr, group_by = c_clas, primary_obs = 1, comparison_obs = 2)
36-
r_ggt <- ggtour(r_bas_attr, scale_sd(r_X), angle = .3) +
37-
proto_basis1d_distribution(
38-
attr_df = r_attr, group_by = r_clas, primary_obs = 1, comparison_obs = 2)
39-
40-
test_that("proto_basis1d_distribution", {
41-
expect_equal(class(c_ggt), c("gg", "ggplot"))
42-
expect_equal(class(r_ggt), c("gg", "ggplot"))
43-
})
44-
45-
## global_view -----
46-
c_gv <- global_view(c_chee)
47-
r_gv <- global_view(r_chee) |> suppressWarnings()
48-
test_that("global_view", {
49-
expect_equal(class(c_gv), c("plotly", "htmlwidget"))
50-
expect_equal(class(r_gv), c("plotly", "htmlwidget"))
51-
})
52-
53-
## global_view as ggplot ----
54-
c_gv <- global_view(c_chee, as_ggplot = TRUE)
55-
r_gv <- global_view(r_chee, as_ggplot = TRUE)
56-
test_that("global_view as_ggplot", {
57-
expect_equal(class(c_gv), c("gg", "ggplot"))
58-
expect_equal(class(r_gv), c("gg", "ggplot"))
59-
})
60-
61-
## radial_cheem_tour -----
62-
c_ggt <- radial_cheem_tour(c_chee, c_bas_attr, 1, 1, 2)
63-
r_ggt <- radial_cheem_tour(r_chee, r_bas_attr, 1, 1, 2)
64-
test_that("radial_cheem_tour", {
65-
expect_equal(class(c_ggt), c("gg", "ggplot"))
66-
expect_equal(class(r_ggt), c("gg", "ggplot"))
67-
})
68-
1+
## Setup -----
2+
{
3+
library("spinifex")
4+
library("cheem")
5+
library("testthat")
6+
7+
## Classification:
8+
c_X <- penguins_na.rm[, 1:4]
9+
c_Y <- c_clas <- penguins_na.rm$species
10+
## Regression:
11+
r_X <- amesHousing2018_NorthAmes[, 1:9]
12+
r_clas <- amesHousing2018_NorthAmes$SubclassMS
13+
r_Y <- amesHousing2018_NorthAmes$SalePrice
14+
15+
r_pred <- ames_rf_pred
16+
c_pred <- penguin_xgb_pred
17+
r_attr <- ames_rf_shap
18+
c_attr <- penguin_xgb_shap
19+
c_chee <- cheem_ls(c_X, c_Y, c_attr, c_pred, c_clas, "label", FALSE)
20+
r_chee <- cheem_ls(r_X, r_Y, r_attr, r_pred, r_clas, "label", FALSE)
21+
}
22+
23+
## basis_attr -----
24+
c_bas_attr <- sug_basis(c_attr, 1)
25+
r_bas_attr <- sug_basis(r_attr, 2)
26+
27+
test_that("basis_attr", {
28+
expect_equal(class(c_bas_attr), c("matrix", "array"))
29+
expect_equal(class(r_bas_attr), c("matrix", "array"))
30+
})
31+
32+
## proto_basis1d_distribution -----
33+
c_ggt <- ggtour(c_bas_attr, scale_sd(c_X), angle = .3) +
34+
proto_basis1d_distribution(
35+
attr_df = c_attr, group_by = c_clas, primary_obs = 1, comparison_obs = 2)
36+
r_ggt <- ggtour(r_bas_attr, scale_sd(r_X), angle = .3) +
37+
proto_basis1d_distribution(
38+
attr_df = r_attr, group_by = r_clas, primary_obs = 1, comparison_obs = 2)
39+
40+
test_that("proto_basis1d_distribution", {
41+
expect_equal(class(c_ggt), c("gg", "ggplot"))
42+
expect_equal(class(r_ggt), c("gg", "ggplot"))
43+
})
44+
45+
## global_view -----
46+
c_gv <- global_view(c_chee)
47+
r_gv <- global_view(r_chee) |> suppressWarnings()
48+
test_that("global_view", {
49+
expect_equal(class(c_gv), c("plotly", "htmlwidget"))
50+
expect_equal(class(r_gv), c("plotly", "htmlwidget"))
51+
})
52+
53+
## global_view as ggplot ----
54+
c_gv <- global_view(c_chee, as_ggplot = TRUE)
55+
r_gv <- global_view(r_chee, as_ggplot = TRUE)
56+
test_that("global_view as_ggplot", {
57+
expect_equal(class(c_gv), c("gg", "ggplot"))
58+
expect_equal(class(r_gv), c("gg", "ggplot"))
59+
})
60+
61+
## radial_cheem_tour -----
62+
c_ggt <- radial_cheem_tour(c_chee, c_bas_attr, 1, 1, 2)
63+
r_ggt <- radial_cheem_tour(r_chee, r_bas_attr, 1, 1, 2)
64+
test_that("radial_cheem_tour", {
65+
expect_equal(class(c_ggt), c("gg", "ggplot"))
66+
expect_equal(class(r_ggt), c("gg", "ggplot"))
67+
})
68+

vignettes/getting-started-with-cheem.Rmd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ ames_chm <- cheem_ls(X, Y,
9090
class = clas,
9191
attr_df = ames_xgb_shap,
9292
pred = ames_xgb_pred,
93-
basis_type = c("pca"),
9493
label = "Ames, xgb, shap")
9594
names(ames_chm)
9695
```
@@ -320,7 +319,6 @@ drag_lm_shap <- as.data.frame(drag_lm_shap)
320319
<!-- peng_umap_chm <- cheem_ls(X, y = NULL, class = NULL, -->
321320
<!-- attr_df = peng_umap2, -->
322321
<!-- pred = NULL, -->
323-
<!-- basis_type = c("pca"), -->
324322
<!-- label = "Penguin, umap2") -->
325323

326324
<!-- global_view(peng_umap_chm, primary_obs = 115, comparison_obs = 296, -->

0 commit comments

Comments
 (0)