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

nearest_neighbor() error with factor predictors & kknn not loaded #264

Closed
BobMuenchen opened this issue Feb 20, 2020 · 2 comments · Fixed by #914
Closed

nearest_neighbor() error with factor predictors & kknn not loaded #264

BobMuenchen opened this issue Feb 20, 2020 · 2 comments · Fixed by #914
Labels
bug an unexpected problem or unintended behavior

Comments

@BobMuenchen
Copy link

# nearest_neighbor() demo showing that for datasets with factor 
# predictors, kknn package must be loaded manually

library(tidyverse)
library("tidymodels")
#> Registered S3 method overwritten by 'xts':
#>   method     from
#>   as.zoo.xts zoo
#> -- Attaching packages -------------------------------------------------------------------------------- tidymodels 0.0.3 --
#> v broom     0.5.4          v recipes   0.1.9     
#> v dials     0.0.4          v rsample   0.0.5     
#> v infer     0.5.1          v yardstick 0.0.5     
#> v parsnip   0.0.5.9000
#> -- Conflicts ----------------------------------------------------------------------------------- tidymodels_conflicts() --
#> x scales::discard()   masks purrr::discard()
#> x dplyr::filter()     masks stats::filter()
#> x recipes::fixed()    masks stringr::fixed()
#> x dplyr::lag()        masks stats::lag()
#> x dials::margin()     masks ggplot2::margin()
#> x yardstick::spec()   masks readr::spec()
#> x recipes::step()     masks stats::step()
#> x recipes::yj_trans() masks scales::yj_trans()

# First, all predictors are numeric:
mtcars_numeric <- mtcars %>% mutate(am = as_factor(am))

# nearest_neighbor() works fine:
mtcars_knn <- 
  nearest_neighbor() %>%  
  set_mode("classification") %>%
  set_engine("kknn") %>%
  parsnip::fit(am ~ ., data = mtcars_numeric)

# Nextd, add a factor predictor:
mtcars_with_factor <- mtcars %>% 
  mutate(am = as_factor(am),
         cyl = as_factor(cyl))

# nearest_neighbor throws an error:
mtcars_knn <- 
  nearest_neighbor() %>%  
  set_mode("classification") %>%
  set_engine("kknn") %>%
  parsnip::fit(am ~ ., data = mtcars_with_factor)
#> Error in get(ctr, mode = "function", envir = parent.frame()): object 'contr.dummy' of mode 'function' was not found
#> Timing stopped at: 0.01 0 0.02

# Now manually load kknn package, rerun above code & it works fine:
library("kknn")
mtcars_knn <- 
  nearest_neighbor() %>%  
  set_mode("classification") %>%
  set_engine("kknn") %>%
  parsnip::fit(am ~ ., data = mtcars_with_factor)

devtools::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  ctype    English_United States.1252  
#>  tz       America/New_York            
#>  date     2020-02-20                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package       * version    date       lib
#>  assertthat      0.2.1      2019-03-21 [1]
#>  backports       1.1.5      2019-10-02 [1]
#>  base64enc       0.1-3      2015-07-28 [1]
#>  bayesplot       1.7.1      2019-12-01 [1]
#>  boot            1.3-23     2019-07-05 [2]
#>  broom         * 0.5.4      2020-01-27 [1]
#>  callr           3.4.2      2020-02-12 [1]
#>  cellranger      1.1.0      2016-07-27 [1]
#>  class           7.3-15     2019-01-01 [2]
#>  cli             2.0.1.9000 2020-02-19 [1]
#>  codetools       0.2-16     2018-12-24 [2]
#>  colorspace      1.4-1      2019-03-18 [1]
#>  colourpicker    1.0        2017-09-27 [1]
#>  crayon          1.3.4      2017-09-16 [1]
#>  crosstalk       1.0.0      2016-12-21 [1]
#>  DBI             1.1.0      2019-12-15 [1]
#>  dbplyr          1.4.2      2019-06-17 [1]
#>  desc            1.2.0      2018-05-01 [1]
#>  devtools        2.2.2      2020-02-17 [1]
#>  dials         * 0.0.4      2019-12-02 [1]
#>  DiceDesign      1.8-1      2019-07-31 [1]
#>  digest          0.6.24     2020-02-12 [1]
#>  dplyr         * 0.8.4      2020-01-31 [1]
#>  DT              0.12       2020-02-05 [1]
#>  dygraphs        1.1.1.6    2018-07-11 [1]
#>  ellipsis        0.3.0      2019-09-20 [1]
#>  evaluate        0.14       2019-05-28 [1]
#>  fansi           0.4.1      2020-01-08 [1]
#>  fastmap         1.0.1      2019-10-08 [1]
#>  forcats       * 0.4.0      2019-02-17 [1]
#>  foreach         1.4.8      2020-02-09 [1]
#>  fs              1.3.1      2019-05-06 [1]
#>  furrr           0.1.0      2018-05-16 [1]
#>  future          1.16.0     2020-01-16 [1]
#>  generics        0.0.2      2019-12-26 [1]
#>  ggplot2       * 3.2.1      2019-08-10 [1]
#>  ggridges        0.5.2      2020-01-12 [1]
#>  globals         0.12.5     2019-12-07 [1]
#>  glue            1.3.1      2019-03-12 [1]
#>  gower           0.2.1      2019-05-14 [1]
#>  GPfit           1.0-8      2019-02-08 [1]
#>  gridExtra       2.3        2017-09-09 [1]
#>  gtable          0.3.0      2019-03-25 [1]
#>  gtools          3.8.1      2018-06-26 [1]
#>  haven           2.2.0      2019-11-08 [1]
#>  highr           0.8        2019-03-20 [1]
#>  hms             0.5.3      2020-01-08 [1]
#>  htmltools       0.4.0      2019-10-04 [1]
#>  htmlwidgets     1.5.1      2019-10-08 [1]
#>  httpuv          1.5.2      2019-09-11 [1]
#>  httr            1.4.1      2019-08-05 [1]
#>  igraph          1.2.4.2    2019-11-27 [1]
#>  infer         * 0.5.1      2019-11-19 [1]
#>  inline          0.3.15     2018-05-18 [1]
#>  ipred           0.9-9      2019-04-28 [1]
#>  iterators       1.0.12     2019-07-26 [1]
#>  janeaustenr     0.1.5      2017-06-10 [1]
#>  jsonlite        1.6.1      2020-02-02 [1]
#>  kknn          * 1.3.1      2016-03-26 [1]
#>  knitr           1.28       2020-02-06 [1]
#>  later           1.0.0      2019-10-04 [1]
#>  lattice         0.20-38    2018-11-04 [2]
#>  lava            1.6.6      2019-08-01 [1]
#>  lazyeval        0.2.2      2019-03-15 [1]
#>  lhs             1.0.1      2019-02-03 [1]
#>  lifecycle       0.1.0      2019-08-01 [1]
#>  listenv         0.8.0      2019-12-05 [1]
#>  lme4            1.1-21     2019-03-05 [1]
#>  loo             2.2.0      2019-12-19 [1]
#>  lubridate       1.7.4      2018-04-11 [1]
#>  magrittr        1.5        2014-11-22 [1]
#>  markdown        1.1        2019-08-07 [1]
#>  MASS            7.3-51.4   2019-03-31 [2]
#>  Matrix          1.2-18     2019-11-27 [2]
#>  matrixStats     0.55.0     2019-09-07 [1]
#>  memoise         1.1.0      2017-04-21 [1]
#>  mime            0.8        2019-12-19 [1]
#>  miniUI          0.1.1.1    2018-05-18 [1]
#>  minqa           1.2.4      2014-10-09 [1]
#>  modelr          0.1.5      2019-08-08 [1]
#>  munsell         0.5.0      2018-06-12 [1]
#>  nlme            3.1-142    2019-11-07 [2]
#>  nloptr          1.2.1      2018-10-03 [1]
#>  nnet            7.3-12     2016-02-02 [2]
#>  parsnip       * 0.0.5.9000 2020-02-19 [1]
#>  pillar          1.4.3      2019-12-20 [1]
#>  pkgbuild        1.0.6      2019-10-09 [1]
#>  pkgconfig       2.0.3      2019-09-22 [1]
#>  pkgload         1.0.2      2018-10-29 [1]
#>  plyr            1.8.5      2019-12-10 [1]
#>  prettyunits     1.1.1      2020-01-24 [1]
#>  pROC            1.16.1     2020-01-14 [1]
#>  processx        3.4.1      2019-07-18 [1]
#>  prodlim         2019.11.13 2019-11-17 [1]
#>  promises        1.1.0      2019-10-04 [1]
#>  ps              1.3.0      2018-12-21 [1]
#>  purrr         * 0.3.3      2019-10-18 [1]
#>  R6              2.4.1      2019-11-12 [1]
#>  Rcpp            1.0.3      2019-11-08 [1]
#>  readr         * 1.3.1      2018-12-21 [1]
#>  readxl          1.3.1      2019-03-13 [1]
#>  recipes       * 0.1.9      2020-01-07 [1]
#>  remotes         2.1.1      2020-02-15 [1]
#>  reprex          0.3.0      2019-05-16 [1]
#>  reshape2        1.4.3      2017-12-11 [1]
#>  rlang           0.4.4      2020-01-28 [1]
#>  rmarkdown       1.18       2019-11-27 [1]
#>  rpart           4.1-15     2019-04-12 [2]
#>  rprojroot       1.3-2      2018-01-03 [1]
#>  rsample       * 0.0.5      2019-07-12 [1]
#>  rsconnect       0.8.16     2019-12-13 [1]
#>  rstan           2.19.2     2019-07-09 [1]
#>  rstanarm        2.19.2     2019-10-03 [1]
#>  rstantools      2.0.0      2019-09-15 [1]
#>  rstudioapi      0.10       2019-03-19 [1]
#>  rvest           0.3.5      2019-11-08 [1]
#>  scales        * 1.1.0      2019-11-18 [1]
#>  sessioninfo     1.1.1      2018-11-05 [1]
#>  shiny           1.4.0      2019-10-10 [1]
#>  shinyjs         1.0        2018-01-08 [1]
#>  shinystan       2.5.0      2018-05-01 [1]
#>  shinythemes     1.1.2      2018-11-06 [1]
#>  SnowballC       0.6.0      2019-01-15 [1]
#>  StanHeaders     2.19.0     2019-09-07 [1]
#>  stringi         1.4.6      2020-02-17 [1]
#>  stringr       * 1.4.0      2019-02-10 [1]
#>  survival        3.1-8      2019-12-03 [2]
#>  testthat        2.3.1      2019-12-01 [1]
#>  threejs         0.3.1      2017-08-13 [1]
#>  tibble        * 2.1.3      2019-06-06 [1]
#>  tidymodels    * 0.0.3      2019-10-04 [1]
#>  tidyposterior   0.0.2      2018-11-15 [1]
#>  tidypredict     0.4.3      2019-09-03 [1]
#>  tidyr         * 1.0.2      2020-01-24 [1]
#>  tidyselect      1.0.0      2020-01-27 [1]
#>  tidytext        0.2.2      2019-07-29 [1]
#>  tidyverse     * 1.3.0      2019-11-21 [1]
#>  timeDate        3043.102   2018-02-21 [1]
#>  tokenizers      0.2.1      2018-03-29 [1]
#>  usethis         1.5.1      2019-07-04 [1]
#>  vctrs           0.2.2      2020-01-24 [1]
#>  withr           2.1.2      2018-03-15 [1]
#>  workflows       0.1.0.9000 2020-02-19 [1]
#>  xfun            0.11       2019-11-12 [1]
#>  xml2            1.2.2      2019-08-09 [1]
#>  xtable          1.8-4      2019-04-21 [1]
#>  xts             0.11-2     2018-11-05 [1]
#>  yaml            2.2.0      2018-07-25 [1]
#>  yardstick     * 0.0.5      2020-01-23 [1]
#>  zoo             1.8-7      2020-01-10 [1]
#>  source                                   
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.0)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  Github (r-lib/cli@e9f041e)               
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  Github (tidymodels/modelgenerics@c15ac43)
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.0)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  Github (tidymodels/parsnip@d992fc0)      
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.0)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.0)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.1)                           
#>  Github (tidymodels/workflows@c89bc0c)    
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.1)                           
#>  CRAN (R 3.6.0)                           
#>  CRAN (R 3.6.2)                           
#>  CRAN (R 3.6.2)                           
#> 
#> [1] C:/Users/BobMu/OneDrive/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-3.6.2/library

Created on 2020-02-20 by the reprex package (v0.3.0)

@juliasilge
Copy link
Member

I have also seen this issue. Related to #251? 🤔

@github-actions
Copy link

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.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants