Extract layout data for plotting a CTA tree
cta_plot_data.RdReturns a pure-data list describing tree topology and layout coordinates.
No graphics are produced. Use this as input to plot.cta_tree
or to custom rendering code.
Layout algorithm: leaves receive sequential integer x-positions in
depth-first (left-to-right) order; internal nodes are centred over their
children. y = -depth so the root sits at the top.
Target-class enrichment: when target_class is supplied,
each terminal leaf is joined to cta_staging_table and
annotated with target-class counts, proportions, and a continuous display
color derived from the endpoint's rank among all endpoints by ascending
target-class proportion. Colors encode relative position within this
tree's endpoint distribution and do not imply clinical thresholds
or categories.
Usage
cta_plot_data(tree, target_class = NULL, class_labels = NULL,
digits = 1, endpoint_palette = NULL)Arguments
- tree
A
cta_treefromoda_cta_fit.- target_class
Integer (or
NULL); the class label treated as the target when enriching endpoint annotations.NULL(default) returns the structural layout only - no endpoint enrichment columns are added.- class_labels
Optional character vector of display names for class labels. Supply as a named vector, e.g.
c("0" = "Alive", "1" = "Deceased"), or as a positional vector (index \(k+1\) maps to class \(k\)).NULL(default) uses"class=C"formatting.- digits
Integer number of decimal places for percentage formatting in
endpoint_label. Default1.- endpoint_palette
Palette for endpoint fill colors, used only when
target_classis supplied. AcceptsNULL(default gradient: warm pink to pale yellow to soft green), a palettefunction(n)returningncolor strings, or a character vector of colors interpolated viacolorRampPalette.
Value
When target_class = NULL: a list with elements
nodes, edges, no_tree, has_weights.
When target_class is supplied: the same list plus
endpoints (a staging data.frame with layout coordinates) and
target_class_used (the integer target class used).
nodesA
data.framewith one row per node. Always-present columns:node_id(integer),parent_id(integer),depth(integer),x(numeric),y(numeric),leaf(logical),attribute(character;NAfor leaves),n_obs(integer),majority_class(integer),ess(numeric;NAfor leaves),label(character multi-line display text).Additional columns present when
target_classis supplied (values areNAon split nodes):endpoint_id(integer),stage(integer),target_class(integer),target_n(numeric),denominator(numeric),target_proportion(numeric; raw continuous proportion, not binned),target_rank(integer; ascending rank of proportion, ties broken byties.method = "first"),endpoint_fill_color(character hex color assigned by rank within this tree - does not imply clinical thresholds or categories),predicted_label(character),target_label(character),endpoint_label(character multi-line display text).edgesA
data.framewith one row per parent-to-child edge and columnsfrom_node_id(integer),to_node_id(integer),x0,y0,x1,y1(numeric centre-to-centre coordinates),label(character branch condition, e.g."V14<=0.5").endpoints(
target_classonly) Adata.framewith one row per endpoint, ordered by ascending stage. Columns include all staging fields fromcta_staging_tableplus layout coordinatesx,y, display columnspredicted_label,target_label,endpoint_fill_color, and integertarget_rank.target_class_used(
target_classonly) The integertarget_classargument used for enrichment.no_treeLogical;
TRUEfor leaf-only fits.has_weightsLogical;
TRUEwhen case weights are active.
Examples
data(mtcars)
X <- mtcars[, c("cyl", "disp", "hp", "wt")]
y <- as.integer(mtcars$am)
tree <- suppressMessages(
oda_cta_fit(X, y, mindenom = 5L, mc_iter = 500L, mc_seed = 42L,
loo = "off")
)
# Structural layout only
pd <- cta_plot_data(tree)
head(pd$nodes)
#> node_id parent_id depth x y leaf attribute n_obs majority_class ess
#> 1 1 0 1 1.5 -1 FALSE wt 32 0 81.78138
#> 2 2 1 2 1.0 -2 TRUE <NA> 14 1 NA
#> 3 3 1 2 2.0 -2 TRUE <NA> 18 0 NA
#> p_mc loo_p loo_status label
#> 1 0 NA OFF wt\nESS=81.8%\nn=32
#> 2 NA NA <NA> class=1\nn=14
#> 3 NA NA <NA> class=0\nn=18
pd$edges
#> from_node_id to_node_id x0 y0 x1 y1 label
#> 1 1 2 1.5 -1 1 -2 wt>3.18
#> 2 1 3 1.5 -1 2 -2 wt<=3.18
# Target-class enrichment
pd2 <- cta_plot_data(tree, target_class = 1L,
class_labels = c("0" = "Manual", "1" = "Auto"))
pd2$endpoints[, c("stage", "target_proportion", "endpoint_fill_color")]
#> stage target_proportion endpoint_fill_color
#> 1 1 0.05555556 #FFEBEE
#> 2 2 0.85714286 #E8F5E9