RDX2 X  g11.1 source6 function(device="") { 9 if(device!="")gfile(width=4.8, height=4, device=device) B oldpar <- par(mfrow=c(2,6),mar=c(4.6,2.6,1.6,1.1),mgp=c(1,.5,0),  oma=c(0,3,0,0))  on.exit(par(oldpar))  require(DAAG) data(spam7) @ nam <- c("crl.tot", "dollar", "bang", "money", "n000", "make") # nr <- sample(1:dim(spam7)[1],500)  yesno<-spam7$yesno[nr]  spam7 <- spam7[nr,nam]  nam2 <- names(spam7)  nam2[2] <- "$"  nam2[1] <- "Total runs"  nam2[5] <- "000"  spam7.2 <- spam7 # spam7.2[,1]<-log(spam7.2[,1]+0.5) ' spam7.2[,2:6]<-log(spam7.2[,2:6]+0.5)  for (namtxt in nam){ C boxplot(split(spam7[,namtxt],yesno),cex=0.65,axes=F,boxwex=0.5) box()  par(mgp=c(1,.75,0)) axis(2)  par(mgp=c(1,.25,0)) $ axis(1,at=1:2,labels=c("n","y"))  i <- match(namtxt,nam) + mtext(side=1,line=1.75,nam2[i],adj=0.5) 1 if(i==1)mtext(side=1,line=2.75,"of capitals")  } 9 xval <-c(.1,.2,.5,1,2,5,10,20,50,100,200,500,1000,2000)  for (namtxt in nam){ E boxplot(split(spam7.2[,namtxt],yesno),cex=0.65,axes=F,boxwex=0.5) box() ! ranx <- range(spam7[,namtxt]) . yloc<-xval[xval>=min(ranx)&xval=7џ min8џўў <7џ max8џўўўўў џџџ?№?шўў-џ@.џ"џ#џ:џ?рўў/џ paste round:џ?№ўўў џџџ?№?аўў-џ?№.џџ?№@ў/џџ n yўў џ0џ1џ%џџўўџ6џ0џ?№ўџ2џ3џ@4џ?ш (Logarithmic scales) outer T.џ?аў2џ3џ?№4џ@ of capitalsўўў2џ3џ?№4џ?ќџџ0џў5џ?рўўў џ figtxt@џ 5Scatterplot matrix for selected variables, in 200 out 2 of 4601 rows in the SPAM data base. Mail was all 4 received by one individual. The figure on the left 6 is for untransformed data, while that on the right is  for log transformed data.ўў catDџ  ўџџџ ў dev.offўўў g11.10џ function(device=""){ = if(device!="")gfile(width=1.5, height=1.5, device=device)  if(!exists("mifemb.rpart")) : mifem.rpart <- rpart(outcome ~ ., method="class", 0 data = mifem, cp = 0.0025)  if(!exists("spam7b.rpart")) 2 mifemb.rpart <- prune(mifem.rpart, cp=0.006) F oldpar<-par(mar=c(1.6,1.6,1.1,1.6),mgp=c(1.1,.5,0),oma=c(0,0,0,0))  on.exit(par(oldpar))  plot(mifemb.rpart)  par(xpd=TRUE) ) text(mifemb.rpart, use.n=T, digits=3)  par(xpd=FALSE)  if(device!="")dev.off()  }ў§џ ўџџџџ ўџџ?ј џ?јџџўўџ ! exists mifemb.rpartўў џ mifem.rpart rpart ~ outcome .ў method classџ mifem cp?dzсGЎ{ўўўџHџIџ spam7b.rpartўў џ mifemb.rpart pruneJџQџ?x“tМj~њўўў џ џ џџџ?љ™™™™™š?љ™™™™™š?ё™™™™™š?љ™™™™™šўџџ?ё™™™™™š?рўџџўўўџ џ џўў plotRџў џ xpd ў textRџ use.nCџ digits@ў џUџ ўџџџ ўFџўўў g11.11џ function(device=""){ > if(device!="")gfile(width=4.5, height=3.75, device=device)  if(!exists("spam7a.rpart")) > spam7a.rpart <- rpart(yesno ~ crl.tot+dollar+bang+money+ < n000+make, data=spam7, cp=0.001) . if(!exists("spam7b.rpart"))spam7b.rpart <- # prune(spam7a.rpart, cp=0.003) H oldpar<-par(mar=c(1.1,1.1,0.6,1.1),mgp=c(1.5,.5,0),oma=c(0,0,0,0),  cex=0.8)  on.exit(par(oldpar)) plot(spam7b.rpart,uniform=T) ' text(spam7b.rpart,cex=0.9,digits=3)  if(device!="")dev.off()  }ў§џ ўџџџџ ўџџ@ џ@џџўўџHџIџ spam7a.rpartўў џ spam7a.rpartKџLџџ#џ#џ#џ#џ#џ crl.tot dollarў bangў moneyў n000ў makeўўџџQџ?PbMвёЉќўўўџHџIџ spam7b.rpartўў џ spam7b.rpartSџZџQџ?h“tМj~њўўў џ џ џџџ?ё™™™™™š?ё™™™™™š?у333333?ё™™™™™šўџџ?ј?рўџџў(џ?щ™™™™™šўўџ џ џўўTџaџ uniformCџўVџaџ(џ?ьЬЬЬЬЬЭXџ@ўџџџ ўFџўўў g11.12џ 'function(acc.df=acctree.df, device=""){ 9 if(device!="")gfile(width=4, height=2, device=device) J ## for(i in 1:100){cvmat2[i,] <- compare.acc.trees(); cat(i,"\n")} F oldpar<-par(mar=c(4.1,4.1, 1.6, 1.6), mgp=c(2.5, 0.75,0), pty="s",  mfrow=c(1,2)  )  on.exit(par(oldpar)) 0 lims <- range(acc.df[,c(2,7,8)], na.rm=TRUE)  D plot(rfcvI ~ rftest, data=acc.df, xlab="Error rate - subset II", B ylab="OOB Error - fit to subset I", xlim=lims, ylim=lims)  abline(0,1) ( mtext(side=3, line=0.25, "A", adj=0) E plot(rptest ~ rftest, data=acc.df, xlab="Error rate - subset II", , ylab="rpart Error rate, subset II",  xlim=lims, ylim=lims)  abline(0,1) ( mtext(side=3, line=0.25, "B", adj=0)  if(device!="")dev.off()  }ў§ acc.df acctree.dfџ ўџџџџ ўџџ@ џ@џџўў џ џ џџџ@ffffff@ffffff?љ™™™™™š?љ™™™™™šўџџ@?шў pty s џџ?№@ўўўџ џ џўў џ lims9џџdџћџ@@@ ўў na.rm ўўTџLџ rfcvI rftestўџdџ xlab Error rate - subset II ylab OOB Error - fit to subset I xlimgџ ylimgџў abline?№ў2џ3џ@4џ?а A5џўTџLџ rptestjџўџdџkџ Error rate - subset IIlџ rpart Error rate, subset IImџgџnџgџўoџ?№ў2џ3џ@4џ?а B5џўџџџ ўFџўўў g11.2џ function(device=""){ > if(device!="")gfile(width=3.0, height=2.75, device=device) B nam <- c("crl.tot", "dollar", "bang", "money", "n000", "make")  library(rpart) F oldpar<-par(mar=c(1.1,1.1,0.6,0.6),mgp=c(1.5,.5,0),oma=c(0,0,0,0))  on.exit(par(oldpar))  if(!exists("spam7.rpart")) B spam7.rpart<-rpart(formula = yesno ~ crl.tot + dollar + bang ; + money + n000 + make, data=spam7)  plot(spam7.rpart)  par(xpd=TRUE)  text(spam7.rpart)  par(xpd=FALSE)  if(device!="")dev.off()  }ў§$џ ўџџџџ ўџџ@ џ@џџўў џџџ crl.tot dollar bang money n000 makeўў libraryKџў џ џ џџџ?ё™™™™™š?ё™™™™™š?у333333?у333333ўџџ?ј?рўџџўўўџ џ џўўџHџIџ spam7.rpartўў џ spam7.rpartKџ formulaLџџ#џ#џ#џ#џ#џ[џ\џў]џў^џў_џў`џўўџџўўўTџsџў џUџ ўVџsџў џUџ ўџџџ ўFџўўў g11.3џ function(device="") { = if(device!="")gfile(width=2.25, height=2.25, device=device)  require(rpart) @ nam <- c("crl.tot", "dollar", "bang", "money", "n000", "make") ) Criterion <- factor(paste("Leaf", 1:5))  Node <- c(1,2,3,4,5) ; tree.df <- data.frame(Criterion = Criterion, Node = Node)  nobs <- dim(tree.df)[[1]] 3 u.tree <- rpart(Node ~ Criterion, data = tree.df, > control = list(minsplit = 2, minbucket = 1)) , oldpar <- par(mar = c(1.1, 1.1, 0.6, 1.1))  on.exit(par(oldpar))  plot(u.tree, uniform=T) par(xpd=T)  text(u.tree) par(xpd=F) H tx <- paste("Tree labelling. For generating this illustrative tree,", J "\nthe only term used to determine the split is a variable", H "\nwith the name `Criterion'. More generally, different", J "\nvariable or factor names may appear at different nodes.", C "\nTerminal nodes (`leaves') are numbered 1, ..., 5")  cat(tx, "\n")  if(device!="")dev.off() invisible() }ў§$џ ўџџџџ ўџџ@ џ@џџўўџKџў џџџ crl.tot dollar bang money n000 makeўў џ Criterion factor@џ Leafџ?№@ўўўў џ Nodeџ?№@@@@ўў џ tree.df data.framevџvџxџxџўў џ nobs [[џyџў?№ўў џ u.treeKџLџxџvџўџyџ control list minsplit@ minbucket?№ўўў џ џ џџџ?ё™™™™™š?ё™™™™™š?у333333?ё™™™™™šўўўџ џ џўўTџ}џbџCџў џUџCџўVџ}џў џUџ*џў џ tx@џ 7Tree labelling. For generating this illustrative tree, 8 the only term used to determine the split is a variable 6 with the name `Criterion'. More generally, different 8 variable or factor names may appear at different nodes. 1 Terminal nodes (`leaves') are numbered 1, ..., 5ўўEџ‚џ  ўџџџ ўFџўў invisibleўў g11.4џ function(device="") { = if(device!="")gfile(width=2.25, height=2.25, device=device) @ nam <- c("crl.tot", "dollar", "bang", "money", "n000", "make") + fac <- factor(rep(paste("Leaf", 1:2), 3))  x <- rep(1:3, rep(2, 3)) Node <- 1:6 > assign("tree.df", data.frame(fac = fac, x = x, Node = Node))  nobs <- dim(tree.df)[[1]] 1 u.tree <- rpart(Node ~ fac + x, data = tree.df, K control = list(minsplit = 2, minbucket = 1, cp = 1e-009)) @ oldpar <- par(mar = c(1.1, 1.1, 1.1, 1.1), mgp = c(1, 0.5, 0), # oma=c(0.5,0.5,0,0))  on.exit(par(oldpar))  plot(u.tree, uniform=T)  par(xpd=TRUE)  text(u.tree)  par(xpd=FALSE)  figtxt <- paste( @ "Here an illustrative tree is generated from", I "\none explanatory factor (`fac') and one explanatory", I "\nvariable (`x'). For factors, the levels that lead", I "\nto a branch to the left are given. For variables,", H "\nthe range of values is given that leads to taking", 1 "\nthe left branch.", sep = "")  cat(figtxt,"\n")  if(device!="")dev.off() invisible() }ў§$џ ўџџџџ ўџџ@ џ@џџўў џџџ crl.tot dollar bang money n000 makeўў џ facwџ rep@џ Leafџ?№@ўў@ўўў џ x†џџ?№@ў†џ@@ўўў џxџџ?№@ўў assign tree.dfzџ…џ…џ‡џ‡џxџxџўў џ{џ|џџyџў?№ўў џ}џKџLџxџ#џ…џ‡џўўџyџ~џџ€џ@џ?№Qџ>. ш&ж•ўўў џ џ џџџ?ё™™™™™š?ё™™™™™š?ё™™™™™š?ё™™™™™šўџџ?№?рўџџ?р?рўўўџ џ џўўTџ}џbџCџў џUџ ўVџ}џў џUџ ў џDџ@џ +Here an illustrative tree is generated from 3 one explanatory factor (`fac') and one explanatory 3 variable (`x'). For factors, the levels that lead 3 to a branch to the left are given. For variables, 2 the range of values is given that leads to taking  the left branch. sep ўўEџDџ  ўџџџ ўFџўўƒџўў g11.5џ Afunction(cuts = c(1845, 2567.5, 2747.5, 3087.5, 3855), device="") {  require(MASS)  data(car.test.frame) = if(device!="")gfile(width=2.75, height=2.75, device=device) M titl <- paste("Mileage versus Weight, for cars described in US April 1990", 0 "\nConsumer Reports.", sep = "") @ oldpar <- par(mar = c(4.1,4.1,1.6,1.1), mgp = c(2.5, 0.75, 0))  par(mex = 1, cex = 1)  on.exit(par(oldpar)) ! assign("car.df",car.test.frame)  attach(car.df) : u.lo <- loess(Mileage~Weight, data = car.df, span = 2/3) 5 plot(Mileage~Weight, data=car.df, xlab = "Weight", + ylab = "Miles per gallon", sub = "") % xy <- loess.smooth(Weight, Mileage)  ord<-order(xy$x)  lines(xy$x[ord],xy$y[ord]) % new.df <- data.frame(Weight = cuts) ( hat <- predict(u.lo, newdata = new.df)  new.df$hat <- hat  print(summary(u.lo))  detach(car.df)  cat(titl,"\n")  if(device!="")dev.off()  invisible(new.df) }ў§ cutsџ@œд@Є@Ѕw@Ј@Ўўџ ўџџ MASSўџ car.test.frameўџџџ ўџџ@ џ@џџўў џ titl@џ :Mileage versus Weight, for cars described in US April 1990  Consumer Reports.‰џ ўў џ џ џџџ@ffffff@ffffff?љ™™™™™š?ё™™™™™šўџџ@?шўўў џ mex?№(џ?№ўџ џ џўўˆџ car.dfџў attach car.dfў џ u.lo loessLџ Mileage Weightўџ‘џ span /@@ўўўTџLџ”џ•џўџ‘џkџ Weightlџ Miles per gallon sub ў џ xy loess.smooth•џ”џўў џ ord orderџ™џ‡џўўў linesџџ™џ‡џў›џўџџ™џ yў›џўў џ new.dfzџ•џ‹џўў џ hat predict’џ newdataŸџўў џџŸџ џў џў print summary’џўў detach‘џўEџŽџ  ўџџџ ўFџўўƒџŸџўў g11.6џ function(device="") { < if(device!="")gfile(width=3.25, height=2.5, device=device)  on.exit(par(oldpar)) F tx <- paste("Tree-based model for predicting Mileage given Weight,", H "\nfor cars described in US April 1990 Consumer Reports.", I "\nFor present illustrative purposes, split criteria have", G "\nbeen changed from the RPART default, to increase the", G "\nnumber of splits. Notice that for this plot we have", 1 "\nused uniform vertical spacing.") @ oldpar <- par(mar = c(1.6, 1.1, 1.6, 1.1), mgp = c(1, 0.5, 0),  oma=c(0,1,0,0))  par(mex = 1, cex = 1)  on.exit(par(oldpar)) " names(car.test.frame)[6] <- "Wt" ? car.tree <- rpart(Mileage~Wt, data=car.test.frame, control = D list(minsplit = 10, minbucket = 5, cp = 0.0001))  plot(car.tree, uniform = T)  par(xpd=TRUE) ' text(car.tree, digits = 3, use.n = T)  par(xpd=FALSE)  cat(tx,"\n")  if(device!="")dev.off()  invisible(car.tree) }ў§$џ ўџџџџ ўџџ@  џ@џџўўџ џ џўў џ‚џ@џ 5Tree-based model for predicting Mileage given Weight, 6 for cars described in US April 1990 Consumer Reports. 7 For present illustrative purposes, split criteria have 5 been changed from the RPART default, to increase the 5 number of splits. Notice that for this plot we have  used uniform vertical spacing.ўў џ џ џџџ?љ™™™™™š?ё™™™™™š?љ™™™™™š?ё™™™™™šўџџ?№?рўџџ?№ўўў џџ?№(џ?№ўџ џ џўў џџ џџў@ў Wtў џ car.treeKџLџ”џ Wtўџџ~џџ€џ@$џ@Qџ?6тыC-ўўўTџЇџbџCџў џUџ ўVџЇџXџ@WџCџў џUџ ўEџ‚џ  ўџџџ ўFџўўƒџЇџўў g11.7џ function(device="") { = if(device!="")gfile(width=2.75, height=2.75, device=device)  on.exit(par(oldpar)) H titl <- paste("Tree-based model for the mileage given weight data of", H "\nFig. 10.6, but with split criteria set to the RPART", J "\ndefault, and with vertical spacing now set to reflect", K "\nthe change in residual sum of squares. (In Fig. 10.6,", J "\nnon-uniform spacing would have given splits that were", 4 "\nbunched up at the lower nodes.)")  @ oldpar <- par(mar = c(1.1, 1.1, 1.1, 1.1), mgp = c(1, 0.5, 0), # oma=c(0.5,0.5,0,0))  par(mex = 1, cex = 1)  on.exit(par(oldpar))  require(MASS)  data(car.test.frame) < car.tree <- rpart(Mileage ~ Weight, data = car.test.frame)  plot(car.tree, uniform = F) ' text(car.tree, digits = 3, use.n = T)  cat("\n",titl,"\n")  if(device!="")dev.off()  invisible(car.tree) }ў§џ ўџџџџ ўџџ@ џ@џџўўџ џ џўў џŽџ@џ 5Tree-based model for the mileage given weight data of 4 Fig. 10.6, but with split criteria set to the RPART 6 default, and with vertical spacing now set to reflect 7 the change in residual sum of squares. (In Fig. 10.6, 6 non-uniform spacing would have given splits that were bunched up at the lower nodes.)ўў џ џ џџџ?ё™™™™™š?ё™™™™™š?ё™™™™™š?ё™™™™™šўџџ?№?рўџџ?р?рўўў џџ?№(џ?№ўџ џ џўўџŒџўџџў џЇџKџLџ”џ•џўџџўўTџЇџbџ*џўVџЇџXџ@WџCџўEџ  Žџ  ўџџџ ўFџўўƒџЇџўў g11.8џ! function(device="") { : if(device!="")gfile(idth=4.5, height=2.5, device=device) J titl <- paste("R plot designed to assist in choosing tree size for the", / "\ncar weight & mileage data.") D oldpar <- par(mar = c(4.1, 4.1, 3.1, 2.6), mgp = c(2.75, 0.75, 0))  on.exit(par(oldpar)) > car.rpart1 <- rpart(Price~., data=car.test.frame, cp=0.0025) > car.rpart2 <- rpart(Price~., data=car.test.frame, cp=0.0025) > car.rpart3 <- rpart(Price~., data=car.test.frame, cp=0.0025)  pr1 <- printcp(car.rpart1)  pr2 <- printcp(car.rpart2)  pr3 <- printcp(car.rpart3)  cp <- pr1[,"CP"] ( cp0 <- sqrt(cp*c(Inf,cp[-length(cp)]))  nsplit <- pr1[,"nsplit"] 6 plot(nsplit,pr1[,"rel error"], xlab="No. of splits", ' ylab="Relative error", type="b") / axis(3,at=nsplit, labels=paste(round(cp0,3))) / mtext(side=3, line=2, "Complexity parameter") ( mtext(side =3, line=2.5,"A", adj=-0.2) 3 plot(nsplit,pr1[,"xerror"], xlab="No. of splits", , ylab="Xval relative error", type="b") . lines(nsplit,pr2[,"xerror"],lty=2, type="b") . lines(nsplit,pr2[,"xerror"],lty=3, type="b") . lines(nsplit,pr3[,"xerror"],lty=3, type="b") / axis(3,at=nsplit,labels=paste(round(cp0, 3))) . mtext(side=3,line=2, "Complexity parameter") ( mtext(side =3, line=2.5,"B", adj=-0.2)  cat(titl, "\n")  if(device!="")dev.off() invisible() }ў§џ ўџџџџ ўџ idth@ џ@џџўў џŽџ@џ 7R plot designed to assist in choosing tree size for the  car weight & mileage data.ўў џ џ џџџ@ffffff@ffffff@ЬЬЬЬЬЭ@ЬЬЬЬЬЭўџџ@?шўўўџ џ џўў џ car.rpart1KџLџ PriceNџўџџQџ?dzсGЎ{ўў џ car.rpart2KџLџ­џNџўџџQџ?dzсGЎ{ўў џ car.rpart3KџLџ­џNџўџџQџ?dzсGЎ{ўў џ pr1 printcpЌџўў џ pr2БџЎџўў џ pr3БџЏџўў џQџџАџћ CPўў џ cp0 sqrt *Qџџ№џQџ - lengthQџўўўўўўў џ nsplitџАџћ nsplitўўTџЙџџАџћ rel errorўkџ No. of splitslџ Relative error type bў-џ@.џЙџ/џ@џAџДџ@ўўў2џ3џ@4џ@ Complexity parameterў2џ3џ@4џ@ A5џЗџ?Щ™™™™™šўўTџЙџџАџћ xerrorўkџ No. of splitslџ Xval relative errorКџ bўџЙџџВџћ xerrorў lty@Кџ bўџЙџџВџћ xerrorўЛџ@Кџ bўџЙџџГџћ xerrorўЛџ@Кџ bў-џ@.џЙџ/џ@џAџДџ@ўўў2џ3џ@4џ@ Complexity parameterў2џ3џ@4џ@ B5џЗџ?Щ™™™™™šўўEџŽџ  ўџџџ ўFџўўƒџўў g11.9џ function(device=""){ @ if(device!="")hardcopy(width=2.5, height=2.5, device=device) N oldpar<-par(mar=c(4.1,4.1,3.1,3.1),mgp=c(2,.5,0), oma=c(0,0,0,0), pty="s",  las=0)  on.exit(par(oldpar))  require(DAAG)  data(mifem)  if(!exists("mifem.rpart")) > mifem.rpart <- rpart(outcome ~ ., data=mifem, cp=0.0025)  plotcp(mifem.rpart)  if(device!="")dev.off()  }ў§џ ўџџџџ ў hardcopyџ@ џ@џџўў џ џ џџџ@ffffff@ffffff@ЬЬЬЬЬЭ@ЬЬЬЬЬЭўџџ@?рўџџўfџ s lasўўџ џ џўўџџўџPџўџHџIџ mifem.rpartўў џJџKџLџMџNџўџPџQџ?dzсGЎ{ўўў plotcpJџўџџџ ўFџўўў gdumpџ 7function(fnam=NULL, prefix="~/r-book/ed2/figures/figs", K xtras=c("hardcopy","renum.fun","renum.files"), splitchar="/ch"){  if(is.null(fnam)){  path <- getwd() 7 pathtag <- strsplit(path, "/ch", fixed=TRUE)[[1]] 5 fnam <- paste(prefix, pathtag[length(pathtag)], ! ".R", sep="")  } - else fnam <- paste(prefix, fnam, sep="/") C objnames <- c(objects(pattern="^g", envir=sys.frame(0)), xtras) & cat("\nDump to file:", fnam, "\n")  print(objnames)  dump(objnames, fnam)  }ў§ fnamў prefix ~/r-book/ed2/figures/figs xtrasџ hardcopy renum.fun renum.filesў splitchar /chўџџ is.nullСџўџ џ path getwdўў џ pathtag|џ strsplitЦџ /ch fixed ў?№ўў џСџ@џТџџШџИџШџўў .R‰џ ўўў џСџ@џТџСџ‰џ /ўўў џ objnamesџ objects pattern ^g envir sys.frameўўУџўўEџ  Dump to file:Сџ  ўЃџЫџў dumpЫџСџўў gsaveџ 7function(fnam=NULL, prefix="~/r-book/ed2/figures/figs", K splitchar="/ch", xtras=c("hardcopy","renum.fun","renum.files")){  if(is.null(fnam)){  path <- getwd() 7 pathtag <- strsplit(path, "/ch", fixed=TRUE)[[1]] 5 fnam <- paste(prefix, pathtag[length(pathtag)], % ".RData", sep="")  } - else fnam <- paste(prefix, fnam, sep="/") C objnames <- c(objects(pattern="^g", envir=sys.frame(0)), xtras) & cat("\nDump to file:", fnam, "\n")  print(objnames) " save(list=objnames, file=fnam)  }ў§СџўТџ ~/r-book/ed2/figures/figsФџ /chУџџ hardcopy renum.fun renum.filesўўџџХџСџўџ џЦџЧџўў џШџ|џЩџЦџ /chЪџ ў?№ўў џСџ@џТџџШџИџШџўў .RData‰џ ўўў џСџ@џТџСџ‰џ /ўўў џЫџџЬџЭџ ^gЮџЯџўўУџўўEџ  Dump to file:Сџ  ўЃџЫџў saveџЫџ fileСџўў gx11.1џ 'function(acc.df=acctree.df, device=""){ 9 if(device!="")gfile(width=4, height=2, device=device) J ## for(i in 1:100){cvmat2[i,] <- compare.acc.trees(); cat(i,"\n")} F oldpar<-par(mar=c(4.1,4.1, 1.6, 1.6), mgp=c(2.5, 0.75,0), pty="s",  mfrow=c(1,2)  )  on.exit(par(oldpar)) + lims <- range(acc.df[,2:4], na.rm=TRUE)  B plot(rpcvI~rptest, data=acc.df, xlab="Error rate - subset II", A ylab="cv Error - fit to subset I", xlim=lims, ylim=lims)  abline(0,1) ( mtext(side=3, line=0.25, "A", adj=0) F plot(rpSEtest~rptest, data=acc.df, xlab="Error rate -- subset II", 2 ylab="Error - subset II, 1SE rule model",  xlim=lims, ylim=lims)  abline(0,1) ( mtext(side=3, line=0.25, "B", adj=0)  if(device!="")dev.off()  }ў§dџeџџ ўџџџџ ўџџ@ џ@џџўў џ џ џџџ@ffffff@ffffff?љ™™™™™š?љ™™™™™šўџџ@?шўfџ s џџ?№@ўўўџ џ џўў џgџ9џџdџћџ@@ўўhџ ўўTџLџ rpcvIpџўџdџkџ Error rate - subset IIlџ cv Error - fit to subset Imџgџnџgџўoџ?№ў2џ3џ@4џ?а A5џўTџLџ rpSEtestpџўџdџkџ Error rate -- subset IIlџ !Error - subset II, 1SE rule modelmџgџnџgџўoџ?№ў2џ3џ@4џ?а B5џўџџџ ўFџўўўНџџ4 =function(width=3.75, height=3.75, color=FALSE, trellis=FALSE, H device=c("","pdf","ps"), path="~/r-book/ed2/Art/", file=NULL, D format=c("nn-nn", "name"), split="\\.", pointsize=c(8,4),  fonts=NULL,  horiz=FALSE, ...){ ) if(!trellis)pointsize <- pointsize[1]  funtxt <- sys.call(1) B nam <- strsplit(as.character(funtxt), "(", fixed=TRUE)[[1]][1] 3 suffix <- switch(device, ps=".eps", pdf=".pdf") N if(is.character(path) & nchar(path)>1 & substring(path, nchar(path))!="/") & path <- paste(path, "/", sep="") - if(is.null(file)) if(format[1]=="nn-nn"){ C if(!is.null(split))dotsplit <- strsplit(nam, split)[[1]] else  dotsplit <- nam 8 if(length(dotsplit)==1)dotsplit <- c("", dotsplit) E nn2 <- paste(if(nchar(dotsplit[2])==1)"0" else "", dotsplit[2],  sep="")  if(nchar(dotsplit[1])>0){ O numstart <- which(unlist(strsplit(dotsplit[1], "")) %in% paste(0:9))[1] / nn1 <- substring(dotsplit[1], numstart) G nn1 <- paste(if(nchar(nn1) == 1) "0" else "", nn1, "-", sep="")  } else nn1 <- "" % file <- paste(nn1, nn2, sep="")  } else file <- nam L if(nchar(file)>4 & substring(file, nchar(file)-nchar(suffix)+1)==suffix)  suffix <- "" - file <- paste(path, file, suffix, sep="") : print(paste("Output will be directed to file:", file))  dev.out <- device[1] 6 dev.fun <- switch(dev.out, pdf=pdf, ps=postscript)  if(trellis){  library(lattice)  if(device=="ps") 1 trellis.device(file=file, device=dev.fun, ? color = color, horiz=horiz, fonts=fonts, < width=width, height=height, ...) else < trellis.device(file=file, device=dev.fun, fonts=fonts, D color = color, width=width, height=height, ...) R trellis.par.set(list(fontsize=list(text=pointsize[1], points=pointsize[2]))) } else  if (dev.out!=""){  print(c(width, height))  if(device=="ps") F dev.fun(file=file, paper="special", horiz=horiz, fonts=fonts, M width=width, height=height, pointsize=pointsize[1], ...) else 6 dev.fun(file=file, paper="special", fonts=fonts, L width=width, height=height, pointsize=pointsize[1], ...)  } D if(trellis)trellis.par.set(list(fontsize=list(text=pointsize[1], < points=pointsize[2])))  }ў§џ@ џ@ color  trellis џџ  pdf psўЦџ ~/r-book/ed2/Art/гџў formatџ nn-nn nameў'џ \. pointsizeџ@ @ў fontsў horiz  ...ћў srcref  ) srcfileђ encoding native.enc timestampAбрЧзР class POSIXt POSIXctў filename /tmp/johnm.hardcopy.R wd /Users/johnm/r/ch11ўўуџ srcfileўуџ srcrefў пџрџуџ srcrefў   Bпџрџуџ srcrefў   3пџрџуџ srcrefў   &пџрџуџ srcrefў  пџрџуџ srcrefў пџрџуџ srcrefў -пџрџуџ srcrefў :пџрџуџ srcrefў пџрџуџ srcrefў 6пџрџуџ srcrefў  2пџрџуџ srcrefў 34<пџрџуџ srcrefўпџрџўџџHџиџў џкџџкџ?№ўўў џ funtxt sys.call?№ўў џџџ|џЩџ as.characterцџў (Ъџ ў?№ў?№ўў џ suffix switchџ ps .eps pdf .pdfўўџ;џ;џ is.characterЦџў > ncharЦџў?№ўўџ substringЦџяџЦџўў /ўў џЦџ@џЦџ /‰џ ўўўџХџгџўџ6џџйџ?№ў nn-nnўоџ пџрџуџ srcrefў 8пџрџуџ srcrefў пџрџуџ srcrefў пџрџуџ srcrefў %пџрџуџ srcrefўпџрџўџџHџХџ'џўў џ dotsplit|џЩџџ'џў?№ўў џёџџўўџ6џИџёџў?№ў џёџџ ёџўўў џ nn2@џџ6џяџџёџ@ўў?№ў 0 ўџёџ@ў‰џ ўўџюџяџџёџ?№ўўўоџ  Oпџрџуџ srcrefў  /пџрџуџ srcrefў  Gпџрџуџ srcrefўпџрџўџ џ numstartџ which %in% unlistЩџџёџ?№ў ўў@џџ@"ўўўў?№ўў џ nn1№џџёџ?№ўѓџўў џїџ@џџ6џяџїџў?№ў 0 ўїџ -‰џ ўўў џїџ ўў џгџ@џїџђџ‰џ ўўў џгџџўўўџ;џюџяџгџў@ў6џ№џгџ#џЗџяџгџўяџщџўў?№ўўщџўў џщџ ўў џгџ@џЦџгџщџ‰џ ўўЃџ@џ Output will be directed to file:гџўў џ dev.outџџ?№ўў џ dev.funъџјџьџьџыџ postscriptўўџиџоџ !!пџрџуџ srcrefў "'Dпџрџуџ srcrefў ))Rпџрџуџ srcrefўпџрџўџrџ latticeўџ6џџ psў trellis.deviceгџгџџљџзџзџмџмџлџлџџџ џ џнџўќџгџгџџљџлџлџзџзџџџ џ џнџўў trellis.par.setџ fontsizeџVџџкџ?№ў pointsџкџ@ўўўўўџџјџ ўоџ ,,пџрџуџ srcrefў -1Fпџрџуџ srcrefўпџрџўџЃџџџ џўўџ6џџ psўљџгџгџ paper specialмџмџлџлџџџ џ џкџџкџ?№ўнџўљџгџгџџ specialлџлџџџ џ џкџџкџ?№ўнџўўўўўџиџ§џџўџџVџџкџ?№ўџџџкџ@ўўўўўў renum.funџ :function(from.prefix=NULL, to.prefix="g",from=4:1, to=4:1, % splitchars="/ch", doit=F){  path <- getwd() : pathtag <- strsplit(path, splitchars, fixed=TRUE)[[1]] + if(length(pathtag)==1)endbit <- "" else & endbit <- pathtag[length(pathtag)] 1 to.prefix <- paste(to.prefix, endbit, sep="") 4 if(is.null(from.prefix))from.prefix <- to.prefix  for(i in 1:length(to)) M {txt<-paste(to.prefix,".",to[i]," <- ", from.prefix,".",from[i],sep="") 7 if(doit)eval(parse(text=txt),envir=sys.frame(0))  print(txt) " if(from.prefix!=to.prefix){ B rm.txt <- paste("rm(",from.prefix,".",from[i],")",sep="") < if(doit)eval(parse(text=rm.txt),envir=sys.frame(0))  print(rm.txt)  }  }  }ў§$ from.prefixў to.prefix g$ fromџ@?№ў$ toџ@?№ў splitchars /ch$ doit*џўџ џЦџЧџўў џШџ|џЩџЦџџЪџ ў?№ўўџ6џИџШџў?№ў џ endbit ў џџџШџИџШџўўўў џџ@џџџ‰џ ўўџХџџў џџџўў$џ0џџ?№Иџџўўџ џ txt@џџ .џџ0џў  <- џ .џџ0џў‰џ ўўџџ eval parseVџ џўЮџЯџўўўЃџ џўџџџџўџ џ rm.txt@џ rm(џ .џџ0џў )‰џ ўўџџ џ џVџ џўЮџЯџўўўЃџ џўўўўўў renum.filesџ Kfunction(from.prefix="~/r-book/ed2/Art/10-", to.prefix="~/r-book/ed2/Art/", ' from=20:7, to=21:8, doit=F){  path <- getwd() 5 pathtag <- strsplit(path, "/ch", fixed=TRUE)[[1]] & endbit <- pathtag[length(pathtag)] 2 if(nchar(endbit)==2)chap <- paste(endbit) else $ chap <- paste("0",endbit,sep="") 4 to.prefix <- paste(to.prefix, chap, "-", sep="") 4 if(is.null(from.prefix))from.prefix <- to.prefix  for(i in 1:length(from)){ 8 if (from[i]<=9) ltext <- paste("0",from[i],sep="") " else ltext <- paste(from[i]) 4 if (to[i]<=9) rtext <- paste("0",to[i],sep="") , else rtext <- paste(to[i]) 8 txt<-paste("mv ", from.prefix, ltext, ".eps", " ", 2 to.prefix, rtext, ".eps", sep="") ; backup<-paste("cp ", from.prefix, ltext, ".eps", " ", & "archive", sep="")  if(doit)system(backup)  if(doit)system(txt)  print(backup)  print(txt)  }  }ў§џ ~/r-book/ed2/Art/10-џ ~/r-book/ed2/Art/$џџ@4@ў$џџ@5@ ў$џ*џўџ џЦџЧџўў џШџ|џЩџЦџ /chЪџ ў?№ўў џџџШџИџШџўўўџ6џяџџў@ў џ chap@џџўў џџ@џ 0џ‰џ ўўў џџ@џџџ -‰џ ўўџХџџў џџџўў$џ0џџ?№Иџџўўџџ <=џџ0џў@"ў џ ltext@џ 0џџ0џў‰џ ўў џџ@џџџ0џўўўўџџџџ0џў@"ў џ rtext@џ 0џџ0џў‰џ ўў џџ@џџџ0џўўўў џ џ@џ mv џџ .eps  џџ .eps‰џ ўў џ backup@џ cp џџ .eps   archive‰џ ўўџџ systemџўўџџџ џўўЃџџўЃџ џўўўўў