tidymodels / dials

Tools for creating tuning parameter values
https://dials.tidymodels.org/
Other
114 stars 27 forks source link

Error in documentation? #81

Closed datalorax closed 4 years ago

datalorax commented 4 years ago

Hi,

Perhaps I'm missing something, but I am unable to get the example from the documentation here to work. Reprex below. I do have the dev versions of dplyr and ggplot2, as you can see, so perhaps there's something there that's interfering?

library(tidymodels)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> ── Attaching packages ────────────────────────────────────── tidymodels 0.0.3 ──
#> ✔ broom     0.5.3          ✔ purrr     0.3.3     
#> ✔ dials     0.0.4          ✔ recipes   0.1.7     
#> ✔ dplyr     0.8.3.9000     ✔ rsample   0.0.5     
#> ✔ ggplot2   3.2.1.9000     ✔ tibble    2.1.3     
#> ✔ infer     0.5.1          ✔ yardstick 0.0.4     
#> ✔ parsnip   0.0.4
#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
#> ✖ purrr::discard()    masks scales::discard()
#> ✖ dplyr::filter()     masks stats::filter()
#> ✖ dplyr::lag()        masks stats::lag()
#> ✖ ggplot2::margin()   masks dials::margin()
#> ✖ recipes::step()     masks stats::step()
#> ✖ recipes::yj_trans() masks scales::yj_trans()
library(dials)

set.seed(1263)
bst_grid <- grid_random(
  trees %>%       range_set(c( 1,  50)), 
  min_n %>%       range_set(c( 2,  30)), 
  sample_size %>% range_set(c(20, 130)), 
  size = 3
)
#> Error: `object` should be a 'quant_param' object

bst_grid
#> Error in eval(expr, envir, enclos): object 'bst_grid' not found

Created on 2019-12-16 by the reprex package (v0.3.0)

EmilHvitfeldt commented 4 years ago

You are correct there is a mistake in the documentation. version 0.0.3 changes all parameter objects to functions. Thus you need to use trees() instead of trees.

Thus your example will be:

library(tidymodels)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> ── Attaching packages ───────────────────────────────────────────── tidymodels 0.0.3 ──
#> ✓ broom     0.5.2          ✓ purrr     0.3.3     
#> ✓ dials     0.0.4          ✓ recipes   0.1.7.9001
#> ✓ dplyr     0.8.3          ✓ rsample   0.0.5     
#> ✓ ggplot2   3.2.1          ✓ tibble    2.1.3     
#> ✓ infer     0.5.1          ✓ yardstick 0.0.4     
#> ✓ parsnip   0.0.4
#> ── Conflicts ──────────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard()    masks scales::discard()
#> x dplyr::filter()     masks stats::filter()
#> x dplyr::lag()        masks stats::lag()
#> x ggplot2::margin()   masks dials::margin()
#> x recipes::step()     masks stats::step()
#> x recipes::yj_trans() masks scales::yj_trans()
library(dials)

set.seed(1263)
bst_grid <- grid_random(
  trees() %>%       range_set(c( 1,  50)), 
  min_n() %>%       range_set(c( 2,  30)), 
  sample_size() %>% range_set(c(20, 130)), 
  size = 3
)
bst_grid
#> # A tibble: 3 x 3
#>   trees min_n sample_size
#>   <int> <int>       <int>
#> 1    23    23         110
#> 2    11    18          26
#> 3    14    12         116

Created on 2019-12-16 by the reprex package (v0.3.0)

datalorax commented 4 years ago

Ah... perfect. Thanks!

datalorax commented 4 years ago

Sorry to follow up here again, but I'm still not able to get the rest of this documentation to run. Specifically, the merge still doesn't seem to work

library(tidymodels)
mod_obj <- boost_tree(mode = "classification", 
                        trees = 50, 
                        min_n = varying(), 
                        sample_size = 3/4)  

set.seed(1263)
bst_grid <- grid_random(
  trees() %>%       range_set(c( 1,  50)), 
  min_n() %>%       range_set(c( 2,  30)), 
  sample_size() %>% range_set(c(20, 130)), 
  size = 3
)

for(i in 1:nrow(bst_grid)) {
  print(merge(mod_obj, bst_grid[i, ]))
}
#> Error in as.data.frame.default(x): cannot coerce class 'c("boost_tree", "model_spec")' to a data.frame

Created on 2019-12-16 by the reprex package (v0.3.0)

topepo commented 4 years ago

tune is about to be released so you would use tune() instead of varying(). I'll update the docs once that is released (hopefully next week).

You will also need to set the engine for merge() to work.

library(tidymodels)
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> ── Attaching packages ───────────────────────────────────────────────────────────────────────────── tidymodels 0.0.3 ──
#> ✓ broom     0.5.2     ✓ purrr     0.3.3
#> ✓ dials     0.0.4     ✓ recipes   0.1.8
#> ✓ dplyr     0.8.3     ✓ rsample   0.0.5
#> ✓ ggplot2   3.2.1     ✓ tibble    2.1.3
#> ✓ infer     0.5.1     ✓ yardstick 0.0.4
#> ✓ parsnip   0.0.4
#> ── Conflicts ──────────────────────────────────────────────────────────────────────────────── tidymodels_conflicts() ──
#> x purrr::discard()    masks scales::discard()
#> x dplyr::filter()     masks stats::filter()
#> x dplyr::lag()        masks stats::lag()
#> x ggplot2::margin()   masks dials::margin()
#> x recipes::step()     masks stats::step()
#> x recipes::yj_trans() masks scales::yj_trans()
library(tune)

mod_obj <- 
  boost_tree(mode = "classification", 
                      trees = 50, 
                      min_n = tune(), 
                      sample_size = 3/4) %>% 
  set_engine("xgboost")

set.seed(1263)
bst_grid <- grid_random(
  trees() %>%       range_set(c( 1,  50)), 
  min_n() %>%       range_set(c( 2,  30)), 
  sample_size() %>% range_set(c(20, 130)), 
  size = 3
)
bst_grid
#> # A tibble: 3 x 3
#>   trees min_n sample_size
#>   <int> <int>       <int>
#> 1    23    23         110
#> 2    11    18          26
#> 3    14    12         116

# only updates the unknown parameters (not trees here)
for(i in 1:nrow(bst_grid)) {
  print(merge(mod_obj, bst_grid[i, ]))
}
#> # A tibble: 1 x 1
#>   x        
#> * <list>   
#> 1 <spec[+]>
#> # A tibble: 1 x 1
#>   x        
#> * <list>   
#> 1 <spec[+]>
#> # A tibble: 1 x 1
#>   x        
#> * <list>   
#> 1 <spec[+]>
merge(mod_obj, bst_grid[1, ])$x
#> [[1]]
#> Boosted Tree Model Specification (classification)
#> 
#> Main Arguments:
#>   trees = 50
#>   min_n = 23
#>   sample_size = 3/4
#> 
#> Computational engine: xgboost

# also, in tune: 
finalize_model(mod_obj, bst_grid[1, ])
#> Boosted Tree Model Specification (classification)
#> 
#> Main Arguments:
#>   trees = 50
#>   min_n = 23
#>   sample_size = 3/4
#> 
#> Computational engine: xgboost

Created on 2020-01-06 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.1 (2019-07-05) #> os macOS Mojave 10.14.6 #> system x86_64, darwin15.6.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2020-01-06 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) #> backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.0) #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 3.6.0) #> bayesplot 1.7.0 2019-05-23 [1] CRAN (R 3.6.0) #> boot 1.3-24 2019-12-20 [1] CRAN (R 3.6.0) #> broom * 0.5.2 2019-04-07 [1] CRAN (R 3.6.0) #> callr 3.4.0 2019-12-09 [1] CRAN (R 3.6.0) #> class 7.3-15 2019-01-01 [1] CRAN (R 3.6.0) #> cli 2.0.0 2019-12-09 [1] CRAN (R 3.6.0) #> codetools 0.2-16 2018-12-24 [1] CRAN (R 3.6.1) #> colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0) #> colourpicker 1.0 2017-09-27 [1] CRAN (R 3.6.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) #> crosstalk 1.0.0 2016-12-21 [1] CRAN (R 3.6.0) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.0) #> devtools 2.2.1 2019-09-24 [1] CRAN (R 3.6.0) #> dials * 0.0.4 2019-12-02 [1] CRAN (R 3.6.0) #> DiceDesign 1.8-1 2019-07-31 [1] CRAN (R 3.6.0) #> digest 0.6.23 2019-11-23 [1] CRAN (R 3.6.1) #> dplyr * 0.8.3 2019-07-04 [1] CRAN (R 3.6.0) #> DT 0.11 2019-12-19 [1] CRAN (R 3.6.0) #> dygraphs 1.1.1.6 2018-07-11 [1] CRAN (R 3.6.0) #> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0) #> fansi 0.4.0 2018-10-05 [1] CRAN (R 3.6.0) #> fastmap 1.0.1 2019-10-08 [1] CRAN (R 3.6.0) #> foreach 1.4.7 2019-07-27 [1] CRAN (R 3.6.0) #> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0) #> furrr 0.1.0 2018-05-16 [1] CRAN (R 3.6.0) #> future 1.15.1 2019-11-25 [1] CRAN (R 3.6.0) #> generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0) #> ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.0) #> ggridges 0.5.1 2018-09-27 [1] CRAN (R 3.6.0) #> globals 0.12.5 2019-12-07 [1] CRAN (R 3.6.0) #> glue 1.3.1 2019-03-12 [1] CRAN (R 3.6.0) #> gower 0.2.1 2019-05-14 [1] CRAN (R 3.6.0) #> GPfit 1.0-8 2019-02-08 [1] CRAN (R 3.6.0) #> gridExtra 2.3 2017-09-09 [1] CRAN (R 3.6.0) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0) #> gtools 3.8.1 2018-06-26 [1] CRAN (R 3.6.0) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) #> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.0) #> htmlwidgets 1.5.1 2019-10-08 [1] CRAN (R 3.6.0) #> httpuv 1.5.2 2019-09-11 [1] CRAN (R 3.6.0) #> igraph 1.2.4.2 2019-11-27 [1] CRAN (R 3.6.0) #> infer * 0.5.1 2019-11-19 [1] CRAN (R 3.6.0) #> inline 0.3.15 2018-05-18 [1] CRAN (R 3.6.0) #> ipred 0.9-9 2019-04-28 [1] CRAN (R 3.6.0) #> iterators 1.0.12 2019-07-26 [1] CRAN (R 3.6.0) #> janeaustenr 0.1.5 2017-06-10 [1] CRAN (R 3.6.0) #> knitr 1.26 2019-11-12 [1] CRAN (R 3.6.0) #> later 1.0.0 2019-10-04 [1] CRAN (R 3.6.1) #> lattice 0.20-38 2018-11-04 [1] CRAN (R 3.6.1) #> lava 1.6.6 2019-08-01 [1] CRAN (R 3.6.0) #> lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0) #> lhs 1.0.1 2019-02-03 [1] CRAN (R 3.6.0) #> lifecycle 0.1.0 2019-08-01 [1] CRAN (R 3.6.0) #> listenv 0.8.0 2019-12-05 [1] CRAN (R 3.6.1) #> lme4 1.1-21 2019-03-05 [1] CRAN (R 3.6.0) #> loo 2.1.0 2019-03-13 [1] CRAN (R 3.6.0) #> lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) #> markdown 1.1 2019-08-07 [1] CRAN (R 3.6.0) #> MASS 7.3-51.5 2019-12-20 [1] CRAN (R 3.6.0) #> Matrix 1.2-18 2019-11-27 [1] CRAN (R 3.6.0) #> matrixStats 0.55.0 2019-09-07 [1] CRAN (R 3.6.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.0) #> mime 0.8 2019-12-19 [1] CRAN (R 3.6.1) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 3.6.0) #> minqa 1.2.4 2014-10-09 [1] CRAN (R 3.6.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0) #> nlme 3.1-140 2019-05-12 [1] CRAN (R 3.6.1) #> nloptr 1.2.1 2018-10-03 [1] CRAN (R 3.6.0) #> nnet 7.3-12 2016-02-02 [1] CRAN (R 3.6.0) #> parsnip * 0.0.4 2019-11-02 [1] CRAN (R 3.6.0) #> pillar 1.4.3.9000 2020-01-05 [1] Github (r-lib/pillar@8f5918c) #> pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.0) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.0) #> plyr 1.8.5 2019-12-10 [1] CRAN (R 3.6.0) #> prettyunits 1.0.2 2015-07-13 [1] CRAN (R 3.6.0) #> pROC 1.15.3 2019-07-21 [1] CRAN (R 3.6.0) #> processx 3.4.1 2019-07-18 [1] CRAN (R 3.6.0) #> prodlim 2019.11.13 2019-11-17 [1] CRAN (R 3.6.0) #> promises 1.1.0 2019-10-04 [1] CRAN (R 3.6.0) #> ps 1.3.0 2018-12-21 [1] CRAN (R 3.6.0) #> purrr * 0.3.3 2019-10-18 [1] CRAN (R 3.6.0) #> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.0) #> Rcpp 1.0.3 2019-11-08 [1] CRAN (R 3.6.0) #> recipes * 0.1.8 2019-12-18 [1] local #> remotes 2.1.0 2019-06-24 [1] CRAN (R 3.6.0) #> reshape2 1.4.3 2017-12-11 [1] CRAN (R 3.6.0) #> rlang 0.4.2 2019-11-23 [1] CRAN (R 3.6.0) #> rmarkdown 2.0 2019-12-12 [1] CRAN (R 3.6.0) #> rpart 4.1-15 2019-04-12 [1] CRAN (R 3.6.0) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.0) #> rsample * 0.0.5 2019-07-12 [1] CRAN (R 3.6.0) #> rsconnect 0.8.15 2019-07-22 [1] CRAN (R 3.6.0) #> rstan 2.19.2 2019-07-09 [1] CRAN (R 3.6.0) #> rstanarm 2.19.2 2019-10-03 [1] CRAN (R 3.6.1) #> rstantools 2.0.0 2019-09-15 [1] CRAN (R 3.6.0) #> rstudioapi 0.10 2019-03-19 [1] CRAN (R 3.6.0) #> scales * 1.1.0 2019-11-18 [1] CRAN (R 3.6.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.0) #> shiny 1.4.0 2019-10-10 [1] CRAN (R 3.6.0) #> shinyjs 1.0 2018-01-08 [1] CRAN (R 3.6.0) #> shinystan 2.5.0 2018-05-01 [1] CRAN (R 3.6.0) #> shinythemes 1.1.2 2018-11-06 [1] CRAN (R 3.6.0) #> SnowballC 0.6.0 2019-01-15 [1] CRAN (R 3.6.0) #> StanHeaders 2.19.0 2019-09-07 [1] CRAN (R 3.6.0) #> stringi 1.4.3 2019-03-12 [1] CRAN (R 3.6.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> survival 2.40-1 2016-10-30 [1] CRAN (R 3.6.1) #> testthat 2.3.1 2019-12-01 [1] CRAN (R 3.6.0) #> threejs 0.3.1 2017-08-13 [1] CRAN (R 3.6.0) #> tibble * 2.1.3 2019-06-06 [1] CRAN (R 3.6.0) #> tidymodels * 0.0.3 2019-10-04 [1] CRAN (R 3.6.0) #> tidyposterior 0.0.2 2018-11-15 [1] CRAN (R 3.6.0) #> tidypredict 0.4.3 2019-09-03 [1] CRAN (R 3.6.0) #> tidyr * 1.0.0 2019-09-11 [1] CRAN (R 3.6.0) #> tidyselect 0.2.5 2018-10-11 [1] CRAN (R 3.6.0) #> tidytext 0.2.2 2019-07-29 [1] CRAN (R 3.6.0) #> timeDate 3043.102 2018-02-21 [1] CRAN (R 3.6.0) #> tokenizers 0.2.1 2018-03-29 [1] CRAN (R 3.6.0) #> tune * 0.0.1 2020-01-05 [1] local #> usethis 1.5.1.9000 2019-12-18 [1] Github (r-lib/usethis@b2e894e) #> utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.0) #> vctrs 0.2.1 2019-12-17 [1] CRAN (R 3.6.1) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0) #> workflows 0.1.0 2019-12-30 [1] CRAN (R 3.6.1) #> xfun 0.11 2019-11-12 [1] CRAN (R 3.6.0) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 3.6.0) #> xts 0.11-2 2018-11-05 [1] CRAN (R 3.6.0) #> yaml 2.2.0 2018-07-25 [1] CRAN (R 3.6.0) #> yardstick * 0.0.4 2019-08-26 [1] CRAN (R 3.6.0) #> zeallot 0.1.0 2018-01-28 [1] CRAN (R 3.6.0) #> zoo 1.8-6 2019-05-28 [1] CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```
datalorax commented 4 years ago

Great, thanks!

carlmcqueen commented 4 years ago

Is merge working from 'base::merge' or a dials function of merge that isn't available to the outside? I still am getting the cannot_coerce message because it's attempting to use the base merge data.frame function.

karnwalone commented 4 years ago

dials package does not seem to have any merge function. I have checked in tune also. Without merge, rest of the example does not seem to work. Is there any alternative or work-around for merge function?

topepo commented 4 years ago

There are merge methods in tune:

library(tune)
methods("merge")
#>  [1] merge.data.frame   merge.default      merge.dendrogram*  merge.estimate*   
#>  [5] merge.glm*         merge.lm*          merge.lvm*         merge.lvmfit*     
#>  [9] merge.model_spec*  merge.multinomial* merge.recipe*     
#> see '?methods' for accessing help and source code

Created on 2020-04-03 by the reprex package (v0.3.0)

I suggest opening another issue and giving a reprex for what you want to do.

github-actions[bot] commented 3 years ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.