Generates a transition table that describes movements and stability between states from one observation to the next. It formats the output as a markdown table, highlighting the number of entities remaining in the same state (diagonal) and those transitioning to different states (off-diagonal).
Arguments
- data
A data frame with columns `from` and `to` indicating the initial and subsequent states of entities, respectively, and a `Freq` column indicating the frequency of transitions.
- state_names
Optional; a vector of state names to replace the default state labels. If NULL, states will be labeled as "State 1", "State 2", etc., based on the unique values in `from` and `to` columns.
Value
A list with two elements: `explanation`, a character string explaining the table, and `table`, a markdown-formatted table of transitions. The diagonal entries (in bold) represent the count of entities that remained in their initial state, while the off-diagonal entries show the count of transitions between different states.
Examples
if (FALSE) { # \dontrun{
df <- read.table(header=TRUE, text="
id wave year_measured religion_believe_god
3 0 1 0
3 1 1 1
4 0 1 0
4 1 1 1
5 0 1 1
5 1 1 0")
transition_matrix <- create_transition_matrix(df, "religion_believe_god", "id")
# Assuming `transition_matrix` is a table with the transition counts between states
# First, convert `transition_matrix` to a dataframe suitable for `transition_table`
df_transition <- as.data.frame.matrix(transition_matrix)
df_transition$from <- rownames(df_transition)
long_df_transition <- tidyr::pivot_longer(df_transition, cols = -from, names_to = "to", values_to = "Freq")
transition_table_data <- transition_table(long_df_transition)
cat(transition_table_data$explanation)
cat("\n")
print(transition_table_data$table)
} # }