title: Classification Algorithm for text using R
body: <p>I wanted to predict class of new document using historical data of text "description" and "class"</p>
  
  <p>Below script I am using , but for new document which I want to predict I am not getting better accuracy , can anyone help me to know which algorithm can be used to increase accuracy. Please advice.</p>
  
  <pre><code>library(plyr)
  library(tm)
  library(e1071)
  
  setwd("C:/Data")
  
  past &lt;- read.csv("Past - Copy.csv",header=T,na.strings=c(""))
  future &lt;- read.csv("Future - Copy.csv",header=T,na.strings=c(""))
  
  training &lt;- rbind.fill(past,future)
  
  Res_Desc_Train &lt;- subset(training,select=c("Class","Description"))
  
  ##Step 1 : Create Document Matrix of ticket Descriptions available past data
  
  docs &lt;- Corpus(VectorSource(Res_Desc_Train$Description))
  docs &lt;-tm_map(docs,content_transformer(tolower))
  
  #remove potentially problematic symbols
  toSpace &lt;- content_transformer(function(x, pattern) { return (gsub(pattern, " ", x))})
  removeSpecialChars &lt;- function(x) gsub("[^a-zA-Z0-9 ]","",x)
  docs &lt;- tm_map(docs, content_transformer(tolower))
  docs &lt;- tm_map(docs, removeNumbers)
  docs &lt;- tm_map(docs, removePunctuation)
  docs &lt;- tm_map(docs, stripWhitespace)
  docs &lt;- tm_map(docs, removeWords, stopwords('english'))
  
  
  #inspect(docs[440])
  dataframe&lt;-data.frame(text=unlist(sapply(docs, `[`, "content")), stringsAsFactors=F)
  
  dtm &lt;- DocumentTermMatrix(docs,control=list(stopwords=FALSE,wordLengths =c(2,Inf)))
  
  ##Let's remove the variables which are 95% or more sparse.
  dtm &lt;- removeSparseTerms(dtm,sparse = 0.95)
  
  Weighteddtm &lt;- weightTfIdf(dtm,normalize=TRUE)
  mat.df &lt;- as.data.frame(data.matrix(Weighteddtm), stringsAsfactors = FALSE)
  mat.df &lt;- cbind(mat.df, Res_Desc_Train$Class)
  colnames(mat.df)[ncol(mat.df)] &lt;- "Class"
  Assignment.Distribution &lt;- table(mat.df$Class)
  
  Res_Desc_Train_Assign &lt;- mat.df$Class
  
  Assignment.Distribution &lt;- table(mat.df$Class)
  
  ### Feature has different ranges, normalizing to bring ranges from 0 to 1
  ### Another way to standardize using z-scores
  
  normalize &lt;- function(x) {
    y &lt;- min(x)
    z &lt;- max(x)
    temp &lt;- x - y
    temp1 &lt;- (z - y)
    temp2 &lt;- temp / temp1
    return(temp2)
  }
  #normalize(c(1,2,3,4,5))
  
  num_col &lt;- ncol(mat.df)-1
  mat.df_normalize &lt;- as.data.frame(lapply(mat.df[,1:num_col], normalize))
  mat.df_normalize &lt;- cbind(mat.df_normalize, Res_Desc_Train_Assign)
  colnames(mat.df_normalize)[ncol(mat.df_normalize)] &lt;- "Class"
  
  #names(mat.df)
  outcomeName &lt;- "Class"
  
  train = mat.df_normalize[c(1:nrow(past)),]
  test = mat.df_normalize[((nrow(past)+1):nrow(training)),]
  
  
  train$Class &lt;- as.factor(train$Class) 
  
  ###SVM Model
  x &lt;- subset(train, select = -Class)
  y &lt;- train$Class
  model &lt;- svm(x, y, probability = TRUE) 
  test1 &lt;- subset(test, select = -Class)
  svm.pred &lt;- predict(model, test1, decision.values = TRUE, probability = TRUE)
  svm_prob &lt;- attr(svm.pred, "probabilities")
  
  finalresult &lt;- cbind(test,svm.pred,svm_prob)
  </code></pre>