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

kknn engine fails with factors without calling library(kknn) #410

Closed
jjcurtin opened this issue Jan 13, 2021 · 4 comments
Closed

kknn engine fails with factors without calling library(kknn) #410

jjcurtin opened this issue Jan 13, 2021 · 4 comments

Comments

@jjcurtin
Copy link

jjcurtin commented Jan 13, 2021

There appears to be a problem when using nearest_neighbor() and the kknn engine with factors that have not been converted to dummy features using a recipe. By default, train.kknn() will convert factors to dummy coded features (see its contrasts argument). However, it uses its own contrast function to do this - contr.dummy(). This function is not available when using nearest_neighbor() unless the user explicitly also loads the kknn package.

The code below reproduces this error. If you add library(kknn) at the top of the code, the error is resolved. It seems that contr.dummy() needs to be available when using tidymodels with kknn engine. Thanks for looking into this

library(tidymodels)
# library(kknn) # uncomment this line to resolve the error below

# simulate data
set.seed(1969)
train <- tibble(x = factor(c(rep(0, 20), rep(1, 20), rep(2, 20))),
                y = rnorm(60, 0, 50))

algo <-
  nearest_neighbor() %>%   
  set_engine("kknn") %>%   
  set_mode("regression")   

rec <- 
  recipe(y ~ x, data = train)

fit <- 
  workflow() %>% 
  add_model(algo) %>% 
  add_recipe(rec) %>% 
  fit(data = train)

# Error in get(ctr, mode = "function", envir = parent.frame()) : 
#   object 'contr.dummy' of mode 'function' was not found
@juliasilge
Copy link
Member

This is the same as #264. Do you mind adding any new info you have to that issue, instead of opening a new one? And/or adding a ➕ or 👍 ?

@topepo
Copy link
Member

topepo commented Jan 13, 2021

This is a problem with that specific package. See KlausVigo/kknn#16. I have a few issues and PRs there (some from 2018) but no action to date.

@jjcurtin
Copy link
Author

jjcurtin commented Jan 14, 2021 via email

@github-actions
Copy link

github-actions bot commented Mar 6, 2021

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 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants