Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when predicting probabilities with multinom and one observation #827

Closed
YvesCR opened this issue Feb 6, 2018 · 1 comment
Closed

Comments

@YvesCR
Copy link

@YvesCR YvesCR commented Feb 6, 2018

When predicting probability from a multinom model, if there is only one observation in the new data set, the predict function of the train class is flashing an error.

Reproductible exemple:

pacman::p_load(nnet, caret)

training <- iris[-1, ]
testing <- iris[1, ]
testing2 <- iris[1:2, ]

fitControl <- trainControl(## 4-fold CV
  method = "cv",
  number = 4)

set.seed(1789)
multinomFit1 <- train(Species ~ ., data = training, 
                 method = "multinom", 
                 trControl = fitControl)
multinomFit1

# ok
predict(multinomFit1, newdata = training, type = "prob")
# ok
predict(multinomFit1, newdata = testing2, type = "prob")
# not ok
predict(multinomFit1, newdata = testing, type = "prob")

Error:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [2] not equal to array extent

I guess it comes from the fact that output of predict.nnet in case of multiple observations is a matrix whereas in the case of one observation it is a named vector.

Session Info:

R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252
[2] LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] caret_6.0-76 ggplot2_2.2.1 lattice_0.20-35 nnet_7.3-12
[5] MASS_7.3-47

loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 magrittr_1.5 splines_3.4.1
[4] munsell_0.4.3 colorspace_1.3-2 rlang_0.1.2
[7] foreach_1.4.3 minqa_1.2.4 stringr_1.2.0
[10] car_2.1-5 plyr_1.8.4 tools_3.4.1
[13] parallel_3.4.1 pbkrtest_0.4-7 grid_3.4.1
[16] gtable_0.2.0 nlme_3.1-131 mgcv_1.8-17
[19] pacman_0.4.6 quantreg_5.33 e1071_1.6-8
[22] class_7.3-14 MatrixModels_0.4-1 iterators_1.0.8
[25] lme4_1.1-13 lazyeval_0.2.0 tibble_1.3.3
[28] Matrix_1.2-10 nloptr_1.0.4 reshape2_1.4.2
[31] ModelMetrics_1.1.0 codetools_0.2-15 stringi_1.1.5
[34] compiler_3.4.1 scales_0.5.0 stats4_3.4.1
[37] SparseM_1.77

topepo added a commit that referenced this issue Feb 6, 2018
@topepo
Copy link
Owner

@topepo topepo commented Feb 6, 2018

That should do it but please test on your data. If you don't want to install from github, you can source the file in models/files/multinom.R and use method = modelInfo.

@YvesCR YvesCR closed this Feb 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.