if (FALSE) {
data("data_SRHS_long")
srhs <- data_SRHS_long
# Covariates
srhs$gender <- factor(
srhs$gender,
levels = c(1, 2),
labels = c("male", "female")
)
srhs$race <- factor(
srhs$race,
levels = c(1, 2, 3),
labels = c("white", "black", "others")
)
srhs$education <- factor(
srhs$education,
levels = 1:5,
labels = c(
"high school",
"general educational diploma",
"high school graduate",
"some college",
"college and above"
)
)
srhs$age_c <- srhs$age - mean(srhs$age)
# 1 = poor, ..., 5 = excellent
srhs$srhs1 <- 6L - as.integer(srhs$srhs)
# Consecutive numeric identifiers and ordering by subject and time
srhs$id <- as.integer(factor(srhs$id))
srhs <- srhs[order(srhs$id, srhs$t), ]
# Select a small reproducible balanced sample for a fast example
set.seed(2026)
n_example <- 2000L
example_ids <- sample(unique(srhs$id), n_example)
srhs_example <- srhs[srhs$id %in% example_ids, , drop = FALSE]
srhs_example$id <- match(srhs_example$id, example_ids)
srhs_example <- srhs_example[order(srhs_example$id, srhs_example$t), ]
# Example 1: gender and age affect both initial and transition probabilities
mod_cov <- mc_multinom(
srhs1 ~ gender + age_c,
data = srhs_example,
baseline = "central",
model_int = "dist1",
model_cov = "dist1",
output = TRUE)
print(mod_cov)
summary(mod_cov)
# Average initial and transition probabilities
colMeans(mod_cov$Piv)
round(apply(mod_cov$PI[, , , 2:max(srhs_example$t)], c(1, 2), mean),3)
# Example 2: covariates affect transitions only
mod_trans <- mc_multinom(
srhs1 ~ gender + age_c,
data = srhs_example,
baseline = "central",
model_int = "dist1",
model_cov = "dist1",
formula_init = srhs1 ~ 1)
print(mod_trans)
summary(mod_trans)
}
Run the code above in your browser using DataLab