> # test.rpart.plot.R: this does a regression test of rpart.plot and rpart.plot
> # Stephen Milborrow Nov 2010 Gardens, Cape Town
> 
> source("test.prolog.R")
Loading required package: rpart
> 
> print(sessionInfo())
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rpart.plot_3.0.8 rpart_4.1-15    

loaded via a namespace (and not attached):
[1] compiler_3.6.1
> print(citation("rpart.plot"))
Warning in citation("rpart.plot") :
  no date field in DESCRIPTION file of package 'rpart.plot'

To cite package 'rpart.plot' in publications use:

  Stephen Milborrow (2019). rpart.plot: Plot 'rpart' Models: An
  Enhanced Version of 'plot.rpart'. R package version 3.0.8.
  http://www.milbo.org/rpart-plot

A BibTeX entry for LaTeX users is

  @Manual{,
    title = {rpart.plot: Plot 'rpart' Models: An Enhanced Version of 'plot.rpart'},
    author = {Stephen Milborrow},
    year = {2019},
    note = {R package version 3.0.8},
    url = {http://www.milbo.org/rpart-plot},
  }

ATTENTION: This citation information has been auto-generated from the
package DESCRIPTION file and may need manual editing, see
'help("citation")'.

> 
> example(rpart.plot)

rprt.p> old.par <- par(mfrow=c(2,2))            # put 4 figures on one page

rprt.p> data(ptitanic)

rprt.p> #---------------------------------------------------------------------------
rprt.p> 
rprt.p> binary.model <- rpart(survived ~ ., data = ptitanic, cp = .02)

rprt.p>                                         # cp = .02 for small demo tree
rprt.p> 
rprt.p> rpart.plot(binary.model,
rprt.p+            main = "titanic survived\n(binary response)")

rprt.p> rpart.plot(binary.model, type = 3, clip.right.labs = FALSE,
rprt.p+            branch = .4,
rprt.p+            box.palette = "Grays",       # override default GnBu palette
rprt.p+            main = "type = 3, clip.right.labs = FALSE, ...\n")

rprt.p> #---------------------------------------------------------------------------
rprt.p> 
rprt.p> anova.model <- rpart(Mileage ~ ., data = cu.summary)

rprt.p> rpart.plot(anova.model,
rprt.p+            shadow.col = "gray",         # add shadows just for kicks
rprt.p+            main = "miles per gallon\n(continuous response)\n")

rprt.p> #---------------------------------------------------------------------------
rprt.p> 
rprt.p> multi.class.model <- rpart(Reliability ~ ., data = cu.summary)

rprt.p> rpart.plot(multi.class.model,
rprt.p+            main = "vehicle reliability\n(multi class response)")

rprt.p> par(old.par)
> example(rpart.plot.version1)

rpr..1> data(ptitanic)

rpr..1> tree <- rpart(survived ~ ., data = ptitanic, cp = .02)

rpr..1>                                 # cp = .02 because want small tree for demo
rpr..1> 
rpr..1> old.par <- par(mfrow = c(2,2))

rpr..1>                                 # put 4 figures on one page
rpr..1> 
rpr..1> rpart.plot.version1(tree, main = "default rpart.plot.version1\n(type = 0, extra = 0)")

rpr..1> rpart.plot.version1(tree, main = "type = 4, extra = 6",
rpr..1+                     type = 4, extra = 6,
rpr..1+                     faclen = 0) # faclen = 0 to print full factor names

rpr..1> rpart.plot.version1(tree, main = "extra = 106,  under = TRUE",
rpr..1+                     extra = 106, under = TRUE, faclen = 0)

rpr..1> # compare to the plotting functions in the rpart package
rpr..1> plot(tree, uniform = TRUE, compress = TRUE, branch = .2)

rpr..1> text(tree, use.n = TRUE, cex = .8, xpd = NA) # cex is a guess, depends on your window size

rpr..1> title("compare to the plotting functions\nin the rpart package", cex.sub = .8)

rpr..1> par(old.par)
> example(prp)

prp> data(ptitanic)

prp> tree <- rpart(survived ~ ., data = ptitanic, cp = .02)

prp>                            # cp = .02 because want small tree for demo
prp> 
prp> old.par <- par(mfrow = c(2,2))

prp>                            # put 4 figures on one page
prp> 
prp> prp(tree, main = "default prp\n(type =  0, extra = 0)")

prp> prp(tree, main = "type = 4,  extra = 6\nbox.palette = \"auto\"",
prp+     type = 4, extra = 6,   # label all nodes, show prob of second class
prp+     box.palette = "auto",  # auto color the nodes based on the model type
prp+     faclen = 0)            # faclen = 0 to print full factor names

prp> cols <- ifelse(tree$frame$yval == 1, "darkred", "green4")

prp>                            # green if survived
prp> 
prp> prp(tree, main = "assorted arguments",
prp+     extra = 106,           # display prob of survival and percent of obs
prp+     nn = TRUE,             # display the node numbers
prp+     fallen.leaves = TRUE,  # put the leaves on the bottom of the page
prp+     shadow.col = "gray",   # shadows under the leaves
prp+     branch.lty = 3,        # draw branches using dotted lines
prp+     branch = .5,           # change angle of branch lines
prp+     faclen = 0,            # faclen = 0 to print full factor names
prp+     trace = 1,             # print the auto calculated cex, xlim, ylim
prp+     split.cex = 1.2,       # make the split text larger than the node text
prp+     split.prefix = "is ",  # put "is " before split text
prp+     split.suffix = "?",    # put "?" after split text
prp+     col = cols, border.col = cols,   # green if survived
prp+     split.box.col = "lightgray",   # lightgray split boxes (default is white)
prp+     split.border.col = "darkgray", # darkgray border on split boxes
prp+     split.round = .5)              # round the split box corners a tad
cex 1   xlim c(-0.2, 1.2)   ylim c(0, 1)

prp> # compare to the plotting functions in the rpart package
prp> plot(tree, uniform = TRUE, compress = TRUE, branch = .2)

prp> text(tree, use.n = TRUE, cex = .8, xpd = NA) # cex is a guess, depends on your window size

prp> title("compare to the plotting functions\nin the rpart package", cex.sub = .8)

prp> par(old.par)
> 
> data(ptitanic)
> example(ptitanic)

ptitnc> data(ptitanic)

ptitnc> summary(ptitanic)
 pclass        survived       sex           age              sibsp       
 1st:323   died    :809   female:466   Min.   : 0.1667   Min.   :0.0000  
 2nd:277   survived:500   male  :843   1st Qu.:21.0000   1st Qu.:0.0000  
 3rd:709                               Median :28.0000   Median :0.0000  
                                       Mean   :29.8811   Mean   :0.4989  
                                       3rd Qu.:39.0000   3rd Qu.:1.0000  
                                       Max.   :80.0000   Max.   :8.0000  
                                       NA's   :263                       
     parch      
 Min.   :0.000  
 1st Qu.:0.000  
 Median :0.000  
 Mean   :0.385  
 3rd Qu.:0.000  
 Max.   :9.000  
                

ptitnc> # survival rate was greater for females
ptitnc> rpart.rules(rpart(survived ~ sex, data = ptitanic))
 survived                   
     0.19 when sex is   male
     0.73 when sex is female

ptitnc> # survival rate was greater for higher classes
ptitnc> rpart.rules(rpart(survived ~ pclass, data = ptitanic))
 survived                   
     0.26 when pclass is 3rd
     0.43 when pclass is 2nd
     0.62 when pclass is 1st

ptitnc> # survival rate was greater for children
ptitnc> rpart.rules(rpart(survived ~ age, data = ptitanic))
 survived                
     0.39 when age >= 8.5
     0.64 when age <  8.5

ptitnc> # main indicator of missing data is 3rd class esp. with many children
ptitnc> obs.with.nas <- rowSums(is.na(ptitanic)) > 0

ptitnc> rpart.rules(rpart(obs.with.nas ~ ., data = ptitanic, method = "class"))
 obs.with.nas                                       
         0.09 when pclass is 1st or 2nd             
         0.29 when pclass is        3rd & sibsp <  7
         0.89 when pclass is        3rd & sibsp >= 7
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
Loading required package: TeachingDemos
> data(ozone1)
> ititanic <- ptitanic
> ititanic$survived <- as.integer(ititanic$survived == "survived")
> 
> example(rpart.predict)

rprt.p> data(ptitanic)

rprt.p> model <- rpart(survived ~ ., data = ptitanic, cp = .02)

rprt.p> head(rpart.predict(model, rules=TRUE))
       died  survived                                                
1 0.2725322 0.7274678 because sex is female                          
2 0.1111111 0.8888889 because sex is   male & age <  9.5 & sibsp <  3
3 0.2725322 0.7274678 because sex is female                          
4 0.8291457 0.1708543 because sex is   male & age >= 9.5             
5 0.2725322 0.7274678 because sex is female                          
6 0.8291457 0.1708543 because sex is   male & age >= 9.5             
> 
> # test format0 and formatf
> 
> x <- c(1.2345, 1.6, 1.23456, 12.345, 124.56,
+        123, 123.456789012345, 1234, 9999, 12345, 123456, 1.234e6, 1.234e7,
+        .123, .0123,
+        1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6,
+        1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6,
+        .999, .999e1, .999e2, .999e3, .999e4, .999e5, .999e6,
+        .999e-1, .999e-2, .999e-3, .999e-4, .999e-5, .999e-6)
> 
> x1 <- c(x, 1.2345e300, 1.2345e-300)
> 
> x  <- c(NA, Inf, -Inf, 0, x, -x)
> x1 <- c(NA, Inf, -Inf, 0, x1, -x1)
> 
> options(digits=7)
> 
> cat("\n--- format0 ---\n")

--- format0 ---
> s4  <- rpart.plot:::format0(x1, digits=4)
> s2  <- rpart.plot:::format0(x1, digits=2)
> s1  <- rpart.plot:::format0(x1, digits=1)
> s0  <- rpart.plot:::format0(x1, digits=0)
> sm3 <- rpart.plot:::format0(x1, digits=-3)
> tab <- data.frame("digits=4"=s4, "digits=2"=s2, "digits=1"=s1, "digits=0"=s0, "digits=-3"=sm3, check.names=F)
> row.names(tab) <- format(x1, digits=15)
> print(tab)
                          digits=4  digits=2 digits=1     digits=0  digits=-3
                    NA          NA        NA       NA           NA         NA
                   Inf         Inf       Inf      Inf          Inf        Inf
                  -Inf        -Inf      -Inf     -Inf         -Inf       -Inf
  0.00000000000000e+00           0         0        0            0          0
  1.23450000000000e+00       1.234       1.2        1       1.2345       1.23
  1.60000000000000e+00         1.6       1.6        2          1.6        1.6
  1.23456000000000e+00       1.235       1.2        1      1.23456       1.23
  1.23450000000000e+01       12.35        12       12       12.345       12.3
  1.24560000000000e+02       124.6       125      125       124.56        125
  1.23000000000000e+02         123       123      123          123        123
  1.23456789012345e+02       123.5       123      123     123.4568        123
  1.23400000000000e+03        1234      1234     1234         1234       1234
  9.99900000000000e+03        9999      9999     9999         9999       9999
  1.23450000000000e+04    12.35e+3     12e+3    12e+3    12.345e+3      12345
  1.23456000000000e+05    123.5e+3    123e+3   123e+3   123.456e+3     123456
  1.23400000000000e+06    1.234e+6    1.2e+6     1e+6     1.234e+6    1234000
  1.23400000000000e+07    12.34e+6     12e+6    12e+6     12.34e+6   12340000
  1.23000000000000e-01       0.123      0.12      0.1        0.123      0.123
  1.23000000000000e-02      0.0123     0.012     0.01       0.0123     0.0123
  1.00000000000000e+00           1         1        1            1          1
  1.00000000000000e+01          10        10       10           10         10
  1.00000000000000e+02         100       100      100          100        100
  1.00000000000000e+03        1000      1000     1000         1000       1000
  1.00000000000000e+04       10e+3     10e+3    10e+3        10e+3      10000
  1.00000000000000e+05      100e+3    100e+3   100e+3       100e+3      1e+05
  1.00000000000000e+06     1000e+3   1000e+3  1000e+3      1000e+3      1e+06
  1.00000000000000e-01         0.1       0.1      0.1          0.1        0.1
  1.00000000000000e-02        0.01      0.01     0.01         0.01       0.01
  1.00000000000000e-03       0.001     0.001    0.001        0.001      0.001
  1.00000000000000e-04      100e-6    100e-6   100e-6       100e-6      1e-04
  1.00000000000000e-05       10e-6     10e-6    10e-6        10e-6      1e-05
  1.00000000000000e-06        1e-6      1e-6     1e-6         1e-6      1e-06
  9.99000000000000e-01       0.999         1        1        0.999      0.999
  9.99000000000000e+00        9.99        10       10         9.99       9.99
  9.99000000000000e+01        99.9       100      100         99.9       99.9
  9.99000000000000e+02         999       999      999          999        999
  9.99000000000000e+03        9990      9990     9990         9990       9990
  9.99000000000000e+04     99.9e+3    100e+3   100e+3      99.9e+3      99900
  9.99000000000000e+05      999e+3    999e+3   999e+3       999e+3     999000
  9.99000000000000e-02      0.0999       0.1      0.1       0.0999     0.0999
  9.99000000000000e-03     0.00999      0.01     0.01      0.00999    0.00999
  9.99000000000000e-04      999e-6    999e-6   999e-6       999e-6   0.000999
  9.99000000000000e-05     99.9e-6    100e-6   100e-6      99.9e-6   9.99e-05
  9.99000000000000e-06     9.99e-6     10e-6    10e-6      9.99e-6   9.99e-06
  9.99000000000000e-07      999e-9    999e-9   999e-9       999e-9   9.99e-07
 1.23450000000000e+300  1.234e+300  1.2e+300   1e+300  1.2345e+300  1.23e+300
 1.23450000000000e-300  1.234e-300  1.2e-300   1e-300  1.2345e-300  1.23e-300
 -1.23450000000000e+00      -1.234      -1.2       -1      -1.2345      -1.23
 -1.60000000000000e+00        -1.6      -1.6       -2         -1.6       -1.6
 -1.23456000000000e+00      -1.235      -1.2       -1     -1.23456      -1.23
 -1.23450000000000e+01      -12.35       -12      -12      -12.345      -12.3
 -1.24560000000000e+02      -124.6      -125     -125      -124.56       -125
 -1.23000000000000e+02        -123      -123     -123         -123       -123
 -1.23456789012345e+02      -123.5      -123     -123    -123.4568       -123
 -1.23400000000000e+03       -1234     -1234    -1234        -1234      -1234
 -9.99900000000000e+03       -9999     -9999    -9999        -9999      -9999
 -1.23450000000000e+04   -12.35e+3    -12e+3   -12e+3   -12.345e+3     -12345
 -1.23456000000000e+05   -123.5e+3   -123e+3  -123e+3  -123.456e+3    -123456
 -1.23400000000000e+06   -1.234e+6   -1.2e+6    -1e+6    -1.234e+6   -1234000
 -1.23400000000000e+07   -12.34e+6    -12e+6   -12e+6    -12.34e+6  -12340000
 -1.23000000000000e-01      -0.123     -0.12     -0.1       -0.123     -0.123
 -1.23000000000000e-02     -0.0123    -0.012    -0.01      -0.0123    -0.0123
 -1.00000000000000e+00          -1        -1       -1           -1         -1
 -1.00000000000000e+01         -10       -10      -10          -10        -10
 -1.00000000000000e+02        -100      -100     -100         -100       -100
 -1.00000000000000e+03       -1000     -1000    -1000        -1000      -1000
 -1.00000000000000e+04      -10e+3    -10e+3   -10e+3       -10e+3     -10000
 -1.00000000000000e+05     -100e+3   -100e+3  -100e+3      -100e+3     -1e+05
 -1.00000000000000e+06    -1000e+3  -1000e+3 -1000e+3     -1000e+3     -1e+06
 -1.00000000000000e-01        -0.1      -0.1     -0.1         -0.1       -0.1
 -1.00000000000000e-02       -0.01     -0.01    -0.01        -0.01      -0.01
 -1.00000000000000e-03      -0.001    -0.001   -0.001       -0.001     -0.001
 -1.00000000000000e-04     -100e-6   -100e-6  -100e-6      -100e-6     -1e-04
 -1.00000000000000e-05      -10e-6    -10e-6   -10e-6       -10e-6     -1e-05
 -1.00000000000000e-06       -1e-6     -1e-6    -1e-6        -1e-6     -1e-06
 -9.99000000000000e-01      -0.999        -1       -1       -0.999     -0.999
 -9.99000000000000e+00       -9.99       -10      -10        -9.99      -9.99
 -9.99000000000000e+01       -99.9      -100     -100        -99.9      -99.9
 -9.99000000000000e+02        -999      -999     -999         -999       -999
 -9.99000000000000e+03       -9990     -9990    -9990        -9990      -9990
 -9.99000000000000e+04    -99.9e+3   -100e+3  -100e+3     -99.9e+3     -99900
 -9.99000000000000e+05     -999e+3   -999e+3  -999e+3      -999e+3    -999000
 -9.99000000000000e-02     -0.0999      -0.1     -0.1      -0.0999    -0.0999
 -9.99000000000000e-03    -0.00999     -0.01    -0.01     -0.00999   -0.00999
 -9.99000000000000e-04     -999e-6   -999e-6  -999e-6      -999e-6  -0.000999
 -9.99000000000000e-05    -99.9e-6   -100e-6  -100e-6     -99.9e-6  -9.99e-05
 -9.99000000000000e-06    -9.99e-6    -10e-6   -10e-6     -9.99e-6  -9.99e-06
 -9.99000000000000e-07     -999e-9   -999e-9  -999e-9      -999e-9  -9.99e-07
-1.23450000000000e+300 -1.234e+300 -1.2e+300  -1e+300 -1.2345e+300 -1.23e+300
-1.23450000000000e-300 -1.234e-300 -1.2e-300  -1e-300 -1.2345e-300 -1.23e-300
> 
> cat("\n--- formatf ---\n")

--- formatf ---
> s4  <- rpart.plot:::formatf(x, digits=4)
> s2  <- rpart.plot:::formatf(x, digits=2)
> s1  <- rpart.plot:::formatf(x, digits=1)
> s0  <- rpart.plot:::formatf(x, digits=0)
> tab <- data.frame("digits=4"=s4, "digits=2"=s2, "digits=1"=s1, "digits=0"=s0,
+                   check.names=F)
> row.names(tab) <- format(x, digits=15)
> print(tab)
                            digits=4     digits=2    digits=1  digits=0
                   NA             NA           NA          NA        NA
                  Inf            Inf          Inf         Inf       Inf
                 -Inf           -Inf         -Inf        -Inf      -Inf
 0.00000000000000e+00         0.0000         0.00         0.0         0
 1.23450000000000e+00         1.2345         1.23         1.2         1
 1.60000000000000e+00         1.6000         1.60         1.6         2
 1.23456000000000e+00         1.2346         1.23         1.2         1
 1.23450000000000e+01        12.3450        12.35        12.3        12
 1.24560000000000e+02       124.5600       124.56       124.6       125
 1.23000000000000e+02       123.0000       123.00       123.0       123
 1.23456789012345e+02       123.4568       123.46       123.5       123
 1.23400000000000e+03      1234.0000      1234.00      1234.0      1234
 9.99900000000000e+03      9999.0000      9999.00      9999.0      9999
 1.23450000000000e+04     12345.0000     12345.00     12345.0     12345
 1.23456000000000e+05    123456.0000    123456.00    123456.0    123456
 1.23400000000000e+06   1234000.0000   1234000.00   1234000.0   1234000
 1.23400000000000e+07  12340000.0000  12340000.00  12340000.0  12340000
 1.23000000000000e-01         0.1230         0.12         0.1         0
 1.23000000000000e-02         0.0123         0.01         0.0         0
 1.00000000000000e+00         1.0000         1.00         1.0         1
 1.00000000000000e+01        10.0000        10.00        10.0        10
 1.00000000000000e+02       100.0000       100.00       100.0       100
 1.00000000000000e+03      1000.0000      1000.00      1000.0      1000
 1.00000000000000e+04     10000.0000     10000.00     10000.0     10000
 1.00000000000000e+05    100000.0000    100000.00    100000.0    100000
 1.00000000000000e+06   1000000.0000   1000000.00   1000000.0   1000000
 1.00000000000000e-01         0.1000         0.10         0.1         0
 1.00000000000000e-02         0.0100         0.01         0.0         0
 1.00000000000000e-03         0.0010         0.00         0.0         0
 1.00000000000000e-04         0.0001         0.00         0.0         0
 1.00000000000000e-05         0.0000         0.00         0.0         0
 1.00000000000000e-06         0.0000         0.00         0.0         0
 9.99000000000000e-01         0.9990         1.00         1.0         1
 9.99000000000000e+00         9.9900         9.99        10.0        10
 9.99000000000000e+01        99.9000        99.90        99.9       100
 9.99000000000000e+02       999.0000       999.00       999.0       999
 9.99000000000000e+03      9990.0000      9990.00      9990.0      9990
 9.99000000000000e+04     99900.0000     99900.00     99900.0     99900
 9.99000000000000e+05    999000.0000    999000.00    999000.0    999000
 9.99000000000000e-02         0.0999         0.10         0.1         0
 9.99000000000000e-03         0.0100         0.01         0.0         0
 9.99000000000000e-04         0.0010         0.00         0.0         0
 9.99000000000000e-05         0.0001         0.00         0.0         0
 9.99000000000000e-06         0.0000         0.00         0.0         0
 9.99000000000000e-07         0.0000         0.00         0.0         0
-1.23450000000000e+00        -1.2345        -1.23        -1.2        -1
-1.60000000000000e+00        -1.6000        -1.60        -1.6        -2
-1.23456000000000e+00        -1.2346        -1.23        -1.2        -1
-1.23450000000000e+01       -12.3450       -12.35       -12.3       -12
-1.24560000000000e+02      -124.5600      -124.56      -124.6      -125
-1.23000000000000e+02      -123.0000      -123.00      -123.0      -123
-1.23456789012345e+02      -123.4568      -123.46      -123.5      -123
-1.23400000000000e+03     -1234.0000     -1234.00     -1234.0     -1234
-9.99900000000000e+03     -9999.0000     -9999.00     -9999.0     -9999
-1.23450000000000e+04    -12345.0000    -12345.00    -12345.0    -12345
-1.23456000000000e+05   -123456.0000   -123456.00   -123456.0   -123456
-1.23400000000000e+06  -1234000.0000  -1234000.00  -1234000.0  -1234000
-1.23400000000000e+07 -12340000.0000 -12340000.00 -12340000.0 -12340000
-1.23000000000000e-01        -0.1230        -0.12        -0.1        -0
-1.23000000000000e-02        -0.0123        -0.01        -0.0        -0
-1.00000000000000e+00        -1.0000        -1.00        -1.0        -1
-1.00000000000000e+01       -10.0000       -10.00       -10.0       -10
-1.00000000000000e+02      -100.0000      -100.00      -100.0      -100
-1.00000000000000e+03     -1000.0000     -1000.00     -1000.0     -1000
-1.00000000000000e+04    -10000.0000    -10000.00    -10000.0    -10000
-1.00000000000000e+05   -100000.0000   -100000.00   -100000.0   -100000
-1.00000000000000e+06  -1000000.0000  -1000000.00  -1000000.0  -1000000
-1.00000000000000e-01        -0.1000        -0.10        -0.1        -0
-1.00000000000000e-02        -0.0100        -0.01        -0.0        -0
-1.00000000000000e-03        -0.0010        -0.00        -0.0        -0
-1.00000000000000e-04        -0.0001        -0.00        -0.0        -0
-1.00000000000000e-05        -0.0000        -0.00        -0.0        -0
-1.00000000000000e-06        -0.0000        -0.00        -0.0        -0
-9.99000000000000e-01        -0.9990        -1.00        -1.0        -1
-9.99000000000000e+00        -9.9900        -9.99       -10.0       -10
-9.99000000000000e+01       -99.9000       -99.90       -99.9      -100
-9.99000000000000e+02      -999.0000      -999.00      -999.0      -999
-9.99000000000000e+03     -9990.0000     -9990.00     -9990.0     -9990
-9.99000000000000e+04    -99900.0000    -99900.00    -99900.0    -99900
-9.99000000000000e+05   -999000.0000   -999000.00   -999000.0   -999000
-9.99000000000000e-02        -0.0999        -0.10        -0.1        -0
-9.99000000000000e-03        -0.0100        -0.01        -0.0        -0
-9.99000000000000e-04        -0.0010        -0.00        -0.0        -0
-9.99000000000000e-05        -0.0001        -0.00        -0.0        -0
-9.99000000000000e-06        -0.0000        -0.00        -0.0        -0
-9.99000000000000e-07        -0.0000        -0.00        -0.0        -0
> 
> cat("\n--- formatf strip.leading.zeros ---\n")

--- formatf strip.leading.zeros ---
> s4  <- rpart.plot:::formatf(x, digits=4, strip.leading.zeros=TRUE)
> s2  <- rpart.plot:::formatf(x, digits=2, strip.leading.zeros=TRUE)
> s1  <- rpart.plot:::formatf(x, digits=1, strip.leading.zeros=TRUE)
> s0  <- rpart.plot:::formatf(x, digits=0, strip.leading.zeros=TRUE)
> tab <- data.frame("digits=4"=s4, "digits=2"=s2, "digits=1"=s1, "digits=0"=s0,
+                   check.names=F)
> row.names(tab) <- format(x, digits=15)
> print(tab)
                            digits=4     digits=2    digits=1  digits=0
                   NA             NA           NA          NA        NA
                  Inf            Inf          Inf         Inf       Inf
                 -Inf           -Inf         -Inf        -Inf      -Inf
 0.00000000000000e+00          .0000          .00          .0         0
 1.23450000000000e+00         1.2345         1.23         1.2         1
 1.60000000000000e+00         1.6000         1.60         1.6         2
 1.23456000000000e+00         1.2346         1.23         1.2         1
 1.23450000000000e+01        12.3450        12.35        12.3        12
 1.24560000000000e+02       124.5600       124.56       124.6       125
 1.23000000000000e+02       123.0000       123.00       123.0       123
 1.23456789012345e+02       123.4568       123.46       123.5       123
 1.23400000000000e+03      1234.0000      1234.00      1234.0      1234
 9.99900000000000e+03      9999.0000      9999.00      9999.0      9999
 1.23450000000000e+04     12345.0000     12345.00     12345.0     12345
 1.23456000000000e+05    123456.0000    123456.00    123456.0    123456
 1.23400000000000e+06   1234000.0000   1234000.00   1234000.0   1234000
 1.23400000000000e+07  12340000.0000  12340000.00  12340000.0  12340000
 1.23000000000000e-01          .1230          .12          .1         0
 1.23000000000000e-02          .0123          .01          .0         0
 1.00000000000000e+00         1.0000         1.00         1.0         1
 1.00000000000000e+01        10.0000        10.00        10.0        10
 1.00000000000000e+02       100.0000       100.00       100.0       100
 1.00000000000000e+03      1000.0000      1000.00      1000.0      1000
 1.00000000000000e+04     10000.0000     10000.00     10000.0     10000
 1.00000000000000e+05    100000.0000    100000.00    100000.0    100000
 1.00000000000000e+06   1000000.0000   1000000.00   1000000.0   1000000
 1.00000000000000e-01          .1000          .10          .1         0
 1.00000000000000e-02          .0100          .01          .0         0
 1.00000000000000e-03          .0010          .00          .0         0
 1.00000000000000e-04          .0001          .00          .0         0
 1.00000000000000e-05          .0000          .00          .0         0
 1.00000000000000e-06          .0000          .00          .0         0
 9.99000000000000e-01          .9990         1.00         1.0         1
 9.99000000000000e+00         9.9900         9.99        10.0        10
 9.99000000000000e+01        99.9000        99.90        99.9       100
 9.99000000000000e+02       999.0000       999.00       999.0       999
 9.99000000000000e+03      9990.0000      9990.00      9990.0      9990
 9.99000000000000e+04     99900.0000     99900.00     99900.0     99900
 9.99000000000000e+05    999000.0000    999000.00    999000.0    999000
 9.99000000000000e-02          .0999          .10          .1         0
 9.99000000000000e-03          .0100          .01          .0         0
 9.99000000000000e-04          .0010          .00          .0         0
 9.99000000000000e-05          .0001          .00          .0         0
 9.99000000000000e-06          .0000          .00          .0         0
 9.99000000000000e-07          .0000          .00          .0         0
-1.23450000000000e+00        -1.2345        -1.23        -1.2        -1
-1.60000000000000e+00        -1.6000        -1.60        -1.6        -2
-1.23456000000000e+00        -1.2346        -1.23        -1.2        -1
-1.23450000000000e+01       -12.3450       -12.35       -12.3       -12
-1.24560000000000e+02      -124.5600      -124.56      -124.6      -125
-1.23000000000000e+02      -123.0000      -123.00      -123.0      -123
-1.23456789012345e+02      -123.4568      -123.46      -123.5      -123
-1.23400000000000e+03     -1234.0000     -1234.00     -1234.0     -1234
-9.99900000000000e+03     -9999.0000     -9999.00     -9999.0     -9999
-1.23450000000000e+04    -12345.0000    -12345.00    -12345.0    -12345
-1.23456000000000e+05   -123456.0000   -123456.00   -123456.0   -123456
-1.23400000000000e+06  -1234000.0000  -1234000.00  -1234000.0  -1234000
-1.23400000000000e+07 -12340000.0000 -12340000.00 -12340000.0 -12340000
-1.23000000000000e-01        -0.1230        -0.12        -0.1        -0
-1.23000000000000e-02        -0.0123        -0.01        -0.0        -0
-1.00000000000000e+00        -1.0000        -1.00        -1.0        -1
-1.00000000000000e+01       -10.0000       -10.00       -10.0       -10
-1.00000000000000e+02      -100.0000      -100.00      -100.0      -100
-1.00000000000000e+03     -1000.0000     -1000.00     -1000.0     -1000
-1.00000000000000e+04    -10000.0000    -10000.00    -10000.0    -10000
-1.00000000000000e+05   -100000.0000   -100000.00   -100000.0   -100000
-1.00000000000000e+06  -1000000.0000  -1000000.00  -1000000.0  -1000000
-1.00000000000000e-01        -0.1000        -0.10        -0.1        -0
-1.00000000000000e-02        -0.0100        -0.01        -0.0        -0
-1.00000000000000e-03        -0.0010        -0.00        -0.0        -0
-1.00000000000000e-04        -0.0001        -0.00        -0.0        -0
-1.00000000000000e-05        -0.0000        -0.00        -0.0        -0
-1.00000000000000e-06        -0.0000        -0.00        -0.0        -0
-9.99000000000000e-01        -0.9990        -1.00        -1.0        -1
-9.99000000000000e+00        -9.9900        -9.99       -10.0       -10
-9.99000000000000e+01       -99.9000       -99.90       -99.9      -100
-9.99000000000000e+02      -999.0000      -999.00      -999.0      -999
-9.99000000000000e+03     -9990.0000     -9990.00     -9990.0     -9990
-9.99000000000000e+04    -99900.0000    -99900.00    -99900.0    -99900
-9.99000000000000e+05   -999000.0000   -999000.00   -999000.0   -999000
-9.99000000000000e-02        -0.0999        -0.10        -0.1        -0
-9.99000000000000e-03        -0.0100        -0.01        -0.0        -0
-9.99000000000000e-04        -0.0010        -0.00        -0.0        -0
-9.99000000000000e-05        -0.0001        -0.00        -0.0        -0
-9.99000000000000e-06        -0.0000        -0.00        -0.0        -0
-9.99000000000000e-07        -0.0000        -0.00        -0.0        -0
> 
> # examples from the vignette
> 
> fit <- rpart(survived~., data=ititanic)
> cols <- ifelse(fit$frame$yval > .5, "palegreen", "pink")
> par(mfrow=c(2,2))
> prp(fit, box.col=cols, main="Page 4", prefix="probability\n", trace=1)
cex 1   xlim c(0, 1)   ylim c(-0.05, 1.05)
> 
> fit <- rpart(survived~., data=ititanic)
> cp <- sort(unique(fit$frame$complexity))[4:5] # just do 2, for a quicker test
> for(i in 1:length(cp)) {
+     col <- ifelse(fit$frame$complexity >= cp[i], 1, "gray")
+     lwd <- ifelse(fit$frame$complexity >= cp[i], 2, 1)
+     prp(fit, type=1, col=col, branch.col=col, lwd=lwd,
+            sub=sprint("movie %g", i), col.s=2, trace=1)
+ }
cex 1   xlim c(0, 1)   ylim c(0, 1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> # return the given node and all its ancestors (a vector of node numbers)
> path.to.root <- function(node, ancestors=NULL)
+ {
+     if(node == 1)   # root?
+         c(1, ancestors)
+     else            # recurse, %/% 2 gives the parent of node
+         c(node, path.to.root(node %/% 2, ancestors))
+ }
> fit.oz <- rpart(O3~., data=ozone1)
> node <- 22 # 22 is our chosen node, arbitrary for this example
> path <- path.to.root(node)
> nodes <- as.numeric(row.names(fit.oz$frame))
> cols <- ifelse(nodes %in% path, 1, "slategray4")
> lwds <- ifelse(nodes %in% path, 2, 1)
> lty  <- ifelse(nodes %in% path, 1, 2)
> prp(fit.oz, type=4, clip.right.labs=F, nn=TRUE, trace=3, # some niceties
+    main=paste("Path to node", node), col.m=3, lwd=lwds, digits=10,
+    col=cols, branch.col=cols, split.col=cols, nn.col=cols)
head(model.frame):
  O3   vh wind humidity temp  ibh dpg ibt vis doy
1  3 5710    4       28   40 2693 -25  87 250  33
2  5 5700    3       37   45  590 -24 128 100  34
3  5 5760    3       51   54 1450  25 139  60  35
4  6 5720    4       69   35 1568  15 121  60  36
5  4 5790    6       19   45 2631 -33 123 100  37
6  4 5790    3       25   55  554 -28 182 250  38
will apply roundint to the following variables in splits: O3 vh wind humidity temp ibh dpg ibt vis doy 
initial scale 0.75
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.75   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> my.labs <- function(x, labs, digits, varlen)
+ {
+     sprint("ozone %.3g\ndev %.1f", x$frame$yval, x$frame$dev)
+ }
> data(ozone1)
> fit <- rpart(O3~., data=ozone1)
> par(mfrow=c(2,2))
> prp(fit, node.fun=my.labs, main="Page 5", trace=1)
cex 0.75   xlim c(0, 1)   ylim c(-0.1, 1.1)
> 
> my.labs2 <- function(x, labs, digits, varlen)
+ {
+     sprint("%s\n%.2g",
+             ifelse(x$frame$yval > .5, "survived", "died"),
+             x$frame$yval)
+ }
> fit2 <- rpart(survived~., data=ititanic)
> prp(fit2, node.fun=my.labs2, main="my.labs2", trace=1)
cex 1   xlim c(0, 1)   ylim c(-0.05, 1.05)
> 
> my.labs3 <- function(x, labs, digits, varlen) # use passed in labs
+ {
+     sprint("%s\n\ndev %.1f", labs, x$frame$dev)
+ }
> prp(fit2, node.fun=my.labs3, main="my.labs3\nextra=100", trace=1, extra=100, under=T)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> # commented out for rpart.plot version 1.4-0 (user mode no longer supported)
> # fit.user <- rpart(survived~., data=ptitanic, cp=.02)
> # fit.user$method <- "user"
> # fit.user$functions$text <- function (yval, dev, wt, ylevel, digits, n, use.n)
> # {
> #     nclass <- (ncol(yval) - 1L)/2
> #     group <- yval[, 1L]
> #     counts <- yval[, 1L + (1L:nclass)]
> #     if (!is.null(ylevel))
> #         group <- ylevel[group]
> #     temp1 <- format(counts)
> #     if (nclass > 1)
> #         temp1 <- apply(matrix(temp1, ncol = nclass), 1, paste, collapse = "/")
> #     cat("use.n=", use.n, "\n")
> #     if (use.n)
> #         out <- paste(group, "!\n", temp1, sep = "")
> #     else
> #         out <- format(group)
> #     return(out)
> # }
> # prp(fit.user, node.fun=my.labs3, main="method=user\nmy.labs3 extra=100", trace=1, extra=100, under=T, prefix="result: ")
> 
> a20 <- rpart(survived~., data=ptitanic, control=list(cp=.02))
> par(mfrow=c(3,3))
> 
> boxes.include.gap <- FALSE
> 
> prp(a20, type=4,
+     main="Page 6",
+     cex.main=.9,
+     under=F,
+     extra=0,
+     split.cex=1,
+     faclen=0, trace=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.931   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a20, type=4,
+     main="(2) extra=1\nprefix=0 suffix=0 split.suffix=0\nsplit.cex=1\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     split.cex=1,
+     faclen=1, trace=1,
+     branch=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.688   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a20, type=4,
+     main="(3) extra=1\nprefix=1 suffix=0 split.suffix=0\nsplit.cex=1\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     prefix="prefix ",
+     suffix="",
+     split.suffix="",
+     split.cex=1,
+     faclen=0, trace=1,
+     split.border.col=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.688   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a20, type=4,
+     main="(4) extra=4\nprefix=1 suffix=1 split.suffix=1\nsplit.cex=1\n",
+     cex.main=.9,
+     under=F,
+     extra=4,
+     prefix="prefix ",
+     suffix="\nsuffix",
+     split.suffix="\nsplit.suffix",
+     split.cex=1,
+     faclen=0, trace=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.438   xlim c(0, 1)   ylim c(-0.1, 1.1)
> 
> prp(a20, type=4,
+     main="(5) extra=5\nprefix=0 suffix=0 split.suffix=0\nsplit.cex=1.4\n",
+     cex.main=.9,
+     under=F,
+     extra=5,
+     split.cex=1.4,
+     faclen=0, trace=1,
+     split.border.col=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.762   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a20, type=4,
+     main="(6) extra=101\nprefix=0 suffix=0 split.suffix=0\nsplit.cex=.7\n",
+     cex.main=.9,
+     under=F,
+     extra=101,
+     split.cex=.7,
+     faclen=0, trace=1,
+     branch=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.65   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a20, type=4,
+     main="(7) extra=1\nprefix=1 suffix=1 split.suffix=1\nsplit.cex=1.4\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     prefix="prefix ",
+     suffix="\nsuffix",
+     split.suffix="\nsplit.suffix",
+     split.cex=1.4,
+     faclen=1, trace=1,
+     split.border.col=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.4   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a20, type=4,
+     main="(8) extra=0\nprefix=1 suffix=1 split.suffix=1\nsplit.cex=.7\n",
+     cex.main=.9,
+     under=F,
+     extra=0,
+     prefix="prefix ",
+     suffix="\nsuffix",
+     split.suffix="\nsplit.suffix",
+     split.cex=.7,
+     faclen=0, trace=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.625   xlim c(0, 1)   ylim c(-0.05, 1.05)
> 
> # TODO split.yshift indexing is confusing
> prp(a20, type=4, trace=2, nn=T, split.border.col=1,
+     main="(9) manual yshift and split.yshift\n",
+     prefix=c("up ", "", "up ", "", "", "", ""),
+     yshift=c(2, 0, 2, 0, 0, 0, 0),
+     branch=1,
+     split.prefix=c("", "", "down ", "", "", "", ""),
+     split.yshift=c(0, -3, 0, 0, 0, 0, 0))
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.688
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.688   xlim c(0, 1)   ylim c(0, 1)
> 
> par(old.par)
> 
> a21 <- rpart(survived~., data=ptitanic, control=list(cp=.02))
> par(mfrow=c(3,3))
> 
> prp(a21, type=1,
+     main="Page 7",
+     cex.main=.9,
+     under=F,
+     extra=0,
+     split.cex=1,
+     faclen=0, trace=1,
+     boxes.include.gap=boxes.include.gap)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(2) extra=1\nprefix=0 suffix=0 split.suffix=0\nsplit.cex=1\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     split.cex=1,
+     faclen=1, trace=1,
+     branch=1,
+     boxes.include.gap=boxes.include.gap)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(3) extra=1\nprefix=1 suffix=0 split.suffix=0\nsplit.cex=1\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     prefix="prefix ",
+     suffix="",
+     split.suffix="",
+     split.cex=1,
+     faclen=0, trace=1,
+     boxes.include.gap=boxes.include.gap)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(4) extra=1\nprefix=1 suffix=1 split.suffix=1\nsplit.cex=1\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     prefix="prefix ",
+     suffix="\nsuffix",
+     split.suffix="\nsplit.suffix",
+     split.cex=1,
+     faclen=1, trace=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.931   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(5) extra=0\nprefix=0 suffix=0 split.suffix=0\nsplit.cex=1.4\n",
+     cex.main=.9,
+     under=F,
+     extra=0,
+     split.cex=1.4,
+     faclen=0, trace=1,
+     branch=1,
+     boxes.include.gap=boxes.include.gap)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(6) extra=1\nprefix=0 suffix=0 split.suffix=0\nsplit.cex=.7\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     split.cex=.7,
+     faclen=1, trace=1,
+     split.border.col=1,
+     boxes.include.gap=boxes.include.gap)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(7) extra=1\nprefix=1 suffix=1 split.suffix=1\nsplit.cex=1.4\n",
+     cex.main=.9,
+     under=F,
+     extra=1,
+     prefix="prefix ",
+     suffix="\nsuffix",
+     split.suffix="\nsplit.suffix",
+     split.cex=1.4,
+     faclen=0, trace=1,
+     branch=1,
+     split.border.col=1,
+     boxes.include.gap=boxes.include.gap)
cex 0.688   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1,
+     main="(8) extra=0\nprefix=1 suffix=1 split.suffix=1\nsplit.cex=.7\n",
+     cex.main=.9,
+     under=F,
+     extra=0,
+     prefix="prefix ",
+     suffix="\nsuffix",
+     split.suffix="\nsplit.suffix",
+     split.cex=.7,
+     faclen=1, trace=1,
+     split.border.col=1,
+     boxes.include.gap=boxes.include.gap)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a21, type=1, trace=2, nn=0, ni=0,
+     main="(9) manual yshift and split.yshift\n",
+     split.border.col=1,
+     cex.main=.9,
+     prefix=c("up\n", "", "up\n", "up\n", "", "", ""),
+     yshift=c(3, 0, 3, 3, 0, 0, 0),
+     split.prefix=c("", "", "down\n", "", "", "", ""),
+     split.yshift=c(0, -1, 0, 0, 0, 0, 0))
will apply roundint to the following variables in splits: sibsp parch 
initial scale 1.33
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
clipped scale 1.33 to max.auto.cex 1
cex 1   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> 
> par(mfrow=c(3,3))
> a8 <- rpart(survived~., data=ptitanic, control=list(cp=.02))
> prp(a8, type=2, main="Page 8")
> prp(a8, type=2, extra=4, main="extra=4")
> prp(a8, type=2, extra=104, main="extra=104")
> 
> prp(a8, type=2, extra=0,   under=T, main="extra=0,   under=T")
> prp(a8, type=2, extra=4,   under=T, main="extra=4,   under=T")
> prp(a8, type=2, extra=104, under=T, main="extra=104, under=T")
> 
> prp(a8, type=2, extra=104, under=T, under.cex=.6, main="extra=104, under=T\nunder.cex=.6")
> prp(a8, type=2, extra=104, under=T, under.cex=1, main="extra=104, under=T\nunder.cex=1.2")
> prp(a8, type=2, extra=104, under=T, split.border.col=1, border.col=0, main="extra=104, under=T\nsplit.border=1, border=0")
> par(old.par)
> 
> par(mfrow=c(2,2))
> fit3 <- rpart(survived~., data=ititanic, control=list(cp=.002))
> prp(fit3, trace=3, nn=0, faclen=0, prefix="prob ", main="Page 9")
head(model.frame):
  survived pclass    sex     age sibsp parch
1        1    1st female 29.0000     0     0
2        1    1st   male  0.9167     1     2
3        0    1st female  2.0000     1     2
4        0    1st   male 30.0000     1     2
5        0    1st female 25.0000     1     2
6        1    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: survived sibsp parch 
initial scale 0.65
    node shifter: cex improvement 1     shift.amount 1.5  <new best
    node shifter: cex improvement 0.538 shift.amount 2.0  
shifter: cex improvement 1 best.shift.amount 1.5 best.split.yshift.amount 0 (won't be used)
cex 0.65   xlim c(0, 1)   ylim c(0, 1)
> prp(fit3, extra=100, trace=3, nn=TRUE, faclen=0, fallen.leaves=TRUE, main="fallen leaves")
head(model.frame):
  survived pclass    sex     age sibsp parch
1        1    1st female 29.0000     0     0
2        1    1st   male  0.9167     1     2
3        0    1st female  2.0000     1     2
4        0    1st   male 30.0000     1     2
5        0    1st female 25.0000     1     2
6        1    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: survived sibsp parch 
initial scale 0.45
    node shifter: cex improvement 0.778 shift.amount 1.5  
    node shifter: cex improvement 0.556 shift.amount 2.0  
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.45   xlim c(0, 1)   ylim c(0, 1)
> prp(fit3, type=4, trace=3, nn=TRUE, clip.right.labs=0, split.border.col=1, main="type=4 ")
head(model.frame):
  survived pclass    sex     age sibsp parch
1        1    1st female 29.0000     0     0
2        1    1st   male  0.9167     1     2
3        0    1st female  2.0000     1     2
4        0    1st   male 30.0000     1     2
5        0    1st female 25.0000     1     2
6        1    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: survived sibsp parch 
initial scale 0.45
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.45   xlim c(0, 1)   ylim c(0, 1)
> # use prefix below to force shifting of fallen leaves to test shifter
> prp(fit3, type=4, branch=.5, extra=1, under=TRUE, trace=3, nn=FALSE, fallen.leaves=1, prefix="probability ", main="type=4, fallen leaves")
head(model.frame):
  survived pclass    sex     age sibsp parch
1        1    1st female 29.0000     0     0
2        1    1st   male  0.9167     1     2
3        0    1st female  2.0000     1     2
4        0    1st   male 30.0000     1     2
5        0    1st female 25.0000     1     2
6        1    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: survived sibsp parch 
initial scale 0.25
    node shifter: cex improvement 1.35  shift.amount 1.5  <new best
    node shifter: cex improvement 1.4   shift.amount 2.0  <new best
shifter: cex improvement 1.4 best.shift.amount 2 best.split.yshift.amount 0 (will be used)
ycompress increased cex by 1.40
cex 0.35   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> par(mfrow=c(2,2))
> fit7 <- rpart(survived ~ ., data=ptitanic, cp=.01)
> # this was wrong until I added check that a shift doesn't move nodes above the nodes for the level above
> prp(fit7, extra=1, branch=1, trace=3, nn=1, main="Page 10")
head(model.frame):
  survived pclass    sex     age sibsp parch
1 survived    1st female 29.0000     0     0
2 survived    1st   male  0.9167     1     2
3     died    1st female  2.0000     1     2
4     died    1st   male 30.0000     1     2
5     died    1st female 25.0000     1     2
6 survived    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: sibsp parch 
initial scale 1.03
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
clipped scale 1.03 to max.auto.cex 1
cex 1   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> par(mfrow=c(2,3))
> prp(fit2, prefix=ifelse(fit2$frame$yval > .5, "survived\n", "died\n"), main="Page 11",
+     fam.main="NewCenturySchoolbook", cex.main=1.3, trace=1,
+     border.col=0, split.border.col="steelblue3")
cex 1   xlim c(0, 1)   ylim c(-0.25, 1.25)
> # test long names and big and small numbers
> ptitanic1 <- ptitanic
> ptitanic1$sibsp1234567890 <- 1e3 * ptitanic1$sibsp
> ptitanic1$sibsp <- NULL
> ptitanic1$age <- 1e-5 * ptitanic1$age
> ptitanic1$parch <- 1e7 * ptitanic1$parch
> fit2 <- rpart(survived~., data=ptitanic1)
> prp(fit2, faclen=0, digits=4, trace=1,
+     border.col=NA, split.border.col="steelblue3", split.round=1)
cex 0.816   xlim c(0, 1)   ylim c(-0.1, 1.1)
> 
> # even more extreme values
> ptitanic2 <- ptitanic
> ptitanic2$sibsp <- 1e12 * ptitanic2$sibsp
> ptitanic2$age <- 1e-16 * ptitanic2$age
> ptitanic2$parch <- 1e12 * ptitanic2$parch
> fit2a <- rpart(survived~., data=ptitanic2)
> prp(fit2a, faclen=0, digits=4, trace=1,
+     border.col=NA, split.border.col="steelblue3", split.round=1)
cex 0.816   xlim c(0, 1)   ylim c(-0.1, 1.1)
> 
> cat("test tweak.splits\n")
test tweak.splits
> splits <- matrix(0, 2, 1)
> colnames(splits) <- "index"
> splits[,"index"] <- c(1234.5, 1235.5)
> cat("format(splits, digits=2)\n")
format(splits, digits=2)
> print(format(splits, digits=2))
     index 
[1,] "1234"
[2,] "1236"
> tweak.splits <- rpart.plot:::tweak.splits(list(splits=splits), roundint=FALSE, digits=2, trace=FALSE)
> cat("format(tweak.splits, digits=2)\n")
format(tweak.splits, digits=2)
> print(format(tweak.splits, digits=2))
     index 
[1,] "1235"
[2,] "1236"
> splits <- matrix(0, 22, 1)
> colnames(splits) <- "index"
> splits[,"index"] <- c( 1234.5,      1235.5,
+                        1234.5e-10,  1235.5e-10,
+                        1234.5e-20,  1235.5e-20,
+                       -1234.5,     -1235.5,
+                       -1234.5e-10, -1235.5e-10,
+                       -1234.5e-20, -1235.5e-20,
+ 
+                        1234.5e10,  1235.5e10,
+                        1234.5e20,  1235.5e20,
+                       -1234.5,    -1235.5,
+                       -1234.5e10, -1235.5e10,
+                       -1234.5e20, -1235.5e20)
> cat("format(splits, digits=4)\n")
format(splits, digits=4)
> print(format(splits, digits=4))
      index       
 [1,] " 1.234e+03"
 [2,] " 1.236e+03"
 [3,] " 1.235e-07"
 [4,] " 1.235e-07"
 [5,] " 1.235e-17"
 [6,] " 1.235e-17"
 [7,] "-1.234e+03"
 [8,] "-1.236e+03"
 [9,] "-1.235e-07"
[10,] "-1.235e-07"
[11,] "-1.235e-17"
[12,] "-1.235e-17"
[13,] " 1.234e+13"
[14,] " 1.236e+13"
[15,] " 1.235e+23"
[16,] " 1.236e+23"
[17,] "-1.234e+03"
[18,] "-1.236e+03"
[19,] "-1.234e+13"
[20,] "-1.236e+13"
[21,] "-1.235e+23"
[22,] "-1.236e+23"
> tweak.splits <- rpart.plot:::tweak.splits(list(splits=splits), roundint=FALSE, digits=2, trace=FALSE)
> cat("format(tweak.splits, digits=4)\n")
format(tweak.splits, digits=4)
> print(format(tweak.splits, digits=4))
      index       
 [1,] " 1.235e+03"
 [2,] " 1.236e+03"
 [3,] " 1.235e-07"
 [4,] " 1.236e-07"
 [5,] " 1.235e-17"
 [6,] " 1.236e-17"
 [7,] "-1.234e+03"
 [8,] "-1.235e+03"
 [9,] "-1.234e-07"
[10,] "-1.235e-07"
[11,] "-1.234e-17"
[12,] "-1.235e-17"
[13,] " 1.234e+13"
[14,] " 1.236e+13"
[15,] " 1.235e+23"
[16,] " 1.236e+23"
[17,] "-1.234e+03"
[18,] "-1.235e+03"
[19,] "-1.234e+13"
[20,] "-1.236e+13"
[21,] "-1.235e+23"
[22,] "-1.236e+23"
> 
> # test small tree, also tests xcompact and ycompact
> fit.small <- rpart(survived~., data=ptitanic1, , control=list(cp=.1))
> prp(fit.small, extra=100, faclen=0, main="small tree", trace=1)
cex 1   xlim c(-0.65, 1.65)   ylim c(-1.55, 2.55)
> 
> # test negative digits
> prp(fit2, faclen=0, digits=-4,
+     border.col=NA, split.border.col="steelblue3", split.round=1)
> par(old.par)
> 
> par(mfrow=c(2,3))
> fit4 <- rpart(survived~., data=ititanic, method="class", control=list(cp=.02))
> prp(fit4, trace=2, cex=.8, tweak=1.1, main="Page 12",
+        xflip=TRUE, yflip=TRUE, type=1, extra=100,  yesno=FALSE)
will apply roundint to the following variables in splits: survived sibsp parch 
initial scale 1.33
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.8 (before applying tweak 1.1)   xlim c(0, 1)   ylim c(0, 1)
Warning: cex and tweak both specified, applying both
> # TODO wanna include family below, but postscript giving me grief
> fit4.strange.method <- fit4
> fit4.strange.method$method <- "unknown.class.method"
> options(warn=2) # treat warnings as errors
> expect.err(try(prp(fit4.strange.method, main="left=FALSE, fonts, user method", left=FALSE, font=c(1,2,3), split.cex=c(1, 1.2), branch=.5, trace=1, extra=1)),
+            "Unrecognized rpart object: treating as a class response model")
Error : (converted from warning) Unrecognized rpart object: treating as a class response model
Got error as expected from try(prp(fit4.strange.method, main = "left=FALSE, fonts, user method",     left = FALSE, font = c(1, 2, 3), split.cex = c(1, 1.2), branch = 0.5,     trace = 1, extra = 1))
> options(warn=1) # print warnings as they occur
> prp(fit4.strange.method, main="left=FALSE, fonts, user method", left=FALSE, font=c(1,2,3), split.cex=c(1, 1.2), branch=.5, trace=1, extra=1)
Warning: Unrecognized rpart object: treating as a class response model
cex 1   xlim c(-0.2, 1.2)   ylim c(-0.15, 1.15)
> prp(fit4, main="unknown.class.method\nuniform=FALSE", uniform=FALSE, trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> prp(fit4, main="unknown.class.method\ntype=1\nbox.palette=\"auto\"", type=1, box.palette="auto")
> data(ozone1)
> fit.oz1 <- rpart(O3~., data=ozone1)
> fit.oz1$method <- "unknown.anova.method"
> options(warn=2) # treat warnings as errors
> expect.err(try(prp(fit.oz1, main="unknown.anova.method\ndigits=7", digits=7, trace=1)),
+            "Unrecognized rpart object: treating as a numeric response model")
Error : (converted from warning) Unrecognized rpart object: treating as a numeric response model
Got error as expected from try(prp(fit.oz1, main = "unknown.anova.method\ndigits=7", digits = 7,     trace = 1))
> options(warn=1) # print warnings as they occur
> obj <- prp(fit.oz1, main="unknown.anova.method\ndigits=7", digits=7, trace=1)
Warning: Unrecognized rpart object: treating as a numeric response model
cex 1   xlim c(0, 1)   ylim c(0, 1)
> obj <- prp(fit.oz1, main="unknown.anova.method\ntype=1\nbox.palette=\"auto\"", type=1, box.palette="auto")
Warning: Unrecognized rpart object: treating as a numeric response model
> cat("obj returned by prp:\n")
obj returned by prp:
> print(obj)
$obj
n= 330 

node), split, n, deviance, yval
      * denotes terminal node

 1) root 330 21115.4100 11.775760  
   2) temp< 67.5 214  4114.3040  7.425234  
     4) ibh>=3573.5 108   689.6296  5.148148 *
     5) ibh< 3573.5 106  2294.1230  9.745283  
      10) dpg< -9.5 35   362.6857  6.457143 *
      11) dpg>=-9.5 71  1366.4790 11.366200  
        22) ibt< 159 40   287.9000  9.050000 *
        23) ibt>=159 31   587.0968 14.354840 *
   3) temp>=67.5 116  5478.4400 19.801720  
     6) ibt< 226.5 55  1276.8360 15.945450  
      12) humidity< 59.5 10   167.6000 10.800000 *
      13) humidity>=59.5 45   785.6444 17.088890 *
     7) ibt>=226.5 61  2646.2620 23.278690  
      14) doy>=306.5 8   398.0000 16.000000 *
      15) doy< 306.5 53  1760.4530 24.377360  
        30) vis>=55 36  1149.8890 22.944440 *
        31) vis< 55 17   380.1176 27.411760 *

$snipped.nodes
NULL

$xlim
[1] 0 1

$ylim
[1] -0.05  1.05

$x
 [1] 0.39948603 0.12197472 0.04154716 0.20240228 0.12269930 0.28210527
 [7] 0.20964801 0.35456253 0.67699734 0.51396851 0.44151124 0.58642577
[13] 0.81104328 0.73134029 0.89074626 0.81828900 0.96320352

$y
 [1] 0.95717280 0.72093869 0.48470457 0.48470457 0.24847046 0.24847046
 [7] 0.01223634 0.01223634 0.72093869 0.48470457 0.24847046 0.24847046
[13] 0.48470457 0.24847046 0.24847046 0.01223634 0.01223634

$branch.x
      [,1]      [,2]       [,3]      [,4]      [,5]      [,6]      [,7]
x 0.399486 0.1219747 0.04154716 0.2024023 0.1226993 0.2821053 0.2096480
        NA 0.3439838 0.10588921 0.1380602 0.1864617 0.2183429 0.2676138
        NA 0.3994860 0.12197472 0.1219747 0.2024023 0.2024023 0.2821053
       [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
x 0.3545625 0.6769973 0.5139685 0.4415112 0.5864258 0.8110433 0.7313403
  0.2965967 0.4549883 0.6327984 0.4994771 0.5284600 0.6922134 0.7951027
  0.2821053 0.3994860 0.6769973 0.5139685 0.5139685 0.6769973 0.8110433
      [,15]     [,16]     [,17]
x 0.8907463 0.8182890 0.9632035
  0.8269839 0.8762548 0.9052377
  0.8110433 0.8907463 0.8907463

$branch.y
       [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
y 0.9619019 0.7256678 0.5274375 0.4894337 0.2912034 0.2531996 0.0549693
         NA 0.9087421 0.6725080 0.6725080 0.4362739 0.4362739 0.2000398
         NA 0.9087421 0.6725080 0.6725080 0.4362739 0.4362739 0.2000398
       [,8]      [,9]     [,10]     [,11]     [,12]     [,13]     [,14]
y 0.0549693 0.7256678 0.4894337 0.2912034 0.2912034 0.4894337 0.2912034
  0.2000398 0.9087421 0.6725080 0.4362739 0.4362739 0.6725080 0.4362739
  0.2000398 0.9087421 0.6725080 0.4362739 0.4362739 0.6725080 0.4362739
      [,15]     [,16]     [,17]
y 0.2531996 0.0549693 0.0549693
  0.4362739 0.2000398 0.2000398
  0.4362739 0.2000398 0.2000398

$labs
 [1] "12"  "7.4" "5.1" "9.7" "6.5" "11"  "9.1" "14"  "20"  "16"  "11"  "17" 
[13] "23"  "16"  "24"  "23"  "27" 

$cex
[1] 1

$boxes
$boxes$x1
 [1] 0.3634229927 0.0807571710 0.0003296112 0.1611847308 0.0814817436
 [6] 0.2460422297 0.1684304569 0.3184994908 0.6409343027 0.4779054653
[11] 0.4054482042 0.5503627264 0.7749802358 0.6952772485 0.8546832230
[16] 0.7822259619 0.9271404841

$boxes$y1
 [1]  0.880253481  0.644019366  0.470460254  0.407785251  0.234226138
 [6]  0.171551135 -0.002007977 -0.002007977  0.644019366  0.407785251
[11]  0.234226138  0.234226138  0.407785251  0.234226138  0.171551135
[16] -0.002007977 -0.002007977

$boxes$x2
 [1] 0.43554907 0.16319228 0.08276472 0.24361984 0.16391685 0.31816831
 [7] 0.25086556 0.39062557 0.71306038 0.55003155 0.47757429 0.62248881
[13] 0.84710632 0.76740333 0.92680930 0.85435204 0.99926657

$boxes$y2
 [1] 0.9372308 0.7009966 0.5274375 0.4647625 0.2912034 0.2285284 0.0549693
 [8] 0.0549693 0.7009966 0.4647625 0.2912034 0.2912034 0.4647625 0.2912034
[15] 0.2285284 0.0549693 0.0549693


$split.labs
[1] ""

$split.cex
 [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

$split.box
$split.box$x1
 [1] 0.297990360 0.005516133          NA 0.110696474          NA 0.191938398
 [7]          NA          NA 0.586830471 0.379506207          NA          NA
[13] 0.700110026          NA 0.796945275          NA          NA

$split.box$y1
 [1] 0.9429285 0.7066944        NA 0.4704603        NA 0.2342261        NA
 [8]        NA 0.7066944 0.4704603        NA        NA 0.4704603        NA
[15] 0.2342261        NA        NA

$split.box$x2
 [1] 0.5009817 0.2384333        NA 0.2941081        NA 0.3722721        NA
 [8]        NA 0.7671642 0.6484308        NA        NA 0.9219765        NA
[15] 0.9845473        NA        NA

$split.box$y2
 [1] 0.9999058 0.7636716        NA 0.5274375        NA 0.2912034        NA
 [8]        NA 0.7636716 0.5274375        NA        NA 0.5274375        NA
[15] 0.2912034        NA        NA


> par(old.par)
> 
> # test extra and faclen etc. on anova model
> a1 <- rpart(survived~., data=ititanic, control=list(cp=.03))
> par(mfrow=c(3, 3))
> plot(a1, unif=TRUE, branch=.3, main="Page 13")
> text(a1, fancy=T, fwidth=.35, fheight=0.3, use.n=TRUE, all=T, digits=3, xpd=NA, pretty=0)
> prp(a1, extra=0, faclen=-3, varlen=2,             type=1, main="extra=0", trace=1)
cex 1   xlim c(-0.2, 1.2)   ylim c(-0.1, 1.1)
> prp(a1, extra=1, faclen=1,  varlen=-2,              type=4, main="extra=1", trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> prp(a1, extra=100, faclen=3,             digits=3,  type=4, clip.right.labs=FALSE, facsep=" or ", main="extra=100", trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> # plot(1, 1, type="n", axes=FALSE, xlab="", ylab="") # blank
> # test xflip and left (note: left=FALSE cannot be used with type=4)
> prp(a1, main="\n\nxflip",                xflip=TRUE,             extra=101, faclen=0, trace=1)
cex 1   xlim c(0, 1)   ylim c(-0.15, 1.15)
> prp(a1, main="type=4",                                type=4, extra=101, faclen=0, trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> prp(a1, main="xflip type=4",              xflip=TRUE, type=4, extra=101, faclen=0, trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> prp(a1, main="xflip type=4 clip.right=FALSE",   xflip=TRUE, type=4, extra=101, faclen=0, clip.right=FALSE, trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> # test extra and faclen etc. on class model
> a2 <- rpart(survived~., data=ptitanic, control=list(cp=.02))
> par(mfrow=c(3, 2))
> plot(a2, unif=TRUE, branch=.3, main="Page 14")
> text(a2, use.n=TRUE, all=T, digits=3, xpd=NA, pretty=0)
> prp(a2, extra=0, eq=" eq ", lt=" lt ", ge=" ge ", facsep="|", xsep="/",
+     type=4, main="extra=0", trace=3, split.border.col=1)
head(model.frame):
  survived pclass    sex     age sibsp parch
1 survived    1st female 29.0000     0     0
2 survived    1st   male  0.9167     1     2
3     died    1st female  2.0000     1     2
4     died    1st   male 30.0000     1     2
5     died    1st female 25.0000     1     2
6 survived    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.931
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.931   xlim c(0, 1)   ylim c(0, 1)
> prp(a2, extra=1, type=1,  xsep=", ", main="extra=1", trace=1)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> prp(a2, extra=100, type=3, clip.right.labs=FALSE, main="extra=100", trace=3, split.border.col=1, ycompress.cex=1)
head(model.frame):
  survived pclass    sex     age sibsp parch
1 survived    1st female 29.0000     0     0
2 survived    1st   male  0.9167     1     2
3     died    1st female  2.0000     1     2
4     died    1st   male 30.0000     1     2
5     died    1st female 25.0000     1     2
6 survived    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.931
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.931   xlim c(0, 1)   ylim c(0, 1)
> prp(a2, extra=4, type=0, main="extra=4", faclen=0, trace=1, under=TRUE, col=2)
cex 1   xlim c(-0.2, 1.2)   ylim c(0, 1)
> prp(a2, extra=104, type=0, main="extra=104", faclen=0, trace=1)
cex 1   xlim c(-0.2, 1.2)   ylim c(0, 1)
> par(old.par)
> 
> par(mfrow=c(9,4), mar = c(4, 3, 2, 1), mgp = c(1.5, .5, 0))
> 
> a4 <- rpart(survived~., data=ptitanic, cp=.03)
> plot(a4, unif=T, branch=.3); text(a4, use.n=1, cex=1, xpd=NA, pretty=0); title("Page 15", cex.main=.9)
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=0, cex.main=.9, main="extra=0\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=1, cex.main=.9, main="extra=1\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=4, cex.main=.9, main="extra=4\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=5, cex.main=.9, main="extra=5\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=6, cex.main=.9, main="extra=6\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, extra=7, under=F, cex.main=.9, main="extra=7\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=8, cex.main=.9, main="extra=8\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=9, cex.main=.9, main="extra=9\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=100, cex.main=.9, main="extra=100\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=101, cex.main=.9, main="extra=101\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=104, cex.main=.9, main="extra=104\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=105, cex.main=.9, main="extra=105\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=106, cex.main=.9, main="extra=106\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=107, cex.main=.9, main="extra=107\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=108, cex.main=.9, main="extra=108\nunder=F")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=109, cex.main=.9, main="extra=109\n under=F")
> 
> empty.plot()
> empty.plot()
> empty.plot()
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=0, cex.main=.9, main="extra=0\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=1, cex.main=.9, main="extra=1\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=4, cex.main=.9, main="extra=4\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=5, cex.main=.9, main="extra=5\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=6, cex.main=.9, main="extra=6\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, extra=7, under=T, cex.main=.9, main="extra=7\nunder=T (ignored)")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=8, cex.main=.9, main="extra=8\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=9, cex.main=.9, main="extra=9\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=100, cex.main=.9, main="extra=100\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=101, cex.main=.9, main="extra=101\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=104, cex.main=.9, main="extra=104\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=105, cex.main=.9, main="extra=105\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=106, cex.main=.9, main="extra=106\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=107, cex.main=.9, main="extra=107\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=108, cex.main=.9, main="extra=108\nunder=T")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=109, cex.main=.9, main="extra=109\n under=T")
> 
> par(old.par)
> 
> par(mfrow=c(9,4), mar = c(4, 3, 2, 1), mgp = c(1.5, .5, 0))
> 
> a4 <- rpart(survived~., data=ptitanic, cp=.03)
> plot(a4, unif=T, branch=.3); text(a4, use.n=1, cex=1, xpd=NA, pretty=0); title("Page 16", cex.main=.9)
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=100, under.percent=0, cex.main=.9, main="extra=100\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=101, under.percent=0, cex.main=.9, main="extra=101\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=104, under.percent=0, cex.main=.9, main="extra=104\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=105, under.percent=0, cex.main=.9, main="extra=105\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=106, under.percent=0, cex.main=.9, main="extra=106\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=107, under.percent=0, cex.main=.9, main="extra=107\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=108, under.percent=0, cex.main=.9, main="extra=108\nunder=F under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=109, under.percent=0, cex.main=.9, main="extra=109\n under=F under.percent=0")
> 
> empty.plot()
> empty.plot()
> empty.plot()
> 
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=100, under.percent=0, cex.main=.9, main="extra=100\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=101, under.percent=0, cex.main=.9, main="extra=101\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=104, under.percent=0, cex.main=.9, main="extra=104\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=105, under.percent=0, cex.main=.9, main="extra=105\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=106, under.percent=0, cex.main=.9, main="extra=106\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=107, under.percent=0, cex.main=.9, main="extra=107\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=108, under.percent=0, cex.main=.9, main="extra=108\nunder=T under.percent=0")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=109, under.percent=0, cex.main=.9, main="extra=109\n under=T under.percent=0")
> 
> par(old.par)
> 
> par(mfrow=c(9,4), mar = c(4, 3, 2, 1), mgp = c(1.5, .5, 0))
> 
> a4 <- rpart(survived~., data=ptitanic, cp=.03)
> plot(a4, unif=T, branch=.3); text(a4, use.n=1, cex=1, xpd=NA, pretty=0); title("Page 17", cex.main=.9)
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=100, under.percent=1, cex.main=.9, main="extra=100\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=101, under.percent=1, cex.main=.9, main="extra=101\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=104, under.percent=1, cex.main=.9, main="extra=104\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=105, under.percent=1, cex.main=.9, main="extra=105\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=106, under.percent=1, cex.main=.9, main="extra=106\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=107, under.percent=1, cex.main=.9, main="extra=107\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=108, under.percent=1, cex.main=.9, main="extra=108\nunder=F under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=F, extra=109, under.percent=1, cex.main=.9, main="extra=109\n under=F under.percent=1")
> 
> empty.plot()
> empty.plot()
> empty.plot()
> 
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> empty.plot()
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=100, under.percent=1, cex.main=.9, main="extra=100\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=101, under.percent=1, cex.main=.9, main="extra=101\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=104, under.percent=1, cex.main=.9, main="extra=104\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=105, under.percent=1, cex.main=.9, main="extra=105\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=106, under.percent=1, cex.main=.9, main="extra=106\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=107, under.percent=1, cex.main=.9, main="extra=107\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=108, under.percent=1, cex.main=.9, main="extra=108\nunder=T under.percent=1")
> prp(a4, type=1, yesno=T, faclen=-1, under=T, extra=109, under.percent=1, cex.main=.9, main="extra=109\n under=T under.percent=1")
> 
> par(old.par)
> 
> # May 2018: extra=9,10,11
> par(mfrow=c(2, 3))
> prp(a4, extra=9, main="extra=9")
> prp(a4, extra=10, main="extra=10")
> prp(a4, extra=11, main="extra=11")
> prp(a4, extra=109, main="extra=109")
> prp(a4, extra=110, main="extra=110")
> prp(a4, extra=111, main="extra=111")
> expect.err(try(prp(a4, extra=12, main="extra=12")), 'extra=12 is illegal')

The 'extra' argument:
    0  No extra information
    1  Number of observations in the node
    2  Class models: Classification rate (ncorrect/nobservations)
       Poisson and exp models: number of events
    3  Class models: Misclassification rate
    4  Class models: Probability per class
    5  Class models: Like 4 but don't display the fitted class
    6  Class models: Probability of second class only
    7  Class models: Like 6 but don't display the fitted class
    8  Class models: Probability of the fitted class
    9  Class models: Probability relative to all observations
    10 Class models: like 9 but display the probability of the second class only

    Add 100 to also display the percentage of observations in the node

Error : extra=12 is illegal
Got error as expected from try(prp(a4, extra = 12, main = "extra=12"))
> expect.err(try(prp(a4, extra=99, main="extra=99")), 'extra=99 is illegal')

The 'extra' argument:
    0  No extra information
    1  Number of observations in the node
    2  Class models: Classification rate (ncorrect/nobservations)
       Poisson and exp models: number of events
    3  Class models: Misclassification rate
    4  Class models: Probability per class
    5  Class models: Like 4 but don't display the fitted class
    6  Class models: Probability of second class only
    7  Class models: Like 6 but don't display the fitted class
    8  Class models: Probability of the fitted class
    9  Class models: Probability relative to all observations
    10 Class models: like 9 but display the probability of the second class only

    Add 100 to also display the percentage of observations in the node

Error : extra=99 is illegal
Got error as expected from try(prp(a4, extra = 99, main = "extra=99"))
> expect.err(try(prp(a4, extra=-1, main="extra=-1")), 'extra=-1 is illegal')

The 'extra' argument:
    0  No extra information
    1  Number of observations in the node
    2  Class models: Classification rate (ncorrect/nobservations)
       Poisson and exp models: number of events
    3  Class models: Misclassification rate
    4  Class models: Probability per class
    5  Class models: Like 4 but don't display the fitted class
    6  Class models: Probability of second class only
    7  Class models: Like 6 but don't display the fitted class
    8  Class models: Probability of the fitted class
    9  Class models: Probability relative to all observations
    10 Class models: like 9 but display the probability of the second class only

    Add 100 to also display the percentage of observations in the node

Error : extra=-1 is illegal
Got error as expected from try(prp(a4, extra = -1, main = "extra=-1"))
> expect.err(try(prp(a4, extra=1.23, main="extra=1.23")), 'extra=1.23 is illegal')

The 'extra' argument:
    0  No extra information
    1  Number of observations in the node
    2  Class models: Classification rate (ncorrect/nobservations)
       Poisson and exp models: number of events
    3  Class models: Misclassification rate
    4  Class models: Probability per class
    5  Class models: Like 4 but don't display the fitted class
    6  Class models: Probability of second class only
    7  Class models: Like 6 but don't display the fitted class
    8  Class models: Probability of the fitted class
    9  Class models: Probability relative to all observations
    10 Class models: like 9 but display the probability of the second class only

    Add 100 to also display the percentage of observations in the node

Error : extra=1.23 is illegal
Got error as expected from try(prp(a4, extra = 1.23, main = "extra=1.23"))
> par(old.par)
> 
> par(mfrow=c(3,3))
> prp(a4, type=1,          extra=2,   main="Page 19")
> prp(a4, type=1, under=T, extra=3,   main="extra=3 (misclassification rate)\nunder=T")
> prp(a4, type=1,          extra=102, main="extra=102 (classification rate)\n")
> prp(a4, type=1, under=T, extra=103, main="extra=103 (misclassification rate)\nunder=T")
> par(old.par)
> 
> # anova with under.percent
> anova.model <- rpart(Mileage ~ ., data = cu.summary)
> par(mfrow=c(4,3))
> for(under.percent in 0:2)
+     rpart.plot(anova.model, extra=100, under=FALSE, under.percent=under.percent,
+                main = paste("extra=100 under=FALSE\nunder.percent=", under.percent))
> for(under.percent in 0:2)
+     rpart.plot(anova.model, extra=100, under=TRUE, under.percent=under.percent,
+                main = paste("extra=100 under=TRUE\nunder.percent=", under.percent))
> for(under.percent in 0:2)
+     rpart.plot(anova.model, extra=101, under=FALSE, under.percent=under.percent,
+                main = paste("extra=101 under=FALSE\nunder.percent=", under.percent))
> for(under.percent in 0:2)
+     rpart.plot(anova.model, extra=101, under=TRUE, under.percent=under.percent,
+                main = paste("extra=101 under=TRUE\nunder.percent=", under.percent))
> par(old.par)
> 
> # poisson
> set.seed(8)
> ozone2 <- ozone1
> ozone2$O3a <- round(runif(330, 1, 10))
> y <- cbind(ozone2$O3, ozone2$O3a)
> poisson.mod <- rpart(y~.-O3-O3a, data=ozone2, control=list(cp=.04))
> par(mfrow=c(2, 3))
> plot(poisson.mod, unif=TRUE, branch=.3, main="Page 21"); text(poisson.mod, use.n=TRUE, all=T, digits=3, xpd=NA, cex=1.1)
> prp(poisson.mod, extra=0, digits=3, type=4, trace=1, main="extra=0\ntype=4")
cex 1   xlim c(-0.2, 1.2)   ylim c(-0.55, 1.55)
> prp(poisson.mod, extra=1, type=4, clip.right=FALSE, under=TRUE, main="extra=1: nbr of events, nbr of obs\ntype=4", trace=1, under.cex=1)
cex 1   xlim c(0, 1)   ylim c(-0.35, 1.35)
> prp(poisson.mod, extra=2, trace=1, type=0, under=T, main="extra=2: nbr of events", under.cex=1)
cex 1   xlim c(-0.2, 1.2)   ylim c(-0.75, 1.75)
> prp(poisson.mod, extra=102, type=4, under=TRUE, xsep="/", main="extra=102\ntype=4", trace=1, under.cex=1)
cex 1   xlim c(0, 1)   ylim c(-0.2, 1.2)
> 
> par(mfrow=c(4,3))
> for(under.percent in 0:2)
+     rpart.plot(poisson.mod, extra=100, under=FALSE, under.percent=under.percent,
+                main = paste("extra=100 under=FALSE\nunder.percent=", under.percent))
> for(under.percent in 0:2)
+     rpart.plot(poisson.mod, extra=100, under=TRUE, under.percent=under.percent,
+                main = paste("extra=100 under=TRUE\nunder.percent=", under.percent))
> for(under.percent in 0:2)
+     rpart.plot(poisson.mod, extra=101, under=FALSE, under.percent=under.percent,
+                main = paste("extra=101 under=FALSE\nunder.percent=", under.percent))
> for(under.percent in 0:2)
+     rpart.plot(poisson.mod, extra=101, under=TRUE, under.percent=under.percent,
+                main = paste("extra=101 under=TRUE\nunder.percent=", under.percent))
> 
> par(old.par)
> 
> # prefix, suffix, etc.
> a7 <- rpart(survived~., data=ptitanic, control=list(cp=.02))
> par(mfrow=c(2, 2))
> # test many parameters, and their vectorization
> prp(a7, main="Page 23", Margin=.03,
+     extra=4, under=T, prefix="res:", suffix=" (probs)", split.suffix="\n\nabc", faclen=0, trace=3,
+     nn=1,
+     under.col=c(2,3), under.font=c(3,2), under.ygap=c(.2,-.2), under.cex=c(1.1, .8),
+     adj=c(0,.5), split.adj=c(.5,1), yshift=c(-.5,.5),
+     shadow.col=c(1,2), split.shadow.col=c("pink","blue"),
+     space=c(.8,.6), yspace=c(.5,.1), border.col=c("gray", "green3", "pink"),
+     lty=c(1,2),
+     shadow.offset=c(.4,1,2),
+     split.shadow.offset=c(.4,.4,1),
+     nn.font=c(1,3), nn.adj=c(1,0), nn.col=c(1,2), nn.border.col=c(0,1,2))
head(model.frame):
  survived pclass    sex     age sibsp parch
1 survived    1st female 29.0000     0     0
2 survived    1st   male  0.9167     1     2
3     died    1st female  2.0000     1     2
4     died    1st   male 30.0000     1     2
5     died    1st female 25.0000     1     2
6 survived    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: sibsp parch 
initial scale 1.06
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
clipped scale 1.06 to max.auto.cex 1
cex 1   xlim c(0, 1)   ylim c(0, 1)
> 
> my.split.labs <- function(x, labs, digits, varlen, faclen)
+ {
+     sprint("my.split.lab\n%s", labs)
+ }
> prp(a7, type=4, extra=4, under=T,
+     faclen=0, trace=3,
+     split.fun=my.split.labs,
+     split.prefix="L[", split.suffix="]L",
+     right.split.prefix="R[", right.split.suffix="]R",
+     round=9, leaf.round=0,
+     ycompress.cex=.8) # force ycompress for testing with type=4
head(model.frame):
  survived pclass    sex     age sibsp parch
1 survived    1st female 29.0000     0     0
2 survived    1st   male  0.9167     1     2
3     died    1st female  2.0000     1     2
4     died    1st   male 30.0000     1     2
5     died    1st female 25.0000     1     2
6 survived    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.849
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.849   xlim c(0, 1)   ylim c(0, 1)
> 
> prp(a7, type=4, extra=1, under=F, prefix="response:",
+     suffix="\n\n (probs)", split.suffix="\n\nabc", faclen=0, trace=3)
head(model.frame):
  survived pclass    sex     age sibsp parch
1 survived    1st female 29.0000     0     0
2 survived    1st   male  0.9167     1     2
3     died    1st female  2.0000     1     2
4     died    1st   male 30.0000     1     2
5     died    1st female 25.0000     1     2
6 survived    1st   male 48.0000     0     0
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.738
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.738   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> data(iris)
> a.iris <- rpart(Species~., data=iris)
> par(mfrow=c(2, 2))
> old.bg <- par(bg="gray")
> prp(a.iris, main="Page 24",
+     type=4, extra=1, under=TRUE,
+     col=c("orange", "green", "wheat")[a.iris$frame$yval], under.col="red",
+     border.col=c(3,4), nn.col=c(2,3),
+     split.border.col=5,
+     shadow.col="black",
+     split.shadow.col="lightgray",
+     branch.col=c("orange4", "white"),
+     branch.lwd=c(3,2), branch.lty=1:3)
> rpart.plot(a.iris, main="rpart.plot\ndefault")
> rpart.plot(a.iris, box.palette=0, main="rpart.plot\nbox.palette=0")
> par(bg=old.bg)
> par(old.par)
> 
> par(mfrow=c(2, 3))
> a <- rpart(survived~., data=ptitanic, control=list(cp=.01))
> prp(a, uniform=T, branch=.4, compress=T, extra=104, trace=2, main="Page 25")
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.931
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.931   xlim c(0, 1)   ylim c(0, 1)
> prp(a, uniform=T, branch=.4, compress=T, extra=104, mar=c(1,2,3,4), trace=2, main="test mar=c(1,2,3,4)")
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.816
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.816   xlim c(0, 1)   ylim c(0, 1)
> prp(a, uniform=T, branch=.4, compress=T, extra=104, mar=c(5,2,3,4), trace=2, main="test mar=c(5,2,3,4)")
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.816
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
cex 0.816   xlim c(0, 1)   ylim c(0, 1)
> prp(a, uniform=T, branch=.4, compress=T, extra=104, xpd=T, trace=2, prefix="123456789", cex=1, main="test xpd=T, par=1")
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.562
    node shifter: cex improvement 1     shift.amount 1.5  <new best
    node shifter: cex improvement 0.778 shift.amount 2.0  
shifter: cex improvement 1 best.shift.amount 1.5 best.split.yshift.amount 0 (won't be used)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> prp(a, uniform=T, branch=.4, compress=T, extra=104, xpd=F, trace=2, prefix="123456789", cex=1, main="test xpd=F, par=1")
will apply roundint to the following variables in splits: sibsp parch 
initial scale 0.562
    node shifter: cex improvement 1     shift.amount 1.5  <new best
    node shifter: cex improvement 0.778 shift.amount 2.0  
shifter: cex improvement 1 best.shift.amount 1.5 best.split.yshift.amount 0 (won't be used)
cex 1   xlim c(0, 1)   ylim c(0, 1)
> par(old.par)
> 
> # shadows (also test different values for yesno)
> 
> a <- rpart(pclass ~ ., data=ptitanic, control=rpart.control(cp=.01))
> par(mfrow=c(2,3))
> prp(a, type=0, faclen=0, extra=1, under=F, shadow.col="darkgray", nn=T, yesno=0, split.shadow.col="darkgray", main="Page 26")
> prp(a, type=1, faclen=0, extra=1, under=F, shadow.col="darkgray", nn=T, yesno=1, main="type=1\nyesno=1")
> prp(a, type=1, faclen=0, extra=2, under=T, shadow.col="darkgray", nn=T, yesno=2, main="type=1\nyesno=2")
> prp(a, type=2, faclen=0, extra=3, under=F, shadow.col="darkgray", nn=T, yesno=0, split.shadow.col="darkgray", main="type=2\nyesno=0")
> prp(a, type=3, faclen=0, extra=4, under=T, shadow.col="darkgray", nn=T, yesno=1, split.shadow.col="darkgray", main="type=3\nyesno=1")
> prp(a, type=4, faclen=0, extra=101, under=T, shadow.col="darkgray", nn=T, yesno=2, split.shadow.col="darkgray", main="type=4\nyesno=2")
> par(old.par)
> 
> # misc.
> 
> # test that do.par correctly restores eveything, also test do.par=FALSE
> a <- rpart(pclass ~ ., data=ptitanic, cp=.005)
> par(mfrow=c(3,3))
> temp.par <- par(no.readonly=TRUE)
> prp(a, trace=2, main="Page 27") # trace=2 so can see the grid
will apply roundint to the following variables in splits: sibsp parch 
initial scale 1.33
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
clipped scale 1.33 to max.auto.cex 1
cex 1   xlim c(0, 1)   ylim c(0, 1)
>     # set par settings that can legally change to NULL for comparison
>     temp.par$usr <- temp.par$fig <- temp.par$mfg <- temp.par$xaxp <- temp.par$yaxp <- NULL
>     par <- par(no.readonly=TRUE)
>     par$usr <- par$fig <- par$mfg <- par$xaxp <- par$yaxp <- NULL
>     stopifnot(isTRUE(all.equal(temp.par, par)))
> prp(a, trace=2, main="do.par=FALSE", do.par=FALSE)
will apply roundint to the following variables in splits: sibsp parch 
initial scale 1.06
shifter: cex improvement 1 best.shift.amount 0 best.split.yshift.amount 0 (won't be used)
clipped scale 1.06 to max.auto.cex 1
cex 1   xlim c(0, 1)   ylim c(0, 1)
>     par <- par(no.readonly=TRUE)
>     par$usr <- par$fig <- par$mfg <- par$xaxp <- par$yaxp <- NULL
>     stopifnot(isTRUE(all.equal(temp.par, par)))
> par(old.par)
> 
> # different branch types (also test different values for yesno)
> a <- rpart(pclass ~ ., data=ptitanic, cp=.02)
> par(mfrow=c(2,3))
> prp(a, branch.type=5, yesno=0, main="Page 28")
> prp(a, branch.type=1, yesno=1, main="branch.type=1\ndev  yesno=1")
> prp(a, branch.type=2, yesno=2, main="branch.type=2\nsqrt(dev)\nuniform=FALSE  yesno=2", uniform=FALSE)
> prp(a, branch.type=6, yesno=0, fallen.leaves=T, main="branch.type=6\ncomplexity\nfallen.leaves  yesno=0")
> prp(a, branch.type=7, yesno=1, fallen.leaves=T, main="branch.type=7\nabs(yval)\nfallen.leaves  yesno=1")
> prp(a, branch.type=8, yesno=2, main="branch.type=8\nyval - min(yval)  yesno=2")
> par(old.par)
> 
> par(mfrow=c(2,3))
> # continuous response
> a.age <- rpart(age~., data=ptitanic, cp=.04)
> prp(a.age, branch.type=7, branch.col="pink", main="Page 29")
> 
> # test different types with branch.type
> # prp(a, type=1, branch.type=5, branch.col="slategray3", main="type=1\nbranch.type=5") # already tested
> prp(a, type=2, branch.type=5, branch.col="slategray3", main="type=2\nbranch.type=5\n")
> # prp(a, type=3, branch.type=5, branch.col="slategray3", main="type=1\nbranch.type=5") # not yet supported
> # prp(a, type=4, branch.type=5, branch.col="slategray3", main="type=1\nbranch.type=5") # not yet supported
> prp(a, type=2, branch.type=3, branch=0, branch.col="slategray3", main="type=2\nbranch.type=3\nbranch=0")
> prp(a, type=2, branch.type=4, branch=1, main="type=2\nbranch.type=4\nbranch=1",
+     branch.col=c("slategray","slateblue2","slateblue")[a$frame$yval])
> 
> branch.fun1 <- function(x)
+ {
+     width <- x$frame$wt
+ }
> root <- rpart(survived ~ ., data=ptitanic, cp=.5)
> prp(a, branch.type=branch.fun1, branch.col="slategray3", main="branch.fun1")
> par(old.par)
> 
> par(mfrow=c(2,3))
> prp(root, branch.type=5, main="Page 30")
> prp(a, branch=0, branch.type=5, branch.tweak=1.5, branch.col="slategray3",
+     branch.fill=2, main="branch.type=5\nbranch args")
> par(old.par)
> 
> par(mfrow=c(4,4))
> set.seed(1924)
> root <- rpart(survived ~ ., data=ptitanic, cp=.5)
> temp <- prp(root, main="Page 31")
> print(temp)
$obj
n= 1309 

node), split, n, loss, yval, (yprob)
      * denotes terminal node

1) root 1309 500 died (0.6180290 0.3819710) *

$snipped.nodes
NULL

$xlim
[1] 0 1

$ylim
[1] 0 1

$x
[1] 0.5

$y
[1] 0.5

$branch.x
  [,1]
x  0.5
    NA
    NA

$branch.y
  [,1]
y  0.5
    NA
    NA

$labs
[1] "died"

$cex
[1] 1

$boxes
$boxes$x1
[1] 0.4308005

$boxes$y1
[1] 0.4731421

$boxes$x2
[1] 0.5691995

$boxes$y2
[1] 0.5805736


$split.labs
[1] ""

$split.cex
[1] 1

$split.box
$split.box$x1
[1] NA

$split.box$y1
[1] NA

$split.box$x2
[1] NA

$split.box$y2
[1] NA


> prp(root, type=1, main="type=1")
> prp(root, type=2, extra=1, main="type=2, extra=1")
> prp(root, type=3, extra=2, under=T, main="type=3, extra=4, under=T")
> prp(root, type=4, extra=3, main="type=4, extra=4",
+     prefix="l[", suffix="]r",
+     split.prefix="L[", split.suffix="]L",
+     right.split.prefix="R[", right.split.suffix="]R",
+     round=9, leaf.round=0)
> prp(root, branch.type=5, main="branch.type=5")
> par(old.par)
> 
> # clip.left.labs and clip.right.labs
> par(mfrow=c(3,3))
> prp(tree, type=4, clip.left.labs=F, clip.right.labs=F, main="clip.left.labs=F, clip.right.labs=F")
> # prp(tree, type=4, clip.left.labs=F, clip.right.labs=T, main="clip.left.labs=F, clip.right.labs=T") #default
> prp(tree, type=3, clip.left.labs=T, clip.right.labs=F, main="clip.left.labs=T, clip.right.labs=F")
> prp(tree, type=3, clip.left.labs=T, clip.right.labs=T, main="clip.left.labs=T, clip.right.labs=T")
> 
> prp(tree, type=3, xflip=T, clip.left.labs=F, clip.right.labs=F, main="clip.left.labs=F, clip.right.labs=F\n                 xflip=T")
> prp(tree, type=3, xflip=T, clip.left.labs=F, clip.right.labs=T, main="clip.left.labs=F, clip.right.labs=T\n                 xflip=T")
> prp(tree, type=4, xflip=T, clip.left.labs=T, clip.right.labs=F, main="clip.left.labs=T, clip.right.labs=F\n                 xflip=T")
> prp(tree, type=4, xflip=T, clip.left.labs=T, clip.right.labs=T, main="clip.left.labs=T, clip.right.labs=T\n                 xflip=T")
> 
> prp(tree, type=4,          clip.left.labs=c(T, F, T), clip.right.labs=c(T, F, F), main="clip.labs vectorization")
> prp(tree, type=4, xflip=T, clip.left.labs=c(T, F, T), clip.right.labs=F,          main="xflip=T\nclip.labs vectorization")
> par(mfrow=c(1,1))
> par(old.par)
> 
> # TODO mvpart is no longer on CRAN
> #
> # # mvpart, must be last because it changes plot.rpart, text.rpart, etc.
> # library(mvpart)
> # data(spider)
> # par(mfrow=c(3,3))
> # a <- mvpart(data.matrix(spider[,1:12])~twigs+water,spider, legend=FALSE, all=TRUE)
> # prp(a, fallen=T, branch=1, under=T, type=0, extra=0, main="mvpart page 1\nnresp=12, extra=0")
> # prp(a, fallen=T, under=T, type=1, extra=2, main="nresp=12, extra=2, under=T", under.cex=1)
> # a <- mvpart(data.matrix(spider[,1:3])~twigs+water,spider, legend=FALSE, all=TRUE)
> # prp(a, under=T, type=1, extra=101, main="extra=101")
> # prp(a, under=T, type=2, extra=102, main="extra=102")
> # prp(a, under=T, type=4, extra=3,   main="extra=3, under=F")
> # prp(a, under=T, type=1, extra=4,   main="extra=4")
> # prp(a, under=T, type=1, extra=105, main="extra=105")
> #
> # prp(a, under=F, type=4, extra=106, main="mvpart page 2\nextra=106, under=F")
> # prp(a, under=T, type=4, extra=107, main="extra=107")
> # prp(a, under=T, type=1, extra=8,   main="extra=8")
> # prp(a, under=F, type=2, extra=109, main="extra=109, under=F")
> # prp(a, under=T, type=3, extra=110, main="extra=110")
> # prp(a, under=T, type=4, extra=111, main="extra=111")
> # par(old.par)
> 
> # # TODO this seems to not work with the new version of rpart (4.0.2)
> # library(rpart.plot)
> # library(rpartOrdinal)
> # library(rpartScore)
> # data(lowbwt)
> # lowbwt <- lowbwt[1:80,]
> # lowbwt$Category.s <-
> #     ifelse(lowbwt$bwt <= 2500, 3,
> #     ifelse(lowbwt$bwt <= 3000, 2,
> #     ifelse(lowbwt$bwt <= 3500, 1,
> #                                0)))
> # # Gives error
> # a <- rpartScore(Category.s ~ age + lwt + race + smoke +
> #                 ptl + ht + ui + ftv, data = lowbwt)
> # prp(a, extra=100, main="rpartScore\nextra=100", under=TRUE)
> 
> # Cannot install rpartOrdinal: package 'rpartOrdinal' is not available (for R version 3.2.0)
> # library(rpartOrdinal)
> # data(lowbwt)
> # lowbwt$Category <- factor(
> #     ifelse(lowbwt$bwt<=2500,3,
> #     ifelse(lowbwt$bwt<=3000,2,
> #     ifelse(lowbwt$bwt<=3500,1,
> #                             0))),ordered=TRUE)
> # a <- rpart(Category~age+lwt+race+smoke+ptl+ht+ui+ftv,data=lowbwt,method=ordinal)
> # prp(a, main="rpartOrdinal\ntype=1, extra=0", type=1, extra=0, faclen=0)
> 
> # TODO mvpart is no longer on CRAN
> #
> # #--- appendix mvpart.R  ---
> #
> # library(mvpart)
> # library(rpart.plot)
> # data(spider)
> # set.seed(1)
> # response <- data.matrix(spider[,1:3, drop=F])
> # tree1 <- mvpart(response~herbs+reft+moss+sand+twigs+water, data=spider,
> #             legend=F, method="mrt", plot.add=F, xv="min")
> #
> # par(par(mfrow=c(4,4)), mar = c(3, 3, 3, 1), mgp = c(1.5, .5, 0))
> # prp1 <- function(tree1, extra, main, type=1, under=T, col=1, yesno=F, tweak=1,
> #                  col.main="skyblue4", cex.main=1, ...)
> # {
> #     prp(tree1, type=type, extra=extra, main=main,
> #         under=under, col=col, yesno=yesno, tweak=tweak,
> #         col.main=col.main, cex.main=cex.main, ...)
> # }
> # prp1(tree1, extra=0, main="extra = 0\ndev", tweak=.8)
> # prp1(tree1, extra=1, type=3, main="extra = 1 (type=3)\ndev,  n")
> # prp1(tree1, extra=2, main="extra = 2\ndev,  frac", tweak=1.2)
> # prp1(tree1, extra=3, main="extra = 3\ndev,  frac / sum(frac)")
> # prp1(tree1, extra=4, main="extra = 4\nsqrt(dev)")
> # prp1(tree1, extra=5, main="extra = 5\nsqrt(dev),  n")
> # prp1(tree1, extra=6, main="extra = 6\nsqrt(dev),  frac", tweak=1.2)
> # prp1(tree1, extra=7, main="extra = 7\nsqrt(dev),  frac / sum(frac)", tweak=1.1)
> # prp1(tree1, extra=8, main="extra = 8\npredom species",   tweak=.8)
> # prp1(tree1, extra=9, main="extra = 9\npredom species,  n", tweak=1)
> # prp1(tree1, extra=10, main="extra = 10\npredom species,  frac", tweak=1.2)
> # prp1(tree1, extra=11, main="extra = 11\npredom spec,  frac / sum(frac)", tweak=1.15)
> # par(old.par)
> 
> # test rpart.plot version 1.5.3 (deal with situation where user has
> # a variable named text in the current environment).
> # Also test use of FUN argument.
> 
> cat("\ntest rpart.plot version 1.5.3\n")

test rpart.plot version 1.5.3
> par(mfrow=c(3,3))
> a100 <- rpart(survived ~ ., data=ptitanic, cp=.02)
> title("a100a", cex=.6)
> prp(a100)
> title("a100b", cex=.6)
> text <- "this is not the text function"
> prp(a100) # graph should be identical to the one on its left
> title("a100c", cex=.6)
> expect.err(try(prp(a100, FUN=function(xbad, y1, labels, ...) text(xbad, y1, labels, ...))),
+            "the FUN argument to the prp function needs the following arguments")
Error : the FUN argument to the prp function needs the following arguments:
    x ...
You have:
    xbad y1 labels ...
Got error as expected from try(prp(a100, FUN = function(xbad, y1, labels, ...) text(xbad,     y1, labels, ...)))
> title("a100d", cex=.6)
> # user specified FUN only has to match up to the dots
> prp(a100, FUN=function(x, y1, labels, ...) text(x, y1, labels, ...))
> 
> my.text <- function(x, y, labels, ...) text(x, y, labels, ...)
> prp(a100, FUN=my.text)
> title("a100e", cex=.6)
> my.bad.text <- function(xbad, y, labels, ...) text(xbad, y, labels, ...)
> expect.err(try(prp(a100, FUN=my.bad.text)),
+            "the FUN argument to the prp function needs the following arguments")
Error : the FUN argument to the prp function needs the following arguments:
    x ...
You have:
    xbad y labels ...
Got error as expected from try(prp(a100, FUN = my.bad.text))
> title("a100f", cex=.6)
> # define the function text in the global environment and use that
> text <- function(x, y, labels, ...) graphics::text(x, y, paste0("my-", labels), ...)
> # TODO boxes below aren't sized correctly for the user generated text
> prp(a100, FUN=text)
> title("a100g", cex=.6)
> remove(text)
> par(old.par)
> 
> source("test.epilog.R")
