I am getting the following error when using the predict on a model that predicts probability of choosing a set of binary mutually exclusive outcomes. Using the multinom function of the nnet package.
Error in predict.multinom(model_name, df.predict, "probs") : NAs are not allowed in subscripted assignments In addition: Warning message: 'newdata' had 5 rows but variables found have 100 rows
Here is a reproducible example:
require(nnet)
response1 <- sample(runif(100))
response2 <- 1-response1
responses <- as.matrix(data.frame(response1 = response1, response2 = response2))
train <- data.matrix(data.frame(var1 = runif(100), var2 = runif(100)))
multinom.mod <- multinom(responses ~ train)
test.df <- data.frame(var1 = runif(5), var2 = runif(5))
predict.vec <- predict(multinom.mod, test.df)
As you can see, the problem is that my response consists of 2 variables. It appears than when I predict on a smaller number of rows than in the training set, the function tried to join the response variables from the training set with the test set.
UPDATE:
The following works with a new predict set. However, the response variables are being treated as categorical variables and so the prediction is incorrect:
require(nnet)
train <- data.frame(response1 = sample(runif(100)), response2 = 1-response1, var1 = runif(100), var2 = runif(100))
multinom.mod <- multinom(response1 + response2 ~ ., train, type = "probs")
test.df <- data.frame(var1 = runif(5), var2 = runif(5))
predict.vec <- predict(multinom.mod, test.df)