Skip to content

cubist: a factor predictor produces an unevaluable formula #322

Description

@EmilHvitfeldt

A Cubist::cubist() model fitted with a factor predictor produces a formula that R cannot evaluate. The split condition keeps the quoted column name and gets no threshold:

set.seed(1)
n <- 120
df <- data.frame(
  y = rnorm(n),
  x = rnorm(n),
  f = factor(sample(c("a", "b", "c", "d"), n, TRUE))
)
df$y <- df$y + as.numeric(df$f) + 0.5 * df$x

mf <- model.frame(y ~ x + f, df)
model <- Cubist::cubist(x = mf[, -1, drop = FALSE], y = mf[[1]])

tidypredict_fit(model)
#> (ifelse(`"f"` <= NA_real_, 1.384984 + x * 0.58, 0) + ifelse(`"f"` <=
#>     NA_real_, 3.7233071 + x * 0.55, 0))/((`"f"` <= NA_real_) +
#>     (`"f"` <= NA_real_))

rlang::eval_tidy(tidypredict_fit(model), df)
#> Error: object '"f"' not found

Two things go wrong in the same place: the column name is carried through with its surrounding quotes, so it is backtick-quoted into a name that does not exist, and the categorical split value never becomes a threshold, so it is NA_real_. Cubist writes categorical rules as f in {a, b} style conditions rather than as a numeric cut, and the parser appears to read them with the numeric path.

Found by sweeping a 4-level unordered factor across every supported regression class rather than one model at a time. The other 15 classes in that sweep either matched their own predict() to ~1e-15 or failed in a way already tracked (#282 randomForest, #283 ranger); this one has no existing issue. #285 and #286 are about the extrapolation clamp and committee assignment and do not cover it.

Every categorical test in test-model-cubist.R goes through the x/y interface with numeric predictors only, which is why the suite is green.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions