Builtin type problem with program.

Hello, I have a problem with this program.

library(ggplot2)
library(magrittr)
library(dplyr)
library(tidytext)
library(gutenbergr)
library(tm)
library(fields)
library(tidyr)
library(igraph)
library(reshape2)
library(wordcloud)
library(MASS)
library(e1071)

data.bbc <- as_tibble(read.table("something", stringsAsFactors = F))
for(i in seq(1, 8867, 1))
{
  if (data.bbc[i,2] == -1) data.bbc[i,2] <- 1
}
data.bbc %<>% arrange(desc(emo)) %>% slice(-c(500:(n()-500)))
data.bbc <- data.bbc[sample(1:nrow(data.bbc)),]
data.bbc$doc_id <- 1:nrow(data.bbc)
source <- DataframeSource(as.data.frame(data.bbc))
corpus <- VCorpus(source)
tdm <- DocumentTermMatrix(corpus)
bbc.svml <- svm(tdm, class, type = "C-classification", kernel = "linear")
bbc.svml.pred <- predict(bbc.svml, tdm)
table(class, bbc.svml.pred)

Every time I try to run this I have errors like this.

bbc.svml <- svm(tdm, class, type = "C-classification", kernel = "linear")
Błąd w poleceniu 'complete.cases(object)':niepoprawny 'type' (builtin) argumentu
bbc.svml.pred <- predict(bbc.svml, tdm)
Błąd w poleceniu 'predict(bbc.svml, tdm)':nie znaleziono obiektu 'bbc.svml'
table(class, bbc.svml.pred)
Błąd w poleceniu 'table(class, bbc.svml.pred)':
nie znaleziono obiektu 'bbc.svml.pred'

I 'll be thankful, if anyone 'd help me with it.

Hi,
It is tough to help you because we can not reproduce your error - using a reproducible example would help (see FAQ: What's a reproducible example (reprex) and how do I do one? - meta - RStudio Community).

However, my guess would be that svm() is running complete.cases() and the object is not a vector. Have a look at your data - is it in the format you expect it to be (is it completely empty?)?

Matt

I have edited my post. Is this what you needed?

That's really helpful. What are you trying at achieve with the svm() here? What is the "class" variable and where does that come from?

I wanted to compare objective class (emo = 0) with subiective class (emo = 1 or -1) using SVM method.

Okay. I think the problem is that you haven't told svm what the class is. I am not sure if svm accepts tdm's but perhaps this would work for you?

#Get your data
data.bbc <- as_tibble(read.table("https://jsienkiewicz.pl/TEXT/lab/data_bbc.csv", stringsAsFactors = F))
for(i in seq(1, 8867, 1))
{
  if (data.bbc[i,2] == -1) data.bbc[i,2] <- 1
}
data.bbc %<>% arrange(desc(emo)) %>% slice(-c(500:(n()-500)))
data.bbc <- data.bbc[sample(1:nrow(data.bbc)),]
data.bbc$doc_id <- 1:nrow(data.bbc)
# Create the document term matrix (a dtm)
library(RTextTools)
dtMatrix <- create_matrix(data.bbc["text"])
container <- create_container(dtMatrix, data.bbc$emo, trainSize=1:11, virgin=FALSE)
# train a SVM Model
model <- train_model(container, "SVM", kernel="linear", cost=1)

Sorry, but it didn't help. But meanwhile in somewhere else I 've found solution. This code does what I wanted without any errors.

library(ggplot2)
library(magrittr)
library(dplyr)
library(tidytext)
library(gutenbergr)
library(tm)
library(fields)
library(tidyr)
library(igraph)
library(reshape2)
library(wordcloud)
library(MASS)
library(e1071)
library(RTextTools)

data.bbc <- as_tibble(read.table("something", stringsAsFactors = F))
for(i in seq(1, 8867, 1))
{
  if (data.bbc[i,2] == -1) data.bbc[i,2] <- 1
}
data.bbc %<>% arrange(desc(emo)) %>% slice(-c(500:(n()-500)))
data.bbc <- data.bbc[sample(1:nrow(data.bbc)),]
data.bbc$doc_id <- 1:nrow(data.bbc)
source <- DataframeSource(as.data.frame(data.bbc))
corpus <- VCorpus(source)
tdm <- DocumentTermMatrix(corpus)
tdm <- as.matrix(tdm)
ind <- apply(tdm, 1, sum) > 1
tdm <- tdm[ind, ]
class <- data.bbc$emo[ind]
bbc.svml <- svm(tdm, class, type = "C-classification", kernel = "linear")
bbc.svml.pred <- predict(bbc.svml, tdm)
table(class, bbc.svml.pred)