I don't have a NA value still i am getting error that NAS are not allowed in subscripted assignments

I am using a dataset from this link (https://www.utsc.utoronto.ca/~butler/c32/ais.txt) where I am having the error

Error in Y[keep, ] <- Y1 : NAs are not allowed in subscripted assignments
Calls: ... eval_with_user_handlers -> eval -> eval -> predict -> predict.multinom
Execution halted

data_url <- "https://www.utsc.utoronto.ca/~butler/c32/ais.txt"
athlete_data <- read.table(data_url, header = TRUE)
sport_played <- multinom(athlete_data$Sport ~ athlete_data$Ht + athlete_data$Wt, data = athlete_data, maxit = 200)
Hts <- c(160, 180, 200)
Wts <- c(50, 75, 100)
combinations <- crossing(Ht = Hts, Wt = Wts)
combinations
p <- predict(sport_played, combinations, type = "probs")
cbind(combinations, p)

If your issue revolves around the use of a particular package; please please please, say what package you are using.

multinum appears both in mgcv package, as well as nnet; perhaps others; I assume that you are using nnet.

Your problem boils down to overzealous use of $ syntax within multinom formula; formulas are great because they can assume the context; the context of the data = you gave. At best in R functions that take formulas the repetition of some_data_source$x etc. serves no advantage, but as in your case, it can often literally cause the functions to bug out.

Here is a working version :

library(nnet) # for multinom()
library(tidyverse) # for crossing()

data_url <- "https://www.utsc.utoronto.ca/~butler/c32/ais.txt"
athlete_data <- read.table(data_url, header = TRUE)
sport_played <- multinom(Sport ~ Ht + Wt, data = athlete_data, maxit = 200)
Hts <- c(160, 180, 200)
Wts <- c(50, 75, 100)
combinations <- crossing(Ht = Hts, Wt = Wts)
combinations
p <- predict(sport_played, combinations, type = "probs")
cbind(combinations, p)

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.