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(6.8 + ,225 + ,0.442 + ,0.672 + ,9.2 + ,6.3 + ,180 + ,0.435 + ,0.797 + ,11.7 + ,6.4 + ,190 + ,0.456 + ,0.761 + ,15.8 + ,6.2 + ,180 + ,0.416 + ,0.651 + ,8.6 + ,6.9 + ,205 + ,0.449 + ,0.9 + ,23.2 + ,6.4 + ,225 + ,0.431 + ,0.78 + ,27.4 + ,6.3 + ,185 + ,0.487 + ,0.771 + ,9.3 + ,6.8 + ,235 + ,0.469 + ,0.75 + ,16 + ,6.9 + ,235 + ,0.435 + ,0.818 + ,4.7 + ,6.7 + ,210 + ,0.48 + ,0.825 + ,12.5 + ,6.9 + ,245 + ,0.516 + ,0.632 + ,20.1 + ,6.9 + ,245 + ,0.493 + ,0.757 + ,9.1 + ,6.3 + ,185 + ,0.374 + ,0.709 + ,8.1 + ,6.1 + ,185 + ,0.424 + ,0.782 + ,8.6 + ,6.2 + ,180 + ,0.441 + ,0.775 + ,20.3 + ,6.8 + ,220 + ,0.503 + ,0.88 + ,25 + ,6.5 + ,194 + ,0.503 + ,0.833 + ,19.2 + ,7.6 + ,225 + ,0.425 + ,0.571 + ,3.3 + ,6.3 + ,210 + ,0.371 + ,0.816 + ,11.2 + ,7.1 + ,240 + ,0.504 + ,0.714 + ,10.5 + ,6.8 + ,225 + ,0.4 + ,0.765 + ,10.1 + ,7.3 + ,263 + ,0.482 + ,0.655 + ,7.2 + ,6.4 + ,210 + ,0.475 + ,0.244 + ,13.6 + ,6.8 + ,235 + ,0.428 + ,0.728 + ,9 + ,7.2 + ,230 + ,0.559 + ,0.721 + ,24.6 + ,6.4 + ,190 + ,0.441 + ,0.757 + ,12.6 + ,6.6 + ,220 + ,0.492 + ,0.747 + ,5.6 + ,6.8 + ,210 + ,0.402 + ,0.739 + ,8.7 + ,6.1 + ,180 + ,0.415 + ,0.713 + ,7.7 + ,6.5 + ,235 + ,0.492 + ,0.742 + ,24.1 + ,6.4 + ,185 + ,0.484 + ,0.861 + ,11.7 + ,6 + ,175 + ,0.387 + ,0.721 + ,7.7 + ,6 + ,192 + ,0.436 + ,0.785 + ,9.6 + ,7.3 + ,263 + ,0.482 + ,0.655 + ,7.2 + ,6.1 + ,180 + ,0.34 + ,0.821 + ,12.3 + ,6.7 + ,240 + ,0.516 + ,0.728 + ,8.9 + ,6.4 + ,210 + ,0.475 + ,0.846 + ,13.6 + ,5.8 + ,160 + ,0.412 + ,0.813 + ,11.2 + ,6.9 + ,230 + ,0.411 + ,0.595 + ,2.8 + ,7 + ,245 + ,0.407 + ,0.573 + ,3.2 + ,7.3 + ,228 + ,0.445 + ,0.726 + ,9.4 + ,5.9 + ,155 + ,0.291 + ,0.707 + ,11.9 + ,6.2 + ,200 + ,0.449 + ,0.804 + ,15.4 + ,6.8 + ,235 + ,0.546 + ,0.784 + ,7.4 + ,7 + ,235 + ,0.48 + ,0.744 + ,18.9 + ,5.9 + ,105 + ,0.359 + ,0.839 + ,7.9 + ,6.1 + ,180 + ,0.528 + ,0.79 + ,12.2 + ,5.7 + ,185 + ,0.352 + ,0.701 + ,11 + ,7.1 + ,245 + ,0.414 + ,0.778 + ,2.8 + ,5.8 + ,180 + ,0.425 + ,0.872 + ,11.8 + ,7.4 + ,240 + ,0.599 + ,0.713 + ,17.1 + ,6.8 + ,225 + ,0.482 + ,0.701 + ,11.6 + ,6.8 + ,215 + ,0.457 + ,0.734 + ,5.8 + ,7 + ,230 + ,0.435 + ,0.764 + ,8.3) + ,dim=c(5 + ,54) + ,dimnames=list(c('X1' + ,'X2' + ,'X3' + ,'X4' + ,'X5') + ,1:54)) > y <- array(NA,dim=c(5,54),dimnames=list(c('X1','X2','X3','X4','X5'),1:54)) > 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 = '4' > par2 = 'none' > par1 = '2' > 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] "X2" > x[,par1] [1] 225 180 190 180 205 225 185 235 235 210 245 245 185 185 180 220 194 225 210 [20] 240 225 263 210 235 230 190 220 210 180 235 185 175 192 263 180 240 210 160 [39] 230 245 228 155 200 235 235 105 180 185 245 180 240 225 215 230 > 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]) 105 155 160 175 180 185 190 192 194 200 205 210 215 220 225 228 230 235 240 245 1 1 1 1 7 5 2 1 1 1 1 5 1 2 5 1 3 6 3 4 263 2 > colnames(x) [1] "X1" "X2" "X3" "X4" "X5" > colnames(x)[par1] [1] "X2" > x[,par1] [1] 225 180 190 180 205 225 185 235 235 210 245 245 185 185 180 220 194 225 210 [20] 240 225 263 210 235 230 190 220 210 180 235 185 175 192 263 180 240 210 160 [39] 230 245 228 155 200 235 235 105 180 185 245 180 240 225 215 230 > 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/1whys1355259526.tab") + } + } > m Conditional inference tree with 4 terminal nodes Response: X2 Inputs: X1, X3, X4, X5 Number of observations: 54 1) X1 <= 6.4; criterion = 1, statistic = 36.893 2) X1 <= 6; criterion = 0.981, statistic = 7.993 3)* weights = 7 2) X1 > 6 4)* weights = 17 1) X1 > 6.4 5) X4 <= 0.728; criterion = 0.975, statistic = 7.489 6)* weights = 14 5) X4 > 0.728 7)* weights = 16 > postscript(file="/var/wessaorg/rcomp/tmp/2ndrk1355259526.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/3wfq11355259526.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 225 238.1429 -13.142857 2 180 191.4706 -11.470588 3 190 191.4706 -1.470588 4 180 191.4706 -11.470588 5 205 224.6250 -19.625000 6 225 191.4706 33.529412 7 185 191.4706 -6.470588 8 235 224.6250 10.375000 9 235 224.6250 10.375000 10 210 224.6250 -14.625000 11 245 238.1429 6.857143 12 245 224.6250 20.375000 13 185 191.4706 -6.470588 14 185 191.4706 -6.470588 15 180 191.4706 -11.470588 16 220 224.6250 -4.625000 17 194 224.6250 -30.625000 18 225 238.1429 -13.142857 19 210 191.4706 18.529412 20 240 238.1429 1.857143 21 225 224.6250 0.375000 22 263 238.1429 24.857143 23 210 191.4706 18.529412 24 235 238.1429 -3.142857 25 230 238.1429 -8.142857 26 190 191.4706 -1.470588 27 220 224.6250 -4.625000 28 210 224.6250 -14.625000 29 180 191.4706 -11.470588 30 235 224.6250 10.375000 31 185 191.4706 -6.470588 32 175 164.5714 10.428571 33 192 164.5714 27.428571 34 263 238.1429 24.857143 35 180 191.4706 -11.470588 36 240 238.1429 1.857143 37 210 191.4706 18.529412 38 160 164.5714 -4.571429 39 230 238.1429 -8.142857 40 245 238.1429 6.857143 41 228 238.1429 -10.142857 42 155 164.5714 -9.571429 43 200 191.4706 8.529412 44 235 224.6250 10.375000 45 235 224.6250 10.375000 46 105 164.5714 -59.571429 47 180 191.4706 -11.470588 48 185 164.5714 20.428571 49 245 224.6250 20.375000 50 180 164.5714 15.428571 51 240 238.1429 1.857143 52 225 238.1429 -13.142857 53 215 224.6250 -9.625000 54 230 224.6250 5.375000 > 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/47yj51355259526.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/5zul41355259526.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/657lf1355259526.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/7mq7n1355259526.tab") + } > > try(system("convert tmp/2ndrk1355259526.ps tmp/2ndrk1355259526.png",intern=TRUE)) character(0) > try(system("convert tmp/3wfq11355259526.ps tmp/3wfq11355259526.png",intern=TRUE)) character(0) > try(system("convert tmp/47yj51355259526.ps tmp/47yj51355259526.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.804 0.460 4.243