# init
libs <- c("tm", "plyr", "class", "RTextTools", "randomForest")
lapply(libs, require, character.only = TRUE)
# set options
options(stringsAsFactors = FALSE)
# set parameters
labels <- read.table('labels.txt')
path <- paste(getwd(), "/data", sep="")
# clean text
cleanCorpus <- function(corpus) {
corpus.tmp <- tm_map(corpus, removePunctuation)
corpus.tmp <- tm_map(corpus.tmp, removeNumbers)
corpus.tmp <- tm_map(corpus.tmp, stripWhitespace)
corpus.tmp <- tm_map(corpus.tmp, content_transformer(tolower))
corpus.tmp <- tm_map(corpus.tmp, stemDocument, language = "english")
corpus.tmp <- tm_map(corpus.tmp, removeWords, stopwords("english"))
return(corpus.tmp)
}
# build TDM
generateTDM <- function(label, path) {
s.dir <- sprintf("%s/%s", path, label)
s.cor <- Corpus(DirSource(directory = s.dir), readerControl = list(language = "en"))
s.cor.cl <- cleanCorpus(s.cor)
s.tdm <- TermDocumentMatrix(s.cor.cl)
s.tdm <- removeSparseTerms(s.tdm, 0.7)
return(list(name = label, tdm = s.tdm))
}
tdm <- lapply(labels, generateTDM, path = path)
# attach name
bindLabelToTDM <- function(tdm) {
s.mat <- t(data.matrix(tdm[["tdm"]]))
s.df <- as.data.frame(s.mat, stringsAsFactors = FALSE)
s.df <- cbind(s.df, rep(tdm[["name"]], nrow(s.df)), row.names = NULL)
colnames(s.df)[ncol(s.df)] <- "targetlabel"
return(s.df)
}
labelTDM <- lapply(tdm, bindLabelToTDM)
# stack
tdm.stack <- do.call(rbind.fill, labelTDM)
tdm.stack[is.na(tdm.stack)] <- 0
# hold-out
train.idx <- sample(nrow(tdm.stack), ceiling(nrow(tdm.stack) * 0.7))
test.idx <- (1:nrow(tdm.stack)) [- train.idx]
tdm.lab <- tdm.stack[, "targetlabel"]
tdm.stack.nl <- tdm.stack[, !colnames(tdm.stack) %in% "targetlabel"]
train <- tdm.stack[train.idx, ]
test <- tdm.stack[test.idx, ]
train$targetlabel <- as.factor(train$targetlabel)
label.rf <- randomForest(targetlabel ~ ., data = train, ntree = 5000, mtry = 15, importance = TRUE)
I am trying multi class classfication for text files using randomForest algorithms. The error I get is probably because of the last or second last line.
Error in eval(expr, envir, enclos) : object '∗' not found
tdm.stack contains columns with names as words found in the document and their cell values as their frequency. The last column contains the class value.
I have tried everything I cant figure out the problem. Please help.