sub<-sample(1:aRow,floor(0.66*aRow))
anomalyTrainingSet<- anomaly[sub,]
anomalyTestSet<- anomaly[-sub,]
anomalyClassifier<- ksvm(AttackType~.,data=anomalyTrainingSet,type = 'C-svc', kernel = 'rbfdot')
Error in if ((type(ret) == "C-svc" || type(ret) == "nu-svc" || type(ret) == :
missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In .local(x, ...) : Variable(s) `' constant. Cannot scale data.
2: In .local(x, ...) : NAs introduced by coercion
anomalyPrediction<-predict(anomalyClassifier, anomalyTestSet[,-aCol])
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'object' in selecting a method for function 'predict': object 'anomalyClassifier' not found
confusionMatrix(anomalyPrediction,anomalyTestSet[,aCol] )
Error: object 'anomalyPrediction' not found
This is the part where your (first) error occurs. It appears that is code from the function ksvm(). You have not posted enough detail for anyone to do some true troubleshooting (FYI you should try to post a reprex, a "reproducible example", e.g. with some short example data, so others can run your code as well to reproduce the error).
But what this error message refers to, is that R is trying to run the conditions (e.g. type(ret) == "C-svc" etc) but somewhere there are missing values (NAs) instead of actual values (numbers or words). R can't give an output for that, e.g. if type(ret) is NA, it cannot check if it is the same as "C-svc". You need to check your data, or your arguments in ksvm().
It looks like none of your columns are constant but some have very few non-zero values. I suspect that when you sample the data to make a training set, you get a subset that does have at least one constant column. You can manually look for constant columns with the summary() function. It will show that the Min and Max are the same in the constant column. You can then remove the column using its column number. Here is an example where I remove the third column.
DF <- data.frame(A = rnorm(5), B = rnorm(5), C = 0, D = rnorm(5))
DF
#> A B C D
#> 1 -1.4695138 -0.2189530 0 -0.9091177
#> 2 -1.0583674 -0.6116602 0 -0.2796064
#> 3 -0.2176584 0.7033546 0 -0.1457569
#> 4 0.7765660 1.5513289 0 -0.2933012
#> 5 -0.7435114 1.3171561 0 0.1938422
summary(DF)
#> A B C D
#> Min. :-1.4695 Min. :-0.6117 Min. :0 Min. :-0.9091
#> 1st Qu.:-1.0584 1st Qu.:-0.2190 1st Qu.:0 1st Qu.:-0.2933
#> Median :-0.7435 Median : 0.7034 Median :0 Median :-0.2796
#> Mean :-0.5425 Mean : 0.5482 Mean :0 Mean :-0.2868
#> 3rd Qu.:-0.2177 3rd Qu.: 1.3172 3rd Qu.:0 3rd Qu.:-0.1458
#> Max. : 0.7766 Max. : 1.5513 Max. :0 Max. : 0.1938
DF_new <- DF[, -3]
summary(DF_new)
#> A B D
#> Min. :-1.4695 Min. :-0.6117 Min. :-0.9091
#> 1st Qu.:-1.0584 1st Qu.:-0.2190 1st Qu.:-0.2933
#> Median :-0.7435 Median : 0.7034 Median :-0.2796
#> Mean :-0.5425 Mean : 0.5482 Mean :-0.2868
#> 3rd Qu.:-0.2177 3rd Qu.: 1.3172 3rd Qu.:-0.1458
#> Max. : 0.7766 Max. : 1.5513 Max. : 0.1938
Created on 2023-08-15 with reprex v2.0.2
You would need to do something similar with your data frame anomalyTrainingSet. There are many other ways to remove a column but that should be sufficient for this case.
I had a look at the code and the dataset. The (first) problem is that AttackType is not categorical, it is not a factor. Add anomaly$AttackType <- factor(anomaly$AttackType) before doing sampling and making training and test sets. FYI: always read the documentation for a function; it's fairly clear that for type = "C-svc" your response variable (y) needs to be factors.
While this makes the code run, note that you still get the warning:
Using the summary() approach by @FJCC will show you which columns have the same values throughout (meaning they are useless in terms of predicting or explaining anything). Always keep in mind that a warning is just that, a warning, and your code will still run. It is your responsibility to check what the warning is about and whether or not it's something you can ignore, or if you need to fix something.