Skip to content

Predicted output coming wrong for textmodel_nb prediction #476

Description

@vibhutittu

I was working on naive bayes for text classification using your package. I was using textmodel_NB , for which you fixed a issue for the wrong priors using docfreq. Now its coming correct but the predictions should also be changed Please see the below example as mentioned in your package:

library(quanteda)
trainingset <- as.dfm(matrix(c(1, 2, 0, 0, 0, 0,
                               0, 2, 0, 0, 1, 0,
                               0, 1, 0, 1, 0, 0,
                               0, 1, 1, 0, 0, 1,
                               0, 3, 1, 0, 0, 1), 
                             ncol=6, nrow=5, byrow=TRUE,
                             dimnames = list(docs = paste("d", 1:5, sep = ""),
                                             features = c("Beijing", "Chinese",  "Japan", "Macao", 
                                                          "Shanghai", "Tokyo"))))
trainingclass <- factor(c("Y", "Y", "Y", "N", NA), ordered = TRUE)
## replicate IIR p261 prediction for test set (document 5)
(nb.p261 <- textmodel_NB(trainingset, trainingclass,prior="docfreq"))
predict(nb.p261, newdata = trainingset[5, ])

Output:

Fitted Naive Bayes model:
Call:
	textmodel_NB(x = trainingset, y = trainingclass, prior = "docfreq")


Training classes and priors:
   N    Y 
0.25 0.75 

		  Likelihoods:		Class Posteriors:
6 x 4 Matrix of class "dgeMatrix"
                  Y         N          Y         N
Beijing  0.14285714 0.1111111 0.30000000 0.7000000
Chinese  0.42857143 0.2222222 0.39130435 0.6086957
Japan    0.07142857 0.2222222 0.09677419 0.9032258
Macao    0.14285714 0.1111111 0.30000000 0.7000000
Shanghai 0.14285714 0.1111111 0.30000000 0.7000000
Tokyo    0.07142857 0.2222222 0.09677419 0.9032258

This is coming correct now

Predicted textmodel of type: Naive Bayes

       lp(N)     lp(Y)     Pr(N)  Pr(Y) Predicted
**d5 -9.206303 -7.808069    0.1981 0.8019         N**

The prediction should be Y as Pr(Y)>Pr(N) but its is giving N

Please fix it to get the correct predictions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions