【问题标题】:Support Vector regression in tuned mood using K-fold method in R使用 R 中的 K-fold 方法调整情绪中的支持向量回归
【发布时间】: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


    【解决方案1】:

    函数tune 为您完成工作,您无需编写循环。查看tune 的 R 文档。那里有一个例子。

    另外,通过设置k=5,看起来您有 5 个数据点,一次只留下一个。也许这会更好sample(1:nrow(data), round(0.80*nrow(data)),replace = TRUE) 尽管没有必要这样做,因为tune 做了所有这些。

    【讨论】:

    • 是的,你没事。我很困惑。感谢您的提示。
    • 我现在有一个问题,因为调整方法是进行内部 10 折交叉验证并将数据拆分为训练和测试集,我如何才能从每个折中单独检索结果,不仅适用于最好的模型?
    猜你喜欢
    • 2017-04-15
    • 1970-01-01
    • 2019-11-25
    • 2020-01-18
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    相关资源
    最近更新 更多