## Slide 5
p <- seq(0,1,len=101)
Q <- matrix(NA,nrow=length(p),ncol=3)
Q[,1] <- 1-pmax(p,1-p)
Q[,2] <- 2*p*(1-p)
Q[,3] <- -(p*log(p) + (1-p)*log(1-p))*.5/log(2)
col <- c("black","red","blue")
matplot(p,Q,type="l",lwd=3,lty=1,ylab=expression(Q[m](T)),col=col)
legend("bottom",col=col,lty=1,lwd=3,legend=c("Misclassification","Gini index","Deviance (rescaled)"))

## CHD data
require(rpart)
require(partykit)
require(party)
heart <- read.delim("../../data/heart.txt")
heart$chd <- factor(heart$chd,labels=c("Healthy","CHD"))

## Slide 8
fit0 <- rpart(chd~.,data=heart)
plot(fit0$cptable[,"CP"],fit0$cptable[,"xerror"],xlab=expression(alpha),ylab="Relative Error",type="o",xlim=rev(range(fit0$cptable[,"CP"])),pch=19,log="x",ylim=c(0.5,1.1),col="red",lwd=2)
lines(fit0$cptable[,"CP"],fit0$cptable[,"rel error"],col="gray60",lwd=2)
points(fit0$cptable[,"CP"],fit0$cptable[,"rel error"],col="gray60",pch=19)
legend("bottomleft",lwd=2,pch=19,col=c("gray60","red"),legend=c("In-sample","Cross-validated"))
axis(3,at=fit0$cptable[,"CP"],labels=fit0$cptable[,"nsplit"])

## Slide 9
fit0 <- rpart(chd~.,data=heart)
alpha <- fit0$cptable[which.min(fit0$cptable[,"xerror"]),"CP"]
fit <- prune(fit0,alpha)
plot(as.party(fit),ip_args=list(id=FALSE),tp_args=list(id=FALSE))

## Slide 10
fit <- ctree(chd~.,data=heart)
plot(fit,ip_args=list(id=FALSE),tp_args=list(id=FALSE))

## Slide 15
x <- runif(1000)
y <- 5*x+rnorm(1000)
nd <- data.frame(x=seq(0,1,len=101))
fit.lm <- lm(y~x)
fit.tr <- rpart(y~x)
plot(x,y,pch=19,col="gray",cex=0.5)
lines(xx,predict(fit.lm,nd))
yy <- predict(fit.tr,nd)
brk <- c(0,which(diff(yy)!=0),101)
for (i in 1:(length(brk)-1))
  {
    ind <- (brk[i]+1):brk[i+1]
    lines(xx[ind],yy[ind],col="red",lwd=3)
  }
