Need help getting method = cforest to work within train() from caret using leave one out cross-validation

Viewed 81

examples_dataset.csv

I have tried looking up so many ways to fix this issue, but no solution so far. I am trying to train conditional inference forests with caret, using the leave one out cross-validation method. I have about 20 (larger) datasets to run this method on, hence the functions to automate some.

A lot of what I have found suggests that my QuantBins are not factors, but I have checked after running prep_df() on the df and those are indeed factors. I get an error when running the conditional inference forests (cif_model()), but not with random forests (rf_model()). The output from trying to make that model is "Something is wrong; all the Accuracy metric values are missing" (pictured below).

Any help and guidance is appreciated!

  ## Example code 
  ## GOAL: create train() code from caret that uses conditional inference forests to assess variable importance with categorical dependent variable using leave one out cross validation
 rm(list=ls())

setwd()

ex.all <- read.csv("examples_dataset.csv", header = TRUE)

loo_ctrl <- trainControl(method = "LOOCV")

#This function works!
rf_model <- function(file.name) {
  model <- train(QuantBins ~ F_Cou + B_Cou + Height + GBH + N_b + N_f + L_u + D_w + N_p 
+ P_Cou, data = file.name, method = "rf", trControl = loo_ctrl, tuneLength = 10, control = 
rpart.control(minbucket = 10), ntree = 50)
  return(model)
}

#This does not.
cif_model <- function(file.name) {
  model <- train(QuantBins ~ F_Cou + B_Cou + Height + GBH + N_b + N_f + L_u + D_w + N_p 
+ P_Cou, data = file.name, method = "cforest", trControl = loo_ctrl, tuneLength = 10, control 
= ctree_control(minbucket = 10), ntree = 50)
  return(model)
}

##### functions used #####
prep_df <- function(file.name) {
  file.name$BINARY <- ifelse(file.name$TOTAL >= 1, "yes", "no")
  file.name$BINARY <- as.factor(file.name$BINARY)
  file.name$L_u <- as.factor(file.name$L_u)
  file.name$TOTAL <- as.numeric(file.name$TOTAL)

  ## Quantile distribution of breaks in Total Fruit
  numbers_of_bins = 5 #this will return four groups
  file.name <- file.name %>% mutate(QuantBins = cut(TOTAL, breaks = unique(quantile(TOTAL,         
probs=seq.int(0,1, by=1/numbers_of_bins))), include.lowest=TRUE))
  print(length(levels(file.name$QuantBins)))
  temp <- levels(file.name$QuantBins)
  file.name$QuantBins <- as.character(file.name$QuantBins)
  
  for(i in 1:length(file.name$QuantBins)) {
    temp1 <- strsplit(file.name$QuantBins[i], ",")
    temp2 <- strsplit(temp1[[1]][1], "\\(")
    temp3 <- strsplit(temp1[[1]][[2]], "\\]")
    file.name$QuantBins[i] <- paste("Fruit", temp2[[1]][2], "to", temp3[[1]][1])
  }
  
  file.name$QuantBins <- as.factor(file.name$QuantBins)
  file.name$QuantBins <- droplevels(file.name$QuantBins)
  print(length(levels(file.name$QuantBins)))
  
  return(file.name)
}

##### running trees #####
ex.all <- prep_df(ex.all)

ex.rf <- rf_model(ex.all)
print(ex.rf)
ex.rf
ex.rf$finalModel$importance

ex.cf <- cif_model(ex.all)
print(ex.cf)
ex.cf
ex.cf$finalModel$importance

Error using cif_model(ex.all) showing "Something is wrong; all the Accuracy metric values are missing"

0 Answers
Related