【发布时间】:2019-01-03 10:18:12
【问题描述】:
我一直在寻找在 R 中使用 K-Fold 方法执行 SVR 的方法,最后,我找到了如何执行此操作,当我在 R 中仅使用 SVM() 函数时,脚本运行良好,但是当我尝试调整 SVM 我得到错误,我想,因为我必须在某种我不知道的意义上改变我的 for 循环。
错误提示“(下标)逻辑下标太长”
代码如下:
library(plyr)
library(e1071)
# cross-validation
# predict the AWC
data<-read.csv("data.csv",header = T)
k = 5
# sample from 1 to k, nrow times (the number of observations in the data)
data$id <- sample(1:k, nrow(data), replace = TRUE)
list <- 1:k
prediction <- data.frame()
testsetCopy <- data.frame()
for (i in 1:k){
# remove rows with id i from dataframe to create training set
# select rows with id i to create test set
train.set <- subset(data, id %in% list[-i])
testset <- subset(data, id %in% c(i))
# Tuning SVM
mymodel <- tune(method = svm,train.y = train.set$AWC,train.x =
train.set, kernel="radial")
#get the best model out of tuned process.
mymodel1<-mymodel$best.model
# remove the response column 1, AWC
temp <- as.data.frame(predict(mymodel1, testset[,-1]))
# append this iteration's predictions to the end of the prediction data frame
prediction <- rbind(prediction, temp)
# append this iteration's test set to the test set copy data frame
# keep only the AWC Column
testsetCopy <- rbind(testsetCopy, as.data.frame(testset[,1]))
}
【问题讨论】:
标签: r regression svm