zachmayer / caretEnsemble

caret models all the way down :turtle:
Other
226 stars 75 forks source link

could not find function "residuals.caretEnsemble" #253

Closed momeni133 closed 3 years ago

momeni133 commented 3 years ago

Hello, I made an ensemble model using caretEnsemble package. When I try to extract the residuals of the model, I receive the below error:

`Error in residuals.caretEnsemble(ensemble_2) : could not find function "residuals.caretEnsemble"

I also tried multiResiduals function but I received the same error message. Any ideas why this happens? Thanks

sessionInfo() R version 4.0.2 (2020-06-22) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:

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

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

other attached packages: [1] caret_6.0-86 ggplot2_3.3.2 lattice_0.20-41
[4] caretEnsemble_2.0.1

loaded via a namespace (and not attached):

[1] pkgload_1.1.0 splines_4.0.2 foreach_1.5.0
[4] prodlim_2019.11.13 Formula_1.2-3 assertthat_0.2.1
[7] sp_1.4-2 stats4_4.0.2 remotes_2.2.0
[10] sessioninfo_1.1.1 ipred_0.9-9 pillar_1.4.6
[13] backports_1.1.9 glue_1.4.2 pROC_1.16.2
[16] digest_0.6.25 evtree_1.0-8 colorspace_1.4-1
[19] recipes_0.1.13 Matrix_1.2-18 plyr_1.8.6
[22] timeDate_3043.102 pkgconfig_2.0.3 devtools_2.3.1
[25] raster_3.3-13 mvtnorm_1.1-1 purrr_0.3.4
[28] scales_1.1.1 processx_3.4.4 gower_0.2.2
[31] lava_1.6.7 tibble_3.0.3 generics_0.0.2
[34] xgboost_1.2.0.1 usethis_1.6.1 ellipsis_0.3.1
[37] withr_2.2.0 pbapply_1.4-3 nnet_7.3-14
[40] cli_2.0.2 survival_3.1-12 magrittr_1.5
[43] crayon_1.3.4 memoise_1.1.0 ps_1.3.4
[46] fs_1.5.0 fansi_0.4.1 doParallel_1.0.15
[49] nlme_3.1-148 MASS_7.3-51.6 class_7.3-17
[52] pkgbuild_1.1.0 tools_4.0.2 fitdistrplus_1.1-1
[55] data.table_1.13.0 prettyunits_1.1.1 lifecycle_0.2.0
[58] stringr_1.4.0 kernlab_0.9-29 glmnet_4.0-2
[61] munsell_0.5.0 callr_3.4.4 compiler_4.0.2
[64] inum_1.0-1 tinytex_0.25 rlang_0.4.7
[67] grid_4.0.2 iterators_1.0.12 rstudioapi_0.11
[70] partykit_1.2-9 testthat_2.3.2 gtable_0.3.0
[73] ModelMetrics_1.2.2.2 codetools_0.2-16 reshape2_1.4.4
[76] R6_2.4.1 gridExtra_2.3 lubridate_1.7.9
[79] dplyr_1.0.2 rgeos_0.5-5 libcoin_1.0-6
[82] rprojroot_1.3-2 shape_1.4.4 desc_1.2.0
[85] stringi_1.4.6 parallel_4.0.2 Rcpp_1.0.5
[88] vctrs_0.3.4 rpart_4.1-15 tidyselect_1.1.0
[91] xfun_0.16

zachmayer commented 3 years ago

Please run sessionInfo() and paste the results

On Tue, Sep 8, 2020 at 7:39 PM im133 notifications@github.com wrote:

Hello,

I made an ensemble model using caretEnsemble package. When I try to extract the residuals of the model, I receive the below error:

Error in residuals.caretEnsemble(model_list) :

could not find function "residuals.caretEnsemble"

I also tried multiResiduals function but I received the same error message.

Any ideas why this happens?

Thanks

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zachmayer/caretEnsemble/issues/253, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEN7VSCHKDCEVJSG7EEOOLSE26DVANCNFSM4RAVDGHQ .

momeni133 commented 3 years ago

Hello, thanks for your quick response. I've already done this. Here it is: sessionInfo() R version 4.0.2 (2020-06-22) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:

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

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

other attached packages: [1] caret_6.0-86 ggplot2_3.3.2 lattice_0.20-41 [4] caretEnsemble_2.0.1

loaded via a namespace (and not attached):

[1] pkgload_1.1.0 splines_4.0.2 foreach_1.5.0 [4] prodlim_2019.11.13 Formula_1.2-3 assertthat_0.2.1 [7] sp_1.4-2 stats4_4.0.2 remotes_2.2.0 [10] sessioninfo_1.1.1 ipred_0.9-9 pillar_1.4.6 [13] backports_1.1.9 glue_1.4.2 pROC_1.16.2 [16] digest_0.6.25 evtree_1.0-8 colorspace_1.4-1 [19] recipes_0.1.13 Matrix_1.2-18 plyr_1.8.6 [22] timeDate_3043.102 pkgconfig_2.0.3 devtools_2.3.1 [25] raster_3.3-13 mvtnorm_1.1-1 purrr_0.3.4 [28] scales_1.1.1 processx_3.4.4 gower_0.2.2 [31] lava_1.6.7 tibble_3.0.3 generics_0.0.2 [34] xgboost_1.2.0.1 usethis_1.6.1 ellipsis_0.3.1 [37] withr_2.2.0 pbapply_1.4-3 nnet_7.3-14 [40] cli_2.0.2 survival_3.1-12 magrittr_1.5 [43] crayon_1.3.4 memoise_1.1.0 ps_1.3.4 [46] fs_1.5.0 fansi_0.4.1 doParallel_1.0.15 [49] nlme_3.1-148 MASS_7.3-51.6 class_7.3-17 [52] pkgbuild_1.1.0 tools_4.0.2 fitdistrplus_1.1-1 [55] data.table_1.13.0 prettyunits_1.1.1 lifecycle_0.2.0 [58] stringr_1.4.0 kernlab_0.9-29 glmnet_4.0-2 [61] munsell_0.5.0 callr_3.4.4 compiler_4.0.2 [64] inum_1.0-1 tinytex_0.25 rlang_0.4.7 [67] grid_4.0.2 iterators_1.0.12 rstudioapi_0.11 [70] partykit_1.2-9 testthat_2.3.2 gtable_0.3.0 [73] ModelMetrics_1.2.2.2 codetools_0.2-16 reshape2_1.4.4 [76] R6_2.4.1 gridExtra_2.3 lubridate_1.7.9 [79] dplyr_1.0.2 rgeos_0.5-5 libcoin_1.0-6 [82] rprojroot_1.3-2 shape_1.4.4 desc_1.2.0 [85] stringi_1.4.6 parallel_4.0.2 Rcpp_1.0.5 [88] vctrs_0.3.4 rpart_4.1-15 tidyselect_1.1.0 [91] xfun_0.16

momeni133 commented 3 years ago

Hello Zach, I am receiving the same error for a couple of other functions such as "summary.caretEnsemble" Have you got a chance to look into this problem?

Thank you very much, Iman

zachmayer commented 3 years ago

I have not had a chance to look at this.

try running: update.packages() devtools::install_github('zachmayer/caretEnsemble') library(caretEnsemble)

If that doesn't work, please post a:

  1. Minimal
  2. Reproducible
  3. Example

of your error. If you don't know how to do this, please read: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

On Thu, Sep 10, 2020 at 3:23 PM im133 notifications@github.com wrote:

Hello Zach, I am receiving the same error for a couple of other functions such as "summary.caretEnsemble" Have you got a chance to look into this problem?

Thank you very much, Iman

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/zachmayer/caretEnsemble/issues/253#issuecomment-690657568, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEN7VTYEV36WDXFQ3XWX6DSFERTDANCNFSM4RAVDGHQ .

momeni133 commented 3 years ago

Hi Zach, The code is: `library(caret) library(caretEnsemble) library(doParallel)

response variable

AADT = c(5658.471, 5492.9, 6013.788,816.873, 1622.88, 463.712, 457.322, 7661.126, 3367.84, 4090.005, 7914.627, 6156.41, 10000.802, 8021.08, 6217.896, 8572.876, 4975.74, 1736.3, 5621.658, 684.877, 1003.275, 4735.575, 9532.8, 2521.664, 5990.94, 3002.512, 5234.18, 4652.205, 1825.668, 2119.878, 6858.616, 7875.182, 10800.816, 3470.36, 3470.36, 4808.178, 3029.338, 4644.032, 4065.04, 3246.053, 2031.688, 3797.915, 4078.251, 1998.994, 3771.442, 2892.297, 2067.642, 11470.9, 291.416, 1401.696)

Predictor1

Forest= c(0.194979851, 0.194418371, 0.304878147, 0.255000768, 0.856836535, 0.918318814, 0.875915007, 0.195093335, 0.373975783, 0.335545753, 0.185332028, 0.189831029, 0.092578802, 0.276719132, 0.172826324, 0.117998802, 0.131141174, 0.077656632, 0.000873553, 0.496520754, 0.120935387, 0.608988255, 0.231603903, 0.538147216, 0.416813658, 0.362239244, 0.167912319, 0.238095623, 0.363733428, 0.428463929, 0.481414354, 0.267738126, 0.017411848, 0.030003398, 0.030003398, 0.014389362, 0.327051249, 0.012388235, 0.116046946, 0.102922264, 0.186257381, 0.072540984, 0.338372563, 0.114397044, 0.029072129, 0.069328241, 0.308007865, 0.236584971, 0.933553957, 0.475009103)

Predictor2

Grassland= c(0.614631646,0.566854833, 0.493754288, 0.684973501, 0.134770713, 0.105142315, 0.104698959, 0.168158461, 0.20430072, 0.248454057, 0.240593538, 0.250391517, 0.222325231, 0.172309508, 0.338234295, 0.534534968, 0.369579956, 0.715027485, 0.640376102, 0.306145853, 0.689637506, 0.236868812, 0.584886158, 0.366380453, 0.430652297, 0.724496493, 0.650782354, 0.684670482, 0.307056755, 0.643363837, 0.431891362, 0.482954238, 0.439069748, 0.855101654, 0.855101654, 0.649591198, 0.684688568, 0.647771416, 0.807575882, 0.809139902, 0.624250097, 0.669138936, 0.601540861, 0.711003103, 0.677001985, 0.747325401, 0.251727316, 0.392524747, 0.068327311, 0.207235115)

Predictor3

Urban = c(0.048753055, 0.13809231,0.068784224, 0.02723427, 0.053452656, 0.022284776, 0.042707044, 0.049471223, 0.020399414, 0.060706625, 0.065742097, 0.066577153, 0.080226327, 0.070932885, 0.058223527, 0.062547538, 0.021514337, 0.077171328, 0.457774219, 0.010510559, 0.093174685, 0.046421523, 0.017453433, 0.017299554, 0.038587264, 0.10464204, 0.06670711, 0.049413173, 0.036294073, 0.023359873, 0.053628934, 0.074559984, 0.11434212, 0.089710809, 0.089710809, 0.031981752, 0.018765368, 0.030675244, 0.049866134, 0.046830641, 0.061481841, 0.066621129, 0.067499798, 0.019491977, 0.053191489, 0.058857799, 0.079989643, 0.095664483, 0.010097986, 0.040223328)

Predictor4

primary_road = c(1L,0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L)

converting variables to a dataframe

data <- as.data.frame(cbind(AADT,Forest,Grassland,Urban,primary_road))

registerDoParallel(16) getDoParWorkers()

set.seed(123) my_control <- trainControl(method = "cv", # for "cross-validation" number = 10, # number of k-folds savePredictions = "final", verboseIter = TRUE, allowParallel = TRUE) model_list <- caretList(log(AADT)~., data=data, trControl = my_control, methodList = c("glm", "svmRadial", "ranger", "xgbTree", "xgbLinear","gbm"), tuneLength = 3, tuneList = NULL, continue_on_fail = FALSE, preProcess = c("center","scale","YeoJohnson"))

resamples <- resamples(model_list)

ensemble <- caretStack(model_list, method = "glmnet", metric = "RMSE", trControl = my_control) residuals.caretEnsemble (ensemble$ens_model)`

Thanks

zachmayer commented 3 years ago

Ahh, the last line is the problem. Use residuals(ensemble$ens_model).

residuals.caretEnsemble is an s3 method. Calling residuals.caretEnsemble confuses R. Calling the residuals generic function allows R to use it's own S3 dispatch and use the method correctly.