Ariana Johnson
Ariana Johnson

Reputation: 11

How do I fix the "Error in validate_function_class():" within Tidymodels when trying to explore Random Forest Metrics

`# Create a split object
train_test_split <-
  rsample::initial_split(
    data = nomissingprep,     
    prop = 0.80   
  ) 
# Split the data and build a training and testing data set
train_test_split <- rsample::initial_split(data = nomissingprep,prop = 0.80) 
train.data <- train_test_split %>% training() 
test.data  <- train_test_split %>% testing()

## Recipe Creation
rec <- recipe(preprecentyear ~ ., data = train.data)


## Validation Set

cv_folds <-
  vfold_cv(train.data, 
           v = 5, 
           strata = preprecentyear) 

## Model Fitting -- Random Forest 

library(ranger)
rf_spec <- 
  rand_forest() %>% 
  set_engine("ranger", importance = "impurity") %>% 
  set_mode("classification")

## Workflow --Random Forest 
rf_wflow <-
  workflow() %>%
  add_recipe(rec) %>% 
  add_model(rf_spec) 

##Random Forest Metrics
rf_res <-
  rf_wflow %>% 
  fit_resamples(
    resamples = cv_folds, 
    metrics = metric_set(
      recall, precision, f_meas, 
      accuracy, kap,
      roc_auc, sens, spec),
    control = control_resamples(save_pred = TRUE)
  )

`

Error in validate_function_class(): ! The combination of metric functions must be:

The following metric function types are being mixed:

I am unsure of how to fix this error. All other code prior to the Random Forest Metrics fit well. Any advice is more than welcome. Thanks

Upvotes: 1

Views: 1002

Answers (1)

Gr5yHawk
Gr5yHawk

Reputation: 1

log_res <- 
  tidymodels::tidymodels_prefer( log_wflow %>% 
  fit_resamples(
    resamples = cross_validation, 
    metrics = metric_set(
    kap, sensitivity, specificity, precision_vec,  recall_vec),
    control = control_resamples(
      save_pred = TRUE)
  ) 
  )  
log_res 

Try it this way.

Upvotes: 0

Related Questions