Confusion Matrix - Error Message - Neuralnet Library

Hi All,

I get an error message "all arguments must have the same length" when I run this code.

Would it be possible for someone to show me how to overcome this error, given `test$y,' and 'pred_label' both have the same input length?

I am currently struggling to generate a confusion matrix (ironically) from this data, and all help would be appreciated.

library(neuralnet)
library(tidyverse)


#MOCK DATA

x1 = rep(1:3, times = 40)
x2 = rep(1:3, times = 40)
x3 = rep(1:3, times = 40)
x4 = rep(1:3, times = 40)
x5 = rep(1:3, times = 40)

y = rep(0:1, times = 60)
y <- as.factor(y)

dat <- data.frame(y, x1, x2, x3, x4, x5)

#SPLIT

set.seed(123)

indexes=createDataPartition(dat$y, p=.85, list = F)
train = dat[indexes, ]
test = dat[-indexes, ] 

xtest = test[, -1]
ytest = test[, 1]

#MODEL

NN1 <- neuralnet(y ~., train,
                 linear.output = FALSE,
                 stepmax=1e7)


#ACCURACY TEST

pred <- predict(NN1, test)
labels <- c("0", "1")
pred_label <- data.frame(max.col(pred)) %>%     
  mutate(pred=labels[max.col.pred.]) %>%
  select(2) %>%
  unlist()

table(test$y, prediction_label)

I am currently following this tutorial. https://www.datacamp.com/tutorial/neural-network-models-r

From the code you provided I do not get '"all arguments must have the same length" '
do you when you run exactly and only the code you shared ?

first I get an error that could not find function "createDataPartition"
I fix this by adding library(caret) and rerun the code again ...
object 'prediction_label' not found
this is a typo and i change prediction_label to pred_label
the result is

   pred_label
    0 1
  0 3 6
  1 6 3

Hi @nirgrahamuk . Thanks for your response. You're right. I forgot to attach Caret.

Attached below is a new code that seems to work. I am currently struggling with generating an AUC from the NN1 model, however. I wondered if you could help out in that regard, if possible?

Would be appreciated.

library(tidyverse)
library(caret)

#MOCK DATA
x1 = rep(1:3, times = 40)
x2 = rep(1:3, times = 40)
x3 = rep(1:3, times = 40)
x4 = rep(1:3, times = 40)
x5 = rep(1:3, times = 40)

y = rep(0:1, times = 60)
y <- as.factor(y)

dat <- data.frame(y, x1, x2, x3, x4, x5)

#SPLIT
set.seed(123)
indexes=createDataPartition(dat$y, p=.85, list = F)
train = dat[indexes, ]
test = dat[-indexes, ] 

#MODEL
NN1 <- neuralnet(y ~., train,
                 linear.output = FALSE,
                 stepmax=1e7)


#ACCURACY TEST
pred <- predict(NN1, newdata = test)
colnames(pred) <- c("0", "1")
tab <- table(test$y, colnames(pred)[max.col(pred)])
confusionMatrix(tab)
library(ModelMetrics)
ModelMetrics::auc(actual=test$y,
                  predicted=as.integer(colnames(pred)[max.col(pred)]))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.