title: NAs are not allowed in subscripted assignments: error in predicting probability
body: <p>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.</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>Here is a reproducible example:</p>
  
  <pre><code>require(nnet)
  
  response1 &lt;- sample(runif(100))
  response2 &lt;- 1-response1
  responses &lt;- as.matrix(data.frame(response1 = response1, response2 = response2))
  
  train &lt;- data.matrix(data.frame(var1 = runif(100), var2 = runif(100)))
  
  multinom.mod &lt;- multinom(responses ~ train)
  
  test.df &lt;- data.frame(var1 = runif(5), var2 = runif(5))
  predict.vec &lt;- predict(multinom.mod, test.df)
  </code></pre>
  
  <p>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.</p>
  
  <p>UPDATE:</p>
  
  <p>The following works with a new predict set. However, the response variables are being treated as categorical variables and so the prediction is incorrect:                                               </p>
  
  <pre><code>require(nnet)
  
  train &lt;- data.frame(response1 = sample(runif(100)), response2 = 1-response1, var1 = runif(100), var2 = runif(100))
  
  multinom.mod &lt;- multinom(response1 + response2 ~ ., train, type = "probs")
  
  test.df &lt;- data.frame(var1 = runif(5), var2 = runif(5))
  
  predict.vec &lt;- predict(multinom.mod, test.df)
  </code></pre>