0

I am trying to get a confusion matrix for my neural network, but I am getting an error: Error in table(data, reference, dnn = dnn, ...) : all arguments must have the same length. There shouldn't be any null values as I removed them before I performed the neural network. I can see that my prediction.net1 has a length of 82 using length(prediction.net1). My df.valid1 has a length of 27 using length(df.valid1). How can I fix this error so that I can view the confusion matrix?

attribute$Number.of.Dining.and.Beverage.Options[is.na(attribute$Number.of.Dining.and.Beverage.Options)] <- 0
attribute$Number.of.Highways[is.na(attribute$Number.of.Highways)] <- 0
attribute$Number.of.Historical.Properties[is.na(attribute$Number.of.Historical.Properties)] <- 0
attribute$Number.of.Entertainment.Options[is.na(attribute$Number.of.Entertainment.Options)] <- 0
attribute$Number.of.Medical.Facilities[is.na(attribute$Number.of.Medical.Facilities)] <- 0
attribute$Number.of.Offices[is.na(attribute$Number.of.Offices)] <- 0
attribute$Number.of.Parks[is.na(attribute$Number.of.Parks)] <- 0
attribute$Number.of.Rail.Roads[is.na(attribute$Number.of.Rail.Roads)] <- 0
attribute$Number.of.Educational.Institutions[is.na(attribute$Number.of.Educational.Institutions)] <- 0
attribute$Number.of.Shops[is.na(attribute$Number.of.Shops)] <- 0
attribute$Number.of.Bus.Stops[is.na(attribute$Number.of.Bus.Stops)] <- 0

str(attribute)

#Creating new data frames for each property type
One_Bed_Property <- attribute %>% select(c(Number.of.Dining.and.Beverage.Options, 
                                           Number.of.Highways,
                                           Number.of.Historical.Properties, 
                                           Number.of.Entertainment.Options,
                                           Number.of.Medical.Facilities,
                                           Number.of.Offices,
                                           Number.of.Parks,
                                           Number.of.Rail.Roads,
                                           Number.of.Educational.Institutions,
                                           Number.of.Shops,
                                           Number.of.Bus.Stops,
                                           Proximity_to_Uptown,
                                           Proximity_to_Light_Rail,
                                           `1_Bed_Target`))

#Dropping NA's from Target Variable
One_Bed_Property1 <- na.omit(One_Bed_Property)

str(One_Bed_Property1)

#Select numeric variables
one_bed_processed <- One_Bed_Property1[,c(0:13)]

#Scale numeric variables
maxs1 <- apply(one_bed_processed,2,max)
mins1 <- apply(one_bed_processed,2,min)

one_bed_processed1 <- as.data.frame(scale(one_bed_processed, 
                                         center = mins1, 
                                         scale=maxs1 - mins1))

one_bed_processed2 <- cbind(one_bed_processed1, One_Bed_Property1)

#Data partition

trainIndex1 <- createDataPartition(one_bed_processed2$`1_Bed_Target`,
                                   p=0.7,
                                   list=FALSE,
                                   times=1)

df.train1 <- one_bed_processed2[trainIndex1,]
df.valid1 <- one_bed_processed2[-trainIndex1,]



#Train neural net
nn1 <- neuralnet(`1_Bed_Target`~ Number.of.Dining.and.Beverage.Options 
                 + Number.of.Highways 
                 + Number.of.Historical.Properties 
                 + Number.of.Entertainment.Options 
                 + Number.of.Medical.Facilities 
                 + Number.of.Offices 
                 + Number.of.Parks
                 + Number.of.Rail.Roads
                 + Number.of.Educational.Institutions
                 + Number.of.Shops
                 + Number.of.Bus.Stops
                 + Proximity_to_Uptown
                 + Proximity_to_Light_Rail, data = df.train1, hidden=c(5,2), linear.output = FALSE)

plot(nn1)

#Model evaluation 
#Changing DV data types
df.valid1$`1_Bed_Target` <- as.factor(df.valid1$`1_Bed_Target`)

prediction.net1 <- predict(nn1, newdata = df.valid1)

prediction.net1 <- ifelse(prediction.net1>0.5,1,0)


confusionMatrix(as.factor(prediction.net1),df.valid1$`1_Bed_Target`)
  • When you run `length` on a data frame it tells you how many columns it has. What is `length(df.valid1$\`1_Bed_Target)\``? – SamR Apr 17 '22 at 11:43
  • @SamR it is showing [1] 41 – Chris McManus Apr 17 '22 at 11:47
  • There is your problem then - you have predicted a vector of exactly twice the length of your target. It would be easier if you posted a reproducible example so we could see what is going on. In the absence of that, have a look at `prediction.net1` when it is created - does it predict two probabilities per row, e.g. P(0) and P(1), or something like that? – SamR Apr 17 '22 at 11:50
  • Try to include that is both *minimal* and reproducible, as described [here](https://stackoverflow.com/help/minimal-reproducible-example) to improve your chances of getting help. Good luck! – jpsmith Apr 17 '22 at 11:52
  • @SamR when its created, two columns appear "V1" and "V2". I believe those are the two probabilities that you are referring to. – Chris McManus Apr 17 '22 at 11:54
  • OK so just pick the greater of your probabilities, something like `preds = ifelse(prediction.net1$V1 > prediction.net1$V2, 0, 1)`, and then feed that to your confusion matrix. – SamR Apr 17 '22 at 11:57
  • @SamR I get this error "Error in prediction.net1$V1 : $ operator is invalid for atomic vectors" – Chris McManus Apr 17 '22 at 12:03
  • I'm afraid I can't help you more without a reproducible example. – SamR Apr 17 '22 at 12:06
  • @SamR how can I post a reproducible example? Do you need the files that I am using? The script is from a professor of mine. – Chris McManus Apr 17 '22 at 12:07
  • https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – SamR Apr 17 '22 at 12:14
  • @SamR, I figured it out! I made the vectors into data frames using data.frame(prediction.net1), then used what you suggested in preds =. Then passed that through confusionmatrix. Thank you for your help!! – Chris McManus Apr 17 '22 at 13:18

0 Answers0