I have a question concerning the RTextTools package. When I was reading textbooks and wikipedia I imagined that text classification is all about defining a classificator. In RTextTools you define learning and testing documents in one command. For example

data <- data[sample(1:3100,size=100,replace=FALSE),]
matrix <- create_matrix(cbind(data$Title,data$Subject), language="english",
removeNumbers=TRUE, stemWords=FALSE, weighting=weightTfIdf)
corpus <- create_corpus(matrix,data$Topic.Code,trainSize=1:75, testSize=76:100,virgin=FALSE)
models <- train_models(corpus, algorithms=c("MAXENT","SVM"))

We create a corpus from one dataset, where like first 75 documents is for learning and the rest is for training. But when it comes to applying text classification theories for practical use, you want already predefined text classificator to be applied for different datasets. For now the only solution i managed to find is to train with the same documents and then use different test data. But it sounds very unconfortable when it comes to large texts. So my question is how to create predefined classificator and then apply it to different datasets with R? It would be preferable to use algorithms from RTextTools.


1 Answer 1


According to documentation, you need function classify_models (excerpt from the documentation page):


Uses a trained model from the train_models function to classify new data.


classify_models(corpus, models, ...)


corpus Class of type matrix_container-class generated by the create_corpus function.

models List of models to be used for classification generated by train_models.

... Other parameters to be passed on to classify_model.

Here is the reproducible example:

###Train model (taken out of `classify_model` man page)

alldata <- read_data(system.file("data/NYTimes.csv.gz",package="RTextTools"),type="csv")
smpl <- sample(1:3100,size=100)
data <- alldata[smpl,]
matrix <- create_matrix(cbind(data$Title,data$Subject), language="english", 
removeNumbers=TRUE, stemWords=FALSE, weighting=weightTfIdf)
corpus <- create_corpus(matrix,data$Topic.Code,trainSize=1:75, testSize=76:100, 

models <- train_models(corpus, algorithms=c("MAXENT","SVM"))

###Create new data which you want to classify: 

newdata <- alldata[sample((1:3100)[-smpl],size=101,replace=FALSE),]

newmatrix <- create_matrix(cbind(newdata$Title,newdata$Subject), language="english", 
removeNumbers=TRUE, stemWords=FALSE, weighting=weightTfIdf)

newcorpus <- create_corpus(newmatrix,newdata$Topic.Code,trainSize=1:1, testSize=2:101, 

newresults1 <- classify_model(newcorpus,models[[1]])
newresults2 <- classify_model(newcorpus,models[[2]])

The last line should produce the error, since the new data has some terms which were not present in the data used to produce the models. To circumvent this, it is better to use create_matrix on all the data you have, and then to train model on the sample of result of create_matrix, instead of the original data.

This solution is a bit quirky, since the convenience function classify_models demands a corpus, where train and test data sets must be explicitly defined. Since the goal is to use the classifier, all data is test data. In this solution I sidestepped the problem by selecting only one observation for train and all the others for the test. To sidestep this it is possible to use predict method directly:

  • $\begingroup$ > predict(SVM,as.compressed.matrix(doc_matrix2)) Error in predict.svm(SVM, as.compressed.matrix(doc_matrix2)) : test data does not match model ! $\endgroup$
    – Chris
    Dec 4, 2015 at 7:27
  • $\begingroup$ Feels like RTextTools is more of a toy. It is nearly impossible in a production environment to create the matrix of all the data you have unless perhaps you are TimeLord. $\endgroup$
    – Chris
    Dec 4, 2015 at 7:29
  • 1
    $\begingroup$ just to add, if you have the error "test data does not match model", when calling create_matrix for your unclassified set you should be providing argument originalMatrix=(you original training matrix) $\endgroup$
    – eli
    Sep 1, 2016 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.