R version 2.15.2 (2012-10-26) -- "Trick or Treat" Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > x <- array(list(25 + ,2 + ,10 + ,1.5 + ,0 + ,6 + ,5.70 + ,11.40 + ,24 + ,2 + ,10 + ,1.5 + ,0 + ,10 + ,17.56 + ,35.12 + ,30 + ,2 + ,10 + ,1.5 + ,2 + ,6 + ,11.28 + ,22.56 + ,2 + ,2 + ,10 + ,1.5 + ,2 + ,10 + ,8.39 + ,16.78 + ,40 + ,2 + ,10 + ,2.5 + ,0 + ,6 + ,16.67 + ,33.34 + ,37 + ,2 + ,10 + ,2.5 + ,0 + ,10 + ,12.04 + ,24.08 + ,16 + ,2 + ,10 + ,2.5 + ,2 + ,6 + ,9.22 + ,18.44 + ,22 + ,2 + ,10 + ,2.5 + ,2 + ,10 + ,3.94 + ,7.88 + ,33 + ,2 + ,30 + ,1.5 + ,0 + ,6 + ,27.02 + ,18.01 + ,17 + ,2 + ,30 + ,1.5 + ,0 + ,10 + ,19.46 + ,12.97 + ,28 + ,2 + ,30 + ,1.5 + ,2 + ,6 + ,18.54 + ,12.36 + ,27 + ,2 + ,30 + ,1.5 + ,2 + ,10 + ,25.70 + ,17.13 + ,14 + ,2 + ,30 + ,2.5 + ,0 + ,6 + ,19.02 + ,12.68 + ,13 + ,2 + ,30 + ,2.5 + ,0 + ,10 + ,22.39 + ,14.93 + ,4 + ,2 + ,30 + ,2.5 + ,2 + ,6 + ,23.85 + ,15.90 + ,21 + ,2 + ,30 + ,2.5 + ,2 + ,10 + ,30.12 + ,20.08 + ,23 + ,6 + ,10 + ,1.5 + ,0 + ,6 + ,13.42 + ,26.84 + ,35 + ,6 + ,10 + ,1.5 + ,0 + ,10 + ,34.26 + ,68.52 + ,19 + ,6 + ,10 + ,1.5 + ,2 + ,6 + ,39.74 + ,79.48 + ,34 + ,6 + ,10 + ,1.5 + ,2 + ,10 + ,10.60 + ,21.20 + ,31 + ,6 + ,10 + ,2.5 + ,0 + ,6 + ,28.89 + ,57.78 + ,9 + ,6 + ,10 + ,2.5 + ,0 + ,10 + ,35.61 + ,71.22 + ,38 + ,6 + ,10 + ,2.5 + ,2 + ,6 + ,17.20 + ,34.40 + ,15 + ,6 + ,10 + ,2.5 + ,2 + ,10 + ,6.00 + ,12.00 + ,39 + ,6 + ,30 + ,1.5 + ,0 + ,6 + ,129.45 + ,86.30 + ,8 + ,6 + ,30 + ,1.5 + ,0 + ,10 + ,107.38 + ,71.59 + ,26 + ,6 + ,30 + ,1.5 + ,2 + ,6 + ,111.66 + ,74.44 + ,11 + ,6 + ,30 + ,1.5 + ,2 + ,10 + ,109.10 + ,72.73 + ,6 + ,6 + ,30 + ,2.5 + ,0 + ,6 + ,100.43 + ,66.95 + ,20 + ,6 + ,30 + ,2.5 + ,0 + ,10 + ,109.28 + ,72.85 + ,10 + ,6 + ,30 + ,2.5 + ,2 + ,6 + ,106.46 + ,70.97 + ,32 + ,6 + ,30 + ,2.5 + ,2 + ,10 + ,134.01 + ,89.34 + ,1 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,10.78 + ,10.78 + ,3 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,9.39 + ,9.39 + ,5 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,9.84 + ,9.84 + ,7 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,13.94 + ,13.94 + ,12 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,12.33 + ,12.33 + ,18 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,7.32 + ,7.32 + ,29 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,7.91 + ,7.91 + ,36 + ,4 + ,20 + ,2.0 + ,1 + ,8 + ,15.58 + ,15.58) + ,dim=c(8 + ,40) + ,dimnames=list(c('RUN' + ,'SPEED1' + ,'TOTAL' + ,'SPEED2' + ,'NUMBER2' + ,'SENS' + ,'TIME' + ,'T20BOLT') + ,1:40)) > y <- array(NA,dim=c(8,40),dimnames=list(c('RUN','SPEED1','TOTAL','SPEED2','NUMBER2','SENS','TIME','T20BOLT'),1:40)) > for (i in 1:dim(x)[1]) + { + for (j in 1:dim(x)[2]) + { + y[i,j] <- as.numeric(x[i,j]) + } + } > par4 = 'no' > par3 = '0' > par2 = 'none' > par1 = '8' > par4 <- 'no' > par3 <- '0' > par2 <- 'none' > par1 <- '8' > #'GNU S' R Code compiled by R2WASP v. 1.2.291 () > #Author: root > #To cite this work: Wessa P., 2012, Recursive Partitioning (Regression Trees) (v1.0.3) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_regression_trees.wasp/ > #Source of accompanying publication: > # > library(party) Loading required package: survival Loading required package: splines Loading required package: grid Loading required package: modeltools Loading required package: stats4 Loading required package: coin Loading required package: mvtnorm Loading required package: zoo Attaching package: 'zoo' The following object(s) are masked from 'package:base': as.Date, as.Date.numeric Loading required package: sandwich Loading required package: strucchange Loading required package: vcd Loading required package: MASS Loading required package: colorspace > library(Hmisc) Hmisc library by Frank E Harrell Jr Type library(help='Hmisc'), ?Overview, or ?Hmisc.Overview') to see overall documentation. NOTE:Hmisc no longer redefines [.factor to drop unused levels when subsetting. To get the old behavior of Hmisc type dropUnusedLevels(). Attaching package: 'Hmisc' The following object(s) are masked from 'package:survival': untangle.specials The following object(s) are masked from 'package:base': format.pval, round.POSIXt, trunc.POSIXt, units > par1 <- as.numeric(par1) > par3 <- as.numeric(par3) > x <- data.frame(t(y)) > is.data.frame(x) [1] TRUE > x <- x[!is.na(x[,par1]),] > k <- length(x[1,]) > n <- length(x[,1]) > colnames(x)[par1] [1] "T20BOLT" > x[,par1] [1] 11.40 35.12 22.56 16.78 33.34 24.08 18.44 7.88 18.01 12.97 12.36 17.13 [13] 12.68 14.93 15.90 20.08 26.84 68.52 79.48 21.20 57.78 71.22 34.40 12.00 [25] 86.30 71.59 74.44 72.73 66.95 72.85 70.97 89.34 10.78 9.39 9.84 13.94 [37] 12.33 7.32 7.91 15.58 > if (par2 == 'kmeans') { + cl <- kmeans(x[,par1], par3) + print(cl) + clm <- matrix(cbind(cl$centers,1:par3),ncol=2) + clm <- clm[sort.list(clm[,1]),] + for (i in 1:par3) { + cl$cluster[cl$cluster==clm[i,2]] <- paste('C',i,sep='') + } + cl$cluster <- as.factor(cl$cluster) + print(cl$cluster) + x[,par1] <- cl$cluster + } > if (par2 == 'quantiles') { + x[,par1] <- cut2(x[,par1],g=par3) + } > if (par2 == 'hclust') { + hc <- hclust(dist(x[,par1])^2, 'cen') + print(hc) + memb <- cutree(hc, k = par3) + dum <- c(mean(x[memb==1,par1])) + for (i in 2:par3) { + dum <- c(dum, mean(x[memb==i,par1])) + } + hcm <- matrix(cbind(dum,1:par3),ncol=2) + hcm <- hcm[sort.list(hcm[,1]),] + for (i in 1:par3) { + memb[memb==hcm[i,2]] <- paste('C',i,sep='') + } + memb <- as.factor(memb) + print(memb) + x[,par1] <- memb + } > if (par2=='equal') { + ed <- cut(as.numeric(x[,par1]),par3,labels=paste('C',1:par3,sep='')) + x[,par1] <- as.factor(ed) + } > table(x[,par1]) 7.32 7.88 7.91 9.39 9.84 10.78 11.4 12 12.33 12.36 12.68 12.97 13.94 1 1 1 1 1 1 1 1 1 1 1 1 1 14.93 15.58 15.9 16.78 17.13 18.01 18.44 20.08 21.2 22.56 24.08 26.84 33.34 1 1 1 1 1 1 1 1 1 1 1 1 1 34.4 35.12 57.78 66.95 68.52 70.97 71.22 71.59 72.73 72.85 74.44 79.48 86.3 1 1 1 1 1 1 1 1 1 1 1 1 1 89.34 1 > colnames(x) [1] "RUN" "SPEED1" "TOTAL" "SPEED2" "NUMBER2" "SENS" "TIME" [8] "T20BOLT" > colnames(x)[par1] [1] "T20BOLT" > x[,par1] [1] 11.40 35.12 22.56 16.78 33.34 24.08 18.44 7.88 18.01 12.97 12.36 17.13 [13] 12.68 14.93 15.90 20.08 26.84 68.52 79.48 21.20 57.78 71.22 34.40 12.00 [25] 86.30 71.59 74.44 72.73 66.95 72.85 70.97 89.34 10.78 9.39 9.84 13.94 [37] 12.33 7.32 7.91 15.58 > if (par2 == 'none') { + m <- ctree(as.formula(paste(colnames(x)[par1],' ~ .',sep='')),data = x) + } > > #Note: the /var/fisher/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/fisher/rcomp/createtable") > > if (par2 != 'none') { + m <- ctree(as.formula(paste('as.factor(',colnames(x)[par1],') ~ .',sep='')),data = x) + if (par4=='yes') { + a<-table.start() + a<-table.row.start(a) + a<-table.element(a,'10-Fold Cross Validation',3+2*par3,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'',1,TRUE) + a<-table.element(a,'Prediction (training)',par3+1,TRUE) + a<-table.element(a,'Prediction (testing)',par3+1,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'Actual',1,TRUE) + for (jjj in 1:par3) a<-table.element(a,paste('C',jjj,sep=''),1,TRUE) + a<-table.element(a,'CV',1,TRUE) + for (jjj in 1:par3) a<-table.element(a,paste('C',jjj,sep=''),1,TRUE) + a<-table.element(a,'CV',1,TRUE) + a<-table.row.end(a) + for (i in 1:10) { + ind <- sample(2, nrow(x), replace=T, prob=c(0.9,0.1)) + m.ct <- ctree(as.formula(paste('as.factor(',colnames(x)[par1],') ~ .',sep='')),data =x[ind==1,]) + if (i==1) { + m.ct.i.pred <- predict(m.ct, newdata=x[ind==1,]) + m.ct.i.actu <- x[ind==1,par1] + m.ct.x.pred <- predict(m.ct, newdata=x[ind==2,]) + m.ct.x.actu <- x[ind==2,par1] + } else { + m.ct.i.pred <- c(m.ct.i.pred,predict(m.ct, newdata=x[ind==1,])) + m.ct.i.actu <- c(m.ct.i.actu,x[ind==1,par1]) + m.ct.x.pred <- c(m.ct.x.pred,predict(m.ct, newdata=x[ind==2,])) + m.ct.x.actu <- c(m.ct.x.actu,x[ind==2,par1]) + } + } + print(m.ct.i.tab <- table(m.ct.i.actu,m.ct.i.pred)) + numer <- 0 + for (i in 1:par3) { + print(m.ct.i.tab[i,i] / sum(m.ct.i.tab[i,])) + numer <- numer + m.ct.i.tab[i,i] + } + print(m.ct.i.cp <- numer / sum(m.ct.i.tab)) + print(m.ct.x.tab <- table(m.ct.x.actu,m.ct.x.pred)) + numer <- 0 + for (i in 1:par3) { + print(m.ct.x.tab[i,i] / sum(m.ct.x.tab[i,])) + numer <- numer + m.ct.x.tab[i,i] + } + print(m.ct.x.cp <- numer / sum(m.ct.x.tab)) + for (i in 1:par3) { + a<-table.row.start(a) + a<-table.element(a,paste('C',i,sep=''),1,TRUE) + for (jjj in 1:par3) a<-table.element(a,m.ct.i.tab[i,jjj]) + a<-table.element(a,round(m.ct.i.tab[i,i]/sum(m.ct.i.tab[i,]),4)) + for (jjj in 1:par3) a<-table.element(a,m.ct.x.tab[i,jjj]) + a<-table.element(a,round(m.ct.x.tab[i,i]/sum(m.ct.x.tab[i,]),4)) + a<-table.row.end(a) + } + a<-table.row.start(a) + a<-table.element(a,'Overall',1,TRUE) + for (jjj in 1:par3) a<-table.element(a,'-') + a<-table.element(a,round(m.ct.i.cp,4)) + for (jjj in 1:par3) a<-table.element(a,'-') + a<-table.element(a,round(m.ct.x.cp,4)) + a<-table.row.end(a) + a<-table.end(a) + table.save(a,file="/var/fisher/rcomp/tmp/1t4bf1354984128.tab") + } + } > m Conditional inference tree with 2 terminal nodes Response: T20BOLT Inputs: RUN, SPEED1, TOTAL, SPEED2, NUMBER2, SENS, TIME Number of observations: 40 1) TIME <= 30.12; criterion = 1, statistic = 28.969 2)* weights = 29 1) TIME > 30.12 3)* weights = 11 > postscript(file="/var/fisher/rcomp/tmp/2gf9l1354984128.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > plot(m) > dev.off() null device 1 > postscript(file="/var/fisher/rcomp/tmp/31akc1354984128.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > plot(x[,par1] ~ as.factor(where(m)),main='Response by Terminal Node',xlab='Terminal Node',ylab='Response') > dev.off() null device 1 > if (par2 == 'none') { + forec <- predict(m) + result <- as.data.frame(cbind(x[,par1],forec,x[,par1]-forec)) + colnames(result) <- c('Actuals','Forecasts','Residuals') + print(result) + } Actuals Forecasts Residuals 1 11.40 18.37828 -6.97827586 2 35.12 18.37828 16.74172414 3 22.56 18.37828 4.18172414 4 16.78 18.37828 -1.59827586 5 33.34 18.37828 14.96172414 6 24.08 18.37828 5.70172414 7 18.44 18.37828 0.06172414 8 7.88 18.37828 -10.49827586 9 18.01 18.37828 -0.36827586 10 12.97 18.37828 -5.40827586 11 12.36 18.37828 -6.01827586 12 17.13 18.37828 -1.24827586 13 12.68 18.37828 -5.69827586 14 14.93 18.37828 -3.44827586 15 15.90 18.37828 -2.47827586 16 20.08 18.37828 1.70172414 17 26.84 18.37828 8.46172414 18 68.52 74.94455 -6.42454545 19 79.48 74.94455 4.53545455 20 21.20 18.37828 2.82172414 21 57.78 18.37828 39.40172414 22 71.22 74.94455 -3.72454545 23 34.40 18.37828 16.02172414 24 12.00 18.37828 -6.37827586 25 86.30 74.94455 11.35545455 26 71.59 74.94455 -3.35454545 27 74.44 74.94455 -0.50454545 28 72.73 74.94455 -2.21454545 29 66.95 74.94455 -7.99454545 30 72.85 74.94455 -2.09454545 31 70.97 74.94455 -3.97454545 32 89.34 74.94455 14.39545455 33 10.78 18.37828 -7.59827586 34 9.39 18.37828 -8.98827586 35 9.84 18.37828 -8.53827586 36 13.94 18.37828 -4.43827586 37 12.33 18.37828 -6.04827586 38 7.32 18.37828 -11.05827586 39 7.91 18.37828 -10.46827586 40 15.58 18.37828 -2.79827586 > if (par2 != 'none') { + print(cbind(as.factor(x[,par1]),predict(m))) + myt <- table(as.factor(x[,par1]),predict(m)) + print(myt) + } > postscript(file="/var/fisher/rcomp/tmp/45ne41354984128.ps",horizontal=F,onefile=F,pagecentre=F,paper="special",width=8.3333333333333,height=5.5555555555556) > if(par2=='none') { + op <- par(mfrow=c(2,2)) + plot(density(result$Actuals),main='Kernel Density Plot of Actuals') + plot(density(result$Residuals),main='Kernel Density Plot of Residuals') + plot(result$Forecasts,result$Actuals,main='Actuals versus Predictions',xlab='Predictions',ylab='Actuals') + plot(density(result$Forecasts),main='Kernel Density Plot of Predictions') + par(op) + } > if(par2!='none') { + plot(myt,main='Confusion Matrix',xlab='Actual',ylab='Predicted') + } > dev.off() null device 1 > if (par2 == 'none') { + detcoef <- cor(result$Forecasts,result$Actuals) + a<-table.start() + a<-table.row.start(a) + a<-table.element(a,'Goodness of Fit',2,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'Correlation',1,TRUE) + a<-table.element(a,round(detcoef,4)) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'R-squared',1,TRUE) + a<-table.element(a,round(detcoef*detcoef,4)) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'RMSE',1,TRUE) + a<-table.element(a,round(sqrt(mean((result$Residuals)^2)),4)) + a<-table.row.end(a) + a<-table.end(a) + table.save(a,file="/var/fisher/rcomp/tmp/5bdaz1354984128.tab") + a<-table.start() + a<-table.row.start(a) + a<-table.element(a,'Actuals, Predictions, and Residuals',4,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'#',header=TRUE) + a<-table.element(a,'Actuals',header=TRUE) + a<-table.element(a,'Forecasts',header=TRUE) + a<-table.element(a,'Residuals',header=TRUE) + a<-table.row.end(a) + for (i in 1:length(result$Actuals)) { + a<-table.row.start(a) + a<-table.element(a,i,header=TRUE) + a<-table.element(a,result$Actuals[i]) + a<-table.element(a,result$Forecasts[i]) + a<-table.element(a,result$Residuals[i]) + a<-table.row.end(a) + } + a<-table.end(a) + table.save(a,file="/var/fisher/rcomp/tmp/6qodf1354984128.tab") + } > if (par2 != 'none') { + a<-table.start() + a<-table.row.start(a) + a<-table.element(a,'Confusion Matrix (predicted in columns / actuals in rows)',par3+1,TRUE) + a<-table.row.end(a) + a<-table.row.start(a) + a<-table.element(a,'',1,TRUE) + for (i in 1:par3) { + a<-table.element(a,paste('C',i,sep=''),1,TRUE) + } + a<-table.row.end(a) + for (i in 1:par3) { + a<-table.row.start(a) + a<-table.element(a,paste('C',i,sep=''),1,TRUE) + for (j in 1:par3) { + a<-table.element(a,myt[i,j]) + } + a<-table.row.end(a) + } + a<-table.end(a) + table.save(a,file="/var/fisher/rcomp/tmp/7upqe1354984128.tab") + } > > try(system("convert tmp/2gf9l1354984128.ps tmp/2gf9l1354984128.png",intern=TRUE)) character(0) > try(system("convert tmp/31akc1354984128.ps tmp/31akc1354984128.png",intern=TRUE)) character(0) > try(system("convert tmp/45ne41354984128.ps tmp/45ne41354984128.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.765 0.575 4.328