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 = '3' > 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] "X2" > x[,par1] [1] 30 28 38 30 22 26 25 18 11 26 25 38 44 30 40 34 47 30 31 23 36 36 30 25 39 [26] 34 31 31 33 25 33 35 42 43 30 33 13 32 36 0 28 14 17 32 30 35 20 28 > 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 11 13 14 17 18 20 22 23 25 26 28 30 31 32 33 34 35 36 38 39 40 42 43 44 47 1 1 1 1 1 1 1 1 1 4 2 3 7 3 2 3 2 2 3 2 1 1 1 1 1 1 > colnames(x) [1] "Y" "X1" "X2" "X3" "X4" "X5" "X6" > colnames(x)[par1] [1] "X2" > x[,par1] [1] 30 28 38 30 22 26 25 18 11 26 25 38 44 30 40 34 47 30 31 23 36 36 30 25 39 [26] 34 31 31 33 25 33 35 42 43 30 33 13 32 36 0 28 14 17 32 30 35 20 28 > 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/1huu01324630394.tab") + } + } > m Conditional inference tree with 4 terminal nodes Response: X2 Inputs: Y, X1, X3, X4, X5, X6 Number of observations: 48 1) X3 <= 51; criterion = 1, statistic = 38.379 2)* weights = 7 1) X3 > 51 3) X3 <= 120; criterion = 1, statistic = 26.06 4) X3 <= 91; criterion = 0.978, statistic = 8.404 5)* weights = 12 4) X3 > 91 6)* weights = 18 3) X3 > 120 7)* weights = 11 > postscript(file="/var/wessaorg/rcomp/tmp/2misb1324630394.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/38bgd1324630394.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 30 31.61111 -1.6111111 2 28 31.61111 -3.6111111 3 38 31.61111 6.3888889 4 30 31.61111 -1.6111111 5 22 13.57143 8.4285714 6 26 26.66667 -0.6666667 7 25 26.66667 -1.6666667 8 18 13.57143 4.4285714 9 11 13.57143 -2.5714286 10 26 31.61111 -5.6111111 11 25 26.66667 -1.6666667 12 38 39.36364 -1.3636364 13 44 39.36364 4.6363636 14 30 26.66667 3.3333333 15 40 39.36364 0.6363636 16 34 26.66667 7.3333333 17 47 39.36364 7.6363636 18 30 31.61111 -1.6111111 19 31 26.66667 4.3333333 20 23 26.66667 -3.6666667 21 36 39.36364 -3.3636364 22 36 31.61111 4.3888889 23 30 31.61111 -1.6111111 24 25 26.66667 -1.6666667 25 39 39.36364 -0.3636364 26 34 31.61111 2.3888889 27 31 31.61111 -0.6111111 28 31 31.61111 -0.6111111 29 33 31.61111 1.3888889 30 25 26.66667 -1.6666667 31 33 31.61111 1.3888889 32 35 39.36364 -4.3636364 33 42 39.36364 2.6363636 34 43 39.36364 3.6363636 35 30 31.61111 -1.6111111 36 33 39.36364 -6.3636364 37 13 13.57143 -0.5714286 38 32 31.61111 0.3888889 39 36 39.36364 -3.3636364 40 0 13.57143 -13.5714286 41 28 26.66667 1.3333333 42 14 13.57143 0.4285714 43 17 13.57143 3.4285714 44 32 31.61111 0.3888889 45 30 31.61111 -1.6111111 46 35 31.61111 3.3888889 47 20 26.66667 -6.6666667 48 28 26.66667 1.3333333 > 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/4yc5i1324630394.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/5h16m1324630394.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/68lh21324630394.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/79dgt1324630394.tab") + } > > try(system("convert tmp/2misb1324630394.ps tmp/2misb1324630394.png",intern=TRUE)) character(0) > try(system("convert tmp/38bgd1324630394.ps tmp/38bgd1324630394.png",intern=TRUE)) character(0) > try(system("convert tmp/4yc5i1324630394.ps tmp/4yc5i1324630394.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 2.482 0.240 2.770