Import Libraries

library(caret)
## Warning: package 'caret' was built under R version 3.6.1
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.6.1

Import Data

heartData <- read.csv("heart.csv")
names(heartData) <- c("age","sex","cp","trestbps","chol","fbs","restecg","thalach","exang","oldpeak","slope","ca","thal","target")

Partioning Data

set.seed(3033)
intrain <- createDataPartition(y=heartData$target,p=0.7,list = FALSE)
training <- heartData[intrain,]
testing <- heartData[-intrain,]

Train Control

trainctr <- trainControl(method = "repeatedcv",number = 10,repeats = 3)

Model Building

svm <- train(target~.,data = training,method="svmLinear",trControl=trainctr,preProcess=c("center","scale"),tuneLength=10)
## Warning in train.default(x, y, weights = w, ...): You are trying to do
## regression and your outcome only has two possible values Are you trying to
## do classification? If so, use a 2 level factor as your outcome column.
svm
## Support Vector Machines with Linear Kernel 
## 
## 213 samples
##  13 predictor
## 
## Pre-processing: centered (13), scaled (13) 
## Resampling: Cross-Validated (10 fold, repeated 3 times) 
## Summary of sample sizes: 192, 191, 192, 192, 192, 192, ... 
## Resampling results:
## 
##   RMSE       Rsquared   MAE      
##   0.3737913  0.4660956  0.2863874
## 
## Tuning parameter 'C' was held constant at a value of 1

Prediction

pred <- predict(svm,testing)
pred
##           1           3           6           7           9          11 
##  0.91220858  0.91955403  0.71015275  0.83144388  0.85709166  0.69526156 
##          18          20          24          25          27          29 
##  0.90774890  0.84762379  0.53113856  0.80385600  0.87772739  0.84906908 
##          31          40          41          43          56          59 
##  0.89536736  1.00222588  0.80361227  0.27482534  0.70126307  1.19624257 
##          60          64          68          81          84          87 
##  0.68643577  0.80775334  0.93523546  1.11780616  0.99938408  0.81419364 
##          88          91          93          95          98          99 
##  0.84819971  0.73440232  0.44058282  0.91718752  0.21235673  0.78572943 
##         100         101         103         104         105         111 
##  0.54591937  0.81404534  0.74038449  0.93551629  1.05421807  0.48398952 
##         120         122         123         127         130         132 
##  0.46686980  0.80592464  0.87688690  0.74385598  0.45865694  0.95058986 
##         139         140         146         150         151         153 
##  0.36118177  0.10351683  0.81276044  1.00397670  0.57363139  0.97548769 
##         156         164         169         180         188         189 
##  0.71410469  0.43382374  0.41677801  0.11436251 -0.01888948  0.76695357 
##         191         193         196         197         198         204 
##  0.37837041  0.34966306  0.04759197  0.73988776  0.35854418  0.42578920 
##         207         208         216         217         220         226 
##  0.16084318  0.27658211  0.17732716  0.66547584  0.02774688  0.13292832 
##         229         231         232         233         241         242 
##  0.99956438  1.05506140 -0.06112785  0.09358952  0.20922915  0.41841250 
##         247         250         252         256         258         260 
##  0.03010592  0.33829857 -0.35559564 -0.14982668  0.21122464  0.68581544 
##         268         269         271         273         282         287 
##  0.43762169 -0.15835608  0.62686061  0.46218900  0.42598075  0.84169718 
##         289         293         298         299         300         303 
##  0.09137211 -0.05216590  0.08647350  0.36608923  0.98372733  0.82537508

Confusion Matrix

(table(pred,testing$target))
##                      
## pred                  0 1
##   -0.355595636370697  1 0
##   -0.158356080063887  1 0
##   -0.149826675313335  1 0
##   -0.061127845080046  1 0
##   -0.0521659012479393 1 0
##   -0.0188894797710494 1 0
##   0.0277468766370212  1 0
##   0.0301059166385811  1 0
##   0.047591965810272   1 0
##   0.0864735001035916  1 0
##   0.0913721132480743  1 0
##   0.0935895228633427  1 0
##   0.103516831419001   0 1
##   0.114362511661135   1 0
##   0.132928317550026   1 0
##   0.160843182602835   1 0
##   0.177327157593892   1 0
##   0.209229149239294   1 0
##   0.211224635933313   1 0
##   0.212356730762734   0 1
##   0.274825340989708   0 1
##   0.276582109400578   1 0
##   0.338298570610323   1 0
##   0.349663057286235   1 0
##   0.358544180633522   1 0
##   0.361181771905816   0 1
##   0.366089232245654   1 0
##   0.378370408404145   1 0
##   0.416778009341693   1 0
##   0.418412502076485   1 0
##   0.425789195897831   1 0
##   0.42598075194453    1 0
##   0.433823743411045   0 1
##   0.437621686120158   1 0
##   0.440582821931563   0 1
##   0.458656939681655   0 1
##   0.462188999483132   1 0
##   0.466869797380144   0 1
##   0.483989519560353   0 1
##   0.531138557232302   0 1
##   0.545919371690102   0 1
##   0.573631385017514   0 1
##   0.626860607405212   1 0
##   0.665475842407207   1 0
##   0.685815436693251   1 0
##   0.686435774624156   0 1
##   0.695261563129712   0 1
##   0.701263073050472   0 1
##   0.710152745753264   0 1
##   0.714104689140721   0 1
##   0.73440231775249    0 1
##   0.73988775523375    1 0
##   0.740384490401618   0 1
##   0.743855984386541   0 1
##   0.766953567534374   1 0
##   0.785729428342587   0 1
##   0.803612265615044   0 1
##   0.803855996943556   0 1
##   0.805924643628543   0 1
##   0.807753335487935   0 1
##   0.812760436934038   0 1
##   0.814045336103371   0 1
##   0.814193643290555   0 1
##   0.825375076291441   1 0
##   0.831443876093629   0 1
##   0.841697179759202   1 0
##   0.847623789490113   0 1
##   0.848199714287553   0 1
##   0.849069077840671   0 1
##   0.857091659952871   0 1
##   0.876886896869789   0 1
##   0.877727393043168   0 1
##   0.895367364142436   0 1
##   0.907748901224985   0 1
##   0.912208580262433   0 1
##   0.917187517434105   0 1
##   0.919554034399004   0 1
##   0.935235463044838   0 1
##   0.935516290768559   0 1
##   0.950589863718723   0 1
##   0.975487687224135   0 1
##   0.983727330712923   1 0
##   0.999384083010583   0 1
##   0.999564376785691   1 0
##   1.00222587568326    0 1
##   1.00397669893042    0 1
##   1.05421806554936    0 1
##   1.05506140217485    1 0
##   1.11780616421615    0 1
##   1.19624257241631    0 1