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(521 + ,18308 + ,185 + ,4.041 + ,79.6 + ,7.2 + ,367 + ,1148 + ,600 + ,0.55 + ,1 + ,8.5 + ,443 + ,18068 + ,372 + ,3.665 + ,32.3 + ,5.7 + ,365 + ,7729 + ,142 + ,2.351 + ,45.1 + ,7.3 + ,614 + ,100484 + ,432 + ,29.76 + ,190.8 + ,7.5 + ,385 + ,16728 + ,290 + ,3.294 + ,31.8 + ,5 + ,286 + ,14630 + ,346 + ,3.287 + ,678.4 + ,6.7 + ,397 + ,4008 + ,328 + ,0.666 + ,340.8 + ,6.2 + ,764 + ,38927 + ,354 + ,12.938 + ,239.6 + ,7.3 + ,427 + ,22322 + ,266 + ,6.478 + ,111.9 + ,5 + ,153 + ,3711 + ,320 + ,1.108 + ,172.5 + ,2.8 + ,231 + ,3136 + ,197 + ,1.007 + ,12.2 + ,6.1 + ,524 + ,50508 + ,266 + ,11.431 + ,205.6 + ,7.1 + ,328 + ,28886 + ,173 + ,5.544 + ,154.6 + ,5.9 + ,240 + ,16996 + ,190 + ,2.777 + ,49.7 + ,4.6 + ,286 + ,13035 + ,239 + ,2.478 + ,30.3 + ,4.4 + ,285 + ,12973 + ,190 + ,3.685 + ,92.8 + ,7.4 + ,569 + ,16309 + ,241 + ,4.22 + ,96.9 + ,7.1 + ,96 + ,5227 + ,189 + ,1.228 + ,39.8 + ,7.5 + ,498 + ,19235 + ,358 + ,4.781 + ,489.2 + ,5.9 + ,481 + ,44487 + ,315 + ,6.016 + ,767.6 + ,9 + ,468 + ,44213 + ,303 + ,9.295 + ,163.6 + ,9.2 + ,177 + ,23619 + ,228 + ,4.375 + ,55 + ,5.1 + ,198 + ,9106 + ,134 + ,2.573 + ,54.9 + ,8.6 + ,458 + ,24917 + ,189 + ,5.117 + ,74.3 + ,6.6 + ,108 + ,3872 + ,196 + ,0.799 + ,5.5 + ,6.9 + ,246 + ,8945 + ,183 + ,1.578 + ,20.5 + ,2.7 + ,291 + ,2373 + ,417 + ,1.202 + ,10.9 + ,5.5 + ,68 + ,7128 + ,233 + ,1.109 + ,123.7 + ,7.2 + ,311 + ,23624 + ,349 + ,7.73 + ,1042 + ,6.6 + ,606 + ,5242 + ,284 + ,1.515 + ,12.5 + ,6.9 + ,512 + ,92629 + ,499 + ,17.99 + ,381 + ,7.2 + ,426 + ,28795 + ,231 + ,6.629 + ,136.1 + ,5.8 + ,47 + ,4487 + ,143 + ,0.639 + ,9.3 + ,4.1 + ,265 + ,48799 + ,249 + ,10.847 + ,264.9 + ,6.4 + ,370 + ,14067 + ,195 + ,3.146 + ,45.8 + ,6.7 + ,312 + ,12693 + ,288 + ,2.842 + ,29.6 + ,6 + ,222 + ,62184 + ,229 + ,11.882 + ,265.1 + ,6.9 + ,280 + ,9153 + ,287 + ,1.003 + ,960.3 + ,8.5 + ,759 + ,14250 + ,224 + ,3.487 + ,115.8 + ,6.2 + ,114 + ,3680 + ,161 + ,0.696 + ,9.2 + ,3.4 + ,419 + ,18063 + ,221 + ,4.877 + ,118.3 + ,6.6 + ,435 + ,65112 + ,237 + ,16.987 + ,64.9 + ,6.6 + ,186 + ,11340 + ,220 + ,1.723 + ,21 + ,4.9 + ,87 + ,4553 + ,185 + ,0.563 + ,60.8 + ,6.4 + ,188 + ,28960 + ,260 + ,6.187 + ,156.3 + ,5.8 + ,303 + ,19201 + ,261 + ,4.867 + ,73.1 + ,6.3 + ,102 + ,7533 + ,118 + ,1.793 + ,74.5 + ,10.5 + ,127 + ,26343 + ,268 + ,4.892 + ,90.1 + ,5.4 + ,251 + ,1641 + ,300 + ,0.454 + ,4.7 + ,5.1 + ,205 + ,145360 + ,237 + ,10.379 + ,889 + ,6.8 + ,453 + ,9066420 + ,240 + ,82.422 + ,609 + ,5.6 + ,320 + ,1038933 + ,185 + ,16.491 + ,1259 + ,3.8 + ,405 + ,2739420 + ,201 + ,60.876 + ,289 + ,8.2 + ,89 + ,61620 + ,193 + ,0.474 + ,475 + ,4.1 + ,74 + ,827530 + ,254 + ,7.523 + ,490 + ,2.8 + ,101 + ,534100 + ,230 + ,5.45 + ,333 + ,6.3 + ,321 + ,328755 + ,197 + ,10.605 + ,300 + ,11.4 + ,315 + ,1413895 + ,248 + ,40.397 + ,210 + ,19.4 + ,229 + ,2909136 + ,258 + ,60.607 + ,650 + ,5.8 + ,302 + ,3604246 + ,206 + ,58.133 + ,512 + ,6.9 + ,216 + ,917504 + ,199 + ,8.192 + ,256 + ,3.5) + ,dim=c(6 + ,62) + ,dimnames=list(c('Assaults' + ,'BachDegrees' + ,'PoliceExp' + ,'Population' + ,'Density' + ,'Unemployment') + ,1:62)) > y <- array(NA,dim=c(6,62),dimnames=list(c('Assaults','BachDegrees','PoliceExp','Population','Density','Unemployment'),1:62)) > 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 = 'none' > par2 = 'none' > par1 = '1' > 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) Warning message: NAs introduced by coercion > 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] "Assaults" > x[,par1] [1] 521 367 443 365 614 385 286 397 764 427 153 231 524 328 240 286 285 569 96 [20] 498 481 468 177 198 458 108 246 291 68 311 606 512 426 47 265 370 312 222 [39] 280 759 114 419 435 186 87 188 303 102 127 251 205 453 320 405 89 74 101 [58] 321 315 229 302 216 > 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]) 47 68 74 87 89 96 101 102 108 114 127 153 177 186 188 198 205 216 222 229 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 231 240 246 251 265 280 285 286 291 302 303 311 312 315 320 321 328 365 367 370 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 385 397 405 419 426 427 435 443 453 458 468 481 498 512 521 524 569 606 614 759 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 764 1 > colnames(x) [1] "Assaults" "BachDegrees" "PoliceExp" "Population" "Density" [6] "Unemployment" > colnames(x)[par1] [1] "Assaults" > x[,par1] [1] 521 367 443 365 614 385 286 397 764 427 153 231 524 328 240 286 285 569 96 [20] 498 481 468 177 198 458 108 246 291 68 311 606 512 426 47 265 370 312 222 [39] 280 759 114 419 435 186 87 188 303 102 127 251 205 453 320 405 89 74 101 [58] 321 315 229 302 216 > if (par2 == 'none') { + m <- ctree(as.formula(paste(colnames(x)[par1],' ~ .',sep='')),data = x) + } > > #Note: the /var/fisher/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/fisher/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/fisher/rcomp/tmp/1w2e71355253035.tab") + } + } > m Conditional inference tree with 2 terminal nodes Response: Assaults Inputs: BachDegrees, PoliceExp, Population, Density, Unemployment Number of observations: 62 1) PoliceExp <= 349; criterion = 0.987, statistic = 9.122 2)* weights = 55 1) PoliceExp > 349 3)* weights = 7 > postscript(file="/var/fisher/rcomp/tmp/2c8ss1355253035.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/fisher/rcomp/tmp/3mv6o1355253035.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 521 293.4000 227.6000000 2 367 498.4286 -131.4285714 3 443 498.4286 -55.4285714 4 365 293.4000 71.6000000 5 614 498.4286 115.5714286 6 385 293.4000 91.6000000 7 286 293.4000 -7.4000000 8 397 293.4000 103.6000000 9 764 498.4286 265.5714286 10 427 293.4000 133.6000000 11 153 293.4000 -140.4000000 12 231 293.4000 -62.4000000 13 524 293.4000 230.6000000 14 328 293.4000 34.6000000 15 240 293.4000 -53.4000000 16 286 293.4000 -7.4000000 17 285 293.4000 -8.4000000 18 569 293.4000 275.6000000 19 96 293.4000 -197.4000000 20 498 498.4286 -0.4285714 21 481 293.4000 187.6000000 22 468 293.4000 174.6000000 23 177 293.4000 -116.4000000 24 198 293.4000 -95.4000000 25 458 293.4000 164.6000000 26 108 293.4000 -185.4000000 27 246 293.4000 -47.4000000 28 291 498.4286 -207.4285714 29 68 293.4000 -225.4000000 30 311 293.4000 17.6000000 31 606 293.4000 312.6000000 32 512 498.4286 13.5714286 33 426 293.4000 132.6000000 34 47 293.4000 -246.4000000 35 265 293.4000 -28.4000000 36 370 293.4000 76.6000000 37 312 293.4000 18.6000000 38 222 293.4000 -71.4000000 39 280 293.4000 -13.4000000 40 759 293.4000 465.6000000 41 114 293.4000 -179.4000000 42 419 293.4000 125.6000000 43 435 293.4000 141.6000000 44 186 293.4000 -107.4000000 45 87 293.4000 -206.4000000 46 188 293.4000 -105.4000000 47 303 293.4000 9.6000000 48 102 293.4000 -191.4000000 49 127 293.4000 -166.4000000 50 251 293.4000 -42.4000000 51 205 293.4000 -88.4000000 52 453 293.4000 159.6000000 53 320 293.4000 26.6000000 54 405 293.4000 111.6000000 55 89 293.4000 -204.4000000 56 74 293.4000 -219.4000000 57 101 293.4000 -192.4000000 58 321 293.4000 27.6000000 59 315 293.4000 21.6000000 60 229 293.4000 -64.4000000 61 302 293.4000 8.6000000 62 216 293.4000 -77.4000000 > if (par2 != 'none') { + print(cbind(as.factor(x[,par1]),predict(m))) + myt <- table(as.factor(x[,par1]),predict(m)) + print(myt) + } > postscript(file="/var/fisher/rcomp/tmp/4rxx11355253035.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/fisher/rcomp/tmp/5e24n1355253035.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/fisher/rcomp/tmp/6pgj91355253035.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/fisher/rcomp/tmp/7lv7q1355253035.tab") + } > > try(system("convert tmp/2c8ss1355253035.ps tmp/2c8ss1355253035.png",intern=TRUE)) character(0) > try(system("convert tmp/3mv6o1355253035.ps tmp/3mv6o1355253035.png",intern=TRUE)) character(0) > try(system("convert tmp/4rxx11355253035.ps tmp/4rxx11355253035.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.933 0.583 4.510