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(631.923 + ,9.911 + ,58608 + ,654.294 + ,8.915 + ,46865 + ,671.833 + ,9.452 + ,51378 + ,586.840 + ,9.112 + ,46235 + ,600.969 + ,8.472 + ,47206 + ,625.568 + ,8.230 + ,45382 + ,558.110 + ,8.384 + ,41227 + ,630.577 + ,8.625 + ,33795 + ,628.654 + ,8.221 + ,31295 + ,603.184 + ,8.649 + ,42625 + ,656.255 + ,8.625 + ,33625 + ,600.730 + ,10.443 + ,21538 + ,670.326 + ,10.357 + ,56421 + ,678.423 + ,8.586 + ,53152 + ,641.502 + ,8.892 + ,53536 + ,625.311 + ,8.329 + ,52408 + ,628.177 + ,8.101 + ,41454 + ,589.767 + ,7.922 + ,38271 + ,582.471 + ,8.120 + ,35306 + ,636.248 + ,7.838 + ,26414 + ,599.885 + ,7.735 + ,31917 + ,621.694 + ,8.406 + ,38030 + ,637.406 + ,8.209 + ,27534 + ,595.994 + ,9.451 + ,18387 + ,696.308 + ,10.041 + ,50556 + ,674.201 + ,9.411 + ,43901 + ,648.861 + ,10.405 + ,48572 + ,649.605 + ,8.467 + ,43899 + ,672.392 + ,8.464 + ,37532 + ,598.396 + ,8.102 + ,40357 + ,613.177 + ,7.627 + ,35489 + ,638.104 + ,7.513 + ,29027 + ,615.632 + ,7.510 + ,34485 + ,634.465 + ,8.291 + ,42598 + ,638.686 + ,8.064 + ,30306 + ,604.243 + ,9.383 + ,26451 + ,706.669 + ,9.706 + ,47460 + ,677.185 + ,8.579 + ,50104 + ,644.328 + ,9.474 + ,61465 + ,664.825 + ,8.318 + ,53726 + ,605.707 + ,8.213 + ,39477 + ,600.136 + ,8.059 + ,43895 + ,612.166 + ,9.111 + ,31481 + ,599.659 + ,7.708 + ,29896 + ,634.210 + ,7.680 + ,33842 + ,618.234 + ,8.014 + ,39120 + ,613.576 + ,8.007 + ,33702 + ,627.200 + ,8.718 + ,25094 + ,668.973 + ,9.486 + ,51442 + ,651.479 + ,9.113 + ,45594 + ,619.661 + ,9.025 + ,52518 + ,644.260 + ,8.476 + ,48564 + ,579.936 + ,7.952 + ,41745 + ,601.752 + ,7.759 + ,49585 + ,595.376 + ,7.835 + ,32747 + ,588.902 + ,7.600 + ,33379 + ,634.341 + ,7.651 + ,35645 + ,594.305 + ,8.319 + ,37034 + ,606.200 + ,8.812 + ,35681 + ,610.926 + ,8.630 + ,20972) + ,dim=c(3 + ,60) + ,dimnames=list(c('Werklozen' + ,'Overlijdens' + ,'Inschrijvingen') + ,1:60)) > y <- array(NA,dim=c(3,60),dimnames=list(c('Werklozen','Overlijdens','Inschrijvingen'),1:60)) > 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 = '4' > par2 = 'none' > par1 = '2' > 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] "Overlijdens" > x[,par1] [1] 9.911 8.915 9.452 9.112 8.472 8.230 8.384 8.625 8.221 8.649 [11] 8.625 10.443 10.357 8.586 8.892 8.329 8.101 7.922 8.120 7.838 [21] 7.735 8.406 8.209 9.451 10.041 9.411 10.405 8.467 8.464 8.102 [31] 7.627 7.513 7.510 8.291 8.064 9.383 9.706 8.579 9.474 8.318 [41] 8.213 8.059 9.111 7.708 7.680 8.014 8.007 8.718 9.486 9.113 [51] 9.025 8.476 7.952 7.759 7.835 7.600 7.651 8.319 8.812 8.630 > 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]) 7.51 7.513 7.6 7.627 7.651 7.68 7.708 7.735 7.759 7.835 7.838 1 1 1 1 1 1 1 1 1 1 1 7.922 7.952 8.007 8.014 8.059 8.064 8.101 8.102 8.12 8.209 8.213 1 1 1 1 1 1 1 1 1 1 1 8.221 8.23 8.291 8.318 8.319 8.329 8.384 8.406 8.464 8.467 8.472 1 1 1 1 1 1 1 1 1 1 1 8.476 8.579 8.586 8.625 8.63 8.649 8.718 8.812 8.892 8.915 9.025 1 1 1 2 1 1 1 1 1 1 1 9.111 9.112 9.113 9.383 9.411 9.451 9.452 9.474 9.486 9.706 9.911 1 1 1 1 1 1 1 1 1 1 1 10.041 10.357 10.405 10.443 1 1 1 1 > colnames(x) [1] "Werklozen" "Overlijdens" "Inschrijvingen" > colnames(x)[par1] [1] "Overlijdens" > x[,par1] [1] 9.911 8.915 9.452 9.112 8.472 8.230 8.384 8.625 8.221 8.649 [11] 8.625 10.443 10.357 8.586 8.892 8.329 8.101 7.922 8.120 7.838 [21] 7.735 8.406 8.209 9.451 10.041 9.411 10.405 8.467 8.464 8.102 [31] 7.627 7.513 7.510 8.291 8.064 9.383 9.706 8.579 9.474 8.318 [41] 8.213 8.059 9.111 7.708 7.680 8.014 8.007 8.718 9.486 9.113 [51] 9.025 8.476 7.952 7.759 7.835 7.600 7.651 8.319 8.812 8.630 > 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/1s50l1324636728.tab") + } + } > m Conditional inference tree with 2 terminal nodes Response: Overlijdens Inputs: Werklozen, Inschrijvingen Number of observations: 60 1) Werklozen <= 644.26; criterion = 0.997, statistic = 10.342 2)* weights = 44 1) Werklozen > 644.26 3)* weights = 16 > postscript(file="/var/wessaorg/rcomp/tmp/2d8c01324636728.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/3gs9l1324636728.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 9.911 8.343386 1.56761364 2 8.915 9.212438 -0.29743750 3 9.452 9.212438 0.23956250 4 9.112 8.343386 0.76861364 5 8.472 8.343386 0.12861364 6 8.230 8.343386 -0.11338636 7 8.384 8.343386 0.04061364 8 8.625 8.343386 0.28161364 9 8.221 8.343386 -0.12238636 10 8.649 8.343386 0.30561364 11 8.625 9.212438 -0.58743750 12 10.443 8.343386 2.09961364 13 10.357 9.212438 1.14456250 14 8.586 9.212438 -0.62643750 15 8.892 8.343386 0.54861364 16 8.329 8.343386 -0.01438636 17 8.101 8.343386 -0.24238636 18 7.922 8.343386 -0.42138636 19 8.120 8.343386 -0.22338636 20 7.838 8.343386 -0.50538636 21 7.735 8.343386 -0.60838636 22 8.406 8.343386 0.06261364 23 8.209 8.343386 -0.13438636 24 9.451 8.343386 1.10761364 25 10.041 9.212438 0.82856250 26 9.411 9.212438 0.19856250 27 10.405 9.212438 1.19256250 28 8.467 9.212438 -0.74543750 29 8.464 9.212438 -0.74843750 30 8.102 8.343386 -0.24138636 31 7.627 8.343386 -0.71638636 32 7.513 8.343386 -0.83038636 33 7.510 8.343386 -0.83338636 34 8.291 8.343386 -0.05238636 35 8.064 8.343386 -0.27938636 36 9.383 8.343386 1.03961364 37 9.706 9.212438 0.49356250 38 8.579 9.212438 -0.63343750 39 9.474 9.212438 0.26156250 40 8.318 9.212438 -0.89443750 41 8.213 8.343386 -0.13038636 42 8.059 8.343386 -0.28438636 43 9.111 8.343386 0.76761364 44 7.708 8.343386 -0.63538636 45 7.680 8.343386 -0.66338636 46 8.014 8.343386 -0.32938636 47 8.007 8.343386 -0.33638636 48 8.718 8.343386 0.37461364 49 9.486 9.212438 0.27356250 50 9.113 9.212438 -0.09943750 51 9.025 8.343386 0.68161364 52 8.476 8.343386 0.13261364 53 7.952 8.343386 -0.39138636 54 7.759 8.343386 -0.58438636 55 7.835 8.343386 -0.50838636 56 7.600 8.343386 -0.74338636 57 7.651 8.343386 -0.69238636 58 8.319 8.343386 -0.02438636 59 8.812 8.343386 0.46861364 60 8.630 8.343386 0.28661364 > 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/4v0fv1324636728.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/5q4kb1324636728.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/6ptcm1324636728.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/7ynno1324636728.tab") + } > > try(system("convert tmp/2d8c01324636728.ps tmp/2d8c01324636728.png",intern=TRUE)) character(0) > try(system("convert tmp/3gs9l1324636728.ps tmp/3gs9l1324636728.png",intern=TRUE)) character(0) > try(system("convert tmp/4v0fv1324636728.ps tmp/4v0fv1324636728.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 2.444 0.225 2.680