You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
predict.textmodel_nb error when newdata has different features from the model #1329
I think predict.textmodel_wordscores() is correct (and more user-friendly).
require(quanteda)
mt1<- dfm(c(text1="a b c", text2="d e f"))
mt2<- dfm(c(text3="a b c", text4="d e f g"))
ws<- textmodel_wordscores(mt1, 1:2)
predict(ws, newdata=mt2) # worksnb<- textmodel_nb(mt1, factor(1:2))
predict(nb, newdata=mt2) # error# Error in predict.textmodel_nb(nb, newdata = mt2) : # feature set in newdata different from that in training set
changed the title [-]predict.textmodel_nb error when newdata has differet features from the model[/-][+]predict.textmodel_nb error when newdata has different features from the model[/+]on Apr 30, 2018
I have added force = TRUE/FALSE as an argument to make newdata conformant, but there is a problem with textmodel_wordscores(). The model terms are not conformant to the feature set when reference scores have NA. In this case predict() never work with force = TRUE even when newdata is not given.
We probably need to drop documents corresponds to NA before saving or disallow NA in reference score entirely.
Simpler solution is to make predict.textmodel_wordscores(x, force = TRUE) by default, which keeps the current behaviour. It can be FALSE for other models. The reason we don't have the same problem with textmodel_nb() is because it applies smoothing, which is not applied by default in wordscores.
As for the return object for predict.textmodel_nb(), we should output a named vector as with the other predict methods, and make this overall similar to predict.glm(). This means:
corresponding to $nb.predicted, $posterior.prob, and $log.posterior.lik, respectively. It would be nice to store the priors ($Pc) also but that's part of the fitted model, so we can omit that, and the call, and the $classlabels.
I'm not crazy about these names (with the . notation) so open to ideas about changing them.
We should match the behaviours of the "Mighty Morphin'"predict.lm() and predict.glm(), for better or worse.
Default: return a named vector
se.fit = TRUE: return a list with the default as the first element, and a second vector (not named) with SEs as the second element.
interval = "confidence": convert the default named vector into a matrix (see below).
Unfortunately (for R in general) this depends on whether se.fit = TRUE and whether intervals are requested. If both are FALSE, it's a vector; if se.fit = TRUE, it's a list, if interval = "confidence" (e.g.) then it's a matrix.
There is predict.glm() output. The predict.textmodel_nb() should return named factor vector because the values are categorical, but I have no opinion on how other two should look like. Just return the matrix?
If we are going to make the output a matrix, we have to change wordfish as well, but it affects many users, so list(posterior.prob =matrix()) seems to be the safe option.
predict.textmodel_wordfish() and predict.textmodel_wordscores() works exactly as they should should already. What we need to do is make predict.textmodel_nb() do the same.
Activity
[-]predict.textmodel_nb error when newdata has differet features from the model[/-][+]predict.textmodel_nb error when newdata has different features from the model[/+]koheiw commentedon Apr 30, 2018
predict.textmodel_nb()also returns all the parameters in a list,but it should be a named vector unless requested by the user via arguments.
koheiw commentedon May 3, 2018
I have added
force = TRUE/FALSEas an argument to makenewdataconformant, but there is a problem withtextmodel_wordscores(). The model terms are not conformant to the feature set when reference scores haveNA. In this casepredict()never work withforce = TRUEeven whennewdatais not given.We probably need to drop documents corresponds to
NAbefore saving or disallowNAin reference score entirely.kbenoit commentedon May 3, 2018
Simpler solution is to make
predict.textmodel_wordscores(x, force = TRUE)by default, which keeps the current behaviour. It can beFALSEfor other models. The reason we don't have the same problem withtextmodel_nb()is because it applies smoothing, which is not applied by default in wordscores.kbenoit commentedon May 3, 2018
As for the return object for
predict.textmodel_nb(), we should output a named vector as with the other predict methods, and make this overall similar topredict.glm(). This means:We add the argument
corresponding to
$nb.predicted,$posterior.prob, and$log.posterior.lik, respectively. It would be nice to store the priors ($Pc) also but that's part of the fitted model, so we can omit that, and the call, and the$classlabels.I'm not crazy about these names (with the . notation) so open to ideas about changing them.
koheiw commentedon May 3, 2018
It looks like this now. I am happy if
predict()returns a vector by default:kbenoit commentedon May 3, 2018
We should match the behaviours of the "Mighty Morphin'"
predict.lm()andpredict.glm(), for better or worse.Default: return a named vector
se.fit = TRUE: return a list with the default as the first element, and a second vector (not named) with SEs as the second element.interval = "confidence": convert the default named vector into a matrix (see below).Unfortunately (for R in general) this depends on whether se.fit = TRUE and whether intervals are requested. If both are FALSE, it's a vector; if se.fit = TRUE, it's a list, if interval = "confidence" (e.g.) then it's a matrix.
koheiw commentedon May 3, 2018
There is
predict.glm()output. Thepredict.textmodel_nb()should return named factor vector because the values are categorical, but I have no opinion on how other two should look like. Just return the matrix?koheiw commentedon May 3, 2018
@kbenoit , you are more than welcome to edit the code.
kbenoit commentedon May 3, 2018
Let's return the existing
$nb.predicted,$posterior.prob, and$log.posterior.lik. Only the first is a vector; the others are matrix objects.One at a time, I mean, depending on the value of
type =.koheiw commentedon May 3, 2018
That fine, but we have to kill these classes
c("predict.textmodel_nb", "list")to return matrix objects.koheiw commentedon May 3, 2018
If we are going to make the output a matrix, we have to change wordfish as well, but it affects many users, so
list(posterior.prob =matrix())seems to be the safe option.kbenoit commentedon May 3, 2018
predict.textmodel_wordfish()andpredict.textmodel_wordscores()works exactly as they should should already. What we need to do is makepredict.textmodel_nb()do the same.koheiw commentedon May 3, 2018
Then we are done with
predict.textmodel_nb().Update news for #1329 fix