R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

data(iris)
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
qplot(Petal.Length,Petal.Width,data=iris,color=Species)

library(e1071)
classificationmymode<-svm(Species~.,data=iris)
linearmymode<-svm(Species~.,data=iris,kernal="linear")
logisticmode<-svm(Species~.,data=iris,kernal="logistic")
tanhmode<-svm(Species~.,data=iris,kernal="tanh")
summary(tanhmode)
## 
## Call:
## svm(formula = Species ~ ., data = iris, kernal = "tanh")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
##       gamma:  0.25 
## 
## Number of Support Vectors:  51
## 
##  ( 8 22 21 )
## 
## 
## Number of Classes:  3 
## 
## Levels: 
##  setosa versicolor virginica
summary(logisticmode)
## 
## Call:
## svm(formula = Species ~ ., data = iris, kernal = "logistic")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
##       gamma:  0.25 
## 
## Number of Support Vectors:  51
## 
##  ( 8 22 21 )
## 
## 
## Number of Classes:  3 
## 
## Levels: 
##  setosa versicolor virginica
summary(classificationmymode)
## 
## Call:
## svm(formula = Species ~ ., data = iris)
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
##       gamma:  0.25 
## 
## Number of Support Vectors:  51
## 
##  ( 8 22 21 )
## 
## 
## Number of Classes:  3 
## 
## Levels: 
##  setosa versicolor virginica
summary(linearmymode)
## 
## Call:
## svm(formula = Species ~ ., data = iris, kernal = "linear")
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  1 
##       gamma:  0.25 
## 
## Number of Support Vectors:  51
## 
##  ( 8 22 21 )
## 
## 
## Number of Classes:  3 
## 
## Levels: 
##  setosa versicolor virginica
plot(tanhmode,data=iris,Petal.Width~Petal.Length,slice=list(Sepal.Width=3,Sepal.Length=4))

#confusion matrix
pred<-predict(tanhmode,iris)
tab<-table(Predicted=pred,Actual=iris$Species)
tab
##             Actual
## Predicted    setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         48         2
##   virginica       0          2        48
1-sum(diag(tab))/sum(tab)
## [1] 0.02666667
#Tuning
set.seed(123)
tmodel<-tune(svm,Species~.,data=iris,ranges = list(epsilon=seq(0,1,0.1),cost=2^(2:9)))
plot(tmodel)

summary(tmodel)
## 
## Parameter tuning of 'svm':
## 
## - sampling method: 10-fold cross validation 
## 
## - best parameters:
##  epsilon cost
##        0    8
## 
## - best performance: 0.03333333 
## 
## - Detailed performance results:
##    epsilon cost      error dispersion
## 1      0.0    4 0.04666667 0.06324555
## 2      0.1    4 0.04666667 0.06324555
## 3      0.2    4 0.04666667 0.06324555
## 4      0.3    4 0.04666667 0.06324555
## 5      0.4    4 0.04666667 0.06324555
## 6      0.5    4 0.04666667 0.06324555
## 7      0.6    4 0.04666667 0.06324555
## 8      0.7    4 0.04666667 0.06324555
## 9      0.8    4 0.04666667 0.06324555
## 10     0.9    4 0.04666667 0.06324555
## 11     1.0    4 0.04666667 0.06324555
## 12     0.0    8 0.03333333 0.06478835
## 13     0.1    8 0.03333333 0.06478835
## 14     0.2    8 0.03333333 0.06478835
## 15     0.3    8 0.03333333 0.06478835
## 16     0.4    8 0.03333333 0.06478835
## 17     0.5    8 0.03333333 0.06478835
## 18     0.6    8 0.03333333 0.06478835
## 19     0.7    8 0.03333333 0.06478835
## 20     0.8    8 0.03333333 0.06478835
## 21     0.9    8 0.03333333 0.06478835
## 22     1.0    8 0.03333333 0.06478835
## 23     0.0   16 0.04000000 0.06440612
## 24     0.1   16 0.04000000 0.06440612
## 25     0.2   16 0.04000000 0.06440612
## 26     0.3   16 0.04000000 0.06440612
## 27     0.4   16 0.04000000 0.06440612
## 28     0.5   16 0.04000000 0.06440612
## 29     0.6   16 0.04000000 0.06440612
## 30     0.7   16 0.04000000 0.06440612
## 31     0.8   16 0.04000000 0.06440612
## 32     0.9   16 0.04000000 0.06440612
## 33     1.0   16 0.04000000 0.06440612
## 34     0.0   32 0.04666667 0.07062333
## 35     0.1   32 0.04666667 0.07062333
## 36     0.2   32 0.04666667 0.07062333
## 37     0.3   32 0.04666667 0.07062333
## 38     0.4   32 0.04666667 0.07062333
## 39     0.5   32 0.04666667 0.07062333
## 40     0.6   32 0.04666667 0.07062333
## 41     0.7   32 0.04666667 0.07062333
## 42     0.8   32 0.04666667 0.07062333
## 43     0.9   32 0.04666667 0.07062333
## 44     1.0   32 0.04666667 0.07062333
## 45     0.0   64 0.04666667 0.07062333
## 46     0.1   64 0.04666667 0.07062333
## 47     0.2   64 0.04666667 0.07062333
## 48     0.3   64 0.04666667 0.07062333
## 49     0.4   64 0.04666667 0.07062333
## 50     0.5   64 0.04666667 0.07062333
## 51     0.6   64 0.04666667 0.07062333
## 52     0.7   64 0.04666667 0.07062333
## 53     0.8   64 0.04666667 0.07062333
## 54     0.9   64 0.04666667 0.07062333
## 55     1.0   64 0.04666667 0.07062333
## 56     0.0  128 0.06000000 0.07981460
## 57     0.1  128 0.06000000 0.07981460
## 58     0.2  128 0.06000000 0.07981460
## 59     0.3  128 0.06000000 0.07981460
## 60     0.4  128 0.06000000 0.07981460
## 61     0.5  128 0.06000000 0.07981460
## 62     0.6  128 0.06000000 0.07981460
## 63     0.7  128 0.06000000 0.07981460
## 64     0.8  128 0.06000000 0.07981460
## 65     0.9  128 0.06000000 0.07981460
## 66     1.0  128 0.06000000 0.07981460
## 67     0.0  256 0.05333333 0.06885304
## 68     0.1  256 0.05333333 0.06885304
## 69     0.2  256 0.05333333 0.06885304
## 70     0.3  256 0.05333333 0.06885304
## 71     0.4  256 0.05333333 0.06885304
## 72     0.5  256 0.05333333 0.06885304
## 73     0.6  256 0.05333333 0.06885304
## 74     0.7  256 0.05333333 0.06885304
## 75     0.8  256 0.05333333 0.06885304
## 76     0.9  256 0.05333333 0.06885304
## 77     1.0  256 0.05333333 0.06885304
## 78     0.0  512 0.06000000 0.06629526
## 79     0.1  512 0.06000000 0.06629526
## 80     0.2  512 0.06000000 0.06629526
## 81     0.3  512 0.06000000 0.06629526
## 82     0.4  512 0.06000000 0.06629526
## 83     0.5  512 0.06000000 0.06629526
## 84     0.6  512 0.06000000 0.06629526
## 85     0.7  512 0.06000000 0.06629526
## 86     0.8  512 0.06000000 0.06629526
## 87     0.9  512 0.06000000 0.06629526
## 88     1.0  512 0.06000000 0.06629526
#best model
permodel<-tmodel$best.model
summary(permodel)
## 
## Call:
## best.tune(method = svm, train.x = Species ~ ., data = iris, ranges = list(epsilon = seq(0, 
##     1, 0.1), cost = 2^(2:9)))
## 
## 
## Parameters:
##    SVM-Type:  C-classification 
##  SVM-Kernel:  radial 
##        cost:  8 
##       gamma:  0.25 
## 
## Number of Support Vectors:  35
## 
##  ( 6 15 14 )
## 
## 
## Number of Classes:  3 
## 
## Levels: 
##  setosa versicolor virginica
plot(permodel,data=iris,Petal.Width~Petal.Length,slice=list(Sepal.Width=3,Sepal.Length=4))

#Misclassification error is only 2
pre<-predict(permodel,iris)
tab<-table(Predicted=pre,Actual=iris$Species)
tab
##             Actual
## Predicted    setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         48         0
##   virginica       0          2        50
1-sum(diag(tab))/sum(tab)
## [1] 0.01333333

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.