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(70.5,4.0,370,53.5,315.0,6166,65.0,4.0,684,76.5,1.7,449,70.0,8.0,643,71.0,5.6,1551,60.5,15.0,616,51.5,503.0,36660,78.0,2.6,403,76.0,2.6,346,57.5,44.0,2471,61.0,24.0,7427,64.5,23.0,2992,78.5,3.8,233,79.0,1.8,609,61.0,96.0,7615,70.0,90.0,370,70.0,4.9,1066,72.0,6.6,600,64.5,21.0,4873,54.5,592.0,3485,56.5,73.0,2364,64.5,14.0,1016,64.5,8.8,1062,73.0,3.9,480,72.0,6.0,559,69.0,3.2,259,64.0,11.0,1340,78.5,2.6,275,53.0,23.0,12550,75.0,3.2,965,68.5,11.0,4883,70.0,5.0,1189,70.5,3.0,226,76.0,3.0,611,75.5,1.3,404,74.5,5.6,576,65.0,29.0,3096),dim=c(3,38),dimnames=list(c('le','ppt','ppp'),1:38)) > y <- array(NA,dim=c(3,38),dimnames=list(c('le','ppt','ppp'),1:38)) > 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' > #'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 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] "le" > x[,par1] [1] 70.5 53.5 65.0 76.5 70.0 71.0 60.5 51.5 78.0 76.0 57.5 61.0 64.5 78.5 79.0 [16] 61.0 70.0 70.0 72.0 64.5 54.5 56.5 64.5 64.5 73.0 72.0 69.0 64.0 78.5 53.0 [31] 75.0 68.5 70.0 70.5 76.0 75.5 74.5 65.0 > 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]) 51.5 53 53.5 54.5 56.5 57.5 60.5 61 64 64.5 65 68.5 69 70 70.5 71 1 1 1 1 1 1 1 2 1 4 2 1 1 4 2 1 72 73 74.5 75 75.5 76 76.5 78 78.5 79 2 1 1 1 1 2 1 1 2 1 > colnames(x) [1] "le" "ppt" "ppp" > colnames(x)[par1] [1] "le" > x[,par1] [1] 70.5 53.5 65.0 76.5 70.0 71.0 60.5 51.5 78.0 76.0 57.5 61.0 64.5 78.5 79.0 [16] 61.0 70.0 70.0 72.0 64.5 54.5 56.5 64.5 64.5 73.0 72.0 69.0 64.0 78.5 53.0 [31] 75.0 68.5 70.0 70.5 76.0 75.5 74.5 65.0 > 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/1nbti1323946805.tab") + } + } > m Conditional inference tree with 3 terminal nodes Response: le Inputs: ppt, ppp Number of observations: 38 1) ppt <= 8; criterion = 1, statistic = 13.581 2) ppt <= 3.8; criterion = 0.964, statistic = 5.579 3)* weights = 11 2) ppt > 3.8 4)* weights = 10 1) ppt > 8 5)* weights = 17 > postscript(file="/var/wessaorg/rcomp/tmp/2mdsl1323946805.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/3qahg1323946805.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 70.5 70.80000 -0.3000000 2 53.5 60.85294 -7.3529412 3 65.0 70.80000 -5.8000000 4 76.5 75.68182 0.8181818 5 70.0 70.80000 -0.8000000 6 71.0 70.80000 0.2000000 7 60.5 60.85294 -0.3529412 8 51.5 60.85294 -9.3529412 9 78.0 75.68182 2.3181818 10 76.0 75.68182 0.3181818 11 57.5 60.85294 -3.3529412 12 61.0 60.85294 0.1470588 13 64.5 60.85294 3.6470588 14 78.5 75.68182 2.8181818 15 79.0 75.68182 3.3181818 16 61.0 60.85294 0.1470588 17 70.0 60.85294 9.1470588 18 70.0 70.80000 -0.8000000 19 72.0 70.80000 1.2000000 20 64.5 60.85294 3.6470588 21 54.5 60.85294 -6.3529412 22 56.5 60.85294 -4.3529412 23 64.5 60.85294 3.6470588 24 64.5 60.85294 3.6470588 25 73.0 70.80000 2.2000000 26 72.0 70.80000 1.2000000 27 69.0 75.68182 -6.6818182 28 64.0 60.85294 3.1470588 29 78.5 75.68182 2.8181818 30 53.0 60.85294 -7.8529412 31 75.0 75.68182 -0.6818182 32 68.5 60.85294 7.6470588 33 70.0 70.80000 -0.8000000 34 70.5 75.68182 -5.1818182 35 76.0 75.68182 0.3181818 36 75.5 75.68182 -0.1818182 37 74.5 70.80000 3.7000000 38 65.0 60.85294 4.1470588 > 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/4dz4k1323946805.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/5m9ex1323946805.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/6m24y1323946805.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/wessaorg/rcomp/tmp/7l55k1323946805.tab") + } > > try(system("convert tmp/2mdsl1323946805.ps tmp/2mdsl1323946805.png",intern=TRUE)) character(0) > try(system("convert tmp/3qahg1323946805.ps tmp/3qahg1323946805.png",intern=TRUE)) character(0) > try(system("convert tmp/4dz4k1323946805.ps tmp/4dz4k1323946805.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 3.107 0.276 3.378