Search code examples
rsvmr-carettext-classificationtext2vec

Text2Vec classification with caret problems


Some context: Working with text classification and big sparse matrices in R

I have been working on a text multi-class classification problem with the text2vec package and caret. The plan is to use text2vec for building the document-term matrix, prune vocabulary and all sorts of pre-processing stuff, and then try different models with caret but I can't get results as when training, caret throws some errors that look like the following:

+ Fold02.Rep1: cost=0.25 
predictions failed for Fold01.Rep1: cost=0.25 Error in as.vector(data) : 
no method for coercing this S4 class to a vector

This happens for all the folds and repetitions. I suposse there is a problem when converting the document-term matrix that text2vec produces to a vector because caret needs to do some calculations, but I am honestly not sure and that is the main reason for this question.

The code used, with some skipped parts, looks as following. Note that I feed caret with the direct result of the document-term matrix that text2vec returns and I am not completely sure this is ok.

library(text2vec)
library(caret)
data("movie_review")
train = movie_review[1:4000, ]
test = movie_review[4001:5000, ]

it <- itoken(train$review, preprocess_function = tolower, tokenizer = word_tokenizer)
vocab <- create_vocabulary(it, stopwords = tokenizers::stopwords())
pruned_vocab <- prune_vocabulary(vocab, term_count_min = 10, doc_proportion_max = 0.5, doc_proportion_min = 0.001)

vectorizer <- vocab_vectorizer(pruned_vocab)
it = itoken(train$review, tokenizer = word_tokenizer, ids = train$id)
dtm_train = create_dtm(it, vectorizer)
it = itoken(test$review, tokenizer = word_tokenizer, ids = test$id)
dtm_test = create_dtm(it, vectorizer)

ctrl.svm.1 <- trainControl(method="repeatedcv",
                           number=10,
                           repeats=5,
                           summaryFunction = multiClassSummary,
                           verboseIter = TRUE)

fit.svm.1 <- train(x = dtm_train, y= as.factor(train$sentiment), 
                   method="svmLinear2",  
                   metric="Accuracy", 
                   trControl = ctrl.svm.1, 
                   scale = FALSE, verbose = TRUE)

As I said, the problem appears when launching the train() function. The dtm_train object is of class:

[1] "dgCMatrix"
attr(,"package")
[1] "Matrix"

And the structure looks like this:

str(dtm_train)
> Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:368047] 2582 2995 3879 3233 2118 2416 2468 2471 3044 3669 ...
  ..@ p       : int [1:6566] 0 0 3 4 4 10 10 14 14 22 ...
  ..@ Dim     : int [1:2] 4000 6565
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:4000] "5814_8" "2381_9" "7759_3" "3630_4" ...
  .. ..$ : chr [1:6565] "floriane" "lil" "elm" "kolchak" ...
  ..@ x       : num [1:368047] 1 1 1 1 1 1 2 2 1 3 ...
  ..@ factors : list()

What am I doing wrong? Why is caret unable to work with this kind of data if in the documentation it implies that is able to?


Solution

  • Íf you turn your S4 class dtm_train into a simple matrix the code will work.

    fit.svm.1 <- train(x = as.matrix(dtm_train), y= as.factor(train$sentiment), 
                       method="svmLinear2",  
                       metric="Accuracy", 
                       trControl = ctrl.svm.1, 
                       scale = FALSE, verbose = TRUE)
    

    Do not forget to do the same for your dtm_test otherwise the predict function will complain as well.

    pred <- predict(fit.svm.1, newdata = as.matrix(dtm_test)