rdplyrensemble-learningvoting-system

How to build a basic conditional voting model in R


I'm trying to produce a basic voting model with conditions in R as part of an ensemble ML model. I ran a logistic regression and svm. I would like the voting model to parse the recommendation.

The the logistic model predicts true negatives more effectively. The SVM model predicts true positives more effectively. I have reduced my predicted factors 1s / 0s and am trying basic addition with conditionality.

logistic <- as.numeric(predictions.logistic)
logistic <- logistic - 1  

svm <- as.numeric(predictions.svm)
svm <- svm - 1

add <- logistic + svm
ensemble <- data.frame(logistic, svm, add)

Here are my first two attempts -

Attempt 1 -- the attempt clearly doesn't work

ensemble$predict <- 
  
    if (ensemble$add == 0) {1} 
    else if (ensemble$add == 2) {1}
    else if (ensemble$add == 1 & logistic == 0) {0}
    else if (ensemble$add == 1 & svm == 1) {1}

Attempt 2 -- this attempt raises no flags in R studio:

if (ensemble$add == 0) {ensemble$predict <- 0} 
if (ensemble$add == 2) {ensemble$predict <- 1}
if (ensemble$add == 1 && logistic == 0) {ensemble$predict <- 0}
if (ensemble$add == 1 && svm == 1) {ensemble$predict <- 1}

I continue to get an error indicating: "Error in if (ensemble$add == 0) { : the condition has length > 1"

Having googled this I am unsure of how this error applies. Is there a more effective way to build the voting model possible by using dplyr?

Attempt 3:

mutate(ensemble, predict = case_when(
    sum = 2 ~ 1,
    sum = 0 ~ 0,
    sum = 1 & logistic = 1 ~ 1,
    sum = 1 & svm = 1 ~ 0))

Output:

Error: unexpected '=' in:
"    sum = 0 ~ 0,
    sum = 1 & logistic ="

Solution

  • mutate needed:

    
    mutate(ensemble, predict = case_when(
        sum == 2 ~ 1,
        sum == 0 ~ 0,
        sum == 1 & logistic == 1 ~ 1,
        sum == 1 & svm == 1 ~ 0))