Krishna Kalyan
Krishna Kalyan

Reputation: 1702

Naive Bayes in R, Sentiment analysis leads to cannot coerce class error

I have a dataframe df_tweets that has two columns tweets and score. Score is a factor with values between 1 to 5

getMatrix <- function(chrVect){
 testsource <- VectorSource(chrVect)
 testcorpus <- Corpus(testsource)
 testcorpus <- tm_map(testcorpus,stripWhitespace)
 testcorpus <- tm_map(testcorpus, removeWords, stopwords('french'))
 testcorpus <- tm_map(testcorpus, removeWords, stopwords('english'))
 testcorpus <- tm_map(testcorpus, content_transformer(tolower))
 testcorpus <- tm_map(testcorpus, removePunctuation)
 testcorpus <- tm_map(testcorpus, removeNumbers)
 testcorpus <- tm_map(testcorpus, PlainTextDocument)

 return(DocumentTermMatrix(testcorpus))
}

op =getMatrix(df_tweets$text)
classifier <-naiveBayes(as.matrix(op), as.factor(df_tweets$avg_score))

When I use the predict function I get an error

myPrediction<- predict(classifier,op)

Error in as.data.frame.default(newdata) : 
cannot coerce class "c("DocumentTermMatrix", "simple_triplet_matrix")" to a data.frame

How can I resolve this?

Upvotes: 1

Views: 508

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 110034

I believe you can wrap as.matrix with as.data.frame or directly with as.matrix.data.frame.

Upvotes: 1

Related Questions