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`)