Learn R Programming

stCEG (version 0.1.0)

create_event_tree: Create an Event Tree from a Dataset

Description

This function constructs an event tree from a given dataset based on user-specified categorical variables. The resulting tree is represented as a graph, where each unique combination of variable values corresponds to a distinct path in the tree.

Usage

create_event_tree(
  dataset,
  columns = seq_along(dataset),
  label_type = "both",
  level_separation = 1000,
  node_distance = 300
)

Value

A visNetwork object displaying the event tree.

Arguments

dataset

A data frame containing categorical variables.

columns

A vector of column indices or names specifying which variables to use for constructing the event tree.

label_type

A character string specifying how edge labels should be displayed. Options are "names" (variable names), "both" (variable names and counts). Default is "both".

level_separation

A numeric value indicating the spacing between levels in the visualization. Default is 1000.

node_distance

A numeric value specifying the horizontal distance between nodes in the visualization. Default is 300.

Details

The function follows these steps:

  1. Extracts the specified columns from the dataset.

  2. Determines unique values for each variable and generates state names dynamically.

  3. Constructs a directed acyclic graph (DAG) using igraph, where each state represents a unique combination of variable values.

  4. Computes transition counts between states.

  5. Uses visNetwork to generate an interactive visualization of the event tree.

Examples

Run this code
data <- homicides
event_tree <- create_event_tree(data, columns = c(1,2,4,5), "both")
event_tree

Run the code above in your browser using DataLab