My problem is the following, my data has a lot of branch off points and the tree grows very rapidly. The end result is not readable, the end nodes are overlapped and even conversion to rules is more or less useless. I am using the rpart package.
#Scoring model
d = sort(sample(nrow(Memmbers),nrow(Memmbers)* .6))
#select training sample
train<-Memmbers[d, ]
test<-Memmbers[-d, ]
s<-glm(verifikation ~ . - userId,data = Memmbers,family = binomial())
summary(s)
library(ROCR)
#score test data set
test$score <- predict(s,type='response',test)
pred<-prediction(test$score,test$verifikation)
perf<- performance(pred,"tpr","fpr")
plot(perf)
max(attr(perf,'y.values')[[1]]-attr(perf,'x.values')[[1]])
#get results of terms in regression
g<-predict(s,type='terms',test)
#function to pick top 3 reasons
#works by sorting coefficient terms in equation
# and selecting top 3 in sort for each loan scored
ftopk<- function(x,top=3){
res=names(x)[order(x, decreasing = TRUE)][1:top]
paste(res,collapse=";",sep="")
}
# Application of the function using the top 3 rows
topk=apply(g,1,ftopk,top=3)
#add reason list to scored tets sample
test<-cbind(test, topk)
library(rpart)
library(rattle)
fit1 <- rpart(verifikation ~ . - userId, data = train)
fancyRpartPlot(fit1);
test$t<-predict(fit1,type='class',test)
################## PLot tree with priors
#score test data
test$score1 <- predict(fit2,type = 'prob',test)
pred5<-prediction(test$score1[,2],test$verifikation)
perf5<- performance(pred5,"tpr","fpr")
#90-10 priors with smaller complexity parameter to allow more complex trees
fit2 <- rpart(verifikation ~ . - userId , data = train,method = "class",parms = list(prior=c(.9,.1)),cp=.0002)
plot(fit2);text(fit2,pos=2,cex=0.1,col="blue");
#compare complexity
printcp(fit1)
printcp(fit2)
plotcp(fit2)
#convert trees to rules
amess<-asRules(fit2)
t.b<-rpart.rules.table(fit2)
library(rattle)
library(rpart.plot)
library(RColorBrewer)
fancyRpartPlot(fit2)
And here is the output of fancyRpartPlot(fit2)
My goal is to extract some useful rules from the entire process to implement in a score card.