Input data and find the feature

library(kernlab)
data(spam)
set.seed(333)
smallSpam<- spam[sample(dim(spam)[1],size=10),] #Pick up 10 observations randomly as the sample
spamLabel<- (smallSpam$type=="spam")*1+1
plot(smallSpam$capitalAve,col=spamLabel) #Index mean the order of the dot; the col mean use the color show the spam or non-spam

###From the graph, we can find that the red dot whose captivalave is more than 2.7 and between 2.4 and 2.45;the else are nonspam

rule1<- function(x){
  prediction<- rep(NA,length(x))
  prediction[x>2.7]<- "spam"
  prediction[x<2.40]<- "nonspam"
  prediction[(x>=2.40& x<=2.45)]<- "spam"
  prediction[(x>2.45 & x<= 2.7)]<- "nonspam"
  return(prediction)
}
table(rule1(smallSpam$capitalAve),smallSpam$type)
##          
##           nonspam spam
##   nonspam       5    0
##   spam          0    5

Although the rule1 will make the algorithm fit the sample perfectly, it might cause the overfitting problem for the bigger data, so we set the rule 2

rule2<- function(x){
  prediction<- rep(NA, length(x))
  prediction[x>2.8]<- "spam"
  prediction[x<=2.8]<- "nonspam"
  return(prediction)
}
table(rule2(smallSpam$capitalAve),smallSpam$type)
##          
##           nonspam spam
##   nonspam       5    1
##   spam          0    4

Now, let’s compare the result between different rules

table(rule1(spam$capitalAve),spam$type)
##          
##           nonspam spam
##   nonspam    2141  588
##   spam        647 1225
table(rule2(spam$capitalAve),spam$type)
##          
##           nonspam spam
##   nonspam    2224  642
##   spam        564 1171
sum(rule1(spam$capitalAve)==spam$type)
## [1] 3366
sum(rule2(spam$capitalAve)==spam$type)
## [1] 3395

From the result, rule2 predict 3395, which is greater than 3366.So, rule 2 will be better