R version 2.13.0 (2011-04-13) Copyright (C) 2011 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 = '1' > 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] "Y" > x[,par1] [1] 210907 120982 176508 179321 123185 52746 385534 33170 101645 149061 [11] 165446 237213 173326 133131 258873 180083 324799 230964 236785 135473 [21] 202925 215147 344297 153935 132943 174724 174415 225548 223632 124817 [31] 221698 210767 170266 260561 84853 294424 101011 215641 325107 7176 [41] 167542 106408 96560 265769 269651 149112 175824 152871 > 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]) 7176 33170 52746 84853 96560 101011 101645 106408 120982 123185 124817 1 1 1 1 1 1 1 1 1 1 1 132943 133131 135473 149061 149112 152871 153935 165446 167542 170266 173326 1 1 1 1 1 1 1 1 1 1 1 174415 174724 175824 176508 179321 180083 202925 210767 210907 215147 215641 1 1 1 1 1 1 1 1 1 1 1 221698 223632 225548 230964 236785 237213 258873 260561 265769 269651 294424 1 1 1 1 1 1 1 1 1 1 1 324799 325107 344297 385534 1 1 1 1 > colnames(x) [1] "Y" "X1" "X2" "X3" "X4" "X5" "X6" > colnames(x)[par1] [1] "Y" > x[,par1] [1] 210907 120982 176508 179321 123185 52746 385534 33170 101645 149061 [11] 165446 237213 173326 133131 258873 180083 324799 230964 236785 135473 [21] 202925 215147 344297 153935 132943 174724 174415 225548 223632 124817 [31] 221698 210767 170266 260561 84853 294424 101011 215641 325107 7176 [41] 167542 106408 96560 265769 269651 149112 175824 152871 > if (par2 == 'none') { + m <- ctree(as.formula(paste(colnames(x)[par1],' ~ .',sep='')),data = x) + } > > #Note: the /var/wessaorg/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/wessaorg/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/wessaorg/rcomp/tmp/1ejwz1324631149.tab") + } + } > m Conditional inference tree with 3 terminal nodes Response: Y Inputs: X1, X2, X3, X4, X5, X6 Number of observations: 48 1) X1 <= 69; criterion = 1, statistic = 30.974 2) X1 <= 42; criterion = 1, statistic = 17.195 3)* weights = 8 2) X1 > 42 4)* weights = 14 1) X1 > 69 5)* weights = 26 > postscript(file="/var/wessaorg/rcomp/tmp/2bxc41324631149.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/wessaorg/rcomp/tmp/3khle1324631149.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 210907 235786.31 -24879.3077 2 120982 153054.50 -32072.5000 3 176508 153054.50 23453.5000 4 179321 235786.31 -56465.3077 5 123185 153054.50 -29869.5000 6 52746 72946.12 -20200.1250 7 385534 235786.31 149747.6923 8 33170 72946.12 -39776.1250 9 101645 72946.12 28698.8750 10 149061 153054.50 -3993.5000 11 165446 153054.50 12391.5000 12 237213 235786.31 1426.6923 13 173326 235786.31 -62460.3077 14 133131 153054.50 -19923.5000 15 258873 235786.31 23086.6923 16 180083 153054.50 27028.5000 17 324799 235786.31 89012.6923 18 230964 235786.31 -4822.3077 19 236785 235786.31 998.6923 20 135473 235786.31 -100313.3077 21 202925 235786.31 -32861.3077 22 215147 235786.31 -20639.3077 23 344297 235786.31 108510.6923 24 153935 153054.50 880.5000 25 132943 235786.31 -102843.3077 26 174724 235786.31 -61062.3077 27 174415 235786.31 -61371.3077 28 225548 235786.31 -10238.3077 29 223632 235786.31 -12154.3077 30 124817 153054.50 -28237.5000 31 221698 235786.31 -14088.3077 32 210767 235786.31 -25019.3077 33 170266 153054.50 17211.5000 34 260561 235786.31 24774.6923 35 84853 72946.12 11906.8750 36 294424 235786.31 58637.6923 37 101011 72946.12 28064.8750 38 215641 235786.31 -20145.3077 39 325107 235786.31 89320.6923 40 7176 72946.12 -65770.1250 41 167542 153054.50 14487.5000 42 106408 72946.12 33461.8750 43 96560 72946.12 23613.8750 44 265769 235786.31 29982.6923 45 269651 235786.31 33864.6923 46 149112 153054.50 -3942.5000 47 175824 153054.50 22769.5000 48 152871 153054.50 -183.5000 > if (par2 != 'none') { + print(cbind(as.factor(x[,par1]),predict(m))) + myt <- table(as.factor(x[,par1]),predict(m)) + print(myt) + } > postscript(file="/var/wessaorg/rcomp/tmp/46oai1324631149.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/wessaorg/rcomp/tmp/5w5j81324631149.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/wessaorg/rcomp/tmp/6nisu1324631149.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/wessaorg/rcomp/tmp/7i4zo1324631149.tab") + } > > try(system("convert tmp/2bxc41324631149.ps tmp/2bxc41324631149.png",intern=TRUE)) character(0) > try(system("convert tmp/3khle1324631149.ps tmp/3khle1324631149.png",intern=TRUE)) character(0) > try(system("convert tmp/46oai1324631149.ps tmp/46oai1324631149.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 2.357 0.215 2.598