0
votes

I am currently trying to build a neural network to predict what rank people within the data will place.

The Rank system is: A,B,C,D,E

Everything runs very smoothly until I get to my confusion matrix. I get the error "Error: data and reference should be factors with the same levels.". I have tried many different methods on other posts but none seem to work.

The levels are both the same in NNPredicitions and test$Rank. I checked them both with table().

library(readxl)
library(caret)
library(neuralnet)
library(forecast)
library(tidyverse)
library(ggplot2)



Indirect <-read_excel("C:/Users/Abdulazizs/Desktop/Projects/Indirect/FIltered Indirect.xlsx", 
    n_max = 500)

Indirect$Direct_or_Indirect <- NULL


Indirect$parentaccount <- NULL


sum(is.na(Indirect))


counts <- table(Indirect$Rank)



barplot(counts)

summary(counts)



part2 <- createDataPartition(Indirect$Rank, times = 1, p = .8, list = FALSE, groups = min(5, length(Indirect$Rank)))

train <- Indirect[part2, ]
test <- Indirect[-part2, ]

set.seed(1234)

TrainingParameters <- trainControl(method = "repeatedcv", number = 10, repeats=10)

as.data.frame(train)
as.data.frame(test)

NNModel <- train(train[,-7], train$Rank,
                  method = "nnet",
                  trControl= TrainingParameters,
                  preProcess=c("scale","center"),
                  na.action = na.omit
)

NNPredictions <-predict(NNModel, test, type = "raw")



summary(NNPredictions)





confusionMatrix(NNPredictions, test$Rank)

length(NNPredictions) length(test$Rank)

length(NNPredictions) [1] 98 length(test$Rank) [1] 98

table(NNPredictions, test$Rank, useNA="ifany") NNPredictions A B C D E A 1 0 0 0 0 B 0 6 0 0 0 C 0 0 11 0 0 D 0 0 0 18 0 E 0 0 0 0 62

1
What does table(NNPredictions, test$Rank, useNA="ifany") show you?dcarlson
@dclarson It Shows: Error in table(NNPredictions, test$Rank, useNA = "ifany") : all arguments must have the same lengthShahidAziz
That is your problem. What is length(NNPredictions) and length(test$Rank)? They have to be the same. If they are not you probably have missing values in train$Rank.dcarlson
I changed type = "prob" to type = "raw". Checked the lengths and both have 98 levels.ShahidAziz
Changing it to "raw" allowed me to use table(NNPredictions, test$Rank, useNA="ifany"). results are listed aboveShahidAziz

1 Answers

0
votes

Also change method = "prob" to method = "raw"

Table1 <- table(NNPredictions, test$Rank, useNA = "ifany")

cnf1 <- confusionMatrix(Table1)

Answered provided by dclarson