# Support Vector Machine
install.packages("kernlab")
# load the package
library(kernlab)
data(iris)
# fit model
fit <- ksvm(Species~., data=iris)
# summarize the fit
print(fit)
# make predictions
predictions <- predict(fit, iris[,1:4], type="response")
# summarize accuracy
table(predictions, iris$Species)


# trabajando con el k-fold con caret y SVM
install.packages(c("e1071","rattle"))
library(e1071,rattle)

# define an 70%/30% train/test split of the dataset
split=0.70
trainIndex <- createDataPartition(iris$Species, p=split, list=FALSE)
data_train <- iris[ trainIndex,]
data_test  <- iris[-trainIndex,]

train_control <- trainControl(method = "cv" , number = 10)

# fit con caret
fit2 <- train(Species~., data=data_train, method="svmLinear", trControl=train_control)


# make predictions
predictions <- predict(fit2, data_test[,1:4])
# summarize accuracy
table(predictions, data_test$Species)
