-2

I am trying a classification with Random Forest in R I have a training data set that has a complexityFlag that is 1 or 0 and I am training my model on the data set using Random Forest:

model1 <- randomForest(as.factor(ComplexityFlag) ~ ContractTypeCode + IndustryLevel2Description + ClaimantAgeAtDisability + Sex, data = data, ntree = 200, importance=TRUE)

Then I want run the model against my test data set, but my test data set does not have the ComplexityFlag. I want the model to predict the ComplexityFlag like so :

test$ComplexityFlag <- as.data.frame(predict(model1, newdata = test, type = "class"))

how can i calculate the ROC am I using the correct approach

Jawahar
  • 183
  • 4
  • 16

1 Answers1

-1

For the ROC curve you can use pROC package. You just need to make sure that predictions is as.numeric().

Here I show it in an example, I reproduced a binary classification problem with the iris data.

data <- iris

# change the problem to a binary classifier (setosa or not setosa)
data$bin_response <- as.factor(ifelse(data$Species=="setosa", 1, 0))
data <- data[, -5] # remove "Species"

set.seed(123)

train_test <- sample(150, 100, replace = F) # we sample casually 100 values for the train

# split train-test data
train <- data[train_test, ]
test <- data[-train_test, ]

Now the model and the curve:

# - model
library(randomForest)

rf_mod <- randomForest(bin_response ~ ., data=train)

# make pred on test data
predictions <- predict(rf_mod, newdata = test[, -5]) # note we remove the "bin_response" col
head(predictions) # lets look at them to check if it's fine
# 2  4 10 13 19 21 
# 1  1  1  1  1  1 
# Levels: 0 1

# now the ROC curve
library(pROC)

roc_result <- roc(test$bin_response, as.numeric(predictions))# Draw ROC curve.
plot(roc_result, print.thres="best", print.thres.best.method="closest.topleft")

enter image description here

RLave
  • 8,144
  • 3
  • 21
  • 37