I am trying to predict the future price by using SVM model but an error comes out. This is my coding:
data <-read_excel("price/data.xlsx")
n=round(nrow(data)*0.80)
set.seed(12345)
sample <- sample(seq_len(nrow(data)), size=n)
train<- data[sample,]
test <- data[-sample,]
data.tune<- tune.svm(Price~.,data = train, kernel="radial",
gamma=10^(-1:1),cost=10^(0:2),epsilon=seq(0,0.1,by=0.01))
data.svm <- svm(Price~.,data=train,gamma=data.tune$best.parameters$gamma,
cost=data.tune$best.parameters$cost,
epsilon=data.tune$best.parameters$epsilon)
train_pred <- predict(data.svm, train)
confusionMatrix(table(train_pred, train$Price))
The error occurs when I run the confusion matrix.
> confusionMatrix(train_pred, train$Price)
Error: `data` and `reference` should be factors with the same levels.
This are their structure:
> str(train_pred)
Named num [1:620] 4.84 5.2 25.44 24.84 7.18 ...
- attr(*, "names")= chr [1:620] "1" "2" "3" "4" ...
> str(train$Price)
num [1:620] 4.92 5.28 26.37 25.29 6.89 ...
I have tried several ways to fix it but is not suitable for my case. Could you help me to fix it? Thank you in advance.