Skip to contents

Extract rules from an rpart object. This can be useful, if the rules must be implemented in another system. The rules contain all the criteria for the binary splits of an rpart tree from the root node down to the specified leaf.

Usage

Rules(x, node = NULL, leafonly = FALSE)

Arguments

x

the rpart object to extract the rules from

node

integer vector, defining the nodes whose details are required.

leafonly

boolean, defining if only the rules leading to end nodes ("leafs") should be returned.

Details

The function builds upon the original function path.rpart, which is bulky in some situations.

Value

a list with the rules

frame

the frame of the rpart

ylevels

the y values of the node

ds.size

the size of the dataset

path

a list of character vecotrs containing the rules

Author

Andri Signorell <andri@signorell.net>

See also

Examples

r.rp <- FitMod(diabetes ~ ., data=d.pima, fitfn="rpart")
Rules(r.rp)
#> 
#>  Rule number: 1 
#> 
#>  Rule number: 2 
#>    glucose< 127.5
#> 
#>  Rule number: 4 [yval=neg cover=271 (35%) prob=0.08]
#>    glucose< 127.5
#>    age< 28.5
#> 
#>  Rule number: 5 
#>    glucose< 127.5
#>    age>=28.5
#> 
#>  Rule number: 10 [yval=neg cover=41 (5%) prob=0.05]
#>    glucose< 127.5
#>    age>=28.5
#>    mass< 26.35
#> 
#>  Rule number: 11 
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#> 
#>  Rule number: 22 [yval=neg cover=55 (7%) prob=0.18]
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose< 99.5
#> 
#>  Rule number: 23 
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#> 
#>  Rule number: 46 
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#> 
#>  Rule number: 92 [yval=neg cover=21 (3%) prob=0.19]
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#>    pedigree< 0.2
#> 
#>  Rule number: 93 
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#>    pedigree>=0.2
#> 
#>  Rule number: 186 
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#>    pedigree>=0.2
#>    pregnant>=1.5
#> 
#>  Rule number: 372 [yval=neg cover=40 (5%) prob=0.30]
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#>    pedigree>=0.2
#>    pregnant>=1.5
#>    pressure>=67
#> 
#>  Rule number: 373 [yval=pos cover=12 (2%) prob=0.75]
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#>    pedigree>=0.2
#>    pregnant>=1.5
#>    pressure< 67
#> 
#>  Rule number: 187 [yval=pos cover=11 (1%) prob=0.82]
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree< 0.561
#>    pedigree>=0.2
#>    pregnant< 1.5
#> 
#>  Rule number: 47 [yval=pos cover=34 (4%) prob=0.74]
#>    glucose< 127.5
#>    age>=28.5
#>    mass>=26.35
#>    glucose>=99.5
#>    pedigree>=0.561
#> 
#>  Rule number: 3 
#>    glucose>=127.5
#> 
#>  Rule number: 6 
#>    glucose>=127.5
#>    mass< 29.95
#> 
#>  Rule number: 12 [yval=neg cover=41 (5%) prob=0.15]
#>    glucose>=127.5
#>    mass< 29.95
#>    glucose< 145.5
#> 
#>  Rule number: 13 
#>    glucose>=127.5
#>    mass< 29.95
#>    glucose>=145.5
#> 
#>  Rule number: 26 [yval=neg cover=21 (3%) prob=0.38]
#>    glucose>=127.5
#>    mass< 29.95
#>    glucose>=145.5
#>    insulin< 14.5
#> 
#>  Rule number: 27 [yval=pos cover=14 (2%) prob=0.71]
#>    glucose>=127.5
#>    mass< 29.95
#>    glucose>=145.5
#>    insulin>=14.5
#> 
#>  Rule number: 7 
#>    glucose>=127.5
#>    mass>=29.95
#> 
#>  Rule number: 14 
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#> 
#>  Rule number: 28 
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#>    age< 30.5
#> 
#>  Rule number: 56 
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#>    age< 30.5
#>    pressure>=61
#> 
#>  Rule number: 112 [yval=neg cover=31 (4%) prob=0.23]
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#>    age< 30.5
#>    pressure>=61
#>    mass< 41.8
#> 
#>  Rule number: 113 [yval=pos cover=9 (1%) prob=0.67]
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#>    age< 30.5
#>    pressure>=61
#>    mass>=41.8
#> 
#>  Rule number: 57 [yval=pos cover=10 (1%) prob=1.00]
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#>    age< 30.5
#>    pressure< 61
#> 
#>  Rule number: 29 [yval=pos cover=65 (8%) prob=0.72]
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose< 157.5
#>    age>=30.5
#> 
#>  Rule number: 15 [yval=pos cover=92 (12%) prob=0.87]
#>    glucose>=127.5
#>    mass>=29.95
#>    glucose>=157.5
#>