# 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< 0.5 45  0 A (1.0000000 0.0000000 0.0000000) *
3) Activity4e>=0.5 79 34 C (0.0000000 0.4303797 0.5696203)
6) Activity7c>=0.5 34  0 B (0.0000000 1.0000000 0.0000000) *
7) Activity7c< 0.5 45  0 C (0.0000000 0.0000000 1.0000000) * library(rpart)
{"ft_lang_label":"__label__en","ft_lang_prob":0.6686377,"math_prob":0.95156217,"size":2029,"snap":"2020-34-2020-40","text_gpt3_token_len":725,"char_repetition_ratio":0.24049383,"word_repetition_ratio":0.20923913,"special_character_ratio":0.4687038,"punctuation_ratio":0.18526316,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98955065,"pos_list":[0,1,2],"im_url_duplicate_count":[null,5,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-15T08:21:07Z\",\"WARC-Record-ID\":\"<urn:uuid:e48e7eaa-672a-437a-b823-2c4906c971bf>\",\"Content-Length\":\"148063\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:6668bc20-0a0c-4f11-8e6c-5ed1f3a33180>\",\"WARC-Concurrent-To\":\"<urn:uuid:1bb93e1f-0fbb-4211-bfef-a5f2c2f54cb9>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://datascience.stackexchange.com/questions/29599/fetching-rules-from-rpart-using-caret-package/40383\",\"WARC-Payload-Digest\":\"sha1:KOFDKVHPXOLB7LZRPELKCAYTT6JHLJXN\",\"WARC-Block-Digest\":\"sha1:GQW7RZOUCWB5YLS337SBMPNTCYDIC64G\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439740733.1_warc_CC-MAIN-20200815065105-20200815095105-00206.warc.gz\"}"}