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.
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 H3view_ltree() also accepts a preset number. By default it
draws an interactive plot with visNetwork, or a static plot if
visNetwork is not installed.
In the static plot latent nodes are circles and observed nodes are
coloured by type. The plot returns the ltree object.
simulate_ltree_data() can generate data from the preset
models; it needs the pcalg and graph packages.
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)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 H1Interactive viewing and file export are also available:
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 TRUEThe 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:
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.
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.