Print a Styled, Formatted Summary of the Decision Tree
print_tree.Rd
Displays a clean, perfectly aligned, color-coded summary of the tree's
current state, based on pre-calculated answer
attributes.
Details
An alternative approach to inspect internal attributes is to use the
data.tree
print() function with named attributes. See the example below.
Available attributes include:
rule : AND or OR for a node
name : The name of the node or leaf
question : The question for leaves
answer : The response provided for leaves or the calculated status of nodes
confidence : The confidence score provided for leaves (0 - 5) or the probability that the answer is correct (50% to 100%) for nodes
true_index : Influence the node has on the overall conclusion, if the response is TRUE
false_index : Influence the node has on the overall conclusion, if the response is FALSE
influence_if_true: Influence the leaf has on the overall conclusion, if the response is TRUE. This is the product of the ancestor values of true_index
influence_if_false: Influence the leaf has on the overall conclusion, if the response is FALSE. This is the product of the ancestor values of false_index
influence_index : The sum of influence_if_true and influence_if_false for each unanswered leaf
Examples
# Load a tree
ethical_tree <- load_tree_df(ethical)
# View the tree - initially all 'plain' as no answers
print_tree(ethical_tree)
#> Tree Rule Answer Confidence
#> Invest in Company X AND
#> |-- Financial Viability AND
#> | |-- Profitability and Growth Signals OR
#> | | |-- FIN1
#> | | |-- FIN2
#> | | `-- FIN3
#> | `-- Solvency and Stability AND
#> | |-- FIN4
#> | `-- FIN5
#> |-- Acceptable Environmental Stewardship OR
#> | |-- Has a Clean Current Record AND
#> | | |-- ENV1
#> | | |-- ENV2
#> | | `-- ENV3
#> | `-- Has a Credible Transition Pathway OR
#> | |-- ENV4
#> | |-- ENV5
#> | `-- ENV6
#> |-- Demonstrable Social Responsibility OR
#> | |-- Shows Excellent Internal Culture OR
#> | | |-- SOC1
#> | | |-- SOC2
#> | | |-- SOC3
#> | | `-- SOC4
#> | `-- Has a Positive External Impact AND
#> | |-- SOC5
#> | |-- SOC6
#> | `-- SOC7
#> `-- Strong Corporate Governance AND
#> |-- GOV1
#> |-- GOV2
#> |-- GOV3
#> |-- GOV4
#> `-- GOV5
# Set an answer for leaf 'FIN2' and update the tree
ethical_tree <- set_answer(ethical_tree, "FIN2", TRUE, 3)
#> Answer for leaf 'FIN2' set to: TRUE with confidence 3/5
ethical_tree <- update_tree(ethical_tree) # Crucial: update the tree to propagate answers
print_tree(ethical_tree)
#> Tree Rule Answer Confidence
#> Invest in Company X AND
#> |-- Financial Viability AND
#> | |-- Profitability and Growth Signals OR TRUE 80%
#> | | |-- FIN1
#> | | |-- FIN2 TRUE 3
#> | | `-- FIN3
#> | `-- Solvency and Stability AND
#> | |-- FIN4
#> | `-- FIN5
#> |-- Acceptable Environmental Stewardship OR
#> | |-- Has a Clean Current Record AND
#> | | |-- ENV1
#> | | |-- ENV2
#> | | `-- ENV3
#> | `-- Has a Credible Transition Pathway OR
#> | |-- ENV4
#> | |-- ENV5
#> | `-- ENV6
#> |-- Demonstrable Social Responsibility OR
#> | |-- Shows Excellent Internal Culture OR
#> | | |-- SOC1
#> | | |-- SOC2
#> | | |-- SOC3
#> | | `-- SOC4
#> | `-- Has a Positive External Impact AND
#> | |-- SOC5
#> | |-- SOC6
#> | `-- SOC7
#> `-- Strong Corporate Governance AND
#> |-- GOV1
#> |-- GOV2
#> |-- GOV3
#> |-- GOV4
#> `-- GOV5
# Alternative approach to inspect internal attributes using `data.tree::print()
# First, recalculate the internal indices
update_tree(ethical_tree)
#> levelName
#> 1 Invest in Company X
#> 2 ¦--Financial Viability
#> 3 ¦ ¦--Profitability and Growth Signals
#> 4 ¦ ¦ ¦--FIN1
#> 5 ¦ ¦ ¦--FIN2
#> 6 ¦ ¦ °--FIN3
#> 7 ¦ °--Solvency and Stability
#> 8 ¦ ¦--FIN4
#> 9 ¦ °--FIN5
#> 10 ¦--Acceptable Environmental Stewardship
#> 11 ¦ ¦--Has a Clean Current Record
#> 12 ¦ ¦ ¦--ENV1
#> 13 ¦ ¦ ¦--ENV2
#> 14 ¦ ¦ °--ENV3
#> 15 ¦ °--Has a Credible Transition Pathway
#> 16 ¦ ¦--ENV4
#> 17 ¦ ¦--ENV5
#> 18 ¦ °--ENV6
#> 19 ¦--Demonstrable Social Responsibility
#> 20 ¦ ¦--Shows Excellent Internal Culture
#> 21 ¦ ¦ ¦--SOC1
#> 22 ¦ ¦ ¦--SOC2
#> 23 ¦ ¦ ¦--SOC3
#> 24 ¦ ¦ °--SOC4
#> 25 ¦ °--Has a Positive External Impact
#> 26 ¦ ¦--SOC5
#> 27 ¦ ¦--SOC6
#> 28 ¦ °--SOC7
#> 29 °--Strong Corporate Governance
#> 30 ¦--GOV1
#> 31 ¦--GOV2
#> 32 ¦--GOV3
#> 33 ¦--GOV4
#> 34 °--GOV5
# Then print the tree, renaming column headings if required
print(ethical_tree, "rule", "true_index", "false_index", influence = "influence_index")
#> levelName rule true_index false_index
#> 1 Invest in Company X AND 0.2500000 1.0000000
#> 2 ¦--Financial Viability AND 1.0000000 1.0000000
#> 3 ¦ ¦--Profitability and Growth Signals OR 1.0000000 0.5000000
#> 4 ¦ ¦ ¦--FIN1 NA NA
#> 5 ¦ ¦ ¦--FIN2 NA NA
#> 6 ¦ ¦ °--FIN3 NA NA
#> 7 ¦ °--Solvency and Stability AND 0.5000000 1.0000000
#> 8 ¦ ¦--FIN4 NA NA
#> 9 ¦ °--FIN5 NA NA
#> 10 ¦--Acceptable Environmental Stewardship OR 1.0000000 0.5000000
#> 11 ¦ ¦--Has a Clean Current Record AND 0.3333333 1.0000000
#> 12 ¦ ¦ ¦--ENV1 NA NA
#> 13 ¦ ¦ ¦--ENV2 NA NA
#> 14 ¦ ¦ °--ENV3 NA NA
#> 15 ¦ °--Has a Credible Transition Pathway OR 1.0000000 0.3333333
#> 16 ¦ ¦--ENV4 NA NA
#> 17 ¦ ¦--ENV5 NA NA
#> 18 ¦ °--ENV6 NA NA
#> 19 ¦--Demonstrable Social Responsibility OR 1.0000000 0.5000000
#> 20 ¦ ¦--Shows Excellent Internal Culture OR 1.0000000 0.2500000
#> 21 ¦ ¦ ¦--SOC1 NA NA
#> 22 ¦ ¦ ¦--SOC2 NA NA
#> 23 ¦ ¦ ¦--SOC3 NA NA
#> 24 ¦ ¦ °--SOC4 NA NA
#> 25 ¦ °--Has a Positive External Impact AND 0.3333333 1.0000000
#> 26 ¦ ¦--SOC5 NA NA
#> 27 ¦ ¦--SOC6 NA NA
#> 28 ¦ °--SOC7 NA NA
#> 29 °--Strong Corporate Governance AND 0.2000000 1.0000000
#> 30 ¦--GOV1 NA NA
#> 31 ¦--GOV2 NA NA
#> 32 ¦--GOV3 NA NA
#> 33 ¦--GOV4 NA NA
#> 34 °--GOV5 NA NA
#> influence
#> 1 NA
#> 2 NA
#> 3 NA
#> 4 NA
#> 5 NA
#> 6 NA
#> 7 NA
#> 8 1.1250000
#> 9 1.1250000
#> 10 NA
#> 11 NA
#> 12 0.5833333
#> 13 0.5833333
#> 14 0.5833333
#> 15 NA
#> 16 0.4166667
#> 17 0.4166667
#> 18 0.4166667
#> 19 NA
#> 20 NA
#> 21 0.3750000
#> 22 0.3750000
#> 23 0.3750000
#> 24 0.3750000
#> 25 NA
#> 26 0.5833333
#> 27 0.5833333
#> 28 0.5833333
#> 29 NA
#> 30 1.0500000
#> 31 1.0500000
#> 32 1.0500000
#> 33 1.0500000
#> 34 1.0500000