title: R: correcting residuals
body: <p>I am trying to correct residuals in the Carseats data set provided in R. I am to start with a random forest, and correct these residuals with a support vector machine with lamda=0.1, then correct those residuals using KNN with lamda=0.1. At each step I am to use 5-fold CV to tune the parameters mtry (3,5,10) for random forest, gamma (0.01,.1,1,10) in svm, and k (1,5,10,20) in KNN. I am very new to this, and I have attempted the problem, but I honestly have no idea if I did any of this right. Here is what I have:</p>
  
  <pre><code>set.seed (1)
  
  ##Random forest
  #mtry=3
  rf3 &lt;- randomForest(Sales ~ ., 
   data = Carseats, mtry = 3, ntree = 500,
    importance = TRUE)
  
  #mtry=5
  rf5 &lt;- randomForest(Sales ~ ., 
   data = Carseats, mtry = 5, ntree = 500,
    importance = TRUE)
  
  #mtry=10
  rf10 &lt;- 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)))
  </code></pre>
  
  <p>Any feedback is appreciated!</p>