fit_resamples and fit error

I got the error message "Error: For a classification model, the outcome should be a factor." after I use fit_resamples. Then I switched to the fit() function and got the same error. Does anyone know why this is?

train_boot <- bootstraps(train)
train_boot
glm_spec <- logistic_reg() %>% 
  set_engine("glm")

rf_spec <- rand_forest() %>% 
  set_mode("classification") %>% 
  set_engine("ranger")
train_wf <- workflow() %>% 
  add_formula(OFFER_STATUS~.)

train_wf
glm_rs <- train_wf %>% 
  add_model(glm_spec) %>% 
  fit_resamples(
    resamples = train_boot, 
    control = control_resamples(save_pred = TRUE, verbose = TRUE)
  )

Modify train$ORDER_STATUS with

train$ORDER_STATUS <- as.factor(train$ORDER_STATUS)

and retry.

Thank you very much! I have realized the problem and added the code as follows:

train$OFFER_STATUS <- as.factor(train$OFFER_STATUS)
rec <- recipe(OFFER_STATUS~., data = train) %>% 
  update_role(ID, new_role = "ID")  %>%
  # impute all other numeric columns with their mean
  step_impute_mean(all_numeric_predictors()) %>% 
    # determine what happens when a new nominal value is encountered in test data (which was missing from the training set)
  step_novel(all_nominal(), -has_role("ID"), new_level="new") %>% 
  # impute all other nominal (character + factor) columns with the value "none"
  step_unknown(all_nominal(), new_level = "none") %>% 

  # convert all strings to factors
  step_string2factor(all_nominal(), -all_outcomes(), -has_role("ID")) %>% 
  # remove constant columns
  step_zv(all_predictors())

rec %>% prep()

But I got a new error message which is "Error in glm.fit(x = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, : NA/NaN/Inf in 'x'"

Do you know why it is?

The x argument to the glm.fit function contains one or more values that are missing, not a number or, possibly, entered as infinite. This is usually a problem with missing values.

See the FAQ: How to do a minimal reproducible example reprex for beginners for how to provide representative data to go along with required libraries and code used to the point that the error arises and more specific suggestions will be possible.

Thank you!!! I'll look into it :grinning_face_with_smiling_eyes:

1 Like