在 R 中使用 K-fold 方法支持调谐情绪中的向量回归
Support Vector regression in tuned mood using K-fold method in R
我一直在寻找在 R 中使用 K-Fold 方法执行 SVR 的方法,最后,我找到了如何执行此操作,当我在 R 中仅使用 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]))
}
函数 tune
为您完成工作,您不需要编写循环。查看 tune
的 R 文档。那里有一个例子。
此外,通过设置 k=5
,看起来您有 5 个数据点,一次漏掉一个。也许这会更好 sample(1:nrow(data), round(0.80*nrow(data)),replace = TRUE)
虽然没有必要这样做,因为 tune
做了所有这些。
我一直在寻找在 R 中使用 K-Fold 方法执行 SVR 的方法,最后,我找到了如何执行此操作,当我在 R 中仅使用 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]))
}
函数 tune
为您完成工作,您不需要编写循环。查看 tune
的 R 文档。那里有一个例子。
此外,通过设置 k=5
,看起来您有 5 个数据点,一次漏掉一个。也许这会更好 sample(1:nrow(data), round(0.80*nrow(data)),replace = TRUE)
虽然没有必要这样做,因为 tune
做了所有这些。