Generates transition tables for sequential waves of data, showing how entities move between states over time. The function produces a series of tables (wave 1 to wave 2, wave 2 to wave 3, etc.) with formatted markdown output highlighting state stability and transitions.
Usage
margot_transition_table(
data,
state_var,
id_var,
wave_var,
waves = NULL,
state_names = NULL
)
Arguments
- data
A data frame containing the observations.
- state_var
The name of the state variable column in `data` as a string. This variable tracks the state changes to be analyzed.
- id_var
The name of the identifier variable column in `data` as a string. This variable distinguishes between different subjects or entities.
- wave_var
The name of the wave/time variable column in `data` as a string.
- waves
Optional vector of wave values to analyze. If NULL, all unique waves in the data are used.
- state_names
Optional vector of state names to replace the default state labels. If NULL, states will be labeled as "State 1", "State 2", etc.
Value
A list with the following components: - `tables`: A list where each element corresponds to a pair of consecutive waves, containing the markdown-formatted transition table - `waves`: A list of wave pairs that were compared - `explanation`: A single explanation that can be used for all tables
Quarto Usage
To use in a Quarto document, you can include the tables with custom captions:
“` # Get explanation “`r, results='asis' cat(transition_tables$explanation) “`
# First transition table “`r #| label: tbl-wave0-1 #| tbl-cap: !expr paste0("Transitions from Wave ", transition_tables$waves[[1]][1], " to Wave ", transition_tables$waves[[1]][2]) transition_tables$tables[[1]] “` “`
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
3 2 1 0
4 0 1 0
4 1 1 1
4 2 1 1
5 0 1 1
5 1 1 0
5 2 1 1")
# Get sequential transition tables between all waves
transition_tables <- margot_transition_table(df, "religion_believe_god", "id", "wave")
# In a standard R session:
cat(transition_tables$explanation)
cat("\n\n")
# Display first transition table (wave 0 to 1)
print(transition_tables$tables[[1]])
# In a Quarto document:
# ```{r, results='asis'}
# cat(transition_tables$explanation)
# ```
#
# ```{r}
# #| label: tbl-transition-wave0-wave1
# #| tbl-cap: "Transition Matrix From Wave 0 to Wave 1"
# transition_tables$tables[[1]]
# ```
} # }