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(356,182,89,386,213,97,444,227,154,387,209,81,327,219,110,448,221,116,225,114,73,182,97,73,460,205,174,411,215,103,342,224,130,361,189,91,377,182,136,331,201,106,428,198,136,340,173,122,352,238,131,461,258,135,221,122,75,198,101,68,422,259,143,329,243,115,320,188,93,375,173,128,364,224,152,351,215,125,380,196,107,319,159,116,322,187,220,386,208,137,221,131,34,187,93,51,344,210,153,342,228,145,365,176,116,313,195,145,356,188,98,337,188,118,389,190,139,326,188,140,343,176,113,357,225,149,220,93,79,218,79,47,391,235,166,425,247,180,332,195,122,298,197,134,360,211,114,336,156,125,325,209,181,393,180,142,301,185,143,426,303,187,265,129,137,210,85,62,429,249,239,440,231,157,357,212,139,431,240,187,442,234,99,442,217,146,544,287,175,420,221,148,396,208,130,482,241,183,261,156,115,211,96,80,448,320,223,468,242,131,464,227,201,425,200,157),dim=c(3,72),dimnames=list(c('Vlaanderen','Walloniƫ','Brussel'),1:72)) > y <- array(NA,dim=c(3,72),dimnames=list(c('Vlaanderen','Walloniƫ','Brussel'),1:72)) > 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 = '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] "Vlaanderen" > x[,par1] [1] 356 386 444 387 327 448 225 182 460 411 342 361 377 331 428 340 352 461 221 [20] 198 422 329 320 375 364 351 380 319 322 386 221 187 344 342 365 313 356 337 [39] 389 326 343 357 220 218 391 425 332 298 360 336 325 393 301 426 265 210 429 [58] 440 357 431 442 442 544 420 396 482 261 211 448 468 464 425 > 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]) [182,336) [336,391) [391,544] 24 24 24 > colnames(x) [1] "Vlaanderen" "Walloni.." "Brussel" > colnames(x)[par1] [1] "Vlaanderen" > x[,par1] [1] [336,391) [336,391) [391,544] [336,391) [182,336) [391,544] [182,336) [8] [182,336) [391,544] [391,544] [336,391) [336,391) [336,391) [182,336) [15] [391,544] [336,391) [336,391) [391,544] [182,336) [182,336) [391,544] [22] [182,336) [182,336) [336,391) [336,391) [336,391) [336,391) [182,336) [29] [182,336) [336,391) [182,336) [182,336) [336,391) [336,391) [336,391) [36] [182,336) [336,391) [336,391) [336,391) [182,336) [336,391) [336,391) [43] [182,336) [182,336) [391,544] [391,544] [182,336) [182,336) [336,391) [50] [336,391) [182,336) [391,544] [182,336) [391,544] [182,336) [182,336) [57] [391,544] [391,544] [336,391) [391,544] [391,544] [391,544] [391,544] [64] [391,544] [391,544] [391,544] [182,336) [182,336) [391,544] [391,544] [71] [391,544] [391,544] Levels: [182,336) [336,391) [391,544] > 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/1mxzx1292094501.tab") + } + } > m Conditional inference tree with 3 terminal nodes Response: as.factor(Vlaanderen) Inputs: Walloni.., Brussel Number of observations: 72 1) Walloni.. <= 159; criterion = 1, statistic = 33.662 2)* weights = 14 1) Walloni.. > 159 3) Walloni.. <= 225; criterion = 1, statistic = 18.329 4)* weights = 40 3) Walloni.. > 225 5)* weights = 18 > postscript(file="/var/www/html/rcomp/tmp/2mxzx1292094501.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/3mxzx1292094501.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,] 3 3 [4,] 2 2 [5,] 1 2 [6,] 3 2 [7,] 1 1 [8,] 1 1 [9,] 3 2 [10,] 3 2 [11,] 2 2 [12,] 2 2 [13,] 2 2 [14,] 1 2 [15,] 3 2 [16,] 2 2 [17,] 2 3 [18,] 3 3 [19,] 1 1 [20,] 1 1 [21,] 3 3 [22,] 1 3 [23,] 1 2 [24,] 2 2 [25,] 2 2 [26,] 2 2 [27,] 2 2 [28,] 1 1 [29,] 1 2 [30,] 2 2 [31,] 1 1 [32,] 1 1 [33,] 2 2 [34,] 2 3 [35,] 2 2 [36,] 1 2 [37,] 2 2 [38,] 2 2 [39,] 2 2 [40,] 1 2 [41,] 2 2 [42,] 2 2 [43,] 1 1 [44,] 1 1 [45,] 3 3 [46,] 3 3 [47,] 1 2 [48,] 1 2 [49,] 2 2 [50,] 2 1 [51,] 1 2 [52,] 3 2 [53,] 1 2 [54,] 3 3 [55,] 1 1 [56,] 1 1 [57,] 3 3 [58,] 3 3 [59,] 2 2 [60,] 3 3 [61,] 3 3 [62,] 3 2 [63,] 3 3 [64,] 3 2 [65,] 3 2 [66,] 3 3 [67,] 1 1 [68,] 1 1 [69,] 3 3 [70,] 3 3 [71,] 3 3 [72,] 3 2 [182,336) [336,391) [391,544] [182,336) 13 10 1 [336,391) 1 21 2 [391,544] 0 9 15 > postscript(file="/var/www/html/rcomp/tmp/4f6y01292094501.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/5bywr1292094501.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/6wydf1292094501.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/7ppcz1292094501.tab") + } > > try(system("convert tmp/2mxzx1292094501.ps tmp/2mxzx1292094501.png",intern=TRUE)) character(0) > try(system("convert tmp/3mxzx1292094501.ps tmp/3mxzx1292094501.png",intern=TRUE)) character(0) > try(system("convert tmp/4f6y01292094501.ps tmp/4f6y01292094501.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.965 0.493 5.266