> # test.rpart.rules.R (also tests rpart.predict and roundint argument)
> 
> source("test.prolog.R")
Loading required package: rpart
> old.width <- options("width")
> 
> example(rpart.rules)

rprt.r> data(ptitanic)

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

rprt.r> rpart.plot(model)

rprt.r> rpart.rules(model)
 survived                                             
     0.05 when sex is   male & age <  9.5 & sibsp >= 3
     0.17 when sex is   male & age >= 9.5             
     0.73 when sex is female                          
     0.89 when sex is   male & age <  9.5 & sibsp <  3
> title("example(rpart.rules)")
> example(print.rpart.rules)

prnt..> data(ptitanic)

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

prnt..> rpart.plot(model)

prnt..> rpart.rules(model)
 survived                                             
     0.05 when sex is   male & age <  9.5 & sibsp >= 3
     0.17 when sex is   male & age >= 9.5             
     0.73 when sex is female                          
     0.89 when sex is   male & age <  9.5 & sibsp <  3
> title("example(print.rpart.rules)")
> 
> # test rpart.model.frame with good and bad data
> par(mfrow=c(2, 2))
> data(trees)
> trees1 <- trees * 10 # so all values integral
> Volume <- rpart(formula=Volume~Girth, data=trees1, cp=.001)
> rules <- rpart.rules(Volume, digits=4)
> rpart.plot(Volume, digits=4, main="Volume\ndata still available")
> print(rules)
 Volume                         
  179.3 when Girth <  125       
  305.3 when Girth is 125 to 162
  559.3 when Girth >=        162
> # can't use digits in print.rpart.rules (instead must specify digits in rpart.rules)
> expect.err(try(print(rpart.rules(Volume), digits=4)), "specify 'digits' in rpart.rules (not in print.rpart.rules)")
Error : specify 'digits' in rpart.rules (not in print.rpart.rules)
Got error as expected from try(print(rpart.rules(Volume), digits = 4))
> expect.err(try(print(rpart.rules(Volume), digit=4)),  "specify 'digits' in rpart.rules (not in print.rpart.rules)")
Error : specify 'digits' in rpart.rules (not in print.rpart.rules)
Got error as expected from try(print(rpart.rules(Volume), digit = 4))
> expect.err(try(print(rpart.rules(Volume), dig=4)),    "specify 'digits' in rpart.rules (not in print.rpart.rules)")
Error : specify 'digits' in rpart.rules (not in print.rpart.rules)
Got error as expected from try(print(rpart.rules(Volume), dig = 4))
> trees1 <- "bad data"
> options(warn=2) # treat warnings as errors
> expect.err(try(rpart.rules(Volume, digits=4)), "(converted from warning) Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).")
Error : (converted from warning) Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
Got error as expected from try(rpart.rules(Volume, digits = 4))
> expect.err(try(rpart.plot(Volume, digits=4, main="Volume\ntrees not available")), "Cannot retrieve the data used to build the model")
Error : (converted from warning) Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).
To silence this warning:
    Call rpart.plot with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
Got error as expected from try(rpart.plot(Volume, digits = 4, main = "Volume\ntrees not available"))
> expect.err(try(prp(Volume, digits=4, main="Volume\ntrees not available")), "Cannot retrieve the data used to build the model")
Error : (converted from warning) Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).
To silence this warning:
    Call prp with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
Got error as expected from try(prp(Volume, digits = 4, main = "Volume\ntrees not available"))
> rpart.plot(Volume, digits=4, roundint=FALSE, main="Volume roundint=FALSE\ndata not available")
> trees1 <- trees[,1:2] # only part of the data
> expect.err(try(rpart.rules(Volume, digits=4)), "(converted from warning) Cannot retrieve the data used to build the model (model.frame: invalid type (list) for variable 'Volume').")
Error : (converted from warning) Cannot retrieve the data used to build the model (model.frame: invalid type (list) for variable 'Volume').
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
Got error as expected from try(rpart.rules(Volume, digits = 4))
> print(rpart.rules(Volume, digits=4, roundint=FALSE, trace=.5)) # trace print: Cannot retrieve the data used to build the model (model.frame: invalid type (list) for variable 'Volume')
Cannot retrieve the data used to build the model (model.frame: invalid type (list) for variable 'Volume')
 Volume                             
  179.3 when Girth <  124.5         
  305.3 when Girth is 124.5 to 161.5
  559.3 when Girth >=          161.5
> trees1 <- trees[,2:3] # only part of the data
> print(rpart.rules(Volume, digits=4, roundint=FALSE, trace=.5)) # trace print: Cannot retrieve the data used to build the model (model.frame: object 'Girth' not found)
Cannot retrieve the data used to build the model (model.frame: object 'Girth' not found)
 Volume                             
  179.3 when Girth <  124.5         
  305.3 when Girth is 124.5 to 161.5
  559.3 when Girth >=          161.5
> 
> # test with rpart argument model=TRUE (also use space in predictor name for testing node label parsing)
> trees1 <- trees * 10 # so all values integral
> trees1[["Girth Around"]] <- trees1$Girth # space in predictor name
> trees1$Girth <- NULL
> options(warn=1) # print warnings as they occur (rpart gives a warning partial argument match of 'length' to 'length.out')
> Volume.modTRUE <- rpart(formula=Volume~., data=trees1, cp=.001, model=TRUE)
> options(warn=2) # treat warnings as errors
> rules <- rpart.rules(Volume.modTRUE, digits=4)
> print(rules)
 Volume                                
  179.3 when Girth Around <  125       
  305.3 when Girth Around is 125 to 162
  559.3 when Girth Around >=        162
> trees1 <- "bad data"
> print(rpart.rules(Volume.modTRUE, digits=4)) # ok, trees1 is ignored
 Volume                                
  179.3 when Girth Around <  125       
  305.3 when Girth Around is 125 to 162
  559.3 when Girth Around >=        162
> rpart.plot(Volume.modTRUE, digits=4, main="Volume.modTRUE\ndata not available")
> rpart.plot(Volume.modTRUE, digits=4, roundint=FALSE, main="Volume.modTRUE roundint=FALSE\ndata not available")
> par(old.par)
> 
> cat0("\n=== test digits, varlen, faclen, trace ===\n\n")

=== test digits, varlen, faclen, trace ===

> 
> options(warn=1) # print warnings as they occur (rpart gives a warning: partial argument match of 'length' to 'length.out')
> mileage <- rpart(Mileage ~ ., data=cu.summary)
> options(warn=2) # treat warnings as errors
> 
> options(warn=1234) # for testing that rpart.rules doesn't mess up options(warn)
> print(rpart.rules(mileage))
 Mileage                                                                 
      19 when Price >=          9447 & Type is               Large or Van
      22 when Price >=          9447 & Type is                     Medium
      24 when Price >=         11485 & Type is Compact or Small or Sporty
      25 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
      32 when Price <  9447                                              
> stopifnot(options("warn") == 1234) # make sure rpart.rules didn't mess up options(warn)
> options(warn=1) # print warnings as they occur
> print(rpart.rules(mileage, digits=0, trace=1)) # this also tests print(rules)

iframe   4 node   8 path c("root", "Price >= 9447", "Type = Large or Medium or Van", "Type = Large or Van")
iframe   5 node   9 path c("root", "Price >= 9447", "Type = Large or Medium or Van", "Type = Medium")
iframe   7 node  10 path c("root", "Price >= 9447", "Type = Compact or Small or Sporty", "Price >= 11485")
iframe   8 node  11 path c("root", "Price >= 9447", "Type = Compact or Small or Sporty", "Price < 11485")
iframe   9 node   3 path c("root", "Price < 9447")

raw rules:
   lab         fit iclass       cover Price= Price< Price>=                      Type= Type< Type>= Reliability= Reliability< Reliability>= Country= Country< Country>=
8             19.3     19 16.66666667                  9447               Large or Van                                                                                 
9      21.76923077     21 21.66666667                  9447                     Medium                                                                                 
10     23.85714286     23 23.33333333                 11485 Compact or Small or Sporty                                                                                 
11     25.45454545     25 18.33333333         11485    9447 Compact or Small or Sporty                                                                                 
3      32.08333333     32 20.00000000          9447                                                                                                                    

processed rules:
    Mileage                                                                 
8  19.30000 when Price >=          9447 & Type is               Large or Van
9  21.76923 when Price >=          9447 & Type is                     Medium
10 23.85714 when Price >=         11485 & Type is Compact or Small or Sporty
11 25.45455 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
3  32.08333 when Price <  9447                                              

  Mileage                                                                 
 19.30000 when Price >=          9447 & Type is               Large or Van
 21.76923 when Price >=          9447 & Type is                     Medium
 23.85714 when Price >=         11485 & Type is Compact or Small or Sporty
 25.45455 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
 32.08333 when Price <  9447                                              
> print(rpart.rules(mileage, digits=2, trace=2))
will apply roundint to the following variables in splits: Mileage Price 

iframe   4 node   8 path c("root", "Price >= 9447", "Type = Large or Medium or Van", "Type = Large or Van")
                split.lab "Price >= 9447"      varname Price op >= cut 9447
                split.lab "Type = Large or Med varname Type op = cut Large or Medium or Van
                split.lab "Type = Large or Van varname Type op = cut Large or Van
iframe   5 node   9 path c("root", "Price >= 9447", "Type = Large or Medium or Van", "Type = Medium")
                split.lab "Price >= 9447"      varname Price op >= cut 9447
                split.lab "Type = Large or Med varname Type op = cut Large or Medium or Van
                split.lab "Type = Medium"      varname Type op = cut Medium
iframe   7 node  10 path c("root", "Price >= 9447", "Type = Compact or Small or Sporty", "Price >= 11485")
                split.lab "Price >= 9447"      varname Price op >= cut 9447
                split.lab "Type = Compact or S varname Type op = cut Compact or Small or Sporty
                split.lab "Price >= 11485"     varname Price op >= cut 11485
iframe   8 node  11 path c("root", "Price >= 9447", "Type = Compact or Small or Sporty", "Price < 11485")
                split.lab "Price >= 9447"      varname Price op >= cut 9447
                split.lab "Type = Compact or S varname Type op = cut Compact or Small or Sporty
                split.lab "Price < 11485"      varname Price op < cut 11485
iframe   9 node   3 path c("root", "Price < 9447")
                split.lab "Price < 9447"       varname Price op < cut 9447

raw rules:
   lab         fit iclass       cover Price= Price< Price>=                      Type= Type< Type>= Reliability= Reliability< Reliability>= Country= Country< Country>=
8             19.3     19 16.66666667                  9447               Large or Van                                                                                 
9      21.76923077     21 21.66666667                  9447                     Medium                                                                                 
10     23.85714286     23 23.33333333                 11485 Compact or Small or Sporty                                                                                 
11     25.45454545     25 18.33333333         11485    9447 Compact or Small or Sporty                                                                                 
3      32.08333333     32 20.00000000          9447                                                                                                                    

processed rules:
   Mileage                                                                 
8       19 when Price >=          9447 & Type is               Large or Van
9       22 when Price >=          9447 & Type is                     Medium
10      24 when Price >=         11485 & Type is Compact or Small or Sporty
11      25 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
3       32 when Price <  9447                                              

 Mileage                                                                 
      19 when Price >=          9447 & Type is               Large or Van
      22 when Price >=          9447 & Type is                     Medium
      24 when Price >=         11485 & Type is Compact or Small or Sporty
      25 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
      32 when Price <  9447                                              
> print(rpart.rules(mileage, digits=3))
 Mileage                                                                 
    19.3 when Price >=          9447 & Type is               Large or Van
    21.8 when Price >=          9447 & Type is                     Medium
    23.9 when Price >=         11485 & Type is Compact or Small or Sporty
    25.5 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
    32.1 when Price <  9447                                              
> print(rpart.rules(mileage, digits=10))
   Mileage                                                                 
 19.300000 when Price >=          9447 & Type is               Large or Van
 21.769231 when Price >=          9447 & Type is                     Medium
 23.857143 when Price >=         11485 & Type is Compact or Small or Sporty
 25.454545 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
 32.083333 when Price <  9447                                              
> print(rpart.rules(mileage, digits=-10)) # should be same as digits=10
   Mileage                                                                 
 19.300000 when Price >=          9447 & Type is               Large or Van
 21.769231 when Price >=          9447 & Type is                     Medium
 23.857143 when Price >=         11485 & Type is Compact or Small or Sporty
 25.454545 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
 32.083333 when Price <  9447                                              
> stopifnot(identical(rpart.rules(mileage, digits=-10), rpart.rules(mileage, digits=10)))
> expect.err(try(rpart.rules(mileage, digits=99)), "digits=99 but it should be between -22 and 22")
Error : digits=99 but it should be between -22 and 22
Got error as expected from try(rpart.rules(mileage, digits = 99))
> 
> print(rpart.rules(mileage, faclen=1))
 Mileage                                          
      19 when Price >=          9447 & Type is  bf
      22 when Price >=          9447 & Type is   c
      24 when Price >=         11485 & Type is ade
      25 when Price is 9447 to 11485 & Type is ade
      32 when Price <  9447                       
> print(rpart.rules(mileage, faclen=2))
 Mileage                                                     
      19 when Price >=          9447 & Type is       Lr or Vn
      22 when Price >=          9447 & Type is             Md
      24 when Price >=         11485 & Type is Cm or Sm or Sp
      25 when Price is 9447 to 11485 & Type is Cm or Sm or Sp
      32 when Price <  9447                                  
> print(rpart.rules(mileage, faclen=-1))
 Mileage                                                     
      19 when Price >=          9447 & Type is       La or Va
      22 when Price >=          9447 & Type is             Me
      24 when Price >=         11485 & Type is Co or Sm or Sp
      25 when Price is 9447 to 11485 & Type is Co or Sm or Sp
      32 when Price <  9447                                  
> print(rpart.rules(mileage, faclen=-2, trace=TRUE))

iframe   4 node   8 path c("root", "Price >= 9447", "Type = La or Me or Va", "Type = La or Va")
iframe   5 node   9 path c("root", "Price >= 9447", "Type = La or Me or Va", "Type = Me")
iframe   7 node  10 path c("root", "Price >= 9447", "Type = Co or Sm or Sp", "Price >= 11485")
iframe   8 node  11 path c("root", "Price >= 9447", "Type = Co or Sm or Sp", "Price < 11485")
iframe   9 node   3 path c("root", "Price < 9447")

raw rules:
   lab         fit iclass       cover Price= Price< Price>=          Type= Type< Type>= Reliability= Reliability< Reliability>= Country= Country< Country>=
8             19.3     19 16.66666667                  9447       La or Va                                                                                 
9      21.76923077     21 21.66666667                  9447             Me                                                                                 
10     23.85714286     23 23.33333333                 11485 Co or Sm or Sp                                                                                 
11     25.45454545     25 18.33333333         11485    9447 Co or Sm or Sp                                                                                 
3      32.08333333     32 20.00000000          9447                                                                                                        

processed rules:
   Mileage                                                     
8       19 when Price >=          9447 & Type is       La or Va
9       22 when Price >=          9447 & Type is             Me
10      24 when Price >=         11485 & Type is Co or Sm or Sp
11      25 when Price is 9447 to 11485 & Type is Co or Sm or Sp
3       32 when Price <  9447                                  

 Mileage                                                     
      19 when Price >=          9447 & Type is       La or Va
      22 when Price >=          9447 & Type is             Me
      24 when Price >=         11485 & Type is Co or Sm or Sp
      25 when Price is 9447 to 11485 & Type is Co or Sm or Sp
      32 when Price <  9447                                  
> print(rpart.rules(mileage, faclen=-4))
 Mileage                                                           
      19 when Price >=          9447 & Type is          Larg or Van
      22 when Price >=          9447 & Type is                 Medi
      24 when Price >=         11485 & Type is Comp or Smal or Spor
      25 when Price is 9447 to 11485 & Type is Comp or Smal or Spor
      32 when Price <  9447                                        
> print(rpart.rules(mileage, faclen=-4, clip.facs=TRUE))
 Mileage                                                   
      19 when Price >=          9447 &          Larg or Van
      22 when Price >=          9447 &                 Medi
      24 when Price >=         11485 & Comp or Smal or Spor
      25 when Price is 9447 to 11485 & Comp or Smal or Spor
      32 when Price <  9447                                
> 
> # rpart.predict (anova model)
> stopifnot(identical(options("width"), old.width))
> owidth <- options("width")
> options(width=1e3)
> print(predict(mileage)[1:5])
 Eagle Summit 4 Ford Escort   4  Ford Festiva 4   Honda Civic 4 Mazda Protege 4 
       32.08333        32.08333        32.08333        32.08333        32.08333 
> print(rpart.predict(mileage)[1:5])
 Eagle Summit 4 Ford Escort   4  Ford Festiva 4   Honda Civic 4 Mazda Protege 4 
       32.08333        32.08333        32.08333        32.08333        32.08333 
> print(head(rpart.predict(mileage, rules=TRUE, when="")))
                  Mileage                                                                    
Eagle Summit 4   32.08333 because Price <  9447                                              
Ford Escort   4  32.08333 because Price <  9447                                              
Ford Festiva 4   32.08333 because Price <  9447                                              
Honda Civic 4    32.08333 because Price <  9447                                              
Mazda Protege 4  32.08333 because Price <  9447                                              
Mercury Tracer 4 32.08333 because Price <  9447                                              
> print(head(rpart.predict(mileage, rules=TRUE, when="WHEN")))
                  Mileage                                                                    
Eagle Summit 4   32.08333 because Price <  9447                                              
Ford Escort   4  32.08333 because Price <  9447                                              
Ford Festiva 4   32.08333 because Price <  9447                                              
Honda Civic 4    32.08333 because Price <  9447                                              
Mazda Protege 4  32.08333 because Price <  9447                                              
Mercury Tracer 4 32.08333 because Price <  9447                                              
> print(head(rpart.predict(mileage, rules=TRUE, when="", because="reason:")))
                  Mileage                                                                    
Eagle Summit 4   32.08333 reason: Price <  9447                                              
Ford Escort   4  32.08333 reason: Price <  9447                                              
Ford Festiva 4   32.08333 reason: Price <  9447                                              
Honda Civic 4    32.08333 reason: Price <  9447                                              
Mazda Protege 4  32.08333 reason: Price <  9447                                              
Mercury Tracer 4 32.08333 reason: Price <  9447                                              
> print(head(rpart.predict(mileage, rules=TRUE, because="")))
                  Mileage                                                            
Eagle Summit 4   32.08333 Price <  9447                                              
Ford Escort   4  32.08333 Price <  9447                                              
Ford Festiva 4   32.08333 Price <  9447                                              
Honda Civic 4    32.08333 Price <  9447                                              
Mazda Protege 4  32.08333 Price <  9447                                              
Mercury Tracer 4 32.08333 Price <  9447                                              
> stopifnot(identical(predict(mileage), rpart.predict(mileage)))
> print(rpart.predict(mileage, rules=TRUE)[12:14,])
                     Mileage                                                                    
Toyota Tercel 4     32.08333 because Price <  9447                                              
Volkswagen Jetta 4  25.45455 because Price is 9447 to 11485 & Type is Compact or Small or Sporty
Chevrolet Camaro V8 23.85714 because Price >=         11485 & Type is Compact or Small or Sporty
> # use max and not identical because rpart.predict(mileage, rules=TRUE)[,1] doesn't have names
> stopifnot(max(abs(predict(mileage) - rpart.predict(mileage, rules=TRUE)[,1])) == 0)
> stopifnot(identical(as.numeric(predict(mileage)), rpart.predict(mileage, rules=TRUE)[,1]))
> print(rpart.predict(mileage, nn=TRUE)[12:14,])
                     Mileage nn
Toyota Tercel 4     32.08333  3
Volkswagen Jetta 4  25.45455 11
Chevrolet Camaro V8 23.85714 10
> stopifnot(max(abs(predict(mileage) - rpart.predict(mileage, nn=TRUE)[,1])) == 0)
> print(rpart.predict(mileage, nn=TRUE, rules=TRUE)[12:14,])
                     Mileage nn                                                                    
Toyota Tercel 4     32.08333  3 because Price <  9447                                              
Volkswagen Jetta 4  25.45455 11 because Price is 9447 to 11485 & Type is Compact or Small or Sporty
Chevrolet Camaro V8 23.85714 10 because Price >=         11485 & Type is Compact or Small or Sporty
> stopifnot(max(abs(predict(mileage) - rpart.predict(mileage, nn=TRUE, rules=TRUE)[,1])) == 0)
> options(width=owidth$width)
> stopifnot(identical(options("width"), old.width))
> 
> # test negative response and predictor values
> cu.summary2 <- cu.summary
> cu.summary2$Mileage <- -cu.summary2$Mileage
> cu.summary2$Price   <- -cu.summary2$Price
> mileage.negative <- rpart(Mileage ~ ., data=cu.summary2)
> cat0("mileage.negative (negative response and predictor values)\n")
mileage.negative (negative response and predictor values)
> print(rpart.rules(mileage.negative))
 Mileage                                                                   
     -32 when Price >=           -9446                                     
     -25 when Price is -11484 to -9446 & Type is Compact or Small or Sporty
     -24 when Price <  -11484          & Type is Compact or Small or Sporty
     -22 when Price <   -9446          & Type is                     Medium
     -19 when Price <   -9446          & Type is               Large or Van
> print(rpart.rules(mileage.negative, varlen=2, faclen=-3, digits=3))
 Mileage                                                     
   -32.1 when Pr >=           -9446                          
   -25.5 when Pr is -11484 to -9446 & Ty is Com or Sma or Spo
   -23.9 when Pr <  -11484          & Ty is Com or Sma or Spo
   -21.8 when Pr <   -9446          & Ty is               Med
   -19.3 when Pr <   -9446          & Ty is        Lar or Van
> 
> data(ptitanic)
> age <- rpart(age ~ ., data=ptitanic)
> 
> print(rpart.rules(age, varlen=0))
  age                                                                                                
  7.1 when pclass is 2nd or 3rd & parch is 1 to 3 & sibsp <  2 & survived is survived & sex is   male
  9.6 when pclass is 2nd or 3rd & parch is 1 to 3 & sibsp >= 2                                       
 20.6 when pclass is 2nd or 3rd & parch is 1 to 3 & sibsp <  2 & survived is survived & sex is female
 26.1 when pclass is 2nd or 3rd & parch is 1 to 3 & sibsp <  2 & survived is     died                
 27.4 when pclass is        3rd & parch <  1                                                         
 27.8 when pclass is        1st & parch >=      2                                                    
 32.1 when pclass is        2nd & parch <  1                                                         
 37.4 when pclass is 2nd or 3rd & parch >=      3                                                    
 40.5 when pclass is        1st & parch <  2                                                         
> print(rpart.rules(age, varlen=1))
  age                                                                               
  7.1 when pc is 2nd or 3rd & pr is 1 to 3 & sb <  2 & sr is survived & sx is   male
  9.6 when pc is 2nd or 3rd & pr is 1 to 3 & sb >= 2                                
 20.6 when pc is 2nd or 3rd & pr is 1 to 3 & sb <  2 & sr is survived & sx is female
 26.1 when pc is 2nd or 3rd & pr is 1 to 3 & sb <  2 & sr is     died               
 27.4 when pc is        3rd & pr <  1                                               
 27.8 when pc is        1st & pr >=      2                                          
 32.1 when pc is        2nd & pr <  1                                               
 37.4 when pc is 2nd or 3rd & pr >=      3                                          
 40.5 when pc is        1st & pr <  2                                               
> print(rpart.rules(age, varlen=2))
  age                                                                               
  7.1 when pc is 2nd or 3rd & pr is 1 to 3 & sb <  2 & sr is survived & sx is   male
  9.6 when pc is 2nd or 3rd & pr is 1 to 3 & sb >= 2                                
 20.6 when pc is 2nd or 3rd & pr is 1 to 3 & sb <  2 & sr is survived & sx is female
 26.1 when pc is 2nd or 3rd & pr is 1 to 3 & sb <  2 & sr is     died               
 27.4 when pc is        3rd & pr <  1                                               
 27.8 when pc is        1st & pr >=      2                                          
 32.1 when pc is        2nd & pr <  1                                               
 37.4 when pc is 2nd or 3rd & pr >=      3                                          
 40.5 when pc is        1st & pr <  2                                               
> print(rpart.rules(age, varlen=3))
  age                                                                                    
  7.1 when pcl is 2nd or 3rd & prc is 1 to 3 & sbs <  2 & srv is survived & sex is   male
  9.6 when pcl is 2nd or 3rd & prc is 1 to 3 & sbs >= 2                                  
 20.6 when pcl is 2nd or 3rd & prc is 1 to 3 & sbs <  2 & srv is survived & sex is female
 26.1 when pcl is 2nd or 3rd & prc is 1 to 3 & sbs <  2 & srv is     died                
 27.4 when pcl is        3rd & prc <  1                                                  
 27.8 when pcl is        1st & prc >=      2                                             
 32.1 when pcl is        2nd & prc <  1                                                  
 37.4 when pcl is 2nd or 3rd & prc >=      3                                             
 40.5 when pcl is        1st & prc <  2                                                  
> print(rpart.rules(age, varlen=-1))
  age                                                                               
  7.1 when pc is 2nd or 3rd & pa is 1 to 3 & si <  2 & su is survived & se is   male
  9.6 when pc is 2nd or 3rd & pa is 1 to 3 & si >= 2                                
 20.6 when pc is 2nd or 3rd & pa is 1 to 3 & si <  2 & su is survived & se is female
 26.1 when pc is 2nd or 3rd & pa is 1 to 3 & si <  2 & su is     died               
 27.4 when pc is        3rd & pa <  1                                               
 27.8 when pc is        1st & pa >=      2                                          
 32.1 when pc is        2nd & pa <  1                                               
 37.4 when pc is 2nd or 3rd & pa >=      3                                          
 40.5 when pc is        1st & pa <  2                                               
> print(rpart.rules(age, varlen=-2))
  age                                                                               
  7.1 when pc is 2nd or 3rd & pa is 1 to 3 & si <  2 & su is survived & se is   male
  9.6 when pc is 2nd or 3rd & pa is 1 to 3 & si >= 2                                
 20.6 when pc is 2nd or 3rd & pa is 1 to 3 & si <  2 & su is survived & se is female
 26.1 when pc is 2nd or 3rd & pa is 1 to 3 & si <  2 & su is     died               
 27.4 when pc is        3rd & pa <  1                                               
 27.8 when pc is        1st & pa >=      2                                          
 32.1 when pc is        2nd & pa <  1                                               
 37.4 when pc is 2nd or 3rd & pa >=      3                                          
 40.5 when pc is        1st & pa <  2                                               
> print(rpart.rules(age, varlen=-3))
  age                                                                                    
  7.1 when pcl is 2nd or 3rd & par is 1 to 3 & sib <  2 & sur is survived & sex is   male
  9.6 when pcl is 2nd or 3rd & par is 1 to 3 & sib >= 2                                  
 20.6 when pcl is 2nd or 3rd & par is 1 to 3 & sib <  2 & sur is survived & sex is female
 26.1 when pcl is 2nd or 3rd & par is 1 to 3 & sib <  2 & sur is     died                
 27.4 when pcl is        3rd & par <  1                                                  
 27.8 when pcl is        1st & par >=      2                                             
 32.1 when pcl is        2nd & par <  1                                                  
 37.4 when pcl is 2nd or 3rd & par >=      3                                             
 40.5 when pcl is        1st & par <  2                                                  
> 
> print(rpart.rules(age, varlen=-3, faclen=1))
  age                                                                
  7.1 when pcl is bc & par is 1 to 3 & sib <  2 & sur is b & sex is b
  9.6 when pcl is bc & par is 1 to 3 & sib >= 2                      
 20.6 when pcl is bc & par is 1 to 3 & sib <  2 & sur is b & sex is a
 26.1 when pcl is bc & par is 1 to 3 & sib <  2 & sur is a           
 27.4 when pcl is  c & par <  1                                      
 27.8 when pcl is  a & par >=      2                                 
 32.1 when pcl is  b & par <  1                                      
 37.4 when pcl is bc & par >=      3                                 
 40.5 when pcl is  a & par <  2                                      
> print(rpart.rules(age, varlen=-3, faclen=2))
  age                                                                        
  7.1 when pcl is 2n or 3r & par is 1 to 3 & sib <  2 & sur is sr & sex is ml
  9.6 when pcl is 2n or 3r & par is 1 to 3 & sib >= 2                        
 20.6 when pcl is 2n or 3r & par is 1 to 3 & sib <  2 & sur is sr & sex is fm
 26.1 when pcl is 2n or 3r & par is 1 to 3 & sib <  2 & sur is dd            
 27.4 when pcl is       3r & par <  1                                        
 27.8 when pcl is       1s & par >=      2                                   
 32.1 when pcl is       2n & par <  1                                        
 37.4 when pcl is 2n or 3r & par >=      3                                   
 40.5 when pcl is       1s & par <  2                                        
> 
> print(rpart.rules(age, varlen=-3, faclen=2, digits=1))
 age                                                                        
   7 when pcl is 2n or 3r & par is 1 to 3 & sib <  2 & sur is sr & sex is ml
  10 when pcl is 2n or 3r & par is 1 to 3 & sib >= 2                        
  21 when pcl is 2n or 3r & par is 1 to 3 & sib <  2 & sur is sr & sex is fm
  26 when pcl is 2n or 3r & par is 1 to 3 & sib <  2 & sur is dd            
  27 when pcl is       3r & par <  1                                        
  28 when pcl is       1s & par >=      2                                   
  32 when pcl is       2n & par <  1                                        
  37 when pcl is 2n or 3r & par >=      3                                   
  41 when pcl is       1s & par <  2                                        
> 
> # test various types of predictor with roundint and cover
> 
> cat0("\n=== test various types of predictor with roundint and cover ===\n")

=== test various types of predictor with roundint and cover ===
> 
> data <- "nonesuch"
> 
> build.models <- function(use.prp, clip.facs, roundint)
+ {
+     form <- Volume~.-Height
+ 
+     # conventional model with data argument and continuous predictors
+     main <- paste0("clip.facs=", clip.facs, " roundint=", roundint, "\ncontinuous predictors")
+     cat0(main, "\n")
+     trees.internal <- trees
+     mod <- rpart(formula=Volume~Girth, data=trees.internal, cp=.001)
+     if(use.prp)
+         prp(mod, clip.facs=clip.facs, roundint=roundint, main=main,
+                type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+     else {
+         rpart.plot(mod, clip.facs=clip.facs, roundint=roundint, main=main,
+                    type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+         rules <- rpart.rules(mod, clip.facs=clip.facs, roundint=roundint, digits=3)
+         print(rules)
+     }
+     main <- "logical and factor"
+     trees.internal <- trees
+     trees.internal$Girth <- ceiling(trees.internal$Girth)
+     trees.internal$logical12 <- trees.internal$Girth > 12
+     trees.internal$fac16     <- factor(ifelse(trees.internal$Girth > 16, "big", "small"))
+     trees.internal$Girth <- NULL
+     mod <- rpart(formula=Volume~., data=trees.internal, cp=.001)
+     if(use.prp)
+         prp(mod, clip.facs=clip.facs, roundint=roundint, main=main,
+                type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+     else {
+         rpart.plot(mod, clip.facs=clip.facs, roundint=roundint, main=main,
+                    type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+         rules <- rpart.rules(mod, clip.facs=clip.facs, roundint=roundint, cover=TRUE, digits=3)
+         print(rules)
+     }
+     main <- "logical and numeric binary"
+     trees.internal <- trees
+     trees.internal$logical12 <- trees.internal$Girth > 12
+     trees.internal$numeric16 <- as.numeric(trees.internal$Girth > 16)
+     trees.internal$Girth <- NULL
+     mod <- rpart(formula=Volume~., data=trees.internal, cp=.001)
+     if(use.prp)
+         prp(mod, clip.facs=clip.facs, roundint=roundint, main=main,
+                type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+     else {
+         rpart.plot(mod, clip.facs=clip.facs, roundint=roundint, main=main,
+                    type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+         rules <- rpart.rules(mod, clip.facs=clip.facs, roundint=roundint, digits=3)
+         print(rules)
+     }
+     main <- "discrete Girth"
+     cat0(main, "\n")
+     trees.internal <- trees
+     g <- trees.internal$Girth
+     trees.internal$Girth <- ifelse(g < 12.4, 1, ifelse(g < 16.1, 2, 3))
+     mod.discrete.girth <- rpart(formula=Volume~Girth, data=trees.internal, cp=.001)
+     if(use.prp)
+         prp(mod.discrete.girth, clip.facs=clip.facs, roundint=roundint, main=main,
+                type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+     else {
+         rpart.plot(mod.discrete.girth, clip.facs=clip.facs, roundint=roundint, main=main,
+                    type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+         rules <- rpart.rules(mod.discrete.girth, clip.facs=clip.facs, roundint=roundint, cover=TRUE, digits=3)
+         print(rules)
+     }
+     main <- "model without data arg"
+     cat0(main, "\n")
+     trees.internal <- trees
+     Volume <- 10 * trees.internal$Volume
+     Girth  <- 10 * trees.internal$Girth
+     mod.without.data.arg <- rpart(formula=Volume~Girth, cp=.001)
+     if(use.prp)
+         prp(mod.without.data.arg, clip.facs=clip.facs, roundint=roundint, main=main,
+                type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+     else {
+         rpart.plot(mod.without.data.arg, clip.facs=clip.facs, roundint=roundint, main=main,
+                    type=4, clip.right.labs=FALSE, digits=3, cex.main=1)
+         rules <- rpart.rules(mod.without.data.arg, clip.facs=clip.facs, roundint=roundint, digits=3)
+         print(rules)
+     }
+     list(mod=mod, mod.without.data.arg=mod.without.data.arg)
+ }
> for(use.prp in c(FALSE, TRUE)) {
+     for(clip.facs in c(FALSE, TRUE))
+         for(roundint in c(FALSE, TRUE)) {
+             par(mfrow=c(3, 3))
+             ret <- build.models(use.prp, clip.facs, roundint)
+         }
+ }
clip.facs=FALSE roundint=FALSE
continuous predictors
 Volume                           
   17.9 when Girth <  12.5        
   30.5 when Girth is 12.5 to 16.2
   55.9 when Girth >=         16.2
 Volume                                        cover
   17.9 when fac16 is small & logical12 is 0     48%
   30.5 when fac16 is small & logical12 is 1     29%
   55.9 when fac16 is   big                      23%
 Volume                                       
   17.9 when numeric16 <  0.5 & logical12 is 0
   30.5 when numeric16 <  0.5 & logical12 is 1
   55.9 when numeric16 >= 0.5                 
discrete Girth
 Volume                            cover
   17.9 when Girth <  1.5            48%
   30.5 when Girth is 1.5 to 2.5     29%
   55.9 when Girth >=        2.5     23%
model without data arg
 Volume                         
    179 when Girth <  125       
    305 when Girth is 125 to 162
    559 when Girth >=        162
clip.facs=FALSE roundint=TRUE
continuous predictors
 Volume                           
   17.9 when Girth <  12.5        
   30.5 when Girth is 12.5 to 16.2
   55.9 when Girth >=         16.2
 Volume                                        cover
   17.9 when fac16 is small & logical12 is 0     48%
   30.5 when fac16 is small & logical12 is 1     29%
   55.9 when fac16 is   big                      23%
 Volume                                     
   17.9 when numeric16 is 0 & logical12 is 0
   30.5 when numeric16 is 0 & logical12 is 1
   55.9 when numeric16 is 1                 
discrete Girth
 Volume                        cover
   17.9 when Girth <  2          48%
   30.5 when Girth is 2 to 3     29%
   55.9 when Girth >=      3     23%
model without data arg
 Volume                         
    179 when Girth <  125       
    305 when Girth is 125 to 162
    559 when Girth >=        162
clip.facs=TRUE roundint=FALSE
continuous predictors
 Volume                           
   17.9 when Girth <  12.5        
   30.5 when Girth is 12.5 to 16.2
   55.9 when Girth >=         16.2
 Volume                              cover
   17.9 when small & not logical12     48%
   30.5 when small & logical12         29%
   55.9 when   big                     23%
 Volume                                      
   17.9 when numeric16 <  0.5 & not logical12
   30.5 when numeric16 <  0.5 & logical12    
   55.9 when numeric16 >= 0.5                
discrete Girth
 Volume                            cover
   17.9 when Girth <  1.5            48%
   30.5 when Girth is 1.5 to 2.5     29%
   55.9 when Girth >=        2.5     23%
model without data arg
 Volume                         
    179 when Girth <  125       
    305 when Girth is 125 to 162
    559 when Girth >=        162
clip.facs=TRUE roundint=TRUE
continuous predictors
 Volume                           
   17.9 when Girth <  12.5        
   30.5 when Girth is 12.5 to 16.2
   55.9 when Girth >=         16.2
 Volume                              cover
   17.9 when small & not logical12     48%
   30.5 when small & logical12         29%
   55.9 when   big                     23%
 Volume                                   
   17.9 when not numeric16 & not logical12
   30.5 when not numeric16 & logical12    
   55.9 when numeric16                    
discrete Girth
 Volume                        cover
   17.9 when Girth <  2          48%
   30.5 when Girth is 2 to 3     29%
   55.9 when Girth >=      3     23%
model without data arg
 Volume                         
    179 when Girth <  125       
    305 when Girth is 125 to 162
    559 when Girth >=        162
clip.facs=FALSE roundint=FALSE
continuous predictors
discrete Girth
model without data arg
clip.facs=FALSE roundint=TRUE
continuous predictors
discrete Girth
model without data arg
clip.facs=TRUE roundint=FALSE
continuous predictors
discrete Girth
model without data arg
clip.facs=TRUE roundint=TRUE
continuous predictors
discrete Girth
model without data arg
> par(old.par)
> 
> mod                  <- ret$mod
> mod.without.data.arg <- ret$mod.without.data.arg
> 
> # check if we can still access the data used to build the model in build.models
> print(rpart.rules(mod, roundint=FALSE, trace=.5)) # ok
Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables)
 Volume                                         
     18 when numeric16 <  0.5 & logical12 <  0.5
     31 when numeric16 <  0.5 & logical12 >= 0.5
     56 when numeric16 >= 0.5                   
> options(warn=2) # treat warnings as errors
> expect.err(try(rpart.rules(mod)), "Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).")
Error : (converted from warning) Cannot retrieve the data used to build the model (so cannot determine roundint and is.binary for the variables).
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
Got error as expected from try(rpart.rules(mod))
> print(rpart.rules(mod.without.data.arg, roundint=FALSE, trace=.5)) # ok
 Volume                         
    179 when Girth <  125       
    305 when Girth is 125 to 162
    559 when Girth >=        162
> print(rpart.rules(mod.without.data.arg, roundint=TRUE, trace=.5))  # ok, because environment saved with formula in model
 Volume                         
    179 when Girth <  125       
    305 when Girth is 125 to 162
    559 when Girth >=        162
> options(warn=1) # print warnings as they occur
> 
> cat0("\n=== fit.oz, digits=4 ===\n")

=== fit.oz, digits=4 ===
> library(earth)
Loading required package: Formula
Loading required package: plotmo
Loading required package: plotrix
Loading required package: TeachingDemos
> data(ozone1)
> fit.oz <- rpart(O3~., data=ozone1)
> cat("rpart.rules(fit.oz)\n")
rpart.rules(fit.oz)
> print(rpart.rules(fit.oz, digits=4))
     O3                                                                                                 
  5.148 when temp <  68              & ibh >= 3574                                                      
  6.457 when temp <  68              & ibh <  3574              & dpg <  -9                             
  9.050 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                             
 10.800 when temp >= 68 & ibt <  227                                                    & humidity <  60
 14.355 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                             
 16.000 when temp >= 68 & ibt >= 227               & doy >= 307                                         
 17.089 when temp >= 68 & ibt <  227                                                    & humidity >= 60
 22.944 when temp >= 68 & ibt >= 227               & doy <  307             & vis >= 55                 
 27.412 when temp >= 68 & ibt >= 227               & doy <  307             & vis <  55                 
> rpart.plot(fit.oz, digits=4, main="fit.oz\ndigits=4")
> cat("rpart.rules(fit.oz), roundint=FALSE\n")
rpart.rules(fit.oz), roundint=FALSE
> print(rpart.rules(fit.oz, roundint=FALSE, digits=4))
     O3                                                                                                           
  5.148 when temp <  67.5                & ibh >= 3574                                                            
  6.457 when temp <  67.5                & ibh <  3574                & dpg <  -9.5                               
  9.050 when temp <  67.5 & ibt <  159.0 & ibh <  3574                & dpg >= -9.5                               
 10.800 when temp >= 67.5 & ibt <  226.5                                                        & humidity <  59.5
 14.355 when temp <  67.5 & ibt >= 159.0 & ibh <  3574                & dpg >= -9.5                               
 16.000 when temp >= 67.5 & ibt >= 226.5               & doy >= 306.5                                             
 17.089 when temp >= 67.5 & ibt <  226.5                                                        & humidity >= 59.5
 22.944 when temp >= 67.5 & ibt >= 226.5               & doy <  306.5               & vis >= 55                   
 27.412 when temp >= 67.5 & ibt >= 226.5               & doy <  306.5               & vis <  55                   
> rpart.plot(fit.oz, roundint=FALSE, digits=4, main="fit.oz\ndigits=4, roundint=FALSE")
> 
> cat0("\n=== oz.vis55 ===\n")

=== oz.vis55 ===
> 
> oz <- ozone1
> oz$vis55 <- oz$vis >= 55 # replace vis with a logical variable
> oz$vis <- NULL
> oz.vis55 <- rpart(O3~., data=oz)
> cat("oz.vis55:\n")
oz.vis55:
> print(rpart.rules(oz.vis55))
   O3                                                                                                  
  5.1 when temp <  68              & ibh >= 3574                                                       
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9                              
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                              
 10.8 when temp >= 68 & ibt <  227                                        & humidity <  60             
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                              
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                          
 17.1 when temp >= 68 & ibt <  227                                        & humidity >= 60             
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 1
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 0
> 
> # test handling of malformed rpart object
> cat("oz.vis55.badmodel:\n")
oz.vis55.badmodel:
> oz.vis55.badmodel <- oz.vis55
> oz.vis55.badmodel$model <- "bad.model.frame"
> print(rpart.rules(oz.vis55.badmodel)) # should give a warning
Warning: Bad 'model' field saved with the rpart model (bad dimensions).
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
   O3                                                                                                      
  5.1 when temp <  68              & ibh >= 3574                                                           
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
 10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
 17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> print(rpart.rules(oz.vis55.badmodel, roundint=FALSE)) # ok
   O3                                                                                                      
  5.1 when temp <  68              & ibh >= 3574                                                           
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
 10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
 17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> 
> cat("oz.vis55.noterms.nocall:\n")
oz.vis55.noterms.nocall:
> oz.vis55.noterms.nocall <- oz.vis55
> oz.vis55.noterms.nocall$terms <- NULL
> oz.vis55.noterms.nocall$call <- NULL
> print(rpart.rules(oz.vis55.noterms.nocall)) # should give a warning
Warning: Invalid formula in rpart model (invalid formula).
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
 response                                                                                                      
      5.1 when temp <  68              & ibh >= 3574                                                           
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
     10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
     17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> print(rpart.rules(oz.vis55.noterms.nocall, roundint=FALSE)) # ok
 response                                                                                                      
      5.1 when temp <  68              & ibh >= 3574                                                           
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
     10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
     17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> 
> oz.vis55.model <- rpart(O3~., data=oz, model=TRUE)
> oz.vis55.model.noterms.nocall <- oz.vis55.model
> oz.vis55.model.noterms.nocall$terms <- NULL
> oz.vis55.model.noterms.nocall$call <- NULL
> print(rpart.rules(oz.vis55.model.noterms.nocall)) # ok (but cannot get response name)
 response                                                                                                  
      5.1 when temp <  68              & ibh >= 3574                                                       
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9                              
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                              
     10.8 when temp >= 68 & ibt <  227                                        & humidity <  60             
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                              
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                          
     17.1 when temp >= 68 & ibt <  227                                        & humidity >= 60             
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 1
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 0
> 
> cat("oz.vis55.badformula:\n")
oz.vis55.badformula:
> oz.vis55.badformula <- oz.vis55
> oz.vis55.badformula$formula <- "bad.formula"
> print(rpart.rules(oz.vis55.badformula)) # should give a warning
Warning: The 'formula' field in the rpart model is not actually a formula).
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
   O3                                                                                                      
  5.1 when temp <  68              & ibh >= 3574                                                           
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
 10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
 17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> print(rpart.rules(oz.vis55.badformula, roundint=FALSE)) # ok
   O3                                                                                                      
  5.1 when temp <  68              & ibh >= 3574                                                           
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
 10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
 17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> 
> cat("oz.vis55.badcall:\n")
oz.vis55.badcall:
> oz.vis55.badcall <- oz.vis55
> oz.vis55.badcall$call <- "bad.call"
> print(rpart.rules(oz.vis55.badcall)) # should give a warning
Warning: Bad 'call' field in rpart model.
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
   O3                                                                                                      
  5.1 when temp <  68              & ibh >= 3574                                                           
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
 10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
 17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> print(rpart.rules(oz.vis55.badcall, roundint=FALSE)) # ok
   O3                                                                                                      
  5.1 when temp <  68              & ibh >= 3574                                                           
  6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
  9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
 10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
 14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
 16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
 17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
 22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
 27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> 
> cat("oz.vis55.badterms:\n")
oz.vis55.badterms:
> oz.vis55.badterms <- oz.vis55
> oz.vis55.badterms$terms <- "bad.terms"
> print(rpart.rules(oz.vis55.badterms)) # should give a warning
Warning: Cannot retrieve the data used to build the model (model.frame: argument is not a valid model).
To silence this warning:
    Call rpart.rules with roundint=FALSE,
    or rebuild the rpart model with model=TRUE.
 response                                                                                                      
      5.1 when temp <  68              & ibh >= 3574                                                           
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
     10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
     17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> print(rpart.rules(oz.vis55.badterms, roundint=FALSE)) # ok
 response                                                                                                      
      5.1 when temp <  68              & ibh >= 3574                                                           
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                                
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                                
     10.8 when temp >= 68 & ibt <  227                                          & humidity <  60               
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                                
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                              
     17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60               
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 >= 0.5
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 <  0.5
> 
> cat("oz.vis55.baddataClasses:\n")
oz.vis55.baddataClasses:
> oz.vis55.baddataClasses <- oz.vis55
> attr(oz.vis55.baddataClasses$terms, "dataClasses") <- "bad.dataClasses"
> print(rpart.rules(oz.vis55.baddataClasses)) # should give a warning
 response                                                                                                  
      5.1 when temp <  68              & ibh >= 3574                                                       
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9                              
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                              
     10.8 when temp >= 68 & ibt <  227                                        & humidity <  60             
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                              
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                          
     17.1 when temp >= 68 & ibt <  227                                        & humidity >= 60             
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 1
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 0
> print(rpart.rules(oz.vis55.baddataClasses, roundint=FALSE)) # ok
 response                                                                                                    
      5.1 when temp <  68              & ibh >= 3574                                                         
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9.5                              
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9.5                              
     10.8 when temp >= 68 & ibt <  227                                          & humidity <  60             
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9.5                              
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                            
     17.1 when temp >= 68 & ibt <  227                                          & humidity >= 60             
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 is 1
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                                & vis55 is 0
> 
> # test handling of malformed rpart object, such as package semtree passes
> # no terms component nor attribute (therefore cannot determine is.binary for the variables)
> cat("oz.vis55.noterms:\n")
oz.vis55.noterms:
> oz.vis55.noterms <- oz.vis55
> oz.vis55.noterms$terms <- NULL
> print(rpart.rules(oz.vis55.noterms, trace=1)) # trace message says "cannot determine is.binary"
no terms component nor attribute (therefore cannot get response name)


iframe   3 node   4 path c("root", "temp < 68", "ibh >= 3574")
iframe   5 node  10 path c("root", "temp < 68", "ibh < 3574", "dpg < -9")
iframe   7 node  22 path c("root", "temp < 68", "ibh < 3574", "dpg >= -9", "ibt < 159")
iframe   8 node  23 path c("root", "temp < 68", "ibh < 3574", "dpg >= -9", "ibt >= 159")
iframe  11 node  12 path c("root", "temp >= 68", "ibt < 227", "humidity < 60")
iframe  12 node  13 path c("root", "temp >= 68", "ibt < 227", "humidity >= 60")
iframe  14 node  14 path c("root", "temp >= 68", "ibt >= 227", "doy >= 307")
iframe  16 node  30 path c("root", "temp >= 68", "ibt >= 227", "doy < 307", "vis55 = 1")
iframe  17 node  31 path c("root", "temp >= 68", "ibt >= 227", "doy < 307", "vis55 = 0")

raw rules:
   lab         fit iclass       cover temp= temp< temp>= ibt= ibt< ibt>= ibh= ibh< ibh>= vh= vh< vh>= humidity= humidity< humidity>= doy= doy< doy>= dpg= dpg< dpg>= vis55= vis55< vis55>= wind= wind< wind>=
4      5.148148148      5 32.72727273          68                                   3574                                                                                                                     
10     6.457142857      6 10.60606061          68                             3574                                                                          -9                                               
22            9.05      9 12.12121212          68              159            3574                                                                                -9                                         
23     14.35483871     14  9.39393939          68                    159      3574                                                                                -9                                         
12            10.8     10  3.03030303                 68       227                                                     60                                                                                    
13     17.08888889     17 13.63636364                 68       227                                                                60                                                                         
14              16     16  2.42424242                 68             227                                                                         307                                                         
30     22.94444444     22 10.90909091                 68             227                                                                   307                            1                                  
31     27.41176471     27  5.15151515                 68             227                                                                   307                            0                                  

processed rules:
   response                                                                                                  
4       5.1 when temp <  68              & ibh >= 3574                                                       
10      6.5 when temp <  68              & ibh <  3574              & dpg <  -9                              
22      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                              
12     10.8 when temp >= 68 & ibt <  227                                        & humidity <  60             
23     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                              
14     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                          
13     17.1 when temp >= 68 & ibt <  227                                        & humidity >= 60             
30     22.9 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 1
31     27.4 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 0

 response                                                                                                  
      5.1 when temp <  68              & ibh >= 3574                                                       
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9                              
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                              
     10.8 when temp >= 68 & ibt <  227                                        & humidity <  60             
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                              
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                          
     17.1 when temp >= 68 & ibt <  227                                        & humidity >= 60             
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 1
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 0
> print(rpart.rules(oz.vis55.noterms)) # silent (but still cannot determine is.binary)
 response                                                                                                  
      5.1 when temp <  68              & ibh >= 3574                                                       
      6.5 when temp <  68              & ibh <  3574              & dpg <  -9                              
      9.1 when temp <  68 & ibt <  159 & ibh <  3574              & dpg >= -9                              
     10.8 when temp >= 68 & ibt <  227                                        & humidity <  60             
     14.4 when temp <  68 & ibt >= 159 & ibh <  3574              & dpg >= -9                              
     16.0 when temp >= 68 & ibt >= 227               & doy >= 307                                          
     17.1 when temp >= 68 & ibt <  227                                        & humidity >= 60             
     22.9 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 1
     27.4 when temp >= 68 & ibt >= 227               & doy <  307                              & vis55 is 0
> 
> cat0("\n=== Country ===\n")

=== Country ===
> Country <- rpart(Country~., data=cu.summary) # factor with 10 levels
> par(mfrow=c(1, 2))
> rpart.plot(Country, type=3, clip.right.labs=FALSE, branch=.3, legend.x=-0.12, legend.y=.97)
> print(rpart.rules(Country, cover=TRUE))
   Country  Bra Eng Fra Ger Jap Jap Kor Mex Swe USA                                                                                                             cover
   Germany [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when Reliability is Much worse or worse or average & Price >=          16669 & Type is Compact or Sporty      9%
     Japan [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when Reliability is          better or Much better & Price <   7038                                           6%
     Japan [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when Reliability is          better or Much better & Price >=          13202                                  8%
 Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when Reliability is          better or Much better & Price is  7038 to 13202                                 15%
       USA [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when Reliability is Much worse or worse or average & Price >=          16669 & Type is   Large or Medium     17%
       USA [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when Reliability is Much worse or worse or average & Price <  16669                                          46%
> print(rpart.rules(Country, nn=TRUE))
 nn   Country  Bra Eng Fra Ger Jap Jap Kor Mex Swe USA                                                                                                          
 12   Germany [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when Reliability is Much worse or worse or average & Price >=          16669 & Type is Compact or Sporty
 10     Japan [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when Reliability is          better or Much better & Price <   7038                                     
  4     Japan [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when Reliability is          better or Much better & Price >=          13202                            
 11 Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when Reliability is          better or Much better & Price is  7038 to 13202                            
 13       USA [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when Reliability is Much worse or worse or average & Price >=          16669 & Type is   Large or Medium
  7       USA [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when Reliability is Much worse or worse or average & Price <  16669                                     
> print(rpart.rules(Country, cover=TRUE, nn=TRUE))
 nn   Country  Bra Eng Fra Ger Jap Jap Kor Mex Swe USA                                                                                                             cover
 12   Germany [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when Reliability is Much worse or worse or average & Price >=          16669 & Type is Compact or Sporty      9%
 10     Japan [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when Reliability is          better or Much better & Price <   7038                                           6%
  4     Japan [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when Reliability is          better or Much better & Price >=          13202                                  8%
 11 Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when Reliability is          better or Much better & Price is  7038 to 13202                                 15%
 13       USA [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when Reliability is Much worse or worse or average & Price >=          16669 & Type is   Large or Medium     17%
  7       USA [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when Reliability is Much worse or worse or average & Price <  16669                                          46%
> 
> print(rpart.rules(Country, style="tall"))
                      Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when
    Reliability is Much worse or worse or average
    Price >= 16669
    Type is Compact or Sporty

Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when
    Reliability is better or Much better
    Price < 7038

Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when
    Reliability is better or Much better
    Price >= 13202

Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when
    Reliability is better or Much better
    Price is 7038 to 13202

Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when
    Reliability is Much worse or worse or average
    Price >= 16669
    Type is Large or Medium

Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when
    Reliability is Much worse or worse or average
    Price < 16669
> print(rpart.rules(Country, style="tall", cover=TRUE))
                      Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] with cover 9% when
    Reliability is Much worse or worse or average
    Price >= 16669
    Type is Compact or Sporty

Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] with cover 6% when
    Reliability is better or Much better
    Price < 7038

Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] with cover 8% when
    Reliability is better or Much better
    Price >= 13202

Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] with cover 15% when
    Reliability is better or Much better
    Price is 7038 to 13202

Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] with cover 17% when
    Reliability is Much worse or worse or average
    Price >= 16669
    Type is Large or Medium

Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] with cover 46% when
    Reliability is Much worse or worse or average
    Price < 16669
> print(rpart.rules(Country, style="tall", nn=TRUE))
                           Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
[12] Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when
         Reliability is Much worse or worse or average
         Price >= 16669
         Type is Compact or Sporty

[10] Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when
         Reliability is better or Much better
         Price < 7038

[4]  Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when
         Reliability is better or Much better
         Price >= 13202

[11] Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when
         Reliability is better or Much better
         Price is 7038 to 13202

[13] Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when
         Reliability is Much worse or worse or average
         Price >= 16669
         Type is Large or Medium

[7]  Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when
         Reliability is Much worse or worse or average
         Price < 16669
> print(rpart.rules(Country, style="tall", cover=TRUE, nn=TRUE))
                           Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
[12] Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] with cover 9% when
         Reliability is Much worse or worse or average
         Price >= 16669
         Type is Compact or Sporty

[10] Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] with cover 6% when
         Reliability is better or Much better
         Price < 7038

[4]  Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] with cover 8% when
         Reliability is better or Much better
         Price >= 13202

[11] Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] with cover 15% when
         Reliability is better or Much better
         Price is 7038 to 13202

[13] Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] with cover 17% when
         Reliability is Much worse or worse or average
         Price >= 16669
         Type is Large or Medium

[7]  Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] with cover 46% when
         Reliability is Much worse or worse or average
         Price < 16669
> 
> print(rpart.rules(Country, style="tallw"))
                      Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when
                                                               Reliability is Much worse or worse or average
                                                               Price >= 16669
                                                               Type is Compact or Sporty

Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when
                                                               Reliability is better or Much better
                                                               Price < 7038

Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when
                                                               Reliability is better or Much better
                                                               Price >= 13202

Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when
                                                               Reliability is better or Much better
                                                               Price is 7038 to 13202

Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when
                                                               Reliability is Much worse or worse or average
                                                               Price >= 16669
                                                               Type is Large or Medium

Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when
                                                               Reliability is Much worse or worse or average
                                                               Price < 16669
> print(rpart.rules(Country, style="tallw", cover=TRUE))
                      Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] with cover 9% when
                                                               Reliability is Much worse or worse or average
                                                               Price >= 16669
                                                               Type is Compact or Sporty

Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] with cover 6% when
                                                               Reliability is better or Much better
                                                               Price < 7038

Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] with cover 8% when
                                                               Reliability is better or Much better
                                                               Price >= 13202

Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] with cover 15% when
                                                               Reliability is better or Much better
                                                               Price is 7038 to 13202

Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] with cover 17% when
                                                               Reliability is Much worse or worse or average
                                                               Price >= 16669
                                                               Type is Large or Medium

Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] with cover 46% when
                                                               Reliability is Much worse or worse or average
                                                               Price < 16669
> print(rpart.rules(Country, style="tallw", nn=TRUE))
                           Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
[12] Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] when
                                                                    Reliability is Much worse or worse or average
                                                                    Price >= 16669
                                                                    Type is Compact or Sporty

[10] Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] when
                                                                    Reliability is better or Much better
                                                                    Price < 7038

[4]  Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] when
                                                                    Reliability is better or Much better
                                                                    Price >= 13202

[11] Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] when
                                                                    Reliability is better or Much better
                                                                    Price is 7038 to 13202

[13] Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] when
                                                                    Reliability is Much worse or worse or average
                                                                    Price >= 16669
                                                                    Type is Large or Medium

[7]  Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] when
                                                                    Reliability is Much worse or worse or average
                                                                    Price < 16669
> print(rpart.rules(Country, style="tallw", cover=TRUE, nn=TRUE))
                           Bra Eng Fra Ger Jap Jap Kor Mex Swe USA 
[12] Country is Germany   [.00 .00 .00 .50 .10 .00 .00 .00 .30 .10] with cover 9% when
                                                                    Reliability is Much worse or worse or average
                                                                    Price >= 16669
                                                                    Type is Compact or Sporty

[10] Country is Japan     [.00 .00 .00 .00 .71 .14 .14 .00 .00 .00] with cover 6% when
                                                                    Reliability is better or Much better
                                                                    Price < 7038

[4]  Country is Japan     [.00 .00 .00 .11 .89 .00 .00 .00 .00 .00] with cover 8% when
                                                                    Reliability is better or Much better
                                                                    Price >= 13202

[11] Country is Japan/USA [.06 .00 .00 .00 .29 .47 .00 .06 .00 .12] with cover 15% when
                                                                    Reliability is better or Much better
                                                                    Price is 7038 to 13202

[13] Country is USA       [.00 .05 .05 .15 .15 .00 .00 .00 .10 .50] with cover 17% when
                                                                    Reliability is Much worse or worse or average
                                                                    Price >= 16669
                                                                    Type is Large or Medium

[7]  Country is USA       [.00 .00 .02 .04 .17 .00 .07 .04 .00 .67] with cover 46% when
                                                                    Reliability is Much worse or worse or average
                                                                    Price < 16669
> 
> expect.err(try(rpart.rules(Country, nonesuch="nonesuch")), 'unused argument (nonesuch = "nonesuch")')
Error in check.if.dot.arg.supported.by.rpart.rules(...) : 
  unused argument (nonesuch = "nonesuch")
Got error as expected from try(rpart.rules(Country, nonesuch = "nonesuch"))
> expect.err(try(print(rpart.rules(Country), nonesuch2="nonesuch2")), 'unused argument (nonesuch2 = "nonesuch2")')
Error in stop.if.dot.arg.used(...) : 
  unused argument (nonesuch2 = "nonesuch2")
Got error as expected from try(print(rpart.rules(Country), nonesuch2 = "nonesuch2"))
> expect.err(try(rpart.rules(99)), "Not an rpart object")
Error in rpart.rules(99) : Not an rpart object
Got error as expected from try(rpart.rules(99))
> expect.err(try(rpart.rules(Country, ylim=c(0,1))), 'unused argument (ylim = c(0, 1))')
Error in check.if.dot.arg.supported.by.rpart.rules(...) : 
  unused argument (ylim = c(0, 1))
Got error as expected from try(rpart.rules(Country, ylim = c(0, 1)))
> 
> # rpart.predict (factor response, 10 levels)
> stopifnot(identical(options("width"), old.width))
> owidth <- options("width")
> options(width=1e3)
> print(predict(Country)[1:5,])
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667
> print(head(rpart.predict(Country, rules=TRUE)))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA                                                                                                            
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669                                     
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038                                     
> print(head(rpart.predict(Country, rules=TRUE, when="")))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA                                                                                                            
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669                                     
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038                                     
> print(head(rpart.predict(Country, rules=TRUE, when="WHEN")))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA                                                                                                            
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669                                     
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038                                     
> print(head(rpart.predict(Country, rules=TRUE, when="", because="reason:")))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA                                                                                                            
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 reason: Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 reason: Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 reason: Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 reason: Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 reason: Reliability is Much worse or worse or average & Price <  16669                                     
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 reason: Reliability is          better or Much better & Price <   7038                                     
> print(head(rpart.predict(Country, rules=TRUE, because="")))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA                                                                                                    
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 Reliability is Much worse or worse or average & Price <  16669                                     
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 Reliability is          better or Much better & Price <   7038                                     
> stopifnot(all.equal(predict(Country), as.matrix(rpart.predict(Country, rules=TRUE)[,1:10])))
> stopifnot(max(abs(predict(Country) - rpart.predict(Country, rules=TRUE)[,1:10])) == 0)
> print(rpart.predict(Country, nn=TRUE)[1:5,])
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA nn
Acura Integra 4 0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 11
Dodge Colt 4    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 10
Dodge Omni 4    0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667  7
Eagle Summit 4  0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 11
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667  7
> stopifnot(all.equal(predict(Country), as.matrix(rpart.predict(Country, nn=TRUE)[,1:10])))
> print(rpart.predict(Country, nn=TRUE, rules=TRUE)[1:5,], digits=2)
                Brazil England France Germany Japan Japan/USA Korea Mexico Sweden  USA nn                                                                                                            
Acura Integra 4  0.059       0  0.000   0.000  0.29      0.47 0.000  0.059      0 0.12 11 because Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4     0.000       0  0.000   0.000  0.71      0.14 0.143  0.000      0 0.00 10 because Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4     0.000       0  0.019   0.037  0.17      0.00 0.074  0.037      0 0.67  7 because Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4   0.059       0  0.000   0.000  0.29      0.47 0.000  0.059      0 0.12 11 because Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4  0.000       0  0.019   0.037  0.17      0.00 0.074  0.037      0 0.67  7 because Reliability is Much worse or worse or average & Price <  16669                                     
> stopifnot(all.equal(predict(Country), as.matrix(rpart.predict(Country, nn=TRUE, rules=TRUE)[,1:10])))
> 
> print(predict(Country, type="class")[1:5])
Acura Integra 4    Dodge Colt 4    Dodge Omni 4  Eagle Summit 4 Ford Escort   4 
      Japan/USA           Japan             USA       Japan/USA             USA 
Levels: Brazil England France Germany Japan Japan/USA Korea Mexico Sweden USA
> print(rpart.predict(Country, type="class", rules=TRUE, because="reason:")[1:5,])
                    class                                                                                                            
Acura Integra 4 Japan/USA reason: Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4        Japan reason: Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4          USA reason: Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  Japan/USA reason: Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4       USA reason: Reliability is Much worse or worse or average & Price <  16669                                     
> print(rpart.predict(Country, type="class", rules=TRUE, because="")[1:5,])
                    class                                                                                                    
Acura Integra 4 Japan/USA Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4        Japan Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4          USA Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  Japan/USA Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4       USA Reliability is Much worse or worse or average & Price <  16669                                     
> pred <- rpart.predict(Country, type="class", rules=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(Country, type="class"), pred))
> print(rpart.predict(Country, type="class", rules=TRUE, because="\n")[1:5,])
                    class                                                                                                        
Acura Integra 4 Japan/USA \\n Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4        Japan \\n Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4          USA \\n Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  Japan/USA \\n Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4       USA \\n Reliability is Much worse or worse or average & Price <  16669                                     
> print(rpart.predict(Country, type="class", rules=TRUE, nn=TRUE)[1:5,])
                    class nn                                                                                                            
Acura Integra 4 Japan/USA 11 because Reliability is          better or Much better & Price is  7038 to 13202                            
Dodge Colt 4        Japan 10 because Reliability is          better or Much better & Price <   7038                                     
Dodge Omni 4          USA  7 because Reliability is Much worse or worse or average & Price <  16669                                     
Eagle Summit 4  Japan/USA 11 because Reliability is          better or Much better & Price is  7038 to 13202                            
Ford Escort   4       USA  7 because Reliability is Much worse or worse or average & Price <  16669                                     
> pred <- rpart.predict(Country, type="class", rules=TRUE, nn=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(Country, type="class"), pred))
> 
> print(rpart.predict(Country, newdata=cu.summary[5:8,]))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000
GEO Metro  3    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000
GEO Prizm  4    0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471
> print(rpart.predict(Country, newdata=cu.summary[5:8,], nn=TRUE))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA nn
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667  7
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 10
GEO Metro  3    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 10
GEO Prizm  4    0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 11
> print(rpart.predict(Country, newdata=cu.summary[5:8,], rules=TRUE))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA                                                                                
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667 because Reliability is Much worse or worse or average & Price <  16669         
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038         
GEO Metro  3    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 because Reliability is          better or Much better & Price <   7038         
GEO Prizm  4    0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 because Reliability is          better or Much better & Price is  7038 to 13202
> print(rpart.predict(Country, newdata=cu.summary[5:8,], nn=TRUE, rules=TRUE))
                    Brazil England     France    Germany     Japan Japan/USA      Korea     Mexico Sweden       USA nn                                                                                
Ford Escort   4 0.00000000       0 0.01851852 0.03703704 0.1666667 0.0000000 0.07407407 0.03703704      0 0.6666667  7 because Reliability is Much worse or worse or average & Price <  16669         
Ford Festiva 4  0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 10 because Reliability is          better or Much better & Price <   7038         
GEO Metro  3    0.00000000       0 0.00000000 0.00000000 0.7142857 0.1428571 0.14285714 0.00000000      0 0.0000000 10 because Reliability is          better or Much better & Price <   7038         
GEO Prizm  4    0.05882353       0 0.00000000 0.00000000 0.2941176 0.4705882 0.00000000 0.05882353      0 0.1176471 11 because Reliability is          better or Much better & Price is  7038 to 13202
> 
> options(width=owidth$width)
> stopifnot(identical(options("width"), old.width))
> 
> cat0("\n=== \"style\" argument ===\n")

=== "style" argument ===
> 
> Species <- rpart(Species ~ ., data=iris)
> rpart.plot(Species, main="rpart(Species ~ ., data=iris)")
> par(old.par)
> print(rpart.rules(Species, style="wide"))
    Species  seto vers virg                                               
     setosa [1.00  .00  .00] when Petal.Length <  2.5                     
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8
> print(rpart.rules(Species, style="wide", cover=TRUE))
    Species  seto vers virg                                                  cover
     setosa [1.00  .00  .00] when Petal.Length <  2.5                          33%
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8     36%
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8     31%
> print(rpart.rules(Species, style="tall"))
                       seto vers virg 
Species is setosa     [1.00  .00  .00] when
    Petal.Length < 2.5

Species is versicolor [ .00  .91  .09] when
    Petal.Length >= 2.5
    Petal.Width < 1.8

Species is virginica  [ .00  .02  .98] when
    Petal.Length >= 2.5
    Petal.Width >= 1.8
> print(rpart.rules(Species, style="tall", cover=TRUE))
                       seto vers virg 
Species is setosa     [1.00  .00  .00] with cover 33% when
    Petal.Length < 2.5

Species is versicolor [ .00  .91  .09] with cover 36% when
    Petal.Length >= 2.5
    Petal.Width < 1.8

Species is virginica  [ .00  .02  .98] with cover 31% when
    Petal.Length >= 2.5
    Petal.Width >= 1.8
> print(rpart.rules(Species, style="tallw"))
                       seto vers virg 
Species is setosa     [1.00  .00  .00] when
                                       Petal.Length < 2.5

Species is versicolor [ .00  .91  .09] when
                                       Petal.Length >= 2.5
                                       Petal.Width < 1.8

Species is virginica  [ .00  .02  .98] when
                                       Petal.Length >= 2.5
                                       Petal.Width >= 1.8
> print(rpart.rules(Species, style="tallw", cover=TRUE))
                       seto vers virg 
Species is setosa     [1.00  .00  .00] with cover 33% when
                                       Petal.Length < 2.5

Species is versicolor [ .00  .91  .09] with cover 36% when
                                       Petal.Length >= 2.5
                                       Petal.Width < 1.8

Species is virginica  [ .00  .02  .98] with cover 31% when
                                       Petal.Length >= 2.5
                                       Petal.Width >= 1.8
> 
> Mileage <- rpart(Mileage ~ ., data=cu.summary)
> rules <- rpart.rules(Mileage)
> print(rules)
 Mileage                                                                 
      19 when Price >=          9447 & Type is               Large or Van
      22 when Price >=          9447 & Type is                     Medium
      24 when Price >=         11485 & Type is Compact or Small or Sporty
      25 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
      32 when Price <  9447                                              
> print(rules, style='tall')
Mileage is 19 when
    Price >= 9447
    Type is Large or Van

Mileage is 22 when
    Price >= 9447
    Type is Medium

Mileage is 24 when
    Price >= 11485
    Type is Compact or Small or Sporty

Mileage is 25 when
    Price is 9447 to 11485
    Type is Compact or Small or Sporty

Mileage is 32 when
    Price < 9447
> print(rules, style='tallw')
Mileage is 19 when
              Price >= 9447
              Type is Large or Van

Mileage is 22 when
              Price >= 9447
              Type is Medium

Mileage is 24 when
              Price >= 11485
              Type is Compact or Small or Sporty

Mileage is 25 when
              Price is 9447 to 11485
              Type is Compact or Small or Sporty

Mileage is 32 when
              Price < 9447
> rules.nn <- rpart.rules(Mileage, nn=TRUE)
> print(rules.nn)
 nn Mileage                                                                 
  8      19 when Price >=          9447 & Type is               Large or Van
  9      22 when Price >=          9447 & Type is                     Medium
 10      24 when Price >=         11485 & Type is Compact or Small or Sporty
 11      25 when Price is 9447 to 11485 & Type is Compact or Small or Sporty
  3      32 when Price <  9447                                              
> print(rules.nn, style='tall')
[8]  Mileage is 19 when
         Price >= 9447
         Type is Large or Van

[9]  Mileage is 22 when
         Price >= 9447
         Type is Medium

[10] Mileage is 24 when
         Price >= 11485
         Type is Compact or Small or Sporty

[11] Mileage is 25 when
         Price is 9447 to 11485
         Type is Compact or Small or Sporty

[3]  Mileage is 32 when
         Price < 9447
> print(rules.nn, style='tallw')
[8]  Mileage is 19 when
                   Price >= 9447
                   Type is Large or Van

[9]  Mileage is 22 when
                   Price >= 9447
                   Type is Medium

[10] Mileage is 24 when
                   Price >= 11485
                   Type is Compact or Small or Sporty

[11] Mileage is 25 when
                   Price is 9447 to 11485
                   Type is Compact or Small or Sporty

[3]  Mileage is 32 when
                   Price < 9447
> 
> data(ptitanic)
> survived <- rpart(survived ~ ., data=ptitanic)
> print(rpart.rules(survived, style="wide", varlen=-3, faclen=2))
 survived                                                                             
     0.05 when sex is ml & age <   9.5         & sib >= 3                             
     0.11 when sex is fm & age >=         16.5 & sib <  3 & pcl is       3r & par >= 4
     0.14 when sex is fm                       & sib >= 3 & pcl is       3r           
     0.17 when sex is ml & age >=          9.5                                        
     0.39 when sex is fm & age >=         27.5 & sib <  3 & pcl is       3r & par <  4
     0.39 when sex is fm & age is 16.5 to 21.5 & sib <  3 & pcl is       3r & par <  4
     0.62 when sex is fm & age is 21.5 to 27.5 & sib <  3 & pcl is       3r & par <  4
     0.73 when sex is fm & age <  16.5         & sib <  3 & pcl is       3r           
     0.89 when sex is ml & age <   9.5         & sib <  3                             
     0.93 when sex is fm                                  & pcl is 1s or 2n           
> print(rpart.rules(survived, style="tall", varlen=-3, faclen=2))
survived is 0.05 when
    sex is ml
    age < 9.5
    sib >= 3

survived is 0.11 when
    sex is fm
    age >= 16.5
    sib < 3
    pcl is 3r
    par >= 4

survived is 0.14 when
    sex is fm
    sib >= 3
    pcl is 3r

survived is 0.17 when
    sex is ml
    age >= 9.5

survived is 0.39 when
    sex is fm
    age >= 27.5
    sib < 3
    pcl is 3r
    par < 4

survived is 0.39 when
    sex is fm
    age is 16.5 to 21.5
    sib < 3
    pcl is 3r
    par < 4

survived is 0.62 when
    sex is fm
    age is 21.5 to 27.5
    sib < 3
    pcl is 3r
    par < 4

survived is 0.73 when
    sex is fm
    age < 16.5
    sib < 3
    pcl is 3r

survived is 0.89 when
    sex is ml
    age < 9.5
    sib < 3

survived is 0.93 when
    sex is fm
    pcl is 1s or 2n
> print(rpart.rules(survived, style="wide", clip.facs=TRUE, varlen=-3, faclen=2))
 survived                                                               
     0.05 when ml & age <   9.5         & sib >= 3                      
     0.11 when fm & age >=         16.5 & sib <  3 &       3r & par >= 4
     0.14 when fm                       & sib >= 3 &       3r           
     0.17 when ml & age >=          9.5                                 
     0.39 when fm & age >=         27.5 & sib <  3 &       3r & par <  4
     0.39 when fm & age is 16.5 to 21.5 & sib <  3 &       3r & par <  4
     0.62 when fm & age is 21.5 to 27.5 & sib <  3 &       3r & par <  4
     0.73 when fm & age <  16.5         & sib <  3 &       3r           
     0.89 when ml & age <   9.5         & sib <  3                      
     0.93 when fm                                  & 1s or 2n           
> print(rpart.rules(survived, style="tall", cover=TRUE, clip.facs=TRUE, varlen=-3, faclen=2))
survived is 0.05 with cover 2% when
    ml
    age < 9.5
    sib >= 3

survived is 0.11 with cover 1% when
    fm
    age >= 16.5
    sib < 3
    3r
    par >= 4

survived is 0.14 with cover 2% when
    fm
    sib >= 3
    3r

survived is 0.17 with cover 61% when
    ml
    age >= 9.5

survived is 0.39 with cover 3% when
    fm
    age >= 27.5
    sib < 3
    3r
    par < 4

survived is 0.39 with cover 2% when
    fm
    age is 16.5 to 21.5
    sib < 3
    3r
    par < 4

survived is 0.62 with cover 6% when
    fm
    age is 21.5 to 27.5
    sib < 3
    3r
    par < 4

survived is 0.73 with cover 3% when
    fm
    age < 16.5
    sib < 3
    3r

survived is 0.89 with cover 2% when
    ml
    age < 9.5
    sib < 3

survived is 0.93 with cover 19% when
    fm
    1s or 2n
> 
> print(rpart.rules(survived, varlen=-3, faclen=2, nn=TRUE))
  nn survived                                                                             
  10     0.05 when sex is ml & age <   9.5         & sib >= 3                             
  52     0.11 when sex is fm & age >=         16.5 & sib <  3 & pcl is       3r & par >= 4
  12     0.14 when sex is fm                       & sib >= 3 & pcl is       3r           
   4     0.17 when sex is ml & age >=          9.5                                        
 106     0.39 when sex is fm & age >=         27.5 & sib <  3 & pcl is       3r & par <  4
 214     0.39 when sex is fm & age is 16.5 to 21.5 & sib <  3 & pcl is       3r & par <  4
 215     0.62 when sex is fm & age is 21.5 to 27.5 & sib <  3 & pcl is       3r & par <  4
  27     0.73 when sex is fm & age <  16.5         & sib <  3 & pcl is       3r           
  11     0.89 when sex is ml & age <   9.5         & sib <  3                             
   7     0.93 when sex is fm                                  & pcl is 1s or 2n           
> print(rpart.rules(survived, varlen=-3, faclen=2, nn=TRUE, cover=TRUE))
  nn survived                                                                                cover
  10     0.05 when sex is ml & age <   9.5         & sib >= 3                                   2%
  52     0.11 when sex is fm & age >=         16.5 & sib <  3 & pcl is       3r & par >= 4      1%
  12     0.14 when sex is fm                       & sib >= 3 & pcl is       3r                 2%
   4     0.17 when sex is ml & age >=          9.5                                             61%
 106     0.39 when sex is fm & age >=         27.5 & sib <  3 & pcl is       3r & par <  4      3%
 214     0.39 when sex is fm & age is 16.5 to 21.5 & sib <  3 & pcl is       3r & par <  4      2%
 215     0.62 when sex is fm & age is 21.5 to 27.5 & sib <  3 & pcl is       3r & par <  4      6%
  27     0.73 when sex is fm & age <  16.5         & sib <  3 & pcl is       3r                 3%
  11     0.89 when sex is ml & age <   9.5         & sib <  3                                   2%
   7     0.93 when sex is fm                                  & pcl is 1s or 2n                19%
> print(rpart.rules(survived, style="wide", varlen=-3, faclen=2, nn=TRUE))
  nn survived                                                                             
  10     0.05 when sex is ml & age <   9.5         & sib >= 3                             
  52     0.11 when sex is fm & age >=         16.5 & sib <  3 & pcl is       3r & par >= 4
  12     0.14 when sex is fm                       & sib >= 3 & pcl is       3r           
   4     0.17 when sex is ml & age >=          9.5                                        
 106     0.39 when sex is fm & age >=         27.5 & sib <  3 & pcl is       3r & par <  4
 214     0.39 when sex is fm & age is 16.5 to 21.5 & sib <  3 & pcl is       3r & par <  4
 215     0.62 when sex is fm & age is 21.5 to 27.5 & sib <  3 & pcl is       3r & par <  4
  27     0.73 when sex is fm & age <  16.5         & sib <  3 & pcl is       3r           
  11     0.89 when sex is ml & age <   9.5         & sib <  3                             
   7     0.93 when sex is fm                                  & pcl is 1s or 2n           
> print(rpart.rules(survived, style="tall", varlen=-3, faclen=2, nn=TRUE))
[10]  survived is 0.05 when
          sex is ml
          age < 9.5
          sib >= 3

[52]  survived is 0.11 when
          sex is fm
          age >= 16.5
          sib < 3
          pcl is 3r
          par >= 4

[12]  survived is 0.14 when
          sex is fm
          sib >= 3
          pcl is 3r

[4]   survived is 0.17 when
          sex is ml
          age >= 9.5

[106] survived is 0.39 when
          sex is fm
          age >= 27.5
          sib < 3
          pcl is 3r
          par < 4

[214] survived is 0.39 when
          sex is fm
          age is 16.5 to 21.5
          sib < 3
          pcl is 3r
          par < 4

[215] survived is 0.62 when
          sex is fm
          age is 21.5 to 27.5
          sib < 3
          pcl is 3r
          par < 4

[27]  survived is 0.73 when
          sex is fm
          age < 16.5
          sib < 3
          pcl is 3r

[11]  survived is 0.89 when
          sex is ml
          age < 9.5
          sib < 3

[7]   survived is 0.93 when
          sex is fm
          pcl is 1s or 2n
> print(rpart.rules(survived, style="wide", clip.facs=TRUE, varlen=-3, faclen=2, nn=TRUE))
  nn survived                                                               
  10     0.05 when ml & age <   9.5         & sib >= 3                      
  52     0.11 when fm & age >=         16.5 & sib <  3 &       3r & par >= 4
  12     0.14 when fm                       & sib >= 3 &       3r           
   4     0.17 when ml & age >=          9.5                                 
 106     0.39 when fm & age >=         27.5 & sib <  3 &       3r & par <  4
 214     0.39 when fm & age is 16.5 to 21.5 & sib <  3 &       3r & par <  4
 215     0.62 when fm & age is 21.5 to 27.5 & sib <  3 &       3r & par <  4
  27     0.73 when fm & age <  16.5         & sib <  3 &       3r           
  11     0.89 when ml & age <   9.5         & sib <  3                      
   7     0.93 when fm                                  & 1s or 2n           
> print(rpart.rules(survived, style="tall", cover=TRUE, clip.facs=TRUE, varlen=-3, faclen=2, nn=TRUE))
[10]  survived is 0.05 with cover 2% when
          ml
          age < 9.5
          sib >= 3

[52]  survived is 0.11 with cover 1% when
          fm
          age >= 16.5
          sib < 3
          3r
          par >= 4

[12]  survived is 0.14 with cover 2% when
          fm
          sib >= 3
          3r

[4]   survived is 0.17 with cover 61% when
          ml
          age >= 9.5

[106] survived is 0.39 with cover 3% when
          fm
          age >= 27.5
          sib < 3
          3r
          par < 4

[214] survived is 0.39 with cover 2% when
          fm
          age is 16.5 to 21.5
          sib < 3
          3r
          par < 4

[215] survived is 0.62 with cover 6% when
          fm
          age is 21.5 to 27.5
          sib < 3
          3r
          par < 4

[27]  survived is 0.73 with cover 3% when
          fm
          age < 16.5
          sib < 3
          3r

[11]  survived is 0.89 with cover 2% when
          ml
          age < 9.5
          sib < 3

[7]   survived is 0.93 with cover 19% when
          fm
          1s or 2n
> 
> # rpart.predict (binomial response)
> stopifnot(identical(options("width"), old.width))
> owidth <- options("width")
> options(width=1e3)
> print(predict(survived)[1:5,])
       died  survived
1 0.0680000 0.9320000
2 0.1111111 0.8888889
3 0.0680000 0.9320000
4 0.8291457 0.1708543
5 0.0680000 0.9320000
> print(head(rpart.predict(survived, rules=TRUE)))
       died  survived                                                                                             
1 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
2 0.1111111 0.8888889 because sex is   male & age <   9.5         & sibsp <  3                                    
3 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
4 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
5 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
6 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
> print(head(rpart.predict(survived, rules=TRUE, when="")))
       died  survived                                                                                             
1 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
2 0.1111111 0.8888889 because sex is   male & age <   9.5         & sibsp <  3                                    
3 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
4 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
5 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
6 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
> print(head(rpart.predict(survived, rules=TRUE, when="WHEN")))
       died  survived                                                                                             
1 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
2 0.1111111 0.8888889 because sex is   male & age <   9.5         & sibsp <  3                                    
3 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
4 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
5 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
6 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
> print(head(rpart.predict(survived, rules=TRUE, when="", because="reason:")))
       died  survived                                                                                             
1 0.0680000 0.9320000 reason: sex is female                                    & pclass is 1st or 2nd             
2 0.1111111 0.8888889 reason: sex is   male & age <   9.5         & sibsp <  3                                    
3 0.0680000 0.9320000 reason: sex is female                                    & pclass is 1st or 2nd             
4 0.8291457 0.1708543 reason: sex is   male & age >=          9.5                                                 
5 0.0680000 0.9320000 reason: sex is female                                    & pclass is 1st or 2nd             
6 0.8291457 0.1708543 reason: sex is   male & age >=          9.5                                                 
> print(head(rpart.predict(survived, rules=TRUE, because="")))
       died  survived                                                                                     
1 0.0680000 0.9320000 sex is female                                    & pclass is 1st or 2nd             
2 0.1111111 0.8888889 sex is   male & age <   9.5         & sibsp <  3                                    
3 0.0680000 0.9320000 sex is female                                    & pclass is 1st or 2nd             
4 0.8291457 0.1708543 sex is   male & age >=          9.5                                                 
5 0.0680000 0.9320000 sex is female                                    & pclass is 1st or 2nd             
6 0.8291457 0.1708543 sex is   male & age >=          9.5                                                 
> options(width=owidth$width)
> stopifnot(identical(options("width"), old.width))
> stopifnot(all.equal(predict(survived), as.matrix(rpart.predict(survived, rules=TRUE)[,1:2])))
> stopifnot(max(abs(predict(survived) - rpart.predict(survived, rules=TRUE)[,1:2])) == 0)
> print(rpart.predict(survived, nn=TRUE)[1:5,])
       died  survived nn
1 0.0680000 0.9320000  7
2 0.1111111 0.8888889 11
3 0.0680000 0.9320000  7
4 0.8291457 0.1708543  4
5 0.0680000 0.9320000  7
> stopifnot(all.equal(predict(survived), as.matrix(rpart.predict(survived, nn=TRUE)[,1:2])))
> print(rpart.predict(survived, nn=TRUE, rules=TRUE)[1:5,])
       died  survived nn
1 0.0680000 0.9320000  7
2 0.1111111 0.8888889 11
3 0.0680000 0.9320000  7
4 0.8291457 0.1708543  4
5 0.0680000 0.9320000  7
                                                                                              
1 because sex is female                                    & pclass is 1st or 2nd             
2 because sex is   male & age <   9.5         & sibsp <  3                                    
3 because sex is female                                    & pclass is 1st or 2nd             
4 because sex is   male & age >=          9.5                                                 
5 because sex is female                                    & pclass is 1st or 2nd             
> stopifnot(all.equal(predict(survived), as.matrix(rpart.predict(survived, nn=TRUE, rules=TRUE)[,1:2])))
> 
> print(predict(survived, type="class")[1:5])
       1        2        3        4        5 
survived survived survived     died survived 
Levels: died survived
> print(rpart.predict(survived, type="class", rules=TRUE)[1:5,])
     class
1 survived
2 survived
3 survived
4     died
5 survived
                                                                                              
1 because sex is female                                    & pclass is 1st or 2nd             
2 because sex is   male & age <   9.5         & sibsp <  3                                    
3 because sex is female                                    & pclass is 1st or 2nd             
4 because sex is   male & age >=          9.5                                                 
5 because sex is female                                    & pclass is 1st or 2nd             
> pred <- rpart.predict(survived, type="class", rules=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(survived, type="class"), pred))
> print(rpart.predict(survived, type="class", rules=TRUE)[1:5,])
     class
1 survived
2 survived
3 survived
4     died
5 survived
                                                                                              
1 because sex is female                                    & pclass is 1st or 2nd             
2 because sex is   male & age <   9.5         & sibsp <  3                                    
3 because sex is female                                    & pclass is 1st or 2nd             
4 because sex is   male & age >=          9.5                                                 
5 because sex is female                                    & pclass is 1st or 2nd             
> print(rpart.predict(survived, type="class", rules=TRUE, nn=TRUE)[1:5,])
     class nn
1 survived  7
2 survived 11
3 survived  7
4     died  4
5 survived  7
                                                                                              
1 because sex is female                                    & pclass is 1st or 2nd             
2 because sex is   male & age <   9.5         & sibsp <  3                                    
3 because sex is female                                    & pclass is 1st or 2nd             
4 because sex is   male & age >=          9.5                                                 
5 because sex is female                                    & pclass is 1st or 2nd             
> pred <- rpart.predict(survived, type="class", rules=TRUE, nn=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(survived, type="class"), pred))
> 
> print(rpart.predict(survived, newdata=ptitanic[5:8,]))
       died  survived
5 0.0680000 0.9320000
6 0.8291457 0.1708543
7 0.0680000 0.9320000
8 0.8291457 0.1708543
> print(rpart.predict(survived, newdata=ptitanic[5:8,], nn=TRUE))
       died  survived nn
5 0.0680000 0.9320000  7
6 0.8291457 0.1708543  4
7 0.0680000 0.9320000  7
8 0.8291457 0.1708543  4
> print(rpart.predict(survived, newdata=ptitanic[5:8,], rules=TRUE))
       died  survived                                                          
5 0.0680000 0.9320000 because sex is female              & pclass is 1st or 2nd
6 0.8291457 0.1708543 because sex is   male & age >= 9.5                       
7 0.0680000 0.9320000 because sex is female              & pclass is 1st or 2nd
8 0.8291457 0.1708543 because sex is   male & age >= 9.5                       
> print(rpart.predict(survived, newdata=ptitanic[5:8,], nn=TRUE, rules=TRUE))
       died  survived nn
5 0.0680000 0.9320000  7
6 0.8291457 0.1708543  4
7 0.0680000 0.9320000  7
8 0.8291457 0.1708543  4
                                                           
5 because sex is female              & pclass is 1st or 2nd
6 because sex is   male & age >= 9.5                       
7 because sex is female              & pclass is 1st or 2nd
8 because sex is   male & age >= 9.5                       
> 
> Reliability <- rpart(Reliability ~ ., data=cu.summary)
> print(rpart.rules(Reliability, style='wide'))
 Reliability  Muc wor ave bet Muc                                                                                              
  Much worse [.78 .00 .22 .00 .00] when Country is Germany or Korea or Mexico or Sweden or USA & Type is                 Sporty
       worse [.29 .43 .07 .21 .00] when Country is                                Korea or USA & Type is       Compact or Small
     average [.00 .25 .50 .25 .00] when Country is                 Germany or Mexico or Sweden & Type is       Compact or Small
     average [.26 .15 .59 .00 .00] when Country is Germany or Korea or Mexico or Sweden or USA & Type is Large or Medium or Van
 Much better [.00 .00 .11 .11 .78] when Country is                          Japan or Japan/USA                                 
> print(rpart.rules(Reliability, style='tall'))
                            Muc wor ave bet Muc 
Reliability is Much worse  [.78 .00 .22 .00 .00] when
    Country is Germany or Korea or Mexico or Sweden or USA
    Type is Sporty

Reliability is worse       [.29 .43 .07 .21 .00] when
    Country is Korea or USA
    Type is Compact or Small

Reliability is average     [.00 .25 .50 .25 .00] when
    Country is Germany or Mexico or Sweden
    Type is Compact or Small

Reliability is average     [.26 .15 .59 .00 .00] when
    Country is Germany or Korea or Mexico or Sweden or USA
    Type is Large or Medium or Van

Reliability is Much better [.00 .00 .11 .11 .78] when
    Country is Japan or Japan/USA
> print(rpart.rules(Reliability, style='tallw'))
                            Muc wor ave bet Muc 
Reliability is Much worse  [.78 .00 .22 .00 .00] when
                                                 Country is Germany or Korea or Mexico or Sweden or USA
                                                 Type is Sporty

Reliability is worse       [.29 .43 .07 .21 .00] when
                                                 Country is Korea or USA
                                                 Type is Compact or Small

Reliability is average     [.00 .25 .50 .25 .00] when
                                                 Country is Germany or Mexico or Sweden
                                                 Type is Compact or Small

Reliability is average     [.26 .15 .59 .00 .00] when
                                                 Country is Germany or Korea or Mexico or Sweden or USA
                                                 Type is Large or Medium or Van

Reliability is Much better [.00 .00 .11 .11 .78] when
                                                 Country is Japan or Japan/USA
> print(rpart.rules(Reliability, style='wide', clip.facs=TRUE))
 Reliability  Muc wor ave bet Muc                                                                           
  Much worse [.78 .00 .22 .00 .00] when Germany or Korea or Mexico or Sweden or USA &                 Sporty
       worse [.29 .43 .07 .21 .00] when                                Korea or USA &       Compact or Small
     average [.00 .25 .50 .25 .00] when                 Germany or Mexico or Sweden &       Compact or Small
     average [.26 .15 .59 .00 .00] when Germany or Korea or Mexico or Sweden or USA & Large or Medium or Van
 Much better [.00 .00 .11 .11 .78] when                          Japan or Japan/USA                         
> print(rpart.rules(Reliability, style='tall', clip.facs=TRUE))
                            Muc wor ave bet Muc 
Reliability is Much worse  [.78 .00 .22 .00 .00] when
    Germany or Korea or Mexico or Sweden or USA
    Sporty

Reliability is worse       [.29 .43 .07 .21 .00] when
    Korea or USA
    Compact or Small

Reliability is average     [.00 .25 .50 .25 .00] when
    Germany or Mexico or Sweden
    Compact or Small

Reliability is average     [.26 .15 .59 .00 .00] when
    Germany or Korea or Mexico or Sweden or USA
    Large or Medium or Van

Reliability is Much better [.00 .00 .11 .11 .78] when
    Japan or Japan/USA
> print(rpart.rules(Reliability, style='tallw', clip.facs=TRUE))
                            Muc wor ave bet Muc 
Reliability is Much worse  [.78 .00 .22 .00 .00] when
                                                 Germany or Korea or Mexico or Sweden or USA
                                                 Sporty

Reliability is worse       [.29 .43 .07 .21 .00] when
                                                 Korea or USA
                                                 Compact or Small

Reliability is average     [.00 .25 .50 .25 .00] when
                                                 Germany or Mexico or Sweden
                                                 Compact or Small

Reliability is average     [.26 .15 .59 .00 .00] when
                                                 Germany or Korea or Mexico or Sweden or USA
                                                 Large or Medium or Van

Reliability is Much better [.00 .00 .11 .11 .78] when
                                                 Japan or Japan/USA
> 
> cat0("\n=== \"extra\" argument, binomial model ===\n")

=== "extra" argument, binomial model ===
> 
> options(warn=2) # treat warnings as errors
> cat0("extra=\"auto\"\n")
extra="auto"
> print(rpart.rules(survived, extra="auto"))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=\"A\"\n")
extra="A"
> print(rpart.rules(survived, extra="A"))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> stopifnot(identical(rpart.rules(survived), rpart.rules(survived, extra=0))) # extra=0 same as "auto"
> cat0("extra=1\n")
extra=1
> expect.err(try(rpart.rules(survived, extra=1)), "extra=1 is not supported by rpart.rules")
Error : (converted from warning) extra=1 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(rpart.rules(survived, extra = 1))
> cat0("extra=2\n")
extra=2
> expect.err(try(rpart.rules(survived, extra=2)), "extra=2 is not supported by rpart.rules")
Error : (converted from warning) extra=2 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(rpart.rules(survived, extra = 2))
> cat0("extra=3\n")
extra=3
> expect.err(try(rpart.rules(survived, extra=3)), "extra=3 is not supported by rpart.rules")
Error : (converted from warning) extra=3 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(rpart.rules(survived, extra = 3))
> cat0("extra=4\n")
extra=4
> print(rpart.rules(survived, extra=4))
 survived  die sur                                                                                           
     died [.95 .05] when sex is   male & age <   9.5         & sibsp >= 3                                    
     died [.89 .11] when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     died [.86 .14] when sex is female                       & sibsp >= 3 & pclass is        3rd             
     died [.83 .17] when sex is   male & age >=          9.5                                                 
     died [.61 .39] when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     died [.61 .39] when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.38 .62] when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.27 .73] when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
 survived [.11 .89] when sex is   male & age <   9.5         & sibsp <  3                                    
 survived [.07 .93] when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=5\n")
extra=5
> print(rpart.rules(survived, extra=5))
 survived  die sur                                                                                           
     died [.95 .05] when sex is   male & age <   9.5         & sibsp >= 3                                    
     died [.89 .11] when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     died [.86 .14] when sex is female                       & sibsp >= 3 & pclass is        3rd             
     died [.83 .17] when sex is   male & age >=          9.5                                                 
     died [.61 .39] when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     died [.61 .39] when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.38 .62] when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.27 .73] when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
 survived [.11 .89] when sex is   male & age <   9.5         & sibsp <  3                                    
 survived [.07 .93] when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=6\n")
extra=6
> print(rpart.rules(survived, extra=6))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=7\n")
extra=7
> print(rpart.rules(survived, extra=7))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=8\n")
extra=8
> print(rpart.rules(survived, extra=8))
 survived                                                                                          
     0.61 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.61 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.83 when sex is   male & age >=          9.5                                                 
     0.86 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.89 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.95 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=9\n")
extra=9
> print(rpart.rules(survived, extra=9))
 survived  die sur                                                                                           
     died [.01 .00] when sex is   male & age <   9.5         & sibsp >= 3                                    
     died [.01 .00] when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     died [.01 .00] when sex is female                       & sibsp >= 3 & pclass is        3rd             
     died [.01 .01] when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     died [.02 .01] when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     died [.50 .10] when sex is   male & age >=          9.5                                                 
 survived [.00 .02] when sex is   male & age <   9.5         & sibsp <  3                                    
 survived [.01 .02] when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
 survived [.02 .04] when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.01 .18] when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=10\n")
extra=10
> print(rpart.rules(survived, extra=10))
 survived                                                                                          
     0.00 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.00 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.00 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.01 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.01 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.10 when sex is   male & age >=          9.5                                                 
     0.02 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.02 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.04 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.18 when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=11\n")
extra=11
> print(rpart.rules(survived, extra=11))
 survived                                                                                          
     0.00 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.00 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.00 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.01 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.01 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.10 when sex is   male & age >=          9.5                                                 
     0.02 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.02 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.04 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.18 when sex is female                                    & pclass is 1st or 2nd             
> cat0("extra=123\n")
extra=123
> expect.err(try(rpart.rules(survived, extra=123)), "extra=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=23 is illegal
Got error as expected from try(rpart.rules(survived, extra = 123))
> options(warn=1) # print warnings as they occur
> 
> cat0("\n=== \"extra\" argument, multiclass model ===\n")

=== "extra" argument, multiclass model ===
> 
> Species <- rpart(Species ~ ., data=iris)
> options(warn=2) # treat warnings as errors
> cat0("default extra\n")
default extra
> print(rpart.rules(Species))
    Species  seto vers virg                                               
     setosa [1.00  .00  .00] when Petal.Length <  2.5                     
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8
> cat0("extra=0\n")
extra=0
> print(rpart.rules(Species, extra=0))
    Species  seto vers virg                                               
     setosa [1.00  .00  .00] when Petal.Length <  2.5                     
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8
> cat0("extra=1\n")
extra=1
> expect.err(try(print(rpart.rules(Species, extra=1))), "extra=1 is not supported by rpart.rules")
Error : (converted from warning) extra=1 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(print(rpart.rules(Species, extra = 1)))
> cat0("extra=2\n")
extra=2
> expect.err(try(print(rpart.rules(Species, extra=2))), "extra=2 is not supported by rpart.rules")
Error : (converted from warning) extra=2 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(print(rpart.rules(Species, extra = 2)))
> cat0("extra=3\n")
extra=3
> expect.err(try(print(rpart.rules(Species, extra=3))), "extra=3 is not supported by rpart.rules")
Error : (converted from warning) extra=3 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(print(rpart.rules(Species, extra = 3)))
> cat0("extra=4\n")
extra=4
> print(rpart.rules(Species, extra=4))
    Species  seto vers virg                                               
     setosa [1.00  .00  .00] when Petal.Length <  2.5                     
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8
> cat0("extra=5\n")
extra=5
> print(rpart.rules(Species, extra=5))
    Species  seto vers virg                                               
     setosa [1.00  .00  .00] when Petal.Length <  2.5                     
 versicolor [ .00  .91  .09] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [ .00  .02  .98] when Petal.Length >= 2.5 & Petal.Width >= 1.8
> cat0("extra=6\n")
extra=6
> expect.err(try(print(rpart.rules(Species, extra=6))), "extra=106 but the response has 3 levels (only the 2nd level is displayed)")
Error : (converted from warning) extra=106 but the response has 3 levels (only the 2nd level is displayed)
Got error as expected from try(print(rpart.rules(Species, extra = 6)))
> cat0("extra=7\n")
extra=7
> expect.err(try(print(rpart.rules(Species, extra=7))), "extra=106 but the response has 3 levels (only the 2nd level is displayed)")
Error : (converted from warning) extra=106 but the response has 3 levels (only the 2nd level is displayed)
Got error as expected from try(print(rpart.rules(Species, extra = 7)))
> cat0("extra=8\n")
extra=8
> print(rpart.rules(Species, extra=8))
 Species                                              
    1.00 when Petal.Length <  2.5                     
    0.91 when Petal.Length >= 2.5 & Petal.Width <  1.8
    0.98 when Petal.Length >= 2.5 & Petal.Width >= 1.8
> cat0("extra=9\n")
extra=9
> print(rpart.rules(Species, extra=9))
    Species  set ver vir                                               
     setosa [.33 .00 .00] when Petal.Length <  2.5                     
 versicolor [.00 .33 .03] when Petal.Length >= 2.5 & Petal.Width <  1.8
  virginica [.00 .01 .30] when Petal.Length >= 2.5 & Petal.Width >= 1.8
> cat0("extra=10\n")
extra=10
> expect.err(try(print(rpart.rules(Species, extra=10))), "extra=110 but the response has 3 levels (only the 2nd level is displayed)")
Error : (converted from warning) extra=110 but the response has 3 levels (only the 2nd level is displayed)
Got error as expected from try(print(rpart.rules(Species, extra = 10)))
> cat0("extra=11\n")
extra=11
> expect.err(try(print(rpart.rules(Species, extra=11))), "extra=110 but the response has 3 levels (only the 2nd level is displayed)")
Error : (converted from warning) extra=110 but the response has 3 levels (only the 2nd level is displayed)
Got error as expected from try(print(rpart.rules(Species, extra = 11)))
> cat0("extra=12\n")
extra=12
> expect.err(try(print(rpart.rules(Species, 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(print(rpart.rules(Species, extra = 12)))
> options(warn=1) # print warnings as they occur
> 
> cat0("\n=== \"and\" and related arguments ===\n")

=== "and" and related arguments ===
> 
> print(rpart.rules(survived))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> print(rpart.rules(survived, and="AND"))
 survived                                                                                                  
     0.05 when sex is   male AND age <   9.5         AND sibsp >= 3                                        
     0.11 when sex is female AND age >=         16.5 AND sibsp <  3 AND pclass is        3rd AND parch >= 4
     0.14 when sex is female                         AND sibsp >= 3 AND pclass is        3rd               
     0.17 when sex is   male AND age >=          9.5                                                       
     0.39 when sex is female AND age >=         27.5 AND sibsp <  3 AND pclass is        3rd AND parch <  4
     0.39 when sex is female AND age is 16.5 to 21.5 AND sibsp <  3 AND pclass is        3rd AND parch <  4
     0.62 when sex is female AND age is 21.5 to 27.5 AND sibsp <  3 AND pclass is        3rd AND parch <  4
     0.73 when sex is female AND age <  16.5         AND sibsp <  3 AND pclass is        3rd               
     0.89 when sex is   male AND age <   9.5         AND sibsp <  3                                        
     0.93 when sex is female                                        AND pclass is 1st or 2nd               
> print(rpart.rules(survived, and="AND",   eq="=", style="tall"))  # 'and' isn't displayed for style="tall"
survived = 0.05 when
    sex = male
    age < 9.5
    sibsp >= 3

survived = 0.11 when
    sex = female
    age >= 16.5
    sibsp < 3
    pclass = 3rd
    parch >= 4

survived = 0.14 when
    sex = female
    sibsp >= 3
    pclass = 3rd

survived = 0.17 when
    sex = male
    age >= 9.5

survived = 0.39 when
    sex = female
    age >= 27.5
    sibsp < 3
    pclass = 3rd
    parch < 4

survived = 0.39 when
    sex = female
    age = 16.5 to 21.5
    sibsp < 3
    pclass = 3rd
    parch < 4

survived = 0.62 when
    sex = female
    age = 21.5 to 27.5
    sibsp < 3
    pclass = 3rd
    parch < 4

survived = 0.73 when
    sex = female
    age < 16.5
    sibsp < 3
    pclass = 3rd

survived = 0.89 when
    sex = male
    age < 9.5
    sibsp < 3

survived = 0.93 when
    sex = female
    pclass = 1st or 2nd
> print(rpart.rules(survived, and=" AND ", eq="=", style="tallw")) # 'and' isn't displayed for style="tallw"
survived = 0.05 when
                 sex = male
                 age < 9.5
                 sibsp >= 3

survived = 0.11 when
                 sex = female
                 age >= 16.5
                 sibsp < 3
                 pclass = 3rd
                 parch >= 4

survived = 0.14 when
                 sex = female
                 sibsp >= 3
                 pclass = 3rd

survived = 0.17 when
                 sex = male
                 age >= 9.5

survived = 0.39 when
                 sex = female
                 age >= 27.5
                 sibsp < 3
                 pclass = 3rd
                 parch < 4

survived = 0.39 when
                 sex = female
                 age = 16.5 to 21.5
                 sibsp < 3
                 pclass = 3rd
                 parch < 4

survived = 0.62 when
                 sex = female
                 age = 21.5 to 27.5
                 sibsp < 3
                 pclass = 3rd
                 parch < 4

survived = 0.73 when
                 sex = female
                 age < 16.5
                 sibsp < 3
                 pclass = 3rd

survived = 0.89 when
                 sex = male
                 age < 9.5
                 sibsp < 3

survived = 0.93 when
                 sex = female
                 pclass = 1st or 2nd
> 
> print(rpart.rules(survived, response.name="survived probability", eq="=", lt="<", ge=">=", and="&&", facsep=","))
 survived probability                                                                                         
                 0.05 when sex =   male && age  <  9.5         && sibsp >= 3                                  
                 0.11 when sex = female && age >=         16.5 && sibsp  < 3 && pclass =     3rd && parch >= 4
                 0.14 when sex = female                        && sibsp >= 3 && pclass =     3rd              
                 0.17 when sex =   male && age >=          9.5                                                
                 0.39 when sex = female && age >=         27.5 && sibsp  < 3 && pclass =     3rd && parch  < 4
                 0.39 when sex = female && age  = 16.5 to 21.5 && sibsp  < 3 && pclass =     3rd && parch  < 4
                 0.62 when sex = female && age  = 21.5 to 27.5 && sibsp  < 3 && pclass =     3rd && parch  < 4
                 0.73 when sex = female && age  < 16.5         && sibsp  < 3 && pclass =     3rd              
                 0.89 when sex =   male && age  <  9.5         && sibsp  < 3                                  
                 0.93 when sex = female                                      && pclass = 1st,2nd              
> 
> print(rpart.rules(survived, style="tall",
+                   response.name="survived probability", facsep=" or ",
+                   eq="is", lt="is less than", ge="is greater than", and="and"))
survived probability is 0.05 when
    sex is male
    age is less than 9.5
    sibsp is greater than 3

survived probability is 0.11 when
    sex is female
    age is greater than 16.5
    sibsp is less than 3
    pclass is 3rd
    parch is greater than 4

survived probability is 0.14 when
    sex is female
    sibsp is greater than 3
    pclass is 3rd

survived probability is 0.17 when
    sex is male
    age is greater than 9.5

survived probability is 0.39 when
    sex is female
    age is greater than 27.5
    sibsp is less than 3
    pclass is 3rd
    parch is less than 4

survived probability is 0.39 when
    sex is female
    age is 16.5 to 21.5
    sibsp is less than 3
    pclass is 3rd
    parch is less than 4

survived probability is 0.62 when
    sex is female
    age is 21.5 to 27.5
    sibsp is less than 3
    pclass is 3rd
    parch is less than 4

survived probability is 0.73 when
    sex is female
    age is less than 16.5
    sibsp is less than 3
    pclass is 3rd

survived probability is 0.89 when
    sex is male
    age is less than 9.5
    sibsp is less than 3

survived probability is 0.93 when
    sex is female
    pclass is 1st or 2nd
> 
> expect.err(try(rpart.rules(survived, and=99)), "'and' is not a character variable")
Error : 'and' is not a character variable (class(and) is "numeric")
Got error as expected from try(rpart.rules(survived, and = 99))
> expect.err(try(rpart.rules(survived, response.name=99)), "'response.name' is not a character variable")
Error : 'response.name' is not a character variable (class(response.name) is "numeric")
Got error as expected from try(rpart.rules(survived, response.name = 99))
> expect.err(try(rpart.rules(survived, response.name=c("a", "b"))), "'response.name' has more than one element")
Error : 'response.name' has more than one element
       response.name = c("a" "b")
Got error as expected from try(rpart.rules(survived, response.name = c("a", "b")))
> expect.err(try(rpart.rules(survived, eq=NULL)), "'eq' is NULL (it should be a string)")
Error : 'eq' is NULL (it should be a string)
Got error as expected from try(rpart.rules(survived, eq = NULL))
> rpart.rules(survived, lt="")
 survived                                                                                          
     0.05 when sex is   male & age     9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp    3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp    3 & pclass is        3rd & parch    4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp    3 & pclass is        3rd & parch    4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp    3 & pclass is        3rd & parch    4
     0.73 when sex is female & age    16.5         & sibsp    3 & pclass is        3rd             
     0.89 when sex is   male & age     9.5         & sibsp    3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, ge="")
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp    3                                    
     0.11 when sex is female & age            16.5 & sibsp <  3 & pclass is        3rd & parch    4
     0.14 when sex is female                       & sibsp    3 & pclass is        3rd             
     0.17 when sex is   male & age             9.5                                                 
     0.39 when sex is female & age            27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, and="")
 survived                                                                                  
     0.05 when sex is   male age <   9.5         sibsp >= 3                                
     0.11 when sex is female age >=         16.5 sibsp <  3 pclass is        3rd parch >= 4
     0.14 when sex is female                     sibsp >= 3 pclass is        3rd           
     0.17 when sex is   male age >=          9.5                                           
     0.39 when sex is female age >=         27.5 sibsp <  3 pclass is        3rd parch <  4
     0.39 when sex is female age is 16.5 to 21.5 sibsp <  3 pclass is        3rd parch <  4
     0.62 when sex is female age is 21.5 to 27.5 sibsp <  3 pclass is        3rd parch <  4
     0.73 when sex is female age <  16.5         sibsp <  3 pclass is        3rd           
     0.89 when sex is   male age <   9.5         sibsp <  3                                
     0.93 when sex is female                                pclass is 1st or 2nd           
> rpart.rules(survived, when="if")
 survived                                                                                        
     0.05 if sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 if sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 if sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 if sex is   male & age >=          9.5                                                 
     0.39 if sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 if sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 if sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 if sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 if sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 if sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, when=" if ")
 survived                                                                                        
     0.05 if sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 if sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 if sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 if sex is   male & age >=          9.5                                                 
     0.39 if sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 if sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 if sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 if sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 if sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 if sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, when="     when")
 survived                                                                                             
     0.05    when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11    when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14    when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17    when sex is   male & age >=          9.5                                                 
     0.39    when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39    when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62    when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73    when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89    when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93    when sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, when="")
 survived                                                                                     
     0.05 sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 sex is   male & age >=          9.5                                                 
     0.39 sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, extra=4)
 survived  die sur                                                                                           
     died [.95 .05] when sex is   male & age <   9.5         & sibsp >= 3                                    
     died [.89 .11] when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     died [.86 .14] when sex is female                       & sibsp >= 3 & pclass is        3rd             
     died [.83 .17] when sex is   male & age >=          9.5                                                 
     died [.61 .39] when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     died [.61 .39] when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.38 .62] when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.27 .73] when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
 survived [.11 .89] when sex is   male & age <   9.5         & sibsp <  3                                    
 survived [.07 .93] when sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, extra=4, when="reason:")
 survived  die sur                                                                                              
     died [.95 .05] reason: sex is   male & age <   9.5         & sibsp >= 3                                    
     died [.89 .11] reason: sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     died [.86 .14] reason: sex is female                       & sibsp >= 3 & pclass is        3rd             
     died [.83 .17] reason: sex is   male & age >=          9.5                                                 
     died [.61 .39] reason: sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     died [.61 .39] reason: sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.38 .62] reason: sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
 survived [.27 .73] reason: sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
 survived [.11 .89] reason: sex is   male & age <   9.5         & sibsp <  3                                    
 survived [.07 .93] reason: sex is female                                    & pclass is 1st or 2nd             
> rpart.rules(survived, extra=4, style="tall", when="\nreason:")
                      die sur 
survived is died     [.95 .05] 
reason:
    sex is male
    age < 9.5
    sibsp >= 3

survived is died     [.89 .11] 
reason:
    sex is female
    age >= 16.5
    sibsp < 3
    pclass is 3rd
    parch >= 4

survived is died     [.86 .14] 
reason:
    sex is female
    sibsp >= 3
    pclass is 3rd

survived is died     [.83 .17] 
reason:
    sex is male
    age >= 9.5

survived is died     [.61 .39] 
reason:
    sex is female
    age >= 27.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is died     [.61 .39] 
reason:
    sex is female
    age is 16.5 to 21.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is survived [.38 .62] 
reason:
    sex is female
    age is 21.5 to 27.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is survived [.27 .73] 
reason:
    sex is female
    age < 16.5
    sibsp < 3
    pclass is 3rd

survived is survived [.11 .89] 
reason:
    sex is male
    age < 9.5
    sibsp < 3

survived is survived [.07 .93] 
reason:
    sex is female
    pclass is 1st or 2nd
> rpart.rules(survived, extra=4, style="tall", when="\n\n")
                      die sur 
survived is died     [.95 .05] 


    sex is male
    age < 9.5
    sibsp >= 3

survived is died     [.89 .11] 


    sex is female
    age >= 16.5
    sibsp < 3
    pclass is 3rd
    parch >= 4

survived is died     [.86 .14] 


    sex is female
    sibsp >= 3
    pclass is 3rd

survived is died     [.83 .17] 


    sex is male
    age >= 9.5

survived is died     [.61 .39] 


    sex is female
    age >= 27.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is died     [.61 .39] 


    sex is female
    age is 16.5 to 21.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is survived [.38 .62] 


    sex is female
    age is 21.5 to 27.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is survived [.27 .73] 


    sex is female
    age < 16.5
    sibsp < 3
    pclass is 3rd

survived is survived [.11 .89] 


    sex is male
    age < 9.5
    sibsp < 3

survived is survived [.07 .93] 


    sex is female
    pclass is 1st or 2nd
> rpart.rules(survived, extra=4, style="tall", when="")
                      die sur 
survived is died     [.95 .05] 
    sex is male
    age < 9.5
    sibsp >= 3

survived is died     [.89 .11] 
    sex is female
    age >= 16.5
    sibsp < 3
    pclass is 3rd
    parch >= 4

survived is died     [.86 .14] 
    sex is female
    sibsp >= 3
    pclass is 3rd

survived is died     [.83 .17] 
    sex is male
    age >= 9.5

survived is died     [.61 .39] 
    sex is female
    age >= 27.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is died     [.61 .39] 
    sex is female
    age is 16.5 to 21.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is survived [.38 .62] 
    sex is female
    age is 21.5 to 27.5
    sibsp < 3
    pclass is 3rd
    parch < 4

survived is survived [.27 .73] 
    sex is female
    age < 16.5
    sibsp < 3
    pclass is 3rd

survived is survived [.11 .89] 
    sex is male
    age < 9.5
    sibsp < 3

survived is survived [.07 .93] 
    sex is female
    pclass is 1st or 2nd
> print.data.frame(rpart.rules(survived))
    survived                                                                  
10      0.05 when sex is   male & age <   9.5         & sibsp >= 3            
52      0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is
12      0.14 when sex is female                       & sibsp >= 3 & pclass is
4       0.17 when sex is   male & age >=          9.5                         
106     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is
214     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is
215     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is
27      0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is
11      0.89 when sex is   male & age <   9.5         & sibsp <  3            
7       0.93 when sex is female                                    & pclass is
                           
10                         
52         3rd & parch >= 4
12         3rd             
4                          
106        3rd & parch <  4
214        3rd & parch <  4
215        3rd & parch <  4
27         3rd             
11                         
7   1st or 2nd             
> print.default(rpart.rules(survived))
$survived
 [1] "0.05" "0.11" "0.14" "0.17" "0.39" "0.39" "0.62" "0.73" "0.89" "0.93"

[[2]]
 [1] "when" "when" "when" "when" "when" "when" "when" "when" "when" "when"

[[3]]
 [1] "sex" "sex" "sex" "sex" "sex" "sex" "sex" "sex" "sex" "sex"

[[4]]
 [1] "is" "is" "is" "is" "is" "is" "is" "is" "is" "is"

[[5]]
 [1] "male"   "female" "female" "male"   "female" "female" "female" "female"
 [9] "male"   "female"

[[6]]
 [1] "&" "&" ""  "&" "&" "&" "&" "&" "&" "" 

[[7]]
 [1] "age" "age" ""    "age" "age" "age" "age" "age" "age" ""   

[[8]]
 [1] "< " ">=" ""   ">=" ">=" "is" "is" "< " "< " ""  

[[9]]
 [1] " 9.5" ""     ""     ""     ""     "16.5" "21.5" "16.5" " 9.5" ""    

[[10]]
 [1] ""   ""   ""   ""   ""   "to" "to" ""   ""   ""  

[[11]]
 [1] ""     "16.5" ""     " 9.5" "27.5" "21.5" "27.5" ""     ""     ""    

[[12]]
 [1] "&" "&" "&" ""  "&" "&" "&" "&" "&" "" 

[[13]]
 [1] "sibsp" "sibsp" "sibsp" ""      "sibsp" "sibsp" "sibsp" "sibsp" "sibsp"
[10] ""     

[[14]]
 [1] ">=" "< " ">=" ""   "< " "< " "< " "< " "< " ""  

[[15]]
 [1] "3" "3" "3" ""  "3" "3" "3" "3" "3" "" 

[[16]]
 [1] ""  "&" "&" ""  "&" "&" "&" "&" ""  "&"

[[17]]
 [1] ""       "pclass" "pclass" ""       "pclass" "pclass" "pclass" "pclass"
 [9] ""       "pclass"

[[18]]
 [1] ""   "is" "is" ""   "is" "is" "is" "is" ""   "is"

[[19]]
 [1] ""           "3rd"        "3rd"        ""           "3rd"       
 [6] "3rd"        "3rd"        "3rd"        ""           "1st or 2nd"

[[20]]
 [1] ""  "&" ""  ""  "&" "&" "&" ""  ""  "" 

[[21]]
 [1] ""      "parch" ""      ""      "parch" "parch" "parch" ""      ""     
[10] ""     

[[22]]
 [1] ""   ">=" ""   ""   "< " "< " "< " ""   ""   ""  

[[23]]
 [1] ""  "4" ""  ""  "4" "4" "4" ""  ""  "" 

attr(,"class")
[1] "rpart.rules" "data.frame" 
attr(,"style")
[1] "wide"
attr(,"eq")
[1] "is"
attr(,"and")
[1] "&"
attr(,"when")
[1] "when"
> 
> cat0("\n=== null model ===\n")

=== null model ===
> data(ptitanic)
> null.model <- rpart(survived~sibsp, data=ptitanic)
> print(rpart.rules(null.model))
 survived           
     0.38 null model
> print(rpart.rules(null.model, style='wide'))
 survived           
     0.38 null model
> print(rpart.rules(null.model, style='tall'))
survived is 0.38 null model
   
> print(rpart.rules(null.model, null.model="NO RULES"))
 survived         
     0.38 NO RULES
> print(rpart.rules(null.model, style='wide', null.model="NO RULES"))
 survived         
     0.38 NO RULES
> print(rpart.rules(null.model, style='tall', null.model="NO RULES"))
survived is 0.38 NO RULES
   
> 
> # rpart.predict (null.model)
> print(predict(null.model)[1:5,])
      died survived
1 0.618029 0.381971
2 0.618029 0.381971
3 0.618029 0.381971
4 0.618029 0.381971
5 0.618029 0.381971
> print(head(rpart.predict(null.model, rules=TRUE)))
      died survived                   
1 0.618029 0.381971 because null model
2 0.618029 0.381971 because null model
3 0.618029 0.381971 because null model
4 0.618029 0.381971 because null model
5 0.618029 0.381971 because null model
6 0.618029 0.381971 because null model
> print(head(rpart.predict(null.model, rules=TRUE, null.model="NO RULES")))
      died survived                 
1 0.618029 0.381971 because NO RULES
2 0.618029 0.381971 because NO RULES
3 0.618029 0.381971 because NO RULES
4 0.618029 0.381971 because NO RULES
5 0.618029 0.381971 because NO RULES
6 0.618029 0.381971 because NO RULES
> print(head(rpart.predict(null.model, rules=TRUE, when="")))
      died survived                   
1 0.618029 0.381971 because null model
2 0.618029 0.381971 because null model
3 0.618029 0.381971 because null model
4 0.618029 0.381971 because null model
5 0.618029 0.381971 because null model
6 0.618029 0.381971 because null model
> print(head(rpart.predict(null.model, rules=TRUE, when="WHEN")))
      died survived                   
1 0.618029 0.381971 because null model
2 0.618029 0.381971 because null model
3 0.618029 0.381971 because null model
4 0.618029 0.381971 because null model
5 0.618029 0.381971 because null model
6 0.618029 0.381971 because null model
> print(head(rpart.predict(null.model, rules=TRUE, when="", because="reason:")))
      died survived                   
1 0.618029 0.381971 reason: null model
2 0.618029 0.381971 reason: null model
3 0.618029 0.381971 reason: null model
4 0.618029 0.381971 reason: null model
5 0.618029 0.381971 reason: null model
6 0.618029 0.381971 reason: null model
> print(head(rpart.predict(null.model, rules=TRUE, because="")))
      died survived           
1 0.618029 0.381971 null model
2 0.618029 0.381971 null model
3 0.618029 0.381971 null model
4 0.618029 0.381971 null model
5 0.618029 0.381971 null model
6 0.618029 0.381971 null model
> stopifnot(all.equal(predict(null.model), as.matrix(rpart.predict(null.model, rules=TRUE)[,1:2])))
> stopifnot(max(abs(predict(null.model) - rpart.predict(null.model, rules=TRUE)[,1:2])) == 0)
> print(rpart.predict(null.model, nn=TRUE)[1:5,])
      died survived nn
1 0.618029 0.381971  1
2 0.618029 0.381971  1
3 0.618029 0.381971  1
4 0.618029 0.381971  1
5 0.618029 0.381971  1
> stopifnot(all.equal(predict(null.model), as.matrix(rpart.predict(null.model, nn=TRUE)[,1:2])))
> print(rpart.predict(null.model, nn=TRUE, rules=TRUE)[1:5,])
      died survived nn                   
1 0.618029 0.381971  1 because null model
2 0.618029 0.381971  1 because null model
3 0.618029 0.381971  1 because null model
4 0.618029 0.381971  1 because null model
5 0.618029 0.381971  1 because null model
> stopifnot(all.equal(predict(null.model), as.matrix(rpart.predict(null.model, nn=TRUE, rules=TRUE)[,1:2])))
> 
> print(predict(null.model, type="class")[1:5])
   1    2    3    4    5 
died died died died died 
Levels: died survived
> print(rpart.predict(null.model, type="class", rules=TRUE, because="     reason: ")[1:5,])
  class                      
1  died    reason: null model
2  died    reason: null model
3  died    reason: null model
4  died    reason: null model
5  died    reason: null model
> pred <- rpart.predict(null.model, type="class", rules=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(null.model, type="class"), pred))
> print(rpart.predict(null.model, type="class", rules=TRUE)[1:5,])
  class                   
1  died because null model
2  died because null model
3  died because null model
4  died because null model
5  died because null model
> print(rpart.predict(null.model, type="class", rules=TRUE, nn=TRUE)[1:5,])
  class nn                   
1  died  1 because null model
2  died  1 because null model
3  died  1 because null model
4  died  1 because null model
5  died  1 because null model
> pred <- rpart.predict(null.model, type="class", rules=TRUE, nn=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(null.model, type="class"), pred))
> 
> print(rpart.predict(null.model, newdata=ptitanic[5:8,]))
         died survived
[1,] 0.618029 0.381971
[2,] 0.618029 0.381971
[3,] 0.618029 0.381971
[4,] 0.618029 0.381971
> print(rpart.predict(null.model, newdata=ptitanic[5:8,], nn=TRUE))
      died survived nn
1 0.618029 0.381971  1
2 0.618029 0.381971  1
3 0.618029 0.381971  1
4 0.618029 0.381971  1
> print(rpart.predict(null.model, newdata=ptitanic[5:8,], rules=TRUE))
      died survived                   
1 0.618029 0.381971 because null model
2 0.618029 0.381971 because null model
3 0.618029 0.381971 because null model
4 0.618029 0.381971 because null model
> print(rpart.predict(null.model, newdata=ptitanic[5:8,], nn=TRUE, rules=TRUE))
      died survived nn                   
1 0.618029 0.381971  1 because null model
2 0.618029 0.381971  1 because null model
3 0.618029 0.381971  1 because null model
4 0.618029 0.381971  1 because null model
> 
> cat0("\n=== \"varorder\" argument ===\n")

=== "varorder" argument ===
> data(ptitanic)
> survived <- rpart(survived ~ ., data=ptitanic)
> print(rpart.rules(survived))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> print(rpart.rules(survived, varorder="pclass"))
 survived                                                                                          
     0.05 when                        sex is   male & age <   9.5         & sibsp >= 3             
     0.11 when pclass is        3rd & sex is female & age >=         16.5 & sibsp <  3 & parch >= 4
     0.14 when pclass is        3rd & sex is female                       & sibsp >= 3             
     0.17 when                        sex is   male & age >=          9.5                          
     0.39 when pclass is        3rd & sex is female & age >=         27.5 & sibsp <  3 & parch <  4
     0.39 when pclass is        3rd & sex is female & age is 16.5 to 21.5 & sibsp <  3 & parch <  4
     0.62 when pclass is        3rd & sex is female & age is 21.5 to 27.5 & sibsp <  3 & parch <  4
     0.73 when pclass is        3rd & sex is female & age <  16.5         & sibsp <  3             
     0.89 when                        sex is   male & age <   9.5         & sibsp <  3             
     0.93 when pclass is 1st or 2nd & sex is female                                                
> print(rpart.rules(survived, varorder=c('pclass', 'sibsp')))
 survived                                                                                          
     0.05 when                        sibsp >= 3 & sex is   male & age <   9.5                     
     0.11 when pclass is        3rd & sibsp <  3 & sex is female & age >=         16.5 & parch >= 4
     0.14 when pclass is        3rd & sibsp >= 3 & sex is female                                   
     0.17 when                                     sex is   male & age >=          9.5             
     0.39 when pclass is        3rd & sibsp <  3 & sex is female & age >=         27.5 & parch <  4
     0.39 when pclass is        3rd & sibsp <  3 & sex is female & age is 16.5 to 21.5 & parch <  4
     0.62 when pclass is        3rd & sibsp <  3 & sex is female & age is 21.5 to 27.5 & parch <  4
     0.73 when pclass is        3rd & sibsp <  3 & sex is female & age <  16.5                     
     0.89 when                        sibsp <  3 & sex is   male & age <   9.5                     
     0.93 when pclass is 1st or 2nd              & sex is female                                   
> print(rpart.rules(survived, varorder=c('pclass', 'sex', 'sibsp')))
 survived                                                                                          
     0.05 when                        sex is   male & sibsp >= 3 & age <   9.5                     
     0.11 when pclass is        3rd & sex is female & sibsp <  3 & age >=         16.5 & parch >= 4
     0.14 when pclass is        3rd & sex is female & sibsp >= 3                                   
     0.17 when                        sex is   male              & age >=          9.5             
     0.39 when pclass is        3rd & sex is female & sibsp <  3 & age >=         27.5 & parch <  4
     0.39 when pclass is        3rd & sex is female & sibsp <  3 & age is 16.5 to 21.5 & parch <  4
     0.62 when pclass is        3rd & sex is female & sibsp <  3 & age is 21.5 to 27.5 & parch <  4
     0.73 when pclass is        3rd & sex is female & sibsp <  3 & age <  16.5                     
     0.89 when                        sex is   male & sibsp <  3 & age <   9.5                     
     0.93 when pclass is 1st or 2nd & sex is female                                                
> print(rpart.rules(survived, varorder="si")) # partial match on sibsp
 survived                                                                                          
     0.05 when sibsp >= 3 & sex is   male & age <   9.5                                            
     0.11 when sibsp <  3 & sex is female & age >=         16.5 & pclass is        3rd & parch >= 4
     0.14 when sibsp >= 3 & sex is female                       & pclass is        3rd             
     0.17 when              sex is   male & age >=          9.5                                    
     0.39 when sibsp <  3 & sex is female & age >=         27.5 & pclass is        3rd & parch <  4
     0.39 when sibsp <  3 & sex is female & age is 16.5 to 21.5 & pclass is        3rd & parch <  4
     0.62 when sibsp <  3 & sex is female & age is 21.5 to 27.5 & pclass is        3rd & parch <  4
     0.73 when sibsp <  3 & sex is female & age <  16.5         & pclass is        3rd             
     0.89 when sibsp <  3 & sex is   male & age <   9.5                                            
     0.93 when              sex is female                       & pclass is 1st or 2nd             
> stopifnot(identical(rpart.rules(survived, varorder=c('pclass', 'sex', 'sibsp')),
+                     rpart.rules(survived, varorder=c('pc',     'se',  'sib'))))
> cat0("\n===varorder error handling ===\n")

===varorder error handling ===
> options(warn=2) # treat warnings as errors
> expect.err(try(rpart.rules(survived, varorder='nonesuch')),      'varorder="nonesuch" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"')
Error : (converted from warning) varorder="nonesuch" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
Got error as expected from try(rpart.rules(survived, varorder = "nonesuch"))
> expect.err(try(rpart.rules(survived, varorder='p')),             'varorder="p" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"')
Error : (converted from warning) varorder="p" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
Got error as expected from try(rpart.rules(survived, varorder = "p"))
> expect.err(try(rpart.rules(survived, varorder='')),              'varorder="" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"')
Error : (converted from warning) varorder="" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
Got error as expected from try(rpart.rules(survived, varorder = ""))
> expect.err(try(rpart.rules(survived, varorder=c('pclass', ''))), 'varorder="" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"')
Error : (converted from warning) varorder="" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
Got error as expected from try(rpart.rules(survived, varorder = c("pclass", "")))
> expect.err(try(rpart.rules(survived, varorder=99)),              'is.character(varorder) is not TRUE')
Error in order.cols(rules, varorder, varnames, nrules.per.var) : 
  is.character(varorder) is not TRUE
Got error as expected from try(rpart.rules(survived, varorder = 99))
> expect.err(try(rpart.rules(survived, shadow.col="gray")),        "rpart.rules: ignoring argument 'shadow.col'")
Error : (converted from warning) rpart.rules: ignoring argument 'shadow.col'
Got error as expected from try(rpart.rules(survived, shadow.col = "gray"))
> options(warn=1) # print warnings as they occur
> rpart.rules(survived, varorder=c('nonesuch1','pcl','nonesuch2')) # will give warnings but still print the rules
Warning: varorder="nonesuch2" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
Warning: varorder="nonesuch1" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
 survived                                                                                          
     0.05 when                        sex is   male & age <   9.5         & sibsp >= 3             
     0.11 when pclass is        3rd & sex is female & age >=         16.5 & sibsp <  3 & parch >= 4
     0.14 when pclass is        3rd & sex is female                       & sibsp >= 3             
     0.17 when                        sex is   male & age >=          9.5                          
     0.39 when pclass is        3rd & sex is female & age >=         27.5 & sibsp <  3 & parch <  4
     0.39 when pclass is        3rd & sex is female & age is 16.5 to 21.5 & sibsp <  3 & parch <  4
     0.62 when pclass is        3rd & sex is female & age is 21.5 to 27.5 & sibsp <  3 & parch <  4
     0.73 when pclass is        3rd & sex is female & age <  16.5         & sibsp <  3             
     0.89 when                        sex is   male & age <   9.5         & sibsp <  3             
     0.93 when pclass is 1st or 2nd & sex is female                                                
> rpart.rules(survived, clip.facs=TRUE, cover=1, varorder=c('nonesuch1','pcl','nonesuch2')) # will give warnings but still print the rules
Warning: varorder="nonesuch2" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
Warning: varorder="nonesuch1" does not uniquely match one of: "sex" "pclass" "parch" "age" "sibsp"
 survived                                                                            cover
     0.05 when                male & age <   9.5         & sibsp >= 3                   2%
     0.11 when        3rd & female & age >=         16.5 & sibsp <  3 & parch >= 4      1%
     0.14 when        3rd & female                       & sibsp >= 3                   2%
     0.17 when                male & age >=          9.5                               61%
     0.39 when        3rd & female & age >=         27.5 & sibsp <  3 & parch <  4      3%
     0.39 when        3rd & female & age is 16.5 to 21.5 & sibsp <  3 & parch <  4      2%
     0.62 when        3rd & female & age is 21.5 to 27.5 & sibsp <  3 & parch <  4      6%
     0.73 when        3rd & female & age <  16.5         & sibsp <  3                   3%
     0.89 when                male & age <   9.5         & sibsp <  3                   2%
     0.93 when 1st or 2nd & female                                                     19%
> 
> par(old.par)
> 
> stopifnot(identical(options("width"), old.width))
> 
> # Survival model
> 
> library('rpart.plot')
> library('survival')
> rpart.surv <- rpart(Surv(age, sibsp == 1) ~ ., ptitanic)
> rpart.plot(rpart.surv, under=TRUE, main="survival model")
> print(rpart.rules(rpart.surv))
 Surv                                                            
 0.53 when parch <  1                             & sex is   male
 1.04 when parch >=      1 & pclass is        1st                
 1.18 when parch <  1                             & sex is female
 1.82 when parch >=      2 & pclass is 2nd or 3rd                
 4.35 when parch is 1 to 2 & pclass is 2nd or 3rd                
> print(rpart.rules(rpart.surv, cover=TRUE, clip.facs=TRUE, digits=4))
   Surv                                              cover
 0.5255 when parch <  1                   &   male     51%
 1.0387 when parch >=      1 &        1st               8%
 1.1823 when parch <  1                   & female     22%
 1.8168 when parch >=      2 & 2nd or 3rd               8%
 4.3502 when parch is 1 to 2 & 2nd or 3rd              11%
> options(warn=2) # treat warnings as errors
> expect.err(try(rpart.rules(rpart.surv, extra=1)), "(converted from warning) extra=1 is not supported by rpart.rules")
Error : (converted from warning) extra=1 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(rpart.rules(rpart.surv, extra = 1))
> expect.err(try(rpart.rules(rpart.surv, extra=2)), "(converted from warning) extra=2 is not supported by rpart.rules")
Error : (converted from warning) extra=2 is not supported by rpart.rules (although useable for plots)
Got error as expected from try(rpart.rules(rpart.surv, extra = 2))
> options(warn=1) # print warnings as they occur
> 
> owidth <- options("width")
> options(width=1e3)
> # rpart.predict (Survival model)
> print(predict(rpart.surv)[1:5])
       1        2        3        4        5 
1.182288 1.038697 1.038697 1.038697 1.038697 
> print(rpart.predict(rpart.surv)[1:5])
       1        2        3        4        5 
1.182288 1.038697 1.038697 1.038697 1.038697 
> stopifnot(identical(predict(rpart.surv), rpart.predict(rpart.surv)))
> print(rpart.predict(rpart.surv, rules=TRUE)[12:14,])
       Surv                                                               
12 1.182288 because parch <  1                             & sex is female
13 1.182288 because parch <  1                             & sex is female
14 1.182288 because parch <  1                             & sex is female
> print(head(rpart.predict(rpart.surv, rules=TRUE, when="")))
       Surv                                                               
1 1.1822877 because parch <  1                             & sex is female
2 1.0386969 because parch >=      1 & pclass is        1st                
3 1.0386969 because parch >=      1 & pclass is        1st                
4 1.0386969 because parch >=      1 & pclass is        1st                
5 1.0386969 because parch >=      1 & pclass is        1st                
6 0.5254767 because parch <  1                             & sex is   male
> print(head(rpart.predict(rpart.surv, rules=TRUE, when="WHEN")))
       Surv                                                               
1 1.1822877 because parch <  1                             & sex is female
2 1.0386969 because parch >=      1 & pclass is        1st                
3 1.0386969 because parch >=      1 & pclass is        1st                
4 1.0386969 because parch >=      1 & pclass is        1st                
5 1.0386969 because parch >=      1 & pclass is        1st                
6 0.5254767 because parch <  1                             & sex is   male
> print(head(rpart.predict(rpart.surv, rules=TRUE, when="", because="reason:")))
       Surv                                                               
1 1.1822877 reason: parch <  1                             & sex is female
2 1.0386969 reason: parch >=      1 & pclass is        1st                
3 1.0386969 reason: parch >=      1 & pclass is        1st                
4 1.0386969 reason: parch >=      1 & pclass is        1st                
5 1.0386969 reason: parch >=      1 & pclass is        1st                
6 0.5254767 reason: parch <  1                             & sex is   male
> print(head(rpart.predict(rpart.surv, rules=TRUE, because="")))
       Surv                                                       
1 1.1822877 parch <  1                             & sex is female
2 1.0386969 parch >=      1 & pclass is        1st                
3 1.0386969 parch >=      1 & pclass is        1st                
4 1.0386969 parch >=      1 & pclass is        1st                
5 1.0386969 parch >=      1 & pclass is        1st                
6 0.5254767 parch <  1                             & sex is   male
> # use max and not identical because rpart.predict(rpart.surv, rules=TRUE)[,1] doesn't have names
> stopifnot(max(abs(predict(rpart.surv) - rpart.predict(rpart.surv, rules=TRUE)[,1])) == 0)
> stopifnot(identical(as.numeric(predict(rpart.surv)), rpart.predict(rpart.surv, rules=TRUE)[,1]))
> print(rpart.predict(rpart.surv, nn=TRUE)[12:14,])
       Surv nn
12 1.182288  5
13 1.182288  5
14 1.182288  5
> stopifnot(max(abs(predict(rpart.surv) - rpart.predict(rpart.surv, nn=TRUE)[,1])) == 0)
> print(rpart.predict(rpart.surv, nn=TRUE, rules=TRUE)[12:14,])
       Surv nn                                                               
12 1.182288  5 because parch <  1                             & sex is female
13 1.182288  5 because parch <  1                             & sex is female
14 1.182288  5 because parch <  1                             & sex is female
> stopifnot(max(abs(predict(rpart.surv) - rpart.predict(rpart.surv, nn=TRUE, rules=TRUE)[,1])) == 0)
> print(rpart.predict(rpart.surv, newdata=ptitanic[5:8,]))
        5         6         7         8 
1.0386969 0.5254767 1.1822877 0.5254767 
> print(rpart.predict(rpart.surv, newdata=ptitanic[5:8,], nn=TRUE))
       Surv nn
5 1.0386969  6
6 0.5254767  4
7 1.1822877  5
8 0.5254767  4
> print(rpart.predict(rpart.surv, newdata=ptitanic[5:8,], rules=TRUE))
       Surv                                                     
5 1.0386969 because parch >=   1 & pclass is 1st                
6 0.5254767 because parch <  1                   & sex is   male
7 1.1822877 because parch <  1                   & sex is female
8 0.5254767 because parch <  1                   & sex is   male
> print(rpart.predict(rpart.surv, newdata=ptitanic[5:8,], nn=TRUE, rules=TRUE))
       Surv nn                                                     
5 1.0386969  6 because parch >=   1 & pclass is 1st                
6 0.5254767  4 because parch <  1                   & sex is   male
7 1.1822877  5 because parch <  1                   & sex is female
8 0.5254767  4 because parch <  1                   & sex is   male
> options(width=owidth$width)
> stopifnot(identical(options("width"), old.width))
> 
> #--- extra tests for rpart.predict ---
> 
> survived <- rpart(survived ~ ., data=ptitanic)
> print(rpart.rules(survived))
 survived                                                                                          
     0.05 when sex is   male & age <   9.5         & sibsp >= 3                                    
     0.11 when sex is female & age >=         16.5 & sibsp <  3 & pclass is        3rd & parch >= 4
     0.14 when sex is female                       & sibsp >= 3 & pclass is        3rd             
     0.17 when sex is   male & age >=          9.5                                                 
     0.39 when sex is female & age >=         27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.39 when sex is female & age is 16.5 to 21.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.62 when sex is female & age is 21.5 to 27.5 & sibsp <  3 & pclass is        3rd & parch <  4
     0.73 when sex is female & age <  16.5         & sibsp <  3 & pclass is        3rd             
     0.89 when sex is   male & age <   9.5         & sibsp <  3                                    
     0.93 when sex is female                                    & pclass is 1st or 2nd             
> owidth <- options(width=1e3)$width
> pred <- rpart.predict(survived, rules=TRUE)[2:4,]
> rownames(pred) <- NULL
> print(pred)
       died  survived                                                                                             
1 0.1111111 0.8888889 because sex is   male & age <   9.5         & sibsp <  3                                    
2 0.0680000 0.9320000 because sex is female                                    & pclass is 1st or 2nd             
3 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                 
> expect.err(try(rpart.predict(survived, rules=TRUE, style="tall")), "style = \"tall\" is not supported by rpart.predict")
Error : style = "tall" is not supported by rpart.predict
Got error as expected from try(rpart.predict(survived, rules = TRUE, style = "tall"))
> print(rpart.predict(survived, rules=TRUE, cover=TRUE)[2:4,])
       died  survived                                                                                                                   
2 0.1111111 0.8888889 because sex is   male  & age <   9.5           & sibsp  <    3                                                  2%
3 0.0680000 0.9320000 because sex is female                                            & pclass  is 1st or 2nd                       19%
4 0.8291457 0.1708543 because sex is   male  & age >=          9.5                                                                   61%
> print(rpart.predict(survived, rules=TRUE, roundint=FALSE)[2:4,])
       died  survived                                                                                                 
2 0.1111111 0.8888889 because sex is   male & age <   9.5         & sibsp <  2.5                                      
3 0.0680000 0.9320000 because sex is female                                      & pclass is 1st or 2nd               
4 0.8291457 0.1708543 because sex is   male & age >=          9.5                                                     
> print(rpart.predict(survived, rules=TRUE, roundint=FALSE, clip.facs=TRUE)[2:4,])
       died  survived                                                                                
2 0.1111111 0.8888889 because   male & age <   9.5         & sibsp <  2.5                            
3 0.0680000 0.9320000 because female                                      & 1st or 2nd               
4 0.8291457 0.1708543 because   male & age >=          9.5                                           
> print(rpart.predict(survived, rules=TRUE, varorder="pclass")[2:4,])
       died  survived                                                                                             
2 0.1111111 0.8888889 because                        sex is   male & age <   9.5         & sibsp <  3             
3 0.0680000 0.9320000 because pclass is 1st or 2nd & sex is female                                                
4 0.8291457 0.1708543 because                        sex is   male & age >=          9.5                          
> print(rpart.predict(survived, rules=TRUE, faclen=-2)[2:4,])
       died  survived                                                                                       
2 0.1111111 0.8888889 because sex is ma & age <   9.5         & sibsp <  3                                  
3 0.0680000 0.9320000 because sex is fe                                    & pclass is 1s or 2n             
4 0.8291457 0.1708543 because sex is ma & age >=          9.5                                               
> print(rpart.predict(survived, rules=TRUE, varlen=2)[2:4,])
       died  survived                                                                                 
2 0.1111111 0.8888889 because sx is   male & ag <   9.5         & sb <  3                             
3 0.0680000 0.9320000 because sx is female                                & pc is 1st or 2nd          
4 0.8291457 0.1708543 because sx is   male & ag >=          9.5                                       
> print(rpart.predict(survived, rules=TRUE, varlen=2)[2:4,], digits=2)
   died survived                                                                                 
2 0.111     0.89 because sx is   male & ag <   9.5         & sb <  3                             
3 0.068     0.93 because sx is female                                & pc is 1st or 2nd          
4 0.829     0.17 because sx is   male & ag >=          9.5                                       
> options(width=owidth)
> 
> set.seed(2020)
> ptit <- ptitanic
> ptit <- ptit[sample.int(nrow(ptitanic)),]
> ptit <- ptit[1:50,]
> pclass <- rpart(pclass~., data=ptit, control=list(cp=.05))
> print(rpart.rules(pclass))
 pclass  1st 2nd 3rd                                                    
    1st [.50 .10 .40] when age >= 17 & parch <  1 & survived is survived
    2nd [.43 .57 .00] when age >= 17 & parch >= 1                       
    3rd [.09 .09 .82] when age >= 17 & parch <  1 & survived is     died
    3rd [.00 .09 .91] when age <  17                                    
> options(digits=2)
> 
> print(predict(pclass)[1:5,])
       1st   2nd  3rd
1260 0.091 0.091 0.82
534  0.429 0.571 0.00
728  0.091 0.091 0.82
321  0.500 0.100 0.40
273  0.500 0.100 0.40
> stopifnot(all.equal(predict(pclass), as.matrix(rpart.predict(pclass, rules=TRUE)[,1:3])))
> stopifnot(max(abs(predict(pclass) - rpart.predict(pclass, rules=TRUE)[,1:3])) == 0)
> print(rpart.predict(pclass, nn=TRUE)[1:5,])
       1st   2nd  3rd nn
1260 0.091 0.091 0.82 11
534  0.429 0.571 0.00  4
728  0.091 0.091 0.82 11
321  0.500 0.100 0.40 10
273  0.500 0.100 0.40 10
> stopifnot(all.equal(predict(pclass), as.matrix(rpart.predict(pclass, nn=TRUE)[,1:3])))
> print(rpart.predict(pclass, nn=TRUE, rules=TRUE)[1:5,])
       1st   2nd  3rd nn                                                      
1260 0.091 0.091 0.82 11 because age >= 17 & parch <  1 & survived is     died
534  0.429 0.571 0.00  4 because age >= 17 & parch >= 1                       
728  0.091 0.091 0.82 11 because age >= 17 & parch <  1 & survived is     died
321  0.500 0.100 0.40 10 because age >= 17 & parch <  1 & survived is survived
273  0.500 0.100 0.40 10 because age >= 17 & parch <  1 & survived is survived
> stopifnot(all.equal(predict(pclass), as.matrix(rpart.predict(pclass, nn=TRUE, rules=TRUE)[,1:3])))
> 
> print(predict(pclass, type="class")[1:5])
1260  534  728  321  273 
 3rd  2nd  3rd  1st  1st 
Levels: 1st 2nd 3rd
> print(rpart.predict(pclass, type="class", rules=TRUE)[1:5,])
     class                                                      
1260   3rd because age >= 17 & parch <  1 & survived is     died
534    2nd because age >= 17 & parch >= 1                       
728    3rd because age >= 17 & parch <  1 & survived is     died
321    1st because age >= 17 & parch <  1 & survived is survived
273    1st because age >= 17 & parch <  1 & survived is survived
> pred <- rpart.predict(pclass, type="class", rules=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(pclass, type="class"), pred))
> print(rpart.predict(pclass, type="class", rules=TRUE)[1:5,])
     class                                                      
1260   3rd because age >= 17 & parch <  1 & survived is     died
534    2nd because age >= 17 & parch >= 1                       
728    3rd because age >= 17 & parch <  1 & survived is     died
321    1st because age >= 17 & parch <  1 & survived is survived
273    1st because age >= 17 & parch <  1 & survived is survived
> print(rpart.predict(pclass, type="class", rules=TRUE, nn=TRUE)[1:5,])
     class nn                                                      
1260   3rd 11 because age >= 17 & parch <  1 & survived is     died
534    2nd  4 because age >= 17 & parch >= 1                       
728    3rd 11 because age >= 17 & parch <  1 & survived is     died
321    1st 10 because age >= 17 & parch <  1 & survived is survived
273    1st 10 because age >= 17 & parch <  1 & survived is survived
> pred <- rpart.predict(pclass, type="class", rules=TRUE, nn=TRUE)
> names <- rownames(pred)
> pred <- pred[,1]
> names(pred) <- names
> stopifnot(all.equal(predict(pclass, type="class"), pred))
> 
> print(rpart.predict(pclass, newdata=ptit[5:8,]))
       1st   2nd  3rd
273  0.500 0.100 0.40
1188 0.000 0.091 0.91
170  0.091 0.091 0.82
945  0.091 0.091 0.82
> print(rpart.predict(pclass, newdata=ptit[5:8,], nn=TRUE))
       1st   2nd  3rd nn
273  0.500 0.100 0.40 10
1188 0.000 0.091 0.91  3
170  0.091 0.091 0.82 11
945  0.091 0.091 0.82 11
> print(rpart.predict(pclass, newdata=ptit[5:8,], rules=TRUE))
       1st   2nd  3rd                                                      
273  0.500 0.100 0.40 because age >= 17 & parch <  1 & survived is survived
1188 0.000 0.091 0.91 because age <  17                                    
170  0.091 0.091 0.82 because age >= 17 & parch <  1 & survived is     died
945  0.091 0.091 0.82 because age >= 17 & parch <  1 & survived is     died
> print(rpart.predict(pclass, newdata=ptit[5:8,], nn=TRUE, rules=TRUE))
       1st   2nd  3rd nn                                                      
273  0.500 0.100 0.40 10 because age >= 17 & parch <  1 & survived is survived
1188 0.000 0.091 0.91  3 because age <  17                                    
170  0.091 0.091 0.82 11 because age >= 17 & parch <  1 & survived is     died
945  0.091 0.091 0.82 11 because age >= 17 & parch <  1 & survived is     died
> 
> # caret package
> 
> library(caret)
Loading required package: lattice
Loading required package: ggplot2

Attaching package: 'caret'

The following object is masked from 'package:survival':

    cluster

> data(iris)
> Petal.Length <- rpart(Petal.Length ~ ., data=iris)
> print(rpart.rules(Petal.Length))
 Petal.Length                                                                    
          1.5 when Species is     setosa                                         
          3.6 when Species is versicolor & Petal.Width <  1.2                    
          4.4 when Species is versicolor & Petal.Width >= 1.2                    
          5.3 when Species is  virginica                      & Sepal.Length <  7
          6.3 when Species is  virginica                      & Sepal.Length >= 7
> 
> # caret train x,y interface
> 
> set.seed(2018)
> # note that caret converts factors to indicator columns before invoking rpart
> iPetal.Length <- 3 # index of Sepal.Length column
> caret.xy <- train(iris[,-iPetal.Length], iris[,iPetal.Length], method="rpart", tuneLength=4)
Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,  :
  There were missing values in resampled performance measures.
> rpart.rules(caret.xy$finalModel)
 .outcome                                               
      1.5 when Species is     setosa                    
      4.3 when Species is versicolor                    
      5.3 when Species is  virginica & Sepal.Length <  7
      6.3 when Species is  virginica & Sepal.Length >= 7
> rpart.rules(caret.xy$finalModel, roundint=FALSE)
 .outcome                                               
      1.5 when Species is     setosa                    
      4.3 when Species is versicolor                    
      5.3 when Species is  virginica & Sepal.Length <  7
      6.3 when Species is  virginica & Sepal.Length >= 7
> plotmo(Petal.Length, method="apartdep", do.par=2)
 plotmo grid:    Sepal.Length Sepal.Width Petal.Width Species
                          5.8           3         1.3  setosa
> rpart.plot(Petal.Length)
> 
> owidth <- options(width=1e3)$width
> head(rpart.predict(caret.xy$finalModel, rules=TRUE))
  .outcome                                                  
1      1.5 because Species is     setosa                    
2      1.5 because Species is     setosa                    
3      1.5 because Species is     setosa                    
4      1.5 because Species is     setosa                    
5      1.5 because Species is     setosa                    
6      1.5 because Species is     setosa                    
> # TODO fails: Error in eval(predvars, data, env) : object 'Speciesversicolor' not found
> # rpart.predict(caret.xy$finalModel, rules=TRUE, newdata=iris[50:52,])
> options(width=owidth)
> 
> plotmo(caret.xy, all2=TRUE, method="apartdep", do.par=2) # Warning: NA in singles, will plot all variables (as if all1=TRUE)
 plotmo grid:    Sepal.Length Sepal.Width Petal.Width Species
                          5.8           3         1.3  setosa
> rpart.plot(caret.xy$finalModel)
> 
> plotmo(caret.xy$finalModel, method="apartdep", do.par=2, all2=TRUE)
 plotmo grid:    Sepal.Length Sepal.Width Petal.Width Species
                          5.8           3         1.3  setosa
> rpart.plot(caret.xy$finalModel)
> 
> # caret train formula interface
> set.seed(2018)
> # note that caret converts factors to indicator columns before invoking rpart
> caret.form <- train(Petal.Length ~ ., data=iris, method="rpart", tuneLength=4)
Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo,  :
  There were missing values in resampled performance measures.
> rpart.rules(caret.form$finalModel)
 .outcome                                                                     
      1.5 when Petal.Width <  0.8                                             
      4.3 when Petal.Width >= 0.8 & Speciesversicolor is 1                    
      5.3 when Petal.Width >= 0.8 & Speciesversicolor is 0 & Sepal.Length <  7
      6.3 when Petal.Width >= 0.8 & Speciesversicolor is 0 & Sepal.Length >= 7
> rpart.rules(caret.form$finalModel, roundint=FALSE)
 .outcome                                                                       
      1.5 when Petal.Width <  0.8                                               
      4.3 when Petal.Width >= 0.8 & Speciesversicolor >= 0.5                    
      5.3 when Petal.Width >= 0.8 & Speciesversicolor <  0.5 & Sepal.Length <  7
      6.3 when Petal.Width >= 0.8 & Speciesversicolor <  0.5 & Sepal.Length >= 7
> plotmo(Petal.Length, method="apartdep", do.par=2)
 plotmo grid:    Sepal.Length Sepal.Width Petal.Width Species
                          5.8           3         1.3  setosa
> rpart.plot(Petal.Length)
> 
> owidth <- options(width=1e3)$width
> head(rpart.predict(caret.form$finalModel, rules=TRUE))
  .outcome                                                                        
1      1.5 because Petal.Width <  0.8                                             
2      1.5 because Petal.Width <  0.8                                             
3      1.5 because Petal.Width <  0.8                                             
4      1.5 because Petal.Width <  0.8                                             
5      1.5 because Petal.Width <  0.8                                             
6      1.5 because Petal.Width <  0.8                                             
> # TODO fails: Error in eval(predvars, data, env) : object 'Speciesversicolor' not found
> # rpart.predict(caret.form$finalModel, rules=TRUE, newdata=iris[50:52,])
> options(width=owidth)
> 
> plotmo(caret.form, all2=TRUE, method="apartdep", do.par=2) # Warning: NA in singles, will plot all variables (as if all1=TRUE)
Warning: NA in singles, will plot all variables (as if all1=TRUE)
 plotmo grid:    Sepal.Length Sepal.Width Petal.Width Species
                          5.8           3         1.3  setosa
> rpart.plot(caret.form$finalModel)
> 
> plotmo(caret.form$finalModel, method="apartdep", do.par=2, all2=TRUE)
 plotmo grid:    Sepal.Length Sepal.Width Petal.Width Speciesversicolor
                          5.8           3         1.3                 0
 Speciesvirginica
                0
> rpart.plot(caret.form$finalModel)
> 
> # Dates
> 
> par(mfrow=c(2, 2))
> set.seed(2018)
> xx <- 5 * rnorm(60)
> data <- data.frame(
+     yy  = c(1:30, 30:1) + xx,
+     xx  = xx,
+     dd = c(as.Date(paste0("2018-08-", 1:30)),
+            as.Date(paste0("2018-09-", 1:30))))
> print(head(data))
    yy    xx         dd
1 -1.1 -2.11 2018-08-01
2 -5.7 -7.75 2018-08-02
3  2.7 -0.32 2018-08-03
4  5.4  1.35 2018-08-04
5 13.7  8.68 2018-08-05
6  4.7 -1.32 2018-08-06
> datemod <- rpart(yy ~ ., data = data)
> print(rpart.rules(datemod))
   yy                                     
  4.2 when xx <  -2 & dd >=          17792
  9.9 when xx >= -2 & dd <  17755         
 12.0 when xx <  -2 & dd <  17792         
 12.3 when xx >= -2 & dd >=          17789
 21.0 when xx >= -2 & dd is 17755 to 17767
 27.0 when xx >= -2 & dd is 17767 to 17789
> plotmo(datemod, pt.col=2, do.par=FALSE)
 plotmo grid:    xx         dd
               -1.2 2018-08-31
> rpart.plot(datemod)
> par(old.par)
> 
> stopifnot(identical(options("width"), old.width))
> source("test.epilog.R")
