R version 2.13.0 (2011-04-13) Copyright (C) 2011 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(210907,0,2,149061,0,0,237213,1,0,133131,1,4,324799,1,0,230964,0,-1,236785,1,0,344297,1,1,174724,1,0,174415,1,3,223632,1,-1,294424,0,4,325107,1,3,106408,0,1,96560,0,0,265769,1,-2,149112,0,-4,152871,0,2,362301,1,2,183167,0,-4,218946,1,2,244052,1,2,341570,1,0,196553,1,-3,143246,0,2,167488,0,0,143756,0,4,152299,1,2,193339,1,2,130585,0,-4,112611,1,3,148446,1,3,182079,0,2,243060,1,-1,162765,1,-3,85574,1,0,225060,0,1,133328,1,-3,100750,1,3,101523,1,0,243511,1,0,152474,1,0,132487,1,3,317394,0,-3,244749,1,0,128423,0,2,97839,0,-1,229242,1,2,324598,0,2,195838,0,-2,254488,0,0,92499,1,-2,224330,0,0,181633,1,6,271856,1,-3,95227,1,3,98146,0,0,118612,0,-2,65475,1,1,108446,0,0,121848,0,2,76302,1,2,98104,0,-3,30989,1,-2,31774,0,1,150580,1,-4,59382,0,1,84105,0,0),dim=c(3,68),dimnames=list(c('RFCseconds','Gender','Testscore'),1:68)) > y <- array(NA,dim=c(3,68),dimnames=list(c('RFCseconds','Gender','Testscore'),1:68)) > 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 = '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 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] "RFCseconds" > x[,par1] [1] 210907 149061 237213 133131 324799 230964 236785 344297 174724 174415 [11] 223632 294424 325107 106408 96560 265769 149112 152871 362301 183167 [21] 218946 244052 341570 196553 143246 167488 143756 152299 193339 130585 [31] 112611 148446 182079 243060 162765 85574 225060 133328 100750 101523 [41] 243511 152474 132487 317394 244749 128423 97839 229242 324598 195838 [51] 254488 92499 224330 181633 271856 95227 98146 118612 65475 108446 [61] 121848 76302 98104 30989 31774 150580 59382 84105 > 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]) 30989 31774 59382 65475 76302 84105 85574 92499 95227 96560 97839 1 1 1 1 1 1 1 1 1 1 1 98104 98146 100750 101523 106408 108446 112611 118612 121848 128423 130585 1 1 1 1 1 1 1 1 1 1 1 132487 133131 133328 143246 143756 148446 149061 149112 150580 152299 152474 1 1 1 1 1 1 1 1 1 1 1 152871 162765 167488 174415 174724 181633 182079 183167 193339 195838 196553 1 1 1 1 1 1 1 1 1 1 1 210907 218946 223632 224330 225060 229242 230964 236785 237213 243060 243511 1 1 1 1 1 1 1 1 1 1 1 244052 244749 254488 265769 271856 294424 317394 324598 324799 325107 341570 1 1 1 1 1 1 1 1 1 1 1 344297 362301 1 1 > colnames(x) [1] "RFCseconds" "Gender" "Testscore" > colnames(x)[par1] [1] "RFCseconds" > x[,par1] [1] 210907 149061 237213 133131 324799 230964 236785 344297 174724 174415 [11] 223632 294424 325107 106408 96560 265769 149112 152871 362301 183167 [21] 218946 244052 341570 196553 143246 167488 143756 152299 193339 130585 [31] 112611 148446 182079 243060 162765 85574 225060 133328 100750 101523 [41] 243511 152474 132487 317394 244749 128423 97839 229242 324598 195838 [51] 254488 92499 224330 181633 271856 95227 98146 118612 65475 108446 [61] 121848 76302 98104 30989 31774 150580 59382 84105 > 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/1h0t51323791401.tab") + } + } > m Conditional inference tree with 1 terminal nodes Response: RFCseconds Inputs: Gender, Testscore Number of observations: 68 1)* weights = 68 > postscript(file="/var/wessaorg/rcomp/tmp/24vuq1323791401.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/3pyp81323791401.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 210907 176956.3 33950.706 2 149061 176956.3 -27895.294 3 237213 176956.3 60256.706 4 133131 176956.3 -43825.294 5 324799 176956.3 147842.706 6 230964 176956.3 54007.706 7 236785 176956.3 59828.706 8 344297 176956.3 167340.706 9 174724 176956.3 -2232.294 10 174415 176956.3 -2541.294 11 223632 176956.3 46675.706 12 294424 176956.3 117467.706 13 325107 176956.3 148150.706 14 106408 176956.3 -70548.294 15 96560 176956.3 -80396.294 16 265769 176956.3 88812.706 17 149112 176956.3 -27844.294 18 152871 176956.3 -24085.294 19 362301 176956.3 185344.706 20 183167 176956.3 6210.706 21 218946 176956.3 41989.706 22 244052 176956.3 67095.706 23 341570 176956.3 164613.706 24 196553 176956.3 19596.706 25 143246 176956.3 -33710.294 26 167488 176956.3 -9468.294 27 143756 176956.3 -33200.294 28 152299 176956.3 -24657.294 29 193339 176956.3 16382.706 30 130585 176956.3 -46371.294 31 112611 176956.3 -64345.294 32 148446 176956.3 -28510.294 33 182079 176956.3 5122.706 34 243060 176956.3 66103.706 35 162765 176956.3 -14191.294 36 85574 176956.3 -91382.294 37 225060 176956.3 48103.706 38 133328 176956.3 -43628.294 39 100750 176956.3 -76206.294 40 101523 176956.3 -75433.294 41 243511 176956.3 66554.706 42 152474 176956.3 -24482.294 43 132487 176956.3 -44469.294 44 317394 176956.3 140437.706 45 244749 176956.3 67792.706 46 128423 176956.3 -48533.294 47 97839 176956.3 -79117.294 48 229242 176956.3 52285.706 49 324598 176956.3 147641.706 50 195838 176956.3 18881.706 51 254488 176956.3 77531.706 52 92499 176956.3 -84457.294 53 224330 176956.3 47373.706 54 181633 176956.3 4676.706 55 271856 176956.3 94899.706 56 95227 176956.3 -81729.294 57 98146 176956.3 -78810.294 58 118612 176956.3 -58344.294 59 65475 176956.3 -111481.294 60 108446 176956.3 -68510.294 61 121848 176956.3 -55108.294 62 76302 176956.3 -100654.294 63 98104 176956.3 -78852.294 64 30989 176956.3 -145967.294 65 31774 176956.3 -145182.294 66 150580 176956.3 -26376.294 67 59382 176956.3 -117574.294 68 84105 176956.3 -92851.294 > 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/4qsh21323791401.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/58vyu1323791401.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/6ccwa1323791401.tab") + } Warning message: In cor(result$Forecasts, result$Actuals) : the standard deviation is zero > 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/797ky1323791401.tab") + } > > try(system("convert tmp/24vuq1323791401.ps tmp/24vuq1323791401.png",intern=TRUE)) character(0) > try(system("convert tmp/3pyp81323791401.ps tmp/3pyp81323791401.png",intern=TRUE)) character(0) > try(system("convert tmp/4qsh21323791401.ps tmp/4qsh21323791401.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 2.845 0.286 3.128