R version 2.12.0 (2010-10-15) Copyright (C) 2010 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(6282929 + ,213118 + ,1081 + ,162556 + ,4324047 + ,81767 + ,309 + ,29790 + ,4108272 + ,153198 + ,458 + ,87550 + ,-1212617 + ,-26007 + ,588 + ,84738 + ,1485329 + ,126942 + ,299 + ,54660 + ,1779876 + ,157214 + ,156 + ,42634 + ,1367203 + ,129352 + ,481 + ,40949 + ,2519076 + ,234817 + ,323 + ,42312 + ,912684 + ,60448 + ,452 + ,37704 + ,1443586 + ,47818 + ,109 + ,16275 + ,1220017 + ,245546 + ,115 + ,25830 + ,984885 + ,48020 + ,110 + ,12679 + ,1457425 + ,-1710 + ,239 + ,18014 + ,-572920 + ,32648 + ,247 + ,43556 + ,929144 + ,95350 + ,497 + ,24524 + ,1151176 + ,151352 + ,103 + ,6532 + ,790090 + ,288170 + ,109 + ,7123 + ,774497 + ,114337 + ,502 + ,20813 + ,990576 + ,37884 + ,248 + ,37597 + ,454195 + ,122844 + ,373 + ,17821 + ,876607 + ,82340 + ,119 + ,12988 + ,711969 + ,79801 + ,84 + ,22330 + ,702380 + ,165548 + ,102 + ,13326 + ,264449 + ,116384 + ,295 + ,16189 + ,450033 + ,134028 + ,105 + ,7146 + ,541063 + ,63838 + ,64 + ,15824 + ,588864 + ,74996 + ,267 + ,26088 + ,-37216 + ,31080 + ,129 + ,11326 + ,783310 + ,32168 + ,37 + ,8568 + ,467359 + ,49857 + ,361 + ,14416 + ,688779 + ,87161 + ,28 + ,3369 + ,608419 + ,106113 + ,85 + ,11819 + ,696348 + ,80570 + ,44 + ,6620 + ,597793 + ,102129 + ,49 + ,4519 + ,821730 + ,301670 + ,22 + ,2220 + ,377934 + ,102313 + ,155 + ,18562 + ,651939 + ,88577 + ,91 + ,10327 + ,697458 + ,112477 + ,81 + ,5336 + ,700368 + ,191778 + ,79 + ,2365 + ,225986 + ,79804 + ,145 + ,4069 + ,348695 + ,128294 + ,816 + ,7710 + ,373683 + ,96448 + ,61 + ,13718 + ,501709 + ,93811 + ,226 + ,4525 + ,413743 + ,117520 + ,105 + ,6869 + ,379825 + ,69159 + ,62 + ,4628 + ,336260 + ,101792 + ,24 + ,3653 + ,636765 + ,210568 + ,26 + ,1265 + ,481231 + ,136996 + ,322 + ,7489 + ,469107 + ,121920 + ,84 + ,4901 + ,211928 + ,76403 + ,33 + ,2284) + ,dim=c(4 + ,50) + ,dimnames=list(c('Wealth' + ,'Dividends' + ,'Trades' + ,'Costs ') + ,1:50)) > y <- array(NA,dim=c(4,50),dimnames=list(c('Wealth','Dividends','Trades','Costs '),1:50)) > 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 = '' > 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] "Wealth" > x[,par1] [1] 6282929 4324047 4108272 -1212617 1485329 1779876 1367203 2519076 [9] 912684 1443586 1220017 984885 1457425 -572920 929144 1151176 [17] 790090 774497 990576 454195 876607 711969 702380 264449 [25] 450033 541063 588864 -37216 783310 467359 688779 608419 [33] 696348 597793 821730 377934 651939 697458 700368 225986 [41] 348695 373683 501709 413743 379825 336260 636765 481231 [49] 469107 211928 > 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]) -1212617 -572920 -37216 211928 225986 264449 336260 348695 1 1 1 1 1 1 1 1 373683 377934 379825 413743 450033 454195 467359 469107 1 1 1 1 1 1 1 1 481231 501709 541063 588864 597793 608419 636765 651939 1 1 1 1 1 1 1 1 688779 696348 697458 700368 702380 711969 774497 783310 1 1 1 1 1 1 1 1 790090 821730 876607 912684 929144 984885 990576 1151176 1 1 1 1 1 1 1 1 1220017 1367203 1443586 1457425 1485329 1779876 2519076 4108272 1 1 1 1 1 1 1 1 4324047 6282929 1 1 > colnames(x) [1] "Wealth" "Dividends" "Trades" "Costs." > colnames(x)[par1] [1] "Wealth" > x[,par1] [1] 6282929 4324047 4108272 -1212617 1485329 1779876 1367203 2519076 [9] 912684 1443586 1220017 984885 1457425 -572920 929144 1151176 [17] 790090 774497 990576 454195 876607 711969 702380 264449 [25] 450033 541063 588864 -37216 783310 467359 688779 608419 [33] 696348 597793 821730 377934 651939 697458 700368 225986 [41] 348695 373683 501709 413743 379825 336260 636765 481231 [49] 469107 211928 > if (par2 == 'none') { + m <- ctree(as.formula(paste(colnames(x)[par1],' ~ .',sep='')),data = x) + } > > #Note: the /var/www/rcomp/createtable file can be downloaded at http://www.wessa.net/cretab > load(file="/var/www/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/rcomp/tmp/1f3ov1293188373.tab") + } + } > m Conditional inference tree with 2 terminal nodes Response: Wealth Inputs: Dividends, Trades, Costs. Number of observations: 50 1) Costs. <= 26088; criterion = 1, statistic = 20.971 2)* weights = 39 1) Costs. > 26088 3)* weights = 11 > postscript(file="/var/www/rcomp/tmp/2f3ov1293188373.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/rcomp/tmp/3f3ov1293188373.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 6282929 1998586.8 4284342.182 2 4324047 1998586.8 2325460.182 3 4108272 1998586.8 2109685.182 4 -1212617 1998586.8 -3211203.818 5 1485329 1998586.8 -513257.818 6 1779876 1998586.8 -218710.818 7 1367203 1998586.8 -631383.818 8 2519076 1998586.8 520489.182 9 912684 1998586.8 -1085902.818 10 1443586 635218.8 808367.205 11 1220017 635218.8 584798.205 12 984885 635218.8 349666.205 13 1457425 635218.8 822206.205 14 -572920 1998586.8 -2571506.818 15 929144 635218.8 293925.205 16 1151176 635218.8 515957.205 17 790090 635218.8 154871.205 18 774497 635218.8 139278.205 19 990576 1998586.8 -1008010.818 20 454195 635218.8 -181023.795 21 876607 635218.8 241388.205 22 711969 635218.8 76750.205 23 702380 635218.8 67161.205 24 264449 635218.8 -370769.795 25 450033 635218.8 -185185.795 26 541063 635218.8 -94155.795 27 588864 635218.8 -46354.795 28 -37216 635218.8 -672434.795 29 783310 635218.8 148091.205 30 467359 635218.8 -167859.795 31 688779 635218.8 53560.205 32 608419 635218.8 -26799.795 33 696348 635218.8 61129.205 34 597793 635218.8 -37425.795 35 821730 635218.8 186511.205 36 377934 635218.8 -257284.795 37 651939 635218.8 16720.205 38 697458 635218.8 62239.205 39 700368 635218.8 65149.205 40 225986 635218.8 -409232.795 41 348695 635218.8 -286523.795 42 373683 635218.8 -261535.795 43 501709 635218.8 -133509.795 44 413743 635218.8 -221475.795 45 379825 635218.8 -255393.795 46 336260 635218.8 -298958.795 47 636765 635218.8 1546.205 48 481231 635218.8 -153987.795 49 469107 635218.8 -166111.795 50 211928 635218.8 -423290.795 > if (par2 != 'none') { + print(cbind(as.factor(x[,par1]),predict(m))) + myt <- table(as.factor(x[,par1]),predict(m)) + print(myt) + } > postscript(file="/var/www/rcomp/tmp/4qc5y1293188373.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/rcomp/tmp/5m4lp1293188373.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/rcomp/tmp/6fv2a1293188373.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/rcomp/tmp/7ie0g1293188373.tab") + } > > try(system("convert tmp/2f3ov1293188373.ps tmp/2f3ov1293188373.png",intern=TRUE)) character(0) > try(system("convert tmp/3f3ov1293188373.ps tmp/3f3ov1293188373.png",intern=TRUE)) character(0) > try(system("convert tmp/4qc5y1293188373.ps tmp/4qc5y1293188373.png",intern=TRUE)) character(0) > > > proc.time() user system elapsed 1.990 0.660 2.631