rmultinomial

Predicting multinom model in R - NAs are not allowed in subscripted assignments


I'm having an issue with a recurring error in a multinomial model I'm trying to create. Specifically, the error occurs when trying to predict test data with the trained model. The test and training data are split from the same dataframe, so everything about them is similar except the length of the data.

The following is what I have so far:

model = multinom(train$type ~ train$`12)OD280/OD315 of diluted wines` + train$`6) Total phenols`, na.action=na.omit)
model

model.pred = predict(model, newdata=train, na.action=na.omit)
model.pred

pred = predict(model, test, na.action=na.omit)

The last line produces the following error:

Error in Y[keep, ] <- Y1 : NAs are not allowed in subscripted assignments

I've been crawling the forums for solutions, and though there are a few similar threads, none of the solutions thus far have alleviated the issue. Any insight into this error or potential solutions that would allow me to predict my data would be immensely helpful.

Thank you!


Solution

  • Placeholder “answer”: It appears that one or more of my comments answered the question, so I will come back and assemble an answer to fill out this space after searching for prior answers that deal with formulas and evaluation environments.

    I think debugging would be a lot easier if you temporarily ran those dataframes through check.names. Furthermore you are violating the cardinal rule of no "$"'s in formulas. (That might be where you are going wrong. Use a data= argument.)

    I spend a fair amount of time searching for a duplicate and will reference some of the "good stuff" that I found but have not really found an answer that seems to make the points I was planning to describe. There's an additional point to be made regarding constructing Surv objects for use in survival regression formulas that is similar in its reasoning. Still haven had time to work on the body of hte answer though.