R version 2.12.0 (2010-10-15) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i486-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(210907 + ,79 + ,30 + ,94 + ,112285 + ,144 + ,145 + ,120982 + ,58 + ,28 + ,103 + ,84786 + ,103 + ,101 + ,176508 + ,60 + ,38 + ,93 + ,83123 + ,98 + ,98 + ,179321 + ,108 + ,30 + ,103 + ,101193 + ,135 + ,132 + ,123185 + ,49 + ,22 + ,51 + ,38361 + ,61 + ,60 + ,52746 + ,0 + ,26 + ,70 + ,68504 + ,39 + ,38 + ,385534 + ,121 + ,25 + ,91 + ,119182 + ,150 + ,144 + ,33170 + ,1 + ,18 + ,22 + ,22807 + ,5 + ,5 + ,101645 + ,20 + ,11 + ,38 + ,17140 + ,28 + ,28 + ,149061 + ,43 + ,26 + ,93 + ,116174 + ,84 + ,84 + ,165446 + ,69 + ,25 + ,60 + ,57635 + ,80 + ,79 + ,237213 + ,78 + ,38 + ,123 + ,66198 + ,130 + ,127 + ,173326 + ,86 + ,44 + ,148 + ,71701 + ,82 + ,78 + ,133131 + ,44 + ,30 + ,90 + ,57793 + ,60 + ,60 + ,258873 + ,104 + ,40 + ,124 + ,80444 + ,131 + ,131 + ,180083 + ,63 + ,34 + ,70 + ,53855 + ,84 + ,84 + ,324799 + ,158 + ,47 + ,168 + ,97668 + ,140 + ,133 + ,230964 + ,102 + ,30 + ,115 + ,133824 + ,151 + ,150 + ,236785 + ,77 + ,31 + ,71 + ,101481 + ,91 + ,91 + ,135473 + ,82 + ,23 + ,66 + ,99645 + ,138 + ,132 + ,202925 + ,115 + ,36 + ,134 + ,114789 + ,150 + ,136 + ,215147 + ,101 + ,36 + ,117 + ,99052 + ,124 + ,124 + ,344297 + ,80 + ,30 + ,108 + ,67654 + ,119 + ,118 + ,153935 + ,50 + ,25 + ,84 + ,65553 + ,73 + ,70 + ,132943 + ,83 + ,39 + ,156 + ,97500 + ,110 + ,107 + ,174724 + ,123 + ,34 + ,120 + ,69112 + ,123 + ,119 + ,174415 + ,73 + ,31 + ,114 + ,82753 + ,90 + ,89 + ,225548 + ,81 + ,31 + ,94 + ,85323 + ,116 + ,112 + ,223632 + ,105 + ,33 + ,120 + ,72654 + ,113 + ,108 + ,124817 + ,47 + ,25 + ,81 + ,30727 + ,56 + ,52 + ,221698 + ,105 + ,33 + ,110 + ,77873 + ,115 + ,112 + ,210767 + ,94 + ,35 + ,133 + ,117478 + ,119 + ,116 + ,170266 + ,44 + ,42 + ,122 + ,74007 + ,129 + ,123 + ,260561 + ,114 + ,43 + ,158 + ,90183 + ,127 + ,125 + ,84853 + ,38 + ,30 + ,109 + ,61542 + ,27 + ,27 + ,294424 + ,107 + ,33 + ,124 + ,101494 + ,175 + ,162 + ,101011 + ,30 + ,13 + ,39 + ,27570 + ,35 + ,32 + ,215641 + ,71 + ,32 + ,92 + ,55813 + ,64 + ,64 + ,325107 + ,84 + ,36 + ,126 + ,79215 + ,96 + ,92 + ,7176 + ,0 + ,0 + ,0 + ,1423 + ,0 + ,0 + ,167542 + ,59 + ,28 + ,70 + ,55461 + ,84 + ,83 + ,106408 + ,33 + ,14 + ,37 + ,31081 + ,41 + ,41 + ,96560 + ,42 + ,17 + ,38 + ,22996 + ,47 + ,47 + ,265769 + ,96 + ,32 + ,120 + ,83122 + ,126 + ,120 + ,269651 + ,106 + ,30 + ,93 + ,70106 + ,105 + ,105 + ,149112 + ,56 + ,35 + ,95 + ,60578 + ,80 + ,79 + ,175824 + ,57 + ,20 + ,77 + ,39992 + ,70 + ,65 + ,152871 + ,59 + ,28 + ,90 + ,79892 + ,73 + ,70) + ,dim=c(7 + ,48) + ,dimnames=list(c('Y' + ,'X1' + ,'X2' + ,'X3' + ,'X4' + ,'X5' + ,'X6') + ,1:48)) > y <- array(NA,dim=c(7,48),dimnames=list(c('Y','X1','X2','X3','X4','X5','X6'),1:48)) > 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 = '3' > par2 = 'none' > par1 = '6' > 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 Loading required package: sandwich Loading required package: strucchange Loading required package: vcd Loading required package: MASS Loading required package: colorspace > library(Hmisc) 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] "X5" > x[,par1] [1] 144 103 98 135 61 39 150 5 28 84 80 130 82 60 131 84 140 151 91 [20] 138 150 124 119 73 110 123 90 116 113 56 115 119 129 127 27 175 35 64 [39] 96 0 84 41 47 126 105 80 70 73 > 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]) 0 5 27 28 35 39 41 47 56 60 61 64 70 73 80 82 84 90 91 96 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 3 1 1 1 98 103 105 110 113 115 116 119 123 124 126 127 129 130 131 135 138 140 144 150 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 151 175 1 1 > colnames(x) [1] "Y" "X1" "X2" "X3" "X4" "X5" "X6" > colnames(x)[par1] [1] "X5" > x[,par1] [1] 144 103 98 135 61 39 150 5 28 84 80 130 82 60 131 84 140 151 91 [20] 138 150 124 119 73 110 123 90 116 113 56 115 119 129 127 27 175 35 64 [39] 96 0 84 41 47 126 105 80 70 73 > if (par2 == 'none') { + m <- ctree(as.formula(paste(colnames(x)[par1],' ~ .',sep='')),data = x) + } > > #Note: the /var/www/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/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/www/rcomp/tmp/1y8et1324630543.tab") + } + } > m Conditional inference tree with 4 terminal nodes Response: X5 Inputs: Y, X1, X2, X3, X4, X6 Number of observations: 48 1) X6 <= 91; criterion = 1, statistic = 46.788 2) X6 <= 47; criterion = 1, statistic = 21.924 3)* weights = 8 2) X6 > 47 4)* weights = 15 1) X6 > 91 5) X6 <= 131; criterion = 1, statistic = 23.201 6)* weights = 17 5) X6 > 131 7)* weights = 8 > postscript(file="/var/www/rcomp/tmp/2ph0u1324630543.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/www/rcomp/tmp/31rag1324630543.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 144 147.87500 -3.8750000 2 103 116.70588 -13.7058824 3 98 116.70588 -18.7058824 4 135 147.87500 -12.8750000 5 61 75.46667 -14.4666667 6 39 27.75000 11.2500000 7 150 147.87500 2.1250000 8 5 27.75000 -22.7500000 9 28 27.75000 0.2500000 10 84 75.46667 8.5333333 11 80 75.46667 4.5333333 12 130 116.70588 13.2941176 13 82 75.46667 6.5333333 14 60 75.46667 -15.4666667 15 131 116.70588 14.2941176 16 84 75.46667 8.5333333 17 140 147.87500 -7.8750000 18 151 147.87500 3.1250000 19 91 75.46667 15.5333333 20 138 147.87500 -9.8750000 21 150 147.87500 2.1250000 22 124 116.70588 7.2941176 23 119 116.70588 2.2941176 24 73 75.46667 -2.4666667 25 110 116.70588 -6.7058824 26 123 116.70588 6.2941176 27 90 75.46667 14.5333333 28 116 116.70588 -0.7058824 29 113 116.70588 -3.7058824 30 56 75.46667 -19.4666667 31 115 116.70588 -1.7058824 32 119 116.70588 2.2941176 33 129 116.70588 12.2941176 34 127 116.70588 10.2941176 35 27 27.75000 -0.7500000 36 175 147.87500 27.1250000 37 35 27.75000 7.2500000 38 64 75.46667 -11.4666667 39 96 116.70588 -20.7058824 40 0 27.75000 -27.7500000 41 84 75.46667 8.5333333 42 41 27.75000 13.2500000 43 47 27.75000 19.2500000 44 126 116.70588 9.2941176 45 105 116.70588 -11.7058824 46 80 75.46667 4.5333333 47 70 75.46667 -5.4666667 48 73 75.46667 -2.4666667 > if (par2 != 'none') { + print(cbind(as.factor(x[,par1]),predict(m))) + myt <- table(as.factor(x[,par1]),predict(m)) + print(myt) + } > postscript(file="/var/www/rcomp/tmp/4ywuk1324630543.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/www/rcomp/tmp/5zwpb1324630543.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/www/rcomp/tmp/6s6vn1324630543.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/www/rcomp/tmp/7e9t51324630543.tab") + } > > try(system("convert tmp/2ph0u1324630543.ps tmp/2ph0u1324630543.png",intern=TRUE)) character(0) > try(system("convert tmp/31rag1324630543.ps tmp/31rag1324630543.png",intern=TRUE)) character(0) > try(system("convert tmp/4ywuk1324630543.ps tmp/4ywuk1324630543.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 2.100 0.140 2.225