Skip to content

Add contr_one_hot function for one-hot encoding support #218

@EmilHvitfeldt

Description

@EmilHvitfeldt

Summary

The convert_data.R file references contr_one_hot in local_one_hot_contrasts() (line 142), but this function is not defined in tidyclust. This means the indicators = "one_hot" code path in .convert_form_to_x_fit() and .convert_form_to_x_new() is currently broken.

Current behavior

data <- data.frame(x = 1:10, cat = factor(rep(c("a", "b"), 5)))
.convert_form_to_x_fit(~., data, indicators = "one_hot")
#> Error: object 'contr_one_hot' of mode 'function' was not found

Options

  1. Import from hardhat: The contr_one_hot function exists in the hardhat package. We could import it.

  2. Define locally: Copy/adapt the function into tidyclust.

  3. Remove the code path: If no engine uses indicators = "one_hot", consider removing this dead code.

Context

  • Currently no engine in tidyclust uses predictor_indicators = "one_hot" encoding
  • Tests for one-hot paths are currently skipped
  • This was discovered during test coverage improvements

Related code

# R/convert_data.R lines 140-145
local_one_hot_contrasts <- function(frame = rlang::caller_env()) {
contrasts <- getOption("contrasts")
  contrasts["unordered"] <- "contr_one_hot"  # <- function doesn't exist
  rlang::local_options(contrasts = contrasts, .frame = frame)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    upkeepmaintenance, infrastructure, and similar

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions