Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passed through data? #33

Open
emstruong opened this issue Nov 18, 2024 · 1 comment
Open

Support passed through data? #33

emstruong opened this issue Nov 18, 2024 · 1 comment
Assignees

Comments

@emstruong
Copy link

Hello,

Thank you for the package. I noticed that tidyrules:::tidy.rpart() seems to have trouble when the data is passed through apply as an additional argument as opposed to being a set argument. Is there a workaround? Sometimes I'd like to iterate through the data...

library(rpart)
library(tidyrules)
#> 
#> Attaching package: 'tidyrules'
#> The following object is masked from 'package:rpart':
#> 
#>     prune

x1 <- lapply(list(as.formula("Kyphosis ~ Age + Number + Start")), \(x) rpart(formula = x, data = kyphosis))
tidy(x1[[1]])
#> ---- Rulelist --------------------------------
#> ▶ Keys: NULL
#> ▶ Number of rules: 5
#> ▶ Model type: rpart
#> ▶ Estimation type: classification
#> ▶ Is validation data set: FALSE
#> 
#> 
#>   rule_nbr LHS                                    RHS   support confidence  lift
#>      <int> <chr>                                  <fct>   <int>      <dbl> <dbl>
#> 1        1 ( Start >= 8.5 ) & ( Start >= 14.5 )   abse…      29      0.968  1.22
#> 2        2 ( Start >= 8.5 ) & ( Start < 14.5 ) &… abse…      12      0.929  1.18
#> 3        3 ( Start >= 8.5 ) & ( Start < 14.5 ) &… abse…      14      0.812  1.03
#> 4        4 ( Start < 8.5 )                        pres…      19      0.571  2.72
#> 5        5 ( Start >= 8.5 ) & ( Start < 14.5 ) &… pres…       7      0.556  2.65
#> ----------------------------------------------


x2 <- lapply(list(as.formula("Kyphosis ~ Age + Number + Start")), rpart, data = kyphosis)
tidy(x2[[1]])
#> Error in is.data.frame(data): ..1 used in an incorrect context, no ... to look in

Created on 2024-11-18 with reprex v2.1.1

@talegari talegari self-assigned this Jan 31, 2025
@talegari
Copy link
Owner

talegari commented Feb 4, 2025

@emstruong Thanks for raising the issue.
The issue seems to be with as.party method from partykit.

library(rpart)
library(tidyrules)
#> 
#> Attaching package: 'tidyrules'
#> The following object is masked from 'package:rpart':
#> 
#>     prune
x1 <- lapply(list(as.formula("Kyphosis ~ Age + Number + Start")), \(x) rpart(formula = x, data = kyphosis))
tidy(x1[[1]])
#> ---- Rulelist --------------------------------
#> ▶ Keys: NULL
#> ▶ Number of rules: 5
#> ▶ Model type: rpart
#> ▶ Estimation type: classification
#> ▶ Is validation data set: FALSE
#> 
#> 
#>   rule_nbr LHS                                    RHS   support confidence  lift
#>      <int> <chr>                                  <fct>   <int>      <dbl> <dbl>
#> 1        1 ( Start >= 8.5 ) & ( Start >= 14.5 )   abse…      29      0.968  1.22
#> 2        2 ( Start >= 8.5 ) & ( Start < 14.5 ) &… abse…      12      0.929  1.18
#> 3        3 ( Start >= 8.5 ) & ( Start < 14.5 ) &… abse…      14      0.812  1.03
#> 4        4 ( Start < 8.5 )                        pres…      19      0.571  2.72
#> 5        5 ( Start >= 8.5 ) & ( Start < 14.5 ) &… pres…       7      0.556  2.65
#> ----------------------------------------------
x2 <- lapply(list(as.formula("Kyphosis ~ Age + Number + Start")),
             rpart,
             data = kyphosis
             )

# this is know to fail
tidy(x2[[1]])
#> Error in is.data.frame(data): ..1 used in an incorrect context, no ... to look in

# error is originating from `partykit::as.party` call
partykit::as.party(x2[[1]])
#> Error in is.data.frame(data): ..1 used in an incorrect context, no ... to look in

We are using partykit to extract rules. For now, I will raise a bugreport in partykit. For long term, let me think if avoiding partykit makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants