Search code examples
rsvmconfusion-matrix

Writing a function that finds the confusion matrix using a svm for classification


I am trying to write a program where the data and the place holder for the y (output) variable are given to the function. The function produces the confusion matrix for the data set and the test data. This is in fact my 5th attempt at this sort of function- which is why most of this function is from a manual using the iris data as the data set- but I seem to get stuck on the y.vec input for the function. Is my method for inserting the y variable into the function correct?

Here is my function.

function(data,y.vec)

{       
    library(e1071)
    library(rpart)
    data=data
  
    index <- 1:nrow(data)
    testindex <- sample(index, trunc(length(index)/3))
    testset <- data[testindex,]
    trainset <- data[-testindex,]
    
    svm.model <- svm(as.factor(data[y.vec]) ~ ., data = trainset, cost = 100, gamma = 1)
    svm.pred <- predict(svm.model, testset[,-y.vec])
    
    table(pred = svm.pred, true = testset[,y.vec])    
}

Solution

  • myFunc <- function(df, y.vec)
      {
        library(e1071) 
      
        df[,y.vec] <- as.factor(df[,y.vec])
        
        set.seed(1)
        index <- 1:nrow(df)
        testindex <- sample(index, trunc(length(index)/3))
        testset <- df[testindex,]
        trainset <- df[-testindex,]
        
        svm.model <- svm(as.formula(paste(y.vec, "~ .")), data = trainset, cost = 100, gamma = 1)
        svm.pred <- predict(svm.model, testset[,!(names(testset) %in% y.vec)])
        
        return(table(pred = svm.pred, true = testset[,y.vec]))
      }
    
    myFunc(iris, "Species")