Fetching rules from rpart using caret package I'm looking to find a way where I can extract rules from my decision tree. I have 8 predictors and they are all categorical variables and the response variable has three outputs A B and C.

I've developed the model and can also create decision tree but I'm having trouble in understanding the tree and be able to extract rules from my model.

The 8 predictors I have can have values a, b, c, d, e.

To give you an example, I want to extract rules from my decision tree like this:


Rule 1:  

Predictor 1 - a  
Predictor 2 - b  
Predictor 3 - c  
Predictor 4 - b  
Predictor 5 - a  
Predictor 6 - d  
Predictor 7 - e  
Predictor 8 - a  

Result: A

Rule 2: 

Predictor 1 - a  
Predictor 2 - b  
Predictor 3 - d  
Predictor 4 - b  
Predictor 5 - a  
Predictor 6 - d  
Predictor 7 - b  
Predictor 8 - a  

Result: A

Rule 3: 

Predictor 1 - a  
Predictor 2 - b  
Predictor 3 - d  
Predictor 4 - a  
Predictor 5 - a  
Predictor 6 - a  
Predictor 7 - b  
Predictor 8 - a  

Result: B  



and so on and so forth.

Is there a way I have rules from my decision tree like I explained above?

Here is the result of my model:


124 samples
8 predictors
3 classes: 'A', 'B', 'C' 


No pre-processing.
Resampling: Cross-Validated (10 fold, repeated 10 times).
Summary of sample sizes: 112, 111, 111, 112, 110, 112, ... 
Resampling results across tuning parameters:

  cp           Accuracy   Kappa  
  0.00000000  1.0000000  1.0000000  
  0.06329114  1.0000000  1.0000000  
  0.12658228  1.0000000  1.0000000  
  0.18987342  1.0000000  1.0000000  
  0.25316456  1.0000000  1.0000000  
  0.31645570  1.0000000  1.0000000  
  0.37974684  1.0000000  1.0000000  
  0.44303797  0.7261846  0.5696935  


Accuracy was used to select the optimal model using the largest value.
The final value used for the model was cp = 0.3797468.
n= 124 

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

1) root 124 79 A (0.3629032 0.2741935 0.3629032)    
  2) Activity4e&lt; 0.5 45  0 A (1.0000000 0.0000000 0.0000000) *  
    3) Activity4e&gt;=0.5 79 34 C (0.0000000 0.4303797 0.5696203)    
      6) Activity7c&gt;=0.5 34  0 B (0.0000000 1.0000000 0.0000000) *  
      7) Activity7c&lt; 0.5 45  0 C (0.0000000 0.0000000 1.0000000) *
You can use the rpart.rules function if you have used rpart to build the original tree.

library(rpart)
fit&lt;-rpart(Reliability~.,data=car.test.frame)
rpart.rules(fit)