R error: all arguments must have the same length

For some reason, the default predict(efit, test_data, type = "class") doesn’t work in this case (probably because your model predicts 0 for all observations in the test dataset). You also need to construct the table using your outcome (i.e. test_data[,ncol(test_data)] returns euribor3m). The following should work:

pre <- predict(efit, test_data, type = "raw") %>%
  as.data.frame() %>%
  mutate(prediction = if_else(0 < 1, 0, 1)) %>%
  pull(prediction)

bayes_table <- table(pre, test_data$y)

accuracy_test_bayes <- sum(diag(bayes_table)) / sum(bayes_table)

list('predict matrix' = bayes_table, 'accuracy' = accuracy_test_bayes)
# $`predict matrix`
#    
# pre    0    1
#   0 7282  956
# 
# $accuracy
# [1] 0.8839524

Leave a Comment