R version 2.12.1 (2010-12-16) 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(9 + ,20 + ,1 + ,14 + ,3 + ,1 + ,1 + ,9 + ,14 + ,1 + ,8 + ,3 + ,0 + ,1 + ,9 + ,18 + ,0 + ,12 + ,6 + ,1 + ,1 + ,9 + ,12 + ,1 + ,7 + ,2 + ,0 + ,1 + ,9 + ,16 + ,0 + ,10 + ,1 + ,1 + ,0 + ,9 + ,13 + ,0 + ,7 + ,2 + ,0 + ,0 + ,9 + ,22 + ,1 + ,16 + ,8 + ,1 + ,1 + ,9 + ,16 + ,1 + ,11 + ,1 + ,1 + ,0 + ,9 + ,20 + ,0 + ,14 + ,4 + ,1 + ,1 + ,9 + ,10 + ,0 + ,6 + ,0 + ,0 + ,0 + ,9 + ,22 + ,0 + ,16 + ,4 + ,1 + ,0 + ,9 + ,17 + ,1 + ,11 + ,2 + ,0 + ,1 + ,9 + ,21 + ,0 + ,16 + ,1 + ,1 + ,1 + ,9 + ,18 + ,1 + ,12 + ,2 + ,1 + ,1 + ,9 + ,13 + ,0 + ,7 + ,3 + ,0 + ,0 + ,9 + ,17 + ,0 + ,13 + ,1 + ,1 + ,0 + ,9 + ,17 + ,1 + ,11 + ,2 + ,1 + ,1 + ,9 + ,19 + ,1 + ,15 + ,6 + ,1 + ,0 + ,9 + ,12 + ,1 + ,7 + ,0 + ,0 + ,1 + ,9 + ,14 + ,1 + ,9 + ,1 + ,0 + ,1 + ,9 + ,13 + ,0 + ,7 + ,3 + ,0 + ,1 + ,9 + ,20 + ,1 + ,14 + ,5 + ,1 + ,1 + ,9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0 + ,15 + ,1 + ,1 + ,1 + ,9 + ,18 + ,0 + ,12 + ,4 + ,1 + ,0 + ,9 + ,14 + ,0 + ,8 + ,3 + ,0 + ,1 + ,9 + ,19 + ,0 + ,13 + ,3 + ,1 + ,1 + ,9 + ,20 + ,1 + ,14 + ,3 + ,1 + ,1 + ,10 + ,18 + ,0 + ,12 + ,2 + ,1 + ,1 + ,10 + ,22 + ,0 + ,16 + ,6 + ,1 + ,1 + ,10 + ,16 + ,1 + ,10 + ,5 + ,1 + ,1 + ,10 + ,20 + ,0 + ,15 + ,5 + ,1 + ,0 + ,10 + ,14 + ,0 + ,8 + ,2 + ,0 + ,1 + ,10 + ,22 + ,1 + ,16 + ,4 + ,1 + ,1 + ,10 + ,25 + ,0 + ,19 + ,2 + ,1 + ,1 + ,10 + ,20 + ,0 + ,14 + ,5 + ,1 + ,0) + ,dim=c(7 + ,64) + ,dimnames=list(c('Month' + ,'Income' + ,'Change' + ,'Size' + ,'Complex' + ,'Big4' + ,'Product') + ,1:64)) > y <- array(NA,dim=c(7,64),dimnames=list(c('Month','Income','Change','Size','Complex','Big4','Product'),1:64)) > 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 = '6' > 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 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] "Income" > x[,par1] [1] 20 14 18 12 16 13 22 16 20 10 22 17 21 18 13 17 17 19 12 14 13 20 20 13 21 [26] 21 19 18 20 14 14 20 21 14 16 21 16 14 19 22 19 11 13 16 14 19 21 12 17 21 [51] 11 19 18 14 19 20 18 22 16 20 14 22 25 20 > 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]) 10 11 12 13 14 16 17 18 19 20 21 22 25 1 2 3 5 9 6 4 5 7 9 7 5 1 > colnames(x) [1] "Month" "Income" "Change" "Size" "Complex" "Big4" "Product" > colnames(x)[par1] [1] "Income" > x[,par1] [1] 20 14 18 12 16 13 22 16 20 10 22 17 21 18 13 17 17 19 12 14 13 20 20 13 21 [26] 21 19 18 20 14 14 20 21 14 16 21 16 14 19 22 19 11 13 16 14 19 21 12 17 21 [51] 11 19 18 14 19 20 18 22 16 20 14 22 25 20 > 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/1t8uk1323781689.tab") + } + } > m Conditional inference tree with 6 terminal nodes Response: Income Inputs: Month, Change, Size, Complex, Big4, Product Number of observations: 64 1) Size <= 11; criterion = 1, statistic = 60.516 2) Size <= 9; criterion = 1, statistic = 24.753 3) Size <= 7; criterion = 0.999, statistic = 14.938 4)* weights = 10 3) Size > 7 5)* weights = 10 2) Size > 9 6)* weights = 8 1) Size > 11 7) Size <= 15; criterion = 1, statistic = 26.927 8) Size <= 13; criterion = 0.997, statistic = 12.3 9)* weights = 9 8) Size > 13 10)* weights = 17 7) Size > 15 11)* weights = 10 > postscript(file="/var/www/rcomp/tmp/2u7rx1323781689.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/3jx4k1323781689.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 20 19.82353 0.1764706 2 14 13.90000 0.1000000 3 18 18.11111 -0.1111111 4 12 12.00000 0.0000000 5 16 16.25000 -0.2500000 6 13 12.00000 1.0000000 7 22 21.90000 0.1000000 8 16 16.25000 -0.2500000 9 20 19.82353 0.1764706 10 10 12.00000 -2.0000000 11 22 21.90000 0.1000000 12 17 16.25000 0.7500000 13 21 21.90000 -0.9000000 14 18 18.11111 -0.1111111 15 13 12.00000 1.0000000 16 17 18.11111 -1.1111111 17 17 16.25000 0.7500000 18 19 19.82353 -0.8235294 19 12 12.00000 0.0000000 20 14 13.90000 0.1000000 21 13 12.00000 1.0000000 22 20 19.82353 0.1764706 23 20 19.82353 0.1764706 24 13 12.00000 1.0000000 25 21 19.82353 1.1764706 26 21 21.90000 -0.9000000 27 19 19.82353 -0.8235294 28 18 19.82353 -1.8235294 29 20 19.82353 0.1764706 30 14 13.90000 0.1000000 31 14 13.90000 0.1000000 32 20 19.82353 0.1764706 33 21 19.82353 1.1764706 34 14 13.90000 0.1000000 35 16 16.25000 -0.2500000 36 21 21.90000 -0.9000000 37 16 16.25000 -0.2500000 38 14 13.90000 0.1000000 39 19 19.82353 -0.8235294 40 22 21.90000 0.1000000 41 19 18.11111 0.8888889 42 11 12.00000 -1.0000000 43 13 13.90000 -0.9000000 44 16 16.25000 -0.2500000 45 14 13.90000 0.1000000 46 19 18.11111 0.8888889 47 21 19.82353 1.1764706 48 12 12.00000 0.0000000 49 17 18.11111 -1.1111111 50 21 21.90000 -0.9000000 51 11 12.00000 -1.0000000 52 19 19.82353 -0.8235294 53 18 18.11111 -0.1111111 54 14 13.90000 0.1000000 55 19 18.11111 0.8888889 56 20 19.82353 0.1764706 57 18 18.11111 -0.1111111 58 22 21.90000 0.1000000 59 16 16.25000 -0.2500000 60 20 19.82353 0.1764706 61 14 13.90000 0.1000000 62 22 21.90000 0.1000000 63 25 21.90000 3.1000000 64 20 19.82353 0.1764706 > 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/4f5ph1323781689.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/5upgu1323781689.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/6iwfg1323781689.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/7pv651323781689.tab") + } > > try(system("convert tmp/2u7rx1323781689.ps tmp/2u7rx1323781689.png",intern=TRUE)) character(0) > try(system("convert tmp/3jx4k1323781689.ps tmp/3jx4k1323781689.png",intern=TRUE)) character(0) > try(system("convert tmp/4f5ph1323781689.ps tmp/4f5ph1323781689.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.632 0.300 4.878