library(e1071)
library(bootstrap)

setwd("E:/D/data/")

df <- na.omit( read.csv( "HW03HeartDiseaseCleveland.csv", header=T) )
# df <- na.omit( read.csv( "HW03PimaIndiansDiabetes.csv", header=T) )
target <- df["Class"]
x <- df[-which(colnames(df)=="Class")]
x.scaled <- scale( x )

kernel <- "r"   	# "r" for radial basis function and "l" for linear kernel
cost   <-  1
type   <- "nu-r"  	# "C" for classification and "nu-r" for regression
ifscale <- F    	# to scale or not

  m <- svm( Class~., cbind(x,Class=target[,1]), scale=ifscale, kernel=kernel, type=type, cost=cost)
  targetPred <- predict( m )

  if ( type=="C" )  cm <- table( targetPred, target[,1])      else
                    cm <- table( (sign(targetPred-0.5)+1)/2, target[,1])

  print( cm )
  print( sum(diag(cm))/sum(cm) ) 


#### 10-fold cv

kernel <- "r"   	# "r" for radial basis function and "l" for linear
cost   <-  1
type   <- "nu-r"  	# "C" for classification and "nu-r" for nu-regression
ifscale <- F    	# to scale or not to scale

  ngroup <- 10     	# the number of folds for cv

  theta.fit <- function ( x, y ) 
                  svm( Class~., data.frame( x, Class=y), 
                                scale=ifscale, kernel=kernel, type=type, cost=cost) 
  theta.predict <- function( fit, x ) predict( fit, x ) 

  results <- crossval( x, target[,1], theta.fit, theta.predict, ngroup=ngroup) 
  targetPred <- results$cv.fit

  if ( type=="C" )  cm <- table( targetPred, target[,1])      else
                    cm <- table( (sign(targetPred-0.5)+1)/2, target[,1])

  print( cm )
  print( sum(diag(cm))/sum(cm) ) 



