Reputation: 43
I wrote a code like this:
library(RTextTools)
library(e1071)
library(SparseM)
pos_feeds = rbind(
c('ICICI Bank stocks soars','positive'),
c('Thomas Cook India stocks increase by 4.92%','positive'),
c('Sensex surges over 1000 pts','positive'),
c('Oil stocks up','positive'),
c('PSU bank stocks rise','positive'),
c('GDP nos are good','positive'),
c('State Bank of India stocks experience jump','positive'),
c('CPI nos are good','positive'),
c('Stock market is positive','positive'),
c('Talwalkar stocks experience sudden increase','positive')
)
neg_feeds = rbind(
c('Tata Nano stocks fall','negative'),
c('Nifty drops below 10000','negative'),
c('IOC stocks fall 3% below normal','negative'),
c('PNB stocks plunge below 40000 mark','negative'),
c('Markets crash a day after demonitization','negative'),
c('Banking stocks plunge','negative'),
c('Sensex drops by a big margin','negatve'),
c('Stocks tumble to new low','negative'),
c('Tata Steel stocks lower than normal','negative'),
c('Bank of India stocks worse than normal','negative')
)
test_feeds = rbind(
c('Citi Bank stocks soars','positive'),
c('Nifty drops below normal to finish at 15000','negative'),
c('Thomas Cook India stocks increase by a big margin','positive'),
c('Sensex surges to finish around the 50000 mark','positive'),
c('Facebook Co stocks fall 10% below usual','negative'),
c('Indian Oil Corp stocks up by 10%','positive'),
c('PNB housing stocks plunge below 50000 mark','negative'),
c('State Bank of India stocks rise','positive'),
c('Markets crash','negative'),
c('Axis Bank stocks tumble to record new low','negative')
)
feeds = rbind(pos_feeds,neg_feeds,test_feeds)
matrix = create_matrix(feeds[,1],language = "english",removeStopwords = FALSE,removeNumbers = TRUE,stemWords = FALSE)
mat = as.matrix(matrix)
classifier = naiveBayes(mat[1:10,], as.factor(feeds[1:10,2]) )
predicted = predict(classifier, mat[11:20,]); predicted
table(feeds[11:20,2], predicted)
recall_accuracy(feeds[11:20,2], predicted)
When running the code I got an error in this line:
predicted = predict(classifier, mat[11:20,]); predicted Error in apply(log(sapply(seq_along(attribs), function(v) { : dim(X) must have a positive length
I did not use the apply function, why am i getting this error? How to use apply function here if I have to use? Can someone help? Can somebody else also help?
Upvotes: 2
Views: 1630
Reputation: 7610
An answer to your question: "I did not use the apply function, why am I getting this error?"
Many functions call other functions. If you get an error for a function you didn't call, the first thing you should do is run traceback()
. This will only work if it's the first thing you do after you get the error. Here, you'll get a traceback that shows you the error occurs in the stack called by predict
.
When you run:
predicted = predict(classifier, mat[11:20,])
predict
calls predict.naiveBayes(classifier, mat[11:20,])
, which then runs through that particular method. This is where you run into the problem. Read the documentation for predict.naiveBayes
, specifically looking at the examples. In each of those cases, the naiveBayes
object passed to predict
has an apriori
element with a dim
of 2. Your object has a dim
of 1. That might be where your problem is (which means its an issue of how you've constructed classifier
.
Upvotes: 0