Introduction to sltree

sltree learns latent tree structures from data with continuous, binary and ordinal variables. Latent correlations are estimated under a Gaussian copula, converted to information distances, and the SLS algorithm recovers the latent nodes and the tree topology from the bottom up.

library(sltree)

1. A true tree

latent_tree_models() contains six preset trees.

model <- latent_tree_models(1)
head(model)
#>    nodes              type parent
#> V1    V1   observed_binary     H1
#> V2    V2   observed_binary     H1
#> V3    V3 observed_Gaussian     H2
#> V4    V4 observed_Gaussian     H2
#> V5    V5 observed_Gaussian     H3
#> V6    V6 observed_Gaussian     H3

view_ltree() also accepts a preset number. By default it draws an interactive plot with visNetwork, or a static plot if visNetwork is not installed.

view_ltree(1)                        # direction = "LR" suits trees with many leaves

In the static plot latent nodes are circles and observed nodes are coloured by type. The plot returns the ltree object.

preset_tree <- view_ltree(1, "plot")

2. Simulating data

simulate_ltree_data() can generate data from the preset models; it needs the pcalg and graph packages.

set.seed(1234)
dat <- simulate_ltree_data(n = 1e5, model = model)

For a small reproducible example using only the package’s required dependencies, consider four observed variables with one common latent Gaussian parent. We generate 400 observations and transform two columns to binary and ordinal variables. The continuous transformations are monotone, so all four columns follow the semi-parametric model.

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)
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))
)
head(x)
#>           V1          V2 V3 V4
#> 1 0.18236563 -3.94440391  0  0
#> 2 1.27587870 -0.22770396  1  0
#> 3 1.84913336  0.58852318  1  2
#> 4 0.08926223 -0.49300478  0  0
#> 5 1.81079240  0.02280814  1  1
#> 6 1.64363714  0.66120197  1  1

true_edges <- data.frame(
  node = c(paste0("V", 1:4), "H1"),
  parent = c(rep("H1", 4), "")
)
true_types <- c("continuous", "continuous", "binary", "ordinal", "latent")
true_tree <- ltree_from_edge(true_edges, true_types)

3. Learning the tree

learn_sltree() computes the information distance and passes it to sls(). With the default model = "stm" it infers the variable types, estimates the latent correlations, projects them with nearPD when Matrix is available, and uses -log|rho| as the distance. Here we set use_near_pd = FALSE so the example also runs without Matrix. The intermediate results show the inferred variable types and estimated information distances.

fit <- learn_sltree(x, tau1 = 3, tau2 = 5, use_near_pd = FALSE,
                   return_intermediate = TRUE)
est_tree <- fit$tree
fit$variable_types
#> [1] "continuous" "continuous" "binary"     "ordinal"
round(fit$distance, 3)
#>       V1    V2    V3    V4
#> V1 0.000 0.419 0.413 0.401
#> V2 0.419 0.000 0.424 0.446
#> V3 0.413 0.424 0.000 0.429
#> V4 0.401 0.446 0.429 0.000
edges_from_ltree(est_tree)
#>   nodes parent
#> 1    V1     H1
#> 2    V2     H1
#> 3    V3     H1
#> 4    V4     H1
view_ltree(est_tree, "plot")

Interactive viewing and file export are also available:

view_ltree(est_tree)
save_ltree(est_tree, file.path(tempdir(), "est_tree"))  # PNG, EPS and HTML

The Gaussian, discrete and parametric mixed models use the same learning interface. Discrete variables must all have the same number of observed states; the parametric mixed model accepts binary and conditionally Gaussian variables.

tree_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)
tree_discrete <- learn_sltree(discrete_x, model = "discrete")
mixed_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
)
tree_mixed <- learn_sltree(mixed_x, model = "mixed")
c(stm = is_ltree(est_tree), gaussian = is_ltree(tree_gaussian),
  discrete = is_ltree(tree_discrete), mixed = is_ltree(tree_mixed))
#>      stm gaussian discrete    mixed 
#>     TRUE     TRUE     TRUE     TRUE

The stm, gaussian and discrete models allow observed variables to be internal nodes; the parametric mixed model restricts them to leaves. Only stm uses nearPD by default; set use_near_pd to change this.

The same steps can be run one at a time:

variable_types <- infer_variable_types(x)
correlation <- latent_correlation(x, variable_types)
distance <- -log(abs(correlation)); diag(distance) <- 0
tree_from_steps <- sls(distance, tau1 = 3, tau2 = 5,
                       node_types = variable_types, node_names = colnames(x))

4. Comparing with the true tree

The RF distance compares the observed-node splits and ignores latent-node names and the root position. Finite samples need not recover the exact true tree; a value of zero indicates complete recovery in this example.

robinson_foulds_distance(true_tree, est_tree)   # 0 if fully recovered
#> [1] 0

5. Observed internal nodes

allow_observed_internal controls whether observed variables may be internal nodes. In sls() it defaults to TRUE; in learn_sltree() the default depends on the model.