source(here::here("common.R"))
fig_dir <- here::here("figures"); fs::dir_create(fig_dir)
sc <- read_rds(here::here("data/scores_all_sites.rds"))
cat_order <- c("Executive function","Reasoning","Spatial cognition",
"Language","Social cognition","Math","Reading")
build_pairs <- function(ds) {
sc |> filter(dataset == ds,
task_id %in% c(core_task_ids, "swr", "sre", "pa"),
!is.na(score), !is.na(age)) |>
group_by(task_id, task_label, task_category, user_id) |>
arrange(age) |> mutate(wave = row_number()) |> ungroup() |>
filter(wave <= 2) |>
group_by(task_id, task_label, task_category, user_id) |>
filter(n_distinct(wave) == 2) |>
summarise(t1 = score[wave == 1], t2 = score[wave == 2],
age = age[wave == 1], .groups = "drop") |>
group_by(task_id, task_label, task_category) |>
filter(n() >= 20) |>
mutate(score1 = pmin(pmax(t1, -6), 6), score2 = pmin(pmax(t2, -6), 6)) |>
ungroup() |>
mutate(task_category = factor(task_category, levels = cat_order),
facet = fct_reorder(task_label, as.integer(task_category)))
}
retest_fig <- function(pairs, title) {
labs_df <- pairs |> group_by(facet) |>
summarise(r = cor(score1, score2, use = "complete.obs"), n = n(),
.groups = "drop") |>
mutate(label = sprintf("r = %.2f\nN = %d", r, n))
ggplot(pairs, aes(x = score1, y = score2)) +
geom_abline(slope = 1, intercept = 0, lty = 3, color = "grey55") +
geom_point(aes(color = age), alpha = 0.55, size = 1.1) +
geom_smooth(method = "lm", se = FALSE, color = "black", linewidth = 0.7) +
geom_text(data = labs_df, aes(x = -Inf, y = Inf, label = label),
hjust = -0.12, vjust = 1.15, size = 3.6, lineheight = 0.95,
family = .levante_font) +
facet_wrap(~facet, scales = "free") +
scale_color_viridis_c(option = "plasma", end = 0.92, name = "Age") +
labs(x = "Score, time 1", y = "Score, time 2", title = title) +
theme(legend.position = "bottom",
strip.text = element_text(face = "bold"))
}