title: Training Model in Caret Using F1 Metric
body: <p>I am trying to fit a random forest model to my dataset and I would like to select the best model based off of the F1 score. I saw a post <a href="https://stackoverflow.com/questions/37666516/caret-package-custom-metric">here</a> describing the code necessary. I attempted to copy the code but I am getting the error </p>
  
  <blockquote>
    <p>"Error in { : task 1 failed - "could not find function "F1_Score" </p>
  </blockquote>
  
  <p>while I run the train function. (FYI the variable I am trying to predict ("pass") is a two class factor "Fail" and "Pass")</p>
  
  <p>See Code Below:</p>
  
  <pre><code>library(MLmetrics)
  library(caret)
  library(doSNOW)
  
  f1 &lt;- function(data, lev = NULL, model = NULL) {
    f1_val &lt;- F1_Score(y_pred = data$pred, y_true = data$obs, positive = lev[1])
    c(F1 = f1_val)
  }
  
  
  
  train.control &lt;- trainControl(method = "repeatedcv",
                                number = 10,
                                repeats = 3,
                                classProbs = TRUE,
                                summaryFunction = f1,
                                search = "grid")
  
  
  tune.grid &lt;- expand.grid(.mtry = seq(from = 1, to = 10, by = 1))
  
  
  cl &lt;- makeCluster(3, type = "SOCK")
  registerDoSNOW(cl)
  random.forest.orig &lt;- train(pass ~ manufacturer+meter.type+premise+size+age+avg.winter+totalizer, 
                       data = meter.train,
                       method = "rf",
                       tuneGrid = tune.grid,
                       metric = "F1",
                       weights = model_weights,
                       trControl = train.control)
  stopCluster(cl)
  </code></pre>