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

tydypredict fails with randomforest regression #77

Open
rogerjdeangelis opened this issue Jul 10, 2020 · 2 comments
Open

tydypredict fails with randomforest regression #77

rogerjdeangelis opened this issue Jul 10, 2020 · 2 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@rogerjdeangelis
Copy link

rogerjdeangelis commented Jul 10, 2020

library(randomForest);                                                                                                                          
library(tidypredict);                                                                                                                           
library(dbplyr);                                                                                                                                
library(dplyr);                                                                                                                                 
head(iris[,1:4]);                                                                                                                               
model <- randomForest(iris$Sepal.Length~ ., data = iris[,2:4], ntree = 1);                                                                      
tidypredict_sql(model,dbplyr::simulate_mssql());                                                                                                

Partial listing

[[1]]                                                                                                                                           
<SQL> CASE                                                                                                                                      
WHEN (`Petal.Length` >= 5.7 AND `Sepal.Width` < 2.75 AND `Petal.Length` >= 4.6) THEN (NULL)                                                     
WHEN (`Petal.Width` < 0.65 AND `Sepal.Width` < 2.55 AND `Sepal.Width` < 3.05 AND `Petal.Length` < 4.6) THEN (NULL)                              
WHEN (`Petal.Length` >= 3.2 AND `Sepal.Width` < 3.9 AND `Sepal.Width` >= 3.05 AND `Petal.Length` < 4.6) THEN (NULL)                             
WHEN (`Petal.Width` < 0.3 AND `Sepal.Width` >= 3.9 AND `Sepal.Width` >= 3.05 AND `Petal.Length` < 4.6) THEN (NULL)                              
WHEN (`Petal.Width` >= 0.3 AND `Sepal.Width` >= 3.9 AND `Sepal.Width` >= 3.05 AND `Petal.Length` < 4.6) THEN (NULL)                             
WHEN (`Petal.Length` < 5.2 AND `Petal.Length` < 5.7 AND `Sepal.Width` < 2.75 AND `Petal.Length` >= 4.6) THEN (NULL)                             
WHEN (`Petal.Length` >= 5.2 AND `Petal.Length` < 5.7 AND `Sepal.Width` < 2.75 AND `Petal.Length` >= 4.6) THEN (NULL)                            
WHEN (`Petal.Length` < 3.65 AND `Petal.Width` >= 0.65 AND `Sepal.Width` < 2.55 AND `Sepal.Width` < 3.05 AND `Petal.Length` < 4.6) THEN (NULL)   
WHEN (`Sepal.Width` < 2.65 AND `Sepal.Width` < 2.85 AND `Sepal.Width` >= 2.55 AND `Sepal.Width` < 3.05 AND `Petal.Length` < 4.6) THEN (NULL)    
WHEN (`Petal.Length` < 2.75 AND `Sepal.Width` >= 2.85 AND `Sepal.Width` >= 2.55 AND `Sepal.Width` < 3.05 AND `Petal.Length` < 4.6) THEN (NULL)  
WHEN (`Sepal.Width` < 2.9 AND `Petal.Width` < 1.75 AND `Petal.Width` < 1.85 AND `Sepal.Width` >= 2.75 AND `Petal.Length` >= 4.6) THEN (NULL)    
WHEN (`Petal.Length` >= 5.8 AND `Petal.Width` >= 1.75 AND `Petal.Width` < 1.85 AND `Sepal.Width` >= 2.75 AND `Petal.Length` >= 4.6) THEN (NULL) 
WHEN (`Petal.Length` >= 5.85 AND `Petal.Length` < 6.0 AND `Petal.Width` >= 1.85 AND `Sepal.Width` >= 2.75 AND `Petal.Length` >= 4.6) THEN (NULL)
WHEN (`Sepal.Width` < 3.4 AND `Petal.Length` >= 6.0 AND `Petal.Width` >= 1.85 AND `Sepal.Width` >= 2.75 AND `Petal.Length` >= 4.6) THEN (NULL)  
WHEN (`Sepal.Width` >= 3.4 AND `Petal.Length` >= 6.0 AND `Petal.Width` >= 1.85 AND `Sepal.Width` >= 2.75 AND `Petal.Length` >= 4.6) THEN (NULL) 
@rogerjdeangelis
Copy link
Author

Hi tidypredict team

I realize the code for just one tree is low priority, but I like to look at the code and the tree diagram to get insight into how randomforest is slicing up the data.

I am a SAS programmer but find myself leaning on tidy and haven packages more and more as time goes on.

Thanks for providing these packages!

Roger

@topepo topepo added the bug an unexpected problem or unintended behavior label Dec 4, 2020
@edgararuiz
Copy link
Collaborator

Hi @rogerjdeangelis, what is the issue that you are seeing? I'm not getting any errors.

If what you want to see is the structure of the tree, you could use parse_model() to get an object that reads the Random Forest model, and breaks it down into a somewhat readable list, here is an example:

library(randomForest)        
library(tidypredict)                                                                                                                           

model <- randomForest(iris$Sepal.Length~ ., data = iris[,2:4], ntree = 1)

parsedmodel  <- parse_model(model)

str(parsedmodel$trees)
#> List of 1
#>  $ :List of 48
#>   ..$ :List of 2
#>   .. ..$ prediction: NULL
#>   .. ..$ path      :List of 3
#>   .. .. ..$ :List of 4
#>   .. .. .. ..$ type: chr "conditional"
#>   .. .. .. ..$ col : chr "Petal.Width"
#>   .. .. .. ..$ val : num 1.15
#>   .. .. .. ..$ op  : chr "less"
#>   .. .. ..$ :List of 4
#>   .. .. .. ..$ type: chr "conditional"
#>   .. .. .. ..$ col : chr "Petal.Length"
#>   .. .. .. ..$ val : num 3.4
#>   .. .. .. ..$ op  : chr "more-equal"
#>   .. .. ..$ :List of 4
.... more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

3 participants