| Title: | Structure Learning for Semiparametric/Latent Tree Models |
|---|---|
| Description: | Learns the structure of Gaussian latent tree models (LTMs), discrete LTMs, parametric mixed LTMs, and semiparametric tree models using information distances and a bottom-up tree reconstruction algorithm. Gaussian models and parametric mixed models with binary and conditionally Gaussian variables use Pearson-correlation-based distances. Discrete models use determinant-based distances and require the same number of observed states across variables. Semiparametric models accommodate continuous, binary, and ordinal variables, with latent correlations estimated under a Gaussian copula model using Kendall's tau and bridge functions. Utilities are provided for data simulation, tree visualization and export, and topology comparison using the Robinson-Foulds distance. The semiparametric framework allowing observed internal nodes is described in Zhou et al. (2026) <doi:10.1016/j.artint.2026.104499>. Earlier structure-learning methods assuming that all observed variables are leaf nodes are described for discrete models in Wang et al. (2017) <doi:10.4310/SII.2017.v10.n4.a11> and for parametric mixed models in Zhou et al. (2020) <https://jmlr.org/papers/v21/20-365.html>. |
| Authors: | Can Zhou [aut, cre], Shuai Wang [aut], Xiaofei Wang [aut], Jianhua Guo [aut], Key Program of the National Natural Science Foundation of China [fnd] (Grant No. 12431009), National Natural Science Foundation of China [fnd] (Grant No. 12171076, 12226003, 12601508), National Key Research and Development Program of China [fnd] (Grant No. 2020YFA0714100), Basic Research Program of Jiangsu [fnd] (Grant No. BK20230439), Alan Genz [ctb, cph] (TVPACK BVND), R Core Team [cph] (Brent_fmin), Mingze Huang [ctb, cph] (latentcor), Grace Yoon [ctb, cph] (latentcor), Christian Müller [ctb, cph] (latentcor), Irina Gaynanova [ctb, cph] (latentcor) |
| Maintainer: | Can Zhou <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.1 |
| Built: | 2026-12-05 02:58:48 UTC |
| Source: | https://github.com/cran/sltree |
edges_from_ltree extracts the edges from a latent tree structure
('ltree' object). vertices_from_ltree extracts the vertices from a
latent tree. The vertices are either node names or node IDs, depending on
the 'use_node_names' parameter.
edges_from_ltree(tree, use_node_names = FALSE) vertices_from_ltree(tree, use_node_names = FALSE)edges_from_ltree(tree, use_node_names = FALSE) vertices_from_ltree(tree, use_node_names = FALSE)
tree |
A 'ltree' object representing a latent tree structure. |
use_node_names |
Logical; return node names instead of node IDs. |
edges_from_ltree returns a data frame with two columns:
"nodes" and "parent", representing the edges. vertices_from_ltree
returns a character vector containing the vertices (nodes) of the latent tree.
Generate a tree by Poisson branching and optionally mark nodes of degree at least 3 as latent.
generate_latent_tree( variable_type = "Gaussian", latent_probability = 0.5, branching_rates = c(2, 2, 1, 0.5, 0.05) )generate_latent_tree( variable_type = "Gaussian", latent_probability = 0.5, branching_rates = c(2, 2, 1, 0.5, 0.05) )
variable_type |
Observed variable type: "Gaussian", "continuous", "binary", "ordinal", or "discrete"; use "mixed" to sample discrete, ordinal, or binary type independently for each observed node. |
latent_probability |
Probability that a node with degree >= 3 is set as a latent node. |
branching_rates |
Non-negative, finite Poisson branching means for successive levels. The last value is reused for deeper levels and must be less than 1. Generation stops with an error before exceeding 100,000 nodes. |
A three-column data frame nodes/type/parent
("" for the root), as used by simulate_ltree_data.
simulate_ltree_data, latent_tree_models
Classifies each column as binary, continuous or ordinal from its values.
infer_variable_types(x)infer_variable_types(x)
x |
A data frame or matrix where each column represents a variable. Must not contain NA values or non-numeric data. Binary variables should only contain 0 or 1. Ordinal variables should only contain consecutive integers starting from 0 (i.e., 0, 1, 2, ...). |
Binary: exactly 2 unique values that are 0 and 1
Ordinal: 3+ unique values that are consecutive integers starting from 0
Continuous: all other numeric variables
A character vector of types for all variables with possible values: "binary", "continuous", or "ordinal".
data <- data.frame( binary_var = c(0, 1, 0, 1), ordinal_var = c(0, 1, 2, 1), continuous_var = c(1.5, 2.3, 4.1, 3.7) ) infer_variable_types(data)data <- data.frame( binary_var = c(0, 1, 0, 1), ordinal_var = c(0, 1, 2, 1), continuous_var = c(1.5, 2.3, 4.1, 3.7) ) infer_variable_types(data)
information_distance returns a numeric information-distance matrix.
The input of information_distance is a by numeric
matrix or data frame, which is a sample of latent tree models (LTM).
Where is the sample size and is the number of observed
variables. The column names of the input will be assigned to column and
row names of the result (information distance matrix).
information_distance( data, type = c("semiparametric", "gaussian", "discrete", "mixed", "stm"), use_near_pd = FALSE, maxit = 10000 )information_distance( data, type = c("semiparametric", "gaussian", "discrete", "mixed", "stm"), use_near_pd = FALSE, maxit = 10000 )
data |
a |
type |
A character scalar selecting one of four model classes (matched
by |
use_near_pd |
Logical; for correlation-based distances, whether to project the estimated correlation matrix onto the nearest positive-definite correlation matrix before computing the distance. Requires Matrix. |
maxit |
Maximum number of iterations passed to
|
The information distance is additive along path on LTM.
Let be two nodes (random variables) in LTM. For Gaussian and
parametric mixed LTM (type = "gaussian" or type = "mixed"),
the information distance between and
is
where is the
Pearson correlation coefficient of and .
For discrete LTM (type = "discrete"), the information distance
is
where is the joint probability matrix and
and are diagonal marginal probability matrices.
All observed variables must have the same number of observed states
(at least two), so that every joint probability matrix is square.
State counts are determined from distinct values present in each column.
Singular joint matrices yield a distance capped at
-log(.Machine$double.xmin).
For semi-parametric tree models (type = "semiparametric",
the default; alias "stm"), the information distance is
where is the latent Pearson
correlation coefficient of and . The coefficient
is estimated with a bridge function that
connects the latent correlation to the population Kendall's
(see latent_correlation).
A numeric by information-distance matrix; its row and
column names are taken from the columns of data.
x <- data.frame(continuous = c(-1.8, -1.2, -0.4, 0.2, 0.8, 1.5), binary = c(0, 0, 0, 1, 1, 1), ordinal = c(0, 0, 1, 1, 2, 2)) information_distance(x, type = "semiparametric") categorical <- data.frame(a = c(2, 1, 0, 2, 1, 0), b = c(2, 2, 1, 1, 0, 0)) information_distance(categorical, type = "discrete")x <- data.frame(continuous = c(-1.8, -1.2, -0.4, 0.2, 0.8, 1.5), binary = c(0, 0, 0, 1, 1, 1), ordinal = c(0, 0, 1, 1, 2, 2)) information_distance(x, type = "semiparametric") categorical <- data.frame(a = c(2, 1, 0, 2, 1, 0), b = c(2, 2, 1, 1, 0, 0)) information_distance(categorical, type = "discrete")
Checks whether an object has class ltree.
is_ltree(tree)is_ltree(tree)
tree |
An R object. |
TRUE or FALSE.
Estimates the latent Pearson correlations of continuous, binary and ordinal variables under a Gaussian copula model.
latent_correlation(x, variable_types)latent_correlation(x, variable_types)
x |
A data frame or matrix where each column represents a variable. Must not contain NA values. Binary variables should contain only 0 and 1. Ordinal variables should contain consecutive integers starting from 0. |
variable_types |
A character vector specifying the type of each variable in x. Must contain values "continuous", "binary", or "ordinal". |
Each ordinal variable is split into binary indicators, one per cut point. Kendall's tau-a is computed for all pairs and mapped to the latent correlation by inverting the bridge functions of Fan et al. (2017). For ordinal variables the estimates are averaged over cut points.
A symmetric latent Pearson-correlation matrix with one row and column
per variable in x.
Fan, J., Liu, H., Ning, Y. and Zou, H. (2017). High dimensional semiparametric latent graphical model for mixed data. Journal of the Royal Statistical Society: Series B, 79(2), 405–421. doi:10.1111/rssb.12168
Huang, M., M\"uller, C. L. and Gaynanova, I. (2021). latentcor: An R package for estimating latent correlations from mixed data types. Journal of Open Source Software, 6(65), 3634. doi:10.21105/joss.03634
set.seed(1) data <- data.frame( continuous_var = rnorm(100), binary_var = rep(0:1, length.out = 100), ordinal_var = rep(0:3, length.out = 100) ) variable_types <- c("continuous", "binary", "ordinal") latent_correlation(data, variable_types)set.seed(1) data <- data.frame( continuous_var = rnorm(100), binary_var = rep(0:1, length.out = 100), ordinal_var = rep(0:3, length.out = 100) ) variable_types <- c("continuous", "binary", "ordinal") latent_correlation(data, variable_types)
Six fixed latent tree structures. Each tree is a three-column data frame
nodes/type/parent ("" is the root).
latent_tree_models(model_id = NULL)latent_tree_models(model_id = NULL)
model_id |
An integer from 1 to 6, or |
A three-column data frame, or a list named model1 through
model6 when model_id = NULL.
simulate_ltree_data, generate_latent_tree
m1 <- latent_tree_models(1) all_models <- latent_tree_models()m1 <- latent_tree_models(1) all_models <- latent_tree_models()
Estimates the information distance for the chosen model and recovers the
tree with sls. Semi-parametric, Gaussian and discrete trees
may have observed internal nodes; in the parametric mixed model every
observed node is a leaf.
learn_sltree( x, tau1 = 3, tau2 = 5, allow_observed_internal = NULL, epsilon_init = 0.1, step_size = 0.1, relation_tolerance = 0.1, use_near_pd = NULL, maxit = 10000, return_intermediate = FALSE, model = c("stm", "gaussian", "discrete", "mixed", "semiparametric") )learn_sltree( x, tau1 = 3, tau2 = 5, allow_observed_internal = NULL, epsilon_init = 0.1, step_size = 0.1, relation_tolerance = 0.1, use_near_pd = NULL, maxit = 10000, return_intermediate = FALSE, model = c("stm", "gaussian", "discrete", "mixed", "semiparametric") )
x |
Numeric data frame or matrix with at least three columns and two rows; it must contain no NA, infinite, or constant columns. |
tau1, tau2
|
distance thresholds for the SLS algorithm. |
allow_observed_internal |
Whether observed variables may be internal
nodes. |
epsilon_init, step_size, relation_tolerance
|
SLS tolerance-search parameters. |
use_near_pd |
Whether to project the correlation matrix with
|
maxit |
maximum number of iterations for |
return_intermediate |
logical; if |
model |
One of
|
If return_intermediate = FALSE, an ltree. Otherwise a
list with tree, correlation, distance,
variable_types,
model, allow_observed_internal, use_near_pd, and
run_time.
information_distance, sls,
latent_correlation, robinson_foulds_distance
set.seed(1234) n <- 400 h <- rnorm(n) gaussian_x <- as.data.frame(replicate(4, 0.8 * h + 0.6 * rnorm(n))) names(gaussian_x) <- paste0("V", 1:4) stm_x <- data.frame( V1 = exp(gaussian_x$V1), V2 = gaussian_x$V2^3, V3 = as.integer(gaussian_x$V3 > 0), V4 = findInterval(gaussian_x$V4, c(-0.5, 0.5)) ) est_stm <- learn_sltree(stm_x, model = "stm", use_near_pd = FALSE) est_gaussian <- learn_sltree(gaussian_x, model = "gaussian") h_binary <- rbinom(n, 1, 0.5) discrete_x <- as.data.frame(replicate(4, rbinom(n, 1, 0.1 + 0.8 * h_binary))) names(discrete_x) <- paste0("V", 1:4) est_discrete <- learn_sltree(discrete_x, model = "discrete") binary_and_gaussian_x <- data.frame( V1 = rnorm(n, mean = 2 * h_binary - 1, sd = 0.6), V2 = rnorm(n, mean = 2 * h_binary - 1, sd = 0.6), V3 = discrete_x$V3, V4 = discrete_x$V4 ) est_mixed <- learn_sltree(binary_and_gaussian_x, model = "mixed") edges_from_ltree(est_stm)set.seed(1234) n <- 400 h <- rnorm(n) gaussian_x <- as.data.frame(replicate(4, 0.8 * h + 0.6 * rnorm(n))) names(gaussian_x) <- paste0("V", 1:4) stm_x <- data.frame( V1 = exp(gaussian_x$V1), V2 = gaussian_x$V2^3, V3 = as.integer(gaussian_x$V3 > 0), V4 = findInterval(gaussian_x$V4, c(-0.5, 0.5)) ) est_stm <- learn_sltree(stm_x, model = "stm", use_near_pd = FALSE) est_gaussian <- learn_sltree(gaussian_x, model = "gaussian") h_binary <- rbinom(n, 1, 0.5) discrete_x <- as.data.frame(replicate(4, rbinom(n, 1, 0.1 + 0.8 * h_binary))) names(discrete_x) <- paste0("V", 1:4) est_discrete <- learn_sltree(discrete_x, model = "discrete") binary_and_gaussian_x <- data.frame( V1 = rnorm(n, mean = 2 * h_binary - 1, sd = 0.6), V2 = rnorm(n, mean = 2 * h_binary - 1, sd = 0.6), V3 = discrete_x$V3, V4 = discrete_x$V4 ) est_mixed <- learn_sltree(binary_and_gaussian_x, model = "mixed") edges_from_ltree(est_stm)
Creates an ltree object from the edge set of a directed latent tree.
ltree_from_edge(edge, node_types = NULL)ltree_from_edge(edge, node_types = NULL)
edge |
A two-column matrix or data frame, representing the edge set of a directed latent tree structure. The first column contains the node labels (with observed nodes labeled as "V1", "V2", ..., and latent nodes as "H1", "H2", ...). The second column contains the parent nodes, with the root node having an empty string as its parent. |
node_types |
A character vector of length equal to the number of nodes,
indicating node types. If named, names must be exactly the node identifiers
and values are reordered to match |
A list of class ltree with components
observed_nodes |
A character vector containing all observed nodes of the latent tree structure. |
latent_nodes |
A character vector containing all latent nodes of the latent tree structure. |
internal_nodes |
A character vector containing all internal nodes of the latent tree structure. |
node_metadata |
A data frame with 4 columns, where:
The first column is a character vector containing all
nodes of the latent tree model;
The second column records "latent" for latent
nodes and "observed_<type>" for observed nodes,
such as "observed_continuous",
"observed_binary", or "observed_ordinal";
The third column is an integer vector indicating the
degree of each node;
The fourth column is an integer vector indicating the
number of observed nodes among the child nodes of each
node. |
parent_by_node |
A character vector indicating the parent node of each
node. This is a named vector, where the names
attribute is all nodes.
"" means that the node has no parent node. |
children_by_node |
A named list in which each element is a set of child nodes of this node. |
observed_descendants |
A named list in which each element is a set of observed descendant nodes of this node. |
active_nodes |
A character vector indicating the active set in the current step of structure learning algorithm, that is, the nodes whose structure need to be explored in the current step. |
discovered_nodes |
A character vector containing the nodes whose structure has been found. |
distance |
A placeholder matrix for the observed nodes, with zero diagonal and all off-diagonal entries equal to one. These values are not information distances estimated from the edge list. |
node_types |
A named character vector with node identifiers
as names. Types are "continuous",
"Gaussian", "binary",
"ordinal", "count",
"categorical", "nominal",
"discrete", or "latent". |
node_names |
A named character vector indicate nodes name,
where its names attribute vector is nodes
labels.
|
The components active_nodes, discovered_nodes, and
distance are
used in structure learning algorithm.
edges <- data.frame( node = c("V1", "V2", "V3", "V4", "V5", "H1", "H2"), parent = c("H1", "V1", "H1", "H2", "H2", "", "H1"), stringsAsFactors = FALSE ) node_types <- rep(c("ordinal", "continuous", "latent"), c(2, 3, 2)) tree <- ltree_from_edge(edges, node_types)edges <- data.frame( node = c("V1", "V2", "V3", "V4", "V5", "H1", "H2"), parent = c("H1", "V1", "H1", "H2", "H2", "", "H1"), stringsAsFactors = FALSE ) node_types <- rep(c("ordinal", "continuous", "latent"), c(2, 3, 2)) tree <- ltree_from_edge(edges, node_types)
Draws a latent tree on the current graphics device. Latent nodes are circles and observed nodes are boxes coloured by type (continuous green, discrete amber). A legend of node types is drawn below the tree.
## S3 method for class 'ltree' plot( x, vertex.size = 24, edge.width = 2, vertex.label.cex = 0.9, layout = NULL, direction = c("LR", "RL", "UD", "DU"), vertex.color = NULL, vertex.frame.color = NULL, legend = TRUE, edge.args = list(), vertex.args = list(), label.args = list(), legend.args = list(), par.args = list(), ... )## S3 method for class 'ltree' plot( x, vertex.size = 24, edge.width = 2, vertex.label.cex = 0.9, layout = NULL, direction = c("LR", "RL", "UD", "DU"), vertex.color = NULL, vertex.frame.color = NULL, legend = TRUE, edge.args = list(), vertex.args = list(), label.args = list(), legend.args = list(), par.args = list(), ... )
x |
An |
vertex.size, edge.width, vertex.label.cex
|
Vertex size, edge width and
label size. Large trees are scaled down together with their labels, so
set label size with |
layout |
Optional two-column matrix of vertex coordinates. By default nodes are placed by a tree layout that avoids overlaps. |
direction |
Direction of the default tree layout: |
vertex.color, vertex.frame.color
|
Fill and border colours of the
vertices, given as one colour for all vertices, one colour per vertex (in
the order of |
legend |
Logical; whether to draw the node-type legend. Default
|
edge.args |
Named list of extra arguments for
|
vertex.args |
Named list of extra arguments for
|
label.args |
Named list of extra arguments for
|
legend.args |
Named list of extra arguments for
|
par.args |
Named list of graphical parameters set with
|
... |
Further arguments passed to |
Argument names follow igraph (vertex.*, edge.*) and base
graphics (main, xlab, col.main, ...). Other arguments
of the underlying graphics functions go in the *.args lists, which
replace the defaults; a NULL entry removes a default.
x, invisibly.
tree <- view_ltree(1, type = "summary") plot(tree) plot(tree, main = "Preset tree 1", direction = "UD", legend = FALSE) plot(tree, vertex.color = c(latent = "grey60"), vertex.frame.color = "black") plot(tree, edge.args = list(lty = 2), legend.args = list(x = "topright", title = "Node type"))tree <- view_ltree(1, type = "summary") plot(tree) plot(tree, main = "Preset tree 1", direction = "UD", legend = FALSE) plot(tree, vertex.color = c(latent = "grey60"), vertex.frame.color = "black") plot(tree, edge.args = list(lty = 2), legend.args = list(x = "topright", title = "Node type"))
Prints the nodes and edges of an ltree object.
## S3 method for class 'ltree' print(x, ...)## S3 method for class 'ltree' print(x, ...)
x |
An |
... |
Ignored. |
x, invisibly.
robinson_foulds_distance computes the Robinson-Foulds distance between two
undirected latent tree structures. The rf distance measures the
topological difference between trees. A distance of 0 indicates
identical tree structures (ignoring root position and hidden node names).
robinson_foulds_distance(reference_tree, estimated_tree)robinson_foulds_distance(reference_tree, estimated_tree)
reference_tree |
An 'ltree' object representing the reference tree. Must include 'observed_nodes', 'internal_nodes', and 'observed_descendants'. |
estimated_tree |
An 'ltree' object representing the estimated tree. Must include 'observed_nodes', 'internal_nodes', and 'observed_descendants'. |
This function compares two trees based on their topologies. The names of latent nodes and the root node do not affect the rf distance.
A non-negative numeric value giving the RF distance.
ltree_from_edge for constructing latent tree structures.
save_ltree writes an interactive HTML file with visNetwork and
EPS and PNG images with plot.ltree.
save_ltree( tree, file = "tree", html = TRUE, eps = TRUE, png = TRUE, width = 12, height = 7, plot_args = list(), png_args = list(), eps_args = list(), html_args = list(), ... )save_ltree( tree, file = "tree", html = TRUE, eps = TRUE, png = TRUE, width = 12, height = 7, plot_args = list(), png_args = list(), eps_args = list(), html_args = list(), ... )
tree |
A |
file |
Output path, preferably without an extension. By default
|
html, eps, png
|
Logical; which formats to write. All three default to
|
width, height
|
EPS/PNG size in inches (PNG is rendered at 150 dpi
unless |
plot_args |
Named list of extra arguments forwarded to
|
png_args |
Named list of extra arguments for
|
eps_args |
Named list of extra arguments for
|
html_args |
Named list of extra arguments for
|
... |
Extra arguments forwarded to the interactive visNetwork
viewer for the HTML; see the interactive options in
|
Invisibly, a named character vector of the files written.
model <- latent_tree_models(1) true_tree <- ltree_from_edge(model[, -2], setNames(model[, 2], model[, 1])) out_dir <- tempfile("sltree-example-") dir.create(out_dir) out_png_only <- file.path(out_dir, "png_only") save_ltree(true_tree, out_png_only, html = FALSE, eps = FALSE) out_print <- file.path(out_dir, "print") save_ltree(true_tree, out_print, html = FALSE, png_args = list(res = 300)) if(requireNamespace("visNetwork", quietly = TRUE)) { out_preset1 <- file.path(out_dir, "preset1") save_ltree(1, out_preset1, html_args = list(selfcontained = FALSE)) out_titled <- file.path(out_dir, "titled") save_ltree(true_tree, out_titled, plot_args = list(main = "True tree"), html_args = list(selfcontained = FALSE)) } # Clean up the files and directories created by this example unlink(out_dir, recursive = TRUE)model <- latent_tree_models(1) true_tree <- ltree_from_edge(model[, -2], setNames(model[, 2], model[, 1])) out_dir <- tempfile("sltree-example-") dir.create(out_dir) out_png_only <- file.path(out_dir, "png_only") save_ltree(true_tree, out_png_only, html = FALSE, eps = FALSE) out_print <- file.path(out_dir, "print") save_ltree(true_tree, out_print, html = FALSE, png_args = list(res = 300)) if(requireNamespace("visNetwork", quietly = TRUE)) { out_preset1 <- file.path(out_dir, "preset1") save_ltree(1, out_preset1, html_args = list(selfcontained = FALSE)) out_titled <- file.path(out_dir, "titled") save_ltree(true_tree, out_titled, plot_args = list(main = "True tree"), html_args = list(selfcontained = FALSE)) } # Clean up the files and directories created by this example unlink(out_dir, recursive = TRUE)
Assign edge correlations, generate latent Gaussian data along the tree, and transform observed variables according to their declared types.
simulate_ltree_data(n, model, rho_lower = 0.2, rho_upper = 0.8)simulate_ltree_data(n, model, rho_lower = 0.2, rho_upper = 0.8)
n |
Observed sample size (number of rows), an integer of at least 2. |
model |
Valid rooted latent tree model (three-column data frame, see
|
rho_lower, rho_upper
|
Bounds of the uniform distribution for edge correlations. |
A list with data, edge_correlations, and
gaussian_correlation.
generate_latent_tree, learn_sltree
Input all information distances between the observed variables,
sls outputs a latent tree structure, where leaf nodes are observed
variables and latent nodes are latent or observed variables.
The result has one root, leaf degree 1, and latent-node degree at least 3.
The root has an empty parent identifier.
sls( distance, allow_observed_internal = TRUE, epsilon_init = 0.1, epsilon_max = max(distance), tau1 = 3, tau2 = 5, step_size = 0.1, relation_tolerance = 0.1, node_types = NULL, node_names = NULL )sls( distance, allow_observed_internal = TRUE, epsilon_init = 0.1, epsilon_max = max(distance), tau1 = 3, tau2 = 5, step_size = 0.1, relation_tolerance = 0.1, node_types = NULL, node_names = NULL )
distance |
A finite, symmetric, non-negative numeric matrix with a zero diagonal and at least three rows/columns. |
allow_observed_internal |
A logical scalar indicates whether the observed
variables can be internal nodes. The default is |
epsilon_init, epsilon_max, step_size
|
Finite numeric scalars. Thresholds
must be non-negative and |
tau1 |
A numeric scalar. |
tau2 |
A numeric scalar. The greater the information distance, the
greater the error. All information distances greater than |
relation_tolerance |
A numeric scalar. The threshold for judging whether the differences of information distances are different. |
node_types |
An optional character vector of node display types.
Recognized continuous, Gaussian, and discrete-type labels are preserved;
unrecognized values are treated as |
node_names |
A character vector with one display name per node. |
The SLS algorithm is a bottom-up structure learning algorithm
based on an additive information distance.
The SLS algorithm determines the basic sibling pair by using
a prescribed threshold (i.e. argument epsilon).
For fixed values of the thresholds epsilon and tau2, the
algorithm may not produce a complete tree. To obtain a complete tree
structure, sls starts the value of the threshold
epsilon from epsilon_init. Then increase epsilon
(if epsilon is less than epsilon_max) with the step size
step_size until the SLS algorithm obtains a tree.
If no tree is obtained for values up to epsilon_max, sls
returns a latent class model.
Let be the set of observed nodes. For , let
be the information distance between and .
Let be the difference of information
distance.
Particularly, if the difference
for , we judge that is a sibling
pair.
Since a longer distance estimate is less accurate for a given number of
samples, not all estimated distances are reliable for structure learning.
The algorithm considers possible sibling pairs whose estimated
distances are controlled by two thresholds
tau1, tau2.
In particular, for each pair of nodes satisfied , the estimated difference was computed only for
node
. Furthermore, if the difference
for any and , we consider
to be a sibling pair.
sls returns a ltree object containing
the following components: observed_nodes, latent_nodes,
internal_nodes, node_metadata, parent_by_node,
children_by_node, observed_descendants, active_nodes,
discovered_nodes, distance, node_types, and
node_names.
See ltree_from_edge for more details about tree structure.
ltree_from_edge for more details about tree structure.
information_distance for getting information distance.
if (requireNamespace("pcalg", quietly = TRUE) && requireNamespace("graph", quietly = TRUE) && requireNamespace("Matrix", quietly = TRUE)) { set.seed(1) model <- latent_tree_models(4) data <- simulate_ltree_data(n = 5e4, model = model) x <- data$data variable_types <- infer_variable_types(x) distance <- information_distance(x, type = "semiparametric", use_near_pd = TRUE) est_tree <- sls(distance, tau1 = 3, tau2 = 5, node_types = variable_types, node_names = colnames(x)) edges_hat <- edges_from_ltree(est_tree) view_ltree(est_tree, "plot") if (requireNamespace("visNetwork", quietly = TRUE)) { widget <- view_ltree(est_tree, "interactive") if (interactive()) { print(widget) } } true_tree <- ltree_from_edge(model[, -2], setNames(model[, 2], model[, 1])) robinson_foulds_distance(true_tree, est_tree) }if (requireNamespace("pcalg", quietly = TRUE) && requireNamespace("graph", quietly = TRUE) && requireNamespace("Matrix", quietly = TRUE)) { set.seed(1) model <- latent_tree_models(4) data <- simulate_ltree_data(n = 5e4, model = model) x <- data$data variable_types <- infer_variable_types(x) distance <- information_distance(x, type = "semiparametric", use_near_pd = TRUE) est_tree <- sls(distance, tau1 = 3, tau2 = 5, node_types = variable_types, node_names = colnames(x)) edges_hat <- edges_from_ltree(est_tree) view_ltree(est_tree, "plot") if (requireNamespace("visNetwork", quietly = TRUE)) { widget <- view_ltree(est_tree, "interactive") if (interactive()) { print(widget) } } true_tree <- ltree_from_edge(model[, -2], setNames(model[, 2], model[, 1])) robinson_foulds_distance(true_tree, est_tree) }
Shows an ltree object or one of the six preset trees as an
interactive HTML widget, a static plot, or a printed summary.
view_ltree(tree, type = c("interactive", "plot", "summary"), ...)view_ltree(tree, type = c("interactive", "plot", "summary"), ...)
tree |
An |
type |
Display type: |
... |
Display options: see the section Interactive options for
|
For the interactive view, a visNetwork htmlwidget, which
is displayed when printed. Otherwise, the displayed ltree object
invisibly. A preset number is converted to an ltree before display.
direction"UD" (top down, default), "DU",
"LR", or "RL".
legend, legend.args
Whether to show the node-type
legend (default TRUE), and a named list of extra arguments for
visNetwork::visLegend (e.g. list(position = "left")).
vertex.color, vertex.frame.color
Fill and border
colours, in the same forms as for plot.ltree.
vertex.args, edge.args
Named lists of extra
arguments for visNetwork::visNodes and
visNetwork::visEdges (e.g. list(font = list(size = 20))
or list(width = 3, color = list(color = "black"))). Nested lists
are merged with the defaults.
layout"tree" (default; fixed Reingold-Tilford
coordinates) or "hierarchical" (visNetwork's own layout).
edge_style"curved" (default) or "straight".
level_sep, node_spacing, tree_spacing
Spacing
in pixels between depths, between siblings, and between subtrees
("hierarchical" only). Defaults 150, 130, and 60.
height, width
Widget size; defaults "97vh"
and "100%".
Passed to visNetwork::visNetwork, e.g.
main, submain, footer, background.
save_ltree, learn_sltree,
latent_tree_models
if(requireNamespace("visNetwork", quietly = TRUE)) { widget <- view_ltree(1, type = "interactive") if (interactive()) { print(widget) } widget2 <- view_ltree(1, direction = "LR", vertex.args = list(font = list(size = 20))) if (interactive()) { print(widget2) } } view_ltree(1, "plot") view_ltree(1, "plot", direction = "UD", main = "Preset tree 1") view_ltree(1, "summary")if(requireNamespace("visNetwork", quietly = TRUE)) { widget <- view_ltree(1, type = "interactive") if (interactive()) { print(widget) } widget2 <- view_ltree(1, direction = "LR", vertex.args = list(font = list(size = 20))) if (interactive()) { print(widget2) } } view_ltree(1, "plot") view_ltree(1, "plot", direction = "UD", main = "Preset tree 1") view_ltree(1, "summary")