library(rwf)

Description

This shows the output of PART RPART functions from rwf.
Installation instructions for rwf can be found here These functions are not installed by default they are in /rwf/working_functions/OTHER/ML_TREE_RPART.R

infert_formula<-formula(factor(case)~age+parity+education+spontaneous+induced)
boston_formula<-formula(c("medv~",paste(names(MASS::Boston)[1:13],collapse="+")))
## Warning in formula.character(c("medv~", paste(names(MASS::Boston)[1:13], : Using formula(x) is deprecated when x is a character vector of length > 1.
##   Consider formula(paste(x, collapse = " ")) instead.
print(infert_formula)
## factor(case) ~ age + parity + education + spontaneous + induced
print(boston_formula)
## medv ~ crim + zn + indus + chas + nox + rm + age + dis + rad + 
##     tax + ptratio + black + lstat
# kfolding
train_test_classification<-k_fold(df=infert,
                                  model_formula=infert_formula)
## Fold Cases: 1 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 2 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 3 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 4 Train: 224 Test: 24 Total: 248 Unique Train: 224 Unique Test: 24 
## Fold Cases: 5 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 6 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 7 Train: 224 Test: 24 Total: 248 Unique Train: 224 Unique Test: 24 
## Fold Cases: 8 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 9 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25 
## Fold Cases: 10 Train: 223 Test: 25 Total: 248 Unique Train: 223 Unique Test: 25
train_test_regression<-k_fold(df=MASS::Boston,
                              model_formula=boston_formula)
## Fold Cases: 1 Train: 455 Test: 51 Total: 506 Unique Train: 455 Unique Test: 51 
## Fold Cases: 2 Train: 455 Test: 51 Total: 506 Unique Train: 455 Unique Test: 51 
## Fold Cases: 3 Train: 456 Test: 50 Total: 506 Unique Train: 456 Unique Test: 50 
## Fold Cases: 4 Train: 455 Test: 51 Total: 506 Unique Train: 455 Unique Test: 51 
## Fold Cases: 5 Train: 456 Test: 50 Total: 506 Unique Train: 456 Unique Test: 50 
## Fold Cases: 6 Train: 455 Test: 51 Total: 506 Unique Train: 455 Unique Test: 51 
## Fold Cases: 7 Train: 456 Test: 50 Total: 506 Unique Train: 456 Unique Test: 50 
## Fold Cases: 8 Train: 455 Test: 51 Total: 506 Unique Train: 455 Unique Test: 51 
## Fold Cases: 9 Train: 456 Test: 50 Total: 506 Unique Train: 456 Unique Test: 50 
## Fold Cases: 10 Train: 455 Test: 51 Total: 506 Unique Train: 455 Unique Test: 51
# srpart models
rtree_classification<-rpart::rpart(infert_formula,
                                   train_test_classification$f$train$f1,
                                   model=TRUE,x=TRUE,y=TRUE)
rtree_regression<-rpart::rpart(boston_formula,
                               train_test_regression$f$train$f1,
                               model=TRUE,x=TRUE,y=TRUE)

Rpart Classification

result<-data.frame(rtree_classification$cptable)
result$nsplit<-factor(result$nsplit+1)
minimun_size<-as.numeric(as.character(result[which.min(result[,"xerror"]),"nsplit"]))
initial_model<-rtree_classification
model<-rpart::prune(rtree_classification,
                    cp=rtree_classification$cptable[which.min(rtree_classification$cptable[,"xerror"]),"CP"])

importance<-model$variable.importance
importance<-data.frame(names=names(importance),importance=importance)
importance$names<-factor(importance$names,levels=rev(as.character(importance$names)))
plot_importance<-ggplot(importance,aes(x=names,y=importance))+
  geom_bar(stat='identity')+
  labs(title="Importance Plot",y="Relative Influence",x="Predictor")+
  theme_bw(base_size=10)+
  scale_x_discrete(limits=rev(levels(names)))+
  coord_flip()
plot_importance

rtree_classification
## n= 223 
## 
## node), split, n, loss, yval, (yprob)
##       * denotes terminal node
## 
##  1) root 223 74 0 (0.6681614 0.3318386)  
##    2) spontaneous< 0.5 128 25 0 (0.8046875 0.1953125) *
##    3) spontaneous>=0.5 95 46 1 (0.4842105 0.5157895)  
##      6) age< 30.5 52 20 0 (0.6153846 0.3846154)  
##       12) parity>=2.5 19  4 0 (0.7894737 0.2105263) *
##       13) parity< 2.5 33 16 0 (0.5151515 0.4848485)  
##         26) spontaneous< 1.5 26 10 0 (0.6153846 0.3846154)  
##           52) parity>=1.5 9  1 0 (0.8888889 0.1111111) *
##           53) parity< 1.5 17  8 1 (0.4705882 0.5294118) *
##         27) spontaneous>=1.5 7  1 1 (0.1428571 0.8571429) *
##      7) age>=30.5 43 14 1 (0.3255814 0.6744186)  
##       14) parity>=3.5 10  4 0 (0.6000000 0.4000000) *
##       15) parity< 3.5 33  8 1 (0.2424242 0.7575758) *
rpart::plotcp(model)

rpart::rsq.rpart(model)
## 
## Classification tree:
## rpart::rpart(formula = infert_formula, data = train_test_classification$f$train$f1, 
##     model = TRUE, x = TRUE, y = TRUE)
## 
## Variables actually used in tree construction:
## [1] age         spontaneous
## 
## Root node error: 74/223 = 0.33184
## 
## n= 223 
## 
##         CP nsplit rel error  xerror     xstd
## 1 0.101351      0    1.0000 1.00000 0.095022
## 2 0.033784      2    0.7973 0.83784 0.090412
## Warning in rpart::rsq.rpart(model): may not be applicable for this method

error<-data.frame(model$cptable)
error$nsplit<-factor(error$nsplit+1)
tree_size<-error[which.min(error[,"xerror"]),"nsplit"]
error<-reshape2::melt(error,id.vars="nsplit")
names(error)<-c("Split","Metric","value")
plot_prune<-ggplot(error,aes(x=Split,y=value,color=Metric))+
  geom_line(aes(group=Metric))+
  geom_point()+
  labs(title=paste("Error Plot","Suggested Size:",tree_size),y="Metric value",x="Size of Tree")+
  theme_bw(base_size=10)
plot_prune

rpart.plot::rpart.plot(model,type=1)

frame<-data.frame(model$frame)
cp<-data.frame(model$cptable)
parameters<-data.frame(parameters=unlist(model$control))
splits<-data.frame(name=row.names(model$splits),model$splits,row.names=NULL)
importance<-data.frame(importance=model$variable.importance)
ordered<-data.frame(ordered=model$ordered)
data<-data.frame(y=model$y,x=model$x,model=model$model)
call<-data.frame(call=call_to_string(model))
result<-list(frame=frame,cp=cp,parameters=parameters,splits=splits,importance=importance,ordered=ordered,call=call)

print(result)
## $frame
##           var   n  wt dev yval complexity ncompete nsurrogate    yval2.V1    yval2.V2    yval2.V3    yval2.V4    yval2.V5 yval2.nodeprob
## 1 spontaneous 223 223  74    1 0.10135135        4          3   1.0000000 149.0000000  74.0000000   0.6681614   0.3318386      1.0000000
## 2       128 128  25    1 0.00000000        0          0   1.0000000 103.0000000  25.0000000   0.8046875   0.1953125      0.5739910
## 3         age  95  95  46    2 0.10135135        4          3   2.0000000  46.0000000  49.0000000   0.4842105   0.5157895      0.4260090
## 6        52  52  20    1 0.03378378        0          0   1.0000000  32.0000000  20.0000000   0.6153846   0.3846154      0.2331839
## 7        43  43  14    2 0.02702703        0          0   2.0000000  14.0000000  29.0000000   0.3255814   0.6744186      0.1928251
## 
## $cp
##           CP nsplit rel.error    xerror       xstd
## 1 0.10135135      0 1.0000000 1.0000000 0.09502215
## 2 0.03378378      2 0.7972973 0.8378378 0.09041170
## 
## $parameters
##                parameters
## minsplit            20.00
## minbucket            7.00
## cp                   0.01
## maxcompete           4.00
## maxsurrogate         5.00
## usesurrogate         2.00
## surrogatestyle       0.00
## maxdepth            30.00
## xval                10.00
## 
## $splits
##           name count ncat      improve index        adj
## 1  spontaneous   223   -1 11.200885798   0.5 0.00000000
## 2          age   223   -1  0.323644455  25.5 0.00000000
## 3    education   223    3  0.080859787   1.0 0.00000000
## 4       parity   223   -1  0.076903366   4.5 0.00000000
## 5      induced   223    1  0.027944733   1.5 0.00000000
## 6       parity     0   -1  0.636771300   2.5 0.14736842
## 7      induced     0    1  0.618834081   0.5 0.10526316
## 8          age     0    1  0.578475336  22.0 0.01052632
## 9          age    95   -1  3.953526033  30.5 0.00000000
## 10 spontaneous    95   -1  2.845488722   1.5 0.00000000
## 11      parity    95    1  2.288605484   2.5 0.00000000
## 12     induced    95    1  0.212021250   0.5 0.00000000
## 13   education    95    3  0.007890046   2.0 0.00000000
## 14 spontaneous     0   -1  0.589473684   1.5 0.09302326
## 15      parity     0   -1  0.557894737   5.5 0.02325581
## 16   education     0    3  0.557894737   3.0 0.02325581
## 
## $importance
##              importance
## spontaneous 11.56865566
## age          4.07143009
## parity       1.74259932
## induced      1.17904061
## education    0.09194247
## 
## $ordered
##             ordered
## age           FALSE
## parity        FALSE
## education     FALSE
## spontaneous   FALSE
## induced       FALSE
## 
## $call
##                                                                                                       call
## 1 rpart::rpart(formula=infert_formula,data=train_test_classification$f$train$f1,,model=TRUE,x=TRUE,y=TRUE)

Rpart Regression

result<-data.frame(rtree_regression$cptable)
result$nsplit<-factor(result$nsplit+1)
minimun_size<-as.numeric(as.character(result[which.min(result[,"xerror"]),"nsplit"]))
initial_model<-rtree_regression
model<-rpart::prune(rtree_regression,cp=rtree_regression$cptable[which.min(rtree_regression$cptable[,"xerror"]),"CP"])

importance<-model$variable.importance
importance<-data.frame(names=names(importance),importance=importance)
importance$names<-factor(importance$names,levels=rev(as.character(importance$names)))
plot_importance<-ggplot(importance,aes(x=names,y=importance))+
  geom_bar(stat='identity')+
  labs(title="Importance Plot",y="Relative Influence",x="Predictor")+
  theme_bw(base_size=10)+
  scale_x_discrete(limits=rev(levels(names)))+
  coord_flip()
plot_importance

rtree_regression
## n= 455 
## 
## node), split, n, deviance, yval
##       * denotes terminal node
## 
##  1) root 455 38518.7300 22.79846  
##    2) rm< 6.941 385 15239.2800 20.12571  
##      4) lstat>=14.4 154  2926.3910 15.23506  
##        8) nox>=0.603 95  1238.1250 13.23158  
##         16) lstat>=19.645 46   401.5122 10.81304 *
##         17) lstat< 19.645 49   314.9498 15.50204 *
##        9) nox< 0.603 59   692.9403 18.46102 *
##      5) lstat< 14.4 231  6173.8160 23.38615  
##       10) dis>=1.5511 224  3203.1260 22.92946  
##         20) rm< 6.5255 171  1402.6330 21.63216 *
##         21) rm>=6.5255 53   584.1679 27.11509 *
##       11) dis< 1.5511 7  1429.0200 38.00000 *
##    3) rm>=6.941 70  5402.6500 37.49857  
##      6) rm< 7.437 43  1829.7580 32.26512  
##       12) lstat>=9.65 7   432.9971 23.05714 *
##       13) lstat< 9.65 36   687.8489 34.05556 *
##      7) rm>=7.437 27   519.5200 45.83333 *
rpart::plotcp(model)

rpart::rsq.rpart(model)
## 
## Regression tree:
## rpart::rpart(formula = boston_formula, data = train_test_regression$f$train$f1, 
##     model = TRUE, x = TRUE, y = TRUE)
## 
## Variables actually used in tree construction:
## [1] dis   lstat nox   rm   
## 
## Root node error: 38519/455 = 84.657
## 
## n= 455 
## 
##         CP nsplit rel error  xerror     xstd
## 1 0.464107      0   1.00000 1.00634 0.087379
## 2 0.159379      1   0.53589 0.59057 0.060113
## 3 0.079270      2   0.37651 0.38982 0.047756
## 4 0.040024      3   0.29724 0.32012 0.042891
## 5 0.031577      4   0.25722 0.32899 0.045074
## 6 0.025840      5   0.22564 0.30701 0.044923
## 7 0.018404      6   0.19980 0.27651 0.043060
## 8 0.013543      7   0.18140 0.27456 0.042179
## 9 0.010000      8   0.16786 0.25973 0.038528

error<-data.frame(model$cptable)
error$nsplit<-factor(error$nsplit+1)
tree_size<-error[which.min(error[,"xerror"]),"nsplit"]
error<-reshape2::melt(error,id.vars="nsplit")
names(error)<-c("Split","Metric","value")
plot_prune<-ggplot(error,aes(x=Split,y=value,color=Metric))+
  geom_line(aes(group=Metric))+
  geom_point()+
  labs(title=paste("Error Plot","Suggested Size:",tree_size),y="Metric value",x="Size of Tree")+
  theme_bw(base_size=10)
plot_prune

rpart.plot::rpart.plot(model,type=1)

frame<-data.frame(model$frame)
cp<-data.frame(model$cptable)
parameters<-data.frame(parameters=unlist(model$control))
splits<-data.frame(name=row.names(model$splits),model$splits,row.names=NULL)
importance<-data.frame(importance=model$variable.importance)
ordered<-data.frame(ordered=model$ordered)
data<-data.frame(y=model$y,x=model$x,model=model$model)
call<-data.frame(call=call_to_string(model))
result<-list(frame=frame,cp=cp,parameters=parameters,splits=splits,importance=importance,ordered=ordered,call=call)

print(result)
## $frame
##       var   n  wt        dev     yval  complexity ncompete nsurrogate
## 1      rm 455 455 38518.7289 22.79846 0.464106790        4          5
## 2   lstat 385 385 15239.2754 20.12571 0.159378808        4          5
## 4     nox 154 154  2926.3906 15.23506 0.025840028        4          5
## 8   lstat  95  95  1238.1253 13.23158 0.013543108        4          5
## 16   46  46   401.5122 10.81304 0.004020501        0          0
## 17   49  49   314.9498 15.50204 0.010000000        0          0
## 9    59  59   692.9403 18.46102 0.004452308        0          0
## 5     dis 231 231  6173.8157 23.38615 0.040023910        4          1
## 10     rm 224 224  3203.1255 22.92946 0.031577483        4          5
## 20  171 171  1402.6331 21.63216 0.006274422        0          0
## 21   53  53   584.1679 27.11509 0.004440859        0          0
## 11    7   7  1429.0200 38.00000 0.010000000        0          0
## 3      rm  70  70  5402.6499 37.49857 0.079269806        4          5
## 6   lstat  43  43  1829.7577 32.26512 0.018404336        4          5
## 12    7   7   432.9971 23.05714 0.010000000        0          0
## 13   36  36   687.8489 34.05556 0.005420060        0          0
## 7    27  27   519.5200 45.83333 0.004164039        0          0
## 
## $cp
##           CP nsplit rel.error    xerror       xstd
## 1 0.46410679      0 1.0000000 1.0063399 0.08737869
## 2 0.15937881      1 0.5358932 0.5905702 0.06011321
## 3 0.07926981      2 0.3765144 0.3898214 0.04775561
## 4 0.04002391      3 0.2972446 0.3201161 0.04289143
## 5 0.03157748      4 0.2572207 0.3289940 0.04507448
## 6 0.02584003      5 0.2256432 0.3070061 0.04492335
## 7 0.01840434      6 0.1998032 0.2765072 0.04305981
## 8 0.01354311      7 0.1813988 0.2745644 0.04217893
## 9 0.01000000      8 0.1678557 0.2597300 0.03852842
## 
## $parameters
##                parameters
## minsplit            20.00
## minbucket            7.00
## cp                   0.01
## maxcompete           4.00
## maxsurrogate         5.00
## usesurrogate         2.00
## surrogatestyle       0.00
## maxdepth            30.00
## xval                10.00
## 
## $splits
##       name count ncat    improve      index        adj
## 1       rm   455   -1 0.46410679   6.941000 0.00000000
## 2    lstat   455    1 0.44229881   9.725000 0.00000000
## 3    indus   455    1 0.25873730   6.660000 0.00000000
## 4  ptratio   455    1 0.23246618  19.650000 0.00000000
## 5      nox   455    1 0.21323307   0.669500 0.00000000
## 6    lstat     0    1 0.89230769   4.830000 0.30000000
## 7  ptratio     0    1 0.87252747  14.550000 0.17142857
## 8       zn     0   -1 0.85714286  87.500000 0.07142857
## 9    indus     0    1 0.85494505   1.605000 0.05714286
## 10    crim     0    1 0.84835165   0.013355 0.01428571
## 11   lstat   385    1 0.40284521  14.400000 0.00000000
## 12     nox   385    1 0.26198079   0.669500 0.00000000
## 13    crim   385    1 0.22568317   5.848030 0.00000000
## 14 ptratio   385    1 0.19631370  19.900000 0.00000000
## 15     age   385    1 0.19083383  75.750000 0.00000000
## 16     age     0    1 0.82077922  88.100000 0.55194805
## 17   indus     0    1 0.77922078  16.570000 0.44805195
## 18     nox     0    1 0.77922078   0.576500 0.44805195
## 19     dis     0   -1 0.77922078   2.239350 0.44805195
## 20     tax     0    1 0.77142857 434.500000 0.42857143
## 21     nox   154    1 0.34012036   0.603000 0.00000000
## 22    crim   154    1 0.33597887   7.464950 0.00000000
## 23     tax   154    1 0.28580671 567.500000 0.00000000
## 24     dis   154   -1 0.28185524   1.986400 0.00000000
## 25 ptratio   154    1 0.25070026  19.450000 0.00000000
## 26   indus     0    1 0.88311688  15.995000 0.69491525
## 27     tax     0    1 0.88311688 397.000000 0.69491525
## 28     dis     0   -1 0.83766234   2.790850 0.57627119
## 29    crim     0    1 0.82467532   1.400920 0.54237288
## 30 ptratio     0    1 0.75974026  19.900000 0.37288136
## 31   lstat    95    1 0.42133321  19.645000 0.00000000
## 32    crim    95    1 0.40969185  11.343000 0.00000000
## 33     dis    95   -1 0.19481532   2.003700 0.00000000
## 34     tax    95    1 0.15909053 551.500000 0.00000000
## 35      rm    95   -1 0.13723275   5.453500 0.00000000
## 36     dis     0   -1 0.82105263   1.663450 0.63043478
## 37      rm     0   -1 0.77894737   5.627500 0.54347826
## 38    crim     0    1 0.73684211  10.533600 0.45652174
## 39     age     0    1 0.69473684  98.850000 0.36956522
## 40     nox     0   -1 0.66315789   0.706500 0.30434783
## 41     dis   231    1 0.24971107   1.551100 0.00000000
## 42   lstat   231    1 0.20793193   9.545000 0.00000000
## 43      rm   231   -1 0.19764412   6.542000 0.00000000
## 44    chas   231   -1 0.09958911   0.500000 0.00000000
## 45    crim   231   -1 0.08938980   4.866945 0.00000000
## 46    crim     0   -1 0.98268398   8.053285 0.42857143
## 47      rm   224   -1 0.37973052   6.525500 0.00000000
## 48   lstat   224    1 0.31435902   7.685000 0.00000000
## 49     nox   224    1 0.14357212   0.512500 0.00000000
## 50     tax   224    1 0.14323414 222.500000 0.00000000
## 51   indus   224    1 0.14267172   4.220000 0.00000000
## 52   lstat     0    1 0.81696429   5.055000 0.22641509
## 53    crim     0    1 0.78125000   0.018370 0.07547170
## 54      zn     0   -1 0.78125000  31.500000 0.07547170
## 55    chas     0   -1 0.76785714   0.500000 0.01886792
## 56     dis     0   -1 0.76785714  10.648000 0.01886792
## 57      rm    70   -1 0.56516196   7.437000 0.00000000
## 58   lstat    70    1 0.31465530   4.680000 0.00000000
## 59 ptratio    70    1 0.17505508  19.150000 0.00000000
## 60    crim    70    1 0.12219048   1.921980 0.00000000
## 61   black    70    1 0.12167649 395.590000 0.00000000
## 62   lstat     0    1 0.78571429   4.505000 0.44444444
## 63 ptratio     0    1 0.65714286  14.750000 0.11111111
## 64   black     0    1 0.65714286 389.885000 0.11111111
## 65    crim     0    1 0.64285714   0.024530 0.07407407
## 66      zn     0   -1 0.64285714  81.250000 0.07407407
## 67   lstat    43    1 0.38743471   9.650000 0.00000000
## 68     rad    43    1 0.20118078   7.500000 0.00000000
## 69     nox    43    1 0.18477792   0.639000 0.00000000
## 70 ptratio    43    1 0.15678520  18.900000 0.00000000
## 71   indus    43    1 0.12461986  12.585000 0.00000000
## 72    crim     0    1 0.90697674   0.724605 0.42857143
## 73     nox     0    1 0.90697674   0.659000 0.42857143
## 74     rad     0    1 0.88372093  16.000000 0.28571429
## 75     tax     0    1 0.88372093 534.500000 0.28571429
## 76 ptratio     0    1 0.88372093  19.700000 0.28571429
## 
## $importance
##          importance
## rm      22430.01299
## lstat   14365.13367
## dis      5217.69336
## indus    4463.82007
## nox      4208.53329
## ptratio  3977.54282
## age      3581.23584
## tax      3525.24236
## crim     2315.87970
## zn       1594.88834
## black     339.26358
## rad       202.54618
## chas       22.94952
## 
## $ordered
##         ordered
## crim      FALSE
## zn        FALSE
## indus     FALSE
## chas      FALSE
## nox       FALSE
## rm        FALSE
## age       FALSE
## dis       FALSE
## rad       FALSE
## tax       FALSE
## ptratio   FALSE
## black     FALSE
## lstat     FALSE
## 
## $call
##                                                                                                   call
## 1 rpart::rpart(formula=boston_formula,data=train_test_regression$f$train$f1,,model=TRUE,x=TRUE,y=TRUE)