Skip to content

Commit

Permalink
updates from CRAN comments (#51)
Browse files Browse the repository at this point in the history
* updates from CRAN comments

* update GHA
  • Loading branch information
topepo authored Jul 2, 2024
1 parent cb293c6 commit b33dd02
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
^\.Rproj\.user$
^codecov\.yml$
^\.github$
^doc$
^Meta$
2 changes: 0 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'}

Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ revdep/data.sqlite
.Rproj.user
.Rhistory
docs
src/Makevars
src/Makevars
/doc/
/Meta/
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Suggests:
modeldata,
rlang,
rmarkdown,
testthat (>= 3.0.0),
tidyrules
rules,
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Biarch: true
Expand All @@ -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
65 changes: 30 additions & 35 deletions vignettes/cubist.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ knitr::opts_chunk$set(echo = TRUE)
library(Cubist)
library(dplyr)
library(rlang)
library(tidyrules)
library(rules)
options(digits = 3)
```

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -125,55 +123,52 @@ 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:

```{r summary-tree}
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).
Expand Down

0 comments on commit b33dd02

Please sign in to comment.