Skip to content

predict.textmodel_nb error when newdata has different features from the model #1329

Closed

Description

@koheiw

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) # works

nb <- 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

Activity

  1. self-assigned this
    on Apr 30, 2018
  2. 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
  3. koheiw commented on Apr 30, 2018

    @koheiw
    CollaboratorAuthor

    predict.textmodel_nb() also returns all the parameters in a list,

    > predict(nb)
    $log.posterior.lik
                  1         2
    text1 -5.205379 -7.284821
    text2 -7.284821 -5.205379
    
    $posterior.prob
                  1         2
    text1 0.8888889 0.1111111
    text2 0.1111111 0.8888889
    
    $nb.predicted
    [1] "1" "2"
    
    $Pc
      1   2 
    0.5 0.5 
    
    $classlabels
    [1] "1" "2"
    
    $call
    predict.textmodel_nb(object = nb)
    
    

    but it should be a named vector unless requested by the user via arguments.

    > predict(nb)
        text1 text2
    [1]     1     2
    Levels: 1 2
    
  4. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    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.

    ws <- textmodel_wordscores(data_dfm_lbgexample, c(-1.5, NA, NA, NA, .75, NA))
    length(coef(ws)) # 34
    nfeat(ws$x) # 37
  5. kbenoit commented on May 3, 2018

    @kbenoit
    Collaborator

    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.

  6. kbenoit commented on May 3, 2018

    @kbenoit
    Collaborator

    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:

    We add the argument

    predict.textmodel_nb(x, type = c("class", "posterior.prob", "log.posterior.lik", )

    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.

  7. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    It looks like this now. I am happy if predict() returns a vector by default:

    > predict(nb)
    text1 text2 
        1     2 
    Levels: 1 2
    > predict(nb, type = "posterior.prob")
    $posterior.prob
                  1         2
    text1 0.8888889 0.1111111
    text2 0.1111111 0.8888889
    
    $classlabels
    [1] "1" "2"
    
    > predict(nb, type = "log.posterior.lik")
    $log.posterior.lik
                  1         2
    text1 -5.205379 -7.284821
    text2 -7.284821 -5.205379
    
    $classlabels
    [1] "1" "2"
    
    
  8. kbenoit commented on May 3, 2018

    @kbenoit
    Collaborator

    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.

    > ws <- textmodel_wordscores(data_dfm_lbgexample, c(seq(-1.5, 1.5, .75), NA))
    > predict(ws)
               R1            R2            R3            R4            R5            V1 
    -1.317931e+00 -7.395598e-01 -8.673617e-18  7.395598e-01  1.317931e+00 -4.480591e-01 
    > predict(ws, se.fit = TRUE)
    $fit
               R1            R2            R3            R4            R5            V1 
    -1.317931e+00 -7.395598e-01 -8.673617e-18  7.395598e-01  1.317931e+00 -4.480591e-01 
    
    $se.fit
    [1] 0.006699613 0.011433605 0.012005250 0.011433605 0.006699613 0.011897667
    
    > predict(ws, se.fit = FALSE, interval = "confidence")
    $fit
                 fit         lwr         upr
    R1 -1.317931e+00 -1.33106234 -1.30480034
    R2 -7.395598e-01 -0.76196925 -0.71715035
    R3 -8.673617e-18 -0.02352986  0.02352986
    R4  7.395598e-01  0.71715035  0.76196925
    R5  1.317931e+00  1.30480034  1.33106234
    V1 -4.480591e-01 -0.47137808 -0.42474008
    
    > predict(ws, se.fit = TRUE, interval = "confidence")
    $fit
                 fit         lwr         upr
    R1 -1.317931e+00 -1.33106234 -1.30480034
    R2 -7.395598e-01 -0.76196925 -0.71715035
    R3 -8.673617e-18 -0.02352986  0.02352986
    R4  7.395598e-01  0.71715035  0.76196925
    R5  1.317931e+00  1.30480034  1.33106234
    V1 -4.480591e-01 -0.47137808 -0.42474008
    
    $se.fit
    [1] 0.006699613 0.011433605 0.012005250 0.011433605 0.006699613 0.011897667
  9. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    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?

    > predict(gl, type = "link")
              1           2           3           4           5           6           7           8 
    0.008139409 0.018774446 0.024995544 0.029409483 0.035630581 0.040044520 0.046265617 0.050679556 
              9 
    0.054103274 
    > predict(gl, type = "response")
            1         2         3         4         5         6         7         8         9 
    122.85904  53.26389  40.00713  34.00264  28.06578  24.97221  21.61432  19.73182  18.48317 
    > predict(gl, type = "term")
            log(u)
    1 -0.026087527
    2 -0.015452491
    3 -0.009231393
    4 -0.004817454
    5  0.001403644
    6  0.005817583
    7  0.012038681
    8  0.016452620
    9  0.019876337
    attr(,"constant")
    [1] 0.03422694
  10. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    @kbenoit , you are more than welcome to edit the code.

  11. kbenoit commented on May 3, 2018

    @kbenoit
    Collaborator

    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 = .

  12. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    That fine, but we have to kill these classes c("predict.textmodel_nb", "list") to return matrix objects.

  13. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    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(wf, interval = "confidence")
    $fit
               fit         lwr         upr
    R1 -1.29056981 -1.31895837 -1.26218126
    R2 -0.61643907 -0.63720710 -0.59567103
    R3  0.03350982  0.01223686  0.05478279
    R4  0.70961244  0.68581641  0.73340848
    R5  1.52333687  1.48915160  1.55752213
    V1 -0.35945025 -0.38042330 -0.33847721
  14. kbenoit commented on May 3, 2018

    @kbenoit
    Collaborator

    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.

    > wf <- textmodel_wordfish(data_dfm_lbgexample, dir = c(1,5))
    > predict(wf)
             R1          R2          R3          R4          R5          V1 
    -1.29056981 -0.61643907  0.03350982  0.70961244  1.52333687 -0.35945025 
    > predict(wf, se.fit = TRUE)
    $fit
             R1          R2          R3          R4          R5          V1 
    -1.29056981 -0.61643907  0.03350982  0.70961244  1.52333687 -0.35945025 
    
    $se.fit
    [1] 0.01448422 0.01059613 0.01085375 0.01214106 0.01744178 0.01070073
    
    > predict(wf, interval = "confidence")
    $fit
               fit         lwr         upr
    R1 -1.29056981 -1.31895837 -1.26218126
    R2 -0.61643907 -0.63720710 -0.59567103
    R3  0.03350982  0.01223686  0.05478279
    R4  0.70961244  0.68581641  0.73340848
    R5  1.52333687  1.48915160  1.55752213
    V1 -0.35945025 -0.38042330 -0.33847721
  15. koheiw commented on May 3, 2018

    @koheiw
    CollaboratorAuthor

    Then we are done with predict.textmodel_nb().

  16. added a commit that references this issue on May 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions