R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 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(16198.9 + ,16896.2 + ,0 + ,16554.2 + ,16698 + ,0 + ,19554.2 + ,19691.6 + ,0 + ,15903.8 + ,15930.7 + ,0 + ,18003.8 + ,17444.6 + ,0 + ,18329.6 + ,17699.4 + ,0 + ,16260.7 + ,15189.8 + ,0 + ,14851.9 + ,15672.7 + ,0 + ,18174.1 + ,17180.8 + ,0 + ,18406.6 + ,17664.9 + ,0 + ,18466.5 + ,17862.9 + ,0 + ,16016.5 + ,16162.3 + ,0 + ,17428.5 + ,17463.6 + ,0 + ,17167.2 + ,16772.1 + ,0 + ,19630 + ,19106.9 + ,0 + ,17183.6 + ,16721.3 + ,0 + ,18344.7 + ,18161.3 + ,0 + ,19301.4 + ,18509.9 + ,0 + ,18147.5 + ,17802.7 + ,0 + ,16192.9 + ,16409.9 + ,0 + ,18374.4 + ,17967.7 + ,0 + ,20515.2 + ,20286.6 + ,0 + ,18957.2 + ,19537.3 + ,0 + ,16471.5 + ,18021.9 + ,0 + ,18746.8 + ,20194.3 + ,0 + ,19009.5 + ,19049.6 + ,0 + ,19211.2 + ,20244.7 + ,0 + ,20547.7 + ,21473.3 + ,0 + ,19325.8 + ,19673.6 + ,0 + ,20605.5 + ,21053.2 + ,0 + ,20056.9 + ,20159.5 + ,0 + ,16141.4 + ,18203.6 + ,0 + ,20359.8 + ,21289.5 + ,0 + ,19711.6 + ,20432.3 + ,1 + ,15638.6 + ,17180.4 + ,1 + ,14384.5 + ,15816.8 + ,1 + ,13855.6 + ,15071.8 + ,1 + ,14308.3 + ,14521.1 + ,1 + ,15290.6 + ,15668.8 + ,1 + ,14423.8 + ,14346.9 + ,1 + ,13779.7 + ,13881 + ,1 + ,15686.3 + ,15465.9 + ,1 + ,14733.8 + ,14238.2 + ,1 + ,12522.5 + ,13557.7 + ,1 + ,16189.4 + ,16127.6 + ,1 + ,16059.1 + ,16793.9 + ,1 + ,16007.1 + ,16014 + ,1 + ,15806.8 + ,16867.9 + ,1 + ,15160 + ,16014.6 + ,0 + ,15692.1 + ,15878.6 + ,0 + ,18908.9 + ,18664.9 + ,0 + ,16969.9 + ,17962.5 + ,0 + ,16997.5 + ,17332.7 + ,0 + ,19858.9 + ,19542.1 + ,0 + ,17681.2 + ,17203.6 + ,0) + ,dim=c(3 + ,55) + ,dimnames=list(c('Uitvoer' + ,'Invoer' + ,'Crisis') + ,1:55)) > y <- array(NA,dim=c(3,55),dimnames=list(c('Uitvoer','Invoer','Crisis'),1:55)) > for (i in 1:dim(x)[1]) + { + for (j in 1:dim(x)[2]) + { + y[i,j] <- as.numeric(x[i,j]) + } + } > par4 = 'yes' > par3 = '4' > par2 = 'quantiles' > par1 = '1' > #'GNU S' R Code compiled by R2WASP v. 1.0.44 () > #Author: Dr. Ian E. Holliday > #To cite this work: Ian E. Holliday, 2009, YOUR SOFTWARE TITLE (vNUMBER) in Free Statistics Software (v$_version), Office for Research Development and Education, URL http://www.wessa.net/rwasp_YOURPAGE.wasp/ > #Source of accompanying publication: > #Technical description: > 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.numeric 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] "Uitvoer" > x[,par1] [1] 16198.9 16554.2 19554.2 15903.8 18003.8 18329.6 16260.7 14851.9 18174.1 [10] 18406.6 18466.5 16016.5 17428.5 17167.2 19630.0 17183.6 18344.7 19301.4 [19] 18147.5 16192.9 18374.4 20515.2 18957.2 16471.5 18746.8 19009.5 19211.2 [28] 20547.7 19325.8 20605.5 20056.9 16141.4 20359.8 19711.6 15638.6 14384.5 [37] 13855.6 14308.3 15290.6 14423.8 13779.7 15686.3 14733.8 12522.5 16189.4 [46] 16059.1 16007.1 15806.8 15160.0 15692.1 18908.9 16969.9 16997.5 19858.9 [55] 17681.2 > 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]) [12522,15904) [15904,17184) [17184,19010) [19010,20606] 14 14 14 13 > colnames(x) [1] "Uitvoer" "Invoer" "Crisis" > colnames(x)[par1] [1] "Uitvoer" > x[,par1] [1] [15904,17184) [15904,17184) [19010,20606] [15904,17184) [17184,19010) [6] [17184,19010) [15904,17184) [12522,15904) [17184,19010) [17184,19010) [11] [17184,19010) [15904,17184) [17184,19010) [15904,17184) [19010,20606] [16] [17184,19010) [17184,19010) [19010,20606] [17184,19010) [15904,17184) [21] [17184,19010) [19010,20606] [17184,19010) [15904,17184) [17184,19010) [26] [19010,20606] [19010,20606] [19010,20606] [19010,20606] [19010,20606] [31] [19010,20606] [15904,17184) [19010,20606] [19010,20606] [12522,15904) [36] [12522,15904) [12522,15904) [12522,15904) [12522,15904) [12522,15904) [41] [12522,15904) [12522,15904) [12522,15904) [12522,15904) [15904,17184) [46] [15904,17184) [15904,17184) [12522,15904) [12522,15904) [12522,15904) [51] [17184,19010) [15904,17184) [15904,17184) [19010,20606] [17184,19010) Levels: [12522,15904) [15904,17184) [17184,19010) [19010,20606] > if (par2 == 'none') { + m <- ctree(as.formula(paste(colnames(x)[par1],' ~ .',sep='')),data = x) + } > > #Note: the /var/www/html/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/html/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/html/rcomp/tmp/13pmc1292172482.tab") + } + } m.ct.i.pred m.ct.i.actu 1 2 3 4 1 104 23 2 0 2 10 93 20 0 3 0 61 41 26 4 0 0 0 122 [1] 0.8062016 [1] 0.7560976 [1] 0.3203125 [1] 1 [1] 0.7171315 m.ct.x.pred m.ct.x.actu 1 2 3 4 1 5 4 2 0 2 6 6 4 1 3 0 7 2 3 4 0 0 1 7 [1] 0.4545455 [1] 0.3529412 [1] 0.1666667 [1] 0.875 [1] 0.4166667 > m Conditional inference tree with 3 terminal nodes Response: as.factor(Uitvoer) Inputs: Invoer, Crisis Number of observations: 55 1) Invoer <= 18203.6; criterion = 1, statistic = 42.038 2) Invoer <= 15878.6; criterion = 1, statistic = 21.042 3)* weights = 12 2) Invoer > 15878.6 4)* weights = 27 1) Invoer > 18203.6 5)* weights = 16 > postscript(file="/var/www/html/rcomp/tmp/2wg4x1292172482.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/html/rcomp/tmp/3wg4x1292172482.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) + } > if (par2 != 'none') { + print(cbind(as.factor(x[,par1]),predict(m))) + myt <- table(as.factor(x[,par1]),predict(m)) + print(myt) + } [,1] [,2] [1,] 2 2 [2,] 2 2 [3,] 4 4 [4,] 2 2 [5,] 3 2 [6,] 3 2 [7,] 2 1 [8,] 1 1 [9,] 3 2 [10,] 3 2 [11,] 3 2 [12,] 2 2 [13,] 3 2 [14,] 2 2 [15,] 4 4 [16,] 3 2 [17,] 3 2 [18,] 4 4 [19,] 3 2 [20,] 2 2 [21,] 3 2 [22,] 4 4 [23,] 3 4 [24,] 2 2 [25,] 3 4 [26,] 4 4 [27,] 4 4 [28,] 4 4 [29,] 4 4 [30,] 4 4 [31,] 4 4 [32,] 2 2 [33,] 4 4 [34,] 4 4 [35,] 1 2 [36,] 1 1 [37,] 1 1 [38,] 1 1 [39,] 1 1 [40,] 1 1 [41,] 1 1 [42,] 1 1 [43,] 1 1 [44,] 1 1 [45,] 2 2 [46,] 2 2 [47,] 2 2 [48,] 1 2 [49,] 1 2 [50,] 1 1 [51,] 3 4 [52,] 2 2 [53,] 2 2 [54,] 4 4 [55,] 3 2 [12522,15904) [15904,17184) [17184,19010) [19010,20606] [12522,15904) 11 3 0 0 [15904,17184) 1 13 0 0 [17184,19010) 0 11 0 3 [19010,20606] 0 0 0 13 > postscript(file="/var/www/html/rcomp/tmp/4ppl01292172482.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/html/rcomp/tmp/5aqjo1292172482.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/html/rcomp/tmp/6v8it1292172482.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/html/rcomp/tmp/7hryh1292172482.tab") + } > > try(system("convert tmp/2wg4x1292172482.ps tmp/2wg4x1292172482.png",intern=TRUE)) character(0) > try(system("convert tmp/3wg4x1292172482.ps tmp/3wg4x1292172482.png",intern=TRUE)) character(0) > try(system("convert tmp/4ppl01292172482.ps tmp/4ppl01292172482.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 2.479 0.496 5.269