title: issue with Dictionary() function in R
body: <p>I have been following an example of Bayesian classifiers according to the book of Lantz entitled "Machine Learning with R". The case is a spam classifier that works with the data of the following link:</p>
  
  <p><a href="http://www.dt.fee.unicamp.br/~tiago/smsspamcollection/" rel="noreferrer">http://www.dt.fee.unicamp.br/~tiago/smsspamcollection/</a></p>
  
  <p>In the code I have a problem in this part:</p>
  
  <pre><code>sms_train&lt;-DocumentTermMatrix(sms_corpus_train,list(dictionary=sms_dict))
  sms_test&lt;-DocumentTermMatrix(sms_corpus_test,list(dictionary=sms_dict))
  </code></pre>
  
  <p>because it says that I should use the following instruction:</p>
  
  <pre><code>sms_dict &lt;- Dictionary(findFreqTerms(sms_dtm_train, 5))
  </code></pre>
  
  <p>The problem is that the Dictionary() function has been deprecated from new versions of tm. What I should do to accomplish what the books says:</p>
  
  <blockquote>
    <p>A dictionary is a data structure allowing us to specify which words
    should appear in a document term matrix. To limit our training and
    test matrixes to only the words in the preceding dictionary, use the
    following command</p>
  </blockquote>
  
  <p>I have done the following:</p>
  
  <pre><code>sms_dict&lt;-findFreqTerms(sms_dtm_train,5)
  sms_train&lt;-DocumentTermMatrix(sms_corpus_train,list(dictionary=sms_dict))
  sms_test&lt;-DocumentTermMatrix(sms_corpus_test,list(dictionary=sms_dict))
  </code></pre>
  
  <p>But I am sure that I am not limiting the test matrices at it says in the book. Even though the code is working, it does not give me the right results. What can I modify in this case?</p>
  
  <p>The complete code for tracking purposes is the following:</p>
  
  <pre><code>sms_raw&lt;-read.csv("sms_spam.csv",stringsAsFactors=FALSE)
  install.packages("tm")
  library(tm)
  sms_corpus&lt;-Corpus(VectorSource(sms_raw$text))
  corpus_clean&lt;-tm_map(sms_corpus,content_transformer(tolower))
  corpus_clean&lt;-tm_map(corpus_clean,removeNumbers)
  corpus_clean&lt;-tm_map(corpus_clean,removeWords,stopwords())
  corpus_clean&lt;-tm_map(corpus_clean,stripWhitespace)
  sms_dtm&lt;-DocumentTermMatrix(corpus_clean)
  sms_raw_train&lt;-sms_raw[1:4169,]
  sms_raw_test&lt;-sms_raw[4170:5559,]
  sms_dtm_train&lt;-sms_dtm[1:4169,]
  sms_dtm_test&lt;-sms_dtm[4170:5559,]
  sms_corpus_train&lt;-corpus_clean[1:4169]
  sms_corpus_test&lt;-corpus_clean[4170:5559]
  sms_dict&lt;-findFreqTerms(sms_dtm_train,5)
  sms_train&lt;-DocumentTermMatrix(sms_corpus_train,list(dictionary=sms_dict))
  sms_test&lt;-DocumentTermMatrix(sms_corpus_test,list(dictionary=sms_dict))
  convert_counts&lt;-function(x){
  x&lt;-ifelse(x&gt;0,1,0)
  x&lt;-factor(x,levels=c(0,1),labels=c("No","Yes"))
  return(x)
  }
  sms_train&lt;-apply(sms_train,MARGIN=2,convert_counts)
  sms_test&lt;-apply(sms_test,MARGIN=2,convert_counts)
  library(e1071)
  sms_classifier&lt;-naiveBayes(sms_train,sms_raw_train$type)
  sms_test_pred&lt;-predict(sms_classifier,sms_test)
  install.packages("gmodels")
  library(gmodels)
  CrossTable(sms_test_pred,sms_raw_test$type,prop.chisq=FALSE,prop.t=FALSE,dnn=c('predicted','actual'))
  </code></pre>
  
  <p>Thanks</p>