santoku
santoku

Reputation: 3427

how to deal with NA in neural network prediction result in R

I'm applying neuralnet on titanic dataset (containing PClass, sex, Age, Sibsp, Parch, Fare, Embarked)

library(caret)
model_nnet <- train(as.factor(Survived) ~.,  
              method="nnet",
              train_df, 
              linout=FALSE, 
              trace = FALSE,
              preProcess = c("center", "scale"))

nnet_predict <- predict(model_nnet, test_df)

While I expected nnet_predict to be same length as testing dataframe (418 records), it actually contains NA and only have 331 results. Any advice on how to deal with it? Thank you

Upvotes: 0

Views: 2041

Answers (1)

Prem
Prem

Reputation: 11955

Look for

summary(test_df)

You can see that there are missing values in Age & Fare column so before running predict() function you need to fix NA in these two columns.

One option could be to -

  • Fill NA in Fare column with it's mean value.
  • Fill NA in Age column with it's mean value wrt Pclass i.e.

if Pclass==1 then missing_age <- 37
if Pclass==2 then missing_age <- 29
else missing_age <- 24

Hope this helps!

Upvotes: 1

Related Questions