From b33dd0206191b2741c5a8060c013acbea5a7d5c1 Mon Sep 17 00:00:00 2001 From: Max Kuhn Date: Mon, 1 Jul 2024 20:16:25 -0400 Subject: [PATCH] updates from CRAN comments (#51) * updates from CRAN comments * update GHA --- .Rbuildignore | 2 + .github/workflows/R-CMD-check.yaml | 2 - .gitignore | 4 +- DESCRIPTION | 6 +-- vignettes/cubist.Rmd | 65 ++++++++++++++---------------- 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/.Rbuildignore b/.Rbuildignore index 9a5fd72..248f73f 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -9,3 +9,5 @@ ^\.Rproj\.user$ ^codecov\.yml$ ^\.github$ +^doc$ +^Meta$ diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index de4caf7..0177b6c 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -26,8 +26,6 @@ jobs: - {os: windows-latest, r: 'devel'} - {os: windows-latest, r: 'release'} - # Use 3.6 to trigger usage of RTools35 - - {os: windows-latest, r: '3.6'} # use 4.1 to check with rtools40's older compiler - {os: windows-latest, r: '4.1'} diff --git a/.gitignore b/.gitignore index 607c321..ff8cd01 100644 --- a/.gitignore +++ b/.gitignore @@ -23,4 +23,6 @@ revdep/data.sqlite .Rproj.user .Rhistory docs -src/Makevars \ No newline at end of file +src/Makevars +/doc/ +/Meta/ diff --git a/DESCRIPTION b/DESCRIPTION index 28858e3..06e8f43 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,8 +31,8 @@ Suggests: modeldata, rlang, rmarkdown, - testthat (>= 3.0.0), - tidyrules + rules, + testthat (>= 3.0.0) VignetteBuilder: knitr Biarch: true @@ -41,4 +41,4 @@ Config/testthat/edition: 3 Encoding: UTF-8 LazyLoad: yes Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 diff --git a/vignettes/cubist.Rmd b/vignettes/cubist.Rmd index 44016b1..be6f579 100644 --- a/vignettes/cubist.Rmd +++ b/vignettes/cubist.Rmd @@ -13,7 +13,7 @@ knitr::opts_chunk$set(echo = TRUE) library(Cubist) library(dplyr) library(rlang) -library(tidyrules) +library(rules) options(digits = 3) ``` @@ -44,8 +44,6 @@ predictors <- "Bsmt_Full_Bath", "Bsmt_Half_Bath", "Full_Bath", "Half_Bath", "TotRms_AbvGrd", "Year_Sold", "Longitude", "Latitude") -ames$Sale_Price <- log10(ames$Sale_Price) - train_pred <- ames[ in_train_set, predictors] test_pred <- ames[-in_train_set, predictors] @@ -125,7 +123,9 @@ R modeling packages such as `caret`, `tidymodels`, and `mlr3` can be used to tun It should be noted that this variable importance measure does not capture the influence of the predictors when using the instance-based correction. -## Tidy rules + +## Extracting Rules + Rules from a Cubist model can be viewed using `summary` as follows: @@ -133,47 +133,42 @@ Rules from a Cubist model can be viewed using `summary` as follows: summary(model_tree) ``` -The `tidyRules` function in the [`tidyrules` package](https://talegari.github.io/tidyrules/) returns rules in a tibble (an extension of data frames) with one row per rule. The tibble provides these information about the rule: `support`, `mean`, `min`, `max`, `error`, `LHS`, `RHS` and `committee`. The values in `LHS` and `RHS` columns are strings which can be parsed as R expressions. These can be pasted inside the parenthesis of `dplyr::filter()` to obtain the rows of the data corresponding to the rule and evaluate the response variable. +The `tidy()` function in the [`rules` package](https://rules.tidymodels.org) returns rules in a tibble (an extension of data frames) with one row per rule. The tibble provides information about the rule and can be used to programatically extra data from the model. For example: + + +```{r} +#| label: rule-extraction +library(rules) + +rule_df <- tidy(model_tree) -```{r tidy_rules_example} -library(tidyrules) +rule_df -tr <- tidyRules(model_tree) -tr -tr[, c("LHS", "RHS")] +rule_df$estimate[[1]] -# lets look at 7th rule -tr$LHS[7] -tr$RHS[7] +rule_df$statistic[[1]] ``` -These results can be used to look at specific parts of the data. For example, the 7th rule predictions are: +The `rule` column can be converted to an R expression that can be used to pull data used by that rule. For example, for the seventh rule: + +```{r} +#| label: rule-data + +# Text +rule_7 <- rule_df$rule[7] + +# Convert to an expression +rule_7 <- rlang::parse_expr(rule_7) +rule_7 + +# Use in a dplyr filter: +nrow(train_pred) -```{r rule-7} library(dplyr) -library(rlang) -char_to_expr <- function(x, index = 1, model = TRUE) { - x <- x %>% dplyr::slice(index) - if (model) { - x <- x %>% dplyr::pull(RHS) %>% rlang::parse_expr() - } else { - x <- x %>% dplyr::pull(LHS) %>% rlang::parse_expr() - } - x -} - -rule_expr <- char_to_expr(tr, 7, model = FALSE) -model_expr <- char_to_expr(tr, 7, model = TRUE) - -# filter the data corresponding to rule 7 -ames %>% - dplyr::filter(eval_tidy(rule_expr, ames)) %>% - dplyr::mutate(sale_price_pred = eval_tidy(model_expr, .)) %>% - dplyr::select(Sale_Price, sale_price_pred) +train_pred %>% filter(!!rule_7) %>% nrow() ``` - ## Variable Importance The `summary()` method for Cubist shows the usage of each variable in either the rule conditions or the (terminal) linear model. In actuality, many more linear models are used in prediction that are shown in the output. Because of this, the variable usage statistics shown at the end of the output of the `summary()` function will probably be inconsistent with the rules also shown in the output. At each split of the tree, Cubist saves a linear model (after feature selection) that is allowed to have terms for each variable used in the current split or any split above it. Quinlan (1992) discusses a smoothing algorithm where each model prediction is a linear combination of the parent and child model along the tree. As such, the final prediction is a function of all the linear models from the initial node to the terminal node. The percentages shown in the Cubist output reflects all the models involved in prediction (as opposed to the terminal models shown in the output).