I have been attempting to analyze a dataset (about 7000 entries) for twitter sentiment analysis. I've been trying to use a Naive Bayes model, in order to predict whether a tweet is negative or not. Confusion matrix has no prediction, just the base rate, meaning the model isn't making any predictions. How can I have it to make predictions? Maybe the removeSparseTerms parameter would need to change. If Bayes can't predict anything, what other models would be good to use for this dataset?
tweets$Negative = as.factor(tweets$Sentiment <= -1)
# Create corpus, Convert to lower-case, remove punctuation, remove stopwords,
# stem document, create frequency matrix
sparse = removeSparseTerms(frequencies, 0.995)
tweetsSparse = as.data.frame(as.matrix(sparse))
tweetsSparse$Negative = tweets$Negative
split = sample.split(tweetsSparse$Negative, SplitRatio = 0.7)
trainSparse = subset(tweetsSparse, split==TRUE)
testSparse = subset(tweetsSparse, split==FALSE)
prepare_testData <- function(model.training.data, test.dtm){
# Create an empty dataframe with column names same as features in training data
train.features <- names(model.training.data)
testData <- matrix(data = rep(0, length(train.features) * nrow(test.dtm)),
nrow = nrow(test.dtm), ncol = length(train.features))
colnames(testData) <- train.features
row.names(testData) <- row.names(test.dtm)
# features common to both train and test are copied from test data
common.features <- intersect(train.features, names(test.dtm))
for(i in 1:length(common.features)) {
testData[,common.features[i]] <- test.dtm[,common.features[i]]
}
testData <- as.data.frame(testData)
return(testData)
}
########### Naive Bayes model training ###########
naive.bayes.model <- train(Negative ~.,
data = trainSparse,
trControl = trainControl(method = "cv", number = 5),
method = "nb")
naive.bayes.testData <- prepare_testData(naive.bayes.model$trainingData[, -ncol(naive.bayes.model$trainingData)],
testSparse)
naive.bayes.pred <- predict(naive.bayes.model, naive.bayes.testData)
naive.bayes.metrics <- confusionMatrix(naive.bayes.pred, testSparse$Negative)