# Count participants with SDSES scores (ensures Sankey reflects analytic sample)
participant_flags <- lent_long %>%
dplyr::transmute(
id,
has_T1 = !is.na(T1SDSES4),
has_T2 = !is.na(T2SDSES4),
has_T3 = !is.na(T3SDSES4)
) %>%
dplyr::filter(has_T1 | has_T2 | has_T3)
n_total <- nrow(participant_flags)
n_T1 <- sum(participant_flags$has_T1)
n_T2 <- sum(participant_flags$has_T2)
n_T3 <- sum(participant_flags$has_T3)
# Calculate different participation patterns
n_T1_only <- sum(participant_flags$has_T1 & !participant_flags$has_T2 & !participant_flags$has_T3)
n_T2_only <- sum(!participant_flags$has_T1 & participant_flags$has_T2 & !participant_flags$has_T3)
n_T3_only <- sum(!participant_flags$has_T1 & !participant_flags$has_T2 & participant_flags$has_T3)
n_T1T2_only <- sum(participant_flags$has_T1 & participant_flags$has_T2 & !participant_flags$has_T3)
n_T1T3_only <- sum(participant_flags$has_T1 & !participant_flags$has_T2 & participant_flags$has_T3)
n_T2T3_only <- sum(!participant_flags$has_T1 & participant_flags$has_T2 & participant_flags$has_T3)
n_all_three <- sum(participant_flags$has_T1 & participant_flags$has_T2 & participant_flags$has_T3)
n_T1T3 <- sum(participant_flags$has_T1 & participant_flags$has_T3)
n_T1_T3 <- n_T1T3 # Alias for reporting chunk that expects this name
# Use the SDSES-analytic cohort as the baseline (removes T0-only participants)
n_baseline <- n_total
n_T2_drop <- n_T2_only + n_T1T2_only
n_T3_drop <- n_T3 - n_T1T3
sankey_nodes <- tibble::tibble(
name = c(
glue::glue("Полная выборка (≥ 1 волна)
N = {n_baseline}"),
glue::glue("T1 (начало поста)
N = {n_T1}"),
glue::glue("T2 (середина поста)
N = {n_T2}"),
glue::glue("T3 (конец поста)
N = {n_T3}"),
glue::glue("От и до
(T1 и T3)
N = {n_T1T3}"),
glue::glue("Выбыли после T1
N = {n_T1_only}"),
glue::glue("Выбыли после T2
N = {n_T2_drop}"),
glue::glue("T3 без T1
N = {n_T3_drop}")
)
)
# Calculate retention rates
retention_T1 <- round(100 * n_T1 / n_baseline, 1)
retention_T2 <- round(100 * n_T2 / n_baseline, 1)
retention_T3 <- round(100 * n_T3 / n_baseline, 1)
# Calculate flows from baseline and between waves, including attrition sinks
sankey_links <- tibble::tibble(
source = c(0, 0, 0, 1, 1, 1, 2, 2, 3, 3),
target = c(1, 2, 3, 2, 3, 5, 3, 6, 4, 7),
value = c(
n_T1, # Baseline to T1
n_T2_only + n_T2T3_only, # Baseline to T2 (skipped T1)
n_T3_only, # Baseline to T3 (only T3)
n_T1T2_only + n_all_three, # T1 to T2
n_T1T3_only, # T1 directly to T3 (missing T2)
n_T1_only, # Attrition after T1
n_T2T3_only + n_all_three, # T2 to T3
n_T2_drop, # Attrition after T2
n_T1T3, # T3 to full longitudinal analysis
n_T3_drop # T3 only without T1
),
label = c(
glue::glue("{retention_T1}% → T1 (N = {n_T1})"),
glue::glue("→ T2 без T1 (N = {n_T2_only + n_T2T3_only})"),
glue::glue("→ T3 без T1/T2 (N = {n_T3_only})"),
glue::glue("T1 → T2 (N = {n_T1T2_only + n_all_three})"),
glue::glue("T1 → T3 (без T2)
N = {n_T1T3_only}"),
glue::glue("Прекратили после T1
N = {n_T1_only}"),
glue::glue("T2 → T3 (N = {n_T2T3_only + n_all_three})"),
glue::glue("Прекратили после T2
N = {n_T2_drop}"),
glue::glue("T3 → Полный анализ
N = {n_T1T3}"),
glue::glue("T3 только без T1
N = {n_T3_drop}")
)
)
attrition_flow <- plotly::plot_ly(
type = "sankey",
height = 720,
arrangement = "snap",
node = list(
label = sankey_nodes$name,
pad = 20,
thickness = 30,
color = c(
rep(scales::alpha(orthodox_palette$primary, 0.85), 5),
rep(scales::alpha("#9E9E9E", 0.6), 3)
)
),
link = list(
source = sankey_links$source,
target = sankey_links$target,
value = sankey_links$value,
label = sankey_links$label,
color = c(
scales::alpha(orthodox_palette$accent, 0.7), # Baseline to T1
scales::alpha(orthodox_palette$accent, 0.4), # Baseline to T2 (skipped T1)
scales::alpha(orthodox_palette$accent, 0.4), # Baseline to T3 (only T3)
scales::alpha(orthodox_palette$accent, 0.6), # T1 to T2
scales::alpha(orthodox_palette$accent, 0.5), # T1 to T3 direct
scales::alpha("#9E9E9E", 0.6), # Dropout after T1
scales::alpha(orthodox_palette$accent, 0.6), # T2 to T3
scales::alpha("#9E9E9E", 0.6), # Dropout after T2
scales::alpha("#FFD700", 0.85), # T3 to Full (emphasized)
scales::alpha("#9E9E9E", 0.6) # T3 only without T1
)
)
) %>%
plotly::layout(
height = 780,
margin = list(l = 40, r = 40, t = 80, b = 110),
title = list(
text = glue::glue(
"Поток участников через волны измерений<br>",
"<sub>Сохранность от Полной выборки: T1 ({retention_T1}%), T2 ({retention_T2}%), T3 ({retention_T3}%)</sub>"
),
x = 0,
font = list(color = orthodox_palette$primary, size = 14)
),
annotations = list(
list(
showarrow = FALSE,
xref = "paper",
yref = "paper",
x = 0.5,
y = -0.12,
xanchor = "center",
yanchor = "top",
font = list(size = 10, color = "gray50")
)
)
)
attrition_flow <- htmlwidgets::onRender(
attrition_flow,
"
function(el, x) {
const desiredHeight = 720;
el.style.height = desiredHeight + 'px';
if (typeof Plotly !== 'undefined') {
Plotly.relayout(el, {height: desiredHeight});
}
}
"
)
attrition_flow