Specifying FFTs directly

Nathaniel Phillips

2017-05-29

There are two ways to define fast-and-frugal trees manually when using the FFTrees() function, either as a sentence using the my.tree argument, or as a dataframe using the tree.definitions argument.

my.tree

The first method is to use the my.tree argument, where my.tree is a sentence describing an FFT. When this argument is specified in FFTrees(), the FFT construction algorithm is bypassed and the algorithm will try to extract the specified FFT from the argument.

For example, let’s look at the heart disease dataset:

head(heartdisease)
##   age sex cp trestbps chol fbs     restecg thalach exang oldpeak slope ca
## 1  63   1 ta      145  233   1 hypertrophy     150     0     2.3  down  0
## 2  67   1  a      160  286   0 hypertrophy     108     1     1.5  flat  3
## 3  67   1  a      120  229   0 hypertrophy     129     1     2.6  flat  2
## 4  37   1 np      130  250   0      normal     187     0     3.5  down  0
## 5  41   0 aa      130  204   0 hypertrophy     172     0     1.4    up  0
## 6  56   1 aa      120  236   0      normal     178     0     0.8    up  0
##     thal diagnosis
## 1     fd         0
## 2 normal         1
## 3     rd         1
## 4 normal         0
## 5 normal         0
## 6 normal         0

Here’s how we could specify an FFT using the cues sex, age, and thal as a sentence:

my.tree = "If sex = 1, predict True.
           If age < 45, predict False. 
           If thal = [fd, normal], predict True. Otherwise, predict False"`

Here are some notes on specifying trees manually:

Now, let’s pass the my.tree argument to FFTrees()

# Specify an FFt verbally with the my.tree argument
my.heart.fft <- FFTrees(diagnosis ~.,
                        data = heartdisease,
                        my.tree = "If sex = 1, predict True.
                                   If age < 45, predict False. 
                                   If thal = [fd, normal], predict True. 
                                   Otherwise, predict False")

Let’s see how well our FFT did:

plot(my.heart.fft)

As you can see, this FFT is pretty terrible – it has a high sensitivity, but a terrible specificity.

Let’s see if we can come up with a better one using the cues thal, cp, and ca

# Specify an FFt verbally with the my.tree argument
my.heart.fft <- FFTrees(diagnosis ~.,
                        data = heartdisease,
                        my.tree = "If thal = [rd,fd], predict True.
                                   If cp != [a], predict False. 
                                   If ca > 1, predict True. 
                                   Otherwise, predict False")

# Plot 
plot(my.heart.fft)

This one looks much better!

tree.definitions

The second way to define one (or more) fast-and-frugal trees is with the tree.definitions argument. This argument should be a dataframe with the following structure:

##   tree nodes classes               cues directions    thresholds     exits
## 1    1     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 0;0;0;0.5
## 5    2     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 0;0;1;0.5
## 3    3     3   c;c;n         thal;cp;ca      =;=;>     rd,fd;a;0   0;1;0.5
## 2    4     3   c;c;n         thal;cp;ca      =;=;>     rd,fd;a;0   1;0;0.5
## 6    5     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 1;0;1;0.5
## 4    6     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 1;1;0;0.5
## 7    7     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 1;1;1;0.5

The dataframe should have 7 columns:

  1. tree: An indexing integer
  2. nodes: The number of nodes in the tree.

The following 5 columns define each node in an FFT, where nodes are separated by semi-colons ;:

  1. classes: The class of each node in the tree. c = character, n = numeric, i = integert.
  2. cues: The names of the cues
  3. directions: The direction of positive decisions for that cue. Even if a cue only has a negative exit branch, the direction should always be specified as if it was making a positive decision.
  4. thresholds: The decision threshold for the cue. For numeric cues, thresholds are single numbers. For factor cues, they are sets of factor values (separted by commas)
  5. exits: The exit direction for the cue. 0 = negative exit, 1 = positive exit, .5 = both a negative and a positive exit (only for the final node in a tree)

On can see examples of tree.definitions dataframes in an FFTrees object. For example, the definitions above can be obtained as follows:

# Create an FFTrees object
heart.fft <- FFTrees::FFTrees(diagnosis ~., 
                              data = heartdisease)

# Get the tree definitions
heart.tree.definitions <- heart.fft$tree.definitions

# Print the result
heart.tree.definitions
##   tree nodes classes               cues directions    thresholds     exits
## 1    1     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 0;0;0;0.5
## 5    2     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 0;0;1;0.5
## 3    3     3   c;c;n         thal;cp;ca      =;=;>     rd,fd;a;0   0;1;0.5
## 2    4     3   c;c;n         thal;cp;ca      =;=;>     rd,fd;a;0   1;0;0.5
## 6    5     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 1;0;1;0.5
## 4    6     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 1;1;0;0.5
## 7    7     4 c;c;n;n thal;cp;ca;thalach    =;=;>;< rd,fd;a;0;148 1;1;1;0.5

One can use tree.definitions dataframes created from FFTrees() as a template, make adjustments, and then feed the dataframe back into FFTrees() to create new, customized trees. Below, I’ll create definitions of two FFTs, then pass them to FFTrees(). The two FFTS can be described as follows.

# Define two trees
my.tree.definitions <- data.frame(tree = c(1, 2),
                                  nodes = c(2, 3),
                                  classes = c("c;n", "n;n;f"),
                                  cues = c("slope;ca", "chol;oldpeak;restecg"),
                                  directions = c("=;>", "<;>;!="),
                                  thresholds = c("down,up;1", "300;2;normal"),
                                  exits = c("0;.5", "1;1;.5"), 
                                  stringsAsFactors = FALSE)

Now, we can pass these trees to FFTrees() and view their resulting performance:

#Pass trees to FFTrees with tree.definitions
my.heart.fft <- FFTrees(diagnosis ~ .,
                        data = heartdisease,
                        tree.definitions = my.tree.definitions)

# Show summary statistics
my.heart.fft
## [1] "2 FFTs predicting diagnosis"
## [1] "FFT #1 {slope,ca} maximizes training wacc:"
##                    train
## cases       :n    303.00
## speed       :mcu    1.54
## frugality   :pci    0.89
## accuracy    :acc    0.57
## weighted    :wacc   0.53
## sensitivity :sens   0.12
## specificity :spec   0.95
# Plot Tree 2
plot(my.heart.fft, tree = 2)

Here is Tree #1: “If slope != [down, up], then predict False. If ca is greater than 1, predict True. Otherwise, predict False”

# Plot Tree 1
plot(my.heart.fft, tree = 1)

Here is Tree #2: “If chol < 300, then predict True If oldpeak is greater than 2, predict True. If restecg is not normal, then predict False. Otherwise, predict True”

# Plot Tree 2
plot(my.heart.fft, tree = 2)