################################################### ### chunk number 1: tree1 ################################################### #line 98 "Lect14.Rnw" library(tree) wine<-read.table('wine2.txt',header=T) # tree1<-tree(as.factor(class)~alcohol+malicacid+alcalinity+phenols+flavanoids+proanthocyanins+colorintensity+hue,data=wine) ################################################### ### chunk number 2: tree1b ################################################### #line 104 "Lect14.Rnw" par(mfrow=c(1,1)) plot(tree1) text(tree1) ################################################### ### chunk number 3: tree1c ################################################### #line 121 "Lect14.Rnw" library(rpart) wine<-read.table('wine2.txt',header=T) # tree2<-rpart(as.factor(class)~alcohol+malicacid+alcalinity+phenols+flavanoids+proanthocyanins+colorintensity+hue,data=wine) ################################################### ### chunk number 4: tree1d ################################################### #line 127 "Lect14.Rnw" par(mfrow=c(1,1)) plot(tree2) text(tree2) ################################################### ### chunk number 5: tree2 ################################################### #line 143 "Lect14.Rnw" cvtree1<-cv.tree(tree1,K=10) plot(cvtree1) print(cvtree1) ################################################### ### chunk number 6: tree3 ################################################### #line 159 "Lect14.Rnw" mim<-min(cvtree1$dev) sizesel<-max(2,max(cvtree1$size[cvtree1$dev==mim])) ptree1<-prune.tree(tree1,best=sizesel) plot(ptree1) text(ptree1) ################################################### ### chunk number 7: tree4 ################################################### #line 176 "Lect14.Rnw" #predtree<-predict.tree(ptree1,newdata=wine,type="class") predtree<-predict(ptree1,newdata=wine,type="class") print(table(wine$class,predtree)) ################################################### ### chunk number 8: treerp ################################################### #line 183 "Lect14.Rnw" plotcp(tree2) ################################################### ### chunk number 9: treespl ################################################### #line 201 "Lect14.Rnw" B<-100 varsWine<-c('alcohol','malicacid','alcalinity','phenols','flavanoids','proanthocyanins','colorintensity','hue') # number of random splits Nbrleaves<-matrix(0,B,1) UsedMatrix<-matrix(0,B,8) UsedFirst<-matrix(0,B,8) MSEMatrix<-matrix(0,B,2) for (bb in (1:B)) { # iie<-sample(seq(1,dim(wine)[1]),50) trdata<-wine[-iie,] tedata<-wine[iie,] tree1<-tree(as.factor(class)~alcohol+malicacid+alcalinity+phenols+flavanoids+proanthocyanins+colorintensity+hue,data=trdata) cvtree1<-cv.tree(tree1,K=10) mim<-min(cvtree1$dev) sizesel<-max(2,max(cvtree1$size[cvtree1$dev==mim])) ptree1<-prune.tree(tree1,best=sizesel) Nbrleaves[bb]<-length(unique(ptree1$where)) predtree<-predict(ptree1,newdata=tedata,type="class") rlabels<-tedata[,1] MSEMatrix[bb,1]<-sum(rlabels!=predtree)/length(predtree) predtree<-predict(tree1,newdata=tedata,type="class") MSEMatrix[bb,2]<-sum(rlabels!=predtree)/length(predtree) #UsedMatrix[bb,as.real(summary(ptree1)$used)-1]<-UsedMatrix[bb,as.real(summary(ptree1)$used)-1]+1 wvars<-whichVars(as.vector(summary(ptree1)$used),varsWine) UsedMatrix[bb,]<-UsedMatrix[bb,]+wvars #UsedFirst[bb,as.real(summary(ptree1)$used)[1]-1]<-UsedFirst[bb,as.real(summary(ptree1)$used)[1]-1]+1 wvars<-whichVars(as.vector(summary(ptree1)$used[1]),varsWine) UsedFirst[bb,]<-UsedFirst[bb,]+wvars } ################################################### ### chunk number 10: treepl ################################################### #line 230 "Lect14.Rnw" boxplot(MSEMatrix[,1]-MSEMatrix[,2],main="PE-CV - PE-full") abline(h=0) print(c(mean(MSEMatrix[,1]),mean(MSEMatrix[,2]))) ################################################### ### chunk number 11: treesum ################################################### #line 246 "Lect14.Rnw" sizetrees<-apply(UsedMatrix,1,sum) hist(sizetrees,main="Size trees") print(c("NbrLeaves",fivenum(Nbrleaves))) ################################################### ### chunk number 12: treesum2 ################################################### #line 260 "Lect14.Rnw" modtab<-apply(UsedMatrix,2,sum)/B print(cbind(names(wine)[-1],modtab)) modfirst<-apply(UsedFirst,2,sum)/B print(cbind(names(wine)[-1],modfirst)) ################################################### ### chunk number 13: SAt1 ################################################### #line 269 "Lect14.Rnw" library(tree) SA<-read.table('SA.dat',header=T) ################################################### ### chunk number 14: SAt2 ################################################### #line 273 "Lect14.Rnw" tree1<-tree(as.factor(chd)~log(age)+sbp+adiposity+obesity+typea+alcohol+as.factor(alcind)+tobacco+as.factor(tobind)+as.factor(famhist)+log(ldl),data=SA,minsize=5) par(mfrow=c(1,1)) plot(tree1) text(tree1) ################################################### ### chunk number 15: SAt3 ################################################### #line 289 "Lect14.Rnw" cvtree1<-cv.tree(tree1,K=10) plot(cvtree1) print(cvtree1) ################################################### ### chunk number 16: SAt3b ################################################### #line 294 "Lect14.Rnw" mim<-min(cvtree1$dev) sizesel<-max(2,max(cvtree1$size[cvtree1$dev==mim])) ptree1<-prune.tree(tree1,best=sizesel) plot(ptree1) text(ptree1) ################################################### ### chunk number 17: SApred ################################################### #line 311 "Lect14.Rnw" predtree<-predict.tree(ptree1,newdata=SA,type="class") print(table(SA$chd,predtree)) ################################################### ### chunk number 18: SArandomspl ################################################### #line 318 "Lect14.Rnw" B<-100 varsSA<-c('log(age)','sbp','adiposity','obesity','typea','alcohol','as.factor(alcind)','tobacco','as.factor(tobind)','as.factor(famhist)','log(ldl)') # number of random splits NbrLeaves<-matrix(0,B,1) UsedMatrix<-matrix(0,B,11) UsedFirst<-matrix(0,B,11) MSEMatrix<-matrix(0,B,2) for (bb in (1:B)) { # iie<-sample(seq(1,dim(SA)[1]),50) trdata<-SA[-iie,] tedata<-SA[iie,] tree1<-tree(as.factor(chd)~log(age)+sbp+adiposity+obesity+typea+alcohol+as.factor(alcind)+tobacco+as.factor(tobind)+as.factor(famhist)+log(ldl),data=trdata,minsize=2) cvtree1<-cv.tree(tree1,K=10) mim<-min(cvtree1$dev) sizesel<-max(2,max(cvtree1$size[cvtree1$dev==mim])) ptree1<-prune.tree(tree1,best=sizesel) NbrLeaves[bb]<-length(unique(ptree1$where)) predtree<-predict(ptree1,newdata=tedata,type="class") rlabels<-tedata[,10] MSEMatrix[bb,1]<-sum(rlabels!=predtree)/length(predtree) predtree<-predict(tree1,newdata=tedata,type="class") MSEMatrix[bb,2]<-sum(rlabels!=predtree)/length(predtree) #UsedMatrix[bb,as.real(summary(ptree1)$used)-1]<-UsedMatrix[bb,as.real(summary(ptree1)$used)-1]+1 wvars<-whichVars(as.vector(summary(ptree1)$used),varsSA) UsedMatrix[bb,]<-UsedMatrix[bb,]+wvars #UsedFirst[bb,as.real(summary(ptree1)$used)[1]-1]<-UsedFirst[bb,as.real(summary(ptree1)$used)[1]-1]+1 wvars<-whichVars(as.vector(summary(ptree1)$used[1]),varsSA) UsedFirst[bb,]<-UsedFirst[bb,]+wvars } ################################################### ### chunk number 19: SAsum ################################################### #line 346 "Lect14.Rnw" print(mean(MSEMatrix[,1])) ################################################### ### chunk number 20: SAsum2 ################################################### #line 349 "Lect14.Rnw" sizetrees<-apply(UsedMatrix,1,sum) print(fivenum(sizetrees)) print(fivenum(NbrLeaves)) ################################################### ### chunk number 21: SAsum3 ################################################### #line 354 "Lect14.Rnw" modtab<-apply(UsedMatrix,2,sum)/B print(cbind(names(SA)[-10],modtab)) modfirst<-apply(UsedFirst,2,sum)/B print(cbind(names(SA)[-10],modfirst)) ################################################### ### chunk number 22: treeldl1 ################################################### #line 363 "Lect14.Rnw" tree1<-tree(ldl~log(age)+sbp+adiposity+obesity+typea+alcohol+alcind+tobacco+tobind+as.factor(chd)+as.factor(famhist),data=SA,minsize=5) par(mfrow=c(1,1)) plot(tree1) text(tree1) ################################################### ### chunk number 23: ldlt3 ################################################### #line 379 "Lect14.Rnw" cvtree1<-cv.tree(tree1,K=10) plot(cvtree1) print(cvtree1) ################################################### ### chunk number 24: ldlt3b ################################################### #line 384 "Lect14.Rnw" mim<-min(cvtree1$dev) sizesel<-max(2,max(cvtree1$size[cvtree1$dev==mim])) ptree1<-prune.tree(tree1,best=sizesel) plot(ptree1) text(ptree1) ################################################### ### chunk number 25: ldlpred ################################################### #line 401 "Lect14.Rnw" predtree<-predict.tree(ptree1,newdata=SA) print(sum((SA$ldl-predtree)^2)/length(predtree)) ################################################### ### chunk number 26: ldlrandomspl ################################################### #line 408 "Lect14.Rnw" B<-100 varsSA<-c('log(age)','sbp','adiposity','obesity','typea','alcohol','as.factor(alcind)','tobacco','as.factor(tobind)','as.factor(chd)','as.factor(famhist)') # number of random splits NbrLeaves<-matrix(0,B,1) UsedMatrix<-matrix(0,B,11) UsedFirst<-matrix(0,B,11) MSEMatrix<-matrix(0,B,2) for (bb in (1:B)) { # iie<-sample(seq(1,dim(SA)[1]),50) trdata<-SA[-iie,] tedata<-SA[iie,] tree1<-tree(ldl~log(age)+sbp+adiposity+obesity+typea+alcohol+as.factor(alcind)+tobacco+as.factor(tobind)+as.factor(chd)+as.factor(famhist),data=trdata,minsize=2) cvtree1<-cv.tree(tree1,K=10) mim<-min(cvtree1$dev) sizesel<-max(2,max(cvtree1$size[cvtree1$dev==mim])) ptree1<-prune.tree(tree1,best=sizesel) NbrLeaves[bb]<-length(unique(ptree1$where)) predtree<-predict(ptree1,newdata=tedata) rlabels<-tedata[,12] MSEMatrix[bb,1]<-sum((rlabels-predtree)^2)/length(predtree) predtree<-predict(tree1,newdata=tedata) MSEMatrix[bb,2]<-sum((rlabels-predtree)^2)/length(predtree) #UsedMatrix[bb,as.real(summary(ptree1)$used)-1]<-UsedMatrix[bb,as.real(summary(ptree1)$used)-1]+1 wvars<-whichVars(as.vector(summary(ptree1)$used),varsSA) UsedMatrix[bb,]<-UsedMatrix[bb,]+wvars #UsedFirst[bb,as.real(summary(ptree1)$used)[1]-1]<-UsedFirst[bb,as.real(summary(ptree1)$used)[1]-1]+1 wvars<-whichVars(as.vector(summary(ptree1)$used[1]),varsSA) UsedFirst[bb,]<-UsedFirst[bb,]+wvars } ################################################### ### chunk number 27: ldlsum ################################################### #line 436 "Lect14.Rnw" print(c(mean(MSEMatrix[,1]),mean(MSEMatrix[,2]))) ################################################### ### chunk number 28: ldlsum2 ################################################### #line 439 "Lect14.Rnw" sizetrees<-apply(UsedMatrix,1,sum) print(fivenum(sizetrees)) print(fivenum(NbrLeaves)) ################################################### ### chunk number 29: ldlsum3 ################################################### #line 444 "Lect14.Rnw" modtab<-apply(UsedMatrix,2,sum)/B print(cbind(names(SA)[-12],modtab)) modfirst<-apply(UsedFirst,2,sum)/B print(cbind(names(SA)[-12],modfirst))