topepo / caret

caret (Classification And Regression Training) R package that contains misc functions for training and plotting classification and regression models
http://topepo.github.io/caret/index.html
1.61k stars 636 forks source link

Plot C5.0 Tree from caret::train #855

Closed BU-1 closed 6 years ago

BU-1 commented 6 years ago

I am unable to plot C5.0 tree models that are generated by the caret::train

library(C50)
library(caret)

Grid = expand.grid(trials = c(1), model = c("tree"),winnow = c(FALSE))
Model = train(Species ~ .,data = iris, method = "C5.0",tuneGrid = Grid)
plot(Model$finalModel)

I receive the following error:

Error in terms.formula(formula, data = data) : 
  argument is not a valid model
topepo commented 6 years ago

Can you send the results of sessionInfo()?

BU-1 commented 6 years ago
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
 [1] parallel  splines   grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2                    partykit_1.1-1                  class_7.3-14                    precrec_0.9.1                  
 [5] pamr_1.55                       cluster_2.0.4                   DMwR_0.4.1                      PRROC_1.3                      
 [9] MLmetrics_1.1.1                 pROC_1.10.0                     DiagrammeR_0.9.2                kknn_1.3.1                     
[13] arm_1.9-3                       lme4_1.1-15                     Matrix_1.2-6                    elmNN_1.0                      
[17] randomGLM_1.02-1                deepboost_0.1.6                 adabag_4.2                      doParallel_1.0.11              
[21] iterators_1.0.9                 foreach_1.4.4                   ada_2.0-5                       nnet_7.3-12                    
[25] xgboost_0.6.4.1                 gbm_2.1.3                       survival_2.41-3                 kernlab_0.9-25                 
[29] caretEnsemble_2.0.0             readxl_1.0.0                    plotly_4.7.1                    gridExtra_2.3                  
[33] tidyr_0.8.0                     tibble_1.4.2                    dplyr_0.7.4                     plyr_1.8.4                     
[37] klaR_0.6-12                     MASS_7.3-45                     tictoc_1.0                      ellipse_0.4.1                  
[41] AppliedPredictiveModeling_1.1-6 scatterplot3d_0.3-40            corrplot_0.84                   C50_0.1.1                      
[45] RODBC_1.3-15                    randomForest_4.6-12             rpart.plot_2.1.2                rpart_4.1-12                   
[49] caret_6.0-78                    ggplot2_2.2.1                   lattice_0.20-33                

loaded via a namespace (and not attached):
 [1] stringr_1.2.0       httr_1.3.1          ddalpha_1.3.1.1     purrr_0.2.4         jsonlite_1.5        gdata_2.18.0       
 [7] gtools_3.5.0        Formula_1.2-2       rlang_0.1.6         bitops_1.0-6        pillar_1.1.0        rgexf_0.15.3       
[13] abind_1.4-5         reshape2_1.4.3      R6_2.2.2            lubridate_1.7.1     recipes_0.1.2       gplots_3.0.1       
[19] xts_0.10-1          stringi_1.1.6       viridis_0.5.0       munsell_0.4.3       e1071_1.6-8         Cubist_0.2.1       
[25] rstudioapi_0.7      htmlwidgets_1.0     DRR_0.0.3           influenceR_0.1.0    robustbase_0.92-8   ROCR_1.0-7         
[31] quantmod_0.4-12     zoo_1.8-1           CORElearn_1.52.0    bindr_0.1           foreign_0.8-66      pkgconfig_2.0.1    
[37] CVST_0.2-1          Rook_1.1-1          tools_3.3.1         brew_1.0-6          compiler_3.3.1      assertthat_0.2.0   
[43] lava_1.6            caTools_1.17.1      igraph_1.1.2        labeling_0.3        ipred_0.9-6         gtable_0.2.0       
[49] glue_1.2.0          mnormt_1.5-5        downloader_0.4      digest_0.6.14       RColorBrewer_1.1-2  htmltools_0.3.6    
[55] KernSmooth_2.23-15  XML_3.98-1.9        readr_1.1.1         broom_0.4.3         data.table_1.10.4-3 sfsmisc_1.1-1      
[61] prodlim_1.6.1       visNetwork_2.0.3    withr_2.1.1         pbapply_1.3-4       lazyeval_0.2.1      magrittr_1.5       
[67] minqa_1.2.4         colorspace_1.3-2    psych_1.7.8         dimRed_0.1.0        timeDate_3042.101   Rcpp_0.12.15       
[73] coda_0.19-1         viridisLite_0.3.0   scales_0.5.0        gower_0.1.2         RcppRoll_0.2.2      TTR_0.23-3         
[79] nloptr_1.0.4        combinat_0.0-8      stats4_3.3.1        cellranger_1.1.0    ModelMetrics_1.1.0  codetools_0.2-14   
[85] curl_3.1            nlme_3.1-128        hms_0.4.1           DEoptimR_1.0-8      tidyselect_0.2.3   
hadjipantelis commented 6 years ago

I get the same error on CentOS 7. This is not specific to the Win-32bit R 3.3.1 version.

It appears that the as.party function within plot.C5.0 fails to recognise the finalModel because the train call is quite elaborate. This leads as.party to try to make a model.frame out of the finalModel as it considers it a non-default partykit object. This last model.frame operation errs.

glglgithub commented 6 years ago

@hadjipantelis , I got the similar problem. any solution?

hadjipantelis commented 6 years ago

@glglgithub As it stands any solutions requiressome code to be rewritten (and most likely that code will be within the C50 rather than caret...). The obvious solution is to take caret::train away from the equation. Use directly the bestTune from the fitted train object to define the relevant C5.0 model manually. Then plot that C5.0 model normally.

glglgithub commented 6 years ago

@hadjipantelis , thanks for the reply. Yes, the way you suggested works great.

topepo commented 6 years ago

That is the best solution. There's no real way to get around in with caret since the formula will not reference back to original objects. 😦