Plot support vector machine with categorical predictor variables

Viewed 175

I'm looking to plot the results of a support vector classification, whereby the predictor variables are categorical (Gender and School). School can be 1 of 20, Gender 1 of 2. The response variable is Attended school (y/n)

The attendance.df is as follows:

   gender school attendance
1    male      1          1
2    male      2          0
3    male      1          1
4  female      2          1
5    male      1          1
6  female      2          0
7  female      3          1
8    male      4          0
9  female      5          1
10 female      6          1
11 female      7          1
12   male      8          1
13   male      9          1
14   male     10          1
15   male     10          1
16   male     11          0
17   male     12          1
18 female     13          1
19   male     14          1
20 female     15          0
21 female     16          1
22   male     17          0
23 female     18          1
24 female     19          0
25 female      4          1
26   male      5          1
27   male      5          1
28   male     20          0

The code for the SVM and plot is:

# Builds linear SVM model
svm.response = tune(svm,attendance~ ., data=attendance.df, kernel="linear", ranges=list(cost=c(0.001,0.01,0.1,1,10,100,1000)))

svm.response$best.parameters # Identifies best parameters to rebuild model below

svm.response = svm(attendance ~ ., data=attendance.df, kernel="linear", cost=10)

# Plots SVM classification plot
plot(svm.response, attendance.df, fill=TRUE)

I want the predictor variables (gender and school) to be on the x and y, with the linear line (the SVM) to separate attendance which will be a black dot for yes, and red dot for no.

With the plot() I have included, the error "‘min’ not meaningful for factors" appears due to all variables being factors. I'm just unsure how to plot

Note: It may help to expand the df, I have deliberately shortned in order to post.

0 Answers
Related