Multinom with a graph matrix as an answer

According to multinom , package nnet , β€œThe answer must be a factor or matrix with K columns that will be interpreted as counters for each of the K classes.” I tried to use this function in the second case, getting an error.

Here is an example of the code I'm doing:

 response <- matrix(round(runif(200,0,1)*100),ncol=20) # 10x20 matrix of counts predictor <- runif(10,0,1) fit1 <- multinom(response ~ predictor) weights1 <- predict(fit1, newdata = 0.5, "probs") 

Here I get:

 'newdata' had 1 row but variables found have 10 rows 

How can I solve this problem?

Bonus question: I also noticed that we can use multinom with a predictor of factors, for example. predictor <- factor(c(1,2,2,3,1,2,3,3,1,2)) . I cannot understand how this is mathematically possible, given that a multi-linear linear logical regression should work only with continuous or dichotomous predictors.

+3
r multinomial nnet
source share
1 answer

The easiest way to get predictions for a new variable is to define the new data as data.frame.

Using sample code

 > predict(fit1, newdata = data.frame(predictor = 0.5), type = "probs") [1] 0.07231972 0.05604055 0.05932186 0.07318140 0.03980245 0.06785690 0.03951593 0.02663618 [9] 0.04490844 0.04683919 0.02298260 0.04801870 0.05559221 0.04209283 0.03799946 0.06406533 [17] 0.04509723 0.02197840 0.06686314 0.06888748 
0
source share

All Articles