0
# Convert x and xnew data frames into matrix format, and convert response 
# column into factor
x <- data.matrix(x)
xnew <- data.matrix(xnew)
y_sub <- ifelse(y == 1, "Class1", "Class0")
y_sub <- as.factor(y_sub)

# SVM with linear kernel - nested cross validation
tg <- expand.grid(C =c(.000001, .00001, .0001, .001, .01, .1, 1, 10, 100))
set.seed(123)
#trCtrl <- trainControl(method = "cv",number = 10, 
                        savePredictions = "final")
ncv_linear <- nestcv.train(y = y_sub, x = x,
                    method = "svmLinear",
                    tuneGrid = tg,
                    n_outer_folds = 10,
                    cv.cores=4)
# Estimated test error rate (1 - Accuracy)
estimated_test_error_linear_kernel <- as.numeric(
                                     1 - ncv_linear$summary[2]$metrics[2])
ncv_linear$finalTune
# Obtaining training error rate
SVM_linear_preds <- predict(ncv_linear, newdata = x)
SVM_linear_preds <- ifelse(SVM_linear_preds == 'Class1', 1, 0)
table(SVM_linear_preds,y)#confusion table
training_error_SVM_linear <- mean(SVM_linear_preds != y) 
                   # We get training error rate from running this line  

Above is my script. I'm trying to do nested cross validation (CV) using nestcv.train() function for linear SVM. Even though I used set.seed(), it's giving me different results for "estimated_test_error_linear_kernel."

I think it's because I have to explicitly set the n_inner_folds to be used in the nested CV, but I'm not sure how to do it. I tried nesting the trainControl function inside nestcv.train() to set the number of inner folds, but that's not working.

David
  • 1
  • 2
  • Welcome to SO! Please provide your code in the body of your post rather than as a screenshot. You can edit your post and click the "Code" button for instructions. – jtr13 Mar 10 '23 at 23:13

0 Answers0