Skip to contents

This function performs a full, bottom-up recalculation of the decision tree's state. It takes the user-provided answers and confidences at the leaf level and propagates the logical outcomes (answer) and aggregate confidence scores up to the parent nodes based on their AND/OR rules.

Usage

calculate_tree(tree)

Arguments

tree

The data.tree object to be calculated. The function modifies this object directly.

Value

The modified tree object (returned invisibly).

Details

This function is one of three called by update_tree(), which does a full recalculation of the decision tree result and optimisation indices.

The function first resets the answer and confidence of all non-leaf nodes to NA to ensure a clean calculation.

It then uses a post-order traversal, which is critical as it guarantees that a parent node is only processed after all of its children have been processed.

The logical rules are applied with short-circuiting:

OR Nodes:

Become TRUE if any child is TRUE. Become FALSE only if all children are answered and none are TRUE.

AND Nodes:

Become FALSE if any child is FALSE. Become TRUE only if all children are answered and none are FALSE.

The confidence calculation is based on the confidences of the children that determined the outcome (e.g., only the TRUE children for a resolved OR node).

Examples

# Load the data
ethical_tree <- load_tree_df(ethical)

# Answer some questions
set_answer(ethical_tree, "FIN2", TRUE, 4)
#> Answer for leaf 'FIN2' set to: TRUE with confidence 4/5
set_answer(ethical_tree, "ENV2", TRUE, 3)
#> Answer for leaf 'ENV2' set to: TRUE with confidence 3/5
set_answer(ethical_tree, "SOC2", TRUE, 4)
#> Answer for leaf 'SOC2' set to: TRUE with confidence 4/5
set_answer(ethical_tree, "GOV2", FALSE, 1)
#> Answer for leaf 'GOV2' set to: FALSE with confidence 1/5

# Calculate the tree
ethical_tree <- calculate_tree(ethical_tree)

# View the result
print_tree(ethical_tree)
#> Tree                                             Rule      Answer      Confidence   
#> Invest in Company X                               AND       FALSE       60% 
#> |-- Financial Viability                           AND                    
#> |   |-- Profitability and Growth Signals          OR        TRUE        90% 
#> |   |   |-- FIN1                                                         
#> |   |   |-- FIN2                                            TRUE        4 
#> |   |   `-- FIN3                                                         
#> |   `-- Solvency and Stability                    AND                    
#> |       |-- FIN4                                                         
#> |       `-- FIN5                                                         
#> |-- Acceptable Environmental Stewardship          OR                     
#> |   |-- Has a Clean Current Record                AND                    
#> |   |   |-- ENV1                                                         
#> |   |   |-- ENV2                                            TRUE        3 
#> |   |   `-- ENV3                                                         
#> |   `-- Has a Credible Transition Pathway         OR                     
#> |       |-- ENV4                                                         
#> |       |-- ENV5                                                         
#> |       `-- ENV6                                                         
#> |-- Demonstrable Social Responsibility            OR        TRUE        90% 
#> |   |-- Shows Excellent Internal Culture          OR        TRUE        90% 
#> |   |   |-- SOC1                                                         
#> |   |   |-- SOC2                                            TRUE        4 
#> |   |   |-- SOC3                                                         
#> |   |   `-- SOC4                                                         
#> |   `-- Has a Positive External Impact            AND                    
#> |       |-- SOC5                                                         
#> |       |-- SOC6                                                         
#> |       `-- SOC7                                                         
#> `-- Strong Corporate Governance                   AND       FALSE       60% 
#>     |-- GOV1                                                             
#>     |-- GOV2                                                FALSE       1 
#>     |-- GOV3                                                             
#>     |-- GOV4                                                             
#>     `-- GOV5