RDX2 X  balanced.sample source  function(cl, nset=2, seed=NULL){ $ if(!is.null(seed))set.seed(seed)  gp <- numeric(length(cl))  ord <- order(cl)  tab <- table(cl) M subs <- unlist(lapply(tab, function(x)sample(rep(1:nset, length.out=x))))  gp[ord] <- subs  gp  } cl$ nset@$ seed { if ! is.null set.seed <- gp numeric length  ord order  tab table  subs unlist lapply function x sample rep :? length.out ,function(x)sample(rep(1:nset, length.out=x))  [   calc.matrixrows.f  +function(dset=Golub, cl=golub.info$cancer){  y <- t(dset) # qr.obj <- qr(model.matrix(~cl))  qty.obj <- qr.qty(qr.obj,y)  tab <- table(factor(cl))  dfb <- length(tab)-1  dfw <- sum(tab)-dfb-1 C ms.between <- apply(qty.obj[2:(dfb+1), , drop=F]^2, 2, sum)/dfb E ms.within <- apply(qty.obj[-(1:(dfb+1)), , drop=F]^2, 2, sum)/dfw ! Fstat <- ms.between/ms.within  }$ dset Golub$ $ golub.info cancer  y t  qr.obj qr model.matrix ~  qty.obj qr.qty%#  factor  dfb -?  dfw-- sum,?  ms.between / apply ^)@ ( +,? drop F@@/,  ms.within123)-4?45,?67@@/.  Fstat108 cvdiscx 9function(dset=Golub[,sub], cl=source.mf[sub], nfold=NULL, 3 test="f", nf.use=2, seed=31, funda=lda,  D selectonce=FALSE, cv=TRUE, subset=NULL, show.fold=FALSE){ 6 ## If nfold is not specified, use leave-one-out CV . if(is.null(nfold))nfold <- sum(!is.na(cl)) ( ## Option to omit one or more points 6 if(!is.null(subset)){cl[!is.na(cl)][!subset] <- NA C nfold[1] <- min(nfold[1], sum(!is.na(cl)))  } 0 if(any(is.na(cl))){dset <- dset[,!is.na(cl)] + cl <- cl[!is.na(cl)]  } + if(length(nfold)==1)nfold <- c(nfold,1)  cl <- factor(cl)  ngp <- length(levels(cl))  genes <- rownames(dset)  nobs <- dim(dset)[2]  if(is.null(genes)){ $ genes <- paste(1:dim(dset)[1]) 9 print("Input rows (features) are not named. Names") E print(paste(1,":", dim(dset)[1], " will be assigned.", sep=""))  rownames(dset) <- genes  }  library(MASS) $ if(!is.null(seed))set.seed(seed)  Fcut <- NULL  maxgenes <- max(nf.use)  if(selectonce){ . stat <- calc.matrixrows.f(dset=dset, cl) 9 Fcut <- list(F=sort(stat, decreasing=TRUE)[nf.use], ) df=c(ngp-1, nobs-ngp)) * ord <- order(-abs(stat))[1:maxgenes]  genes.ord <- genes[ord] 9 selectonce.df <- data.frame(t(dset[ord, , drop=F])) 0 acc.resub <- acc.sel1 <- numeric(maxgenes)  for(ng in nf.use){ B resub.xda <- funda(cl~., data=selectonce.df[,1:ng,drop=F]) + hat.rsb <- predict(resub.xda)$class P hat.sel1 <- funda(cl~., data=selectonce.df[,1:ng,drop=F], CV=TRUE)$class % tab.rsb <- table(hat.rsb, cl) & tab.one <- table(hat.sel1, cl) N acc.resub[ng] <- sum(tab.rsb[row(tab.rsb)==col(tab.rsb)])/sum(tab.rsb) M acc.sel1[ng] <- sum(tab.one[row(tab.one)==col(tab.one)])/sum(tab.one)  }  } else {  Fcut <- NULL # acc.resub <- acc.sel1 <- NULL  genes.ord <- NULL  } P if(!cv)return(list(acc.resub=acc.resub, acc.sel1=acc.sel1, genes=genes.ord)) H######################################################################## $ ## Cross-validation calculations 7 if(nfold[1]==nobs)foldid <- sample(1:nfold[1]) else , foldid <- sapply(1:nfold[2], function(x) ? balanced.sample(source.mf, nset=nfold[1])) 8 genelist <- matrix("", nrow=maxgenes, ncol=nfold[1]) ! ufold <- sort(unique(foldid))  testscores <- NULL  acc.cv <- numeric(maxgenes)  for(i in ufold){  cat(paste(i,":",sep="")) % trainset <- (1:nobs)[foldid!=i] ! cli <- factor(cl[trainset]) > stat <- calc.matrixrows.f(dset=dset[, trainset], cl=cli) + ordi <- order(-abs(stat))[1:maxgenes] ! genelist[,i] <- genes[ordi]  } ( ulist <- unique(as.vector(genelist)) . df <- data.frame(t(dset[ulist, , drop=F]))  names(df) <- ulist G####################################################################### + if(!show.fold)cat("\nNo. of features:")  for(ng in nf.use){  hat <- cl - if(!show.fold)cat(paste(ng,":",sep="")) ! if(show.fold)cat("\nFold:")  for(i in ufold){  if(show.fold) cat(paste(i,":",sep="")) & testset <- (1:nobs)[foldid==i] ' trainset <- (1:nobs)[foldid!=i] ntest <- length(testset)  ntrain <- nobs-ntest $ genes.i <- genelist[1:ng, i] , dfi <- df[-testset, genes.i, drop=F] . newdfi <- df[testset, genes.i, drop=F]  cli <- cl[-testset] ( xy.xda <- funda(cli~., data=dfi) 2 subs <- match(colnames(dfi), rownames(df)) I newpred.xda <- predict(xy.xda, newdata=newdfi, method="debiased") ) hat[testset] <- newpred.xda$class  }  if(show.fold)cat("\n")  tab <- table(hat,cl) 9 acc.cv[ng] <- sum(tab[row(tab)==col(tab)])/sum(tab)  } . if(length(nf.use)>1&all(diff(nf.use)==1)){  nobs <- length(cl)  ng1 <- length(acc.cv)  maxacc1 <- max(acc.cv) % sub1 <- match(maxacc1, acc.cv) ( nextacc1 <- max(acc.cv[acc.cv<1]) 9 lower1 <- maxacc1-sqrt(nextacc1*(1-nextacc1)/nobs) + lsub1 <- min((1:ng1)[acc.cv>lower1]) . lower <- c("Best accuracy, less 1SD ", ; paste(paste(round(c(lower1),2), c(lsub1), : sep=" ("), " features) ", sep="")) ! best <- c("Best accuracy", : paste(paste(round(c(maxacc1),2), c(sub1), 7 sep=" ("), " features)", sep="")) # acc.df <- cbind(lower, best) - dimnames(acc.df) <- list(c("Accuracy", ; "(Cross-validation)"),c("","")) ! print(acc.df, quote=FALSE)  } ? invisible(list(folds=foldid, data=df, cl=cl, acc.cv=acc.cv, : acc.sel1=acc.sel1, acc.resub=acc.resub, H genelist=genelist, genes.ord = genes.ord, Fcut=Fcut))  } sub$ source.mf; nfold test f$ nf.use@@? funda lda selectonce  cv $ subset show.fold  = =/ is.na D FD  =? min=?/F anyF F F ===? = c=? +  ngp levels  genes rownames  nobs dim@ M M paste?P? print *Input rows (features) are not named. NamesRQ? :P?  will be assigned. sep  NM library MASS    Fcut  maxgenes max?B  stat V list7 sortY decreasing ? dfJ-K?-OK - absY?W  genes.ordM  selectonce.df data.frame$67  acc.resub  acc.sel1 W for ng?  resub.xda@( . data`?e67  hat.rsb  predictf class  hat.sel1 @(gh`?e67 CV  class  tab.rsbi  tab.onek be1/mI rowm colm/m ce1/nIonpn/n V b c _C returnZbbccM_I=?O  foldid?=? r sapply?=@<=? function(x) > balanced.sample(source.mf, nset=nfold[1])  genelist matrix  nrowW ncol=?  ufold[ uniquer  testscores  acc.cv Wd ix catQ| :S   trainset4?O !=r|  cli+~ Y~  ordi-^Y?W t|M  ulisty as.vectort ]a$67  names]E}  No. of features:de?  hatE}Qe :S E}  Fold:d|xE}Q| :S   testset4?OIr| ~4?Or|  ntest  ntrain-O  genes.it?e|  dfi]-67  newdfi]67 -  xy.xda@(gh  match colnamesN]  newpred.xdaj newdata method debiased   classE}    {e1/Iop/ & >?? allI diff?? O  ng1{  maxacc1X{  sub1{  nextacc1X{ <{?  lower1- sqrt1 *4-?O  lsub1G4?{  lowerJ Best accuracy, less 1SD QQ roundJ@JS  ( features) S   bestJ Best accuracyQQJ@JS  ( features)S   acc.df cbind  dimnamesZJ Accuracy (Cross-validation)J  R quote  invisibleZ foldsrh]{{ccbbtt__VV cvplotf @function(scorelist=source.mf1.scores, ndisc=NULL, plot.disc=1:2, G xlab=NULL, ylab=NULL, plotch=1:3, colr= palette()[c(2,4,6)], I plotch.other=4, colr.other="gray40", cex.other=1, lwd.other=1, H circle=NULL, cl.circle=NULL, circle.pos=c(1,1), adj.circle=1, M circle.colr="gray40", lwd.circle=2, adj.title=0.5, join.legends=T, M prefix.title="Golub data - ", cex.title=1.0, ratio=1, plot.folds=F ){  library(MASS)  cl <- scorelist$cl " cl.other <- scorelist$cl.other  nf.use <- scorelist$nf.use  if(length(plot.disc)==2){  n1 <- plot.disc[1]  n2 <- plot.disc[2] A if(is.null(xlab))xlab <- paste("Discriminant function", n1) A if(is.null(ylab))ylab <- paste("Discriminant function", n2) 9 } else stop("plot.disc must be a vector of length 2") & if(!is.factor(cl))cl <- factor(cl)  levnames <- levels(cl) 1 if(is.null(ndisc))ndisc <- length(levnames)-1 ! fitscores <- scorelist$scores # other.scores <- scorelist$other  ngp <- length(levnames) " n1lim <- range(fitscores[,n1]) " n2lim <- range(fitscores[,n2])  if(!is.null(cl.other)){ 1 n1lim <- range(c(n1lim, other.scores[,n1])) 1 n2lim <- range(c(n2lim, other.scores[,n2])) ! levnum <- unclass(cl.other) ( levnames.other <- levels(cl.other) ' intlev.other <- unclass(cl.other) 1 ngp.other <- length(levels(cl.other))   } * n1 <- plot.disc[1]; n2 <- plot.disc[2]  intlev <- unclass(cl)  oldpar <- par(lwd=1)  on.exit(par(oldpar)) $ eqscplot(n1lim, n2lim, type="n", / xlab=xlab, ylab=ylab, ratio=ratio) < points(fitscores[,n1], fitscores[,n2], col=colr[intlev],  pch=plotch[intlev])  if(!is.null(cl.other)) 2 points(other.scores[,n1], other.scores[,n2], , pch=plotch.other[intlev.other], * col=colr.other[intlev.other],  cex=cex.other)  if(!is.null(cl.circle)){ , cl.circle <- factor(cl.circle[circle]) % lev.circle <- levels(cl.circle) ? points(fitscores[circle,n1], fitscores[circle,n2], pch=1, K cex=1.75, col=circle.colr[unclass(cl.circle)], lwd=lwd.circle)  }  par(xpd=TRUE)  chw <- par()$cxy[1]  chh <- par()$cxy[2]  par(lwd=1.5)  ypos <- par()$usr[4] xmid <- mean(par()$usr[1:2])  top.pos <- 0 7 mtext(side=3, line=(top.pos+1), paste(prefix.title, > nf.use, "features"), cex=cex.title, adj=adj.title) . ypos.legend <- ypos+(top.pos-0.45)*chh*0.8 ( if(join.legends&!is.null(cl.other)){ K leg.info <- legend(xmid, ypos.legend, xjust=0.5, yjust=0, plot=FALSE, L x.intersp=0.5, ncol=ngp, legend=levnames, cex=0.85) D legother.info <- legend(xmid, ypos.legend, xjust=0.5, yjust=0, J plot=FALSE, pch=plotch.other, x.intersp=0.5, N ncol=ngp.other, legend=levnames.other, cex=0.85) 1 leftoff <- 0.5*legother.info$rect$w-0.5*chw - rightoff <- 0.5*leg.info$rect$w+0.5*chw  ypos.other <- ypos.legend  } else {  leftoff <- 0  rightoff <- 0 . ypos.other <- ypos+(top.pos-1.5)*chh*0.8  } 9 legend(xmid-leftoff, ypos.legend, xjust=0.5, yjust=0,  bty="n", pch=plotch, - x.intersp=0.5, col=colr, ncol=ngp, % legend=levnames, cex=0.85)  if(!is.null(cl.other)) H lego.info <- legend(xmid+rightoff, ypos.other, xjust=0.5, yjust=0, Z pch=plotch.other, x.intersp=0.5, col=colr.other, ncol=ngp.other, C legend=levnames.other, cex=0.85, bty="n") ' if(!is.null(cl.other)&join.legends) D text(lego.info$rect$left+c(0.4*chw,lego.info$rect$w-0.25*chw), 8 rep(ypos.other,2)+0.8*chh, labels=c("(",")"))  par(lwd=lwd.circle) 1 if(!is.null(cl.circle))if(lev.circle[1]!=""){ ( xy <- par()$usr[circle.pos+c(1,3)]  legend(xy[1], xy[2], N xjust=adj.circle[1], yjust=circle.pos[2], bty="n", x.intersp=0.5, < pch=rep(1,length(lev.circle)), col=circle.colr, = ncol=1, legend=lev.circle, cex=0.85, pt.cex=1.5)  }  par(lwd=1, xpd=F)  if(plot.folds){ B mtext(side=1, line=1.25, "Discriminant function 1", outer=T) B mtext(side=2, line=1.25, "Discriminant function 2", outer=T)  }  }$ scorelist source.mf1.scores ndisc plot.disc?@ xlab ylab plotch?@ colr paletteJ@@@ plotch.other@ colr.other gray40 cex.other? lwd.other?$ circle$ cl.circle circle.posJ?? adj.circle?$ circle.colr gray40 lwd.circle@ adj.title? join.legends T prefix.title Golub data -  cex.title? ratio? plot.folds7TU   cl  cl.other  cl.other ?  nf.useI@  n1?  n2@  Q Discriminant function  Q Discriminant function stop &plot.disc must be a vector of length 2 is.factor +  levnamesL  -?  fitscores  scores  other.scores  other K  n1lim range  n2lim  J J  levnum unclass  levnames.otherL  intlev.other  ngp.otherL ? @  intlev  oldpar par lwd? on.exit eqscplot type n pointsp pch p cex  +  lev.circleL??p xpd   chw  cxy?  chh  cxy@?  ypos  usr@  xmid mean  usr?@  top.pos mtext side@ line45?Q? features adj  ypos.legend54-??陙   leg.info legend xjust? yjust plot  x.intersp?wK?333333  legother.info? ?w?333333  leftoff-?   rect w?  rightoff5?  ?  ypos.other   54-??陙-? bty n?pwK?333333   lego.info5??pw?333333 n  text5   leftJ?ٙ-  ?5@?陙 labelsJ ( ) ?   xy  usr5J?@?@?@ n??pw??333333 pt.cex??7?? Discriminant function 1 outer@? Discriminant function 2 cvscoresU 4function(cvlist=source.mf1.cv, nf.use=3, ndisc=NULL, 8 cl.other=factor("PB:f"), dset.other=Golub.PBf ){  library(MASS)  folds <- cvlist$folds ; ugenes <- unique(as.vector(cvlist$genelist[1:nf.use,]))  df <- cvlist$data[, ugenes]  cl <- cvlist$cl  if(!length(cl)==dim(df)[1]) = stop(paste("length(cl) =", length(cl),"does not equal", 4 "dim(cvlist$df)[1] =", dim(df)[1]))  levnames <- levels(cl) 1 if(is.null(ndisc))ndisc <- length(levnames)-1  ngp <- length(levnames)  nobs <- dim(df)[1] ufold <- sort(unique(folds))  nfold <- length(ufold) 7 allscores <- matrix(0, nrow=nobs, ncol=ndisc*nfold)  if(!is.null(cl.other)){ " cl.other <- factor(cl.other) J if(is.null(dim(dset.other)))stop("dset.other must have dimension 2") / if(!length(cl.other)==dim(dset.other)[2]) K stop(paste("length(cl.other) =", length(cl.other),"does not equal", ? "dim(dset.other)[2] =", dim(dset.other)[2])) @ df.other <- data.frame(t(dset.other[ugenes, ,drop=FALSE]))  }  else other.scores <- NULL  for(i in ufold){  cat(paste(i,":",sep="")) ! testi <- (1:nobs)[folds==i] " traini <- (1:nobs)[folds!=i]  ntest <- length(testi)  ntrain <- nobs-ntest - genes.i <- cvlist$genelist[1:nf.use, i] 7 dfi <- as.data.frame(df[-testi, genes.i, drop=F]) 9 newdfi <- as.data.frame(df[testi, genes.i, drop=F])  cli <- cl[-testi] $ xy.xda <- lda(cli~., data=dfi) - allscores[, ((i-1)*ndisc)+(1:ndisc)] <- 2 predict(xy.xda, newdata=df, dimen=ndisc)$x  } 4 allscores.pcp <- pcp(allscores, varscores=FALSE) 2 globals <- sweep(allscores.pcp$g[,1:ndisc], 2, 6 allscores.pcp$sdev[1:ndisc], "/") ! ntimes <- table(folds)[folds] 5 ntimes.genes <- table(cvlist$genelist[1:nf.use,])  av <- colMeans(df) 1 fitscores <- matrix(0, nrow=nobs, ncol=ndisc)  for(i in ufold){  cat(paste(i,":",sep="")) ! testi <- (1:nobs)[folds==i] " traini <- (1:nobs)[folds!=i] - genes.i <- cvlist$genelist[1:nf.use, i] 4 dfi <- data.frame(df[-testi, genes.i, drop=F]) 6 newdfi <- data.frame(df[testi, genes.i, drop=F])  cli <- cl[-testi] ( traini.xda <- lda(cli~., data=dfi) / scorei <- predict(traini.xda)$x[,1:ndisc] 8 newpred.xda <- predict(traini.xda, newdata=newdfi) 7 scorei.out <- newpred.xda$x[,1:ndisc, drop=FALSE] , scorei.all <- globals[-testi, 1:ndisc] # avcol <- colMeans(scorei.all) 3 scorei.all <- sweep(scorei.all, 2, avcol,"-")  avi <- colMeans(scorei) ) scorei <- sweep(scorei, 2, avi,"-") + trans <- qr.solve(scorei, scorei.all) 2 scorei.out <- sweep(scorei.out, 2, avi, "-") C fitscores[testi,] <- sweep(scorei.out%*%trans, 2, avcol, "+")  }  if(!is.null(cl.other)){ " xy.xda <- lda(cl~., data=df) 4 train.scores <- predict(xy.xda, dimen=ndisc)$x 7 other.scores <- predict(xy.xda, newdata=df.other, , dimen=ndisc)$x avcol <- colMeans(globals) 0 all.scores <- sweep(globals, 2, avcol,"-") ( av.train <- colMeans(train.scores) ; train.scores <- sweep(train.scores, 2, av.train, "-") 1 trans <- qr.solve(train.scores, all.scores) @ other.scores <- sweep(other.scores%*%trans, 2, avcol, "+")  } cat("\n") ? invisible(list(scores=fitscores, cl=cl, other=other.scores, 5 cl.other=cl.other, nf.use=nf.use))  }$ cvlist source.mf1.cv$?@$+ PB:f dset.other Golub.PBfTU   folds  ugenesy  genelist?? ]  data   clIP]?Q length(cl) = does not equal dim(cvlist$df)[1] =P]? L  -? K OP]? x[y =x  allscoresuvOw=  + P dset.other must have dimension 2IP@Q length(cl.other) = does not equal dim(dset.other)[2] =P@  df.othera$6  d|x}Q| :S   testi4?OI|  traini4?O|  -O   genelist??|  as.data.frame]-67 ]67 - A(gh 544-|?4? j] dimen x  allscores.pcp pcp varscores   globals sweep   g?@   sdev? /  ntimes  ntimes.genes  genelist??  av colMeans] uvOwd|x}Q| :S  4?OI| 4?O|   genelist??| a]-67 a]67 -  traini.xdaA(gh  scorei j x? j  scorei.out  x?6   scorei.all -?  avcol @ -  avi @ -  trans qr.solve @ -  %*%@ +  A(gh]  train.scores j   j     all.scores @ -  av.train @ -  @ +}  Z scores other??! H'(*/01)+,-.FGHDEC78;453269:<=AB?@> "#$%& ! HL BM PB class factor HL F M! factor HL CALGB CCG DFCI St-Jude! factor HL BM:NA BM:f BM:m PB:NA PB:f PB:m! factor HL allB allT aml! factor Samples BM.PB Gender Source tissue.mf cancer row.namesH 39 40 42 47 48 49 41 43 44 45 46 70 71 72 68 69 67 55 56 59 52 53 51 50 54 57 58 60 61 65 66 63 64 62 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 34 35 36 37 38 28 29 30 31 32 33! data.frame order.features 8function(dset, cl, subset=NULL, test="f", values=FALSE){ D if(dim(dset)[2]!=length(cl))stop(paste("Dimension 2 of dset is", C dim(dset)[2], "differs from the length of cl (=",  length(cl))) ; ## Ensure that cl is a factor & has no redundant levels  if(is.null(subset))  cl <- factor(cl)  else  cl <- factor(cl[subset])  intlev <- unclass(cl) - 1  if(is.null(subset)) ) stat <- calc.matrixrows.f(dset, cl)  else 3 stat <- calc.matrixrows.f(dset[, subset], cl)  ord <- order(-abs(stat)) 6 if(!values)ord else(list(ord=ord, stat=stat[ord]))  }D> f values P@Q Dimension 2 of dset isP@ differs from the length of cl (= D + +D -? D Y YD -^Y$4ZYY . function(x=USArrests,  varscores=TRUE,  cases="rows",  center="vars",  standardize=FALSE,  scale.cases=1,  log=FALSE,  sc=1,  reflect=c(1,1), ...) {  x <- as.matrix(x) avv <- 0 sdv <- 1 ( casedim <- 2-as.logical(cases=="rows")  vardim <- 3-casedim 5 ## casedim=1 if rows are cases; otherwise casedim=2 9 ## scale.cases=0 gives a pure rotation of the variables 2 ## scale.cases=1 weights a/c the singular values  ncases <- dim(x)[casedim]  nvar <- dim(x)[vardim] ( if(is.null(sc))sc <- dim(x)[casedim]-1  if(log)x <- log(x, base=2)  if(standardize){ ! avv <- apply(x, vardim, mean) ' sdv <- apply(x, vardim, sd)  " x <- sweep(x, vardim, avv,"-") " x <- sweep(x, vardim, sdv,"/")  } 8 else if(as.logical(match("vars", center, nomatch=0))){ avv <- apply(x,vardim, mean) # x <- sweep(x, vardim, avv,"-")}  ) svdx <- La.svd(x, method = c("dgesdd")) h <- NULL  if(cases=="rows"){ ; g <- sweep(svdx$u, 2, svdx$d^scale.cases, "*")*sqrt(sc)  if(varscores) 9 h <- t((svdx$d^(1-scale.cases)* svdx$vt ))/sqrt(sc)  }  else if(cases=="columns"){ ? g <- sweep(t(svdx$vt), 2, svdx$d^scale.cases, "*")*sqrt(sc)  if(varscores) @ h <- sweep(svdx$u, 2, svdx$d^(1-scale.cases),"*")/sqrt(sc)  } F invisible(list(g=g, rotation=h, av=avv, sdev=svdx$d/sqrt(ncases-1))) } USArrests$   cases rows center vars standardize  scale.cases? log  sc? reflectJ?? ...  as.matrix  avv  sdv?  casedim-@ as.logicalI& rows  vardim-@1  ncasesP1  nvarP3 + +-P1?* * base@( /23 023 sd 3/ - 30 /2 vars' nomatch /23 3/ -  svdx La.svdJ dgesdd  hI& rows  g 9 u@3 9 d) *+  ;1$43 9 d4-?) 9 vt+I& columns <$ 9 vt@3 9 d) *+  ;1 9 u@3 9>4-?) *+Z<< rotation;/ sdev1 9 d-4? plot.train.test Hfunction(dset=Golub.BM, nf.use=c(11,11), cl=cancer.BM, traintest=gp.id){  oldpar <- par(mfrow=c(1,2))  on.exit(par(oldpar)) " traintest <- factor(traintest) , train <- traintest==levels(traintest)[1] . testset <- traintest==levels(traintest)[2]  cl1 <- cl[train]  cl2 <- cl[testset]  nf1 <- nf.use[1] 2 ord1 <- order.features(dset, cl, subset=train) 2 df1 <- data.frame(t(dset[ord1[1:nf1], train])) 4 df2 <- data.frame(t(dset[ord1[1:nf1], testset])) df1.lda <- lda(df1, cl1)  - scores <- predict(df1.lda, newdata=df2)$x < scoreplot(scores, cl2, scores.other=NULL, cl.other=NULL)  nf2 <- nf.use[2] 4 ord2 <- order.features(dset, cl, subset=testset) 4 df2 <- data.frame(t(dset[ord2[1:nf2], testset])) 2 df1 <- data.frame(t(dset[ord2[1:nf2], train])) df2.lda <- lda(df2, cl2)  - scores <- predict(df2.lda, newdata=df1)$x < scoreplot(scores, cl1, scores.other=NULL, cl.other=NULL)  }$ Golub.BM$?J@&@&$ cancer.BM$ traintest gp.id  mfrowJ?@ E+E  trainIELE? IELE@  cl1H  cl2  nf1??  ord1#DH  df1a$L?KH  df2a$L?K  df1.ldaAMI  jON x scoreplotJ scores.other  nf2?@  ord2#D Na$S?R Ma$S?RH  df2.ldaANJ  jTM xPIQPZ ;function(scores, cl, scores.other, cl.other, plot.disc=1:2, G xlab=NULL, ylab=NULL, plotch=1:3, colr= palette()[c(2,4,6)], I plotch.other=4, colr.other="gray40", cex.other=1, lwd.other=1, H circle=NULL, cl.circle=NULL, circle.pos=c(1,1), adj.circle=1, M circle.colr="gray40", lwd.circle=2, join.legends=T, cex.title=1.0,  ratio=1 ){  library(MASS)  oldpar <- par(lwd=1)  on.exit(par(oldpar))  if(length(plot.disc)==2){  n1 <- plot.disc[1]  n2 <- plot.disc[2] A if(is.null(xlab))xlab <- paste("Discriminant function", n1) A if(is.null(ylab))ylab <- paste("Discriminant function", n2) 9 } else stop("plot.disc must be a vector of length 2") & if(!is.factor(cl))cl <- factor(cl)  levnames <- levels(cl)  fitscores <- scores  ngp <- length(levnames) " n1lim <- range(fitscores[,n1]) " n2lim <- range(fitscores[,n2])  if(!is.null(cl.other)){ " cl.other <- factor(cl.other) 1 n1lim <- range(c(n1lim, scores.other[,n1])) 1 n2lim <- range(c(n2lim, scores.other[,n2])) ' intlev.other <- unclass(cl.other) ( levnames.other <- levels(cl.other) 1 ngp.other <- length(levels(cl.other))   }  intlev <- unclass(cl) : eqscplot(n1lim, n2lim, type="n", xlab=xlab, ylab=ylab) < points(fitscores[,n1], fitscores[,n2], col=colr[intlev],  pch=plotch[intlev])  if(!is.null(cl.other)) 2 points(scores.other[,n1], scores.other[,n2], , pch=plotch.other[intlev.other], * col=colr.other[intlev.other],  cex=cex.other)  if(!is.null(cl.circle)){ , cl.circle <- factor(cl.circle[circle]) % lev.circle <- levels(cl.circle) ? points(fitscores[circle,n1], fitscores[circle,n2], pch=1, K cex=1.75, col=circle.colr[unclass(cl.circle)], lwd=lwd.circle)  }  par(xpd=TRUE)  chw <- par()$cxy[1]  chh <- par()$cxy[2]  par(lwd=1.5)  ypos <- par()$usr[4] xmid <- mean(par()$usr[1:2])  top.pos <- 0 . ypos.legend <- ypos+(top.pos-0.45)*chh*0.8 ( if(join.legends&!is.null(cl.other)){ K leg.info <- legend(xmid, ypos.legend, xjust=0.5, yjust=0, plot=FALSE, L x.intersp=0.5, ncol=ngp, legend=levnames, cex=0.85) D legother.info <- legend(xmid, ypos.legend, xjust=0.5, yjust=0, J plot=FALSE, pch=plotch.other, x.intersp=0.5, N ncol=ngp.other, legend=levnames.other, cex=0.85) 1 leftoff <- 0.5*legother.info$rect$w-0.5*chw - rightoff <- 0.5*leg.info$rect$w+0.5*chw  ypos.other <- ypos.legend  } else {  leftoff <- 0  rightoff <- 0 . ypos.other <- ypos+(top.pos-1.5)*chh*0.8  } 9 legend(xmid-leftoff, ypos.legend, xjust=0.5, yjust=0,  bty="n", pch=plotch, - x.intersp=0.5, col=colr, ncol=ngp, % legend=levnames, cex=0.85)  if(!is.null(cl.other)) F lego.info <- legend(xmid+rightoff, ypos.other, xjust=0.5, yjust=0, H pch=plotch.other, x.intersp=0.5, col=colr.other, ' ncol=ngp.other, A legend=levnames.other, cex=0.85, bty="n") ' if(!is.null(cl.other)&join.legends) D text(lego.info$rect$left+c(0.4*chw,lego.info$rect$w-0.25*chw), 8 rep(ypos.other,2)+0.8*chh, labels=c("(",")"))  par(lwd=lwd.circle) 1 if(!is.null(cl.circle))if(lev.circle[1]!=""){ ( xy <- par()$usr[circle.pos+c(1,3)]  legend(xy[1], xy[2], N xjust=adj.circle[1], yjust=circle.pos[2], bty="n", x.intersp=0.5, < pch=rep(1,length(lev.circle)), col=circle.colr, = ncol=1, legend=lev.circle, cex=0.85, pt.cex=1.5)  }  par(lwd=1, xpd=F)  }$Q$?@?@J@@@@ gray40??J??? gray40@??TU ?I@ ? @  Q Discriminant function  Q Discriminant function &plot.disc must be a vector of length 2 + L  K    + JQ JQ  L L  np QQp  + L??p    cxy?   cxy@?   usr@   usr?@  54-??陙  ? ?wK?333333 ? ?w?333333 -?   rect w? 5?   rect w?    54-??陙-? n?pwK?333333  5??pw?333333 n 5   rect leftJ?ٙ-   rect w?5@?陙J ( ) ?    usr5J?@?@?@ n??pw??333333??7