[R] Help please with error from nnet::multinom
David Winsemius
dwinsemius at comcast.net
Sun Jun 26 22:57:51 CEST 2016
> On Jun 26, 2016, at 1:03 PM, Lars Bishop <lars52r at gmail.com> wrote:
>
> Thanks, David. Sorry, do you mean this?
>
> library(nnet)
> set.seed(1)
> ysim <- gl(3, 100)
> y <- model.matrix(~ysim -1)
> X <- matrix( 3 * runif(length(ysim)), nrow = 300, ncol = 3)
> X_new <- matrix( 3 * runif(length(ysim)), nrow = 200, ncol = 3)
> fit <- multinom(y ~ X, trace = FALSE)
> pred <- predict(fit, setNames(data.frame(X_new),c("X1","X2","X3") ), type = "probs")
>
> Error in predict.multinom(fit, setNames(data.frame(X_new), c("X1", "X2", :
> NAs are not allowed in subscripted assignments
> In addition: Warning message:
> 'newdata' had 200 rows but variables found have 300 rows
Apparently I mixed some of your original code with some of my newer code that created dataframes. Using `str(fit)` we see that the model object does recognize that the argument named "X" is a matrix although it also considers the 'coefnames' to be: chr [1:4] "(Intercept)" "X1" "X2" "X3"
The $ terms :Classes 'terms', 'formula' language y ~ X
.. ..- attr(*, "variables")= language list(y, X)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y" "X"
.. .. .. ..$ : chr "X"
.. ..- attr(*, "term.labels")= chr "X"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y, X)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "nmatrix.3" "nmatrix.3"
.. .. ..- attr(*, "names")= chr [1:2] "y" "X"
$ weights : num [1:300, 1] 1 1 1 1 1 1 1 1 1 1 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:300] "1" "2" "3" "4" ...
.. ..$ : NULL
$ deviance : num 653
$ rank : int 2
$ lab : chr [1:3] "ysim1" "ysim2" "ysim3"
$ coefnames : chr [1:4] "(Intercept)" "X1" "X2" "X3"
$ vcoefnames : chr [1:4] "(Intercept)" "X1" "X2" "X3"
I haven't found a newdata argument that It may be that is is so difficult to match the various assignments that predict.multinom cannot be convinced that a new argument has all the correct attributes. This is the code that is used:
else {
newdata <- as.data.frame(newdata)
rn <- row.names(newdata)
Terms <- delete.response(object$terms)
m <- model.frame(Terms, newdata, na.action = na.omit,
xlev = object$xlevels)
if (!is.null(cl <- attr(Terms, "dataClasses")))
.checkMFClasses(cl, m)
keep <- match(row.names(m), rn)
X <- model.matrix(Terms, m, contrasts = object$contrasts)
Y1 <- predict.nnet(object, X)
Y <- matrix(NA, nrow(newdata), ncol(Y1), dimnames = list(rn,
colnames(Y1)))
Y[keep, ] <- Y1
}
I failed with both these:
> str( setNames(as.data.frame(X_new),c("X1","X2","X3") ) )
'data.frame': 200 obs. of 3 variables:
$ X1: num 2.021 0.285 1.478 1.385 1.126 ...
$ X2: num 1.66 2.06 1.97 1.99 1.42 ...
$ X3: num 1.977 0.555 2.863 2.694 2.831 ...
> str( data.matrix( setNames(as.data.frame(X_new),c("X1","X2","X3") ) ) )
num [1:200, 1:3] 2.021 0.285 1.478 1.385 1.126 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:3] "X1" "X2" "X3"
>
> On Sun, Jun 26, 2016 at 3:46 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> > On Jun 26, 2016, at 12:39 PM, Lars Bishop <lars52r at gmail.com> wrote:
> >
> > Many thanks David. That works. Looks then this error will always occur in predict.multinom whenever the data argument is missing in the mutlinom fit, but the data argument is optional as per documentation.
>
> I don't agree with that analysis. The problem occurs because of a mismatch of names in the new data argument. With your original code this runs without error:
>
> pred <- predict(fit, setNames(data.frame(X_new),c("X1","X2","X3") ), type = "probs")
>
> --
> David.
> >
> > Best,
> > Lars.
> >
> > On Sun, Jun 26, 2016 at 3:14 PM, David Winsemius <dwinsemius at comcast.net> wrote:
> >
> > > On Jun 26, 2016, at 11:32 AM, Lars Bishop <lars52r at gmail.com> wrote:
> > >
> > > Thanks Bert.
> > >
> > > But I it doesn't complain when predict is used on X instead of X_new
> > > (using nnet_7.3-12), which is even more puzzling to me:
> > >
> > > pred <- predict(fit, X, type = "probs")
> >
> > Indeed: There is a predict.multinom function and it does have 'probs' as an acceptable argument to type:
> >
> > I got success (or at least an absence of an error message) with:
> >
> > #----------
> > X <- data.frame(matrix( 3 * runif(length(ysim)), nrow = 300, ncol = 3))
> > X_new <- data.frame(matrix( 3 * runif(length(ysim)), nrow = 200, ncol = 3))
> > str(X)
> >
> > 'data.frame': 300 obs. of 3 variables:
> > $ X1: num 0.797 1.116 1.719 2.725 0.605 ...
> > $ X2: num 0.797 1.116 1.719 2.725 0.605 ...
> > $ X3: num 0.797 1.116 1.719 2.725 0.605 ...
> >
> > fit <- multinom(y ~ ., data=X, trace = FALSE)
> > pred <- predict(fit, setNames(X_new, names(X)), type = "probs")
> >
> > > head(pred)
> > ysim1 ysim2 ysim3
> > 1 0.3519378 0.3517418 0.2963204
> > 2 0.3135513 0.3138573 0.3725915
> > 3 0.3603779 0.3600461 0.2795759
> > 4 0.3572297 0.3569498 0.2858206
> > 5 0.3481512 0.3480128 0.3038360
> > 6 0.3813310 0.3806118 0.2380572
> >
> > #------------
> >
> >
> > > head(pred)
> > > ysim1 ysim2 ysim3
> > > 1 0.3059421 0.3063284 0.3877295
> > > 2 0.3200219 0.3202551 0.3597230
> > > 3 0.3452414 0.3451460 0.3096125
> > > 4 0.3827077 0.3819603 0.2353320
> > > 5 0.2973288 0.2977994 0.4048718
> > > 6 0.3817027 0.3809759 0.2373214
> > >
> > > Thanks again,
> > > Lars.
> > >
> > >
> > > On Sun, Jun 26, 2016 at 1:05 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
> > >
> > >> Well, for one thing, there is no "probs" method for predict.nnet, at
> > >> least in my version: nnet_7.3-12
> > >>
> > >> Cheers,
> > >> Bert
> > >>
> > >>
> > >> Bert Gunter
> > >>
> > >> "The trouble with having an open mind is that people keep coming along
> > >> and sticking things into it."
> > >> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> > >>
> > >>
> > >> On Sun, Jun 26, 2016 at 9:27 AM, Lars Bishop <lars52r at gmail.com> wrote:
> > >>> Hello,
> > >>>
> > >>> I'd appreciate your help in spotting the reason for the error and warning
> > >>> messages below.
> > >>>
> > >>> library(nnet)
> > >>> set.seed(1)
> > >>> ysim <- gl(3, 100)
> > >>> y <- model.matrix(~ysim -1)
> > >>> X <- matrix( 3 * runif(length(ysim)), nrow = 300, ncol = 3)
> > >>> X_new <- matrix( 3 * runif(length(ysim)), nrow = 200, ncol = 3)
> > >>>
> > >>> fit <- multinom(y ~ X, trace = FALSE)
> > >>> pred <- predict(fit, X_new, type = "probs")
> > >>>
> > >>> Error in predict.multinom(fit, X_new, type = "probs") :
> > >>> NAs are not allowed in subscripted assignments
> > >>> In addition: Warning message:
> > >>> 'newdata' had 200 rows but variables found have 300 rows
> > >>>
> > >>> Thanks,
> > >>> Lars.
> > >>>
> > >>> [[alternative HTML version deleted]]
> > >>>
> > >>> ______________________________________________
> > >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > >>> https://stat.ethz.ch/mailman/listinfo/r-help
> > >>> PLEASE do read the posting guide
> > >> http://www.R-project.org/posting-guide.html
> > >>> and provide commented, minimal, self-contained, reproducible code.
> > >>
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> >
> > David Winsemius
> > Alameda, CA, USA
> >
> >
>
> David Winsemius
> Alameda, CA, USA
>
>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list