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 634 forks source link

How can I obtain probabilities using caret with ranger? #1275

Closed hbaniecki closed 2 years ago

hbaniecki commented 2 years ago

I couldn't make it work.

data(iris)

library(caret)
library(ranger)

model <- train(Species  ~ ., data = iris, method = "ranger")

predict(model, iris) # ok

predict(model, iris, type="prob") # error

Error in [.data.frame(out, , obsLevels, drop = FALSE) : undefined columns selected

R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] ranger_0.13.1   caret_6.0-90    lattice_0.20-44 ggplot2_3.3.5  

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1     purrr_0.3.4          reshape2_1.4.4      
 [4] listenv_0.8.0        splines_4.1.1        colorspace_2.0-3    
 [7] vctrs_0.3.8          generics_0.1.1       stats4_4.1.1        
[10] utf8_1.2.2           survival_3.2-13      prodlim_2019.11.13  
[13] rlang_1.0.1          e1071_1.7-9          ModelMetrics_1.2.2.2
[16] pillar_1.7.0         glue_1.6.1           withr_2.4.3         
[19] DBI_1.1.2            foreach_1.5.1        lifecycle_1.0.1     
[22] plyr_1.8.6           lava_1.6.10          stringr_1.4.0       
[25] timeDate_3043.102    munsell_0.5.0        gtable_0.3.0        
[28] future_1.23.0        recipes_0.1.17       codetools_0.2-18    
[31] parallel_4.1.1       class_7.3-19         fansi_1.0.2         
[34] Rcpp_1.0.7           scales_1.1.1         ipred_0.9-12        
[37] parallelly_1.28.1    digest_0.6.29        stringi_1.7.6       
[40] dplyr_1.0.7          grid_4.1.1           cli_3.2.0           
[43] tools_4.1.1          magrittr_2.0.2       proxy_0.4-26        
[46] tibble_3.1.6         crayon_1.5.0         future.apply_1.8.1  
[49] pkgconfig_2.0.3      ellipsis_0.3.2       MASS_7.3-54         
[52] Matrix_1.3-4         data.table_1.14.2    pROC_1.18.0         
[55] lubridate_1.8.0      gower_0.2.2          assertthat_0.2.1    
[58] iterators_1.0.13     R6_2.5.1             globals_0.14.0      
[61] rpart_4.1-15         nnet_7.3-16          nlme_3.1-152        
[64] compiler_4.1.1     
hbaniecki commented 2 years ago

Solved using trControl = trainControl(classProbs = TRUE):

data(iris)

library(caret)
library(ranger)

model <- train(Species  ~ ., data = iris, method = "ranger",
               trControl = trainControl(classProbs = TRUE))

predict(model, iris)

predict(model, iris, type="prob")