Description about Data :-
The dataset name is cats. It is from MASS package.
data(cats , package = "MASS")
str(cats)## 'data.frame': 144 obs. of 3 variables:
## $ Sex: Factor w/ 2 levels "F","M": 1 1 1 1 1 1 1 1 1 1 ...
## $ Bwt: num 2 2 2 2.1 2.1 2.1 2.1 2.1 2.1 2.1 ...
## $ Hwt: num 7 7.4 9.5 7.2 7.3 7.6 8.1 8.2 8.3 8.5 ...
summary(cats)## Sex Bwt Hwt
## F:47 Min. :2.000 Min. : 6.30
## M:97 1st Qu.:2.300 1st Qu.: 8.95
## Median :2.700 Median :10.10
## Mean :2.724 Mean :10.63
## 3rd Qu.:3.025 3rd Qu.:12.12
## Max. :3.900 Max. :20.50
Statisical Model 1 :-
Fitting SVM Classifier :-
m <- svm(Sex~. , data = cats)
plot(m , data = cats)It seems , lot of objects are mis-classified with this default SVM model.
Getting more details about default SVM :-
summary(m)##
## Call:
## svm(formula = Sex ~ ., data = cats)
##
##
## Parameters:
## SVM-Type: C-classification
## SVM-Kernel: radial
## cost: 1
##
## Number of Support Vectors: 84
##
## ( 39 45 )
##
##
## Number of Classes: 2
##
## Levels:
## F M
data.frame(predict(m) , cats$Sex)## predict.m. cats.Sex
## 1 F F
## 2 F F
## 3 F F
## 4 F F
## 5 F F
## 6 F F
## 7 F F
## 8 F F
## 9 F F
## 10 F F
## 11 F F
## 12 F F
## 13 F F
## 14 F F
## 15 F F
## 16 F F
## 17 F F
## 18 F F
## 19 F F
## 20 F F
## 21 F F
## 22 F F
## 23 F F
## 24 F F
## 25 F F
## 26 F F
## 27 F F
## 28 F F
## 29 F F
## 30 F F
## 31 M F
## 32 F F
## 33 F F
## 34 F F
## 35 M F
## 36 M F
## 37 M F
## 38 M F
## 39 M F
## 40 M F
## 41 M F
## 42 M F
## 43 M F
## 44 M F
## 45 M F
## 46 M F
## 47 M F
## 48 F M
## 49 F M
## 50 F M
## 51 F M
## 52 F M
## 53 F M
## 54 F M
## 55 F M
## 56 F M
## 57 F M
## 58 F M
## 59 F M
## 60 M M
## 61 M M
## 62 M M
## 63 F M
## 64 F M
## 65 M M
## 66 M M
## 67 M M
## 68 M M
## 69 M M
## 70 M M
## 71 M M
## 72 M M
## 73 M M
## 74 M M
## 75 M M
## 76 M M
## 77 M M
## 78 M M
## 79 M M
## 80 M M
## 81 M M
## 82 M M
## 83 M M
## 84 M M
## 85 M M
## 86 M M
## 87 M M
## 88 M M
## 89 M M
## 90 M M
## 91 M M
## 92 M M
## 93 M M
## 94 M M
## 95 M M
## 96 M M
## 97 M M
## 98 M M
## 99 M M
## 100 M M
## 101 M M
## 102 M M
## 103 M M
## 104 M M
## 105 M M
## 106 M M
## 107 M M
## 108 M M
## 109 M M
## 110 M M
## 111 M M
## 112 M M
## 113 M M
## 114 M M
## 115 M M
## 116 M M
## 117 M M
## 118 M M
## 119 M M
## 120 M M
## 121 M M
## 122 M M
## 123 M M
## 124 M M
## 125 M M
## 126 M M
## 127 M M
## 128 M M
## 129 M M
## 130 M M
## 131 M M
## 132 M M
## 133 M M
## 134 M M
## 135 M M
## 136 M M
## 137 M M
## 138 M M
## 139 M M
## 140 M M
## 141 M M
## 142 M M
## 143 M M
## 144 M M
table(true=cats$Sex , predicted = predict(m))## predicted
## true F M
## F 33 14
## M 14 83
Tunning the parameters :-
obj <- tune(svm,
Sex~.,
data = cats,
ranges = list(gamma = seq(0 , 1.0 , 0.1),
cost = seq(10,100,.1)),
tunecontrol = tune.control(sampling = "fix"))plot(obj)obj##
## Parameter tuning of 'svm':
##
## - sampling method: fixed training/validation set
##
## - best parameters:
## gamma cost
## 0 10
##
## - best performance: 0.2083333
Fitting the SVM with optimized parameters :-
m2 <- svm(Sex~. , data = cats , gamma=0.5 , cost = 67.8)
plot(m2, data = cats)table(true = cats$Sex , predicted = predict(m2))## predicted
## true F M
## F 33 14
## M 11 86