Search code examples
rdataframeloopspredictionconfusion-matrix

R: Rearranging code / Loops for Confusion Matrix


I am basically trying to work a confusion matrix with some prediction / response variables under a couple of conditions, but im not sure how to lay out the code.

for my prediction values, my current code is:

Prediction <- factor (rep(data$percentage <=40.0 || data$binary ==1,levels=c("TRUE","FALSE")))

but I am pretty sure this is incorrect because what I want to achieve is the following conditions:

  • if percentage is <=40 then consider this as TRUE
  • but if percentage is shown as NA then refer to data$binary column
  • based on this, if data$binary = 1, then consider this as TRUE.

Any help would be much appreciated !


Solution

  • not to hard code, I worked directly on your data frame;

    library(dplyr)
    
    data %>%
    mutate(logistic_column=rowSums(data)) %>% 
    mutate(prediction=ifelse(is.na(logistic_column),
                             ifelse(binary==1,TRUE,FALSE),
                             ifelse(percentage<=40,TRUE,FALSE))) %>% 
    select(prediction) %>% 
    pull -> prediction