【发布时间】:2017-03-11 21:21:22
【问题描述】:
我正在尝试纠正 R 中提供的 Carseats 数据集中的残差。我将从随机森林开始,使用 lamda=0.1 的支持向量机纠正这些残差,然后使用带有 lamda= 的 KNN 纠正这些残差0.1。在每个步骤中,我将使用 5 倍 CV 来调整随机森林的参数 mtry (3,5,10)、svm 中的 gamma (0.01,.1,1,10) 和 k (1,5,10, 20) 在 KNN 中。我对此很陌生,我已经尝试过这个问题,但老实说,我不知道我是否做对了。这是我所拥有的:
set.seed (1)
##Random forest
#mtry=3
rf3 <- randomForest(Sales ~ .,
data = Carseats, mtry = 3, ntree = 500,
importance = TRUE)
#mtry=5
rf5 <- randomForest(Sales ~ .,
data = Carseats, mtry = 5, ntree = 500,
importance = TRUE)
#mtry=10
rf10 <- randomForest(Sales ~ .,
data = Carseats, mtry = 10, ntree = 500,
importance = TRUE)
#cross validation to pick best mtry -- am getting an error
library(tree)
cv.carseats = rfcv(trainx=Carseats[,-11], trainy=Carseats[,-11],cv.fold=5)
cv.carseats
##SVM
library(e1071)
f = svm(Sales~.,data=Carseats)
#gamma = 0.01
svm(Sales~., data=Carseats, type = NULL, kernel = "polynomial", degree = 3,
gamma = if (is.vector(x)) .01
else 1 / ncol(x),
coef0 = 0, cost = 1)
#gamma = 0.1
svm(Sales~., data=Carseats, type = NULL, kernel = "polynomial", degree = 3,
gamma = if (is.vector(x)) 0.1
else 1 / ncol(x),
coef0 = 0, cost = 1)
#gamma = 1
svm(Sales~., data=Carseats, type = NULL, kernel = "polynomial", degree = 3,
gamma = if (is.vector(x)) 1
else 1 / ncol(x),
coef0 = 0, cost = 1)
#gamma = 10
svm(Sales~., data=Carseats, type = NULL, kernel = "polynomial", degree = 3,
gamma = if (is.vector(x)) 10
else 1 / ncol(x),
coef0 = 0, cost = 1)
#cross validation to pick best gamma
tune.out=tune(svm,Sales~.,data=Carseats,kernel ="polynomial",
ranges =list(cost=c(0.01,0.1,1,10)))
感谢任何反馈!
【问题讨论】:
标签: r svm random-forest cross-validation knn