Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

r - Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : object is not a matrix

I am working on classification model (SVM) on qualitative data, the data was downloaded in the UCI repository. This is the link to the datasets used and it has been splited into train and test sets, my problem is that i have fit a model but I was unable to predict my model (review_ted_model) on the test dataset. can anyone help me with error because I'm fed up, please?

This was the problem I was getting.

```
predict_svm_rbf <- predict(review_ted_model, newdata = test)
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  object is not a matrix
In addition: Warning message:
'newdata' had 53766 rows but variable found had 1 row 
 ```

The ouput to the codes are too much for me to copy here. These are the codes have tried and they worked perfectly except for the last line of the code which was already stated above.

library(caret)
library(mlbench)
library(tidyverse)
library(tidytext)
library(data.table)
train=as.data.frame(fread("drugsComTrain_raw.tsv"))
test=as.data.frame(fread("drugsComTest_raw.tsv"))
train %>% head()
summary(train)
str(train)
nrow(train)
train[duplicated(train)|duplicated(train, fromLast = T), ]
sapply(train, function(x) sum(is.na(x)))

#Finding the total count of missing values.
sum(!complete.cases(train))
head(train)

train  <- train %>% mutate(document = row_number())
train$condition <- as.character(train$condition)
train$review <- as.character(train$review)
train$drugName <- as.character(train$drugName)
summary(train)
str(train)


trainText <- train %>% 
  unnest_tokens(word, review) %>% 
  anti_join(stop_words)


length(unique(trainText$drugName))


unique(trainText$drugName)
abc = trainText[which(trainText$drugName %in% c("Methadose","Didrex","Forteo","Kava")),]
abc

abc %>% 
  count(word, drugName, sort = TRUE) %>% 
  mutate(drugName = factor(drugName),
         word = as.factor(word),
         word = fct_reorder(word,n)
  ) %>% 
  group_by(drugName) %>% 
  top_n(10)  %>% 
  ungroup() %>% 
  ggplot(aes(word, n)) +
  geom_col(aes(fill = drugName)) +
  facet_wrap(~drugName, scales = "free") +
  coord_flip()

View(abc)
abc 
library(caret)
library(LiblineaR)
review_ted_model <- train(condition ~ word, data = abc, method = "svmLinear3")

plot(review_ted_model)

View(test)
predict_svm_rbf <- predict(review_ted_model, newdata = test)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...