TheandorR package is designed to be flexible, allowingyou to define your decision tree structure in several different formatsdepending on your preference and source data. This guide explains thethree primary formats and the functions used to load them.
The examples below use theethical investment decisiontree, which is included as a dataset within this package.
This is often the most intuitive and human-readable format, as itsindented structure visually represents the tree’s hierarchy. It ishighly recommended for creating and managing trees in a text editor.
Required Fields:
name: The name of the node.nodes: A list of child nodes (this key is used insteadof the reserved wordchildren).rule : Required for parent nodes, can beAND orORquestion : Required for leaf nodesAn excerpt from a YAML file (ethical.yml) looks likethis:
name: Invest in Company Xrule: ANDnodes:-name: Financial Viabilityrule: ANDnodes:-name: Profitability and Growth Signalsrule: ORquestion:"Is the company showing strong signs of profitability?"nodes:-name: FIN1question:"Company demonstrates consistent, high revenue growth."# ... and so onTo load a tree from a YAML file, use theload_tree_yaml() function.
An excerpt from a JSON file (ethical.json) looks likethis:
{"name":"Invest in Company X","rule":"AND","nodes":[{"name":"Financial Viability","rule":"AND","nodes":[{"name":"Profitability and Growth Signals","rule":"OR","question":"Is the company showing strong signs of profitability?","nodes":[{"name":"FIN1","question":"Company demonstrates consistent, high revenue growth."},#...andsoonTo load a tree from a JSON file, use theload_tree_json() function.
This is a flat, tabular format common in spreadsheets and databases.The tree structure is defined by aparent column thatcontains theid of its parent node. This is also the formatof theethical dataset included in the package.
Required Fields:
id: A unique numeric ID for every node. The root musthaveid = 0.name: The name/code for the node.parent: Theid of the parent node. Theroot’s parent should beNA.rule : Required for parent nodes, can beAND orORquestion : Required for leaf nodesThe first few rows of theethical dataset look likethis:
| id | name | question | rule | parent |
|---|---|---|---|---|
| 0 | Invest in Company X | NA | AND | NA |
| 1 | Financial Viability | NA | AND | 0 |
| 2 | Acceptable Environmental Stewardship | NA | OR | 0 |
| 3 | Demonstrable Social Responsibility | NA | OR | 0 |
| 4 | Strong Corporate Governance | NA | AND | 0 |
| 5 | Profitability and Growth Signals | NA | OR | 1 |
| 6 | Solvency and Stability | NA | AND | 1 |
| 7 | FIN1 | Company demonstrates consistent, high revenuegrowth. | NA | 5 |
| 8 | FIN2 | Company maintains a high net profit margin for itsindustry. | NA | 5 |
| 9 | FIN3 | Company holds a dominant or rapidly growing marketshare. | NA | 5 |
To load this format, you can useload_tree_df() if youalready have a data frame, orload_tree_csv() to read itfrom a.csv file.
# Load the example 'ethical' data frame that comes with the packagedata(ethical)# Build the tree from the data frame objecttree_from_df<-load_tree_df(ethical)# The structure is identical to the one loaded from YAMLprint(tree_from_df$children[[1]]$name)# Prints "Financial Viability"#> [1] "Financial Viability"This is another flat-file format that is very robust. The hierarchyis defined by a singlepath column that contains the full,delimited path from the root to each node.
Required Fields:
path: A string with a character (like/)separating the names of the nodes in the hierarchy.question: Required for leaves, NA for parentnodes.rule: Required for parent nodes, NA for leaves.An excerpt from a CSV file in this format would look like this:
| path | rule |
|---|---|
| Invest in Company X | AND |
| Invest in Company X/Financial Viability | AND |
| Invest in Company X/Financial Viability/Profitabilityand Growth Signals | OR |
| Invest in Company X/Financial Viability/Profitabilityand Growth Signals/FIN1 | NA |
To load this format from a CSV file, use theload_tree_csv_path() function.
# This code shows how to load a path string CSV from the package's example datapath_csv_path<-system.file("extdata","ethical_path.csv",package ="andorR")tree_from_path<-load_tree_csv_path(path_csv_path)print(tree_from_path)| Format Name | Best For | Key Structure | Core Loading Function |
|---|---|---|---|
| Hierarchical | Manual creation, readability, version control(git) | Indented YAML or JSON file with anodeskey | load_tree_yaml() orload_tree_json() |
| Relational | Exporting from databases, spreadsheets (Excel) | A flat table withid andparent columns | load_tree_csv() |
| Path String | Programmatic generation, ensuring structuralintegrity | A flat table with a singlepathcolumn | load_tree_csv_path() |