Skip to content

Poor error when x has no column names #398

@DavisVaughan

Description

@DavisVaughan
  library(parsnip)
  set.seed(1234)
  predictrs <- matrix(rnorm(100*20), ncol = 20)
  response <- as.factor(sample(1:4, 100, replace = TRUE))
  fit <- multinom_reg() %>%
    set_engine("glmnet") %>%
    fit_xy(x = predictrs, y = response)
#> Error in glmnet::glmnet(x = maybe_matrix(x), y = y, family = "multinomial"): x should be a matrix with 2 or more columns
#> Timing stopped at: 0.002 0 0.002

Found while looking at some butcher test failures

Traced the issue to here:

parsnip/R/fit_helpers.R

Lines 69 to 71 in 375e310

if (remove_intercept) {
env$x <- env$x[, colnames(env$x) != "(Intercept)", drop = FALSE]
}

because there are no column names, colnames(env$x) != "(Intercept)" evaluates to logical(0), so we select 0 columns here. So then we eventually get the error message of x should be a matrix with 2 or more columns

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions