[R] About Tree Package
Si Qi L.
liusiqi.nine at gmail.com
Tue Apr 29 00:36:19 CEST 2014
Hi,
I will be very grateful if you can give a help on my problem. I am
really stuck on it now. The problem is that I need to construct a
classification tree model and prune tree in order to test the learning
dataset for getting its sensitivity and specificity. But my codes
seems wrong somewhere, will you guys please help me out? Many
thanks!:D
These are codes:
library(tree)
attach(learning.set1)
Status.f<- factor(Status)
tree1<-tree(Status.f ~ Gender.compl+ X2.4.times.per.month+
Once.a.month+Council.tenant+Living.with.friends.family+Living.with.parents+Owner.occupier+X1.year.to.2.years+X2.to.4.years+X3.months.to.a.year+Less.than.3.months+Empl.compl+Reqloanamount+EmpNetMonthlyPay+Customer_Age+RTI+acc.compl+iic.compl+irb.compl+jbc.compl+jic.compl+jq.compl+kic.compl+lbc.compl+mbc.compl+njc.compl+or.compl+pq.compl+pr.compl+qic.compl+teb.compl+tpb.compl+vbc.compl+yzb.compl+zr.compl,
method="class", data=learning.set1, split="gini")
summary(tree1)
print(tree1)
plot(tree1)
text(tree1)
pfit<-prune.tree(tree1,
k=tree1$cptable[which.min(tree1$cptable[,"xerror"]),"CP"])
plot(pfit, uniform=TRUE)
text(pfit)
pred<-predict(tree1,learning.set1,type="vector")
table1<-table(learning.set1$Status,predict(tree1,type="vector"))
> table1
0 1
0 1429 108
1 273 164
sum<-sum(learning.set1$Status==pred)/length(pred)
sens<- function(table1) { table1[2,2] / sum(table1[,2]) }
spec<- function(table1) { table1[1,1] / sum(table1[,1]) }
myt<-matrix(c(1429,273,108,164), ncol=2)
sens
spec
More information about the R-help
mailing list