{"word_count": 183, "character_count": 1869}
Consider the following toy dataset, with some spam/ham information, and two words, "viagra" and "lottery".&#62; load(spam.RData)&#62; head(db)      Y viagra lottery27 spam      0       137  ham      0       157 spam      0       089  ham      0       020 spam      1       086  ham      0       0For the first node, compute Gini index for the two variables,&#62; gini=function(variable){+ T=table(db$Y,db[,variable])+ nx=apply(T,2,sum)+ ProbCond=T/matrix(rep(nx,each=2),2,2)+ ProbCond+ Gini=-ProbCond*(1-ProbCond)+ sum(matrix(rep(nx,each=2),2,2)/sum(nx)*Gini)}&#62; gini("viagra")[1] -0.44&#62; gini("lottery")[1] -0.487Here Gini index is maximal for "viagra", so that will be the first node.On the left node (emails without "viagra"), the component of Gini index is&#62; -75/100*(.4*.6+.6*.4)[1] -0.36If we decide to split (using the second word, "lottery"), at this node, the new Gini index would be&#62; idx=which(db$viagra==0)&#62; T=table(db[idx,"Y"],db[idx,"lottery"])&#62; nx=apply(T,2,sum)&#62; ProbCond=T/matrix(rep(nx,each=2),2,2)&#62; Gini=-ProbCond*(1-ProbCond)&#62; sum(matrix(rep(nx,each=2),2,2)/100*+       Gini)[1] -0.333Since Gini index is larger, we decide to split (based on the second word) here. On the other node (emails with "viagra"), the component of Gini index is&#62; -25/100*(.8*.2+.2*.8)[1] -0.08and if decide to split (again, according to the second word), we get&#62; idx=which(db$viagra==1)&#62; T=table(db[idx,"Y"],db[idx,"lottery"])&#62; nx=apply(T,2,sum)&#62; ProbCond=T/matrix(rep(nx,each=2),2,2)&#62; Gini=-ProbCond*(1-ProbCond)&#62; sum(matrix(rep(nx,each=2),2,2)/100*+       Gini)[1] -0.0792which is only slightly larger. Splitting would not be very interesting, here. To visualize the tree, use&#62; library(rpart)&#62; arbre = rpart(factor(Y)~.,data=db)&#62; library(rpart.plot)&#62; rpart.plot(arbre,type=4,extra=6)&#160;&#160;