## 10-fold cross-validationÀ» ÅëÇÑ ¸ð¼ö Ž»ö
//»ç¿ëÇÒ µ¥ÀÌÅ͸¦ »ý¼ºÇÑ´Ù.
// "kernlab" ÆÐÅ°ÁöÀÇ spam µ¥ÀÌÅ͸¦ ºÒ·¯¿Â´Ù.
install.packages("kernlab")
library(kernlab)
data(spam)
// ÀüüÀڷḦ 3:1·Î ºÐÇÒÇÑ´Ù. (ÇнÀ : ½ÇÇè)
n <- nrow(spam)
sub <- sample(1:n , round(0.75 * n)) # 1¹ø°~n¹ø° °üÃøÄ¡¸¦ 3:1 ºñÀ²·Î ³ª´«´Ù. (replace ¿É¼ÇÀº µðÆúÆ®°¡ FALSE ÀÌ´Ù.)
spam.1 <- spam[ sub, ]
spam.2 <- spam[-sub, ]
// svm() ÇÔ¼ö¿¡¼ ¸ð¼ö gamma¿Í cost °ªÀ» ´Ù¸£°Ô µÎ¸é ¸ðÇü¼º°ú°¡ ´Ù¸£°Ô ³ªÅ¸³¯ ¼ö ÀÖ´Ù.
// e1071 ÆÐÅ°ÁöÀÇ tune.svm()À¸·Î SVM ¸ðÇüÀÇ ÃÖÀû ÆĶó¹ÌÅ͸¦ ã¾Æº¸ÀÚ.
// ÀÌ ÇÔ¼ö´Â 10-°ã ±³Â÷ Ÿ´ç¼º Æò°¡(10-fold cross-validation) ¹æ¹ýÀ» ¾´´Ù.
// gamma °ª 0.1, 1 , 10
// cost °ª 0.1, 1 , 10
// ÃÑ 9°³ Á¶ÇÕ Áß¿¡¼ ÃÖÀûÀÇ (gamma, cost) Á¶ÇÕÀ» ã¾Æº¸ÀÚ.
// 10-fold cross-validation ÇÏ´Â tune()ÇÔ¼ö¸¦ »ç¿ë
install.packages("e1071")
library(e1071)
tune.svm <- tune(svm, type~. , data= spam.1 , ranges= list(gamma=c(0.1 , 1 , 10) , cost=c(0.1 , 1 , 10))) # ½Ã°£ ¿À·¡ °É¸²
summary(tune.svm)
// °è»ê½Ã°£À» ¾Ë¾Æº»´Ù. (847ÃÊ)